patch-realtime 914 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156
  1. From:
  2. https://www.kernel.org/pub/linux/kernel/projects/rt/4.4/
  3. diff --git a/Documentation/hwlat_detector.txt b/Documentation/hwlat_detector.txt
  4. new file mode 100644
  5. index 000000000000..cb61516483d3
  6. --- /dev/null
  7. +++ b/Documentation/hwlat_detector.txt
  8. @@ -0,0 +1,64 @@
  9. +Introduction:
  10. +-------------
  11. +
  12. +The module hwlat_detector is a special purpose kernel module that is used to
  13. +detect large system latencies induced by the behavior of certain underlying
  14. +hardware or firmware, independent of Linux itself. The code was developed
  15. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  16. +however there is nothing x86 specific about this patchset. It was
  17. +originally written for use by the "RT" patch since the Real Time
  18. +kernel is highly latency sensitive.
  19. +
  20. +SMIs are usually not serviced by the Linux kernel, which typically does not
  21. +even know that they are occuring. SMIs are instead are set up by BIOS code
  22. +and are serviced by BIOS code, usually for "critical" events such as
  23. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  24. +other tasks and those tasks can spend an inordinate amount of time in the
  25. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  26. +you are trying to keep event service latencies down in the microsecond range.
  27. +
  28. +The hardware latency detector works by hogging all of the cpus for configurable
  29. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  30. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  31. +time when the polling was interrupted and since the machine is stopped and
  32. +interrupts turned off the only thing that could do that would be an SMI.
  33. +
  34. +Note that the SMI detector should *NEVER* be used in a production environment.
  35. +It is intended to be run manually to determine if the hardware platform has a
  36. +problem with long system firmware service routines.
  37. +
  38. +Usage:
  39. +------
  40. +
  41. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  42. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  43. +step required to start the hwlat_detector. It is possible to redefine the
  44. +threshold in microseconds (us) above which latency spikes will be taken
  45. +into account (parameter "threshold=").
  46. +
  47. +Example:
  48. +
  49. + # modprobe hwlat_detector enabled=1 threshold=100
  50. +
  51. +After the module is loaded, it creates a directory named "hwlat_detector" under
  52. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  53. +to have debugfs mounted, which might be on /sys/debug on your system.
  54. +
  55. +The /debug/hwlat_detector interface contains the following files:
  56. +
  57. +count - number of latency spikes observed since last reset
  58. +enable - a global enable/disable toggle (0/1), resets count
  59. +max - maximum hardware latency actually observed (usecs)
  60. +sample - a pipe from which to read current raw sample data
  61. + in the format <timestamp> <latency observed usecs>
  62. + (can be opened O_NONBLOCK for a single sample)
  63. +threshold - minimum latency value to be considered (usecs)
  64. +width - time period to sample with CPUs held (usecs)
  65. + must be less than the total window size (enforced)
  66. +window - total period of sampling, width being inside (usecs)
  67. +
  68. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  69. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  70. +observe any latencies that exceed the threshold (initially 100 usecs),
  71. +then we write to a global sample ring buffer of 8K samples, which is
  72. +consumed by reading from the "sample" (pipe) debugfs file interface.
  73. diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
  74. index 0e4102ae1a61..26b5f39d57a8 100644
  75. --- a/Documentation/kernel-parameters.txt
  76. +++ b/Documentation/kernel-parameters.txt
  77. @@ -1629,6 +1629,15 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
  78. ip= [IP_PNP]
  79. See Documentation/filesystems/nfs/nfsroot.txt.
  80. + irqaffinity= [SMP] Set the default irq affinity mask
  81. + Format:
  82. + <cpu number>,...,<cpu number>
  83. + or
  84. + <cpu number>-<cpu number>
  85. + (must be a positive range in ascending order)
  86. + or a mixture
  87. + <cpu number>,...,<cpu number>-<cpu number>
  88. +
  89. irqfixup [HW]
  90. When an interrupt is not handled search all handlers
  91. for it. Intended to get systems with badly broken
  92. diff --git a/Documentation/sysrq.txt b/Documentation/sysrq.txt
  93. index 13f5619b2203..f64d075ba647 100644
  94. --- a/Documentation/sysrq.txt
  95. +++ b/Documentation/sysrq.txt
  96. @@ -59,10 +59,17 @@ On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>,
  97. On other - If you know of the key combos for other architectures, please
  98. let me know so I can add them to this section.
  99. -On all - write a character to /proc/sysrq-trigger. e.g.:
  100. -
  101. +On all - write a character to /proc/sysrq-trigger, e.g.:
  102. echo t > /proc/sysrq-trigger
  103. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  104. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  105. + Send an ICMP echo request with this pattern plus the particular
  106. + SysRq command key. Example:
  107. + # ping -c1 -s57 -p0102030468
  108. + will trigger the SysRq-H (help) command.
  109. +
  110. +
  111. * What are the 'command' keys?
  112. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  113. 'b' - Will immediately reboot the system without syncing or unmounting
  114. diff --git a/Documentation/trace/histograms.txt b/Documentation/trace/histograms.txt
  115. new file mode 100644
  116. index 000000000000..6f2aeabf7faa
  117. --- /dev/null
  118. +++ b/Documentation/trace/histograms.txt
  119. @@ -0,0 +1,186 @@
  120. + Using the Linux Kernel Latency Histograms
  121. +
  122. +
  123. +This document gives a short explanation how to enable, configure and use
  124. +latency histograms. Latency histograms are primarily relevant in the
  125. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  126. +and are used in the quality management of the Linux real-time
  127. +capabilities.
  128. +
  129. +
  130. +* Purpose of latency histograms
  131. +
  132. +A latency histogram continuously accumulates the frequencies of latency
  133. +data. There are two types of histograms
  134. +- potential sources of latencies
  135. +- effective latencies
  136. +
  137. +
  138. +* Potential sources of latencies
  139. +
  140. +Potential sources of latencies are code segments where interrupts,
  141. +preemption or both are disabled (aka critical sections). To create
  142. +histograms of potential sources of latency, the kernel stores the time
  143. +stamp at the start of a critical section, determines the time elapsed
  144. +when the end of the section is reached, and increments the frequency
  145. +counter of that latency value - irrespective of whether any concurrently
  146. +running process is affected by latency or not.
  147. +- Configuration items (in the Kernel hacking/Tracers submenu)
  148. + CONFIG_INTERRUPT_OFF_LATENCY
  149. + CONFIG_PREEMPT_OFF_LATENCY
  150. +
  151. +
  152. +* Effective latencies
  153. +
  154. +Effective latencies are actually occuring during wakeup of a process. To
  155. +determine effective latencies, the kernel stores the time stamp when a
  156. +process is scheduled to be woken up, and determines the duration of the
  157. +wakeup time shortly before control is passed over to this process. Note
  158. +that the apparent latency in user space may be somewhat longer, since the
  159. +process may be interrupted after control is passed over to it but before
  160. +the execution in user space takes place. Simply measuring the interval
  161. +between enqueuing and wakeup may also not appropriate in cases when a
  162. +process is scheduled as a result of a timer expiration. The timer may have
  163. +missed its deadline, e.g. due to disabled interrupts, but this latency
  164. +would not be registered. Therefore, the offsets of missed timers are
  165. +recorded in a separate histogram. If both wakeup latency and missed timer
  166. +offsets are configured and enabled, a third histogram may be enabled that
  167. +records the overall latency as a sum of the timer latency, if any, and the
  168. +wakeup latency. This histogram is called "timerandwakeup".
  169. +- Configuration items (in the Kernel hacking/Tracers submenu)
  170. + CONFIG_WAKEUP_LATENCY
  171. + CONFIG_MISSED_TIMER_OFSETS
  172. +
  173. +
  174. +* Usage
  175. +
  176. +The interface to the administration of the latency histograms is located
  177. +in the debugfs file system. To mount it, either enter
  178. +
  179. +mount -t sysfs nodev /sys
  180. +mount -t debugfs nodev /sys/kernel/debug
  181. +
  182. +from shell command line level, or add
  183. +
  184. +nodev /sys sysfs defaults 0 0
  185. +nodev /sys/kernel/debug debugfs defaults 0 0
  186. +
  187. +to the file /etc/fstab. All latency histogram related files are then
  188. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  189. +particular histogram type is enabled by writing non-zero to the related
  190. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  191. +Select "preemptirqsoff" for the histograms of potential sources of
  192. +latencies and "wakeup" for histograms of effective latencies etc. The
  193. +histogram data - one per CPU - are available in the files
  194. +
  195. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  196. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  197. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  198. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  199. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  200. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  201. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  202. +
  203. +The histograms are reset by writing non-zero to the file "reset" in a
  204. +particular latency directory. To reset all latency data, use
  205. +
  206. +#!/bin/sh
  207. +
  208. +TRACINGDIR=/sys/kernel/debug/tracing
  209. +HISTDIR=$TRACINGDIR/latency_hist
  210. +
  211. +if test -d $HISTDIR
  212. +then
  213. + cd $HISTDIR
  214. + for i in `find . | grep /reset$`
  215. + do
  216. + echo 1 >$i
  217. + done
  218. +fi
  219. +
  220. +
  221. +* Data format
  222. +
  223. +Latency data are stored with a resolution of one microsecond. The
  224. +maximum latency is 10,240 microseconds. The data are only valid, if the
  225. +overflow register is empty. Every output line contains the latency in
  226. +microseconds in the first row and the number of samples in the second
  227. +row. To display only lines with a positive latency count, use, for
  228. +example,
  229. +
  230. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  231. +
  232. +#Minimum latency: 0 microseconds.
  233. +#Average latency: 0 microseconds.
  234. +#Maximum latency: 25 microseconds.
  235. +#Total samples: 3104770694
  236. +#There are 0 samples greater or equal than 10240 microseconds
  237. +#usecs samples
  238. + 0 2984486876
  239. + 1 49843506
  240. + 2 58219047
  241. + 3 5348126
  242. + 4 2187960
  243. + 5 3388262
  244. + 6 959289
  245. + 7 208294
  246. + 8 40420
  247. + 9 4485
  248. + 10 14918
  249. + 11 18340
  250. + 12 25052
  251. + 13 19455
  252. + 14 5602
  253. + 15 969
  254. + 16 47
  255. + 17 18
  256. + 18 14
  257. + 19 1
  258. + 20 3
  259. + 21 2
  260. + 22 5
  261. + 23 2
  262. + 25 1
  263. +
  264. +
  265. +* Wakeup latency of a selected process
  266. +
  267. +To only collect wakeup latency data of a particular process, write the
  268. +PID of the requested process to
  269. +
  270. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  271. +
  272. +PIDs are not considered, if this variable is set to 0.
  273. +
  274. +
  275. +* Details of the process with the highest wakeup latency so far
  276. +
  277. +Selected data of the process that suffered from the highest wakeup
  278. +latency that occurred in a particular CPU are available in the file
  279. +
  280. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  281. +
  282. +In addition, other relevant system data at the time when the
  283. +latency occurred are given.
  284. +
  285. +The format of the data is (all in one line):
  286. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  287. +<- <PID> <Priority> <Command> <Timestamp>
  288. +
  289. +The value of <Timeroffset> is only relevant in the combined timer
  290. +and wakeup latency recording. In the wakeup recording, it is
  291. +always 0, in the missed_timer_offsets recording, it is the same
  292. +as <Latency>.
  293. +
  294. +When retrospectively searching for the origin of a latency and
  295. +tracing was not enabled, it may be helpful to know the name and
  296. +some basic data of the task that (finally) was switching to the
  297. +late real-tlme task. In addition to the victim's data, also the
  298. +data of the possible culprit are therefore displayed after the
  299. +"<-" symbol.
  300. +
  301. +Finally, the timestamp of the time when the latency occurred
  302. +in <seconds>.<microseconds> after the most recent system boot
  303. +is provided.
  304. +
  305. +These data are also reset when the wakeup histogram is reset.
  306. diff --git a/Makefile b/Makefile
  307. index 695c64ec160c..45b60fceb84b 100644
  308. --- a/Makefile
  309. +++ b/Makefile
  310. @@ -768,6 +768,9 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=strict-prototypes)
  311. # Prohibit date/time macros, which would make the build non-deterministic
  312. KBUILD_CFLAGS += $(call cc-option,-Werror=date-time)
  313. +# enforce correct pointer usage
  314. +KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
  315. +
  316. # use the deterministic mode of AR if available
  317. KBUILD_ARFLAGS := $(call ar-option,D)
  318. diff --git a/arch/Kconfig b/arch/Kconfig
  319. index 4e949e58b192..3b26d76933fb 100644
  320. --- a/arch/Kconfig
  321. +++ b/arch/Kconfig
  322. @@ -9,6 +9,7 @@ config OPROFILE
  323. tristate "OProfile system profiling"
  324. depends on PROFILING
  325. depends on HAVE_OPROFILE
  326. + depends on !PREEMPT_RT_FULL
  327. select RING_BUFFER
  328. select RING_BUFFER_ALLOW_SWAP
  329. help
  330. @@ -52,6 +53,7 @@ config KPROBES
  331. config JUMP_LABEL
  332. bool "Optimize very unlikely/likely branches"
  333. depends on HAVE_ARCH_JUMP_LABEL
  334. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  335. help
  336. This option enables a transparent branch optimization that
  337. makes certain almost-always-true or almost-always-false branch
  338. diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
  339. index 34e1569a11ee..79c4603e9453 100644
  340. --- a/arch/arm/Kconfig
  341. +++ b/arch/arm/Kconfig
  342. @@ -33,7 +33,7 @@ config ARM
  343. select HARDIRQS_SW_RESEND
  344. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  345. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  346. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32
  347. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && !PREEMPT_RT_BASE
  348. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32
  349. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  350. select HAVE_ARCH_TRACEHOOK
  351. @@ -68,6 +68,7 @@ config ARM
  352. select HAVE_PERF_EVENTS
  353. select HAVE_PERF_REGS
  354. select HAVE_PERF_USER_STACK_DUMP
  355. + select HAVE_PREEMPT_LAZY
  356. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  357. select HAVE_REGS_AND_STACK_ACCESS_API
  358. select HAVE_SYSCALL_TRACEPOINTS
  359. diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h
  360. index 12ebfcc1d539..c962084605bc 100644
  361. --- a/arch/arm/include/asm/switch_to.h
  362. +++ b/arch/arm/include/asm/switch_to.h
  363. @@ -3,6 +3,13 @@
  364. #include <linux/thread_info.h>
  365. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  366. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  367. +#else
  368. +static inline void
  369. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  370. +#endif
  371. +
  372. /*
  373. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  374. * during a TLB maintenance operation, so execute an inner-shareable dsb
  375. @@ -25,6 +32,7 @@ extern struct task_struct *__switch_to(struct task_struct *, struct thread_info
  376. #define switch_to(prev,next,last) \
  377. do { \
  378. __complete_pending_tlbi(); \
  379. + switch_kmaps(prev, next); \
  380. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  381. } while (0)
  382. diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
  383. index 776757d1604a..1f36a4eccc72 100644
  384. --- a/arch/arm/include/asm/thread_info.h
  385. +++ b/arch/arm/include/asm/thread_info.h
  386. @@ -49,6 +49,7 @@ struct cpu_context_save {
  387. struct thread_info {
  388. unsigned long flags; /* low level flags */
  389. int preempt_count; /* 0 => preemptable, <0 => bug */
  390. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  391. mm_segment_t addr_limit; /* address limit */
  392. struct task_struct *task; /* main task structure */
  393. __u32 cpu; /* cpu */
  394. @@ -142,7 +143,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  395. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  396. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  397. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  398. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  399. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  400. +#define TIF_NEED_RESCHED_LAZY 7
  401. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  402. #define TIF_USING_IWMMXT 17
  403. @@ -152,6 +154,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  404. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  405. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  406. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  407. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  408. #define _TIF_UPROBE (1 << TIF_UPROBE)
  409. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  410. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  411. @@ -167,7 +170,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  412. * Change these and you break ASM code in entry-common.S
  413. */
  414. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  415. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  416. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  417. + _TIF_NEED_RESCHED_LAZY)
  418. #endif /* __KERNEL__ */
  419. #endif /* __ASM_ARM_THREAD_INFO_H */
  420. diff --git a/arch/arm/kernel/asm-offsets.c b/arch/arm/kernel/asm-offsets.c
  421. index 871b8267d211..4dbe70de7318 100644
  422. --- a/arch/arm/kernel/asm-offsets.c
  423. +++ b/arch/arm/kernel/asm-offsets.c
  424. @@ -65,6 +65,7 @@ int main(void)
  425. BLANK();
  426. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  427. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  428. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  429. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  430. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  431. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  432. diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
  433. index 3ce377f7251f..d044cea59f54 100644
  434. --- a/arch/arm/kernel/entry-armv.S
  435. +++ b/arch/arm/kernel/entry-armv.S
  436. @@ -215,11 +215,18 @@ __irq_svc:
  437. #ifdef CONFIG_PREEMPT
  438. get_thread_info tsk
  439. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  440. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  441. teq r8, #0 @ if preempt count != 0
  442. + bne 1f @ return from exeption
  443. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  444. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  445. + blne svc_preempt @ preempt!
  446. +
  447. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  448. + teq r8, #0 @ if preempt lazy count != 0
  449. movne r0, #0 @ force flags to 0
  450. - tst r0, #_TIF_NEED_RESCHED
  451. + tst r0, #_TIF_NEED_RESCHED_LAZY
  452. blne svc_preempt
  453. +1:
  454. #endif
  455. svc_exit r5, irq = 1 @ return from exception
  456. @@ -234,8 +241,14 @@ svc_preempt:
  457. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  458. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  459. tst r0, #_TIF_NEED_RESCHED
  460. + bne 1b
  461. + tst r0, #_TIF_NEED_RESCHED_LAZY
  462. reteq r8 @ go again
  463. - b 1b
  464. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  465. + teq r0, #0 @ if preempt lazy count != 0
  466. + beq 1b
  467. + ret r8 @ go again
  468. +
  469. #endif
  470. __und_fault:
  471. diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
  472. index 30a7228eaceb..c3bd6cbfce4b 100644
  473. --- a/arch/arm/kernel/entry-common.S
  474. +++ b/arch/arm/kernel/entry-common.S
  475. @@ -36,7 +36,9 @@ ret_fast_syscall:
  476. UNWIND(.cantunwind )
  477. disable_irq_notrace @ disable interrupts
  478. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  479. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  480. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  481. + bne fast_work_pending
  482. + tst r1, #_TIF_SECCOMP
  483. bne fast_work_pending
  484. /* perform architecture specific actions before user return */
  485. @@ -62,8 +64,11 @@ ret_fast_syscall:
  486. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  487. disable_irq_notrace @ disable interrupts
  488. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  489. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  490. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  491. + bne do_slower_path
  492. + tst r1, #_TIF_SECCOMP
  493. beq no_work_pending
  494. +do_slower_path:
  495. UNWIND(.fnend )
  496. ENDPROC(ret_fast_syscall)
  497. diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
  498. index 4adfb46e3ee9..15f1d94b47c5 100644
  499. --- a/arch/arm/kernel/process.c
  500. +++ b/arch/arm/kernel/process.c
  501. @@ -319,6 +319,30 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  502. }
  503. #ifdef CONFIG_MMU
  504. +/*
  505. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  506. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  507. + * fail.
  508. + */
  509. +static int __init vectors_user_mapping_init_page(void)
  510. +{
  511. + struct page *page;
  512. + unsigned long addr = 0xffff0000;
  513. + pgd_t *pgd;
  514. + pud_t *pud;
  515. + pmd_t *pmd;
  516. +
  517. + pgd = pgd_offset_k(addr);
  518. + pud = pud_offset(pgd, addr);
  519. + pmd = pmd_offset(pud, addr);
  520. + page = pmd_page(*(pmd));
  521. +
  522. + pgtable_page_ctor(page);
  523. +
  524. + return 0;
  525. +}
  526. +late_initcall(vectors_user_mapping_init_page);
  527. +
  528. #ifdef CONFIG_KUSER_HELPERS
  529. /*
  530. * The vectors page is always readable from user space for the
  531. diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c
  532. index 7b8f2141427b..96541e00b74a 100644
  533. --- a/arch/arm/kernel/signal.c
  534. +++ b/arch/arm/kernel/signal.c
  535. @@ -572,7 +572,8 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  536. */
  537. trace_hardirqs_off();
  538. do {
  539. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  540. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  541. + _TIF_NEED_RESCHED_LAZY))) {
  542. schedule();
  543. } else {
  544. if (unlikely(!user_mode(regs)))
  545. diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
  546. index b26361355dae..e5754e3b03c4 100644
  547. --- a/arch/arm/kernel/smp.c
  548. +++ b/arch/arm/kernel/smp.c
  549. @@ -230,8 +230,6 @@ int __cpu_disable(void)
  550. flush_cache_louis();
  551. local_flush_tlb_all();
  552. - clear_tasks_mm_cpumask(cpu);
  553. -
  554. return 0;
  555. }
  556. @@ -247,6 +245,9 @@ void __cpu_die(unsigned int cpu)
  557. pr_err("CPU%u: cpu didn't die\n", cpu);
  558. return;
  559. }
  560. +
  561. + clear_tasks_mm_cpumask(cpu);
  562. +
  563. pr_notice("CPU%u: shutdown\n", cpu);
  564. /*
  565. diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
  566. index 0bee233fef9a..314cfb232a63 100644
  567. --- a/arch/arm/kernel/unwind.c
  568. +++ b/arch/arm/kernel/unwind.c
  569. @@ -93,7 +93,7 @@ extern const struct unwind_idx __start_unwind_idx[];
  570. static const struct unwind_idx *__origin_unwind_idx;
  571. extern const struct unwind_idx __stop_unwind_idx[];
  572. -static DEFINE_SPINLOCK(unwind_lock);
  573. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  574. static LIST_HEAD(unwind_tables);
  575. /* Convert a prel31 symbol to an absolute address */
  576. @@ -201,7 +201,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  577. /* module unwind tables */
  578. struct unwind_table *table;
  579. - spin_lock_irqsave(&unwind_lock, flags);
  580. + raw_spin_lock_irqsave(&unwind_lock, flags);
  581. list_for_each_entry(table, &unwind_tables, list) {
  582. if (addr >= table->begin_addr &&
  583. addr < table->end_addr) {
  584. @@ -213,7 +213,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  585. break;
  586. }
  587. }
  588. - spin_unlock_irqrestore(&unwind_lock, flags);
  589. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  590. }
  591. pr_debug("%s: idx = %p\n", __func__, idx);
  592. @@ -529,9 +529,9 @@ struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  593. tab->begin_addr = text_addr;
  594. tab->end_addr = text_addr + text_size;
  595. - spin_lock_irqsave(&unwind_lock, flags);
  596. + raw_spin_lock_irqsave(&unwind_lock, flags);
  597. list_add_tail(&tab->list, &unwind_tables);
  598. - spin_unlock_irqrestore(&unwind_lock, flags);
  599. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  600. return tab;
  601. }
  602. @@ -543,9 +543,9 @@ void unwind_table_del(struct unwind_table *tab)
  603. if (!tab)
  604. return;
  605. - spin_lock_irqsave(&unwind_lock, flags);
  606. + raw_spin_lock_irqsave(&unwind_lock, flags);
  607. list_del(&tab->list);
  608. - spin_unlock_irqrestore(&unwind_lock, flags);
  609. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  610. kfree(tab);
  611. }
  612. diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
  613. index e06fd299de08..bb43bc396f06 100644
  614. --- a/arch/arm/kvm/arm.c
  615. +++ b/arch/arm/kvm/arm.c
  616. @@ -498,18 +498,18 @@ static void kvm_arm_resume_guest(struct kvm *kvm)
  617. struct kvm_vcpu *vcpu;
  618. kvm_for_each_vcpu(i, vcpu, kvm) {
  619. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  620. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  621. vcpu->arch.pause = false;
  622. - wake_up_interruptible(wq);
  623. + swake_up(wq);
  624. }
  625. }
  626. static void vcpu_sleep(struct kvm_vcpu *vcpu)
  627. {
  628. - wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
  629. + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu);
  630. - wait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  631. + swait_event_interruptible(*wq, ((!vcpu->arch.power_off) &&
  632. (!vcpu->arch.pause)));
  633. }
  634. @@ -568,7 +568,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  635. * involves poking the GIC, which must be done in a
  636. * non-preemptible context.
  637. */
  638. - preempt_disable();
  639. + migrate_disable();
  640. kvm_timer_flush_hwstate(vcpu);
  641. kvm_vgic_flush_hwstate(vcpu);
  642. @@ -587,7 +587,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  643. local_irq_enable();
  644. kvm_timer_sync_hwstate(vcpu);
  645. kvm_vgic_sync_hwstate(vcpu);
  646. - preempt_enable();
  647. + migrate_enable();
  648. continue;
  649. }
  650. @@ -641,7 +641,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  651. kvm_vgic_sync_hwstate(vcpu);
  652. - preempt_enable();
  653. + migrate_enable();
  654. ret = handle_exit(vcpu, run, ret);
  655. }
  656. diff --git a/arch/arm/kvm/psci.c b/arch/arm/kvm/psci.c
  657. index a9b3b905e661..c2b131527a64 100644
  658. --- a/arch/arm/kvm/psci.c
  659. +++ b/arch/arm/kvm/psci.c
  660. @@ -70,7 +70,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  661. {
  662. struct kvm *kvm = source_vcpu->kvm;
  663. struct kvm_vcpu *vcpu = NULL;
  664. - wait_queue_head_t *wq;
  665. + struct swait_queue_head *wq;
  666. unsigned long cpu_id;
  667. unsigned long context_id;
  668. phys_addr_t target_pc;
  669. @@ -119,7 +119,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
  670. smp_mb(); /* Make sure the above is visible */
  671. wq = kvm_arch_vcpu_wq(vcpu);
  672. - wake_up_interruptible(wq);
  673. + swake_up(wq);
  674. return PSCI_RET_SUCCESS;
  675. }
  676. diff --git a/arch/arm/mach-at91/Kconfig b/arch/arm/mach-at91/Kconfig
  677. index 28656c2b54a0..3f501305ca26 100644
  678. --- a/arch/arm/mach-at91/Kconfig
  679. +++ b/arch/arm/mach-at91/Kconfig
  680. @@ -99,6 +99,7 @@ config HAVE_AT91_USB_CLK
  681. config COMMON_CLK_AT91
  682. bool
  683. select COMMON_CLK
  684. + select MFD_SYSCON
  685. config HAVE_AT91_SMD
  686. bool
  687. diff --git a/arch/arm/mach-at91/at91rm9200.c b/arch/arm/mach-at91/at91rm9200.c
  688. index c1a7c6cc00e1..63b4fa25b48a 100644
  689. --- a/arch/arm/mach-at91/at91rm9200.c
  690. +++ b/arch/arm/mach-at91/at91rm9200.c
  691. @@ -12,7 +12,6 @@
  692. #include <linux/of_platform.h>
  693. #include <asm/mach/arch.h>
  694. -#include <asm/system_misc.h>
  695. #include "generic.h"
  696. #include "soc.h"
  697. @@ -33,7 +32,6 @@ static void __init at91rm9200_dt_device_init(void)
  698. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  699. - arm_pm_idle = at91rm9200_idle;
  700. at91rm9200_pm_init();
  701. }
  702. diff --git a/arch/arm/mach-at91/at91sam9.c b/arch/arm/mach-at91/at91sam9.c
  703. index 7eb64f763034..cada2a6412b3 100644
  704. --- a/arch/arm/mach-at91/at91sam9.c
  705. +++ b/arch/arm/mach-at91/at91sam9.c
  706. @@ -62,8 +62,6 @@ static void __init at91sam9_common_init(void)
  707. soc_dev = soc_device_to_device(soc);
  708. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  709. -
  710. - arm_pm_idle = at91sam9_idle;
  711. }
  712. static void __init at91sam9_dt_device_init(void)
  713. diff --git a/arch/arm/mach-at91/generic.h b/arch/arm/mach-at91/generic.h
  714. index b0fa7dc7286d..28ca57a2060f 100644
  715. --- a/arch/arm/mach-at91/generic.h
  716. +++ b/arch/arm/mach-at91/generic.h
  717. @@ -11,27 +11,18 @@
  718. #ifndef _AT91_GENERIC_H
  719. #define _AT91_GENERIC_H
  720. -#include <linux/of.h>
  721. -#include <linux/reboot.h>
  722. -
  723. - /* Map io */
  724. -extern void __init at91_map_io(void);
  725. -extern void __init at91_alt_map_io(void);
  726. -
  727. -/* idle */
  728. -extern void at91rm9200_idle(void);
  729. -extern void at91sam9_idle(void);
  730. -
  731. #ifdef CONFIG_PM
  732. extern void __init at91rm9200_pm_init(void);
  733. extern void __init at91sam9260_pm_init(void);
  734. extern void __init at91sam9g45_pm_init(void);
  735. extern void __init at91sam9x5_pm_init(void);
  736. +extern void __init sama5_pm_init(void);
  737. #else
  738. static inline void __init at91rm9200_pm_init(void) { }
  739. static inline void __init at91sam9260_pm_init(void) { }
  740. static inline void __init at91sam9g45_pm_init(void) { }
  741. static inline void __init at91sam9x5_pm_init(void) { }
  742. +static inline void __init sama5_pm_init(void) { }
  743. #endif
  744. #endif /* _AT91_GENERIC_H */
  745. diff --git a/arch/arm/mach-at91/pm.c b/arch/arm/mach-at91/pm.c
  746. index 23726fb31741..f06270198bf1 100644
  747. --- a/arch/arm/mach-at91/pm.c
  748. +++ b/arch/arm/mach-at91/pm.c
  749. @@ -31,10 +31,13 @@
  750. #include <asm/mach/irq.h>
  751. #include <asm/fncpy.h>
  752. #include <asm/cacheflush.h>
  753. +#include <asm/system_misc.h>
  754. #include "generic.h"
  755. #include "pm.h"
  756. +static void __iomem *pmc;
  757. +
  758. /*
  759. * FIXME: this is needed to communicate between the pinctrl driver and
  760. * the PM implementation in the machine. Possibly part of the PM
  761. @@ -87,7 +90,7 @@ static int at91_pm_verify_clocks(void)
  762. unsigned long scsr;
  763. int i;
  764. - scsr = at91_pmc_read(AT91_PMC_SCSR);
  765. + scsr = readl(pmc + AT91_PMC_SCSR);
  766. /* USB must not be using PLLB */
  767. if ((scsr & at91_pm_data.uhp_udp_mask) != 0) {
  768. @@ -101,8 +104,7 @@ static int at91_pm_verify_clocks(void)
  769. if ((scsr & (AT91_PMC_PCK0 << i)) == 0)
  770. continue;
  771. -
  772. - css = at91_pmc_read(AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  773. + css = readl(pmc + AT91_PMC_PCKR(i)) & AT91_PMC_CSS;
  774. if (css != AT91_PMC_CSS_SLOW) {
  775. pr_err("AT91: PM - Suspend-to-RAM with PCK%d src %d\n", i, css);
  776. return 0;
  777. @@ -145,8 +147,8 @@ static void at91_pm_suspend(suspend_state_t state)
  778. flush_cache_all();
  779. outer_disable();
  780. - at91_suspend_sram_fn(at91_pmc_base, at91_ramc_base[0],
  781. - at91_ramc_base[1], pm_data);
  782. + at91_suspend_sram_fn(pmc, at91_ramc_base[0],
  783. + at91_ramc_base[1], pm_data);
  784. outer_resume();
  785. }
  786. @@ -353,6 +355,21 @@ static __init void at91_dt_ramc(void)
  787. at91_pm_set_standby(standby);
  788. }
  789. +void at91rm9200_idle(void)
  790. +{
  791. + /*
  792. + * Disable the processor clock. The processor will be automatically
  793. + * re-enabled by an interrupt or by a reset.
  794. + */
  795. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  796. +}
  797. +
  798. +void at91sam9_idle(void)
  799. +{
  800. + writel(AT91_PMC_PCK, pmc + AT91_PMC_SCDR);
  801. + cpu_do_idle();
  802. +}
  803. +
  804. static void __init at91_pm_sram_init(void)
  805. {
  806. struct gen_pool *sram_pool;
  807. @@ -399,13 +416,36 @@ static void __init at91_pm_sram_init(void)
  808. &at91_pm_suspend_in_sram, at91_pm_suspend_in_sram_sz);
  809. }
  810. -static void __init at91_pm_init(void)
  811. +static const struct of_device_id atmel_pmc_ids[] __initconst = {
  812. + { .compatible = "atmel,at91rm9200-pmc" },
  813. + { .compatible = "atmel,at91sam9260-pmc" },
  814. + { .compatible = "atmel,at91sam9g45-pmc" },
  815. + { .compatible = "atmel,at91sam9n12-pmc" },
  816. + { .compatible = "atmel,at91sam9x5-pmc" },
  817. + { .compatible = "atmel,sama5d3-pmc" },
  818. + { .compatible = "atmel,sama5d2-pmc" },
  819. + { /* sentinel */ },
  820. +};
  821. +
  822. +static void __init at91_pm_init(void (*pm_idle)(void))
  823. {
  824. - at91_pm_sram_init();
  825. + struct device_node *pmc_np;
  826. if (at91_cpuidle_device.dev.platform_data)
  827. platform_device_register(&at91_cpuidle_device);
  828. + pmc_np = of_find_matching_node(NULL, atmel_pmc_ids);
  829. + pmc = of_iomap(pmc_np, 0);
  830. + if (!pmc) {
  831. + pr_err("AT91: PM not supported, PMC not found\n");
  832. + return;
  833. + }
  834. +
  835. + if (pm_idle)
  836. + arm_pm_idle = pm_idle;
  837. +
  838. + at91_pm_sram_init();
  839. +
  840. if (at91_suspend_sram_fn)
  841. suspend_set_ops(&at91_pm_ops);
  842. else
  843. @@ -424,7 +464,7 @@ void __init at91rm9200_pm_init(void)
  844. at91_pm_data.uhp_udp_mask = AT91RM9200_PMC_UHP | AT91RM9200_PMC_UDP;
  845. at91_pm_data.memctrl = AT91_MEMCTRL_MC;
  846. - at91_pm_init();
  847. + at91_pm_init(at91rm9200_idle);
  848. }
  849. void __init at91sam9260_pm_init(void)
  850. @@ -432,7 +472,7 @@ void __init at91sam9260_pm_init(void)
  851. at91_dt_ramc();
  852. at91_pm_data.memctrl = AT91_MEMCTRL_SDRAMC;
  853. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  854. - return at91_pm_init();
  855. + at91_pm_init(at91sam9_idle);
  856. }
  857. void __init at91sam9g45_pm_init(void)
  858. @@ -440,7 +480,7 @@ void __init at91sam9g45_pm_init(void)
  859. at91_dt_ramc();
  860. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP;
  861. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  862. - return at91_pm_init();
  863. + at91_pm_init(at91sam9_idle);
  864. }
  865. void __init at91sam9x5_pm_init(void)
  866. @@ -448,5 +488,13 @@ void __init at91sam9x5_pm_init(void)
  867. at91_dt_ramc();
  868. at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  869. at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  870. - return at91_pm_init();
  871. + at91_pm_init(at91sam9_idle);
  872. +}
  873. +
  874. +void __init sama5_pm_init(void)
  875. +{
  876. + at91_dt_ramc();
  877. + at91_pm_data.uhp_udp_mask = AT91SAM926x_PMC_UHP | AT91SAM926x_PMC_UDP;
  878. + at91_pm_data.memctrl = AT91_MEMCTRL_DDRSDR;
  879. + at91_pm_init(NULL);
  880. }
  881. diff --git a/arch/arm/mach-at91/sama5.c b/arch/arm/mach-at91/sama5.c
  882. index d9cf6799aec0..df8fdf1cf66d 100644
  883. --- a/arch/arm/mach-at91/sama5.c
  884. +++ b/arch/arm/mach-at91/sama5.c
  885. @@ -51,7 +51,7 @@ static void __init sama5_dt_device_init(void)
  886. soc_dev = soc_device_to_device(soc);
  887. of_platform_populate(NULL, of_default_bus_match_table, NULL, soc_dev);
  888. - at91sam9x5_pm_init();
  889. + sama5_pm_init();
  890. }
  891. static const char *const sama5_dt_board_compat[] __initconst = {
  892. diff --git a/arch/arm/mach-exynos/platsmp.c b/arch/arm/mach-exynos/platsmp.c
  893. index 98a2c0cbb833..310dce500d3e 100644
  894. --- a/arch/arm/mach-exynos/platsmp.c
  895. +++ b/arch/arm/mach-exynos/platsmp.c
  896. @@ -230,7 +230,7 @@ static void __iomem *scu_base_addr(void)
  897. return (void __iomem *)(S5P_VA_SCU);
  898. }
  899. -static DEFINE_SPINLOCK(boot_lock);
  900. +static DEFINE_RAW_SPINLOCK(boot_lock);
  901. static void exynos_secondary_init(unsigned int cpu)
  902. {
  903. @@ -243,8 +243,8 @@ static void exynos_secondary_init(unsigned int cpu)
  904. /*
  905. * Synchronise with the boot thread.
  906. */
  907. - spin_lock(&boot_lock);
  908. - spin_unlock(&boot_lock);
  909. + raw_spin_lock(&boot_lock);
  910. + raw_spin_unlock(&boot_lock);
  911. }
  912. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  913. @@ -308,7 +308,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  914. * Set synchronisation state between this boot processor
  915. * and the secondary one
  916. */
  917. - spin_lock(&boot_lock);
  918. + raw_spin_lock(&boot_lock);
  919. /*
  920. * The secondary processor is waiting to be released from
  921. @@ -335,7 +335,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  922. if (timeout == 0) {
  923. printk(KERN_ERR "cpu1 power enable failed");
  924. - spin_unlock(&boot_lock);
  925. + raw_spin_unlock(&boot_lock);
  926. return -ETIMEDOUT;
  927. }
  928. }
  929. @@ -381,7 +381,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  930. * calibrations, then wait for it to finish
  931. */
  932. fail:
  933. - spin_unlock(&boot_lock);
  934. + raw_spin_unlock(&boot_lock);
  935. return pen_release != -1 ? ret : 0;
  936. }
  937. diff --git a/arch/arm/mach-hisi/platmcpm.c b/arch/arm/mach-hisi/platmcpm.c
  938. index b5f8f5ffda79..9753a84df9c4 100644
  939. --- a/arch/arm/mach-hisi/platmcpm.c
  940. +++ b/arch/arm/mach-hisi/platmcpm.c
  941. @@ -61,7 +61,7 @@
  942. static void __iomem *sysctrl, *fabric;
  943. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  944. -static DEFINE_SPINLOCK(boot_lock);
  945. +static DEFINE_RAW_SPINLOCK(boot_lock);
  946. static u32 fabric_phys_addr;
  947. /*
  948. * [0]: bootwrapper physical address
  949. @@ -113,7 +113,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  950. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  951. return -EINVAL;
  952. - spin_lock_irq(&boot_lock);
  953. + raw_spin_lock_irq(&boot_lock);
  954. if (hip04_cpu_table[cluster][cpu])
  955. goto out;
  956. @@ -147,7 +147,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  957. out:
  958. hip04_cpu_table[cluster][cpu]++;
  959. - spin_unlock_irq(&boot_lock);
  960. + raw_spin_unlock_irq(&boot_lock);
  961. return 0;
  962. }
  963. @@ -162,11 +162,11 @@ static void hip04_cpu_die(unsigned int l_cpu)
  964. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  965. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  966. - spin_lock(&boot_lock);
  967. + raw_spin_lock(&boot_lock);
  968. hip04_cpu_table[cluster][cpu]--;
  969. if (hip04_cpu_table[cluster][cpu] == 1) {
  970. /* A power_up request went ahead of us. */
  971. - spin_unlock(&boot_lock);
  972. + raw_spin_unlock(&boot_lock);
  973. return;
  974. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  975. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  976. @@ -174,7 +174,7 @@ static void hip04_cpu_die(unsigned int l_cpu)
  977. }
  978. last_man = hip04_cluster_is_down(cluster);
  979. - spin_unlock(&boot_lock);
  980. + raw_spin_unlock(&boot_lock);
  981. if (last_man) {
  982. /* Since it's Cortex A15, disable L2 prefetching. */
  983. asm volatile(
  984. @@ -203,7 +203,7 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  985. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  986. count = TIMEOUT_MSEC / POLL_MSEC;
  987. - spin_lock_irq(&boot_lock);
  988. + raw_spin_lock_irq(&boot_lock);
  989. for (tries = 0; tries < count; tries++) {
  990. if (hip04_cpu_table[cluster][cpu])
  991. goto err;
  992. @@ -211,10 +211,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  993. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  994. if (data & CORE_WFI_STATUS(cpu))
  995. break;
  996. - spin_unlock_irq(&boot_lock);
  997. + raw_spin_unlock_irq(&boot_lock);
  998. /* Wait for clean L2 when the whole cluster is down. */
  999. msleep(POLL_MSEC);
  1000. - spin_lock_irq(&boot_lock);
  1001. + raw_spin_lock_irq(&boot_lock);
  1002. }
  1003. if (tries >= count)
  1004. goto err;
  1005. @@ -231,10 +231,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  1006. goto err;
  1007. if (hip04_cluster_is_down(cluster))
  1008. hip04_set_snoop_filter(cluster, 0);
  1009. - spin_unlock_irq(&boot_lock);
  1010. + raw_spin_unlock_irq(&boot_lock);
  1011. return 1;
  1012. err:
  1013. - spin_unlock_irq(&boot_lock);
  1014. + raw_spin_unlock_irq(&boot_lock);
  1015. return 0;
  1016. }
  1017. #endif
  1018. diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig
  1019. index 8ceda2844c4f..08bcf8fb76f2 100644
  1020. --- a/arch/arm/mach-imx/Kconfig
  1021. +++ b/arch/arm/mach-imx/Kconfig
  1022. @@ -524,7 +524,7 @@ config SOC_IMX6Q
  1023. bool "i.MX6 Quad/DualLite support"
  1024. select ARM_ERRATA_764369 if SMP
  1025. select HAVE_ARM_SCU if SMP
  1026. - select HAVE_ARM_TWD if SMP
  1027. + select HAVE_ARM_TWD
  1028. select PCI_DOMAINS if PCI
  1029. select PINCTRL_IMX6Q
  1030. select SOC_IMX6
  1031. diff --git a/arch/arm/mach-omap2/omap-smp.c b/arch/arm/mach-omap2/omap-smp.c
  1032. index 79e1f876d1c9..7e625c17f78e 100644
  1033. --- a/arch/arm/mach-omap2/omap-smp.c
  1034. +++ b/arch/arm/mach-omap2/omap-smp.c
  1035. @@ -43,7 +43,7 @@
  1036. /* SCU base address */
  1037. static void __iomem *scu_base;
  1038. -static DEFINE_SPINLOCK(boot_lock);
  1039. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1040. void __iomem *omap4_get_scu_base(void)
  1041. {
  1042. @@ -74,8 +74,8 @@ static void omap4_secondary_init(unsigned int cpu)
  1043. /*
  1044. * Synchronise with the boot thread.
  1045. */
  1046. - spin_lock(&boot_lock);
  1047. - spin_unlock(&boot_lock);
  1048. + raw_spin_lock(&boot_lock);
  1049. + raw_spin_unlock(&boot_lock);
  1050. }
  1051. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1052. @@ -89,7 +89,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1053. * Set synchronisation state between this boot processor
  1054. * and the secondary one
  1055. */
  1056. - spin_lock(&boot_lock);
  1057. + raw_spin_lock(&boot_lock);
  1058. /*
  1059. * Update the AuxCoreBoot0 with boot state for secondary core.
  1060. @@ -166,7 +166,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1061. * Now the secondary core is starting up let it run its
  1062. * calibrations, then wait for it to finish
  1063. */
  1064. - spin_unlock(&boot_lock);
  1065. + raw_spin_unlock(&boot_lock);
  1066. return 0;
  1067. }
  1068. diff --git a/arch/arm/mach-prima2/platsmp.c b/arch/arm/mach-prima2/platsmp.c
  1069. index e46c91094dde..dcb3ed0c26da 100644
  1070. --- a/arch/arm/mach-prima2/platsmp.c
  1071. +++ b/arch/arm/mach-prima2/platsmp.c
  1072. @@ -22,7 +22,7 @@
  1073. static void __iomem *clk_base;
  1074. -static DEFINE_SPINLOCK(boot_lock);
  1075. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1076. static void sirfsoc_secondary_init(unsigned int cpu)
  1077. {
  1078. @@ -36,8 +36,8 @@ static void sirfsoc_secondary_init(unsigned int cpu)
  1079. /*
  1080. * Synchronise with the boot thread.
  1081. */
  1082. - spin_lock(&boot_lock);
  1083. - spin_unlock(&boot_lock);
  1084. + raw_spin_lock(&boot_lock);
  1085. + raw_spin_unlock(&boot_lock);
  1086. }
  1087. static const struct of_device_id clk_ids[] = {
  1088. @@ -75,7 +75,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1089. /* make sure write buffer is drained */
  1090. mb();
  1091. - spin_lock(&boot_lock);
  1092. + raw_spin_lock(&boot_lock);
  1093. /*
  1094. * The secondary processor is waiting to be released from
  1095. @@ -107,7 +107,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1096. * now the secondary core is starting up let it run its
  1097. * calibrations, then wait for it to finish
  1098. */
  1099. - spin_unlock(&boot_lock);
  1100. + raw_spin_unlock(&boot_lock);
  1101. return pen_release != -1 ? -ENOSYS : 0;
  1102. }
  1103. diff --git a/arch/arm/mach-qcom/platsmp.c b/arch/arm/mach-qcom/platsmp.c
  1104. index 9b00123a315d..0a49fe1bc8cf 100644
  1105. --- a/arch/arm/mach-qcom/platsmp.c
  1106. +++ b/arch/arm/mach-qcom/platsmp.c
  1107. @@ -46,7 +46,7 @@
  1108. extern void secondary_startup_arm(void);
  1109. -static DEFINE_SPINLOCK(boot_lock);
  1110. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1111. #ifdef CONFIG_HOTPLUG_CPU
  1112. static void qcom_cpu_die(unsigned int cpu)
  1113. @@ -60,8 +60,8 @@ static void qcom_secondary_init(unsigned int cpu)
  1114. /*
  1115. * Synchronise with the boot thread.
  1116. */
  1117. - spin_lock(&boot_lock);
  1118. - spin_unlock(&boot_lock);
  1119. + raw_spin_lock(&boot_lock);
  1120. + raw_spin_unlock(&boot_lock);
  1121. }
  1122. static int scss_release_secondary(unsigned int cpu)
  1123. @@ -284,7 +284,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1124. * set synchronisation state between this boot processor
  1125. * and the secondary one
  1126. */
  1127. - spin_lock(&boot_lock);
  1128. + raw_spin_lock(&boot_lock);
  1129. /*
  1130. * Send the secondary CPU a soft interrupt, thereby causing
  1131. @@ -297,7 +297,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  1132. * now the secondary core is starting up let it run its
  1133. * calibrations, then wait for it to finish
  1134. */
  1135. - spin_unlock(&boot_lock);
  1136. + raw_spin_unlock(&boot_lock);
  1137. return ret;
  1138. }
  1139. diff --git a/arch/arm/mach-spear/platsmp.c b/arch/arm/mach-spear/platsmp.c
  1140. index fd4297713d67..b0553b2c2d53 100644
  1141. --- a/arch/arm/mach-spear/platsmp.c
  1142. +++ b/arch/arm/mach-spear/platsmp.c
  1143. @@ -32,7 +32,7 @@ static void write_pen_release(int val)
  1144. sync_cache_w(&pen_release);
  1145. }
  1146. -static DEFINE_SPINLOCK(boot_lock);
  1147. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1148. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  1149. @@ -47,8 +47,8 @@ static void spear13xx_secondary_init(unsigned int cpu)
  1150. /*
  1151. * Synchronise with the boot thread.
  1152. */
  1153. - spin_lock(&boot_lock);
  1154. - spin_unlock(&boot_lock);
  1155. + raw_spin_lock(&boot_lock);
  1156. + raw_spin_unlock(&boot_lock);
  1157. }
  1158. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1159. @@ -59,7 +59,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1160. * set synchronisation state between this boot processor
  1161. * and the secondary one
  1162. */
  1163. - spin_lock(&boot_lock);
  1164. + raw_spin_lock(&boot_lock);
  1165. /*
  1166. * The secondary processor is waiting to be released from
  1167. @@ -84,7 +84,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1168. * now the secondary core is starting up let it run its
  1169. * calibrations, then wait for it to finish
  1170. */
  1171. - spin_unlock(&boot_lock);
  1172. + raw_spin_unlock(&boot_lock);
  1173. return pen_release != -1 ? -ENOSYS : 0;
  1174. }
  1175. diff --git a/arch/arm/mach-sti/platsmp.c b/arch/arm/mach-sti/platsmp.c
  1176. index c4ad6eae67fa..e830b20b212f 100644
  1177. --- a/arch/arm/mach-sti/platsmp.c
  1178. +++ b/arch/arm/mach-sti/platsmp.c
  1179. @@ -35,7 +35,7 @@ static void write_pen_release(int val)
  1180. sync_cache_w(&pen_release);
  1181. }
  1182. -static DEFINE_SPINLOCK(boot_lock);
  1183. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1184. static void sti_secondary_init(unsigned int cpu)
  1185. {
  1186. @@ -48,8 +48,8 @@ static void sti_secondary_init(unsigned int cpu)
  1187. /*
  1188. * Synchronise with the boot thread.
  1189. */
  1190. - spin_lock(&boot_lock);
  1191. - spin_unlock(&boot_lock);
  1192. + raw_spin_lock(&boot_lock);
  1193. + raw_spin_unlock(&boot_lock);
  1194. }
  1195. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1196. @@ -60,7 +60,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1197. * set synchronisation state between this boot processor
  1198. * and the secondary one
  1199. */
  1200. - spin_lock(&boot_lock);
  1201. + raw_spin_lock(&boot_lock);
  1202. /*
  1203. * The secondary processor is waiting to be released from
  1204. @@ -91,7 +91,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1205. * now the secondary core is starting up let it run its
  1206. * calibrations, then wait for it to finish
  1207. */
  1208. - spin_unlock(&boot_lock);
  1209. + raw_spin_unlock(&boot_lock);
  1210. return pen_release != -1 ? -ENOSYS : 0;
  1211. }
  1212. diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
  1213. index daafcf121ce0..b8aa1e9ee8ee 100644
  1214. --- a/arch/arm/mm/fault.c
  1215. +++ b/arch/arm/mm/fault.c
  1216. @@ -430,6 +430,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1217. if (addr < TASK_SIZE)
  1218. return do_page_fault(addr, fsr, regs);
  1219. + if (interrupts_enabled(regs))
  1220. + local_irq_enable();
  1221. +
  1222. if (user_mode(regs))
  1223. goto bad_area;
  1224. @@ -497,6 +500,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  1225. static int
  1226. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  1227. {
  1228. + if (interrupts_enabled(regs))
  1229. + local_irq_enable();
  1230. +
  1231. do_bad_area(addr, fsr, regs);
  1232. return 0;
  1233. }
  1234. diff --git a/arch/arm/mm/highmem.c b/arch/arm/mm/highmem.c
  1235. index d02f8187b1cc..542692dbd40a 100644
  1236. --- a/arch/arm/mm/highmem.c
  1237. +++ b/arch/arm/mm/highmem.c
  1238. @@ -34,6 +34,11 @@ static inline pte_t get_fixmap_pte(unsigned long vaddr)
  1239. return *ptep;
  1240. }
  1241. +static unsigned int fixmap_idx(int type)
  1242. +{
  1243. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1244. +}
  1245. +
  1246. void *kmap(struct page *page)
  1247. {
  1248. might_sleep();
  1249. @@ -54,12 +59,13 @@ EXPORT_SYMBOL(kunmap);
  1250. void *kmap_atomic(struct page *page)
  1251. {
  1252. + pte_t pte = mk_pte(page, kmap_prot);
  1253. unsigned int idx;
  1254. unsigned long vaddr;
  1255. void *kmap;
  1256. int type;
  1257. - preempt_disable();
  1258. + preempt_disable_nort();
  1259. pagefault_disable();
  1260. if (!PageHighMem(page))
  1261. return page_address(page);
  1262. @@ -79,7 +85,7 @@ void *kmap_atomic(struct page *page)
  1263. type = kmap_atomic_idx_push();
  1264. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1265. + idx = fixmap_idx(type);
  1266. vaddr = __fix_to_virt(idx);
  1267. #ifdef CONFIG_DEBUG_HIGHMEM
  1268. /*
  1269. @@ -93,7 +99,10 @@ void *kmap_atomic(struct page *page)
  1270. * in place, so the contained TLB flush ensures the TLB is updated
  1271. * with the new mapping.
  1272. */
  1273. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  1274. +#ifdef CONFIG_PREEMPT_RT_FULL
  1275. + current->kmap_pte[type] = pte;
  1276. +#endif
  1277. + set_fixmap_pte(idx, pte);
  1278. return (void *)vaddr;
  1279. }
  1280. @@ -106,44 +115,75 @@ void __kunmap_atomic(void *kvaddr)
  1281. if (kvaddr >= (void *)FIXADDR_START) {
  1282. type = kmap_atomic_idx();
  1283. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1284. + idx = fixmap_idx(type);
  1285. if (cache_is_vivt())
  1286. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  1287. +#ifdef CONFIG_PREEMPT_RT_FULL
  1288. + current->kmap_pte[type] = __pte(0);
  1289. +#endif
  1290. #ifdef CONFIG_DEBUG_HIGHMEM
  1291. BUG_ON(vaddr != __fix_to_virt(idx));
  1292. - set_fixmap_pte(idx, __pte(0));
  1293. #else
  1294. (void) idx; /* to kill a warning */
  1295. #endif
  1296. + set_fixmap_pte(idx, __pte(0));
  1297. kmap_atomic_idx_pop();
  1298. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  1299. /* this address was obtained through kmap_high_get() */
  1300. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  1301. }
  1302. pagefault_enable();
  1303. - preempt_enable();
  1304. + preempt_enable_nort();
  1305. }
  1306. EXPORT_SYMBOL(__kunmap_atomic);
  1307. void *kmap_atomic_pfn(unsigned long pfn)
  1308. {
  1309. + pte_t pte = pfn_pte(pfn, kmap_prot);
  1310. unsigned long vaddr;
  1311. int idx, type;
  1312. struct page *page = pfn_to_page(pfn);
  1313. - preempt_disable();
  1314. + preempt_disable_nort();
  1315. pagefault_disable();
  1316. if (!PageHighMem(page))
  1317. return page_address(page);
  1318. type = kmap_atomic_idx_push();
  1319. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1320. + idx = fixmap_idx(type);
  1321. vaddr = __fix_to_virt(idx);
  1322. #ifdef CONFIG_DEBUG_HIGHMEM
  1323. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  1324. #endif
  1325. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  1326. +#ifdef CONFIG_PREEMPT_RT_FULL
  1327. + current->kmap_pte[type] = pte;
  1328. +#endif
  1329. + set_fixmap_pte(idx, pte);
  1330. return (void *)vaddr;
  1331. }
  1332. +#if defined CONFIG_PREEMPT_RT_FULL
  1333. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  1334. +{
  1335. + int i;
  1336. +
  1337. + /*
  1338. + * Clear @prev's kmap_atomic mappings
  1339. + */
  1340. + for (i = 0; i < prev_p->kmap_idx; i++) {
  1341. + int idx = fixmap_idx(i);
  1342. +
  1343. + set_fixmap_pte(idx, __pte(0));
  1344. + }
  1345. + /*
  1346. + * Restore @next_p's kmap_atomic mappings
  1347. + */
  1348. + for (i = 0; i < next_p->kmap_idx; i++) {
  1349. + int idx = fixmap_idx(i);
  1350. +
  1351. + if (!pte_none(next_p->kmap_pte[i]))
  1352. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  1353. + }
  1354. +}
  1355. +#endif
  1356. diff --git a/arch/arm/plat-versatile/platsmp.c b/arch/arm/plat-versatile/platsmp.c
  1357. index 53feb90c840c..b4a8d54fc3f3 100644
  1358. --- a/arch/arm/plat-versatile/platsmp.c
  1359. +++ b/arch/arm/plat-versatile/platsmp.c
  1360. @@ -30,7 +30,7 @@ static void write_pen_release(int val)
  1361. sync_cache_w(&pen_release);
  1362. }
  1363. -static DEFINE_SPINLOCK(boot_lock);
  1364. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1365. void versatile_secondary_init(unsigned int cpu)
  1366. {
  1367. @@ -43,8 +43,8 @@ void versatile_secondary_init(unsigned int cpu)
  1368. /*
  1369. * Synchronise with the boot thread.
  1370. */
  1371. - spin_lock(&boot_lock);
  1372. - spin_unlock(&boot_lock);
  1373. + raw_spin_lock(&boot_lock);
  1374. + raw_spin_unlock(&boot_lock);
  1375. }
  1376. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1377. @@ -55,7 +55,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1378. * Set synchronisation state between this boot processor
  1379. * and the secondary one
  1380. */
  1381. - spin_lock(&boot_lock);
  1382. + raw_spin_lock(&boot_lock);
  1383. /*
  1384. * This is really belt and braces; we hold unintended secondary
  1385. @@ -85,7 +85,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1386. * now the secondary core is starting up let it run its
  1387. * calibrations, then wait for it to finish
  1388. */
  1389. - spin_unlock(&boot_lock);
  1390. + raw_spin_unlock(&boot_lock);
  1391. return pen_release != -1 ? -ENOSYS : 0;
  1392. }
  1393. diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
  1394. index 871f21783866..1baa6537cf3f 100644
  1395. --- a/arch/arm64/Kconfig
  1396. +++ b/arch/arm64/Kconfig
  1397. @@ -76,6 +76,7 @@ config ARM64
  1398. select HAVE_PERF_REGS
  1399. select HAVE_PERF_USER_STACK_DUMP
  1400. select HAVE_RCU_TABLE_FREE
  1401. + select HAVE_PREEMPT_LAZY
  1402. select HAVE_SYSCALL_TRACEPOINTS
  1403. select IOMMU_DMA if IOMMU_SUPPORT
  1404. select IRQ_DOMAIN
  1405. @@ -562,7 +563,7 @@ config XEN_DOM0
  1406. config XEN
  1407. bool "Xen guest support on ARM64"
  1408. - depends on ARM64 && OF
  1409. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1410. select SWIOTLB_XEN
  1411. help
  1412. Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
  1413. diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
  1414. index 90c7ff233735..5f4e89fbc290 100644
  1415. --- a/arch/arm64/include/asm/thread_info.h
  1416. +++ b/arch/arm64/include/asm/thread_info.h
  1417. @@ -49,6 +49,7 @@ struct thread_info {
  1418. mm_segment_t addr_limit; /* address limit */
  1419. struct task_struct *task; /* main task structure */
  1420. int preempt_count; /* 0 => preemptable, <0 => bug */
  1421. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1422. int cpu; /* cpu */
  1423. };
  1424. @@ -103,6 +104,7 @@ static inline struct thread_info *current_thread_info(void)
  1425. #define TIF_NEED_RESCHED 1
  1426. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1427. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1428. +#define TIF_NEED_RESCHED_LAZY 4
  1429. #define TIF_NOHZ 7
  1430. #define TIF_SYSCALL_TRACE 8
  1431. #define TIF_SYSCALL_AUDIT 9
  1432. @@ -118,6 +120,7 @@ static inline struct thread_info *current_thread_info(void)
  1433. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1434. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1435. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1436. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1437. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1438. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1439. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1440. @@ -126,7 +129,8 @@ static inline struct thread_info *current_thread_info(void)
  1441. #define _TIF_32BIT (1 << TIF_32BIT)
  1442. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  1443. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  1444. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  1445. + _TIF_NEED_RESCHED_LAZY)
  1446. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1447. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  1448. diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
  1449. index 087cf9a65359..d74475928399 100644
  1450. --- a/arch/arm64/kernel/asm-offsets.c
  1451. +++ b/arch/arm64/kernel/asm-offsets.c
  1452. @@ -35,6 +35,7 @@ int main(void)
  1453. BLANK();
  1454. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1455. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1456. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1457. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1458. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1459. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1460. diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
  1461. index 5a3753d09e20..05d73c4c03f6 100644
  1462. --- a/arch/arm64/kernel/entry.S
  1463. +++ b/arch/arm64/kernel/entry.S
  1464. @@ -376,11 +376,16 @@ el1_irq:
  1465. #ifdef CONFIG_PREEMPT
  1466. get_thread_info tsk
  1467. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1468. - cbnz w24, 1f // preempt count != 0
  1469. + cbnz w24, 2f // preempt count != 0
  1470. ldr x0, [tsk, #TI_FLAGS] // get flags
  1471. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1472. - bl el1_preempt
  1473. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1474. +
  1475. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1476. + cbnz w24, 2f // preempt lazy count != 0
  1477. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1478. 1:
  1479. + bl el1_preempt
  1480. +2:
  1481. #endif
  1482. #ifdef CONFIG_TRACE_IRQFLAGS
  1483. bl trace_hardirqs_on
  1484. @@ -394,6 +399,7 @@ el1_preempt:
  1485. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1486. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1487. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1488. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1489. ret x24
  1490. #endif
  1491. @@ -638,6 +644,7 @@ ret_fast_syscall_trace:
  1492. */
  1493. work_pending:
  1494. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1495. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1496. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1497. ldr x2, [sp, #S_PSTATE]
  1498. mov x0, sp // 'regs'
  1499. diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
  1500. index db459612de44..bd8be6a0e745 100644
  1501. --- a/arch/mips/Kconfig
  1502. +++ b/arch/mips/Kconfig
  1503. @@ -2410,7 +2410,7 @@ config CPU_R4400_WORKAROUNDS
  1504. #
  1505. config HIGHMEM
  1506. bool "High Memory Support"
  1507. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1508. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1509. config CPU_SUPPORTS_HIGHMEM
  1510. bool
  1511. diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c
  1512. index e86b7499921a..b2a2f678c5dc 100644
  1513. --- a/arch/mips/kvm/mips.c
  1514. +++ b/arch/mips/kvm/mips.c
  1515. @@ -454,8 +454,8 @@ int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1516. dvcpu->arch.wait = 0;
  1517. - if (waitqueue_active(&dvcpu->wq))
  1518. - wake_up_interruptible(&dvcpu->wq);
  1519. + if (swait_active(&dvcpu->wq))
  1520. + swake_up(&dvcpu->wq);
  1521. return 0;
  1522. }
  1523. @@ -1183,8 +1183,8 @@ static void kvm_mips_comparecount_func(unsigned long data)
  1524. kvm_mips_callbacks->queue_timer_int(vcpu);
  1525. vcpu->arch.wait = 0;
  1526. - if (waitqueue_active(&vcpu->wq))
  1527. - wake_up_interruptible(&vcpu->wq);
  1528. + if (swait_active(&vcpu->wq))
  1529. + swake_up(&vcpu->wq);
  1530. }
  1531. /* low level hrtimer wake routine */
  1532. diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
  1533. index db49e0d796b1..1d2be228661c 100644
  1534. --- a/arch/powerpc/Kconfig
  1535. +++ b/arch/powerpc/Kconfig
  1536. @@ -60,10 +60,11 @@ config LOCKDEP_SUPPORT
  1537. config RWSEM_GENERIC_SPINLOCK
  1538. bool
  1539. + default y if PREEMPT_RT_FULL
  1540. config RWSEM_XCHGADD_ALGORITHM
  1541. bool
  1542. - default y
  1543. + default y if !PREEMPT_RT_FULL
  1544. config GENERIC_LOCKBREAK
  1545. bool
  1546. @@ -141,6 +142,7 @@ config PPC
  1547. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1548. select GENERIC_STRNCPY_FROM_USER
  1549. select GENERIC_STRNLEN_USER
  1550. + select HAVE_PREEMPT_LAZY
  1551. select HAVE_MOD_ARCH_SPECIFIC
  1552. select MODULES_USE_ELF_RELA
  1553. select CLONE_BACKWARDS
  1554. @@ -319,7 +321,7 @@ menu "Kernel options"
  1555. config HIGHMEM
  1556. bool "High memory support"
  1557. - depends on PPC32
  1558. + depends on PPC32 && !PREEMPT_RT_FULL
  1559. source kernel/Kconfig.hz
  1560. source kernel/Kconfig.preempt
  1561. diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
  1562. index cfa758c6b4f6..f8673ff84b31 100644
  1563. --- a/arch/powerpc/include/asm/kvm_host.h
  1564. +++ b/arch/powerpc/include/asm/kvm_host.h
  1565. @@ -286,7 +286,7 @@ struct kvmppc_vcore {
  1566. struct list_head runnable_threads;
  1567. struct list_head preempt_list;
  1568. spinlock_t lock;
  1569. - wait_queue_head_t wq;
  1570. + struct swait_queue_head wq;
  1571. spinlock_t stoltb_lock; /* protects stolen_tb and preempt_tb */
  1572. u64 stolen_tb;
  1573. u64 preempt_tb;
  1574. @@ -626,7 +626,7 @@ struct kvm_vcpu_arch {
  1575. u8 prodded;
  1576. u32 last_inst;
  1577. - wait_queue_head_t *wqp;
  1578. + struct swait_queue_head *wqp;
  1579. struct kvmppc_vcore *vcore;
  1580. int ret;
  1581. int trap;
  1582. diff --git a/arch/powerpc/include/asm/thread_info.h b/arch/powerpc/include/asm/thread_info.h
  1583. index 7efee4a3240b..40e6fa1b85b2 100644
  1584. --- a/arch/powerpc/include/asm/thread_info.h
  1585. +++ b/arch/powerpc/include/asm/thread_info.h
  1586. @@ -42,6 +42,8 @@ struct thread_info {
  1587. int cpu; /* cpu we're on */
  1588. int preempt_count; /* 0 => preemptable,
  1589. <0 => BUG */
  1590. + int preempt_lazy_count; /* 0 => preemptable,
  1591. + <0 => BUG */
  1592. unsigned long local_flags; /* private flags for thread */
  1593. /* low level flags - has atomic operations done on it */
  1594. @@ -82,8 +84,7 @@ static inline struct thread_info *current_thread_info(void)
  1595. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1596. #define TIF_SIGPENDING 1 /* signal pending */
  1597. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1598. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1599. - TIF_NEED_RESCHED */
  1600. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1601. #define TIF_32BIT 4 /* 32 bit binary */
  1602. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1603. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1604. @@ -101,6 +102,8 @@ static inline struct thread_info *current_thread_info(void)
  1605. #if defined(CONFIG_PPC64)
  1606. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1607. #endif
  1608. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1609. + TIF_NEED_RESCHED */
  1610. /* as above, but as bit values */
  1611. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1612. @@ -119,14 +122,16 @@ static inline struct thread_info *current_thread_info(void)
  1613. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1614. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1615. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1616. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1617. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1618. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1619. _TIF_NOHZ)
  1620. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1621. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1622. - _TIF_RESTORE_TM)
  1623. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1624. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1625. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1626. /* Bits in local_flags */
  1627. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1628. diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c
  1629. index 221d584d089f..d6d0c59ef8ae 100644
  1630. --- a/arch/powerpc/kernel/asm-offsets.c
  1631. +++ b/arch/powerpc/kernel/asm-offsets.c
  1632. @@ -160,6 +160,7 @@ int main(void)
  1633. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1634. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1635. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1636. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1637. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1638. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1639. diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
  1640. index 2405631e91a2..c21b4b42eaa0 100644
  1641. --- a/arch/powerpc/kernel/entry_32.S
  1642. +++ b/arch/powerpc/kernel/entry_32.S
  1643. @@ -818,7 +818,14 @@ resume_kernel:
  1644. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1645. bne restore
  1646. andi. r8,r8,_TIF_NEED_RESCHED
  1647. + bne+ 1f
  1648. + lwz r0,TI_PREEMPT_LAZY(r9)
  1649. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1650. + bne restore
  1651. + lwz r0,TI_FLAGS(r9)
  1652. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1653. beq+ restore
  1654. +1:
  1655. lwz r3,_MSR(r1)
  1656. andi. r0,r3,MSR_EE /* interrupts off? */
  1657. beq restore /* don't schedule if so */
  1658. @@ -829,11 +836,11 @@ resume_kernel:
  1659. */
  1660. bl trace_hardirqs_off
  1661. #endif
  1662. -1: bl preempt_schedule_irq
  1663. +2: bl preempt_schedule_irq
  1664. CURRENT_THREAD_INFO(r9, r1)
  1665. lwz r3,TI_FLAGS(r9)
  1666. - andi. r0,r3,_TIF_NEED_RESCHED
  1667. - bne- 1b
  1668. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1669. + bne- 2b
  1670. #ifdef CONFIG_TRACE_IRQFLAGS
  1671. /* And now, to properly rebalance the above, we tell lockdep they
  1672. * are being turned back on, which will happen when we return
  1673. @@ -1154,7 +1161,7 @@ global_dbcr0:
  1674. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1675. do_work: /* r10 contains MSR_KERNEL here */
  1676. - andi. r0,r9,_TIF_NEED_RESCHED
  1677. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1678. beq do_user_signal
  1679. do_resched: /* r10 contains MSR_KERNEL here */
  1680. @@ -1175,7 +1182,7 @@ recheck:
  1681. MTMSRD(r10) /* disable interrupts */
  1682. CURRENT_THREAD_INFO(r9, r1)
  1683. lwz r9,TI_FLAGS(r9)
  1684. - andi. r0,r9,_TIF_NEED_RESCHED
  1685. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1686. bne- do_resched
  1687. andi. r0,r9,_TIF_USER_WORK_MASK
  1688. beq restore_user
  1689. diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
  1690. index a94f155db78e..5bb3148e592b 100644
  1691. --- a/arch/powerpc/kernel/entry_64.S
  1692. +++ b/arch/powerpc/kernel/entry_64.S
  1693. @@ -683,7 +683,7 @@ _GLOBAL(ret_from_except_lite)
  1694. #else
  1695. beq restore
  1696. #endif
  1697. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1698. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1699. beq 2f
  1700. bl restore_interrupts
  1701. SCHEDULE_USER
  1702. @@ -745,10 +745,18 @@ resume_kernel:
  1703. #ifdef CONFIG_PREEMPT
  1704. /* Check if we need to preempt */
  1705. + lwz r8,TI_PREEMPT(r9)
  1706. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1707. + bne restore
  1708. andi. r0,r4,_TIF_NEED_RESCHED
  1709. + bne+ check_count
  1710. +
  1711. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1712. beq+ restore
  1713. + lwz r8,TI_PREEMPT_LAZY(r9)
  1714. +
  1715. /* Check that preempt_count() == 0 and interrupts are enabled */
  1716. - lwz r8,TI_PREEMPT(r9)
  1717. +check_count:
  1718. cmpwi cr1,r8,0
  1719. ld r0,SOFTE(r1)
  1720. cmpdi r0,0
  1721. @@ -765,7 +773,7 @@ resume_kernel:
  1722. /* Re-test flags and eventually loop */
  1723. CURRENT_THREAD_INFO(r9, r1)
  1724. ld r4,TI_FLAGS(r9)
  1725. - andi. r0,r4,_TIF_NEED_RESCHED
  1726. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1727. bne 1b
  1728. /*
  1729. diff --git a/arch/powerpc/kernel/irq.c b/arch/powerpc/kernel/irq.c
  1730. index 290559df1e8b..070afa6da35d 100644
  1731. --- a/arch/powerpc/kernel/irq.c
  1732. +++ b/arch/powerpc/kernel/irq.c
  1733. @@ -614,6 +614,7 @@ void irq_ctx_init(void)
  1734. }
  1735. }
  1736. +#ifndef CONFIG_PREEMPT_RT_FULL
  1737. void do_softirq_own_stack(void)
  1738. {
  1739. struct thread_info *curtp, *irqtp;
  1740. @@ -631,6 +632,7 @@ void do_softirq_own_stack(void)
  1741. if (irqtp->flags)
  1742. set_bits(irqtp->flags, &curtp->flags);
  1743. }
  1744. +#endif
  1745. irq_hw_number_t virq_to_hw(unsigned int virq)
  1746. {
  1747. diff --git a/arch/powerpc/kernel/misc_32.S b/arch/powerpc/kernel/misc_32.S
  1748. index ed3ab509faca..8b261416c070 100644
  1749. --- a/arch/powerpc/kernel/misc_32.S
  1750. +++ b/arch/powerpc/kernel/misc_32.S
  1751. @@ -40,6 +40,7 @@
  1752. * We store the saved ksp_limit in the unused part
  1753. * of the STACK_FRAME_OVERHEAD
  1754. */
  1755. +#ifndef CONFIG_PREEMPT_RT_FULL
  1756. _GLOBAL(call_do_softirq)
  1757. mflr r0
  1758. stw r0,4(r1)
  1759. @@ -56,6 +57,7 @@ _GLOBAL(call_do_softirq)
  1760. stw r10,THREAD+KSP_LIMIT(r2)
  1761. mtlr r0
  1762. blr
  1763. +#endif
  1764. /*
  1765. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1766. diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
  1767. index db475d41b57a..96b7ef80e05d 100644
  1768. --- a/arch/powerpc/kernel/misc_64.S
  1769. +++ b/arch/powerpc/kernel/misc_64.S
  1770. @@ -30,6 +30,7 @@
  1771. .text
  1772. +#ifndef CONFIG_PREEMPT_RT_FULL
  1773. _GLOBAL(call_do_softirq)
  1774. mflr r0
  1775. std r0,16(r1)
  1776. @@ -40,6 +41,7 @@ _GLOBAL(call_do_softirq)
  1777. ld r0,16(r1)
  1778. mtlr r0
  1779. blr
  1780. +#endif
  1781. _GLOBAL(call_do_irq)
  1782. mflr r0
  1783. diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
  1784. index c2024ac9d4e8..2303788da7e1 100644
  1785. --- a/arch/powerpc/kvm/Kconfig
  1786. +++ b/arch/powerpc/kvm/Kconfig
  1787. @@ -172,6 +172,7 @@ config KVM_E500MC
  1788. config KVM_MPIC
  1789. bool "KVM in-kernel MPIC emulation"
  1790. depends on KVM && E500
  1791. + depends on !PREEMPT_RT_FULL
  1792. select HAVE_KVM_IRQCHIP
  1793. select HAVE_KVM_IRQFD
  1794. select HAVE_KVM_IRQ_ROUTING
  1795. diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
  1796. index a7352b59e6f9..df34a6432873 100644
  1797. --- a/arch/powerpc/kvm/book3s_hv.c
  1798. +++ b/arch/powerpc/kvm/book3s_hv.c
  1799. @@ -114,11 +114,11 @@ static bool kvmppc_ipi_thread(int cpu)
  1800. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  1801. {
  1802. int cpu;
  1803. - wait_queue_head_t *wqp;
  1804. + struct swait_queue_head *wqp;
  1805. wqp = kvm_arch_vcpu_wq(vcpu);
  1806. - if (waitqueue_active(wqp)) {
  1807. - wake_up_interruptible(wqp);
  1808. + if (swait_active(wqp)) {
  1809. + swake_up(wqp);
  1810. ++vcpu->stat.halt_wakeup;
  1811. }
  1812. @@ -707,8 +707,8 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  1813. tvcpu->arch.prodded = 1;
  1814. smp_mb();
  1815. if (vcpu->arch.ceded) {
  1816. - if (waitqueue_active(&vcpu->wq)) {
  1817. - wake_up_interruptible(&vcpu->wq);
  1818. + if (swait_active(&vcpu->wq)) {
  1819. + swake_up(&vcpu->wq);
  1820. vcpu->stat.halt_wakeup++;
  1821. }
  1822. }
  1823. @@ -1447,7 +1447,7 @@ static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1824. INIT_LIST_HEAD(&vcore->runnable_threads);
  1825. spin_lock_init(&vcore->lock);
  1826. spin_lock_init(&vcore->stoltb_lock);
  1827. - init_waitqueue_head(&vcore->wq);
  1828. + init_swait_queue_head(&vcore->wq);
  1829. vcore->preempt_tb = TB_NIL;
  1830. vcore->lpcr = kvm->arch.lpcr;
  1831. vcore->first_vcpuid = core * threads_per_subcore;
  1832. @@ -2519,10 +2519,9 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1833. {
  1834. struct kvm_vcpu *vcpu;
  1835. int do_sleep = 1;
  1836. + DECLARE_SWAITQUEUE(wait);
  1837. - DEFINE_WAIT(wait);
  1838. -
  1839. - prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1840. + prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1841. /*
  1842. * Check one last time for pending exceptions and ceded state after
  1843. @@ -2536,7 +2535,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1844. }
  1845. if (!do_sleep) {
  1846. - finish_wait(&vc->wq, &wait);
  1847. + finish_swait(&vc->wq, &wait);
  1848. return;
  1849. }
  1850. @@ -2544,7 +2543,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1851. trace_kvmppc_vcore_blocked(vc, 0);
  1852. spin_unlock(&vc->lock);
  1853. schedule();
  1854. - finish_wait(&vc->wq, &wait);
  1855. + finish_swait(&vc->wq, &wait);
  1856. spin_lock(&vc->lock);
  1857. vc->vcore_state = VCORE_INACTIVE;
  1858. trace_kvmppc_vcore_blocked(vc, 1);
  1859. @@ -2600,7 +2599,7 @@ static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1860. kvmppc_start_thread(vcpu, vc);
  1861. trace_kvm_guest_enter(vcpu);
  1862. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1863. - wake_up(&vc->wq);
  1864. + swake_up(&vc->wq);
  1865. }
  1866. }
  1867. diff --git a/arch/powerpc/platforms/ps3/device-init.c b/arch/powerpc/platforms/ps3/device-init.c
  1868. index 3f175e8aedb4..c4c02f91904c 100644
  1869. --- a/arch/powerpc/platforms/ps3/device-init.c
  1870. +++ b/arch/powerpc/platforms/ps3/device-init.c
  1871. @@ -752,7 +752,7 @@ static int ps3_notification_read_write(struct ps3_notification_device *dev,
  1872. }
  1873. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1874. - res = wait_event_interruptible(dev->done.wait,
  1875. + res = swait_event_interruptible(dev->done.wait,
  1876. dev->done.done || kthread_should_stop());
  1877. if (kthread_should_stop())
  1878. res = -EINTR;
  1879. diff --git a/arch/s390/include/asm/kvm_host.h b/arch/s390/include/asm/kvm_host.h
  1880. index e9a983f40a24..bbdc539fb3c6 100644
  1881. --- a/arch/s390/include/asm/kvm_host.h
  1882. +++ b/arch/s390/include/asm/kvm_host.h
  1883. @@ -427,7 +427,7 @@ struct kvm_s390_irq_payload {
  1884. struct kvm_s390_local_interrupt {
  1885. spinlock_t lock;
  1886. struct kvm_s390_float_interrupt *float_int;
  1887. - wait_queue_head_t *wq;
  1888. + struct swait_queue_head *wq;
  1889. atomic_t *cpuflags;
  1890. DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
  1891. struct kvm_s390_irq_payload irq;
  1892. diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
  1893. index 6a75352f453c..cc862c486002 100644
  1894. --- a/arch/s390/kvm/interrupt.c
  1895. +++ b/arch/s390/kvm/interrupt.c
  1896. @@ -868,13 +868,13 @@ no_timer:
  1897. void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
  1898. {
  1899. - if (waitqueue_active(&vcpu->wq)) {
  1900. + if (swait_active(&vcpu->wq)) {
  1901. /*
  1902. * The vcpu gave up the cpu voluntarily, mark it as a good
  1903. * yield-candidate.
  1904. */
  1905. vcpu->preempted = true;
  1906. - wake_up_interruptible(&vcpu->wq);
  1907. + swake_up(&vcpu->wq);
  1908. vcpu->stat.halt_wakeup++;
  1909. }
  1910. }
  1911. diff --git a/arch/sh/kernel/irq.c b/arch/sh/kernel/irq.c
  1912. index 6c0378c0b8b5..abd58b4dff97 100644
  1913. --- a/arch/sh/kernel/irq.c
  1914. +++ b/arch/sh/kernel/irq.c
  1915. @@ -147,6 +147,7 @@ void irq_ctx_exit(int cpu)
  1916. hardirq_ctx[cpu] = NULL;
  1917. }
  1918. +#ifndef CONFIG_PREEMPT_RT_FULL
  1919. void do_softirq_own_stack(void)
  1920. {
  1921. struct thread_info *curctx;
  1922. @@ -174,6 +175,7 @@ void do_softirq_own_stack(void)
  1923. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1924. );
  1925. }
  1926. +#endif
  1927. #else
  1928. static inline void handle_one_irq(unsigned int irq)
  1929. {
  1930. diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
  1931. index 56442d2d7bbc..8c9598f534c9 100644
  1932. --- a/arch/sparc/Kconfig
  1933. +++ b/arch/sparc/Kconfig
  1934. @@ -189,12 +189,10 @@ config NR_CPUS
  1935. source kernel/Kconfig.hz
  1936. config RWSEM_GENERIC_SPINLOCK
  1937. - bool
  1938. - default y if SPARC32
  1939. + def_bool PREEMPT_RT_FULL
  1940. config RWSEM_XCHGADD_ALGORITHM
  1941. - bool
  1942. - default y if SPARC64
  1943. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1944. config GENERIC_HWEIGHT
  1945. bool
  1946. diff --git a/arch/sparc/kernel/irq_64.c b/arch/sparc/kernel/irq_64.c
  1947. index e22416ce56ea..d359de71153a 100644
  1948. --- a/arch/sparc/kernel/irq_64.c
  1949. +++ b/arch/sparc/kernel/irq_64.c
  1950. @@ -854,6 +854,7 @@ void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  1951. set_irq_regs(old_regs);
  1952. }
  1953. +#ifndef CONFIG_PREEMPT_RT_FULL
  1954. void do_softirq_own_stack(void)
  1955. {
  1956. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1957. @@ -868,6 +869,7 @@ void do_softirq_own_stack(void)
  1958. __asm__ __volatile__("mov %0, %%sp"
  1959. : : "r" (orig_sp));
  1960. }
  1961. +#endif
  1962. #ifdef CONFIG_HOTPLUG_CPU
  1963. void fixup_irqs(void)
  1964. diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
  1965. index 436639a31624..6ee1dd0deadc 100644
  1966. --- a/arch/x86/Kconfig
  1967. +++ b/arch/x86/Kconfig
  1968. @@ -17,6 +17,7 @@ config X86_64
  1969. ### Arch settings
  1970. config X86
  1971. def_bool y
  1972. + select HAVE_PREEMPT_LAZY
  1973. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  1974. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  1975. select ANON_INODES
  1976. @@ -212,8 +213,11 @@ config ARCH_MAY_HAVE_PC_FDC
  1977. def_bool y
  1978. depends on ISA_DMA_API
  1979. +config RWSEM_GENERIC_SPINLOCK
  1980. + def_bool PREEMPT_RT_FULL
  1981. +
  1982. config RWSEM_XCHGADD_ALGORITHM
  1983. - def_bool y
  1984. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1985. config GENERIC_CALIBRATE_DELAY
  1986. def_bool y
  1987. @@ -848,7 +852,7 @@ config IOMMU_HELPER
  1988. config MAXSMP
  1989. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  1990. depends on X86_64 && SMP && DEBUG_KERNEL
  1991. - select CPUMASK_OFFSTACK
  1992. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  1993. ---help---
  1994. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  1995. If unsure, say N.
  1996. diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
  1997. index 3633ad6145c5..c6d5458ee7f9 100644
  1998. --- a/arch/x86/crypto/aesni-intel_glue.c
  1999. +++ b/arch/x86/crypto/aesni-intel_glue.c
  2000. @@ -383,14 +383,14 @@ static int ecb_encrypt(struct blkcipher_desc *desc,
  2001. err = blkcipher_walk_virt(desc, &walk);
  2002. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2003. - kernel_fpu_begin();
  2004. while ((nbytes = walk.nbytes)) {
  2005. + kernel_fpu_begin();
  2006. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2007. - nbytes & AES_BLOCK_MASK);
  2008. + nbytes & AES_BLOCK_MASK);
  2009. + kernel_fpu_end();
  2010. nbytes &= AES_BLOCK_SIZE - 1;
  2011. err = blkcipher_walk_done(desc, &walk, nbytes);
  2012. }
  2013. - kernel_fpu_end();
  2014. return err;
  2015. }
  2016. @@ -407,14 +407,14 @@ static int ecb_decrypt(struct blkcipher_desc *desc,
  2017. err = blkcipher_walk_virt(desc, &walk);
  2018. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2019. - kernel_fpu_begin();
  2020. while ((nbytes = walk.nbytes)) {
  2021. + kernel_fpu_begin();
  2022. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2023. nbytes & AES_BLOCK_MASK);
  2024. + kernel_fpu_end();
  2025. nbytes &= AES_BLOCK_SIZE - 1;
  2026. err = blkcipher_walk_done(desc, &walk, nbytes);
  2027. }
  2028. - kernel_fpu_end();
  2029. return err;
  2030. }
  2031. @@ -431,14 +431,14 @@ static int cbc_encrypt(struct blkcipher_desc *desc,
  2032. err = blkcipher_walk_virt(desc, &walk);
  2033. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2034. - kernel_fpu_begin();
  2035. while ((nbytes = walk.nbytes)) {
  2036. + kernel_fpu_begin();
  2037. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2038. nbytes & AES_BLOCK_MASK, walk.iv);
  2039. + kernel_fpu_end();
  2040. nbytes &= AES_BLOCK_SIZE - 1;
  2041. err = blkcipher_walk_done(desc, &walk, nbytes);
  2042. }
  2043. - kernel_fpu_end();
  2044. return err;
  2045. }
  2046. @@ -455,14 +455,14 @@ static int cbc_decrypt(struct blkcipher_desc *desc,
  2047. err = blkcipher_walk_virt(desc, &walk);
  2048. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2049. - kernel_fpu_begin();
  2050. while ((nbytes = walk.nbytes)) {
  2051. + kernel_fpu_begin();
  2052. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2053. nbytes & AES_BLOCK_MASK, walk.iv);
  2054. + kernel_fpu_end();
  2055. nbytes &= AES_BLOCK_SIZE - 1;
  2056. err = blkcipher_walk_done(desc, &walk, nbytes);
  2057. }
  2058. - kernel_fpu_end();
  2059. return err;
  2060. }
  2061. @@ -514,18 +514,20 @@ static int ctr_crypt(struct blkcipher_desc *desc,
  2062. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  2063. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2064. - kernel_fpu_begin();
  2065. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  2066. + kernel_fpu_begin();
  2067. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  2068. nbytes & AES_BLOCK_MASK, walk.iv);
  2069. + kernel_fpu_end();
  2070. nbytes &= AES_BLOCK_SIZE - 1;
  2071. err = blkcipher_walk_done(desc, &walk, nbytes);
  2072. }
  2073. if (walk.nbytes) {
  2074. + kernel_fpu_begin();
  2075. ctr_crypt_final(ctx, &walk);
  2076. + kernel_fpu_end();
  2077. err = blkcipher_walk_done(desc, &walk, 0);
  2078. }
  2079. - kernel_fpu_end();
  2080. return err;
  2081. }
  2082. diff --git a/arch/x86/crypto/cast5_avx_glue.c b/arch/x86/crypto/cast5_avx_glue.c
  2083. index 8648158f3916..d7699130ee36 100644
  2084. --- a/arch/x86/crypto/cast5_avx_glue.c
  2085. +++ b/arch/x86/crypto/cast5_avx_glue.c
  2086. @@ -59,7 +59,7 @@ static inline void cast5_fpu_end(bool fpu_enabled)
  2087. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2088. bool enc)
  2089. {
  2090. - bool fpu_enabled = false;
  2091. + bool fpu_enabled;
  2092. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  2093. const unsigned int bsize = CAST5_BLOCK_SIZE;
  2094. unsigned int nbytes;
  2095. @@ -75,7 +75,7 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2096. u8 *wsrc = walk->src.virt.addr;
  2097. u8 *wdst = walk->dst.virt.addr;
  2098. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2099. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2100. /* Process multi-block batch */
  2101. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  2102. @@ -103,10 +103,9 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  2103. } while (nbytes >= bsize);
  2104. done:
  2105. + cast5_fpu_end(fpu_enabled);
  2106. err = blkcipher_walk_done(desc, walk, nbytes);
  2107. }
  2108. -
  2109. - cast5_fpu_end(fpu_enabled);
  2110. return err;
  2111. }
  2112. @@ -227,7 +226,7 @@ done:
  2113. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2114. struct scatterlist *src, unsigned int nbytes)
  2115. {
  2116. - bool fpu_enabled = false;
  2117. + bool fpu_enabled;
  2118. struct blkcipher_walk walk;
  2119. int err;
  2120. @@ -236,12 +235,11 @@ static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2121. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2122. while ((nbytes = walk.nbytes)) {
  2123. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2124. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2125. nbytes = __cbc_decrypt(desc, &walk);
  2126. + cast5_fpu_end(fpu_enabled);
  2127. err = blkcipher_walk_done(desc, &walk, nbytes);
  2128. }
  2129. -
  2130. - cast5_fpu_end(fpu_enabled);
  2131. return err;
  2132. }
  2133. @@ -311,7 +309,7 @@ done:
  2134. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2135. struct scatterlist *src, unsigned int nbytes)
  2136. {
  2137. - bool fpu_enabled = false;
  2138. + bool fpu_enabled;
  2139. struct blkcipher_walk walk;
  2140. int err;
  2141. @@ -320,13 +318,12 @@ static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  2142. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  2143. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  2144. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  2145. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  2146. nbytes = __ctr_crypt(desc, &walk);
  2147. + cast5_fpu_end(fpu_enabled);
  2148. err = blkcipher_walk_done(desc, &walk, nbytes);
  2149. }
  2150. - cast5_fpu_end(fpu_enabled);
  2151. -
  2152. if (walk.nbytes) {
  2153. ctr_crypt_final(desc, &walk);
  2154. err = blkcipher_walk_done(desc, &walk, 0);
  2155. diff --git a/arch/x86/crypto/glue_helper.c b/arch/x86/crypto/glue_helper.c
  2156. index 6a85598931b5..3a506ce7ed93 100644
  2157. --- a/arch/x86/crypto/glue_helper.c
  2158. +++ b/arch/x86/crypto/glue_helper.c
  2159. @@ -39,7 +39,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2160. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  2161. const unsigned int bsize = 128 / 8;
  2162. unsigned int nbytes, i, func_bytes;
  2163. - bool fpu_enabled = false;
  2164. + bool fpu_enabled;
  2165. int err;
  2166. err = blkcipher_walk_virt(desc, walk);
  2167. @@ -49,7 +49,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2168. u8 *wdst = walk->dst.virt.addr;
  2169. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2170. - desc, fpu_enabled, nbytes);
  2171. + desc, false, nbytes);
  2172. for (i = 0; i < gctx->num_funcs; i++) {
  2173. func_bytes = bsize * gctx->funcs[i].num_blocks;
  2174. @@ -71,10 +71,10 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  2175. }
  2176. done:
  2177. + glue_fpu_end(fpu_enabled);
  2178. err = blkcipher_walk_done(desc, walk, nbytes);
  2179. }
  2180. - glue_fpu_end(fpu_enabled);
  2181. return err;
  2182. }
  2183. @@ -194,7 +194,7 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2184. struct scatterlist *src, unsigned int nbytes)
  2185. {
  2186. const unsigned int bsize = 128 / 8;
  2187. - bool fpu_enabled = false;
  2188. + bool fpu_enabled;
  2189. struct blkcipher_walk walk;
  2190. int err;
  2191. @@ -203,12 +203,12 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  2192. while ((nbytes = walk.nbytes)) {
  2193. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2194. - desc, fpu_enabled, nbytes);
  2195. + desc, false, nbytes);
  2196. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  2197. + glue_fpu_end(fpu_enabled);
  2198. err = blkcipher_walk_done(desc, &walk, nbytes);
  2199. }
  2200. - glue_fpu_end(fpu_enabled);
  2201. return err;
  2202. }
  2203. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  2204. @@ -277,7 +277,7 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2205. struct scatterlist *src, unsigned int nbytes)
  2206. {
  2207. const unsigned int bsize = 128 / 8;
  2208. - bool fpu_enabled = false;
  2209. + bool fpu_enabled;
  2210. struct blkcipher_walk walk;
  2211. int err;
  2212. @@ -286,13 +286,12 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  2213. while ((nbytes = walk.nbytes) >= bsize) {
  2214. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2215. - desc, fpu_enabled, nbytes);
  2216. + desc, false, nbytes);
  2217. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  2218. + glue_fpu_end(fpu_enabled);
  2219. err = blkcipher_walk_done(desc, &walk, nbytes);
  2220. }
  2221. - glue_fpu_end(fpu_enabled);
  2222. -
  2223. if (walk.nbytes) {
  2224. glue_ctr_crypt_final_128bit(
  2225. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  2226. @@ -347,7 +346,7 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2227. void *tweak_ctx, void *crypt_ctx)
  2228. {
  2229. const unsigned int bsize = 128 / 8;
  2230. - bool fpu_enabled = false;
  2231. + bool fpu_enabled;
  2232. struct blkcipher_walk walk;
  2233. int err;
  2234. @@ -360,21 +359,21 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  2235. /* set minimum length to bsize, for tweak_fn */
  2236. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2237. - desc, fpu_enabled,
  2238. + desc, false,
  2239. nbytes < bsize ? bsize : nbytes);
  2240. -
  2241. /* calculate first value of T */
  2242. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  2243. + glue_fpu_end(fpu_enabled);
  2244. while (nbytes) {
  2245. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  2246. + desc, false, nbytes);
  2247. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  2248. + glue_fpu_end(fpu_enabled);
  2249. err = blkcipher_walk_done(desc, &walk, nbytes);
  2250. nbytes = walk.nbytes;
  2251. }
  2252. -
  2253. - glue_fpu_end(fpu_enabled);
  2254. -
  2255. return err;
  2256. }
  2257. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  2258. diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
  2259. index 1a4477cedc49..75a301b6a5b6 100644
  2260. --- a/arch/x86/entry/common.c
  2261. +++ b/arch/x86/entry/common.c
  2262. @@ -220,7 +220,7 @@ long syscall_trace_enter(struct pt_regs *regs)
  2263. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  2264. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  2265. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  2266. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  2267. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2268. {
  2269. @@ -236,9 +236,16 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  2270. /* We have work to do. */
  2271. local_irq_enable();
  2272. - if (cached_flags & _TIF_NEED_RESCHED)
  2273. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  2274. schedule();
  2275. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  2276. + if (unlikely(current->forced_info.si_signo)) {
  2277. + struct task_struct *t = current;
  2278. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  2279. + t->forced_info.si_signo = 0;
  2280. + }
  2281. +#endif
  2282. if (cached_flags & _TIF_UPROBE)
  2283. uprobe_notify_resume(regs);
  2284. diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
  2285. index f3b6d54e0042..2d722ee01fc2 100644
  2286. --- a/arch/x86/entry/entry_32.S
  2287. +++ b/arch/x86/entry/entry_32.S
  2288. @@ -278,8 +278,24 @@ END(ret_from_exception)
  2289. ENTRY(resume_kernel)
  2290. DISABLE_INTERRUPTS(CLBR_ANY)
  2291. need_resched:
  2292. + # preempt count == 0 + NEED_RS set?
  2293. cmpl $0, PER_CPU_VAR(__preempt_count)
  2294. +#ifndef CONFIG_PREEMPT_LAZY
  2295. jnz restore_all
  2296. +#else
  2297. + jz test_int_off
  2298. +
  2299. + # atleast preempt count == 0 ?
  2300. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2301. + jne restore_all
  2302. +
  2303. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  2304. + jnz restore_all
  2305. +
  2306. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  2307. + jz restore_all
  2308. +test_int_off:
  2309. +#endif
  2310. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  2311. jz restore_all
  2312. call preempt_schedule_irq
  2313. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  2314. index a55697d19824..316081a2ca85 100644
  2315. --- a/arch/x86/entry/entry_64.S
  2316. +++ b/arch/x86/entry/entry_64.S
  2317. @@ -579,7 +579,23 @@ retint_kernel:
  2318. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  2319. jnc 1f
  2320. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  2321. +#ifndef CONFIG_PREEMPT_LAZY
  2322. jnz 1f
  2323. +#else
  2324. + jz do_preempt_schedule_irq
  2325. +
  2326. + # atleast preempt count == 0 ?
  2327. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  2328. + jnz 1f
  2329. +
  2330. + GET_THREAD_INFO(%rcx)
  2331. + cmpl $0, TI_preempt_lazy_count(%rcx)
  2332. + jnz 1f
  2333. +
  2334. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  2335. + jnc 1f
  2336. +do_preempt_schedule_irq:
  2337. +#endif
  2338. call preempt_schedule_irq
  2339. jmp 0b
  2340. 1:
  2341. @@ -867,6 +883,7 @@ bad_gs:
  2342. jmp 2b
  2343. .previous
  2344. +#ifndef CONFIG_PREEMPT_RT_FULL
  2345. /* Call softirq on interrupt stack. Interrupts are off. */
  2346. ENTRY(do_softirq_own_stack)
  2347. pushq %rbp
  2348. @@ -879,6 +896,7 @@ ENTRY(do_softirq_own_stack)
  2349. decl PER_CPU_VAR(irq_count)
  2350. ret
  2351. END(do_softirq_own_stack)
  2352. +#endif
  2353. #ifdef CONFIG_XEN
  2354. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  2355. diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
  2356. index 01bcde84d3e4..5dbd2d0f91e0 100644
  2357. --- a/arch/x86/include/asm/preempt.h
  2358. +++ b/arch/x86/include/asm/preempt.h
  2359. @@ -79,17 +79,33 @@ static __always_inline void __preempt_count_sub(int val)
  2360. * a decrement which hits zero means we have no preempt_count and should
  2361. * reschedule.
  2362. */
  2363. -static __always_inline bool __preempt_count_dec_and_test(void)
  2364. +static __always_inline bool ____preempt_count_dec_and_test(void)
  2365. {
  2366. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), "e");
  2367. }
  2368. +static __always_inline bool __preempt_count_dec_and_test(void)
  2369. +{
  2370. + if (____preempt_count_dec_and_test())
  2371. + return true;
  2372. +#ifdef CONFIG_PREEMPT_LAZY
  2373. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  2374. +#else
  2375. + return false;
  2376. +#endif
  2377. +}
  2378. +
  2379. /*
  2380. * Returns true when we need to resched and can (barring IRQ state).
  2381. */
  2382. static __always_inline bool should_resched(int preempt_offset)
  2383. {
  2384. +#ifdef CONFIG_PREEMPT_LAZY
  2385. + return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset ||
  2386. + test_thread_flag(TIF_NEED_RESCHED_LAZY));
  2387. +#else
  2388. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  2389. +#endif
  2390. }
  2391. #ifdef CONFIG_PREEMPT
  2392. diff --git a/arch/x86/include/asm/signal.h b/arch/x86/include/asm/signal.h
  2393. index 2138c9ae19ee..3f5b4ee2e2c1 100644
  2394. --- a/arch/x86/include/asm/signal.h
  2395. +++ b/arch/x86/include/asm/signal.h
  2396. @@ -23,6 +23,19 @@ typedef struct {
  2397. unsigned long sig[_NSIG_WORDS];
  2398. } sigset_t;
  2399. +/*
  2400. + * Because some traps use the IST stack, we must keep preemption
  2401. + * disabled while calling do_trap(), but do_trap() may call
  2402. + * force_sig_info() which will grab the signal spin_locks for the
  2403. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  2404. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  2405. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  2406. + * trap.
  2407. + */
  2408. +#if defined(CONFIG_PREEMPT_RT_FULL)
  2409. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  2410. +#endif
  2411. +
  2412. #ifndef CONFIG_COMPAT
  2413. typedef sigset_t compat_sigset_t;
  2414. #endif
  2415. diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
  2416. index 58505f01962f..02fa39652cd6 100644
  2417. --- a/arch/x86/include/asm/stackprotector.h
  2418. +++ b/arch/x86/include/asm/stackprotector.h
  2419. @@ -59,7 +59,7 @@
  2420. */
  2421. static __always_inline void boot_init_stack_canary(void)
  2422. {
  2423. - u64 canary;
  2424. + u64 uninitialized_var(canary);
  2425. u64 tsc;
  2426. #ifdef CONFIG_X86_64
  2427. @@ -70,8 +70,15 @@ static __always_inline void boot_init_stack_canary(void)
  2428. * of randomness. The TSC only matters for very early init,
  2429. * there it already has some randomness on most systems. Later
  2430. * on during the bootup the random pool has true entropy too.
  2431. + *
  2432. + * For preempt-rt we need to weaken the randomness a bit, as
  2433. + * we can't call into the random generator from atomic context
  2434. + * due to locking constraints. We just leave canary
  2435. + * uninitialized and use the TSC based randomness on top of it.
  2436. */
  2437. +#ifndef CONFIG_PREEMPT_RT_FULL
  2438. get_random_bytes(&canary, sizeof(canary));
  2439. +#endif
  2440. tsc = rdtsc();
  2441. canary += tsc + (tsc << 32UL);
  2442. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
  2443. index c7b551028740..ddb63bd90e3c 100644
  2444. --- a/arch/x86/include/asm/thread_info.h
  2445. +++ b/arch/x86/include/asm/thread_info.h
  2446. @@ -58,6 +58,8 @@ struct thread_info {
  2447. __u32 status; /* thread synchronous flags */
  2448. __u32 cpu; /* current CPU */
  2449. mm_segment_t addr_limit;
  2450. + int preempt_lazy_count; /* 0 => lazy preemptable
  2451. + <0 => BUG */
  2452. unsigned int sig_on_uaccess_error:1;
  2453. unsigned int uaccess_err:1; /* uaccess failed */
  2454. };
  2455. @@ -95,6 +97,7 @@ struct thread_info {
  2456. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  2457. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2458. #define TIF_SECCOMP 8 /* secure computing */
  2459. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  2460. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  2461. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  2462. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  2463. @@ -119,6 +122,7 @@ struct thread_info {
  2464. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  2465. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  2466. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  2467. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  2468. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  2469. #define _TIF_UPROBE (1 << TIF_UPROBE)
  2470. #define _TIF_NOTSC (1 << TIF_NOTSC)
  2471. @@ -152,6 +156,8 @@ struct thread_info {
  2472. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  2473. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  2474. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2475. +
  2476. #define STACK_WARN (THREAD_SIZE/8)
  2477. /*
  2478. diff --git a/arch/x86/include/asm/uv/uv_bau.h b/arch/x86/include/asm/uv/uv_bau.h
  2479. index fc808b83fccb..ebb40118abf5 100644
  2480. --- a/arch/x86/include/asm/uv/uv_bau.h
  2481. +++ b/arch/x86/include/asm/uv/uv_bau.h
  2482. @@ -615,9 +615,9 @@ struct bau_control {
  2483. cycles_t send_message;
  2484. cycles_t period_end;
  2485. cycles_t period_time;
  2486. - spinlock_t uvhub_lock;
  2487. - spinlock_t queue_lock;
  2488. - spinlock_t disable_lock;
  2489. + raw_spinlock_t uvhub_lock;
  2490. + raw_spinlock_t queue_lock;
  2491. + raw_spinlock_t disable_lock;
  2492. /* tunables */
  2493. int max_concurr;
  2494. int max_concurr_const;
  2495. @@ -776,15 +776,15 @@ static inline int atom_asr(short i, struct atomic_short *v)
  2496. * to be lowered below the current 'v'. atomic_add_unless can only stop
  2497. * on equal.
  2498. */
  2499. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  2500. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  2501. {
  2502. - spin_lock(lock);
  2503. + raw_spin_lock(lock);
  2504. if (atomic_read(v) >= u) {
  2505. - spin_unlock(lock);
  2506. + raw_spin_unlock(lock);
  2507. return 0;
  2508. }
  2509. atomic_inc(v);
  2510. - spin_unlock(lock);
  2511. + raw_spin_unlock(lock);
  2512. return 1;
  2513. }
  2514. diff --git a/arch/x86/include/asm/uv/uv_hub.h b/arch/x86/include/asm/uv/uv_hub.h
  2515. index ea7074784cc4..01ec643ce66e 100644
  2516. --- a/arch/x86/include/asm/uv/uv_hub.h
  2517. +++ b/arch/x86/include/asm/uv/uv_hub.h
  2518. @@ -492,7 +492,7 @@ struct uv_blade_info {
  2519. unsigned short nr_online_cpus;
  2520. unsigned short pnode;
  2521. short memory_nid;
  2522. - spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2523. + raw_spinlock_t nmi_lock; /* obsolete, see uv_hub_nmi */
  2524. unsigned long nmi_count; /* obsolete, see uv_hub_nmi */
  2525. };
  2526. extern struct uv_blade_info *uv_blade_info;
  2527. diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
  2528. index fdb0fbfb1197..678c711e2a16 100644
  2529. --- a/arch/x86/kernel/apic/io_apic.c
  2530. +++ b/arch/x86/kernel/apic/io_apic.c
  2531. @@ -1711,7 +1711,8 @@ static bool io_apic_level_ack_pending(struct mp_chip_data *data)
  2532. static inline bool ioapic_irqd_mask(struct irq_data *data)
  2533. {
  2534. /* If we are moving the irq we need to mask it */
  2535. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  2536. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  2537. + !irqd_irq_inprogress(data))) {
  2538. mask_ioapic_irq(data);
  2539. return true;
  2540. }
  2541. diff --git a/arch/x86/kernel/apic/x2apic_uv_x.c b/arch/x86/kernel/apic/x2apic_uv_x.c
  2542. index 4a139465f1d4..ad2afff02b36 100644
  2543. --- a/arch/x86/kernel/apic/x2apic_uv_x.c
  2544. +++ b/arch/x86/kernel/apic/x2apic_uv_x.c
  2545. @@ -947,7 +947,7 @@ void __init uv_system_init(void)
  2546. uv_blade_info[blade].pnode = pnode;
  2547. uv_blade_info[blade].nr_possible_cpus = 0;
  2548. uv_blade_info[blade].nr_online_cpus = 0;
  2549. - spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2550. + raw_spin_lock_init(&uv_blade_info[blade].nmi_lock);
  2551. min_pnode = min(pnode, min_pnode);
  2552. max_pnode = max(pnode, max_pnode);
  2553. blade++;
  2554. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  2555. index 439df975bc7a..b7954ddd6a0a 100644
  2556. --- a/arch/x86/kernel/asm-offsets.c
  2557. +++ b/arch/x86/kernel/asm-offsets.c
  2558. @@ -32,6 +32,7 @@ void common(void) {
  2559. OFFSET(TI_flags, thread_info, flags);
  2560. OFFSET(TI_status, thread_info, status);
  2561. OFFSET(TI_addr_limit, thread_info, addr_limit);
  2562. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  2563. BLANK();
  2564. OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
  2565. @@ -89,4 +90,5 @@ void common(void) {
  2566. BLANK();
  2567. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  2568. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  2569. }
  2570. diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
  2571. index 7e8a736d09db..430a4ec07811 100644
  2572. --- a/arch/x86/kernel/cpu/mcheck/mce.c
  2573. +++ b/arch/x86/kernel/cpu/mcheck/mce.c
  2574. @@ -41,6 +41,8 @@
  2575. #include <linux/debugfs.h>
  2576. #include <linux/irq_work.h>
  2577. #include <linux/export.h>
  2578. +#include <linux/jiffies.h>
  2579. +#include <linux/swork.h>
  2580. #include <asm/processor.h>
  2581. #include <asm/traps.h>
  2582. @@ -1236,7 +1238,7 @@ void mce_log_therm_throt_event(__u64 status)
  2583. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  2584. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  2585. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  2586. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  2587. static unsigned long mce_adjust_timer_default(unsigned long interval)
  2588. {
  2589. @@ -1245,32 +1247,18 @@ static unsigned long mce_adjust_timer_default(unsigned long interval)
  2590. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  2591. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  2592. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  2593. {
  2594. - unsigned long when = jiffies + interval;
  2595. - unsigned long flags;
  2596. -
  2597. - local_irq_save(flags);
  2598. -
  2599. - if (timer_pending(t)) {
  2600. - if (time_before(when, t->expires))
  2601. - mod_timer_pinned(t, when);
  2602. - } else {
  2603. - t->expires = round_jiffies(when);
  2604. - add_timer_on(t, smp_processor_id());
  2605. - }
  2606. -
  2607. - local_irq_restore(flags);
  2608. + if (!interval)
  2609. + return HRTIMER_NORESTART;
  2610. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  2611. + return HRTIMER_RESTART;
  2612. }
  2613. -static void mce_timer_fn(unsigned long data)
  2614. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  2615. {
  2616. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2617. - int cpu = smp_processor_id();
  2618. unsigned long iv;
  2619. - WARN_ON(cpu != data);
  2620. -
  2621. iv = __this_cpu_read(mce_next_interval);
  2622. if (mce_available(this_cpu_ptr(&cpu_info))) {
  2623. @@ -1293,7 +1281,7 @@ static void mce_timer_fn(unsigned long data)
  2624. done:
  2625. __this_cpu_write(mce_next_interval, iv);
  2626. - __restart_timer(t, iv);
  2627. + return __restart_timer(timer, iv);
  2628. }
  2629. /*
  2630. @@ -1301,7 +1289,7 @@ done:
  2631. */
  2632. void mce_timer_kick(unsigned long interval)
  2633. {
  2634. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2635. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2636. unsigned long iv = __this_cpu_read(mce_next_interval);
  2637. __restart_timer(t, interval);
  2638. @@ -1316,7 +1304,7 @@ static void mce_timer_delete_all(void)
  2639. int cpu;
  2640. for_each_online_cpu(cpu)
  2641. - del_timer_sync(&per_cpu(mce_timer, cpu));
  2642. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  2643. }
  2644. static void mce_do_trigger(struct work_struct *work)
  2645. @@ -1326,6 +1314,56 @@ static void mce_do_trigger(struct work_struct *work)
  2646. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2647. +static void __mce_notify_work(struct swork_event *event)
  2648. +{
  2649. + /* Not more than two messages every minute */
  2650. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2651. +
  2652. + /* wake processes polling /dev/mcelog */
  2653. + wake_up_interruptible(&mce_chrdev_wait);
  2654. +
  2655. + /*
  2656. + * There is no risk of missing notifications because
  2657. + * work_pending is always cleared before the function is
  2658. + * executed.
  2659. + */
  2660. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  2661. + schedule_work(&mce_trigger_work);
  2662. +
  2663. + if (__ratelimit(&ratelimit))
  2664. + pr_info(HW_ERR "Machine check events logged\n");
  2665. +}
  2666. +
  2667. +#ifdef CONFIG_PREEMPT_RT_FULL
  2668. +static bool notify_work_ready __read_mostly;
  2669. +static struct swork_event notify_work;
  2670. +
  2671. +static int mce_notify_work_init(void)
  2672. +{
  2673. + int err;
  2674. +
  2675. + err = swork_get();
  2676. + if (err)
  2677. + return err;
  2678. +
  2679. + INIT_SWORK(&notify_work, __mce_notify_work);
  2680. + notify_work_ready = true;
  2681. + return 0;
  2682. +}
  2683. +
  2684. +static void mce_notify_work(void)
  2685. +{
  2686. + if (notify_work_ready)
  2687. + swork_queue(&notify_work);
  2688. +}
  2689. +#else
  2690. +static void mce_notify_work(void)
  2691. +{
  2692. + __mce_notify_work(NULL);
  2693. +}
  2694. +static inline int mce_notify_work_init(void) { return 0; }
  2695. +#endif
  2696. +
  2697. /*
  2698. * Notify the user(s) about new machine check events.
  2699. * Can be called from interrupt context, but not from machine check/NMI
  2700. @@ -1333,19 +1371,8 @@ static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2701. */
  2702. int mce_notify_irq(void)
  2703. {
  2704. - /* Not more than two messages every minute */
  2705. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2706. -
  2707. if (test_and_clear_bit(0, &mce_need_notify)) {
  2708. - /* wake processes polling /dev/mcelog */
  2709. - wake_up_interruptible(&mce_chrdev_wait);
  2710. -
  2711. - if (mce_helper[0])
  2712. - schedule_work(&mce_trigger_work);
  2713. -
  2714. - if (__ratelimit(&ratelimit))
  2715. - pr_info(HW_ERR "Machine check events logged\n");
  2716. -
  2717. + mce_notify_work();
  2718. return 1;
  2719. }
  2720. return 0;
  2721. @@ -1639,7 +1666,7 @@ static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
  2722. }
  2723. }
  2724. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2725. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  2726. {
  2727. unsigned long iv = check_interval * HZ;
  2728. @@ -1648,16 +1675,17 @@ static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2729. per_cpu(mce_next_interval, cpu) = iv;
  2730. - t->expires = round_jiffies(jiffies + iv);
  2731. - add_timer_on(t, cpu);
  2732. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  2733. + 0, HRTIMER_MODE_REL_PINNED);
  2734. }
  2735. static void __mcheck_cpu_init_timer(void)
  2736. {
  2737. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2738. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2739. unsigned int cpu = smp_processor_id();
  2740. - setup_timer(t, mce_timer_fn, cpu);
  2741. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2742. + t->function = mce_timer_fn;
  2743. mce_start_timer(cpu, t);
  2744. }
  2745. @@ -2376,6 +2404,8 @@ static void mce_disable_cpu(void *h)
  2746. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2747. return;
  2748. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2749. +
  2750. if (!(action & CPU_TASKS_FROZEN))
  2751. cmci_clear();
  2752. @@ -2398,6 +2428,7 @@ static void mce_reenable_cpu(void *h)
  2753. if (b->init)
  2754. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  2755. }
  2756. + __mcheck_cpu_init_timer();
  2757. }
  2758. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2759. @@ -2405,7 +2436,6 @@ static int
  2760. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2761. {
  2762. unsigned int cpu = (unsigned long)hcpu;
  2763. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2764. switch (action & ~CPU_TASKS_FROZEN) {
  2765. case CPU_ONLINE:
  2766. @@ -2425,11 +2455,9 @@ mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2767. break;
  2768. case CPU_DOWN_PREPARE:
  2769. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2770. - del_timer_sync(t);
  2771. break;
  2772. case CPU_DOWN_FAILED:
  2773. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2774. - mce_start_timer(cpu, t);
  2775. break;
  2776. }
  2777. @@ -2468,6 +2496,10 @@ static __init int mcheck_init_device(void)
  2778. goto err_out;
  2779. }
  2780. + err = mce_notify_work_init();
  2781. + if (err)
  2782. + goto err_out;
  2783. +
  2784. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2785. err = -ENOMEM;
  2786. goto err_out;
  2787. diff --git a/arch/x86/kernel/cpu/perf_event_intel_rapl.c b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2788. index ed446bdcbf31..d2ac364e2118 100644
  2789. --- a/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2790. +++ b/arch/x86/kernel/cpu/perf_event_intel_rapl.c
  2791. @@ -117,7 +117,7 @@ static struct perf_pmu_events_attr event_attr_##v = { \
  2792. };
  2793. struct rapl_pmu {
  2794. - spinlock_t lock;
  2795. + raw_spinlock_t lock;
  2796. int n_active; /* number of active events */
  2797. struct list_head active_list;
  2798. struct pmu *pmu; /* pointer to rapl_pmu_class */
  2799. @@ -220,13 +220,13 @@ static enum hrtimer_restart rapl_hrtimer_handle(struct hrtimer *hrtimer)
  2800. if (!pmu->n_active)
  2801. return HRTIMER_NORESTART;
  2802. - spin_lock_irqsave(&pmu->lock, flags);
  2803. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2804. list_for_each_entry(event, &pmu->active_list, active_entry) {
  2805. rapl_event_update(event);
  2806. }
  2807. - spin_unlock_irqrestore(&pmu->lock, flags);
  2808. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2809. hrtimer_forward_now(hrtimer, pmu->timer_interval);
  2810. @@ -263,9 +263,9 @@ static void rapl_pmu_event_start(struct perf_event *event, int mode)
  2811. struct rapl_pmu *pmu = __this_cpu_read(rapl_pmu);
  2812. unsigned long flags;
  2813. - spin_lock_irqsave(&pmu->lock, flags);
  2814. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2815. __rapl_pmu_event_start(pmu, event);
  2816. - spin_unlock_irqrestore(&pmu->lock, flags);
  2817. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2818. }
  2819. static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2820. @@ -274,7 +274,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2821. struct hw_perf_event *hwc = &event->hw;
  2822. unsigned long flags;
  2823. - spin_lock_irqsave(&pmu->lock, flags);
  2824. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2825. /* mark event as deactivated and stopped */
  2826. if (!(hwc->state & PERF_HES_STOPPED)) {
  2827. @@ -299,7 +299,7 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
  2828. hwc->state |= PERF_HES_UPTODATE;
  2829. }
  2830. - spin_unlock_irqrestore(&pmu->lock, flags);
  2831. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2832. }
  2833. static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2834. @@ -308,14 +308,14 @@ static int rapl_pmu_event_add(struct perf_event *event, int mode)
  2835. struct hw_perf_event *hwc = &event->hw;
  2836. unsigned long flags;
  2837. - spin_lock_irqsave(&pmu->lock, flags);
  2838. + raw_spin_lock_irqsave(&pmu->lock, flags);
  2839. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  2840. if (mode & PERF_EF_START)
  2841. __rapl_pmu_event_start(pmu, event);
  2842. - spin_unlock_irqrestore(&pmu->lock, flags);
  2843. + raw_spin_unlock_irqrestore(&pmu->lock, flags);
  2844. return 0;
  2845. }
  2846. @@ -603,7 +603,7 @@ static int rapl_cpu_prepare(int cpu)
  2847. pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu));
  2848. if (!pmu)
  2849. return -1;
  2850. - spin_lock_init(&pmu->lock);
  2851. + raw_spin_lock_init(&pmu->lock);
  2852. INIT_LIST_HEAD(&pmu->active_list);
  2853. diff --git a/arch/x86/kernel/dumpstack_32.c b/arch/x86/kernel/dumpstack_32.c
  2854. index 464ffd69b92e..00db1aad1548 100644
  2855. --- a/arch/x86/kernel/dumpstack_32.c
  2856. +++ b/arch/x86/kernel/dumpstack_32.c
  2857. @@ -42,7 +42,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2858. unsigned long *stack, unsigned long bp,
  2859. const struct stacktrace_ops *ops, void *data)
  2860. {
  2861. - const unsigned cpu = get_cpu();
  2862. + const unsigned cpu = get_cpu_light();
  2863. int graph = 0;
  2864. u32 *prev_esp;
  2865. @@ -86,7 +86,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2866. break;
  2867. touch_nmi_watchdog();
  2868. }
  2869. - put_cpu();
  2870. + put_cpu_light();
  2871. }
  2872. EXPORT_SYMBOL(dump_trace);
  2873. diff --git a/arch/x86/kernel/dumpstack_64.c b/arch/x86/kernel/dumpstack_64.c
  2874. index 5f1c6266eb30..c331e3fef465 100644
  2875. --- a/arch/x86/kernel/dumpstack_64.c
  2876. +++ b/arch/x86/kernel/dumpstack_64.c
  2877. @@ -152,7 +152,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2878. unsigned long *stack, unsigned long bp,
  2879. const struct stacktrace_ops *ops, void *data)
  2880. {
  2881. - const unsigned cpu = get_cpu();
  2882. + const unsigned cpu = get_cpu_light();
  2883. struct thread_info *tinfo;
  2884. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  2885. unsigned long dummy;
  2886. @@ -241,7 +241,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2887. * This handles the process stack:
  2888. */
  2889. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  2890. - put_cpu();
  2891. + put_cpu_light();
  2892. }
  2893. EXPORT_SYMBOL(dump_trace);
  2894. @@ -255,7 +255,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2895. int cpu;
  2896. int i;
  2897. - preempt_disable();
  2898. + migrate_disable();
  2899. cpu = smp_processor_id();
  2900. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  2901. @@ -291,7 +291,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2902. pr_cont(" %016lx", *stack++);
  2903. touch_nmi_watchdog();
  2904. }
  2905. - preempt_enable();
  2906. + migrate_enable();
  2907. pr_cont("\n");
  2908. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  2909. diff --git a/arch/x86/kernel/irq_32.c b/arch/x86/kernel/irq_32.c
  2910. index 38da8f29a9c8..ce71f7098f15 100644
  2911. --- a/arch/x86/kernel/irq_32.c
  2912. +++ b/arch/x86/kernel/irq_32.c
  2913. @@ -128,6 +128,7 @@ void irq_ctx_init(int cpu)
  2914. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2915. }
  2916. +#ifndef CONFIG_PREEMPT_RT_FULL
  2917. void do_softirq_own_stack(void)
  2918. {
  2919. struct thread_info *curstk;
  2920. @@ -146,6 +147,7 @@ void do_softirq_own_stack(void)
  2921. call_on_stack(__do_softirq, isp);
  2922. }
  2923. +#endif
  2924. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2925. {
  2926. diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
  2927. index 47190bd399e7..807950860fb7 100644
  2928. --- a/arch/x86/kernel/kvm.c
  2929. +++ b/arch/x86/kernel/kvm.c
  2930. @@ -36,6 +36,7 @@
  2931. #include <linux/kprobes.h>
  2932. #include <linux/debugfs.h>
  2933. #include <linux/nmi.h>
  2934. +#include <linux/swait.h>
  2935. #include <asm/timer.h>
  2936. #include <asm/cpu.h>
  2937. #include <asm/traps.h>
  2938. @@ -91,14 +92,14 @@ static void kvm_io_delay(void)
  2939. struct kvm_task_sleep_node {
  2940. struct hlist_node link;
  2941. - wait_queue_head_t wq;
  2942. + struct swait_queue_head wq;
  2943. u32 token;
  2944. int cpu;
  2945. bool halted;
  2946. };
  2947. static struct kvm_task_sleep_head {
  2948. - spinlock_t lock;
  2949. + raw_spinlock_t lock;
  2950. struct hlist_head list;
  2951. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  2952. @@ -122,17 +123,17 @@ void kvm_async_pf_task_wait(u32 token)
  2953. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  2954. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  2955. struct kvm_task_sleep_node n, *e;
  2956. - DEFINE_WAIT(wait);
  2957. + DECLARE_SWAITQUEUE(wait);
  2958. rcu_irq_enter();
  2959. - spin_lock(&b->lock);
  2960. + raw_spin_lock(&b->lock);
  2961. e = _find_apf_task(b, token);
  2962. if (e) {
  2963. /* dummy entry exist -> wake up was delivered ahead of PF */
  2964. hlist_del(&e->link);
  2965. kfree(e);
  2966. - spin_unlock(&b->lock);
  2967. + raw_spin_unlock(&b->lock);
  2968. rcu_irq_exit();
  2969. return;
  2970. @@ -141,13 +142,13 @@ void kvm_async_pf_task_wait(u32 token)
  2971. n.token = token;
  2972. n.cpu = smp_processor_id();
  2973. n.halted = is_idle_task(current) || preempt_count() > 1;
  2974. - init_waitqueue_head(&n.wq);
  2975. + init_swait_queue_head(&n.wq);
  2976. hlist_add_head(&n.link, &b->list);
  2977. - spin_unlock(&b->lock);
  2978. + raw_spin_unlock(&b->lock);
  2979. for (;;) {
  2980. if (!n.halted)
  2981. - prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  2982. + prepare_to_swait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  2983. if (hlist_unhashed(&n.link))
  2984. break;
  2985. @@ -166,7 +167,7 @@ void kvm_async_pf_task_wait(u32 token)
  2986. }
  2987. }
  2988. if (!n.halted)
  2989. - finish_wait(&n.wq, &wait);
  2990. + finish_swait(&n.wq, &wait);
  2991. rcu_irq_exit();
  2992. return;
  2993. @@ -178,8 +179,8 @@ static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  2994. hlist_del_init(&n->link);
  2995. if (n->halted)
  2996. smp_send_reschedule(n->cpu);
  2997. - else if (waitqueue_active(&n->wq))
  2998. - wake_up(&n->wq);
  2999. + else if (swait_active(&n->wq))
  3000. + swake_up(&n->wq);
  3001. }
  3002. static void apf_task_wake_all(void)
  3003. @@ -189,14 +190,14 @@ static void apf_task_wake_all(void)
  3004. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  3005. struct hlist_node *p, *next;
  3006. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  3007. - spin_lock(&b->lock);
  3008. + raw_spin_lock(&b->lock);
  3009. hlist_for_each_safe(p, next, &b->list) {
  3010. struct kvm_task_sleep_node *n =
  3011. hlist_entry(p, typeof(*n), link);
  3012. if (n->cpu == smp_processor_id())
  3013. apf_task_wake_one(n);
  3014. }
  3015. - spin_unlock(&b->lock);
  3016. + raw_spin_unlock(&b->lock);
  3017. }
  3018. }
  3019. @@ -212,7 +213,7 @@ void kvm_async_pf_task_wake(u32 token)
  3020. }
  3021. again:
  3022. - spin_lock(&b->lock);
  3023. + raw_spin_lock(&b->lock);
  3024. n = _find_apf_task(b, token);
  3025. if (!n) {
  3026. /*
  3027. @@ -225,17 +226,17 @@ again:
  3028. * Allocation failed! Busy wait while other cpu
  3029. * handles async PF.
  3030. */
  3031. - spin_unlock(&b->lock);
  3032. + raw_spin_unlock(&b->lock);
  3033. cpu_relax();
  3034. goto again;
  3035. }
  3036. n->token = token;
  3037. n->cpu = smp_processor_id();
  3038. - init_waitqueue_head(&n->wq);
  3039. + init_swait_queue_head(&n->wq);
  3040. hlist_add_head(&n->link, &b->list);
  3041. } else
  3042. apf_task_wake_one(n);
  3043. - spin_unlock(&b->lock);
  3044. + raw_spin_unlock(&b->lock);
  3045. return;
  3046. }
  3047. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  3048. @@ -486,7 +487,7 @@ void __init kvm_guest_init(void)
  3049. paravirt_ops_setup();
  3050. register_reboot_notifier(&kvm_pv_reboot_nb);
  3051. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  3052. - spin_lock_init(&async_pf_sleepers[i].lock);
  3053. + raw_spin_lock_init(&async_pf_sleepers[i].lock);
  3054. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  3055. x86_init.irqs.trap_init = kvm_apf_trap_init;
  3056. diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
  3057. index 697f90db0e37..424aec4a4c71 100644
  3058. --- a/arch/x86/kernel/nmi.c
  3059. +++ b/arch/x86/kernel/nmi.c
  3060. @@ -231,7 +231,7 @@ pci_serr_error(unsigned char reason, struct pt_regs *regs)
  3061. #endif
  3062. if (panic_on_unrecovered_nmi)
  3063. - panic("NMI: Not continuing");
  3064. + nmi_panic(regs, "NMI: Not continuing");
  3065. pr_emerg("Dazed and confused, but trying to continue\n");
  3066. @@ -255,8 +255,16 @@ io_check_error(unsigned char reason, struct pt_regs *regs)
  3067. reason, smp_processor_id());
  3068. show_regs(regs);
  3069. - if (panic_on_io_nmi)
  3070. - panic("NMI IOCK error: Not continuing");
  3071. + if (panic_on_io_nmi) {
  3072. + nmi_panic(regs, "NMI IOCK error: Not continuing");
  3073. +
  3074. + /*
  3075. + * If we end up here, it means we have received an NMI while
  3076. + * processing panic(). Simply return without delaying and
  3077. + * re-enabling NMIs.
  3078. + */
  3079. + return;
  3080. + }
  3081. /* Re-enable the IOCK line, wait for a few seconds */
  3082. reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
  3083. @@ -297,7 +305,7 @@ unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  3084. pr_emerg("Do you have a strange power saving mode enabled?\n");
  3085. if (unknown_nmi_panic || panic_on_unrecovered_nmi)
  3086. - panic("NMI: Not continuing");
  3087. + nmi_panic(regs, "NMI: Not continuing");
  3088. pr_emerg("Dazed and confused, but trying to continue\n");
  3089. }
  3090. diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
  3091. index 9f950917528b..4dd4beae917a 100644
  3092. --- a/arch/x86/kernel/process_32.c
  3093. +++ b/arch/x86/kernel/process_32.c
  3094. @@ -35,6 +35,7 @@
  3095. #include <linux/uaccess.h>
  3096. #include <linux/io.h>
  3097. #include <linux/kdebug.h>
  3098. +#include <linux/highmem.h>
  3099. #include <asm/pgtable.h>
  3100. #include <asm/ldt.h>
  3101. @@ -210,6 +211,35 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  3102. }
  3103. EXPORT_SYMBOL_GPL(start_thread);
  3104. +#ifdef CONFIG_PREEMPT_RT_FULL
  3105. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  3106. +{
  3107. + int i;
  3108. +
  3109. + /*
  3110. + * Clear @prev's kmap_atomic mappings
  3111. + */
  3112. + for (i = 0; i < prev_p->kmap_idx; i++) {
  3113. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3114. + pte_t *ptep = kmap_pte - idx;
  3115. +
  3116. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  3117. + }
  3118. + /*
  3119. + * Restore @next_p's kmap_atomic mappings
  3120. + */
  3121. + for (i = 0; i < next_p->kmap_idx; i++) {
  3122. + int idx = i + KM_TYPE_NR * smp_processor_id();
  3123. +
  3124. + if (!pte_none(next_p->kmap_pte[i]))
  3125. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  3126. + }
  3127. +}
  3128. +#else
  3129. +static inline void
  3130. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  3131. +#endif
  3132. +
  3133. /*
  3134. * switch_to(x,y) should switch tasks from x to y.
  3135. @@ -286,6 +316,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  3136. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  3137. __switch_to_xtra(prev_p, next_p, tss);
  3138. + switch_kmaps(prev_p, next_p);
  3139. +
  3140. /*
  3141. * Leave lazy mode, flushing any hypercalls made here.
  3142. * This must be done before restoring TLS segments so
  3143. diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c
  3144. index f660d63f40fe..8384207adde2 100644
  3145. --- a/arch/x86/kernel/reboot.c
  3146. +++ b/arch/x86/kernel/reboot.c
  3147. @@ -726,6 +726,7 @@ static int crashing_cpu;
  3148. static nmi_shootdown_cb shootdown_callback;
  3149. static atomic_t waiting_for_crash_ipi;
  3150. +static int crash_ipi_issued;
  3151. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  3152. {
  3153. @@ -788,6 +789,9 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3154. smp_send_nmi_allbutself();
  3155. + /* Kick CPUs looping in NMI context. */
  3156. + WRITE_ONCE(crash_ipi_issued, 1);
  3157. +
  3158. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  3159. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  3160. mdelay(1);
  3161. @@ -796,6 +800,22 @@ void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3162. /* Leave the nmi callback set */
  3163. }
  3164. +
  3165. +/* Override the weak function in kernel/panic.c */
  3166. +void nmi_panic_self_stop(struct pt_regs *regs)
  3167. +{
  3168. + while (1) {
  3169. + /*
  3170. + * Wait for the crash dumping IPI to be issued, and then
  3171. + * call its callback directly.
  3172. + */
  3173. + if (READ_ONCE(crash_ipi_issued))
  3174. + crash_nmi_callback(0, regs); /* Don't return */
  3175. +
  3176. + cpu_relax();
  3177. + }
  3178. +}
  3179. +
  3180. #else /* !CONFIG_SMP */
  3181. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  3182. {
  3183. diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
  3184. index 4d30b865be30..20d9e9fb3b74 100644
  3185. --- a/arch/x86/kvm/lapic.c
  3186. +++ b/arch/x86/kvm/lapic.c
  3187. @@ -1195,7 +1195,7 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
  3188. static void apic_timer_expired(struct kvm_lapic *apic)
  3189. {
  3190. struct kvm_vcpu *vcpu = apic->vcpu;
  3191. - wait_queue_head_t *q = &vcpu->wq;
  3192. + struct swait_queue_head *q = &vcpu->wq;
  3193. struct kvm_timer *ktimer = &apic->lapic_timer;
  3194. if (atomic_read(&apic->lapic_timer.pending))
  3195. @@ -1204,8 +1204,8 @@ static void apic_timer_expired(struct kvm_lapic *apic)
  3196. atomic_inc(&apic->lapic_timer.pending);
  3197. kvm_set_pending_timer(vcpu);
  3198. - if (waitqueue_active(q))
  3199. - wake_up_interruptible(q);
  3200. + if (swait_active(q))
  3201. + swake_up(q);
  3202. if (apic_lvtt_tscdeadline(apic))
  3203. ktimer->expired_tscdeadline = ktimer->tscdeadline;
  3204. @@ -1801,6 +1801,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
  3205. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  3206. HRTIMER_MODE_ABS);
  3207. apic->lapic_timer.timer.function = apic_timer_fn;
  3208. + apic->lapic_timer.timer.irqsafe = 1;
  3209. /*
  3210. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  3211. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  3212. index be222666b1c2..697457897e7c 100644
  3213. --- a/arch/x86/kvm/x86.c
  3214. +++ b/arch/x86/kvm/x86.c
  3215. @@ -5793,6 +5793,13 @@ int kvm_arch_init(void *opaque)
  3216. goto out;
  3217. }
  3218. +#ifdef CONFIG_PREEMPT_RT_FULL
  3219. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3220. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  3221. + return -EOPNOTSUPP;
  3222. + }
  3223. +#endif
  3224. +
  3225. r = kvm_mmu_module_init();
  3226. if (r)
  3227. goto out_free_percpu;
  3228. diff --git a/arch/x86/mm/highmem_32.c b/arch/x86/mm/highmem_32.c
  3229. index a6d739258137..bd24ba1c4a86 100644
  3230. --- a/arch/x86/mm/highmem_32.c
  3231. +++ b/arch/x86/mm/highmem_32.c
  3232. @@ -32,10 +32,11 @@ EXPORT_SYMBOL(kunmap);
  3233. */
  3234. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3235. {
  3236. + pte_t pte = mk_pte(page, prot);
  3237. unsigned long vaddr;
  3238. int idx, type;
  3239. - preempt_disable();
  3240. + preempt_disable_nort();
  3241. pagefault_disable();
  3242. if (!PageHighMem(page))
  3243. @@ -45,7 +46,10 @@ void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  3244. idx = type + KM_TYPE_NR*smp_processor_id();
  3245. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3246. BUG_ON(!pte_none(*(kmap_pte-idx)));
  3247. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  3248. +#ifdef CONFIG_PREEMPT_RT_FULL
  3249. + current->kmap_pte[type] = pte;
  3250. +#endif
  3251. + set_pte(kmap_pte-idx, pte);
  3252. arch_flush_lazy_mmu_mode();
  3253. return (void *)vaddr;
  3254. @@ -88,6 +92,9 @@ void __kunmap_atomic(void *kvaddr)
  3255. * is a bad idea also, in case the page changes cacheability
  3256. * attributes or becomes a protected page in a hypervisor.
  3257. */
  3258. +#ifdef CONFIG_PREEMPT_RT_FULL
  3259. + current->kmap_pte[type] = __pte(0);
  3260. +#endif
  3261. kpte_clear_flush(kmap_pte-idx, vaddr);
  3262. kmap_atomic_idx_pop();
  3263. arch_flush_lazy_mmu_mode();
  3264. @@ -100,7 +107,7 @@ void __kunmap_atomic(void *kvaddr)
  3265. #endif
  3266. pagefault_enable();
  3267. - preempt_enable();
  3268. + preempt_enable_nort();
  3269. }
  3270. EXPORT_SYMBOL(__kunmap_atomic);
  3271. diff --git a/arch/x86/mm/iomap_32.c b/arch/x86/mm/iomap_32.c
  3272. index 9c0ff045fdd4..dd25dd1671b6 100644
  3273. --- a/arch/x86/mm/iomap_32.c
  3274. +++ b/arch/x86/mm/iomap_32.c
  3275. @@ -56,6 +56,7 @@ EXPORT_SYMBOL_GPL(iomap_free);
  3276. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3277. {
  3278. + pte_t pte = pfn_pte(pfn, prot);
  3279. unsigned long vaddr;
  3280. int idx, type;
  3281. @@ -65,7 +66,12 @@ void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  3282. type = kmap_atomic_idx_push();
  3283. idx = type + KM_TYPE_NR * smp_processor_id();
  3284. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  3285. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  3286. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  3287. +
  3288. +#ifdef CONFIG_PREEMPT_RT_FULL
  3289. + current->kmap_pte[type] = pte;
  3290. +#endif
  3291. + set_pte(kmap_pte - idx, pte);
  3292. arch_flush_lazy_mmu_mode();
  3293. return (void *)vaddr;
  3294. @@ -113,6 +119,9 @@ iounmap_atomic(void __iomem *kvaddr)
  3295. * is a bad idea also, in case the page changes cacheability
  3296. * attributes or becomes a protected page in a hypervisor.
  3297. */
  3298. +#ifdef CONFIG_PREEMPT_RT_FULL
  3299. + current->kmap_pte[type] = __pte(0);
  3300. +#endif
  3301. kpte_clear_flush(kmap_pte-idx, vaddr);
  3302. kmap_atomic_idx_pop();
  3303. }
  3304. diff --git a/arch/x86/platform/uv/tlb_uv.c b/arch/x86/platform/uv/tlb_uv.c
  3305. index 3b6ec42718e4..7871083de089 100644
  3306. --- a/arch/x86/platform/uv/tlb_uv.c
  3307. +++ b/arch/x86/platform/uv/tlb_uv.c
  3308. @@ -714,9 +714,9 @@ static void destination_plugged(struct bau_desc *bau_desc,
  3309. quiesce_local_uvhub(hmaster);
  3310. - spin_lock(&hmaster->queue_lock);
  3311. + raw_spin_lock(&hmaster->queue_lock);
  3312. reset_with_ipi(&bau_desc->distribution, bcp);
  3313. - spin_unlock(&hmaster->queue_lock);
  3314. + raw_spin_unlock(&hmaster->queue_lock);
  3315. end_uvhub_quiesce(hmaster);
  3316. @@ -736,9 +736,9 @@ static void destination_timeout(struct bau_desc *bau_desc,
  3317. quiesce_local_uvhub(hmaster);
  3318. - spin_lock(&hmaster->queue_lock);
  3319. + raw_spin_lock(&hmaster->queue_lock);
  3320. reset_with_ipi(&bau_desc->distribution, bcp);
  3321. - spin_unlock(&hmaster->queue_lock);
  3322. + raw_spin_unlock(&hmaster->queue_lock);
  3323. end_uvhub_quiesce(hmaster);
  3324. @@ -759,7 +759,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3325. cycles_t tm1;
  3326. hmaster = bcp->uvhub_master;
  3327. - spin_lock(&hmaster->disable_lock);
  3328. + raw_spin_lock(&hmaster->disable_lock);
  3329. if (!bcp->baudisabled) {
  3330. stat->s_bau_disabled++;
  3331. tm1 = get_cycles();
  3332. @@ -772,7 +772,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  3333. }
  3334. }
  3335. }
  3336. - spin_unlock(&hmaster->disable_lock);
  3337. + raw_spin_unlock(&hmaster->disable_lock);
  3338. }
  3339. static void count_max_concurr(int stat, struct bau_control *bcp,
  3340. @@ -835,7 +835,7 @@ static void record_send_stats(cycles_t time1, cycles_t time2,
  3341. */
  3342. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  3343. {
  3344. - spinlock_t *lock = &hmaster->uvhub_lock;
  3345. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  3346. atomic_t *v;
  3347. v = &hmaster->active_descriptor_count;
  3348. @@ -968,7 +968,7 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3349. struct bau_control *hmaster;
  3350. hmaster = bcp->uvhub_master;
  3351. - spin_lock(&hmaster->disable_lock);
  3352. + raw_spin_lock(&hmaster->disable_lock);
  3353. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  3354. stat->s_bau_reenabled++;
  3355. for_each_present_cpu(tcpu) {
  3356. @@ -980,10 +980,10 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  3357. tbcp->period_giveups = 0;
  3358. }
  3359. }
  3360. - spin_unlock(&hmaster->disable_lock);
  3361. + raw_spin_unlock(&hmaster->disable_lock);
  3362. return 0;
  3363. }
  3364. - spin_unlock(&hmaster->disable_lock);
  3365. + raw_spin_unlock(&hmaster->disable_lock);
  3366. return -1;
  3367. }
  3368. @@ -1901,9 +1901,9 @@ static void __init init_per_cpu_tunables(void)
  3369. bcp->cong_reps = congested_reps;
  3370. bcp->disabled_period = sec_2_cycles(disabled_period);
  3371. bcp->giveup_limit = giveup_limit;
  3372. - spin_lock_init(&bcp->queue_lock);
  3373. - spin_lock_init(&bcp->uvhub_lock);
  3374. - spin_lock_init(&bcp->disable_lock);
  3375. + raw_spin_lock_init(&bcp->queue_lock);
  3376. + raw_spin_lock_init(&bcp->uvhub_lock);
  3377. + raw_spin_lock_init(&bcp->disable_lock);
  3378. }
  3379. }
  3380. diff --git a/arch/x86/platform/uv/uv_time.c b/arch/x86/platform/uv/uv_time.c
  3381. index 2b158a9fa1d7..5e0b122620cb 100644
  3382. --- a/arch/x86/platform/uv/uv_time.c
  3383. +++ b/arch/x86/platform/uv/uv_time.c
  3384. @@ -57,7 +57,7 @@ static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  3385. /* There is one of these allocated per node */
  3386. struct uv_rtc_timer_head {
  3387. - spinlock_t lock;
  3388. + raw_spinlock_t lock;
  3389. /* next cpu waiting for timer, local node relative: */
  3390. int next_cpu;
  3391. /* number of cpus on this node: */
  3392. @@ -177,7 +177,7 @@ static __init int uv_rtc_allocate_timers(void)
  3393. uv_rtc_deallocate_timers();
  3394. return -ENOMEM;
  3395. }
  3396. - spin_lock_init(&head->lock);
  3397. + raw_spin_lock_init(&head->lock);
  3398. head->ncpus = uv_blade_nr_possible_cpus(bid);
  3399. head->next_cpu = -1;
  3400. blade_info[bid] = head;
  3401. @@ -231,7 +231,7 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3402. unsigned long flags;
  3403. int next_cpu;
  3404. - spin_lock_irqsave(&head->lock, flags);
  3405. + raw_spin_lock_irqsave(&head->lock, flags);
  3406. next_cpu = head->next_cpu;
  3407. *t = expires;
  3408. @@ -243,12 +243,12 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  3409. if (uv_setup_intr(cpu, expires)) {
  3410. *t = ULLONG_MAX;
  3411. uv_rtc_find_next_timer(head, pnode);
  3412. - spin_unlock_irqrestore(&head->lock, flags);
  3413. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3414. return -ETIME;
  3415. }
  3416. }
  3417. - spin_unlock_irqrestore(&head->lock, flags);
  3418. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3419. return 0;
  3420. }
  3421. @@ -267,7 +267,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3422. unsigned long flags;
  3423. int rc = 0;
  3424. - spin_lock_irqsave(&head->lock, flags);
  3425. + raw_spin_lock_irqsave(&head->lock, flags);
  3426. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  3427. rc = 1;
  3428. @@ -279,7 +279,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3429. uv_rtc_find_next_timer(head, pnode);
  3430. }
  3431. - spin_unlock_irqrestore(&head->lock, flags);
  3432. + raw_spin_unlock_irqrestore(&head->lock, flags);
  3433. return rc;
  3434. }
  3435. @@ -299,13 +299,18 @@ static int uv_rtc_unset_timer(int cpu, int force)
  3436. static cycle_t uv_read_rtc(struct clocksource *cs)
  3437. {
  3438. unsigned long offset;
  3439. + cycle_t cycles;
  3440. + preempt_disable();
  3441. if (uv_get_min_hub_revision_id() == 1)
  3442. offset = 0;
  3443. else
  3444. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  3445. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3446. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  3447. + preempt_enable();
  3448. +
  3449. + return cycles;
  3450. }
  3451. /*
  3452. diff --git a/block/blk-core.c b/block/blk-core.c
  3453. index f8e64cac981a..842cfe492cf4 100644
  3454. --- a/block/blk-core.c
  3455. +++ b/block/blk-core.c
  3456. @@ -125,6 +125,9 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
  3457. INIT_LIST_HEAD(&rq->queuelist);
  3458. INIT_LIST_HEAD(&rq->timeout_list);
  3459. +#ifdef CONFIG_PREEMPT_RT_FULL
  3460. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3461. +#endif
  3462. rq->cpu = -1;
  3463. rq->q = q;
  3464. rq->__sector = (sector_t) -1;
  3465. @@ -233,7 +236,7 @@ EXPORT_SYMBOL(blk_start_queue_async);
  3466. **/
  3467. void blk_start_queue(struct request_queue *q)
  3468. {
  3469. - WARN_ON(!irqs_disabled());
  3470. + WARN_ON_NONRT(!irqs_disabled());
  3471. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  3472. __blk_run_queue(q);
  3473. @@ -657,7 +660,7 @@ int blk_queue_enter(struct request_queue *q, gfp_t gfp)
  3474. if (!gfpflags_allow_blocking(gfp))
  3475. return -EBUSY;
  3476. - ret = wait_event_interruptible(q->mq_freeze_wq,
  3477. + ret = swait_event_interruptible(q->mq_freeze_wq,
  3478. !atomic_read(&q->mq_freeze_depth) ||
  3479. blk_queue_dying(q));
  3480. if (blk_queue_dying(q))
  3481. @@ -677,7 +680,7 @@ static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  3482. struct request_queue *q =
  3483. container_of(ref, struct request_queue, q_usage_counter);
  3484. - wake_up_all(&q->mq_freeze_wq);
  3485. + swake_up_all(&q->mq_freeze_wq);
  3486. }
  3487. struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3488. @@ -739,7 +742,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  3489. q->bypass_depth = 1;
  3490. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  3491. - init_waitqueue_head(&q->mq_freeze_wq);
  3492. + init_swait_queue_head(&q->mq_freeze_wq);
  3493. /*
  3494. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  3495. @@ -3198,7 +3201,7 @@ static void queue_unplugged(struct request_queue *q, unsigned int depth,
  3496. blk_run_queue_async(q);
  3497. else
  3498. __blk_run_queue(q);
  3499. - spin_unlock(q->queue_lock);
  3500. + spin_unlock_irq(q->queue_lock);
  3501. }
  3502. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  3503. @@ -3246,7 +3249,6 @@ EXPORT_SYMBOL(blk_check_plugged);
  3504. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3505. {
  3506. struct request_queue *q;
  3507. - unsigned long flags;
  3508. struct request *rq;
  3509. LIST_HEAD(list);
  3510. unsigned int depth;
  3511. @@ -3266,11 +3268,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3512. q = NULL;
  3513. depth = 0;
  3514. - /*
  3515. - * Save and disable interrupts here, to avoid doing it for every
  3516. - * queue lock we have to take.
  3517. - */
  3518. - local_irq_save(flags);
  3519. while (!list_empty(&list)) {
  3520. rq = list_entry_rq(list.next);
  3521. list_del_init(&rq->queuelist);
  3522. @@ -3283,7 +3280,7 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3523. queue_unplugged(q, depth, from_schedule);
  3524. q = rq->q;
  3525. depth = 0;
  3526. - spin_lock(q->queue_lock);
  3527. + spin_lock_irq(q->queue_lock);
  3528. }
  3529. /*
  3530. @@ -3310,8 +3307,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  3531. */
  3532. if (q)
  3533. queue_unplugged(q, depth, from_schedule);
  3534. -
  3535. - local_irq_restore(flags);
  3536. }
  3537. void blk_finish_plug(struct blk_plug *plug)
  3538. diff --git a/block/blk-ioc.c b/block/blk-ioc.c
  3539. index 381cb50a673c..dc8785233d94 100644
  3540. --- a/block/blk-ioc.c
  3541. +++ b/block/blk-ioc.c
  3542. @@ -7,6 +7,7 @@
  3543. #include <linux/bio.h>
  3544. #include <linux/blkdev.h>
  3545. #include <linux/slab.h>
  3546. +#include <linux/delay.h>
  3547. #include "blk.h"
  3548. @@ -109,7 +110,7 @@ static void ioc_release_fn(struct work_struct *work)
  3549. spin_unlock(q->queue_lock);
  3550. } else {
  3551. spin_unlock_irqrestore(&ioc->lock, flags);
  3552. - cpu_relax();
  3553. + cpu_chill();
  3554. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  3555. }
  3556. }
  3557. @@ -187,7 +188,7 @@ retry:
  3558. spin_unlock(icq->q->queue_lock);
  3559. } else {
  3560. spin_unlock_irqrestore(&ioc->lock, flags);
  3561. - cpu_relax();
  3562. + cpu_chill();
  3563. goto retry;
  3564. }
  3565. }
  3566. diff --git a/block/blk-iopoll.c b/block/blk-iopoll.c
  3567. index 0736729d6494..3e21e31d0d7e 100644
  3568. --- a/block/blk-iopoll.c
  3569. +++ b/block/blk-iopoll.c
  3570. @@ -35,6 +35,7 @@ void blk_iopoll_sched(struct blk_iopoll *iop)
  3571. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  3572. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3573. local_irq_restore(flags);
  3574. + preempt_check_resched_rt();
  3575. }
  3576. EXPORT_SYMBOL(blk_iopoll_sched);
  3577. @@ -132,6 +133,7 @@ static void blk_iopoll_softirq(struct softirq_action *h)
  3578. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3579. local_irq_enable();
  3580. + preempt_check_resched_rt();
  3581. }
  3582. /**
  3583. @@ -201,6 +203,7 @@ static int blk_iopoll_cpu_notify(struct notifier_block *self,
  3584. this_cpu_ptr(&blk_cpu_iopoll));
  3585. __raise_softirq_irqoff(BLOCK_IOPOLL_SOFTIRQ);
  3586. local_irq_enable();
  3587. + preempt_check_resched_rt();
  3588. }
  3589. return NOTIFY_OK;
  3590. diff --git a/block/blk-mq-cpu.c b/block/blk-mq-cpu.c
  3591. index bb3ed488f7b5..628c6c13c482 100644
  3592. --- a/block/blk-mq-cpu.c
  3593. +++ b/block/blk-mq-cpu.c
  3594. @@ -16,7 +16,7 @@
  3595. #include "blk-mq.h"
  3596. static LIST_HEAD(blk_mq_cpu_notify_list);
  3597. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  3598. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  3599. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3600. unsigned long action, void *hcpu)
  3601. @@ -25,7 +25,10 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3602. struct blk_mq_cpu_notifier *notify;
  3603. int ret = NOTIFY_OK;
  3604. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3605. + if (action != CPU_POST_DEAD)
  3606. + return NOTIFY_OK;
  3607. +
  3608. + spin_lock(&blk_mq_cpu_notify_lock);
  3609. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  3610. ret = notify->notify(notify->data, action, cpu);
  3611. @@ -33,7 +36,7 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  3612. break;
  3613. }
  3614. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3615. + spin_unlock(&blk_mq_cpu_notify_lock);
  3616. return ret;
  3617. }
  3618. @@ -41,16 +44,16 @@ void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3619. {
  3620. BUG_ON(!notifier->notify);
  3621. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3622. + spin_lock(&blk_mq_cpu_notify_lock);
  3623. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  3624. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3625. + spin_unlock(&blk_mq_cpu_notify_lock);
  3626. }
  3627. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  3628. {
  3629. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  3630. + spin_lock(&blk_mq_cpu_notify_lock);
  3631. list_del(&notifier->list);
  3632. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  3633. + spin_unlock(&blk_mq_cpu_notify_lock);
  3634. }
  3635. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  3636. diff --git a/block/blk-mq.c b/block/blk-mq.c
  3637. index 6d6f8feb48c0..7cdf19e4aaea 100644
  3638. --- a/block/blk-mq.c
  3639. +++ b/block/blk-mq.c
  3640. @@ -92,7 +92,7 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
  3641. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  3642. {
  3643. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3644. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  3645. }
  3646. /*
  3647. @@ -130,7 +130,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)
  3648. WARN_ON_ONCE(freeze_depth < 0);
  3649. if (!freeze_depth) {
  3650. percpu_ref_reinit(&q->q_usage_counter);
  3651. - wake_up_all(&q->mq_freeze_wq);
  3652. + swake_up_all(&q->mq_freeze_wq);
  3653. }
  3654. }
  3655. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  3656. @@ -149,7 +149,7 @@ void blk_mq_wake_waiters(struct request_queue *q)
  3657. * dying, we need to ensure that processes currently waiting on
  3658. * the queue are notified as well.
  3659. */
  3660. - wake_up_all(&q->mq_freeze_wq);
  3661. + swake_up_all(&q->mq_freeze_wq);
  3662. }
  3663. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  3664. @@ -196,6 +196,9 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  3665. rq->resid_len = 0;
  3666. rq->sense = NULL;
  3667. +#ifdef CONFIG_PREEMPT_RT_FULL
  3668. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  3669. +#endif
  3670. INIT_LIST_HEAD(&rq->timeout_list);
  3671. rq->timeout = 0;
  3672. @@ -325,6 +328,17 @@ void blk_mq_end_request(struct request *rq, int error)
  3673. }
  3674. EXPORT_SYMBOL(blk_mq_end_request);
  3675. +#ifdef CONFIG_PREEMPT_RT_FULL
  3676. +
  3677. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  3678. +{
  3679. + struct request *rq = container_of(work, struct request, work);
  3680. +
  3681. + rq->q->softirq_done_fn(rq);
  3682. +}
  3683. +
  3684. +#else
  3685. +
  3686. static void __blk_mq_complete_request_remote(void *data)
  3687. {
  3688. struct request *rq = data;
  3689. @@ -332,6 +346,8 @@ static void __blk_mq_complete_request_remote(void *data)
  3690. rq->q->softirq_done_fn(rq);
  3691. }
  3692. +#endif
  3693. +
  3694. static void blk_mq_ipi_complete_request(struct request *rq)
  3695. {
  3696. struct blk_mq_ctx *ctx = rq->mq_ctx;
  3697. @@ -343,19 +359,23 @@ static void blk_mq_ipi_complete_request(struct request *rq)
  3698. return;
  3699. }
  3700. - cpu = get_cpu();
  3701. + cpu = get_cpu_light();
  3702. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  3703. shared = cpus_share_cache(cpu, ctx->cpu);
  3704. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  3705. +#ifdef CONFIG_PREEMPT_RT_FULL
  3706. + schedule_work_on(ctx->cpu, &rq->work);
  3707. +#else
  3708. rq->csd.func = __blk_mq_complete_request_remote;
  3709. rq->csd.info = rq;
  3710. rq->csd.flags = 0;
  3711. smp_call_function_single_async(ctx->cpu, &rq->csd);
  3712. +#endif
  3713. } else {
  3714. rq->q->softirq_done_fn(rq);
  3715. }
  3716. - put_cpu();
  3717. + put_cpu_light();
  3718. }
  3719. static void __blk_mq_complete_request(struct request *rq)
  3720. @@ -862,14 +882,14 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  3721. return;
  3722. if (!async) {
  3723. - int cpu = get_cpu();
  3724. + int cpu = get_cpu_light();
  3725. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  3726. __blk_mq_run_hw_queue(hctx);
  3727. - put_cpu();
  3728. + put_cpu_light();
  3729. return;
  3730. }
  3731. - put_cpu();
  3732. + put_cpu_light();
  3733. }
  3734. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  3735. @@ -1617,7 +1637,7 @@ static int blk_mq_hctx_notify(void *data, unsigned long action,
  3736. {
  3737. struct blk_mq_hw_ctx *hctx = data;
  3738. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3739. + if (action == CPU_POST_DEAD)
  3740. return blk_mq_hctx_cpu_offline(hctx, cpu);
  3741. /*
  3742. diff --git a/block/blk-mq.h b/block/blk-mq.h
  3743. index 713820b47b31..3cb6feb4fe23 100644
  3744. --- a/block/blk-mq.h
  3745. +++ b/block/blk-mq.h
  3746. @@ -74,7 +74,10 @@ struct blk_align_bitmap {
  3747. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3748. unsigned int cpu)
  3749. {
  3750. - return per_cpu_ptr(q->queue_ctx, cpu);
  3751. + struct blk_mq_ctx *ctx;
  3752. +
  3753. + ctx = per_cpu_ptr(q->queue_ctx, cpu);
  3754. + return ctx;
  3755. }
  3756. /*
  3757. @@ -85,12 +88,12 @@ static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3758. */
  3759. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  3760. {
  3761. - return __blk_mq_get_ctx(q, get_cpu());
  3762. + return __blk_mq_get_ctx(q, get_cpu_light());
  3763. }
  3764. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  3765. {
  3766. - put_cpu();
  3767. + put_cpu_light();
  3768. }
  3769. struct blk_mq_alloc_data {
  3770. diff --git a/block/blk-softirq.c b/block/blk-softirq.c
  3771. index 53b1737e978d..81c3c0a62edf 100644
  3772. --- a/block/blk-softirq.c
  3773. +++ b/block/blk-softirq.c
  3774. @@ -51,6 +51,7 @@ static void trigger_softirq(void *data)
  3775. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3776. local_irq_restore(flags);
  3777. + preempt_check_resched_rt();
  3778. }
  3779. /*
  3780. @@ -93,6 +94,7 @@ static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
  3781. this_cpu_ptr(&blk_cpu_done));
  3782. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3783. local_irq_enable();
  3784. + preempt_check_resched_rt();
  3785. }
  3786. return NOTIFY_OK;
  3787. @@ -150,6 +152,7 @@ do_local:
  3788. goto do_local;
  3789. local_irq_restore(flags);
  3790. + preempt_check_resched_rt();
  3791. }
  3792. /**
  3793. diff --git a/block/bounce.c b/block/bounce.c
  3794. index 1cb5dd3a5da1..2f1ec8a67cbe 100644
  3795. --- a/block/bounce.c
  3796. +++ b/block/bounce.c
  3797. @@ -55,11 +55,11 @@ static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  3798. unsigned long flags;
  3799. unsigned char *vto;
  3800. - local_irq_save(flags);
  3801. + local_irq_save_nort(flags);
  3802. vto = kmap_atomic(to->bv_page);
  3803. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  3804. kunmap_atomic(vto);
  3805. - local_irq_restore(flags);
  3806. + local_irq_restore_nort(flags);
  3807. }
  3808. #else /* CONFIG_HIGHMEM */
  3809. diff --git a/crypto/algapi.c b/crypto/algapi.c
  3810. index 59bf491fe3d8..f98e79c8cd77 100644
  3811. --- a/crypto/algapi.c
  3812. +++ b/crypto/algapi.c
  3813. @@ -719,13 +719,13 @@ EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  3814. int crypto_register_notifier(struct notifier_block *nb)
  3815. {
  3816. - return blocking_notifier_chain_register(&crypto_chain, nb);
  3817. + return srcu_notifier_chain_register(&crypto_chain, nb);
  3818. }
  3819. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  3820. int crypto_unregister_notifier(struct notifier_block *nb)
  3821. {
  3822. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  3823. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  3824. }
  3825. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  3826. diff --git a/crypto/api.c b/crypto/api.c
  3827. index bbc147cb5dec..bc1a848f02ec 100644
  3828. --- a/crypto/api.c
  3829. +++ b/crypto/api.c
  3830. @@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
  3831. DECLARE_RWSEM(crypto_alg_sem);
  3832. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  3833. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  3834. +SRCU_NOTIFIER_HEAD(crypto_chain);
  3835. EXPORT_SYMBOL_GPL(crypto_chain);
  3836. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  3837. @@ -236,10 +236,10 @@ int crypto_probing_notify(unsigned long val, void *v)
  3838. {
  3839. int ok;
  3840. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3841. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3842. if (ok == NOTIFY_DONE) {
  3843. request_module("cryptomgr");
  3844. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3845. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3846. }
  3847. return ok;
  3848. diff --git a/crypto/internal.h b/crypto/internal.h
  3849. index 00e42a3ed814..2e85551e235f 100644
  3850. --- a/crypto/internal.h
  3851. +++ b/crypto/internal.h
  3852. @@ -47,7 +47,7 @@ struct crypto_larval {
  3853. extern struct list_head crypto_alg_list;
  3854. extern struct rw_semaphore crypto_alg_sem;
  3855. -extern struct blocking_notifier_head crypto_chain;
  3856. +extern struct srcu_notifier_head crypto_chain;
  3857. #ifdef CONFIG_PROC_FS
  3858. void __init crypto_init_proc(void);
  3859. @@ -143,7 +143,7 @@ static inline int crypto_is_moribund(struct crypto_alg *alg)
  3860. static inline void crypto_notify(unsigned long val, void *v)
  3861. {
  3862. - blocking_notifier_call_chain(&crypto_chain, val, v);
  3863. + srcu_notifier_call_chain(&crypto_chain, val, v);
  3864. }
  3865. #endif /* _CRYPTO_INTERNAL_H */
  3866. diff --git a/drivers/acpi/acpica/acglobal.h b/drivers/acpi/acpica/acglobal.h
  3867. index faa97604d878..941497f31cf0 100644
  3868. --- a/drivers/acpi/acpica/acglobal.h
  3869. +++ b/drivers/acpi/acpica/acglobal.h
  3870. @@ -116,7 +116,7 @@ ACPI_GLOBAL(u8, acpi_gbl_global_lock_pending);
  3871. * interrupt level
  3872. */
  3873. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  3874. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3875. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3876. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  3877. /* Mutex for _OSI support */
  3878. diff --git a/drivers/acpi/acpica/hwregs.c b/drivers/acpi/acpica/hwregs.c
  3879. index 3cf77afd142c..dc32e72132f1 100644
  3880. --- a/drivers/acpi/acpica/hwregs.c
  3881. +++ b/drivers/acpi/acpica/hwregs.c
  3882. @@ -269,14 +269,14 @@ acpi_status acpi_hw_clear_acpi_status(void)
  3883. ACPI_BITMASK_ALL_FIXED_STATUS,
  3884. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  3885. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3886. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3887. /* Clear the fixed events in PM1 A/B */
  3888. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  3889. ACPI_BITMASK_ALL_FIXED_STATUS);
  3890. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3891. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3892. if (ACPI_FAILURE(status)) {
  3893. goto exit;
  3894. diff --git a/drivers/acpi/acpica/hwxface.c b/drivers/acpi/acpica/hwxface.c
  3895. index 5f97468df8ff..8c017f15da7d 100644
  3896. --- a/drivers/acpi/acpica/hwxface.c
  3897. +++ b/drivers/acpi/acpica/hwxface.c
  3898. @@ -374,7 +374,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3899. return_ACPI_STATUS(AE_BAD_PARAMETER);
  3900. }
  3901. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3902. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3903. /*
  3904. * At this point, we know that the parent register is one of the
  3905. @@ -435,7 +435,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3906. unlock_and_exit:
  3907. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3908. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3909. return_ACPI_STATUS(status);
  3910. }
  3911. diff --git a/drivers/acpi/acpica/utmutex.c b/drivers/acpi/acpica/utmutex.c
  3912. index ce406e39b669..41a75eb3ae9d 100644
  3913. --- a/drivers/acpi/acpica/utmutex.c
  3914. +++ b/drivers/acpi/acpica/utmutex.c
  3915. @@ -88,7 +88,7 @@ acpi_status acpi_ut_mutex_initialize(void)
  3916. return_ACPI_STATUS (status);
  3917. }
  3918. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  3919. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  3920. if (ACPI_FAILURE (status)) {
  3921. return_ACPI_STATUS (status);
  3922. }
  3923. @@ -156,7 +156,7 @@ void acpi_ut_mutex_terminate(void)
  3924. /* Delete the spinlocks */
  3925. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  3926. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  3927. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  3928. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  3929. /* Delete the reader/writer lock */
  3930. diff --git a/drivers/ata/libata-sff.c b/drivers/ata/libata-sff.c
  3931. index 7dbba387d12a..65beb7abb4e7 100644
  3932. --- a/drivers/ata/libata-sff.c
  3933. +++ b/drivers/ata/libata-sff.c
  3934. @@ -678,9 +678,9 @@ unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  3935. unsigned long flags;
  3936. unsigned int consumed;
  3937. - local_irq_save(flags);
  3938. + local_irq_save_nort(flags);
  3939. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  3940. - local_irq_restore(flags);
  3941. + local_irq_restore_nort(flags);
  3942. return consumed;
  3943. }
  3944. @@ -719,7 +719,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  3945. unsigned long flags;
  3946. /* FIXME: use a bounce buffer */
  3947. - local_irq_save(flags);
  3948. + local_irq_save_nort(flags);
  3949. buf = kmap_atomic(page);
  3950. /* do the actual data transfer */
  3951. @@ -727,7 +727,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  3952. do_write);
  3953. kunmap_atomic(buf);
  3954. - local_irq_restore(flags);
  3955. + local_irq_restore_nort(flags);
  3956. } else {
  3957. buf = page_address(page);
  3958. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  3959. @@ -864,7 +864,7 @@ next_sg:
  3960. unsigned long flags;
  3961. /* FIXME: use bounce buffer */
  3962. - local_irq_save(flags);
  3963. + local_irq_save_nort(flags);
  3964. buf = kmap_atomic(page);
  3965. /* do the actual data transfer */
  3966. @@ -872,7 +872,7 @@ next_sg:
  3967. count, rw);
  3968. kunmap_atomic(buf);
  3969. - local_irq_restore(flags);
  3970. + local_irq_restore_nort(flags);
  3971. } else {
  3972. buf = page_address(page);
  3973. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  3974. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
  3975. index 370c2f76016d..65e0b375a291 100644
  3976. --- a/drivers/block/zram/zram_drv.c
  3977. +++ b/drivers/block/zram/zram_drv.c
  3978. @@ -520,6 +520,8 @@ static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
  3979. goto out_error;
  3980. }
  3981. + zram_meta_init_table_locks(meta, disksize);
  3982. +
  3983. return meta;
  3984. out_error:
  3985. @@ -568,12 +570,12 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  3986. unsigned long handle;
  3987. size_t size;
  3988. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3989. + zram_lock_table(&meta->table[index]);
  3990. handle = meta->table[index].handle;
  3991. size = zram_get_obj_size(meta, index);
  3992. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  3993. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3994. + zram_unlock_table(&meta->table[index]);
  3995. clear_page(mem);
  3996. return 0;
  3997. }
  3998. @@ -584,7 +586,7 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  3999. else
  4000. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  4001. zs_unmap_object(meta->mem_pool, handle);
  4002. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4003. + zram_unlock_table(&meta->table[index]);
  4004. /* Should NEVER happen. Return bio error if it does. */
  4005. if (unlikely(ret)) {
  4006. @@ -604,14 +606,14 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  4007. struct zram_meta *meta = zram->meta;
  4008. page = bvec->bv_page;
  4009. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4010. + zram_lock_table(&meta->table[index]);
  4011. if (unlikely(!meta->table[index].handle) ||
  4012. zram_test_flag(meta, index, ZRAM_ZERO)) {
  4013. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4014. + zram_unlock_table(&meta->table[index]);
  4015. handle_zero_page(bvec);
  4016. return 0;
  4017. }
  4018. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4019. + zram_unlock_table(&meta->table[index]);
  4020. if (is_partial_io(bvec))
  4021. /* Use a temporary buffer to decompress the page */
  4022. @@ -689,10 +691,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4023. if (user_mem)
  4024. kunmap_atomic(user_mem);
  4025. /* Free memory associated with this sector now. */
  4026. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4027. + zram_lock_table(&meta->table[index]);
  4028. zram_free_page(zram, index);
  4029. zram_set_flag(meta, index, ZRAM_ZERO);
  4030. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4031. + zram_unlock_table(&meta->table[index]);
  4032. atomic64_inc(&zram->stats.zero_pages);
  4033. ret = 0;
  4034. @@ -752,12 +754,12 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  4035. * Free memory associated with this sector
  4036. * before overwriting unused sectors.
  4037. */
  4038. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4039. + zram_lock_table(&meta->table[index]);
  4040. zram_free_page(zram, index);
  4041. meta->table[index].handle = handle;
  4042. zram_set_obj_size(meta, index, clen);
  4043. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4044. + zram_unlock_table(&meta->table[index]);
  4045. /* Update stats */
  4046. atomic64_add(clen, &zram->stats.compr_data_size);
  4047. @@ -800,9 +802,9 @@ static void zram_bio_discard(struct zram *zram, u32 index,
  4048. }
  4049. while (n >= PAGE_SIZE) {
  4050. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4051. + zram_lock_table(&meta->table[index]);
  4052. zram_free_page(zram, index);
  4053. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4054. + zram_unlock_table(&meta->table[index]);
  4055. atomic64_inc(&zram->stats.notify_free);
  4056. index++;
  4057. n -= PAGE_SIZE;
  4058. @@ -928,9 +930,9 @@ static void zram_slot_free_notify(struct block_device *bdev,
  4059. zram = bdev->bd_disk->private_data;
  4060. meta = zram->meta;
  4061. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  4062. + zram_lock_table(&meta->table[index]);
  4063. zram_free_page(zram, index);
  4064. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  4065. + zram_unlock_table(&meta->table[index]);
  4066. atomic64_inc(&zram->stats.notify_free);
  4067. }
  4068. diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
  4069. index 8e92339686d7..9e3e953d680e 100644
  4070. --- a/drivers/block/zram/zram_drv.h
  4071. +++ b/drivers/block/zram/zram_drv.h
  4072. @@ -72,6 +72,9 @@ enum zram_pageflags {
  4073. struct zram_table_entry {
  4074. unsigned long handle;
  4075. unsigned long value;
  4076. +#ifdef CONFIG_PREEMPT_RT_BASE
  4077. + spinlock_t lock;
  4078. +#endif
  4079. };
  4080. struct zram_stats {
  4081. @@ -119,4 +122,42 @@ struct zram {
  4082. */
  4083. bool claim; /* Protected by bdev->bd_mutex */
  4084. };
  4085. +
  4086. +#ifndef CONFIG_PREEMPT_RT_BASE
  4087. +static inline void zram_lock_table(struct zram_table_entry *table)
  4088. +{
  4089. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  4090. +}
  4091. +
  4092. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4093. +{
  4094. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  4095. +}
  4096. +
  4097. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  4098. +#else /* CONFIG_PREEMPT_RT_BASE */
  4099. +static inline void zram_lock_table(struct zram_table_entry *table)
  4100. +{
  4101. + spin_lock(&table->lock);
  4102. + __set_bit(ZRAM_ACCESS, &table->value);
  4103. +}
  4104. +
  4105. +static inline void zram_unlock_table(struct zram_table_entry *table)
  4106. +{
  4107. + __clear_bit(ZRAM_ACCESS, &table->value);
  4108. + spin_unlock(&table->lock);
  4109. +}
  4110. +
  4111. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  4112. +{
  4113. + size_t num_pages = disksize >> PAGE_SHIFT;
  4114. + size_t index;
  4115. +
  4116. + for (index = 0; index < num_pages; index++) {
  4117. + spinlock_t *lock = &meta->table[index].lock;
  4118. + spin_lock_init(lock);
  4119. + }
  4120. +}
  4121. +#endif /* CONFIG_PREEMPT_RT_BASE */
  4122. +
  4123. #endif
  4124. diff --git a/drivers/char/random.c b/drivers/char/random.c
  4125. index 491a4dce13fe..cf69b6b42208 100644
  4126. --- a/drivers/char/random.c
  4127. +++ b/drivers/char/random.c
  4128. @@ -799,8 +799,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4129. } sample;
  4130. long delta, delta2, delta3;
  4131. - preempt_disable();
  4132. -
  4133. sample.jiffies = jiffies;
  4134. sample.cycles = random_get_entropy();
  4135. sample.num = num;
  4136. @@ -841,7 +839,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  4137. */
  4138. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  4139. }
  4140. - preempt_enable();
  4141. }
  4142. void add_input_randomness(unsigned int type, unsigned int code,
  4143. @@ -894,28 +891,27 @@ static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
  4144. return *(ptr + f->reg_idx++);
  4145. }
  4146. -void add_interrupt_randomness(int irq, int irq_flags)
  4147. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  4148. {
  4149. struct entropy_store *r;
  4150. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  4151. - struct pt_regs *regs = get_irq_regs();
  4152. unsigned long now = jiffies;
  4153. cycles_t cycles = random_get_entropy();
  4154. __u32 c_high, j_high;
  4155. - __u64 ip;
  4156. unsigned long seed;
  4157. int credit = 0;
  4158. if (cycles == 0)
  4159. - cycles = get_reg(fast_pool, regs);
  4160. + cycles = get_reg(fast_pool, NULL);
  4161. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  4162. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  4163. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  4164. fast_pool->pool[1] ^= now ^ c_high;
  4165. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  4166. + if (!ip)
  4167. + ip = _RET_IP_;
  4168. fast_pool->pool[2] ^= ip;
  4169. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  4170. - get_reg(fast_pool, regs);
  4171. + get_reg(fast_pool, NULL);
  4172. fast_mix(fast_pool);
  4173. add_interrupt_bench(cycles);
  4174. diff --git a/drivers/clk/at91/clk-generated.c b/drivers/clk/at91/clk-generated.c
  4175. index abc80949e1dd..4ad3298eb372 100644
  4176. --- a/drivers/clk/at91/clk-generated.c
  4177. +++ b/drivers/clk/at91/clk-generated.c
  4178. @@ -15,8 +15,8 @@
  4179. #include <linux/clkdev.h>
  4180. #include <linux/clk/at91_pmc.h>
  4181. #include <linux/of.h>
  4182. -#include <linux/of_address.h>
  4183. -#include <linux/io.h>
  4184. +#include <linux/mfd/syscon.h>
  4185. +#include <linux/regmap.h>
  4186. #include "pmc.h"
  4187. @@ -28,8 +28,9 @@
  4188. struct clk_generated {
  4189. struct clk_hw hw;
  4190. - struct at91_pmc *pmc;
  4191. + struct regmap *regmap;
  4192. struct clk_range range;
  4193. + spinlock_t *lock;
  4194. u32 id;
  4195. u32 gckdiv;
  4196. u8 parent_id;
  4197. @@ -41,49 +42,52 @@ struct clk_generated {
  4198. static int clk_generated_enable(struct clk_hw *hw)
  4199. {
  4200. struct clk_generated *gck = to_clk_generated(hw);
  4201. - struct at91_pmc *pmc = gck->pmc;
  4202. - u32 tmp;
  4203. + unsigned long flags;
  4204. pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
  4205. __func__, gck->gckdiv, gck->parent_id);
  4206. - pmc_lock(pmc);
  4207. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4208. - tmp = pmc_read(pmc, AT91_PMC_PCR) &
  4209. - ~(AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK);
  4210. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_GCKCSS(gck->parent_id)
  4211. - | AT91_PMC_PCR_CMD
  4212. - | AT91_PMC_PCR_GCKDIV(gck->gckdiv)
  4213. - | AT91_PMC_PCR_GCKEN);
  4214. - pmc_unlock(pmc);
  4215. + spin_lock_irqsave(gck->lock, flags);
  4216. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4217. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4218. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4219. + AT91_PMC_PCR_GCKDIV_MASK | AT91_PMC_PCR_GCKCSS_MASK |
  4220. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4221. + AT91_PMC_PCR_GCKCSS(gck->parent_id) |
  4222. + AT91_PMC_PCR_CMD |
  4223. + AT91_PMC_PCR_GCKDIV(gck->gckdiv) |
  4224. + AT91_PMC_PCR_GCKEN);
  4225. + spin_unlock_irqrestore(gck->lock, flags);
  4226. return 0;
  4227. }
  4228. static void clk_generated_disable(struct clk_hw *hw)
  4229. {
  4230. struct clk_generated *gck = to_clk_generated(hw);
  4231. - struct at91_pmc *pmc = gck->pmc;
  4232. - u32 tmp;
  4233. -
  4234. - pmc_lock(pmc);
  4235. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4236. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_GCKEN;
  4237. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  4238. - pmc_unlock(pmc);
  4239. + unsigned long flags;
  4240. +
  4241. + spin_lock_irqsave(gck->lock, flags);
  4242. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4243. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4244. + regmap_update_bits(gck->regmap, AT91_PMC_PCR,
  4245. + AT91_PMC_PCR_CMD | AT91_PMC_PCR_GCKEN,
  4246. + AT91_PMC_PCR_CMD);
  4247. + spin_unlock_irqrestore(gck->lock, flags);
  4248. }
  4249. static int clk_generated_is_enabled(struct clk_hw *hw)
  4250. {
  4251. struct clk_generated *gck = to_clk_generated(hw);
  4252. - struct at91_pmc *pmc = gck->pmc;
  4253. - int ret;
  4254. + unsigned long flags;
  4255. + unsigned int status;
  4256. - pmc_lock(pmc);
  4257. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4258. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_GCKEN);
  4259. - pmc_unlock(pmc);
  4260. + spin_lock_irqsave(gck->lock, flags);
  4261. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4262. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4263. + regmap_read(gck->regmap, AT91_PMC_PCR, &status);
  4264. + spin_unlock_irqrestore(gck->lock, flags);
  4265. - return ret;
  4266. + return status & AT91_PMC_PCR_GCKEN ? 1 : 0;
  4267. }
  4268. static unsigned long
  4269. @@ -214,13 +218,14 @@ static const struct clk_ops generated_ops = {
  4270. */
  4271. static void clk_generated_startup(struct clk_generated *gck)
  4272. {
  4273. - struct at91_pmc *pmc = gck->pmc;
  4274. u32 tmp;
  4275. + unsigned long flags;
  4276. - pmc_lock(pmc);
  4277. - pmc_write(pmc, AT91_PMC_PCR, (gck->id & AT91_PMC_PCR_PID_MASK));
  4278. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  4279. - pmc_unlock(pmc);
  4280. + spin_lock_irqsave(gck->lock, flags);
  4281. + regmap_write(gck->regmap, AT91_PMC_PCR,
  4282. + (gck->id & AT91_PMC_PCR_PID_MASK));
  4283. + regmap_read(gck->regmap, AT91_PMC_PCR, &tmp);
  4284. + spin_unlock_irqrestore(gck->lock, flags);
  4285. gck->parent_id = (tmp & AT91_PMC_PCR_GCKCSS_MASK)
  4286. >> AT91_PMC_PCR_GCKCSS_OFFSET;
  4287. @@ -229,8 +234,8 @@ static void clk_generated_startup(struct clk_generated *gck)
  4288. }
  4289. static struct clk * __init
  4290. -at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4291. - const char **parent_names, u8 num_parents,
  4292. +at91_clk_register_generated(struct regmap *regmap, spinlock_t *lock, const char
  4293. + *name, const char **parent_names, u8 num_parents,
  4294. u8 id, const struct clk_range *range)
  4295. {
  4296. struct clk_generated *gck;
  4297. @@ -249,7 +254,8 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4298. gck->id = id;
  4299. gck->hw.init = &init;
  4300. - gck->pmc = pmc;
  4301. + gck->regmap = regmap;
  4302. + gck->lock = lock;
  4303. gck->range = *range;
  4304. clk = clk_register(NULL, &gck->hw);
  4305. @@ -261,8 +267,7 @@ at91_clk_register_generated(struct at91_pmc *pmc, const char *name,
  4306. return clk;
  4307. }
  4308. -void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4309. - struct at91_pmc *pmc)
  4310. +void __init of_sama5d2_clk_generated_setup(struct device_node *np)
  4311. {
  4312. int num;
  4313. u32 id;
  4314. @@ -272,6 +277,7 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4315. const char *parent_names[GENERATED_SOURCE_MAX];
  4316. struct device_node *gcknp;
  4317. struct clk_range range = CLK_RANGE(0, 0);
  4318. + struct regmap *regmap;
  4319. num_parents = of_clk_get_parent_count(np);
  4320. if (num_parents <= 0 || num_parents > GENERATED_SOURCE_MAX)
  4321. @@ -283,6 +289,10 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4322. if (!num || num > PERIPHERAL_MAX)
  4323. return;
  4324. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4325. + if (IS_ERR(regmap))
  4326. + return;
  4327. +
  4328. for_each_child_of_node(np, gcknp) {
  4329. if (of_property_read_u32(gcknp, "reg", &id))
  4330. continue;
  4331. @@ -296,11 +306,14 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,
  4332. of_at91_get_clk_range(gcknp, "atmel,clk-output-range",
  4333. &range);
  4334. - clk = at91_clk_register_generated(pmc, name, parent_names,
  4335. - num_parents, id, &range);
  4336. + clk = at91_clk_register_generated(regmap, &pmc_pcr_lock, name,
  4337. + parent_names, num_parents,
  4338. + id, &range);
  4339. if (IS_ERR(clk))
  4340. continue;
  4341. of_clk_add_provider(gcknp, of_clk_src_simple_get, clk);
  4342. }
  4343. }
  4344. +CLK_OF_DECLARE(of_sama5d2_clk_generated_setup, "atmel,sama5d2-clk-generated",
  4345. + of_sama5d2_clk_generated_setup);
  4346. diff --git a/drivers/clk/at91/clk-h32mx.c b/drivers/clk/at91/clk-h32mx.c
  4347. index a165230e7eda..8e20c8a76db7 100644
  4348. --- a/drivers/clk/at91/clk-h32mx.c
  4349. +++ b/drivers/clk/at91/clk-h32mx.c
  4350. @@ -15,15 +15,9 @@
  4351. #include <linux/clk-provider.h>
  4352. #include <linux/clkdev.h>
  4353. #include <linux/clk/at91_pmc.h>
  4354. -#include <linux/delay.h>
  4355. #include <linux/of.h>
  4356. -#include <linux/of_address.h>
  4357. -#include <linux/of_irq.h>
  4358. -#include <linux/io.h>
  4359. -#include <linux/interrupt.h>
  4360. -#include <linux/irq.h>
  4361. -#include <linux/sched.h>
  4362. -#include <linux/wait.h>
  4363. +#include <linux/regmap.h>
  4364. +#include <linux/mfd/syscon.h>
  4365. #include "pmc.h"
  4366. @@ -31,7 +25,7 @@
  4367. struct clk_sama5d4_h32mx {
  4368. struct clk_hw hw;
  4369. - struct at91_pmc *pmc;
  4370. + struct regmap *regmap;
  4371. };
  4372. #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
  4373. @@ -40,8 +34,10 @@ static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw *hw,
  4374. unsigned long parent_rate)
  4375. {
  4376. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4377. + unsigned int mckr;
  4378. - if (pmc_read(h32mxclk->pmc, AT91_PMC_MCKR) & AT91_PMC_H32MXDIV)
  4379. + regmap_read(h32mxclk->regmap, AT91_PMC_MCKR, &mckr);
  4380. + if (mckr & AT91_PMC_H32MXDIV)
  4381. return parent_rate / 2;
  4382. if (parent_rate > H32MX_MAX_FREQ)
  4383. @@ -70,18 +66,16 @@ static int clk_sama5d4_h32mx_set_rate(struct clk_hw *hw, unsigned long rate,
  4384. unsigned long parent_rate)
  4385. {
  4386. struct clk_sama5d4_h32mx *h32mxclk = to_clk_sama5d4_h32mx(hw);
  4387. - struct at91_pmc *pmc = h32mxclk->pmc;
  4388. - u32 tmp;
  4389. + u32 mckr = 0;
  4390. if (parent_rate != rate && (parent_rate / 2) != rate)
  4391. return -EINVAL;
  4392. - pmc_lock(pmc);
  4393. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_H32MXDIV;
  4394. if ((parent_rate / 2) == rate)
  4395. - tmp |= AT91_PMC_H32MXDIV;
  4396. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  4397. - pmc_unlock(pmc);
  4398. + mckr = AT91_PMC_H32MXDIV;
  4399. +
  4400. + regmap_update_bits(h32mxclk->regmap, AT91_PMC_MCKR,
  4401. + AT91_PMC_H32MXDIV, mckr);
  4402. return 0;
  4403. }
  4404. @@ -92,14 +86,18 @@ static const struct clk_ops h32mx_ops = {
  4405. .set_rate = clk_sama5d4_h32mx_set_rate,
  4406. };
  4407. -void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4408. - struct at91_pmc *pmc)
  4409. +static void __init of_sama5d4_clk_h32mx_setup(struct device_node *np)
  4410. {
  4411. struct clk_sama5d4_h32mx *h32mxclk;
  4412. struct clk_init_data init;
  4413. const char *parent_name;
  4414. + struct regmap *regmap;
  4415. struct clk *clk;
  4416. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4417. + if (IS_ERR(regmap))
  4418. + return;
  4419. +
  4420. h32mxclk = kzalloc(sizeof(*h32mxclk), GFP_KERNEL);
  4421. if (!h32mxclk)
  4422. return;
  4423. @@ -113,7 +111,7 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4424. init.flags = CLK_SET_RATE_GATE;
  4425. h32mxclk->hw.init = &init;
  4426. - h32mxclk->pmc = pmc;
  4427. + h32mxclk->regmap = regmap;
  4428. clk = clk_register(NULL, &h32mxclk->hw);
  4429. if (IS_ERR(clk)) {
  4430. @@ -123,3 +121,5 @@ void __init of_sama5d4_clk_h32mx_setup(struct device_node *np,
  4431. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4432. }
  4433. +CLK_OF_DECLARE(of_sama5d4_clk_h32mx_setup, "atmel,sama5d4-clk-h32mx",
  4434. + of_sama5d4_clk_h32mx_setup);
  4435. diff --git a/drivers/clk/at91/clk-main.c b/drivers/clk/at91/clk-main.c
  4436. index fd7247deabdc..4bfc94d6c26e 100644
  4437. --- a/drivers/clk/at91/clk-main.c
  4438. +++ b/drivers/clk/at91/clk-main.c
  4439. @@ -13,13 +13,8 @@
  4440. #include <linux/clk/at91_pmc.h>
  4441. #include <linux/delay.h>
  4442. #include <linux/of.h>
  4443. -#include <linux/of_address.h>
  4444. -#include <linux/of_irq.h>
  4445. -#include <linux/io.h>
  4446. -#include <linux/interrupt.h>
  4447. -#include <linux/irq.h>
  4448. -#include <linux/sched.h>
  4449. -#include <linux/wait.h>
  4450. +#include <linux/mfd/syscon.h>
  4451. +#include <linux/regmap.h>
  4452. #include "pmc.h"
  4453. @@ -34,18 +29,14 @@
  4454. struct clk_main_osc {
  4455. struct clk_hw hw;
  4456. - struct at91_pmc *pmc;
  4457. - unsigned int irq;
  4458. - wait_queue_head_t wait;
  4459. + struct regmap *regmap;
  4460. };
  4461. #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw)
  4462. struct clk_main_rc_osc {
  4463. struct clk_hw hw;
  4464. - struct at91_pmc *pmc;
  4465. - unsigned int irq;
  4466. - wait_queue_head_t wait;
  4467. + struct regmap *regmap;
  4468. unsigned long frequency;
  4469. unsigned long accuracy;
  4470. };
  4471. @@ -54,51 +45,47 @@ struct clk_main_rc_osc {
  4472. struct clk_rm9200_main {
  4473. struct clk_hw hw;
  4474. - struct at91_pmc *pmc;
  4475. + struct regmap *regmap;
  4476. };
  4477. #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw)
  4478. struct clk_sam9x5_main {
  4479. struct clk_hw hw;
  4480. - struct at91_pmc *pmc;
  4481. - unsigned int irq;
  4482. - wait_queue_head_t wait;
  4483. + struct regmap *regmap;
  4484. u8 parent;
  4485. };
  4486. #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw)
  4487. -static irqreturn_t clk_main_osc_irq_handler(int irq, void *dev_id)
  4488. +static inline bool clk_main_osc_ready(struct regmap *regmap)
  4489. {
  4490. - struct clk_main_osc *osc = dev_id;
  4491. + unsigned int status;
  4492. - wake_up(&osc->wait);
  4493. - disable_irq_nosync(osc->irq);
  4494. + regmap_read(regmap, AT91_PMC_SR, &status);
  4495. - return IRQ_HANDLED;
  4496. + return status & AT91_PMC_MOSCS;
  4497. }
  4498. static int clk_main_osc_prepare(struct clk_hw *hw)
  4499. {
  4500. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4501. - struct at91_pmc *pmc = osc->pmc;
  4502. + struct regmap *regmap = osc->regmap;
  4503. u32 tmp;
  4504. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4505. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4506. + tmp &= ~MOR_KEY_MASK;
  4507. +
  4508. if (tmp & AT91_PMC_OSCBYPASS)
  4509. return 0;
  4510. if (!(tmp & AT91_PMC_MOSCEN)) {
  4511. tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
  4512. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4513. + regmap_write(regmap, AT91_CKGR_MOR, tmp);
  4514. }
  4515. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS)) {
  4516. - enable_irq(osc->irq);
  4517. - wait_event(osc->wait,
  4518. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS);
  4519. - }
  4520. + while (!clk_main_osc_ready(regmap))
  4521. + cpu_relax();
  4522. return 0;
  4523. }
  4524. @@ -106,9 +93,10 @@ static int clk_main_osc_prepare(struct clk_hw *hw)
  4525. static void clk_main_osc_unprepare(struct clk_hw *hw)
  4526. {
  4527. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4528. - struct at91_pmc *pmc = osc->pmc;
  4529. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4530. + struct regmap *regmap = osc->regmap;
  4531. + u32 tmp;
  4532. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4533. if (tmp & AT91_PMC_OSCBYPASS)
  4534. return;
  4535. @@ -116,20 +104,22 @@ static void clk_main_osc_unprepare(struct clk_hw *hw)
  4536. return;
  4537. tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
  4538. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4539. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4540. }
  4541. static int clk_main_osc_is_prepared(struct clk_hw *hw)
  4542. {
  4543. struct clk_main_osc *osc = to_clk_main_osc(hw);
  4544. - struct at91_pmc *pmc = osc->pmc;
  4545. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4546. + struct regmap *regmap = osc->regmap;
  4547. + u32 tmp, status;
  4548. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4549. if (tmp & AT91_PMC_OSCBYPASS)
  4550. return 1;
  4551. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCS) &&
  4552. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN));
  4553. + regmap_read(regmap, AT91_PMC_SR, &status);
  4554. +
  4555. + return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN);
  4556. }
  4557. static const struct clk_ops main_osc_ops = {
  4558. @@ -139,18 +129,16 @@ static const struct clk_ops main_osc_ops = {
  4559. };
  4560. static struct clk * __init
  4561. -at91_clk_register_main_osc(struct at91_pmc *pmc,
  4562. - unsigned int irq,
  4563. +at91_clk_register_main_osc(struct regmap *regmap,
  4564. const char *name,
  4565. const char *parent_name,
  4566. bool bypass)
  4567. {
  4568. - int ret;
  4569. struct clk_main_osc *osc;
  4570. struct clk *clk = NULL;
  4571. struct clk_init_data init;
  4572. - if (!pmc || !irq || !name || !parent_name)
  4573. + if (!name || !parent_name)
  4574. return ERR_PTR(-EINVAL);
  4575. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4576. @@ -164,85 +152,70 @@ at91_clk_register_main_osc(struct at91_pmc *pmc,
  4577. init.flags = CLK_IGNORE_UNUSED;
  4578. osc->hw.init = &init;
  4579. - osc->pmc = pmc;
  4580. - osc->irq = irq;
  4581. -
  4582. - init_waitqueue_head(&osc->wait);
  4583. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4584. - ret = request_irq(osc->irq, clk_main_osc_irq_handler,
  4585. - IRQF_TRIGGER_HIGH, name, osc);
  4586. - if (ret) {
  4587. - kfree(osc);
  4588. - return ERR_PTR(ret);
  4589. - }
  4590. + osc->regmap = regmap;
  4591. if (bypass)
  4592. - pmc_write(pmc, AT91_CKGR_MOR,
  4593. - (pmc_read(pmc, AT91_CKGR_MOR) &
  4594. - ~(MOR_KEY_MASK | AT91_PMC_MOSCEN)) |
  4595. - AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4596. + regmap_update_bits(regmap,
  4597. + AT91_CKGR_MOR, MOR_KEY_MASK |
  4598. + AT91_PMC_MOSCEN,
  4599. + AT91_PMC_OSCBYPASS | AT91_PMC_KEY);
  4600. clk = clk_register(NULL, &osc->hw);
  4601. - if (IS_ERR(clk)) {
  4602. - free_irq(irq, osc);
  4603. + if (IS_ERR(clk))
  4604. kfree(osc);
  4605. - }
  4606. return clk;
  4607. }
  4608. -void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  4609. - struct at91_pmc *pmc)
  4610. +static void __init of_at91rm9200_clk_main_osc_setup(struct device_node *np)
  4611. {
  4612. struct clk *clk;
  4613. - unsigned int irq;
  4614. const char *name = np->name;
  4615. const char *parent_name;
  4616. + struct regmap *regmap;
  4617. bool bypass;
  4618. of_property_read_string(np, "clock-output-names", &name);
  4619. bypass = of_property_read_bool(np, "atmel,osc-bypass");
  4620. parent_name = of_clk_get_parent_name(np, 0);
  4621. - irq = irq_of_parse_and_map(np, 0);
  4622. - if (!irq)
  4623. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4624. + if (IS_ERR(regmap))
  4625. return;
  4626. - clk = at91_clk_register_main_osc(pmc, irq, name, parent_name, bypass);
  4627. + clk = at91_clk_register_main_osc(regmap, name, parent_name, bypass);
  4628. if (IS_ERR(clk))
  4629. return;
  4630. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4631. }
  4632. +CLK_OF_DECLARE(at91rm9200_clk_main_osc, "atmel,at91rm9200-clk-main-osc",
  4633. + of_at91rm9200_clk_main_osc_setup);
  4634. -static irqreturn_t clk_main_rc_osc_irq_handler(int irq, void *dev_id)
  4635. +static bool clk_main_rc_osc_ready(struct regmap *regmap)
  4636. {
  4637. - struct clk_main_rc_osc *osc = dev_id;
  4638. + unsigned int status;
  4639. - wake_up(&osc->wait);
  4640. - disable_irq_nosync(osc->irq);
  4641. + regmap_read(regmap, AT91_PMC_SR, &status);
  4642. - return IRQ_HANDLED;
  4643. + return status & AT91_PMC_MOSCRCS;
  4644. }
  4645. static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4646. {
  4647. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4648. - struct at91_pmc *pmc = osc->pmc;
  4649. - u32 tmp;
  4650. + struct regmap *regmap = osc->regmap;
  4651. + unsigned int mor;
  4652. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4653. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4654. - if (!(tmp & AT91_PMC_MOSCRCEN)) {
  4655. - tmp |= AT91_PMC_MOSCRCEN | AT91_PMC_KEY;
  4656. - pmc_write(pmc, AT91_CKGR_MOR, tmp);
  4657. - }
  4658. + if (!(mor & AT91_PMC_MOSCRCEN))
  4659. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4660. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
  4661. + AT91_PMC_MOSCRCEN | AT91_PMC_KEY);
  4662. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS)) {
  4663. - enable_irq(osc->irq);
  4664. - wait_event(osc->wait,
  4665. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS);
  4666. - }
  4667. + while (!clk_main_rc_osc_ready(regmap))
  4668. + cpu_relax();
  4669. return 0;
  4670. }
  4671. @@ -250,23 +223,28 @@ static int clk_main_rc_osc_prepare(struct clk_hw *hw)
  4672. static void clk_main_rc_osc_unprepare(struct clk_hw *hw)
  4673. {
  4674. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4675. - struct at91_pmc *pmc = osc->pmc;
  4676. - u32 tmp = pmc_read(pmc, AT91_CKGR_MOR);
  4677. + struct regmap *regmap = osc->regmap;
  4678. + unsigned int mor;
  4679. +
  4680. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4681. - if (!(tmp & AT91_PMC_MOSCRCEN))
  4682. + if (!(mor & AT91_PMC_MOSCRCEN))
  4683. return;
  4684. - tmp &= ~(MOR_KEY_MASK | AT91_PMC_MOSCRCEN);
  4685. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_KEY);
  4686. + regmap_update_bits(regmap, AT91_CKGR_MOR,
  4687. + MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY);
  4688. }
  4689. static int clk_main_rc_osc_is_prepared(struct clk_hw *hw)
  4690. {
  4691. struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw);
  4692. - struct at91_pmc *pmc = osc->pmc;
  4693. + struct regmap *regmap = osc->regmap;
  4694. + unsigned int mor, status;
  4695. - return !!((pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCRCS) &&
  4696. - (pmc_read(pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCRCEN));
  4697. + regmap_read(regmap, AT91_CKGR_MOR, &mor);
  4698. + regmap_read(regmap, AT91_PMC_SR, &status);
  4699. +
  4700. + return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS);
  4701. }
  4702. static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw,
  4703. @@ -294,17 +272,15 @@ static const struct clk_ops main_rc_osc_ops = {
  4704. };
  4705. static struct clk * __init
  4706. -at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4707. - unsigned int irq,
  4708. +at91_clk_register_main_rc_osc(struct regmap *regmap,
  4709. const char *name,
  4710. u32 frequency, u32 accuracy)
  4711. {
  4712. - int ret;
  4713. struct clk_main_rc_osc *osc;
  4714. struct clk *clk = NULL;
  4715. struct clk_init_data init;
  4716. - if (!pmc || !irq || !name || !frequency)
  4717. + if (!name || !frequency)
  4718. return ERR_PTR(-EINVAL);
  4719. osc = kzalloc(sizeof(*osc), GFP_KERNEL);
  4720. @@ -318,63 +294,53 @@ at91_clk_register_main_rc_osc(struct at91_pmc *pmc,
  4721. init.flags = CLK_IS_ROOT | CLK_IGNORE_UNUSED;
  4722. osc->hw.init = &init;
  4723. - osc->pmc = pmc;
  4724. - osc->irq = irq;
  4725. + osc->regmap = regmap;
  4726. osc->frequency = frequency;
  4727. osc->accuracy = accuracy;
  4728. - init_waitqueue_head(&osc->wait);
  4729. - irq_set_status_flags(osc->irq, IRQ_NOAUTOEN);
  4730. - ret = request_irq(osc->irq, clk_main_rc_osc_irq_handler,
  4731. - IRQF_TRIGGER_HIGH, name, osc);
  4732. - if (ret)
  4733. - return ERR_PTR(ret);
  4734. -
  4735. clk = clk_register(NULL, &osc->hw);
  4736. - if (IS_ERR(clk)) {
  4737. - free_irq(irq, osc);
  4738. + if (IS_ERR(clk))
  4739. kfree(osc);
  4740. - }
  4741. return clk;
  4742. }
  4743. -void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  4744. - struct at91_pmc *pmc)
  4745. +static void __init of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np)
  4746. {
  4747. struct clk *clk;
  4748. - unsigned int irq;
  4749. u32 frequency = 0;
  4750. u32 accuracy = 0;
  4751. const char *name = np->name;
  4752. + struct regmap *regmap;
  4753. of_property_read_string(np, "clock-output-names", &name);
  4754. of_property_read_u32(np, "clock-frequency", &frequency);
  4755. of_property_read_u32(np, "clock-accuracy", &accuracy);
  4756. - irq = irq_of_parse_and_map(np, 0);
  4757. - if (!irq)
  4758. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4759. + if (IS_ERR(regmap))
  4760. return;
  4761. - clk = at91_clk_register_main_rc_osc(pmc, irq, name, frequency,
  4762. - accuracy);
  4763. + clk = at91_clk_register_main_rc_osc(regmap, name, frequency, accuracy);
  4764. if (IS_ERR(clk))
  4765. return;
  4766. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4767. }
  4768. +CLK_OF_DECLARE(at91sam9x5_clk_main_rc_osc, "atmel,at91sam9x5-clk-main-rc-osc",
  4769. + of_at91sam9x5_clk_main_rc_osc_setup);
  4770. -static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4771. +static int clk_main_probe_frequency(struct regmap *regmap)
  4772. {
  4773. unsigned long prep_time, timeout;
  4774. - u32 tmp;
  4775. + unsigned int mcfr;
  4776. timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT);
  4777. do {
  4778. prep_time = jiffies;
  4779. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4780. - if (tmp & AT91_PMC_MAINRDY)
  4781. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4782. + if (mcfr & AT91_PMC_MAINRDY)
  4783. return 0;
  4784. usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT);
  4785. } while (time_before(prep_time, timeout));
  4786. @@ -382,34 +348,37 @@ static int clk_main_probe_frequency(struct at91_pmc *pmc)
  4787. return -ETIMEDOUT;
  4788. }
  4789. -static unsigned long clk_main_recalc_rate(struct at91_pmc *pmc,
  4790. +static unsigned long clk_main_recalc_rate(struct regmap *regmap,
  4791. unsigned long parent_rate)
  4792. {
  4793. - u32 tmp;
  4794. + unsigned int mcfr;
  4795. if (parent_rate)
  4796. return parent_rate;
  4797. pr_warn("Main crystal frequency not set, using approximate value\n");
  4798. - tmp = pmc_read(pmc, AT91_CKGR_MCFR);
  4799. - if (!(tmp & AT91_PMC_MAINRDY))
  4800. + regmap_read(regmap, AT91_CKGR_MCFR, &mcfr);
  4801. + if (!(mcfr & AT91_PMC_MAINRDY))
  4802. return 0;
  4803. - return ((tmp & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4804. + return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV;
  4805. }
  4806. static int clk_rm9200_main_prepare(struct clk_hw *hw)
  4807. {
  4808. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4809. - return clk_main_probe_frequency(clkmain->pmc);
  4810. + return clk_main_probe_frequency(clkmain->regmap);
  4811. }
  4812. static int clk_rm9200_main_is_prepared(struct clk_hw *hw)
  4813. {
  4814. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4815. + unsigned int status;
  4816. +
  4817. + regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status);
  4818. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MCFR) & AT91_PMC_MAINRDY);
  4819. + return status & AT91_PMC_MAINRDY ? 1 : 0;
  4820. }
  4821. static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4822. @@ -417,7 +386,7 @@ static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw,
  4823. {
  4824. struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw);
  4825. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4826. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4827. }
  4828. static const struct clk_ops rm9200_main_ops = {
  4829. @@ -427,7 +396,7 @@ static const struct clk_ops rm9200_main_ops = {
  4830. };
  4831. static struct clk * __init
  4832. -at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4833. +at91_clk_register_rm9200_main(struct regmap *regmap,
  4834. const char *name,
  4835. const char *parent_name)
  4836. {
  4837. @@ -435,7 +404,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4838. struct clk *clk = NULL;
  4839. struct clk_init_data init;
  4840. - if (!pmc || !name)
  4841. + if (!name)
  4842. return ERR_PTR(-EINVAL);
  4843. if (!parent_name)
  4844. @@ -452,7 +421,7 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4845. init.flags = 0;
  4846. clkmain->hw.init = &init;
  4847. - clkmain->pmc = pmc;
  4848. + clkmain->regmap = regmap;
  4849. clk = clk_register(NULL, &clkmain->hw);
  4850. if (IS_ERR(clk))
  4851. @@ -461,52 +430,54 @@ at91_clk_register_rm9200_main(struct at91_pmc *pmc,
  4852. return clk;
  4853. }
  4854. -void __init of_at91rm9200_clk_main_setup(struct device_node *np,
  4855. - struct at91_pmc *pmc)
  4856. +static void __init of_at91rm9200_clk_main_setup(struct device_node *np)
  4857. {
  4858. struct clk *clk;
  4859. const char *parent_name;
  4860. const char *name = np->name;
  4861. + struct regmap *regmap;
  4862. parent_name = of_clk_get_parent_name(np, 0);
  4863. of_property_read_string(np, "clock-output-names", &name);
  4864. - clk = at91_clk_register_rm9200_main(pmc, name, parent_name);
  4865. + regmap = syscon_node_to_regmap(of_get_parent(np));
  4866. + if (IS_ERR(regmap))
  4867. + return;
  4868. +
  4869. + clk = at91_clk_register_rm9200_main(regmap, name, parent_name);
  4870. if (IS_ERR(clk))
  4871. return;
  4872. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  4873. }
  4874. +CLK_OF_DECLARE(at91rm9200_clk_main, "atmel,at91rm9200-clk-main",
  4875. + of_at91rm9200_clk_main_setup);
  4876. -static irqreturn_t clk_sam9x5_main_irq_handler(int irq, void *dev_id)
  4877. +static inline bool clk_sam9x5_main_ready(struct regmap *regmap)
  4878. {
  4879. - struct clk_sam9x5_main *clkmain = dev_id;
  4880. + unsigned int status;
  4881. - wake_up(&clkmain->wait);
  4882. - disable_irq_nosync(clkmain->irq);
  4883. + regmap_read(regmap, AT91_PMC_SR, &status);
  4884. - return IRQ_HANDLED;
  4885. + return status & AT91_PMC_MOSCSELS ? 1 : 0;
  4886. }
  4887. static int clk_sam9x5_main_prepare(struct clk_hw *hw)
  4888. {
  4889. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4890. - struct at91_pmc *pmc = clkmain->pmc;
  4891. + struct regmap *regmap = clkmain->regmap;
  4892. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4893. - enable_irq(clkmain->irq);
  4894. - wait_event(clkmain->wait,
  4895. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4896. - }
  4897. + while (!clk_sam9x5_main_ready(regmap))
  4898. + cpu_relax();
  4899. - return clk_main_probe_frequency(pmc);
  4900. + return clk_main_probe_frequency(regmap);
  4901. }
  4902. static int clk_sam9x5_main_is_prepared(struct clk_hw *hw)
  4903. {
  4904. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4905. - return !!(pmc_read(clkmain->pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4906. + return clk_sam9x5_main_ready(clkmain->regmap);
  4907. }
  4908. static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  4909. @@ -514,30 +485,28 @@ static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw,
  4910. {
  4911. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4912. - return clk_main_recalc_rate(clkmain->pmc, parent_rate);
  4913. + return clk_main_recalc_rate(clkmain->regmap, parent_rate);
  4914. }
  4915. static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  4916. {
  4917. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4918. - struct at91_pmc *pmc = clkmain->pmc;
  4919. - u32 tmp;
  4920. + struct regmap *regmap = clkmain->regmap;
  4921. + unsigned int tmp;
  4922. if (index > 1)
  4923. return -EINVAL;
  4924. - tmp = pmc_read(pmc, AT91_CKGR_MOR) & ~MOR_KEY_MASK;
  4925. + regmap_read(regmap, AT91_CKGR_MOR, &tmp);
  4926. + tmp &= ~MOR_KEY_MASK;
  4927. if (index && !(tmp & AT91_PMC_MOSCSEL))
  4928. - pmc_write(pmc, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4929. + regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
  4930. else if (!index && (tmp & AT91_PMC_MOSCSEL))
  4931. - pmc_write(pmc, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  4932. + regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);
  4933. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS)) {
  4934. - enable_irq(clkmain->irq);
  4935. - wait_event(clkmain->wait,
  4936. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MOSCSELS);
  4937. - }
  4938. + while (!clk_sam9x5_main_ready(regmap))
  4939. + cpu_relax();
  4940. return 0;
  4941. }
  4942. @@ -545,8 +514,11 @@ static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index)
  4943. static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw)
  4944. {
  4945. struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw);
  4946. + unsigned int status;
  4947. +
  4948. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  4949. - return !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) & AT91_PMC_MOSCEN);
  4950. + return status & AT91_PMC_MOSCEN ? 1 : 0;
  4951. }
  4952. static const struct clk_ops sam9x5_main_ops = {
  4953. @@ -558,18 +530,17 @@ static const struct clk_ops sam9x5_main_ops = {
  4954. };
  4955. static struct clk * __init
  4956. -at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  4957. - unsigned int irq,
  4958. +at91_clk_register_sam9x5_main(struct regmap *regmap,
  4959. const char *name,
  4960. const char **parent_names,
  4961. int num_parents)
  4962. {
  4963. - int ret;
  4964. struct clk_sam9x5_main *clkmain;
  4965. struct clk *clk = NULL;
  4966. struct clk_init_data init;
  4967. + unsigned int status;
  4968. - if (!pmc || !irq || !name)
  4969. + if (!name)
  4970. return ERR_PTR(-EINVAL);
  4971. if (!parent_names || !num_parents)
  4972. @@ -586,51 +557,42 @@ at91_clk_register_sam9x5_main(struct at91_pmc *pmc,
  4973. init.flags = CLK_SET_PARENT_GATE;
  4974. clkmain->hw.init = &init;
  4975. - clkmain->pmc = pmc;
  4976. - clkmain->irq = irq;
  4977. - clkmain->parent = !!(pmc_read(clkmain->pmc, AT91_CKGR_MOR) &
  4978. - AT91_PMC_MOSCEN);
  4979. - init_waitqueue_head(&clkmain->wait);
  4980. - irq_set_status_flags(clkmain->irq, IRQ_NOAUTOEN);
  4981. - ret = request_irq(clkmain->irq, clk_sam9x5_main_irq_handler,
  4982. - IRQF_TRIGGER_HIGH, name, clkmain);
  4983. - if (ret)
  4984. - return ERR_PTR(ret);
  4985. + clkmain->regmap = regmap;
  4986. + regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status);
  4987. + clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0;
  4988. clk = clk_register(NULL, &clkmain->hw);
  4989. - if (IS_ERR(clk)) {
  4990. - free_irq(clkmain->irq, clkmain);
  4991. + if (IS_ERR(clk))
  4992. kfree(clkmain);
  4993. - }
  4994. return clk;
  4995. }
  4996. -void __init of_at91sam9x5_clk_main_setup(struct device_node *np,
  4997. - struct at91_pmc *pmc)
  4998. +static void __init of_at91sam9x5_clk_main_setup(struct device_node *np)
  4999. {
  5000. struct clk *clk;
  5001. const char *parent_names[2];
  5002. int num_parents;
  5003. - unsigned int irq;
  5004. const char *name = np->name;
  5005. + struct regmap *regmap;
  5006. num_parents = of_clk_get_parent_count(np);
  5007. if (num_parents <= 0 || num_parents > 2)
  5008. return;
  5009. of_clk_parent_fill(np, parent_names, num_parents);
  5010. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5011. + if (IS_ERR(regmap))
  5012. + return;
  5013. of_property_read_string(np, "clock-output-names", &name);
  5014. - irq = irq_of_parse_and_map(np, 0);
  5015. - if (!irq)
  5016. - return;
  5017. -
  5018. - clk = at91_clk_register_sam9x5_main(pmc, irq, name, parent_names,
  5019. + clk = at91_clk_register_sam9x5_main(regmap, name, parent_names,
  5020. num_parents);
  5021. if (IS_ERR(clk))
  5022. return;
  5023. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5024. }
  5025. +CLK_OF_DECLARE(at91sam9x5_clk_main, "atmel,at91sam9x5-clk-main",
  5026. + of_at91sam9x5_clk_main_setup);
  5027. diff --git a/drivers/clk/at91/clk-master.c b/drivers/clk/at91/clk-master.c
  5028. index 620ea323356b..7d4a1864ea7c 100644
  5029. --- a/drivers/clk/at91/clk-master.c
  5030. +++ b/drivers/clk/at91/clk-master.c
  5031. @@ -12,13 +12,8 @@
  5032. #include <linux/clkdev.h>
  5033. #include <linux/clk/at91_pmc.h>
  5034. #include <linux/of.h>
  5035. -#include <linux/of_address.h>
  5036. -#include <linux/of_irq.h>
  5037. -#include <linux/io.h>
  5038. -#include <linux/wait.h>
  5039. -#include <linux/sched.h>
  5040. -#include <linux/interrupt.h>
  5041. -#include <linux/irq.h>
  5042. +#include <linux/mfd/syscon.h>
  5043. +#include <linux/regmap.h>
  5044. #include "pmc.h"
  5045. @@ -44,32 +39,26 @@ struct clk_master_layout {
  5046. struct clk_master {
  5047. struct clk_hw hw;
  5048. - struct at91_pmc *pmc;
  5049. - unsigned int irq;
  5050. - wait_queue_head_t wait;
  5051. + struct regmap *regmap;
  5052. const struct clk_master_layout *layout;
  5053. const struct clk_master_characteristics *characteristics;
  5054. };
  5055. -static irqreturn_t clk_master_irq_handler(int irq, void *dev_id)
  5056. +static inline bool clk_master_ready(struct regmap *regmap)
  5057. {
  5058. - struct clk_master *master = (struct clk_master *)dev_id;
  5059. + unsigned int status;
  5060. - wake_up(&master->wait);
  5061. - disable_irq_nosync(master->irq);
  5062. + regmap_read(regmap, AT91_PMC_SR, &status);
  5063. - return IRQ_HANDLED;
  5064. + return status & AT91_PMC_MCKRDY ? 1 : 0;
  5065. }
  5066. +
  5067. static int clk_master_prepare(struct clk_hw *hw)
  5068. {
  5069. struct clk_master *master = to_clk_master(hw);
  5070. - struct at91_pmc *pmc = master->pmc;
  5071. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY)) {
  5072. - enable_irq(master->irq);
  5073. - wait_event(master->wait,
  5074. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5075. - }
  5076. + while (!clk_master_ready(master->regmap))
  5077. + cpu_relax();
  5078. return 0;
  5079. }
  5080. @@ -78,7 +67,7 @@ static int clk_master_is_prepared(struct clk_hw *hw)
  5081. {
  5082. struct clk_master *master = to_clk_master(hw);
  5083. - return !!(pmc_read(master->pmc, AT91_PMC_SR) & AT91_PMC_MCKRDY);
  5084. + return clk_master_ready(master->regmap);
  5085. }
  5086. static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5087. @@ -88,18 +77,16 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5088. u8 div;
  5089. unsigned long rate = parent_rate;
  5090. struct clk_master *master = to_clk_master(hw);
  5091. - struct at91_pmc *pmc = master->pmc;
  5092. const struct clk_master_layout *layout = master->layout;
  5093. const struct clk_master_characteristics *characteristics =
  5094. master->characteristics;
  5095. - u32 tmp;
  5096. + unsigned int mckr;
  5097. - pmc_lock(pmc);
  5098. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & layout->mask;
  5099. - pmc_unlock(pmc);
  5100. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5101. + mckr &= layout->mask;
  5102. - pres = (tmp >> layout->pres_shift) & MASTER_PRES_MASK;
  5103. - div = (tmp >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5104. + pres = (mckr >> layout->pres_shift) & MASTER_PRES_MASK;
  5105. + div = (mckr >> MASTER_DIV_SHIFT) & MASTER_DIV_MASK;
  5106. if (characteristics->have_div3_pres && pres == MASTER_PRES_MAX)
  5107. rate /= 3;
  5108. @@ -119,9 +106,11 @@ static unsigned long clk_master_recalc_rate(struct clk_hw *hw,
  5109. static u8 clk_master_get_parent(struct clk_hw *hw)
  5110. {
  5111. struct clk_master *master = to_clk_master(hw);
  5112. - struct at91_pmc *pmc = master->pmc;
  5113. + unsigned int mckr;
  5114. - return pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_CSS;
  5115. + regmap_read(master->regmap, AT91_PMC_MCKR, &mckr);
  5116. +
  5117. + return mckr & AT91_PMC_CSS;
  5118. }
  5119. static const struct clk_ops master_ops = {
  5120. @@ -132,18 +121,17 @@ static const struct clk_ops master_ops = {
  5121. };
  5122. static struct clk * __init
  5123. -at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5124. +at91_clk_register_master(struct regmap *regmap,
  5125. const char *name, int num_parents,
  5126. const char **parent_names,
  5127. const struct clk_master_layout *layout,
  5128. const struct clk_master_characteristics *characteristics)
  5129. {
  5130. - int ret;
  5131. struct clk_master *master;
  5132. struct clk *clk = NULL;
  5133. struct clk_init_data init;
  5134. - if (!pmc || !irq || !name || !num_parents || !parent_names)
  5135. + if (!name || !num_parents || !parent_names)
  5136. return ERR_PTR(-EINVAL);
  5137. master = kzalloc(sizeof(*master), GFP_KERNEL);
  5138. @@ -159,20 +147,10 @@ at91_clk_register_master(struct at91_pmc *pmc, unsigned int irq,
  5139. master->hw.init = &init;
  5140. master->layout = layout;
  5141. master->characteristics = characteristics;
  5142. - master->pmc = pmc;
  5143. - master->irq = irq;
  5144. - init_waitqueue_head(&master->wait);
  5145. - irq_set_status_flags(master->irq, IRQ_NOAUTOEN);
  5146. - ret = request_irq(master->irq, clk_master_irq_handler,
  5147. - IRQF_TRIGGER_HIGH, "clk-master", master);
  5148. - if (ret) {
  5149. - kfree(master);
  5150. - return ERR_PTR(ret);
  5151. - }
  5152. + master->regmap = regmap;
  5153. clk = clk_register(NULL, &master->hw);
  5154. if (IS_ERR(clk)) {
  5155. - free_irq(master->irq, master);
  5156. kfree(master);
  5157. }
  5158. @@ -217,15 +195,15 @@ out_free_characteristics:
  5159. }
  5160. static void __init
  5161. -of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5162. +of_at91_clk_master_setup(struct device_node *np,
  5163. const struct clk_master_layout *layout)
  5164. {
  5165. struct clk *clk;
  5166. int num_parents;
  5167. - unsigned int irq;
  5168. const char *parent_names[MASTER_SOURCE_MAX];
  5169. const char *name = np->name;
  5170. struct clk_master_characteristics *characteristics;
  5171. + struct regmap *regmap;
  5172. num_parents = of_clk_get_parent_count(np);
  5173. if (num_parents <= 0 || num_parents > MASTER_SOURCE_MAX)
  5174. @@ -239,11 +217,11 @@ of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc,
  5175. if (!characteristics)
  5176. return;
  5177. - irq = irq_of_parse_and_map(np, 0);
  5178. - if (!irq)
  5179. - goto out_free_characteristics;
  5180. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5181. + if (IS_ERR(regmap))
  5182. + return;
  5183. - clk = at91_clk_register_master(pmc, irq, name, num_parents,
  5184. + clk = at91_clk_register_master(regmap, name, num_parents,
  5185. parent_names, layout,
  5186. characteristics);
  5187. if (IS_ERR(clk))
  5188. @@ -256,14 +234,16 @@ out_free_characteristics:
  5189. kfree(characteristics);
  5190. }
  5191. -void __init of_at91rm9200_clk_master_setup(struct device_node *np,
  5192. - struct at91_pmc *pmc)
  5193. +static void __init of_at91rm9200_clk_master_setup(struct device_node *np)
  5194. {
  5195. - of_at91_clk_master_setup(np, pmc, &at91rm9200_master_layout);
  5196. + of_at91_clk_master_setup(np, &at91rm9200_master_layout);
  5197. }
  5198. +CLK_OF_DECLARE(at91rm9200_clk_master, "atmel,at91rm9200-clk-master",
  5199. + of_at91rm9200_clk_master_setup);
  5200. -void __init of_at91sam9x5_clk_master_setup(struct device_node *np,
  5201. - struct at91_pmc *pmc)
  5202. +static void __init of_at91sam9x5_clk_master_setup(struct device_node *np)
  5203. {
  5204. - of_at91_clk_master_setup(np, pmc, &at91sam9x5_master_layout);
  5205. + of_at91_clk_master_setup(np, &at91sam9x5_master_layout);
  5206. }
  5207. +CLK_OF_DECLARE(at91sam9x5_clk_master, "atmel,at91sam9x5-clk-master",
  5208. + of_at91sam9x5_clk_master_setup);
  5209. diff --git a/drivers/clk/at91/clk-peripheral.c b/drivers/clk/at91/clk-peripheral.c
  5210. index 58f3b568e9cb..d69cd2a121b1 100644
  5211. --- a/drivers/clk/at91/clk-peripheral.c
  5212. +++ b/drivers/clk/at91/clk-peripheral.c
  5213. @@ -12,11 +12,13 @@
  5214. #include <linux/clkdev.h>
  5215. #include <linux/clk/at91_pmc.h>
  5216. #include <linux/of.h>
  5217. -#include <linux/of_address.h>
  5218. -#include <linux/io.h>
  5219. +#include <linux/mfd/syscon.h>
  5220. +#include <linux/regmap.h>
  5221. #include "pmc.h"
  5222. +DEFINE_SPINLOCK(pmc_pcr_lock);
  5223. +
  5224. #define PERIPHERAL_MAX 64
  5225. #define PERIPHERAL_AT91RM9200 0
  5226. @@ -33,7 +35,7 @@
  5227. struct clk_peripheral {
  5228. struct clk_hw hw;
  5229. - struct at91_pmc *pmc;
  5230. + struct regmap *regmap;
  5231. u32 id;
  5232. };
  5233. @@ -41,8 +43,9 @@ struct clk_peripheral {
  5234. struct clk_sam9x5_peripheral {
  5235. struct clk_hw hw;
  5236. - struct at91_pmc *pmc;
  5237. + struct regmap *regmap;
  5238. struct clk_range range;
  5239. + spinlock_t *lock;
  5240. u32 id;
  5241. u32 div;
  5242. bool auto_div;
  5243. @@ -54,7 +57,6 @@ struct clk_sam9x5_peripheral {
  5244. static int clk_peripheral_enable(struct clk_hw *hw)
  5245. {
  5246. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5247. - struct at91_pmc *pmc = periph->pmc;
  5248. int offset = AT91_PMC_PCER;
  5249. u32 id = periph->id;
  5250. @@ -62,14 +64,14 @@ static int clk_peripheral_enable(struct clk_hw *hw)
  5251. return 0;
  5252. if (id > PERIPHERAL_ID_MAX)
  5253. offset = AT91_PMC_PCER1;
  5254. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5255. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5256. +
  5257. return 0;
  5258. }
  5259. static void clk_peripheral_disable(struct clk_hw *hw)
  5260. {
  5261. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5262. - struct at91_pmc *pmc = periph->pmc;
  5263. int offset = AT91_PMC_PCDR;
  5264. u32 id = periph->id;
  5265. @@ -77,21 +79,23 @@ static void clk_peripheral_disable(struct clk_hw *hw)
  5266. return;
  5267. if (id > PERIPHERAL_ID_MAX)
  5268. offset = AT91_PMC_PCDR1;
  5269. - pmc_write(pmc, offset, PERIPHERAL_MASK(id));
  5270. + regmap_write(periph->regmap, offset, PERIPHERAL_MASK(id));
  5271. }
  5272. static int clk_peripheral_is_enabled(struct clk_hw *hw)
  5273. {
  5274. struct clk_peripheral *periph = to_clk_peripheral(hw);
  5275. - struct at91_pmc *pmc = periph->pmc;
  5276. int offset = AT91_PMC_PCSR;
  5277. + unsigned int status;
  5278. u32 id = periph->id;
  5279. if (id < PERIPHERAL_ID_MIN)
  5280. return 1;
  5281. if (id > PERIPHERAL_ID_MAX)
  5282. offset = AT91_PMC_PCSR1;
  5283. - return !!(pmc_read(pmc, offset) & PERIPHERAL_MASK(id));
  5284. + regmap_read(periph->regmap, offset, &status);
  5285. +
  5286. + return status & PERIPHERAL_MASK(id) ? 1 : 0;
  5287. }
  5288. static const struct clk_ops peripheral_ops = {
  5289. @@ -101,14 +105,14 @@ static const struct clk_ops peripheral_ops = {
  5290. };
  5291. static struct clk * __init
  5292. -at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5293. +at91_clk_register_peripheral(struct regmap *regmap, const char *name,
  5294. const char *parent_name, u32 id)
  5295. {
  5296. struct clk_peripheral *periph;
  5297. struct clk *clk = NULL;
  5298. struct clk_init_data init;
  5299. - if (!pmc || !name || !parent_name || id > PERIPHERAL_ID_MAX)
  5300. + if (!name || !parent_name || id > PERIPHERAL_ID_MAX)
  5301. return ERR_PTR(-EINVAL);
  5302. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5303. @@ -123,7 +127,7 @@ at91_clk_register_peripheral(struct at91_pmc *pmc, const char *name,
  5304. periph->id = id;
  5305. periph->hw.init = &init;
  5306. - periph->pmc = pmc;
  5307. + periph->regmap = regmap;
  5308. clk = clk_register(NULL, &periph->hw);
  5309. if (IS_ERR(clk))
  5310. @@ -160,53 +164,58 @@ static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral *periph)
  5311. static int clk_sam9x5_peripheral_enable(struct clk_hw *hw)
  5312. {
  5313. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5314. - struct at91_pmc *pmc = periph->pmc;
  5315. - u32 tmp;
  5316. + unsigned long flags;
  5317. if (periph->id < PERIPHERAL_ID_MIN)
  5318. return 0;
  5319. - pmc_lock(pmc);
  5320. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5321. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_DIV_MASK;
  5322. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_DIV(periph->div)
  5323. - | AT91_PMC_PCR_CMD
  5324. - | AT91_PMC_PCR_EN);
  5325. - pmc_unlock(pmc);
  5326. + spin_lock_irqsave(periph->lock, flags);
  5327. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5328. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5329. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5330. + AT91_PMC_PCR_DIV_MASK | AT91_PMC_PCR_CMD |
  5331. + AT91_PMC_PCR_EN,
  5332. + AT91_PMC_PCR_DIV(periph->div) |
  5333. + AT91_PMC_PCR_CMD |
  5334. + AT91_PMC_PCR_EN);
  5335. + spin_unlock_irqrestore(periph->lock, flags);
  5336. +
  5337. return 0;
  5338. }
  5339. static void clk_sam9x5_peripheral_disable(struct clk_hw *hw)
  5340. {
  5341. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5342. - struct at91_pmc *pmc = periph->pmc;
  5343. - u32 tmp;
  5344. + unsigned long flags;
  5345. if (periph->id < PERIPHERAL_ID_MIN)
  5346. return;
  5347. - pmc_lock(pmc);
  5348. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5349. - tmp = pmc_read(pmc, AT91_PMC_PCR) & ~AT91_PMC_PCR_EN;
  5350. - pmc_write(pmc, AT91_PMC_PCR, tmp | AT91_PMC_PCR_CMD);
  5351. - pmc_unlock(pmc);
  5352. + spin_lock_irqsave(periph->lock, flags);
  5353. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5354. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5355. + regmap_update_bits(periph->regmap, AT91_PMC_PCR,
  5356. + AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD,
  5357. + AT91_PMC_PCR_CMD);
  5358. + spin_unlock_irqrestore(periph->lock, flags);
  5359. }
  5360. static int clk_sam9x5_peripheral_is_enabled(struct clk_hw *hw)
  5361. {
  5362. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5363. - struct at91_pmc *pmc = periph->pmc;
  5364. - int ret;
  5365. + unsigned long flags;
  5366. + unsigned int status;
  5367. if (periph->id < PERIPHERAL_ID_MIN)
  5368. return 1;
  5369. - pmc_lock(pmc);
  5370. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5371. - ret = !!(pmc_read(pmc, AT91_PMC_PCR) & AT91_PMC_PCR_EN);
  5372. - pmc_unlock(pmc);
  5373. + spin_lock_irqsave(periph->lock, flags);
  5374. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5375. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5376. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5377. + spin_unlock_irqrestore(periph->lock, flags);
  5378. - return ret;
  5379. + return status & AT91_PMC_PCR_EN ? 1 : 0;
  5380. }
  5381. static unsigned long
  5382. @@ -214,19 +223,20 @@ clk_sam9x5_peripheral_recalc_rate(struct clk_hw *hw,
  5383. unsigned long parent_rate)
  5384. {
  5385. struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(hw);
  5386. - struct at91_pmc *pmc = periph->pmc;
  5387. - u32 tmp;
  5388. + unsigned long flags;
  5389. + unsigned int status;
  5390. if (periph->id < PERIPHERAL_ID_MIN)
  5391. return parent_rate;
  5392. - pmc_lock(pmc);
  5393. - pmc_write(pmc, AT91_PMC_PCR, (periph->id & AT91_PMC_PCR_PID_MASK));
  5394. - tmp = pmc_read(pmc, AT91_PMC_PCR);
  5395. - pmc_unlock(pmc);
  5396. + spin_lock_irqsave(periph->lock, flags);
  5397. + regmap_write(periph->regmap, AT91_PMC_PCR,
  5398. + (periph->id & AT91_PMC_PCR_PID_MASK));
  5399. + regmap_read(periph->regmap, AT91_PMC_PCR, &status);
  5400. + spin_unlock_irqrestore(periph->lock, flags);
  5401. - if (tmp & AT91_PMC_PCR_EN) {
  5402. - periph->div = PERIPHERAL_RSHIFT(tmp);
  5403. + if (status & AT91_PMC_PCR_EN) {
  5404. + periph->div = PERIPHERAL_RSHIFT(status);
  5405. periph->auto_div = false;
  5406. } else {
  5407. clk_sam9x5_peripheral_autodiv(periph);
  5408. @@ -318,15 +328,15 @@ static const struct clk_ops sam9x5_peripheral_ops = {
  5409. };
  5410. static struct clk * __init
  5411. -at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5412. - const char *parent_name, u32 id,
  5413. - const struct clk_range *range)
  5414. +at91_clk_register_sam9x5_peripheral(struct regmap *regmap, spinlock_t *lock,
  5415. + const char *name, const char *parent_name,
  5416. + u32 id, const struct clk_range *range)
  5417. {
  5418. struct clk_sam9x5_peripheral *periph;
  5419. struct clk *clk = NULL;
  5420. struct clk_init_data init;
  5421. - if (!pmc || !name || !parent_name)
  5422. + if (!name || !parent_name)
  5423. return ERR_PTR(-EINVAL);
  5424. periph = kzalloc(sizeof(*periph), GFP_KERNEL);
  5425. @@ -342,7 +352,8 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5426. periph->id = id;
  5427. periph->hw.init = &init;
  5428. periph->div = 0;
  5429. - periph->pmc = pmc;
  5430. + periph->regmap = regmap;
  5431. + periph->lock = lock;
  5432. periph->auto_div = true;
  5433. periph->range = *range;
  5434. @@ -356,7 +367,7 @@ at91_clk_register_sam9x5_peripheral(struct at91_pmc *pmc, const char *name,
  5435. }
  5436. static void __init
  5437. -of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5438. +of_at91_clk_periph_setup(struct device_node *np, u8 type)
  5439. {
  5440. int num;
  5441. u32 id;
  5442. @@ -364,6 +375,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5443. const char *parent_name;
  5444. const char *name;
  5445. struct device_node *periphclknp;
  5446. + struct regmap *regmap;
  5447. parent_name = of_clk_get_parent_name(np, 0);
  5448. if (!parent_name)
  5449. @@ -373,6 +385,10 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5450. if (!num || num > PERIPHERAL_MAX)
  5451. return;
  5452. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5453. + if (IS_ERR(regmap))
  5454. + return;
  5455. +
  5456. for_each_child_of_node(np, periphclknp) {
  5457. if (of_property_read_u32(periphclknp, "reg", &id))
  5458. continue;
  5459. @@ -384,7 +400,7 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5460. name = periphclknp->name;
  5461. if (type == PERIPHERAL_AT91RM9200) {
  5462. - clk = at91_clk_register_peripheral(pmc, name,
  5463. + clk = at91_clk_register_peripheral(regmap, name,
  5464. parent_name, id);
  5465. } else {
  5466. struct clk_range range = CLK_RANGE(0, 0);
  5467. @@ -393,7 +409,9 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5468. "atmel,clk-output-range",
  5469. &range);
  5470. - clk = at91_clk_register_sam9x5_peripheral(pmc, name,
  5471. + clk = at91_clk_register_sam9x5_peripheral(regmap,
  5472. + &pmc_pcr_lock,
  5473. + name,
  5474. parent_name,
  5475. id, &range);
  5476. }
  5477. @@ -405,14 +423,16 @@ of_at91_clk_periph_setup(struct device_node *np, struct at91_pmc *pmc, u8 type)
  5478. }
  5479. }
  5480. -void __init of_at91rm9200_clk_periph_setup(struct device_node *np,
  5481. - struct at91_pmc *pmc)
  5482. +static void __init of_at91rm9200_clk_periph_setup(struct device_node *np)
  5483. {
  5484. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91RM9200);
  5485. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91RM9200);
  5486. }
  5487. +CLK_OF_DECLARE(at91rm9200_clk_periph, "atmel,at91rm9200-clk-peripheral",
  5488. + of_at91rm9200_clk_periph_setup);
  5489. -void __init of_at91sam9x5_clk_periph_setup(struct device_node *np,
  5490. - struct at91_pmc *pmc)
  5491. +static void __init of_at91sam9x5_clk_periph_setup(struct device_node *np)
  5492. {
  5493. - of_at91_clk_periph_setup(np, pmc, PERIPHERAL_AT91SAM9X5);
  5494. + of_at91_clk_periph_setup(np, PERIPHERAL_AT91SAM9X5);
  5495. }
  5496. +CLK_OF_DECLARE(at91sam9x5_clk_periph, "atmel,at91sam9x5-clk-peripheral",
  5497. + of_at91sam9x5_clk_periph_setup);
  5498. diff --git a/drivers/clk/at91/clk-pll.c b/drivers/clk/at91/clk-pll.c
  5499. index 18b60f4895a6..fb2e0b56d4b7 100644
  5500. --- a/drivers/clk/at91/clk-pll.c
  5501. +++ b/drivers/clk/at91/clk-pll.c
  5502. @@ -12,14 +12,8 @@
  5503. #include <linux/clkdev.h>
  5504. #include <linux/clk/at91_pmc.h>
  5505. #include <linux/of.h>
  5506. -#include <linux/of_address.h>
  5507. -#include <linux/of_irq.h>
  5508. -#include <linux/io.h>
  5509. -#include <linux/kernel.h>
  5510. -#include <linux/wait.h>
  5511. -#include <linux/sched.h>
  5512. -#include <linux/interrupt.h>
  5513. -#include <linux/irq.h>
  5514. +#include <linux/mfd/syscon.h>
  5515. +#include <linux/regmap.h>
  5516. #include "pmc.h"
  5517. @@ -58,9 +52,7 @@ struct clk_pll_layout {
  5518. struct clk_pll {
  5519. struct clk_hw hw;
  5520. - struct at91_pmc *pmc;
  5521. - unsigned int irq;
  5522. - wait_queue_head_t wait;
  5523. + struct regmap *regmap;
  5524. u8 id;
  5525. u8 div;
  5526. u8 range;
  5527. @@ -69,20 +61,19 @@ struct clk_pll {
  5528. const struct clk_pll_characteristics *characteristics;
  5529. };
  5530. -static irqreturn_t clk_pll_irq_handler(int irq, void *dev_id)
  5531. +static inline bool clk_pll_ready(struct regmap *regmap, int id)
  5532. {
  5533. - struct clk_pll *pll = (struct clk_pll *)dev_id;
  5534. + unsigned int status;
  5535. - wake_up(&pll->wait);
  5536. - disable_irq_nosync(pll->irq);
  5537. + regmap_read(regmap, AT91_PMC_SR, &status);
  5538. - return IRQ_HANDLED;
  5539. + return status & PLL_STATUS_MASK(id) ? 1 : 0;
  5540. }
  5541. static int clk_pll_prepare(struct clk_hw *hw)
  5542. {
  5543. struct clk_pll *pll = to_clk_pll(hw);
  5544. - struct at91_pmc *pmc = pll->pmc;
  5545. + struct regmap *regmap = pll->regmap;
  5546. const struct clk_pll_layout *layout = pll->layout;
  5547. const struct clk_pll_characteristics *characteristics =
  5548. pll->characteristics;
  5549. @@ -90,39 +81,34 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5550. u32 mask = PLL_STATUS_MASK(id);
  5551. int offset = PLL_REG(id);
  5552. u8 out = 0;
  5553. - u32 pllr, icpr;
  5554. + unsigned int pllr;
  5555. + unsigned int status;
  5556. u8 div;
  5557. u16 mul;
  5558. - pllr = pmc_read(pmc, offset);
  5559. + regmap_read(regmap, offset, &pllr);
  5560. div = PLL_DIV(pllr);
  5561. mul = PLL_MUL(pllr, layout);
  5562. - if ((pmc_read(pmc, AT91_PMC_SR) & mask) &&
  5563. + regmap_read(regmap, AT91_PMC_SR, &status);
  5564. + if ((status & mask) &&
  5565. (div == pll->div && mul == pll->mul))
  5566. return 0;
  5567. if (characteristics->out)
  5568. out = characteristics->out[pll->range];
  5569. - if (characteristics->icpll) {
  5570. - icpr = pmc_read(pmc, AT91_PMC_PLLICPR) & ~PLL_ICPR_MASK(id);
  5571. - icpr |= (characteristics->icpll[pll->range] <<
  5572. - PLL_ICPR_SHIFT(id));
  5573. - pmc_write(pmc, AT91_PMC_PLLICPR, icpr);
  5574. - }
  5575. - pllr &= ~layout->pllr_mask;
  5576. - pllr |= layout->pllr_mask &
  5577. - (pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5578. - (out << PLL_OUT_SHIFT) |
  5579. - ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5580. - pmc_write(pmc, offset, pllr);
  5581. -
  5582. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  5583. - enable_irq(pll->irq);
  5584. - wait_event(pll->wait,
  5585. - pmc_read(pmc, AT91_PMC_SR) & mask);
  5586. - }
  5587. + if (characteristics->icpll)
  5588. + regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  5589. + characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  5590. +
  5591. + regmap_update_bits(regmap, offset, layout->pllr_mask,
  5592. + pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  5593. + (out << PLL_OUT_SHIFT) |
  5594. + ((pll->mul & layout->mul_mask) << layout->mul_shift));
  5595. +
  5596. + while (!clk_pll_ready(regmap, pll->id))
  5597. + cpu_relax();
  5598. return 0;
  5599. }
  5600. @@ -130,32 +116,35 @@ static int clk_pll_prepare(struct clk_hw *hw)
  5601. static int clk_pll_is_prepared(struct clk_hw *hw)
  5602. {
  5603. struct clk_pll *pll = to_clk_pll(hw);
  5604. - struct at91_pmc *pmc = pll->pmc;
  5605. - return !!(pmc_read(pmc, AT91_PMC_SR) &
  5606. - PLL_STATUS_MASK(pll->id));
  5607. + return clk_pll_ready(pll->regmap, pll->id);
  5608. }
  5609. static void clk_pll_unprepare(struct clk_hw *hw)
  5610. {
  5611. struct clk_pll *pll = to_clk_pll(hw);
  5612. - struct at91_pmc *pmc = pll->pmc;
  5613. - const struct clk_pll_layout *layout = pll->layout;
  5614. - int offset = PLL_REG(pll->id);
  5615. - u32 tmp = pmc_read(pmc, offset) & ~(layout->pllr_mask);
  5616. + unsigned int mask = pll->layout->pllr_mask;
  5617. - pmc_write(pmc, offset, tmp);
  5618. + regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  5619. }
  5620. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  5621. unsigned long parent_rate)
  5622. {
  5623. struct clk_pll *pll = to_clk_pll(hw);
  5624. + unsigned int pllr;
  5625. + u16 mul;
  5626. + u8 div;
  5627. - if (!pll->div || !pll->mul)
  5628. + regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  5629. +
  5630. + div = PLL_DIV(pllr);
  5631. + mul = PLL_MUL(pllr, pll->layout);
  5632. +
  5633. + if (!div || !mul)
  5634. return 0;
  5635. - return (parent_rate / pll->div) * (pll->mul + 1);
  5636. + return (parent_rate / div) * (mul + 1);
  5637. }
  5638. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  5639. @@ -308,7 +297,7 @@ static const struct clk_ops pll_ops = {
  5640. };
  5641. static struct clk * __init
  5642. -at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5643. +at91_clk_register_pll(struct regmap *regmap, const char *name,
  5644. const char *parent_name, u8 id,
  5645. const struct clk_pll_layout *layout,
  5646. const struct clk_pll_characteristics *characteristics)
  5647. @@ -316,9 +305,8 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5648. struct clk_pll *pll;
  5649. struct clk *clk = NULL;
  5650. struct clk_init_data init;
  5651. - int ret;
  5652. int offset = PLL_REG(id);
  5653. - u32 tmp;
  5654. + unsigned int pllr;
  5655. if (id > PLL_MAX_ID)
  5656. return ERR_PTR(-EINVAL);
  5657. @@ -337,23 +325,13 @@ at91_clk_register_pll(struct at91_pmc *pmc, unsigned int irq, const char *name,
  5658. pll->hw.init = &init;
  5659. pll->layout = layout;
  5660. pll->characteristics = characteristics;
  5661. - pll->pmc = pmc;
  5662. - pll->irq = irq;
  5663. - tmp = pmc_read(pmc, offset) & layout->pllr_mask;
  5664. - pll->div = PLL_DIV(tmp);
  5665. - pll->mul = PLL_MUL(tmp, layout);
  5666. - init_waitqueue_head(&pll->wait);
  5667. - irq_set_status_flags(pll->irq, IRQ_NOAUTOEN);
  5668. - ret = request_irq(pll->irq, clk_pll_irq_handler, IRQF_TRIGGER_HIGH,
  5669. - id ? "clk-pllb" : "clk-plla", pll);
  5670. - if (ret) {
  5671. - kfree(pll);
  5672. - return ERR_PTR(ret);
  5673. - }
  5674. + pll->regmap = regmap;
  5675. + regmap_read(regmap, offset, &pllr);
  5676. + pll->div = PLL_DIV(pllr);
  5677. + pll->mul = PLL_MUL(pllr, layout);
  5678. clk = clk_register(NULL, &pll->hw);
  5679. if (IS_ERR(clk)) {
  5680. - free_irq(pll->irq, pll);
  5681. kfree(pll);
  5682. }
  5683. @@ -483,12 +461,12 @@ out_free_characteristics:
  5684. }
  5685. static void __init
  5686. -of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5687. +of_at91_clk_pll_setup(struct device_node *np,
  5688. const struct clk_pll_layout *layout)
  5689. {
  5690. u32 id;
  5691. - unsigned int irq;
  5692. struct clk *clk;
  5693. + struct regmap *regmap;
  5694. const char *parent_name;
  5695. const char *name = np->name;
  5696. struct clk_pll_characteristics *characteristics;
  5697. @@ -500,15 +478,15 @@ of_at91_clk_pll_setup(struct device_node *np, struct at91_pmc *pmc,
  5698. of_property_read_string(np, "clock-output-names", &name);
  5699. - characteristics = of_at91_clk_pll_get_characteristics(np);
  5700. - if (!characteristics)
  5701. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5702. + if (IS_ERR(regmap))
  5703. return;
  5704. - irq = irq_of_parse_and_map(np, 0);
  5705. - if (!irq)
  5706. + characteristics = of_at91_clk_pll_get_characteristics(np);
  5707. + if (!characteristics)
  5708. return;
  5709. - clk = at91_clk_register_pll(pmc, irq, name, parent_name, id, layout,
  5710. + clk = at91_clk_register_pll(regmap, name, parent_name, id, layout,
  5711. characteristics);
  5712. if (IS_ERR(clk))
  5713. goto out_free_characteristics;
  5714. @@ -520,26 +498,30 @@ out_free_characteristics:
  5715. kfree(characteristics);
  5716. }
  5717. -void __init of_at91rm9200_clk_pll_setup(struct device_node *np,
  5718. - struct at91_pmc *pmc)
  5719. +static void __init of_at91rm9200_clk_pll_setup(struct device_node *np)
  5720. {
  5721. - of_at91_clk_pll_setup(np, pmc, &at91rm9200_pll_layout);
  5722. + of_at91_clk_pll_setup(np, &at91rm9200_pll_layout);
  5723. }
  5724. +CLK_OF_DECLARE(at91rm9200_clk_pll, "atmel,at91rm9200-clk-pll",
  5725. + of_at91rm9200_clk_pll_setup);
  5726. -void __init of_at91sam9g45_clk_pll_setup(struct device_node *np,
  5727. - struct at91_pmc *pmc)
  5728. +static void __init of_at91sam9g45_clk_pll_setup(struct device_node *np)
  5729. {
  5730. - of_at91_clk_pll_setup(np, pmc, &at91sam9g45_pll_layout);
  5731. + of_at91_clk_pll_setup(np, &at91sam9g45_pll_layout);
  5732. }
  5733. +CLK_OF_DECLARE(at91sam9g45_clk_pll, "atmel,at91sam9g45-clk-pll",
  5734. + of_at91sam9g45_clk_pll_setup);
  5735. -void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  5736. - struct at91_pmc *pmc)
  5737. +static void __init of_at91sam9g20_clk_pllb_setup(struct device_node *np)
  5738. {
  5739. - of_at91_clk_pll_setup(np, pmc, &at91sam9g20_pllb_layout);
  5740. + of_at91_clk_pll_setup(np, &at91sam9g20_pllb_layout);
  5741. }
  5742. +CLK_OF_DECLARE(at91sam9g20_clk_pllb, "atmel,at91sam9g20-clk-pllb",
  5743. + of_at91sam9g20_clk_pllb_setup);
  5744. -void __init of_sama5d3_clk_pll_setup(struct device_node *np,
  5745. - struct at91_pmc *pmc)
  5746. +static void __init of_sama5d3_clk_pll_setup(struct device_node *np)
  5747. {
  5748. - of_at91_clk_pll_setup(np, pmc, &sama5d3_pll_layout);
  5749. + of_at91_clk_pll_setup(np, &sama5d3_pll_layout);
  5750. }
  5751. +CLK_OF_DECLARE(sama5d3_clk_pll, "atmel,sama5d3-clk-pll",
  5752. + of_sama5d3_clk_pll_setup);
  5753. diff --git a/drivers/clk/at91/clk-plldiv.c b/drivers/clk/at91/clk-plldiv.c
  5754. index ea226562bb40..2bed26481027 100644
  5755. --- a/drivers/clk/at91/clk-plldiv.c
  5756. +++ b/drivers/clk/at91/clk-plldiv.c
  5757. @@ -12,8 +12,8 @@
  5758. #include <linux/clkdev.h>
  5759. #include <linux/clk/at91_pmc.h>
  5760. #include <linux/of.h>
  5761. -#include <linux/of_address.h>
  5762. -#include <linux/io.h>
  5763. +#include <linux/mfd/syscon.h>
  5764. +#include <linux/regmap.h>
  5765. #include "pmc.h"
  5766. @@ -21,16 +21,18 @@
  5767. struct clk_plldiv {
  5768. struct clk_hw hw;
  5769. - struct at91_pmc *pmc;
  5770. + struct regmap *regmap;
  5771. };
  5772. static unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw,
  5773. unsigned long parent_rate)
  5774. {
  5775. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5776. - struct at91_pmc *pmc = plldiv->pmc;
  5777. + unsigned int mckr;
  5778. - if (pmc_read(pmc, AT91_PMC_MCKR) & AT91_PMC_PLLADIV2)
  5779. + regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr);
  5780. +
  5781. + if (mckr & AT91_PMC_PLLADIV2)
  5782. return parent_rate / 2;
  5783. return parent_rate;
  5784. @@ -57,18 +59,12 @@ static int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate,
  5785. unsigned long parent_rate)
  5786. {
  5787. struct clk_plldiv *plldiv = to_clk_plldiv(hw);
  5788. - struct at91_pmc *pmc = plldiv->pmc;
  5789. - u32 tmp;
  5790. - if (parent_rate != rate && (parent_rate / 2) != rate)
  5791. + if ((parent_rate != rate) && (parent_rate / 2 != rate))
  5792. return -EINVAL;
  5793. - pmc_lock(pmc);
  5794. - tmp = pmc_read(pmc, AT91_PMC_MCKR) & ~AT91_PMC_PLLADIV2;
  5795. - if ((parent_rate / 2) == rate)
  5796. - tmp |= AT91_PMC_PLLADIV2;
  5797. - pmc_write(pmc, AT91_PMC_MCKR, tmp);
  5798. - pmc_unlock(pmc);
  5799. + regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2,
  5800. + parent_rate != rate ? AT91_PMC_PLLADIV2 : 0);
  5801. return 0;
  5802. }
  5803. @@ -80,7 +76,7 @@ static const struct clk_ops plldiv_ops = {
  5804. };
  5805. static struct clk * __init
  5806. -at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5807. +at91_clk_register_plldiv(struct regmap *regmap, const char *name,
  5808. const char *parent_name)
  5809. {
  5810. struct clk_plldiv *plldiv;
  5811. @@ -98,7 +94,7 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5812. init.flags = CLK_SET_RATE_GATE;
  5813. plldiv->hw.init = &init;
  5814. - plldiv->pmc = pmc;
  5815. + plldiv->regmap = regmap;
  5816. clk = clk_register(NULL, &plldiv->hw);
  5817. @@ -109,27 +105,27 @@ at91_clk_register_plldiv(struct at91_pmc *pmc, const char *name,
  5818. }
  5819. static void __init
  5820. -of_at91_clk_plldiv_setup(struct device_node *np, struct at91_pmc *pmc)
  5821. +of_at91sam9x5_clk_plldiv_setup(struct device_node *np)
  5822. {
  5823. struct clk *clk;
  5824. const char *parent_name;
  5825. const char *name = np->name;
  5826. + struct regmap *regmap;
  5827. parent_name = of_clk_get_parent_name(np, 0);
  5828. of_property_read_string(np, "clock-output-names", &name);
  5829. - clk = at91_clk_register_plldiv(pmc, name, parent_name);
  5830. + regmap = syscon_node_to_regmap(of_get_parent(np));
  5831. + if (IS_ERR(regmap))
  5832. + return;
  5833. + clk = at91_clk_register_plldiv(regmap, name, parent_name);
  5834. if (IS_ERR(clk))
  5835. return;
  5836. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  5837. return;
  5838. }
  5839. -
  5840. -void __init of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  5841. - struct at91_pmc *pmc)
  5842. -{
  5843. - of_at91_clk_plldiv_setup(np, pmc);
  5844. -}
  5845. +CLK_OF_DECLARE(at91sam9x5_clk_plldiv, "atmel,at91sam9x5-clk-plldiv",
  5846. + of_at91sam9x5_clk_plldiv_setup);
  5847. diff --git a/drivers/clk/at91/clk-programmable.c b/drivers/clk/at91/clk-programmable.c
  5848. index 14b270b85fec..bc0be629671b 100644
  5849. --- a/drivers/clk/at91/clk-programmable.c
  5850. +++ b/drivers/clk/at91/clk-programmable.c
  5851. @@ -12,10 +12,8 @@
  5852. #include <linux/clkdev.h>
  5853. #include <linux/clk/at91_pmc.h>
  5854. #include <linux/of.h>
  5855. -#include <linux/of_address.h>
  5856. -#include <linux/io.h>
  5857. -#include <linux/wait.h>
  5858. -#include <linux/sched.h>
  5859. +#include <linux/mfd/syscon.h>
  5860. +#include <linux/regmap.h>
  5861. #include "pmc.h"
  5862. @@ -24,6 +22,7 @@
  5863. #define PROG_STATUS_MASK(id) (1 << ((id) + 8))
  5864. #define PROG_PRES_MASK 0x7
  5865. +#define PROG_PRES(layout, pckr) ((pckr >> layout->pres_shift) & PROG_PRES_MASK)
  5866. #define PROG_MAX_RM9200_CSS 3
  5867. struct clk_programmable_layout {
  5868. @@ -34,7 +33,7 @@ struct clk_programmable_layout {
  5869. struct clk_programmable {
  5870. struct clk_hw hw;
  5871. - struct at91_pmc *pmc;
  5872. + struct regmap *regmap;
  5873. u8 id;
  5874. const struct clk_programmable_layout *layout;
  5875. };
  5876. @@ -44,14 +43,12 @@ struct clk_programmable {
  5877. static unsigned long clk_programmable_recalc_rate(struct clk_hw *hw,
  5878. unsigned long parent_rate)
  5879. {
  5880. - u32 pres;
  5881. struct clk_programmable *prog = to_clk_programmable(hw);
  5882. - struct at91_pmc *pmc = prog->pmc;
  5883. - const struct clk_programmable_layout *layout = prog->layout;
  5884. + unsigned int pckr;
  5885. +
  5886. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5887. - pres = (pmc_read(pmc, AT91_PMC_PCKR(prog->id)) >> layout->pres_shift) &
  5888. - PROG_PRES_MASK;
  5889. - return parent_rate >> pres;
  5890. + return parent_rate >> PROG_PRES(prog->layout, pckr);
  5891. }
  5892. static int clk_programmable_determine_rate(struct clk_hw *hw,
  5893. @@ -101,36 +98,36 @@ static int clk_programmable_set_parent(struct clk_hw *hw, u8 index)
  5894. {
  5895. struct clk_programmable *prog = to_clk_programmable(hw);
  5896. const struct clk_programmable_layout *layout = prog->layout;
  5897. - struct at91_pmc *pmc = prog->pmc;
  5898. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) & ~layout->css_mask;
  5899. + unsigned int mask = layout->css_mask;
  5900. + unsigned int pckr = 0;
  5901. if (layout->have_slck_mck)
  5902. - tmp &= AT91_PMC_CSSMCK_MCK;
  5903. + mask |= AT91_PMC_CSSMCK_MCK;
  5904. if (index > layout->css_mask) {
  5905. - if (index > PROG_MAX_RM9200_CSS && layout->have_slck_mck) {
  5906. - tmp |= AT91_PMC_CSSMCK_MCK;
  5907. - return 0;
  5908. - } else {
  5909. + if (index > PROG_MAX_RM9200_CSS && !layout->have_slck_mck)
  5910. return -EINVAL;
  5911. - }
  5912. +
  5913. + pckr |= AT91_PMC_CSSMCK_MCK;
  5914. }
  5915. - pmc_write(pmc, AT91_PMC_PCKR(prog->id), tmp | index);
  5916. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id), mask, pckr);
  5917. +
  5918. return 0;
  5919. }
  5920. static u8 clk_programmable_get_parent(struct clk_hw *hw)
  5921. {
  5922. - u32 tmp;
  5923. - u8 ret;
  5924. struct clk_programmable *prog = to_clk_programmable(hw);
  5925. - struct at91_pmc *pmc = prog->pmc;
  5926. const struct clk_programmable_layout *layout = prog->layout;
  5927. + unsigned int pckr;
  5928. + u8 ret;
  5929. +
  5930. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5931. +
  5932. + ret = pckr & layout->css_mask;
  5933. - tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id));
  5934. - ret = tmp & layout->css_mask;
  5935. - if (layout->have_slck_mck && (tmp & AT91_PMC_CSSMCK_MCK) && !ret)
  5936. + if (layout->have_slck_mck && (pckr & AT91_PMC_CSSMCK_MCK) && !ret)
  5937. ret = PROG_MAX_RM9200_CSS + 1;
  5938. return ret;
  5939. @@ -140,26 +137,27 @@ static int clk_programmable_set_rate(struct clk_hw *hw, unsigned long rate,
  5940. unsigned long parent_rate)
  5941. {
  5942. struct clk_programmable *prog = to_clk_programmable(hw);
  5943. - struct at91_pmc *pmc = prog->pmc;
  5944. const struct clk_programmable_layout *layout = prog->layout;
  5945. unsigned long div = parent_rate / rate;
  5946. + unsigned int pckr;
  5947. int shift = 0;
  5948. - u32 tmp = pmc_read(pmc, AT91_PMC_PCKR(prog->id)) &
  5949. - ~(PROG_PRES_MASK << layout->pres_shift);
  5950. +
  5951. + regmap_read(prog->regmap, AT91_PMC_PCKR(prog->id), &pckr);
  5952. if (!div)
  5953. return -EINVAL;
  5954. shift = fls(div) - 1;
  5955. - if (div != (1<<shift))
  5956. + if (div != (1 << shift))
  5957. return -EINVAL;
  5958. if (shift >= PROG_PRES_MASK)
  5959. return -EINVAL;
  5960. - pmc_write(pmc, AT91_PMC_PCKR(prog->id),
  5961. - tmp | (shift << layout->pres_shift));
  5962. + regmap_update_bits(prog->regmap, AT91_PMC_PCKR(prog->id),
  5963. + PROG_PRES_MASK << layout->pres_shift,
  5964. + shift << layout->pres_shift);
  5965. return 0;
  5966. }
  5967. @@ -173,7 +171,7 @@ static const struct clk_ops programmable_ops = {
  5968. };
  5969. static struct clk * __init
  5970. -at91_clk_register_programmable(struct at91_pmc *pmc,
  5971. +at91_clk_register_programmable(struct regmap *regmap,
  5972. const char *name, const char **parent_names,
  5973. u8 num_parents, u8 id,
  5974. const struct clk_programmable_layout *layout)
  5975. @@ -198,7 +196,7 @@ at91_clk_register_programmable(struct at91_pmc *pmc,
  5976. prog->id = id;
  5977. prog->layout = layout;
  5978. prog->hw.init = &init;
  5979. - prog->pmc = pmc;
  5980. + prog->regmap = regmap;
  5981. clk = clk_register(NULL, &prog->hw);
  5982. if (IS_ERR(clk))
  5983. @@ -226,7 +224,7 @@ static const struct clk_programmable_layout at91sam9x5_programmable_layout = {
  5984. };
  5985. static void __init
  5986. -of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  5987. +of_at91_clk_prog_setup(struct device_node *np,
  5988. const struct clk_programmable_layout *layout)
  5989. {
  5990. int num;
  5991. @@ -236,6 +234,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  5992. const char *parent_names[PROG_SOURCE_MAX];
  5993. const char *name;
  5994. struct device_node *progclknp;
  5995. + struct regmap *regmap;
  5996. num_parents = of_clk_get_parent_count(np);
  5997. if (num_parents <= 0 || num_parents > PROG_SOURCE_MAX)
  5998. @@ -247,6 +246,10 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  5999. if (!num || num > (PROG_ID_MAX + 1))
  6000. return;
  6001. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6002. + if (IS_ERR(regmap))
  6003. + return;
  6004. +
  6005. for_each_child_of_node(np, progclknp) {
  6006. if (of_property_read_u32(progclknp, "reg", &id))
  6007. continue;
  6008. @@ -254,7 +257,7 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6009. if (of_property_read_string(np, "clock-output-names", &name))
  6010. name = progclknp->name;
  6011. - clk = at91_clk_register_programmable(pmc, name,
  6012. + clk = at91_clk_register_programmable(regmap, name,
  6013. parent_names, num_parents,
  6014. id, layout);
  6015. if (IS_ERR(clk))
  6016. @@ -265,20 +268,23 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,
  6017. }
  6018. -void __init of_at91rm9200_clk_prog_setup(struct device_node *np,
  6019. - struct at91_pmc *pmc)
  6020. +static void __init of_at91rm9200_clk_prog_setup(struct device_node *np)
  6021. {
  6022. - of_at91_clk_prog_setup(np, pmc, &at91rm9200_programmable_layout);
  6023. + of_at91_clk_prog_setup(np, &at91rm9200_programmable_layout);
  6024. }
  6025. +CLK_OF_DECLARE(at91rm9200_clk_prog, "atmel,at91rm9200-clk-programmable",
  6026. + of_at91rm9200_clk_prog_setup);
  6027. -void __init of_at91sam9g45_clk_prog_setup(struct device_node *np,
  6028. - struct at91_pmc *pmc)
  6029. +static void __init of_at91sam9g45_clk_prog_setup(struct device_node *np)
  6030. {
  6031. - of_at91_clk_prog_setup(np, pmc, &at91sam9g45_programmable_layout);
  6032. + of_at91_clk_prog_setup(np, &at91sam9g45_programmable_layout);
  6033. }
  6034. +CLK_OF_DECLARE(at91sam9g45_clk_prog, "atmel,at91sam9g45-clk-programmable",
  6035. + of_at91sam9g45_clk_prog_setup);
  6036. -void __init of_at91sam9x5_clk_prog_setup(struct device_node *np,
  6037. - struct at91_pmc *pmc)
  6038. +static void __init of_at91sam9x5_clk_prog_setup(struct device_node *np)
  6039. {
  6040. - of_at91_clk_prog_setup(np, pmc, &at91sam9x5_programmable_layout);
  6041. + of_at91_clk_prog_setup(np, &at91sam9x5_programmable_layout);
  6042. }
  6043. +CLK_OF_DECLARE(at91sam9x5_clk_prog, "atmel,at91sam9x5-clk-programmable",
  6044. + of_at91sam9x5_clk_prog_setup);
  6045. diff --git a/drivers/clk/at91/clk-slow.c b/drivers/clk/at91/clk-slow.c
  6046. index d0d5076a9b94..221c09684ba3 100644
  6047. --- a/drivers/clk/at91/clk-slow.c
  6048. +++ b/drivers/clk/at91/clk-slow.c
  6049. @@ -13,17 +13,11 @@
  6050. #include <linux/clk.h>
  6051. #include <linux/clk-provider.h>
  6052. #include <linux/clkdev.h>
  6053. -#include <linux/slab.h>
  6054. #include <linux/clk/at91_pmc.h>
  6055. #include <linux/delay.h>
  6056. #include <linux/of.h>
  6057. -#include <linux/of_address.h>
  6058. -#include <linux/of_irq.h>
  6059. -#include <linux/io.h>
  6060. -#include <linux/interrupt.h>
  6061. -#include <linux/irq.h>
  6062. -#include <linux/sched.h>
  6063. -#include <linux/wait.h>
  6064. +#include <linux/mfd/syscon.h>
  6065. +#include <linux/regmap.h>
  6066. #include "pmc.h"
  6067. #include "sckc.h"
  6068. @@ -59,7 +53,7 @@ struct clk_slow_rc_osc {
  6069. struct clk_sam9260_slow {
  6070. struct clk_hw hw;
  6071. - struct at91_pmc *pmc;
  6072. + struct regmap *regmap;
  6073. };
  6074. #define to_clk_sam9260_slow(hw) container_of(hw, struct clk_sam9260_slow, hw)
  6075. @@ -393,8 +387,11 @@ void __init of_at91sam9x5_clk_slow_setup(struct device_node *np,
  6076. static u8 clk_sam9260_slow_get_parent(struct clk_hw *hw)
  6077. {
  6078. struct clk_sam9260_slow *slowck = to_clk_sam9260_slow(hw);
  6079. + unsigned int status;
  6080. - return !!(pmc_read(slowck->pmc, AT91_PMC_SR) & AT91_PMC_OSCSEL);
  6081. + regmap_read(slowck->regmap, AT91_PMC_SR, &status);
  6082. +
  6083. + return status & AT91_PMC_OSCSEL ? 1 : 0;
  6084. }
  6085. static const struct clk_ops sam9260_slow_ops = {
  6086. @@ -402,7 +399,7 @@ static const struct clk_ops sam9260_slow_ops = {
  6087. };
  6088. static struct clk * __init
  6089. -at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6090. +at91_clk_register_sam9260_slow(struct regmap *regmap,
  6091. const char *name,
  6092. const char **parent_names,
  6093. int num_parents)
  6094. @@ -411,7 +408,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6095. struct clk *clk = NULL;
  6096. struct clk_init_data init;
  6097. - if (!pmc || !name)
  6098. + if (!name)
  6099. return ERR_PTR(-EINVAL);
  6100. if (!parent_names || !num_parents)
  6101. @@ -428,7 +425,7 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6102. init.flags = 0;
  6103. slowck->hw.init = &init;
  6104. - slowck->pmc = pmc;
  6105. + slowck->regmap = regmap;
  6106. clk = clk_register(NULL, &slowck->hw);
  6107. if (IS_ERR(clk))
  6108. @@ -439,29 +436,34 @@ at91_clk_register_sam9260_slow(struct at91_pmc *pmc,
  6109. return clk;
  6110. }
  6111. -void __init of_at91sam9260_clk_slow_setup(struct device_node *np,
  6112. - struct at91_pmc *pmc)
  6113. +static void __init of_at91sam9260_clk_slow_setup(struct device_node *np)
  6114. {
  6115. struct clk *clk;
  6116. const char *parent_names[2];
  6117. int num_parents;
  6118. const char *name = np->name;
  6119. + struct regmap *regmap;
  6120. num_parents = of_clk_get_parent_count(np);
  6121. if (num_parents != 2)
  6122. return;
  6123. of_clk_parent_fill(np, parent_names, num_parents);
  6124. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6125. + if (IS_ERR(regmap))
  6126. + return;
  6127. of_property_read_string(np, "clock-output-names", &name);
  6128. - clk = at91_clk_register_sam9260_slow(pmc, name, parent_names,
  6129. + clk = at91_clk_register_sam9260_slow(regmap, name, parent_names,
  6130. num_parents);
  6131. if (IS_ERR(clk))
  6132. return;
  6133. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6134. }
  6135. +CLK_OF_DECLARE(at91sam9260_clk_slow, "atmel,at91sam9260-clk-slow",
  6136. + of_at91sam9260_clk_slow_setup);
  6137. /*
  6138. * FIXME: All slow clk users are not properly claiming it (get + prepare +
  6139. diff --git a/drivers/clk/at91/clk-smd.c b/drivers/clk/at91/clk-smd.c
  6140. index a7f8501cfa05..e6948a52005a 100644
  6141. --- a/drivers/clk/at91/clk-smd.c
  6142. +++ b/drivers/clk/at91/clk-smd.c
  6143. @@ -12,8 +12,8 @@
  6144. #include <linux/clkdev.h>
  6145. #include <linux/clk/at91_pmc.h>
  6146. #include <linux/of.h>
  6147. -#include <linux/of_address.h>
  6148. -#include <linux/io.h>
  6149. +#include <linux/mfd/syscon.h>
  6150. +#include <linux/regmap.h>
  6151. #include "pmc.h"
  6152. @@ -24,7 +24,7 @@
  6153. struct at91sam9x5_clk_smd {
  6154. struct clk_hw hw;
  6155. - struct at91_pmc *pmc;
  6156. + struct regmap *regmap;
  6157. };
  6158. #define to_at91sam9x5_clk_smd(hw) \
  6159. @@ -33,13 +33,13 @@ struct at91sam9x5_clk_smd {
  6160. static unsigned long at91sam9x5_clk_smd_recalc_rate(struct clk_hw *hw,
  6161. unsigned long parent_rate)
  6162. {
  6163. - u32 tmp;
  6164. - u8 smddiv;
  6165. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6166. - struct at91_pmc *pmc = smd->pmc;
  6167. + unsigned int smdr;
  6168. + u8 smddiv;
  6169. +
  6170. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6171. + smddiv = (smdr & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6172. - tmp = pmc_read(pmc, AT91_PMC_SMD);
  6173. - smddiv = (tmp & AT91_PMC_SMD_DIV) >> SMD_DIV_SHIFT;
  6174. return parent_rate / (smddiv + 1);
  6175. }
  6176. @@ -67,40 +67,38 @@ static long at91sam9x5_clk_smd_round_rate(struct clk_hw *hw, unsigned long rate,
  6177. static int at91sam9x5_clk_smd_set_parent(struct clk_hw *hw, u8 index)
  6178. {
  6179. - u32 tmp;
  6180. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6181. - struct at91_pmc *pmc = smd->pmc;
  6182. if (index > 1)
  6183. return -EINVAL;
  6184. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMDS;
  6185. - if (index)
  6186. - tmp |= AT91_PMC_SMDS;
  6187. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6188. +
  6189. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMDS,
  6190. + index ? AT91_PMC_SMDS : 0);
  6191. +
  6192. return 0;
  6193. }
  6194. static u8 at91sam9x5_clk_smd_get_parent(struct clk_hw *hw)
  6195. {
  6196. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6197. - struct at91_pmc *pmc = smd->pmc;
  6198. + unsigned int smdr;
  6199. - return pmc_read(pmc, AT91_PMC_SMD) & AT91_PMC_SMDS;
  6200. + regmap_read(smd->regmap, AT91_PMC_SMD, &smdr);
  6201. +
  6202. + return smdr & AT91_PMC_SMDS;
  6203. }
  6204. static int at91sam9x5_clk_smd_set_rate(struct clk_hw *hw, unsigned long rate,
  6205. unsigned long parent_rate)
  6206. {
  6207. - u32 tmp;
  6208. struct at91sam9x5_clk_smd *smd = to_at91sam9x5_clk_smd(hw);
  6209. - struct at91_pmc *pmc = smd->pmc;
  6210. unsigned long div = parent_rate / rate;
  6211. if (parent_rate % rate || div < 1 || div > (SMD_MAX_DIV + 1))
  6212. return -EINVAL;
  6213. - tmp = pmc_read(pmc, AT91_PMC_SMD) & ~AT91_PMC_SMD_DIV;
  6214. - tmp |= (div - 1) << SMD_DIV_SHIFT;
  6215. - pmc_write(pmc, AT91_PMC_SMD, tmp);
  6216. +
  6217. + regmap_update_bits(smd->regmap, AT91_PMC_SMD, AT91_PMC_SMD_DIV,
  6218. + (div - 1) << SMD_DIV_SHIFT);
  6219. return 0;
  6220. }
  6221. @@ -114,7 +112,7 @@ static const struct clk_ops at91sam9x5_smd_ops = {
  6222. };
  6223. static struct clk * __init
  6224. -at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6225. +at91sam9x5_clk_register_smd(struct regmap *regmap, const char *name,
  6226. const char **parent_names, u8 num_parents)
  6227. {
  6228. struct at91sam9x5_clk_smd *smd;
  6229. @@ -132,7 +130,7 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6230. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  6231. smd->hw.init = &init;
  6232. - smd->pmc = pmc;
  6233. + smd->regmap = regmap;
  6234. clk = clk_register(NULL, &smd->hw);
  6235. if (IS_ERR(clk))
  6236. @@ -141,13 +139,13 @@ at91sam9x5_clk_register_smd(struct at91_pmc *pmc, const char *name,
  6237. return clk;
  6238. }
  6239. -void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6240. - struct at91_pmc *pmc)
  6241. +static void __init of_at91sam9x5_clk_smd_setup(struct device_node *np)
  6242. {
  6243. struct clk *clk;
  6244. int num_parents;
  6245. const char *parent_names[SMD_SOURCE_MAX];
  6246. const char *name = np->name;
  6247. + struct regmap *regmap;
  6248. num_parents = of_clk_get_parent_count(np);
  6249. if (num_parents <= 0 || num_parents > SMD_SOURCE_MAX)
  6250. @@ -157,10 +155,16 @@ void __init of_at91sam9x5_clk_smd_setup(struct device_node *np,
  6251. of_property_read_string(np, "clock-output-names", &name);
  6252. - clk = at91sam9x5_clk_register_smd(pmc, name, parent_names,
  6253. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6254. + if (IS_ERR(regmap))
  6255. + return;
  6256. +
  6257. + clk = at91sam9x5_clk_register_smd(regmap, name, parent_names,
  6258. num_parents);
  6259. if (IS_ERR(clk))
  6260. return;
  6261. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6262. }
  6263. +CLK_OF_DECLARE(at91sam9x5_clk_smd, "atmel,at91sam9x5-clk-smd",
  6264. + of_at91sam9x5_clk_smd_setup);
  6265. diff --git a/drivers/clk/at91/clk-system.c b/drivers/clk/at91/clk-system.c
  6266. index 3f5314344286..8f35d8172909 100644
  6267. --- a/drivers/clk/at91/clk-system.c
  6268. +++ b/drivers/clk/at91/clk-system.c
  6269. @@ -12,13 +12,8 @@
  6270. #include <linux/clkdev.h>
  6271. #include <linux/clk/at91_pmc.h>
  6272. #include <linux/of.h>
  6273. -#include <linux/of_address.h>
  6274. -#include <linux/io.h>
  6275. -#include <linux/irq.h>
  6276. -#include <linux/of_irq.h>
  6277. -#include <linux/interrupt.h>
  6278. -#include <linux/wait.h>
  6279. -#include <linux/sched.h>
  6280. +#include <linux/mfd/syscon.h>
  6281. +#include <linux/regmap.h>
  6282. #include "pmc.h"
  6283. @@ -29,9 +24,7 @@
  6284. #define to_clk_system(hw) container_of(hw, struct clk_system, hw)
  6285. struct clk_system {
  6286. struct clk_hw hw;
  6287. - struct at91_pmc *pmc;
  6288. - unsigned int irq;
  6289. - wait_queue_head_t wait;
  6290. + struct regmap *regmap;
  6291. u8 id;
  6292. };
  6293. @@ -39,58 +32,54 @@ static inline int is_pck(int id)
  6294. {
  6295. return (id >= 8) && (id <= 15);
  6296. }
  6297. -static irqreturn_t clk_system_irq_handler(int irq, void *dev_id)
  6298. +
  6299. +static inline bool clk_system_ready(struct regmap *regmap, int id)
  6300. {
  6301. - struct clk_system *sys = (struct clk_system *)dev_id;
  6302. + unsigned int status;
  6303. - wake_up(&sys->wait);
  6304. - disable_irq_nosync(sys->irq);
  6305. + regmap_read(regmap, AT91_PMC_SR, &status);
  6306. - return IRQ_HANDLED;
  6307. + return status & (1 << id) ? 1 : 0;
  6308. }
  6309. static int clk_system_prepare(struct clk_hw *hw)
  6310. {
  6311. struct clk_system *sys = to_clk_system(hw);
  6312. - struct at91_pmc *pmc = sys->pmc;
  6313. - u32 mask = 1 << sys->id;
  6314. - pmc_write(pmc, AT91_PMC_SCER, mask);
  6315. + regmap_write(sys->regmap, AT91_PMC_SCER, 1 << sys->id);
  6316. if (!is_pck(sys->id))
  6317. return 0;
  6318. - while (!(pmc_read(pmc, AT91_PMC_SR) & mask)) {
  6319. - if (sys->irq) {
  6320. - enable_irq(sys->irq);
  6321. - wait_event(sys->wait,
  6322. - pmc_read(pmc, AT91_PMC_SR) & mask);
  6323. - } else
  6324. - cpu_relax();
  6325. - }
  6326. + while (!clk_system_ready(sys->regmap, sys->id))
  6327. + cpu_relax();
  6328. +
  6329. return 0;
  6330. }
  6331. static void clk_system_unprepare(struct clk_hw *hw)
  6332. {
  6333. struct clk_system *sys = to_clk_system(hw);
  6334. - struct at91_pmc *pmc = sys->pmc;
  6335. - pmc_write(pmc, AT91_PMC_SCDR, 1 << sys->id);
  6336. + regmap_write(sys->regmap, AT91_PMC_SCDR, 1 << sys->id);
  6337. }
  6338. static int clk_system_is_prepared(struct clk_hw *hw)
  6339. {
  6340. struct clk_system *sys = to_clk_system(hw);
  6341. - struct at91_pmc *pmc = sys->pmc;
  6342. + unsigned int status;
  6343. +
  6344. + regmap_read(sys->regmap, AT91_PMC_SCSR, &status);
  6345. - if (!(pmc_read(pmc, AT91_PMC_SCSR) & (1 << sys->id)))
  6346. + if (!(status & (1 << sys->id)))
  6347. return 0;
  6348. if (!is_pck(sys->id))
  6349. return 1;
  6350. - return !!(pmc_read(pmc, AT91_PMC_SR) & (1 << sys->id));
  6351. + regmap_read(sys->regmap, AT91_PMC_SR, &status);
  6352. +
  6353. + return status & (1 << sys->id) ? 1 : 0;
  6354. }
  6355. static const struct clk_ops system_ops = {
  6356. @@ -100,13 +89,12 @@ static const struct clk_ops system_ops = {
  6357. };
  6358. static struct clk * __init
  6359. -at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6360. - const char *parent_name, u8 id, int irq)
  6361. +at91_clk_register_system(struct regmap *regmap, const char *name,
  6362. + const char *parent_name, u8 id)
  6363. {
  6364. struct clk_system *sys;
  6365. struct clk *clk = NULL;
  6366. struct clk_init_data init;
  6367. - int ret;
  6368. if (!parent_name || id > SYSTEM_MAX_ID)
  6369. return ERR_PTR(-EINVAL);
  6370. @@ -123,44 +111,33 @@ at91_clk_register_system(struct at91_pmc *pmc, const char *name,
  6371. sys->id = id;
  6372. sys->hw.init = &init;
  6373. - sys->pmc = pmc;
  6374. - sys->irq = irq;
  6375. - if (irq) {
  6376. - init_waitqueue_head(&sys->wait);
  6377. - irq_set_status_flags(sys->irq, IRQ_NOAUTOEN);
  6378. - ret = request_irq(sys->irq, clk_system_irq_handler,
  6379. - IRQF_TRIGGER_HIGH, name, sys);
  6380. - if (ret) {
  6381. - kfree(sys);
  6382. - return ERR_PTR(ret);
  6383. - }
  6384. - }
  6385. + sys->regmap = regmap;
  6386. clk = clk_register(NULL, &sys->hw);
  6387. - if (IS_ERR(clk)) {
  6388. - if (irq)
  6389. - free_irq(sys->irq, sys);
  6390. + if (IS_ERR(clk))
  6391. kfree(sys);
  6392. - }
  6393. return clk;
  6394. }
  6395. -static void __init
  6396. -of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6397. +static void __init of_at91rm9200_clk_sys_setup(struct device_node *np)
  6398. {
  6399. int num;
  6400. - int irq = 0;
  6401. u32 id;
  6402. struct clk *clk;
  6403. const char *name;
  6404. struct device_node *sysclknp;
  6405. const char *parent_name;
  6406. + struct regmap *regmap;
  6407. num = of_get_child_count(np);
  6408. if (num > (SYSTEM_MAX_ID + 1))
  6409. return;
  6410. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6411. + if (IS_ERR(regmap))
  6412. + return;
  6413. +
  6414. for_each_child_of_node(np, sysclknp) {
  6415. if (of_property_read_u32(sysclknp, "reg", &id))
  6416. continue;
  6417. @@ -168,21 +145,14 @@ of_at91_clk_sys_setup(struct device_node *np, struct at91_pmc *pmc)
  6418. if (of_property_read_string(np, "clock-output-names", &name))
  6419. name = sysclknp->name;
  6420. - if (is_pck(id))
  6421. - irq = irq_of_parse_and_map(sysclknp, 0);
  6422. -
  6423. parent_name = of_clk_get_parent_name(sysclknp, 0);
  6424. - clk = at91_clk_register_system(pmc, name, parent_name, id, irq);
  6425. + clk = at91_clk_register_system(regmap, name, parent_name, id);
  6426. if (IS_ERR(clk))
  6427. continue;
  6428. of_clk_add_provider(sysclknp, of_clk_src_simple_get, clk);
  6429. }
  6430. }
  6431. -
  6432. -void __init of_at91rm9200_clk_sys_setup(struct device_node *np,
  6433. - struct at91_pmc *pmc)
  6434. -{
  6435. - of_at91_clk_sys_setup(np, pmc);
  6436. -}
  6437. +CLK_OF_DECLARE(at91rm9200_clk_sys, "atmel,at91rm9200-clk-system",
  6438. + of_at91rm9200_clk_sys_setup);
  6439. diff --git a/drivers/clk/at91/clk-usb.c b/drivers/clk/at91/clk-usb.c
  6440. index 8ab8502778a2..650ca45892c0 100644
  6441. --- a/drivers/clk/at91/clk-usb.c
  6442. +++ b/drivers/clk/at91/clk-usb.c
  6443. @@ -12,8 +12,8 @@
  6444. #include <linux/clkdev.h>
  6445. #include <linux/clk/at91_pmc.h>
  6446. #include <linux/of.h>
  6447. -#include <linux/of_address.h>
  6448. -#include <linux/io.h>
  6449. +#include <linux/mfd/syscon.h>
  6450. +#include <linux/regmap.h>
  6451. #include "pmc.h"
  6452. @@ -27,7 +27,7 @@
  6453. struct at91sam9x5_clk_usb {
  6454. struct clk_hw hw;
  6455. - struct at91_pmc *pmc;
  6456. + struct regmap *regmap;
  6457. };
  6458. #define to_at91sam9x5_clk_usb(hw) \
  6459. @@ -35,7 +35,7 @@ struct at91sam9x5_clk_usb {
  6460. struct at91rm9200_clk_usb {
  6461. struct clk_hw hw;
  6462. - struct at91_pmc *pmc;
  6463. + struct regmap *regmap;
  6464. u32 divisors[4];
  6465. };
  6466. @@ -45,13 +45,12 @@ struct at91rm9200_clk_usb {
  6467. static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
  6468. unsigned long parent_rate)
  6469. {
  6470. - u32 tmp;
  6471. - u8 usbdiv;
  6472. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6473. - struct at91_pmc *pmc = usb->pmc;
  6474. + unsigned int usbr;
  6475. + u8 usbdiv;
  6476. - tmp = pmc_read(pmc, AT91_PMC_USB);
  6477. - usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6478. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6479. + usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
  6480. return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
  6481. }
  6482. @@ -109,33 +108,31 @@ static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
  6483. static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
  6484. {
  6485. - u32 tmp;
  6486. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6487. - struct at91_pmc *pmc = usb->pmc;
  6488. if (index > 1)
  6489. return -EINVAL;
  6490. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
  6491. - if (index)
  6492. - tmp |= AT91_PMC_USBS;
  6493. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6494. +
  6495. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6496. + index ? AT91_PMC_USBS : 0);
  6497. +
  6498. return 0;
  6499. }
  6500. static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
  6501. {
  6502. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6503. - struct at91_pmc *pmc = usb->pmc;
  6504. + unsigned int usbr;
  6505. - return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
  6506. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6507. +
  6508. + return usbr & AT91_PMC_USBS;
  6509. }
  6510. static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6511. unsigned long parent_rate)
  6512. {
  6513. - u32 tmp;
  6514. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6515. - struct at91_pmc *pmc = usb->pmc;
  6516. unsigned long div;
  6517. if (!rate)
  6518. @@ -145,9 +142,8 @@ static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6519. if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
  6520. return -EINVAL;
  6521. - tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
  6522. - tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
  6523. - pmc_write(pmc, AT91_PMC_USB, tmp);
  6524. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
  6525. + (div - 1) << SAM9X5_USB_DIV_SHIFT);
  6526. return 0;
  6527. }
  6528. @@ -163,28 +159,28 @@ static const struct clk_ops at91sam9x5_usb_ops = {
  6529. static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
  6530. {
  6531. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6532. - struct at91_pmc *pmc = usb->pmc;
  6533. - pmc_write(pmc, AT91_PMC_USB,
  6534. - pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
  6535. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
  6536. + AT91_PMC_USBS);
  6537. +
  6538. return 0;
  6539. }
  6540. static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
  6541. {
  6542. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6543. - struct at91_pmc *pmc = usb->pmc;
  6544. - pmc_write(pmc, AT91_PMC_USB,
  6545. - pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
  6546. + regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
  6547. }
  6548. static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
  6549. {
  6550. struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
  6551. - struct at91_pmc *pmc = usb->pmc;
  6552. + unsigned int usbr;
  6553. - return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
  6554. + regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
  6555. +
  6556. + return usbr & AT91_PMC_USBS;
  6557. }
  6558. static const struct clk_ops at91sam9n12_usb_ops = {
  6559. @@ -197,7 +193,7 @@ static const struct clk_ops at91sam9n12_usb_ops = {
  6560. };
  6561. static struct clk * __init
  6562. -at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6563. +at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
  6564. const char **parent_names, u8 num_parents)
  6565. {
  6566. struct at91sam9x5_clk_usb *usb;
  6567. @@ -216,7 +212,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6568. CLK_SET_RATE_PARENT;
  6569. usb->hw.init = &init;
  6570. - usb->pmc = pmc;
  6571. + usb->regmap = regmap;
  6572. clk = clk_register(NULL, &usb->hw);
  6573. if (IS_ERR(clk))
  6574. @@ -226,7 +222,7 @@ at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6575. }
  6576. static struct clk * __init
  6577. -at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6578. +at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
  6579. const char *parent_name)
  6580. {
  6581. struct at91sam9x5_clk_usb *usb;
  6582. @@ -244,7 +240,7 @@ at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6583. init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
  6584. usb->hw.init = &init;
  6585. - usb->pmc = pmc;
  6586. + usb->regmap = regmap;
  6587. clk = clk_register(NULL, &usb->hw);
  6588. if (IS_ERR(clk))
  6589. @@ -257,12 +253,12 @@ static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
  6590. unsigned long parent_rate)
  6591. {
  6592. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6593. - struct at91_pmc *pmc = usb->pmc;
  6594. - u32 tmp;
  6595. + unsigned int pllbr;
  6596. u8 usbdiv;
  6597. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
  6598. - usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6599. + regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
  6600. +
  6601. + usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
  6602. if (usb->divisors[usbdiv])
  6603. return parent_rate / usb->divisors[usbdiv];
  6604. @@ -310,10 +306,8 @@ static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
  6605. static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6606. unsigned long parent_rate)
  6607. {
  6608. - u32 tmp;
  6609. int i;
  6610. struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
  6611. - struct at91_pmc *pmc = usb->pmc;
  6612. unsigned long div;
  6613. if (!rate)
  6614. @@ -323,10 +317,10 @@ static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
  6615. for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
  6616. if (usb->divisors[i] == div) {
  6617. - tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
  6618. - ~AT91_PMC_USBDIV;
  6619. - tmp |= i << RM9200_USB_DIV_SHIFT;
  6620. - pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
  6621. + regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
  6622. + AT91_PMC_USBDIV,
  6623. + i << RM9200_USB_DIV_SHIFT);
  6624. +
  6625. return 0;
  6626. }
  6627. }
  6628. @@ -341,7 +335,7 @@ static const struct clk_ops at91rm9200_usb_ops = {
  6629. };
  6630. static struct clk * __init
  6631. -at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6632. +at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
  6633. const char *parent_name, const u32 *divisors)
  6634. {
  6635. struct at91rm9200_clk_usb *usb;
  6636. @@ -359,7 +353,7 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6637. init.flags = CLK_SET_RATE_PARENT;
  6638. usb->hw.init = &init;
  6639. - usb->pmc = pmc;
  6640. + usb->regmap = regmap;
  6641. memcpy(usb->divisors, divisors, sizeof(usb->divisors));
  6642. clk = clk_register(NULL, &usb->hw);
  6643. @@ -369,13 +363,13 @@ at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
  6644. return clk;
  6645. }
  6646. -void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6647. - struct at91_pmc *pmc)
  6648. +static void __init of_at91sam9x5_clk_usb_setup(struct device_node *np)
  6649. {
  6650. struct clk *clk;
  6651. int num_parents;
  6652. const char *parent_names[USB_SOURCE_MAX];
  6653. const char *name = np->name;
  6654. + struct regmap *regmap;
  6655. num_parents = of_clk_get_parent_count(np);
  6656. if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
  6657. @@ -385,19 +379,26 @@ void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
  6658. of_property_read_string(np, "clock-output-names", &name);
  6659. - clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
  6660. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6661. + if (IS_ERR(regmap))
  6662. + return;
  6663. +
  6664. + clk = at91sam9x5_clk_register_usb(regmap, name, parent_names,
  6665. + num_parents);
  6666. if (IS_ERR(clk))
  6667. return;
  6668. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6669. }
  6670. +CLK_OF_DECLARE(at91sam9x5_clk_usb, "atmel,at91sam9x5-clk-usb",
  6671. + of_at91sam9x5_clk_usb_setup);
  6672. -void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6673. - struct at91_pmc *pmc)
  6674. +static void __init of_at91sam9n12_clk_usb_setup(struct device_node *np)
  6675. {
  6676. struct clk *clk;
  6677. const char *parent_name;
  6678. const char *name = np->name;
  6679. + struct regmap *regmap;
  6680. parent_name = of_clk_get_parent_name(np, 0);
  6681. if (!parent_name)
  6682. @@ -405,20 +406,26 @@ void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
  6683. of_property_read_string(np, "clock-output-names", &name);
  6684. - clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
  6685. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6686. + if (IS_ERR(regmap))
  6687. + return;
  6688. +
  6689. + clk = at91sam9n12_clk_register_usb(regmap, name, parent_name);
  6690. if (IS_ERR(clk))
  6691. return;
  6692. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6693. }
  6694. +CLK_OF_DECLARE(at91sam9n12_clk_usb, "atmel,at91sam9n12-clk-usb",
  6695. + of_at91sam9n12_clk_usb_setup);
  6696. -void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6697. - struct at91_pmc *pmc)
  6698. +static void __init of_at91rm9200_clk_usb_setup(struct device_node *np)
  6699. {
  6700. struct clk *clk;
  6701. const char *parent_name;
  6702. const char *name = np->name;
  6703. u32 divisors[4] = {0, 0, 0, 0};
  6704. + struct regmap *regmap;
  6705. parent_name = of_clk_get_parent_name(np, 0);
  6706. if (!parent_name)
  6707. @@ -430,9 +437,15 @@ void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
  6708. of_property_read_string(np, "clock-output-names", &name);
  6709. - clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
  6710. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6711. + if (IS_ERR(regmap))
  6712. + return;
  6713. +
  6714. + clk = at91rm9200_clk_register_usb(regmap, name, parent_name, divisors);
  6715. if (IS_ERR(clk))
  6716. return;
  6717. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6718. }
  6719. +CLK_OF_DECLARE(at91rm9200_clk_usb, "atmel,at91rm9200-clk-usb",
  6720. + of_at91rm9200_clk_usb_setup);
  6721. diff --git a/drivers/clk/at91/clk-utmi.c b/drivers/clk/at91/clk-utmi.c
  6722. index ca561e90a60f..61fcf399e58c 100644
  6723. --- a/drivers/clk/at91/clk-utmi.c
  6724. +++ b/drivers/clk/at91/clk-utmi.c
  6725. @@ -11,14 +11,9 @@
  6726. #include <linux/clk-provider.h>
  6727. #include <linux/clkdev.h>
  6728. #include <linux/clk/at91_pmc.h>
  6729. -#include <linux/interrupt.h>
  6730. -#include <linux/irq.h>
  6731. #include <linux/of.h>
  6732. -#include <linux/of_address.h>
  6733. -#include <linux/of_irq.h>
  6734. -#include <linux/io.h>
  6735. -#include <linux/sched.h>
  6736. -#include <linux/wait.h>
  6737. +#include <linux/mfd/syscon.h>
  6738. +#include <linux/regmap.h>
  6739. #include "pmc.h"
  6740. @@ -26,37 +21,30 @@
  6741. struct clk_utmi {
  6742. struct clk_hw hw;
  6743. - struct at91_pmc *pmc;
  6744. - unsigned int irq;
  6745. - wait_queue_head_t wait;
  6746. + struct regmap *regmap;
  6747. };
  6748. #define to_clk_utmi(hw) container_of(hw, struct clk_utmi, hw)
  6749. -static irqreturn_t clk_utmi_irq_handler(int irq, void *dev_id)
  6750. +static inline bool clk_utmi_ready(struct regmap *regmap)
  6751. {
  6752. - struct clk_utmi *utmi = (struct clk_utmi *)dev_id;
  6753. + unsigned int status;
  6754. - wake_up(&utmi->wait);
  6755. - disable_irq_nosync(utmi->irq);
  6756. + regmap_read(regmap, AT91_PMC_SR, &status);
  6757. - return IRQ_HANDLED;
  6758. + return status & AT91_PMC_LOCKU;
  6759. }
  6760. static int clk_utmi_prepare(struct clk_hw *hw)
  6761. {
  6762. struct clk_utmi *utmi = to_clk_utmi(hw);
  6763. - struct at91_pmc *pmc = utmi->pmc;
  6764. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) | AT91_PMC_UPLLEN |
  6765. - AT91_PMC_UPLLCOUNT | AT91_PMC_BIASEN;
  6766. + unsigned int uckr = AT91_PMC_UPLLEN | AT91_PMC_UPLLCOUNT |
  6767. + AT91_PMC_BIASEN;
  6768. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6769. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, uckr, uckr);
  6770. - while (!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU)) {
  6771. - enable_irq(utmi->irq);
  6772. - wait_event(utmi->wait,
  6773. - pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6774. - }
  6775. + while (!clk_utmi_ready(utmi->regmap))
  6776. + cpu_relax();
  6777. return 0;
  6778. }
  6779. @@ -64,18 +52,15 @@ static int clk_utmi_prepare(struct clk_hw *hw)
  6780. static int clk_utmi_is_prepared(struct clk_hw *hw)
  6781. {
  6782. struct clk_utmi *utmi = to_clk_utmi(hw);
  6783. - struct at91_pmc *pmc = utmi->pmc;
  6784. - return !!(pmc_read(pmc, AT91_PMC_SR) & AT91_PMC_LOCKU);
  6785. + return clk_utmi_ready(utmi->regmap);
  6786. }
  6787. static void clk_utmi_unprepare(struct clk_hw *hw)
  6788. {
  6789. struct clk_utmi *utmi = to_clk_utmi(hw);
  6790. - struct at91_pmc *pmc = utmi->pmc;
  6791. - u32 tmp = pmc_read(pmc, AT91_CKGR_UCKR) & ~AT91_PMC_UPLLEN;
  6792. - pmc_write(pmc, AT91_CKGR_UCKR, tmp);
  6793. + regmap_update_bits(utmi->regmap, AT91_CKGR_UCKR, AT91_PMC_UPLLEN, 0);
  6794. }
  6795. static unsigned long clk_utmi_recalc_rate(struct clk_hw *hw,
  6796. @@ -93,10 +78,9 @@ static const struct clk_ops utmi_ops = {
  6797. };
  6798. static struct clk * __init
  6799. -at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6800. +at91_clk_register_utmi(struct regmap *regmap,
  6801. const char *name, const char *parent_name)
  6802. {
  6803. - int ret;
  6804. struct clk_utmi *utmi;
  6805. struct clk *clk = NULL;
  6806. struct clk_init_data init;
  6807. @@ -112,52 +96,36 @@ at91_clk_register_utmi(struct at91_pmc *pmc, unsigned int irq,
  6808. init.flags = CLK_SET_RATE_GATE;
  6809. utmi->hw.init = &init;
  6810. - utmi->pmc = pmc;
  6811. - utmi->irq = irq;
  6812. - init_waitqueue_head(&utmi->wait);
  6813. - irq_set_status_flags(utmi->irq, IRQ_NOAUTOEN);
  6814. - ret = request_irq(utmi->irq, clk_utmi_irq_handler,
  6815. - IRQF_TRIGGER_HIGH, "clk-utmi", utmi);
  6816. - if (ret) {
  6817. - kfree(utmi);
  6818. - return ERR_PTR(ret);
  6819. - }
  6820. + utmi->regmap = regmap;
  6821. clk = clk_register(NULL, &utmi->hw);
  6822. - if (IS_ERR(clk)) {
  6823. - free_irq(utmi->irq, utmi);
  6824. + if (IS_ERR(clk))
  6825. kfree(utmi);
  6826. - }
  6827. return clk;
  6828. }
  6829. -static void __init
  6830. -of_at91_clk_utmi_setup(struct device_node *np, struct at91_pmc *pmc)
  6831. +static void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np)
  6832. {
  6833. - unsigned int irq;
  6834. struct clk *clk;
  6835. const char *parent_name;
  6836. const char *name = np->name;
  6837. + struct regmap *regmap;
  6838. parent_name = of_clk_get_parent_name(np, 0);
  6839. of_property_read_string(np, "clock-output-names", &name);
  6840. - irq = irq_of_parse_and_map(np, 0);
  6841. - if (!irq)
  6842. + regmap = syscon_node_to_regmap(of_get_parent(np));
  6843. + if (IS_ERR(regmap))
  6844. return;
  6845. - clk = at91_clk_register_utmi(pmc, irq, name, parent_name);
  6846. + clk = at91_clk_register_utmi(regmap, name, parent_name);
  6847. if (IS_ERR(clk))
  6848. return;
  6849. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  6850. return;
  6851. }
  6852. -
  6853. -void __init of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  6854. - struct at91_pmc *pmc)
  6855. -{
  6856. - of_at91_clk_utmi_setup(np, pmc);
  6857. -}
  6858. +CLK_OF_DECLARE(at91sam9x5_clk_utmi, "atmel,at91sam9x5-clk-utmi",
  6859. + of_at91sam9x5_clk_utmi_setup);
  6860. diff --git a/drivers/clk/at91/pmc.c b/drivers/clk/at91/pmc.c
  6861. index 8476b570779b..526df5ba042d 100644
  6862. --- a/drivers/clk/at91/pmc.c
  6863. +++ b/drivers/clk/at91/pmc.c
  6864. @@ -12,36 +12,13 @@
  6865. #include <linux/clkdev.h>
  6866. #include <linux/clk/at91_pmc.h>
  6867. #include <linux/of.h>
  6868. -#include <linux/of_address.h>
  6869. -#include <linux/io.h>
  6870. -#include <linux/interrupt.h>
  6871. -#include <linux/irq.h>
  6872. -#include <linux/irqchip/chained_irq.h>
  6873. -#include <linux/irqdomain.h>
  6874. -#include <linux/of_irq.h>
  6875. +#include <linux/mfd/syscon.h>
  6876. +#include <linux/regmap.h>
  6877. #include <asm/proc-fns.h>
  6878. #include "pmc.h"
  6879. -void __iomem *at91_pmc_base;
  6880. -EXPORT_SYMBOL_GPL(at91_pmc_base);
  6881. -
  6882. -void at91rm9200_idle(void)
  6883. -{
  6884. - /*
  6885. - * Disable the processor clock. The processor will be automatically
  6886. - * re-enabled by an interrupt or by a reset.
  6887. - */
  6888. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6889. -}
  6890. -
  6891. -void at91sam9_idle(void)
  6892. -{
  6893. - at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  6894. - cpu_do_idle();
  6895. -}
  6896. -
  6897. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  6898. struct clk_range *range)
  6899. {
  6900. @@ -64,402 +41,3 @@ int of_at91_get_clk_range(struct device_node *np, const char *propname,
  6901. return 0;
  6902. }
  6903. EXPORT_SYMBOL_GPL(of_at91_get_clk_range);
  6904. -
  6905. -static void pmc_irq_mask(struct irq_data *d)
  6906. -{
  6907. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6908. -
  6909. - pmc_write(pmc, AT91_PMC_IDR, 1 << d->hwirq);
  6910. -}
  6911. -
  6912. -static void pmc_irq_unmask(struct irq_data *d)
  6913. -{
  6914. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6915. -
  6916. - pmc_write(pmc, AT91_PMC_IER, 1 << d->hwirq);
  6917. -}
  6918. -
  6919. -static int pmc_irq_set_type(struct irq_data *d, unsigned type)
  6920. -{
  6921. - if (type != IRQ_TYPE_LEVEL_HIGH) {
  6922. - pr_warn("PMC: type not supported (support only IRQ_TYPE_LEVEL_HIGH type)\n");
  6923. - return -EINVAL;
  6924. - }
  6925. -
  6926. - return 0;
  6927. -}
  6928. -
  6929. -static void pmc_irq_suspend(struct irq_data *d)
  6930. -{
  6931. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6932. -
  6933. - pmc->imr = pmc_read(pmc, AT91_PMC_IMR);
  6934. - pmc_write(pmc, AT91_PMC_IDR, pmc->imr);
  6935. -}
  6936. -
  6937. -static void pmc_irq_resume(struct irq_data *d)
  6938. -{
  6939. - struct at91_pmc *pmc = irq_data_get_irq_chip_data(d);
  6940. -
  6941. - pmc_write(pmc, AT91_PMC_IER, pmc->imr);
  6942. -}
  6943. -
  6944. -static struct irq_chip pmc_irq = {
  6945. - .name = "PMC",
  6946. - .irq_disable = pmc_irq_mask,
  6947. - .irq_mask = pmc_irq_mask,
  6948. - .irq_unmask = pmc_irq_unmask,
  6949. - .irq_set_type = pmc_irq_set_type,
  6950. - .irq_suspend = pmc_irq_suspend,
  6951. - .irq_resume = pmc_irq_resume,
  6952. -};
  6953. -
  6954. -static struct lock_class_key pmc_lock_class;
  6955. -
  6956. -static int pmc_irq_map(struct irq_domain *h, unsigned int virq,
  6957. - irq_hw_number_t hw)
  6958. -{
  6959. - struct at91_pmc *pmc = h->host_data;
  6960. -
  6961. - irq_set_lockdep_class(virq, &pmc_lock_class);
  6962. -
  6963. - irq_set_chip_and_handler(virq, &pmc_irq,
  6964. - handle_level_irq);
  6965. - irq_set_chip_data(virq, pmc);
  6966. -
  6967. - return 0;
  6968. -}
  6969. -
  6970. -static int pmc_irq_domain_xlate(struct irq_domain *d,
  6971. - struct device_node *ctrlr,
  6972. - const u32 *intspec, unsigned int intsize,
  6973. - irq_hw_number_t *out_hwirq,
  6974. - unsigned int *out_type)
  6975. -{
  6976. - struct at91_pmc *pmc = d->host_data;
  6977. - const struct at91_pmc_caps *caps = pmc->caps;
  6978. -
  6979. - if (WARN_ON(intsize < 1))
  6980. - return -EINVAL;
  6981. -
  6982. - *out_hwirq = intspec[0];
  6983. -
  6984. - if (!(caps->available_irqs & (1 << *out_hwirq)))
  6985. - return -EINVAL;
  6986. -
  6987. - *out_type = IRQ_TYPE_LEVEL_HIGH;
  6988. -
  6989. - return 0;
  6990. -}
  6991. -
  6992. -static const struct irq_domain_ops pmc_irq_ops = {
  6993. - .map = pmc_irq_map,
  6994. - .xlate = pmc_irq_domain_xlate,
  6995. -};
  6996. -
  6997. -static irqreturn_t pmc_irq_handler(int irq, void *data)
  6998. -{
  6999. - struct at91_pmc *pmc = (struct at91_pmc *)data;
  7000. - unsigned long sr;
  7001. - int n;
  7002. -
  7003. - sr = pmc_read(pmc, AT91_PMC_SR) & pmc_read(pmc, AT91_PMC_IMR);
  7004. - if (!sr)
  7005. - return IRQ_NONE;
  7006. -
  7007. - for_each_set_bit(n, &sr, BITS_PER_LONG)
  7008. - generic_handle_irq(irq_find_mapping(pmc->irqdomain, n));
  7009. -
  7010. - return IRQ_HANDLED;
  7011. -}
  7012. -
  7013. -static const struct at91_pmc_caps at91rm9200_caps = {
  7014. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7015. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7016. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7017. - AT91_PMC_PCK3RDY,
  7018. -};
  7019. -
  7020. -static const struct at91_pmc_caps at91sam9260_caps = {
  7021. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7022. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7023. - AT91_PMC_PCK1RDY,
  7024. -};
  7025. -
  7026. -static const struct at91_pmc_caps at91sam9g45_caps = {
  7027. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7028. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7029. - AT91_PMC_PCK1RDY,
  7030. -};
  7031. -
  7032. -static const struct at91_pmc_caps at91sam9n12_caps = {
  7033. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_LOCKB |
  7034. - AT91_PMC_MCKRDY | AT91_PMC_PCK0RDY |
  7035. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7036. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7037. -};
  7038. -
  7039. -static const struct at91_pmc_caps at91sam9x5_caps = {
  7040. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7041. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7042. - AT91_PMC_PCK1RDY | AT91_PMC_MOSCSELS |
  7043. - AT91_PMC_MOSCRCS | AT91_PMC_CFDEV,
  7044. -};
  7045. -
  7046. -static const struct at91_pmc_caps sama5d2_caps = {
  7047. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7048. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7049. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7050. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7051. - AT91_PMC_CFDEV | AT91_PMC_GCKRDY,
  7052. -};
  7053. -
  7054. -static const struct at91_pmc_caps sama5d3_caps = {
  7055. - .available_irqs = AT91_PMC_MOSCS | AT91_PMC_LOCKA | AT91_PMC_MCKRDY |
  7056. - AT91_PMC_LOCKU | AT91_PMC_PCK0RDY |
  7057. - AT91_PMC_PCK1RDY | AT91_PMC_PCK2RDY |
  7058. - AT91_PMC_MOSCSELS | AT91_PMC_MOSCRCS |
  7059. - AT91_PMC_CFDEV,
  7060. -};
  7061. -
  7062. -static struct at91_pmc *__init at91_pmc_init(struct device_node *np,
  7063. - void __iomem *regbase, int virq,
  7064. - const struct at91_pmc_caps *caps)
  7065. -{
  7066. - struct at91_pmc *pmc;
  7067. -
  7068. - if (!regbase || !virq || !caps)
  7069. - return NULL;
  7070. -
  7071. - at91_pmc_base = regbase;
  7072. -
  7073. - pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
  7074. - if (!pmc)
  7075. - return NULL;
  7076. -
  7077. - spin_lock_init(&pmc->lock);
  7078. - pmc->regbase = regbase;
  7079. - pmc->virq = virq;
  7080. - pmc->caps = caps;
  7081. -
  7082. - pmc->irqdomain = irq_domain_add_linear(np, 32, &pmc_irq_ops, pmc);
  7083. -
  7084. - if (!pmc->irqdomain)
  7085. - goto out_free_pmc;
  7086. -
  7087. - pmc_write(pmc, AT91_PMC_IDR, 0xffffffff);
  7088. - if (request_irq(pmc->virq, pmc_irq_handler,
  7089. - IRQF_SHARED | IRQF_COND_SUSPEND, "pmc", pmc))
  7090. - goto out_remove_irqdomain;
  7091. -
  7092. - return pmc;
  7093. -
  7094. -out_remove_irqdomain:
  7095. - irq_domain_remove(pmc->irqdomain);
  7096. -out_free_pmc:
  7097. - kfree(pmc);
  7098. -
  7099. - return NULL;
  7100. -}
  7101. -
  7102. -static const struct of_device_id pmc_clk_ids[] __initconst = {
  7103. - /* Slow oscillator */
  7104. - {
  7105. - .compatible = "atmel,at91sam9260-clk-slow",
  7106. - .data = of_at91sam9260_clk_slow_setup,
  7107. - },
  7108. - /* Main clock */
  7109. - {
  7110. - .compatible = "atmel,at91rm9200-clk-main-osc",
  7111. - .data = of_at91rm9200_clk_main_osc_setup,
  7112. - },
  7113. - {
  7114. - .compatible = "atmel,at91sam9x5-clk-main-rc-osc",
  7115. - .data = of_at91sam9x5_clk_main_rc_osc_setup,
  7116. - },
  7117. - {
  7118. - .compatible = "atmel,at91rm9200-clk-main",
  7119. - .data = of_at91rm9200_clk_main_setup,
  7120. - },
  7121. - {
  7122. - .compatible = "atmel,at91sam9x5-clk-main",
  7123. - .data = of_at91sam9x5_clk_main_setup,
  7124. - },
  7125. - /* PLL clocks */
  7126. - {
  7127. - .compatible = "atmel,at91rm9200-clk-pll",
  7128. - .data = of_at91rm9200_clk_pll_setup,
  7129. - },
  7130. - {
  7131. - .compatible = "atmel,at91sam9g45-clk-pll",
  7132. - .data = of_at91sam9g45_clk_pll_setup,
  7133. - },
  7134. - {
  7135. - .compatible = "atmel,at91sam9g20-clk-pllb",
  7136. - .data = of_at91sam9g20_clk_pllb_setup,
  7137. - },
  7138. - {
  7139. - .compatible = "atmel,sama5d3-clk-pll",
  7140. - .data = of_sama5d3_clk_pll_setup,
  7141. - },
  7142. - {
  7143. - .compatible = "atmel,at91sam9x5-clk-plldiv",
  7144. - .data = of_at91sam9x5_clk_plldiv_setup,
  7145. - },
  7146. - /* Master clock */
  7147. - {
  7148. - .compatible = "atmel,at91rm9200-clk-master",
  7149. - .data = of_at91rm9200_clk_master_setup,
  7150. - },
  7151. - {
  7152. - .compatible = "atmel,at91sam9x5-clk-master",
  7153. - .data = of_at91sam9x5_clk_master_setup,
  7154. - },
  7155. - /* System clocks */
  7156. - {
  7157. - .compatible = "atmel,at91rm9200-clk-system",
  7158. - .data = of_at91rm9200_clk_sys_setup,
  7159. - },
  7160. - /* Peripheral clocks */
  7161. - {
  7162. - .compatible = "atmel,at91rm9200-clk-peripheral",
  7163. - .data = of_at91rm9200_clk_periph_setup,
  7164. - },
  7165. - {
  7166. - .compatible = "atmel,at91sam9x5-clk-peripheral",
  7167. - .data = of_at91sam9x5_clk_periph_setup,
  7168. - },
  7169. - /* Programmable clocks */
  7170. - {
  7171. - .compatible = "atmel,at91rm9200-clk-programmable",
  7172. - .data = of_at91rm9200_clk_prog_setup,
  7173. - },
  7174. - {
  7175. - .compatible = "atmel,at91sam9g45-clk-programmable",
  7176. - .data = of_at91sam9g45_clk_prog_setup,
  7177. - },
  7178. - {
  7179. - .compatible = "atmel,at91sam9x5-clk-programmable",
  7180. - .data = of_at91sam9x5_clk_prog_setup,
  7181. - },
  7182. - /* UTMI clock */
  7183. -#if defined(CONFIG_HAVE_AT91_UTMI)
  7184. - {
  7185. - .compatible = "atmel,at91sam9x5-clk-utmi",
  7186. - .data = of_at91sam9x5_clk_utmi_setup,
  7187. - },
  7188. -#endif
  7189. - /* USB clock */
  7190. -#if defined(CONFIG_HAVE_AT91_USB_CLK)
  7191. - {
  7192. - .compatible = "atmel,at91rm9200-clk-usb",
  7193. - .data = of_at91rm9200_clk_usb_setup,
  7194. - },
  7195. - {
  7196. - .compatible = "atmel,at91sam9x5-clk-usb",
  7197. - .data = of_at91sam9x5_clk_usb_setup,
  7198. - },
  7199. - {
  7200. - .compatible = "atmel,at91sam9n12-clk-usb",
  7201. - .data = of_at91sam9n12_clk_usb_setup,
  7202. - },
  7203. -#endif
  7204. - /* SMD clock */
  7205. -#if defined(CONFIG_HAVE_AT91_SMD)
  7206. - {
  7207. - .compatible = "atmel,at91sam9x5-clk-smd",
  7208. - .data = of_at91sam9x5_clk_smd_setup,
  7209. - },
  7210. -#endif
  7211. -#if defined(CONFIG_HAVE_AT91_H32MX)
  7212. - {
  7213. - .compatible = "atmel,sama5d4-clk-h32mx",
  7214. - .data = of_sama5d4_clk_h32mx_setup,
  7215. - },
  7216. -#endif
  7217. -#if defined(CONFIG_HAVE_AT91_GENERATED_CLK)
  7218. - {
  7219. - .compatible = "atmel,sama5d2-clk-generated",
  7220. - .data = of_sama5d2_clk_generated_setup,
  7221. - },
  7222. -#endif
  7223. - { /*sentinel*/ }
  7224. -};
  7225. -
  7226. -static void __init of_at91_pmc_setup(struct device_node *np,
  7227. - const struct at91_pmc_caps *caps)
  7228. -{
  7229. - struct at91_pmc *pmc;
  7230. - struct device_node *childnp;
  7231. - void (*clk_setup)(struct device_node *, struct at91_pmc *);
  7232. - const struct of_device_id *clk_id;
  7233. - void __iomem *regbase = of_iomap(np, 0);
  7234. - int virq;
  7235. -
  7236. - if (!regbase)
  7237. - return;
  7238. -
  7239. - virq = irq_of_parse_and_map(np, 0);
  7240. - if (!virq)
  7241. - return;
  7242. -
  7243. - pmc = at91_pmc_init(np, regbase, virq, caps);
  7244. - if (!pmc)
  7245. - return;
  7246. - for_each_child_of_node(np, childnp) {
  7247. - clk_id = of_match_node(pmc_clk_ids, childnp);
  7248. - if (!clk_id)
  7249. - continue;
  7250. - clk_setup = clk_id->data;
  7251. - clk_setup(childnp, pmc);
  7252. - }
  7253. -}
  7254. -
  7255. -static void __init of_at91rm9200_pmc_setup(struct device_node *np)
  7256. -{
  7257. - of_at91_pmc_setup(np, &at91rm9200_caps);
  7258. -}
  7259. -CLK_OF_DECLARE(at91rm9200_clk_pmc, "atmel,at91rm9200-pmc",
  7260. - of_at91rm9200_pmc_setup);
  7261. -
  7262. -static void __init of_at91sam9260_pmc_setup(struct device_node *np)
  7263. -{
  7264. - of_at91_pmc_setup(np, &at91sam9260_caps);
  7265. -}
  7266. -CLK_OF_DECLARE(at91sam9260_clk_pmc, "atmel,at91sam9260-pmc",
  7267. - of_at91sam9260_pmc_setup);
  7268. -
  7269. -static void __init of_at91sam9g45_pmc_setup(struct device_node *np)
  7270. -{
  7271. - of_at91_pmc_setup(np, &at91sam9g45_caps);
  7272. -}
  7273. -CLK_OF_DECLARE(at91sam9g45_clk_pmc, "atmel,at91sam9g45-pmc",
  7274. - of_at91sam9g45_pmc_setup);
  7275. -
  7276. -static void __init of_at91sam9n12_pmc_setup(struct device_node *np)
  7277. -{
  7278. - of_at91_pmc_setup(np, &at91sam9n12_caps);
  7279. -}
  7280. -CLK_OF_DECLARE(at91sam9n12_clk_pmc, "atmel,at91sam9n12-pmc",
  7281. - of_at91sam9n12_pmc_setup);
  7282. -
  7283. -static void __init of_at91sam9x5_pmc_setup(struct device_node *np)
  7284. -{
  7285. - of_at91_pmc_setup(np, &at91sam9x5_caps);
  7286. -}
  7287. -CLK_OF_DECLARE(at91sam9x5_clk_pmc, "atmel,at91sam9x5-pmc",
  7288. - of_at91sam9x5_pmc_setup);
  7289. -
  7290. -static void __init of_sama5d2_pmc_setup(struct device_node *np)
  7291. -{
  7292. - of_at91_pmc_setup(np, &sama5d2_caps);
  7293. -}
  7294. -CLK_OF_DECLARE(sama5d2_clk_pmc, "atmel,sama5d2-pmc",
  7295. - of_sama5d2_pmc_setup);
  7296. -
  7297. -static void __init of_sama5d3_pmc_setup(struct device_node *np)
  7298. -{
  7299. - of_at91_pmc_setup(np, &sama5d3_caps);
  7300. -}
  7301. -CLK_OF_DECLARE(sama5d3_clk_pmc, "atmel,sama5d3-pmc",
  7302. - of_sama5d3_pmc_setup);
  7303. diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
  7304. index f65739272779..5771fff0ee3f 100644
  7305. --- a/drivers/clk/at91/pmc.h
  7306. +++ b/drivers/clk/at91/pmc.h
  7307. @@ -14,8 +14,11 @@
  7308. #include <linux/io.h>
  7309. #include <linux/irqdomain.h>
  7310. +#include <linux/regmap.h>
  7311. #include <linux/spinlock.h>
  7312. +extern spinlock_t pmc_pcr_lock;
  7313. +
  7314. struct clk_range {
  7315. unsigned long min;
  7316. unsigned long max;
  7317. @@ -23,102 +26,7 @@ struct clk_range {
  7318. #define CLK_RANGE(MIN, MAX) {.min = MIN, .max = MAX,}
  7319. -struct at91_pmc_caps {
  7320. - u32 available_irqs;
  7321. -};
  7322. -
  7323. -struct at91_pmc {
  7324. - void __iomem *regbase;
  7325. - int virq;
  7326. - spinlock_t lock;
  7327. - const struct at91_pmc_caps *caps;
  7328. - struct irq_domain *irqdomain;
  7329. - u32 imr;
  7330. -};
  7331. -
  7332. -static inline void pmc_lock(struct at91_pmc *pmc)
  7333. -{
  7334. - spin_lock(&pmc->lock);
  7335. -}
  7336. -
  7337. -static inline void pmc_unlock(struct at91_pmc *pmc)
  7338. -{
  7339. - spin_unlock(&pmc->lock);
  7340. -}
  7341. -
  7342. -static inline u32 pmc_read(struct at91_pmc *pmc, int offset)
  7343. -{
  7344. - return readl(pmc->regbase + offset);
  7345. -}
  7346. -
  7347. -static inline void pmc_write(struct at91_pmc *pmc, int offset, u32 value)
  7348. -{
  7349. - writel(value, pmc->regbase + offset);
  7350. -}
  7351. -
  7352. int of_at91_get_clk_range(struct device_node *np, const char *propname,
  7353. struct clk_range *range);
  7354. -void of_at91sam9260_clk_slow_setup(struct device_node *np,
  7355. - struct at91_pmc *pmc);
  7356. -
  7357. -void of_at91rm9200_clk_main_osc_setup(struct device_node *np,
  7358. - struct at91_pmc *pmc);
  7359. -void of_at91sam9x5_clk_main_rc_osc_setup(struct device_node *np,
  7360. - struct at91_pmc *pmc);
  7361. -void of_at91rm9200_clk_main_setup(struct device_node *np,
  7362. - struct at91_pmc *pmc);
  7363. -void of_at91sam9x5_clk_main_setup(struct device_node *np,
  7364. - struct at91_pmc *pmc);
  7365. -
  7366. -void of_at91rm9200_clk_pll_setup(struct device_node *np,
  7367. - struct at91_pmc *pmc);
  7368. -void of_at91sam9g45_clk_pll_setup(struct device_node *np,
  7369. - struct at91_pmc *pmc);
  7370. -void of_at91sam9g20_clk_pllb_setup(struct device_node *np,
  7371. - struct at91_pmc *pmc);
  7372. -void of_sama5d3_clk_pll_setup(struct device_node *np,
  7373. - struct at91_pmc *pmc);
  7374. -void of_at91sam9x5_clk_plldiv_setup(struct device_node *np,
  7375. - struct at91_pmc *pmc);
  7376. -
  7377. -void of_at91rm9200_clk_master_setup(struct device_node *np,
  7378. - struct at91_pmc *pmc);
  7379. -void of_at91sam9x5_clk_master_setup(struct device_node *np,
  7380. - struct at91_pmc *pmc);
  7381. -
  7382. -void of_at91rm9200_clk_sys_setup(struct device_node *np,
  7383. - struct at91_pmc *pmc);
  7384. -
  7385. -void of_at91rm9200_clk_periph_setup(struct device_node *np,
  7386. - struct at91_pmc *pmc);
  7387. -void of_at91sam9x5_clk_periph_setup(struct device_node *np,
  7388. - struct at91_pmc *pmc);
  7389. -
  7390. -void of_at91rm9200_clk_prog_setup(struct device_node *np,
  7391. - struct at91_pmc *pmc);
  7392. -void of_at91sam9g45_clk_prog_setup(struct device_node *np,
  7393. - struct at91_pmc *pmc);
  7394. -void of_at91sam9x5_clk_prog_setup(struct device_node *np,
  7395. - struct at91_pmc *pmc);
  7396. -
  7397. -void of_at91sam9x5_clk_utmi_setup(struct device_node *np,
  7398. - struct at91_pmc *pmc);
  7399. -
  7400. -void of_at91rm9200_clk_usb_setup(struct device_node *np,
  7401. - struct at91_pmc *pmc);
  7402. -void of_at91sam9x5_clk_usb_setup(struct device_node *np,
  7403. - struct at91_pmc *pmc);
  7404. -void of_at91sam9n12_clk_usb_setup(struct device_node *np,
  7405. - struct at91_pmc *pmc);
  7406. -
  7407. -void of_at91sam9x5_clk_smd_setup(struct device_node *np,
  7408. - struct at91_pmc *pmc);
  7409. -
  7410. -void of_sama5d4_clk_h32mx_setup(struct device_node *np,
  7411. - struct at91_pmc *pmc);
  7412. -
  7413. -void of_sama5d2_clk_generated_setup(struct device_node *np,
  7414. - struct at91_pmc *pmc);
  7415. -
  7416. #endif /* __PMC_H_ */
  7417. diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
  7418. index 4da2af9694a2..5b6f57f500b8 100644
  7419. --- a/drivers/clocksource/tcb_clksrc.c
  7420. +++ b/drivers/clocksource/tcb_clksrc.c
  7421. @@ -23,8 +23,7 @@
  7422. * this 32 bit free-running counter. the second channel is not used.
  7423. *
  7424. * - The third channel may be used to provide a 16-bit clockevent
  7425. - * source, used in either periodic or oneshot mode. This runs
  7426. - * at 32 KiHZ, and can handle delays of up to two seconds.
  7427. + * source, used in either periodic or oneshot mode.
  7428. *
  7429. * A boot clocksource and clockevent source are also currently needed,
  7430. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  7431. @@ -74,6 +73,8 @@ static struct clocksource clksrc = {
  7432. struct tc_clkevt_device {
  7433. struct clock_event_device clkevt;
  7434. struct clk *clk;
  7435. + bool clk_enabled;
  7436. + u32 freq;
  7437. void __iomem *regs;
  7438. };
  7439. @@ -82,15 +83,26 @@ static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  7440. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  7441. }
  7442. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  7443. - * because using one of the divided clocks would usually mean the
  7444. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  7445. - *
  7446. - * A divided clock could be good for high resolution timers, since
  7447. - * 30.5 usec resolution can seem "low".
  7448. - */
  7449. static u32 timer_clock;
  7450. +static void tc_clk_disable(struct clock_event_device *d)
  7451. +{
  7452. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7453. +
  7454. + clk_disable(tcd->clk);
  7455. + tcd->clk_enabled = false;
  7456. +}
  7457. +
  7458. +static void tc_clk_enable(struct clock_event_device *d)
  7459. +{
  7460. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7461. +
  7462. + if (tcd->clk_enabled)
  7463. + return;
  7464. + clk_enable(tcd->clk);
  7465. + tcd->clk_enabled = true;
  7466. +}
  7467. +
  7468. static int tc_shutdown(struct clock_event_device *d)
  7469. {
  7470. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  7471. @@ -98,8 +110,14 @@ static int tc_shutdown(struct clock_event_device *d)
  7472. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  7473. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  7474. + return 0;
  7475. +}
  7476. +
  7477. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  7478. +{
  7479. + tc_shutdown(d);
  7480. if (!clockevent_state_detached(d))
  7481. - clk_disable(tcd->clk);
  7482. + tc_clk_disable(d);
  7483. return 0;
  7484. }
  7485. @@ -112,9 +130,9 @@ static int tc_set_oneshot(struct clock_event_device *d)
  7486. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  7487. tc_shutdown(d);
  7488. - clk_enable(tcd->clk);
  7489. + tc_clk_enable(d);
  7490. - /* slow clock, count up to RC, then irq and stop */
  7491. + /* count up to RC, then irq and stop */
  7492. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  7493. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  7494. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7495. @@ -134,12 +152,12 @@ static int tc_set_periodic(struct clock_event_device *d)
  7496. /* By not making the gentime core emulate periodic mode on top
  7497. * of oneshot, we get lower overhead and improved accuracy.
  7498. */
  7499. - clk_enable(tcd->clk);
  7500. + tc_clk_enable(d);
  7501. - /* slow clock, count up to RC, then irq and restart */
  7502. + /* count up to RC, then irq and restart */
  7503. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  7504. regs + ATMEL_TC_REG(2, CMR));
  7505. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7506. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  7507. /* Enable clock and interrupts on RC compare */
  7508. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  7509. @@ -166,9 +184,13 @@ static struct tc_clkevt_device clkevt = {
  7510. .features = CLOCK_EVT_FEAT_PERIODIC |
  7511. CLOCK_EVT_FEAT_ONESHOT,
  7512. /* Should be lower than at91rm9200's system timer */
  7513. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7514. .rating = 125,
  7515. +#else
  7516. + .rating = 200,
  7517. +#endif
  7518. .set_next_event = tc_next_event,
  7519. - .set_state_shutdown = tc_shutdown,
  7520. + .set_state_shutdown = tc_shutdown_clk_off,
  7521. .set_state_periodic = tc_set_periodic,
  7522. .set_state_oneshot = tc_set_oneshot,
  7523. },
  7524. @@ -188,8 +210,9 @@ static irqreturn_t ch2_irq(int irq, void *handle)
  7525. return IRQ_NONE;
  7526. }
  7527. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7528. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  7529. {
  7530. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  7531. int ret;
  7532. struct clk *t2_clk = tc->clk[2];
  7533. int irq = tc->irq[2];
  7534. @@ -210,7 +233,11 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7535. clkevt.regs = tc->regs;
  7536. clkevt.clk = t2_clk;
  7537. - timer_clock = clk32k_divisor_idx;
  7538. + timer_clock = divisor_idx;
  7539. + if (!divisor)
  7540. + clkevt.freq = 32768;
  7541. + else
  7542. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  7543. clkevt.clkevt.cpumask = cpumask_of(0);
  7544. @@ -221,7 +248,7 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  7545. return ret;
  7546. }
  7547. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  7548. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  7549. return ret;
  7550. }
  7551. @@ -358,7 +385,11 @@ static int __init tcb_clksrc_init(void)
  7552. goto err_disable_t1;
  7553. /* channel 2: periodic and oneshot timer support */
  7554. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  7555. ret = setup_clkevents(tc, clk32k_divisor_idx);
  7556. +#else
  7557. + ret = setup_clkevents(tc, best_divisor_idx);
  7558. +#endif
  7559. if (ret)
  7560. goto err_unregister_clksrc;
  7561. diff --git a/drivers/clocksource/timer-atmel-pit.c b/drivers/clocksource/timer-atmel-pit.c
  7562. index d911c5dca8f1..7a40f7e88468 100644
  7563. --- a/drivers/clocksource/timer-atmel-pit.c
  7564. +++ b/drivers/clocksource/timer-atmel-pit.c
  7565. @@ -46,6 +46,7 @@ struct pit_data {
  7566. u32 cycle;
  7567. u32 cnt;
  7568. unsigned int irq;
  7569. + bool irq_requested;
  7570. struct clk *mck;
  7571. };
  7572. @@ -96,15 +97,29 @@ static int pit_clkevt_shutdown(struct clock_event_device *dev)
  7573. /* disable irq, leaving the clocksource active */
  7574. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  7575. + if (data->irq_requested) {
  7576. + free_irq(data->irq, data);
  7577. + data->irq_requested = false;
  7578. + }
  7579. return 0;
  7580. }
  7581. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  7582. /*
  7583. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  7584. */
  7585. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  7586. {
  7587. struct pit_data *data = clkevt_to_pit_data(dev);
  7588. + int ret;
  7589. +
  7590. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7591. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7592. + "at91_tick", data);
  7593. + if (ret)
  7594. + panic(pr_fmt("Unable to setup IRQ\n"));
  7595. +
  7596. + data->irq_requested = true;
  7597. /* update clocksource counter */
  7598. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  7599. @@ -181,7 +196,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7600. {
  7601. unsigned long pit_rate;
  7602. unsigned bits;
  7603. - int ret;
  7604. /*
  7605. * Use our actual MCK to figure out how many MCK/16 ticks per
  7606. @@ -206,13 +220,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
  7607. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  7608. clocksource_register_hz(&data->clksrc, pit_rate);
  7609. - /* Set up irq handler */
  7610. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  7611. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7612. - "at91_tick", data);
  7613. - if (ret)
  7614. - panic(pr_fmt("Unable to setup IRQ\n"));
  7615. -
  7616. /* Set up and register clockevents */
  7617. data->clkevt.name = "pit";
  7618. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  7619. diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
  7620. index 29d21d68df5a..103d0fd70cc4 100644
  7621. --- a/drivers/clocksource/timer-atmel-st.c
  7622. +++ b/drivers/clocksource/timer-atmel-st.c
  7623. @@ -115,18 +115,29 @@ static void clkdev32k_disable_and_flush_irq(void)
  7624. last_crtr = read_CRTR();
  7625. }
  7626. +static int atmel_st_irq;
  7627. +
  7628. static int clkevt32k_shutdown(struct clock_event_device *evt)
  7629. {
  7630. clkdev32k_disable_and_flush_irq();
  7631. irqmask = 0;
  7632. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  7633. + free_irq(atmel_st_irq, regmap_st);
  7634. return 0;
  7635. }
  7636. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7637. {
  7638. + int ret;
  7639. +
  7640. clkdev32k_disable_and_flush_irq();
  7641. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7642. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7643. + "at91_tick", regmap_st);
  7644. + if (ret)
  7645. + panic(pr_fmt("Unable to setup IRQ\n"));
  7646. +
  7647. /*
  7648. * ALM for oneshot irqs, set by next_event()
  7649. * before 32 seconds have passed.
  7650. @@ -139,8 +150,16 @@ static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  7651. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  7652. {
  7653. + int ret;
  7654. +
  7655. clkdev32k_disable_and_flush_irq();
  7656. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  7657. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7658. + "at91_tick", regmap_st);
  7659. + if (ret)
  7660. + panic(pr_fmt("Unable to setup IRQ\n"));
  7661. +
  7662. /* PIT for periodic irqs; fixed rate of 1/HZ */
  7663. irqmask = AT91_ST_PITS;
  7664. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  7665. @@ -198,7 +217,7 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7666. {
  7667. struct clk *sclk;
  7668. unsigned int sclk_rate, val;
  7669. - int irq, ret;
  7670. + int ret;
  7671. regmap_st = syscon_node_to_regmap(node);
  7672. if (IS_ERR(regmap_st))
  7673. @@ -210,17 +229,10 @@ static void __init atmel_st_timer_init(struct device_node *node)
  7674. regmap_read(regmap_st, AT91_ST_SR, &val);
  7675. /* Get the interrupts property */
  7676. - irq = irq_of_parse_and_map(node, 0);
  7677. - if (!irq)
  7678. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  7679. + if (!atmel_st_irq)
  7680. panic(pr_fmt("Unable to get IRQ from DT\n"));
  7681. - /* Make IRQs happen for the system timer */
  7682. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  7683. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  7684. - "at91_tick", regmap_st);
  7685. - if (ret)
  7686. - panic(pr_fmt("Unable to setup IRQ\n"));
  7687. -
  7688. sclk = of_clk_get(node, 0);
  7689. if (IS_ERR(sclk))
  7690. panic(pr_fmt("Unable to get slow clock\n"));
  7691. diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
  7692. index c59bdcb83217..8f23161d80be 100644
  7693. --- a/drivers/cpufreq/Kconfig.x86
  7694. +++ b/drivers/cpufreq/Kconfig.x86
  7695. @@ -123,7 +123,7 @@ config X86_POWERNOW_K7_ACPI
  7696. config X86_POWERNOW_K8
  7697. tristate "AMD Opteron/Athlon64 PowerNow!"
  7698. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  7699. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  7700. help
  7701. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  7702. Support for K10 and newer processors is now in acpi-cpufreq.
  7703. diff --git a/drivers/cpuidle/coupled.c b/drivers/cpuidle/coupled.c
  7704. index 344058f8501a..d5657d50ac40 100644
  7705. --- a/drivers/cpuidle/coupled.c
  7706. +++ b/drivers/cpuidle/coupled.c
  7707. @@ -119,7 +119,6 @@ struct cpuidle_coupled {
  7708. #define CPUIDLE_COUPLED_NOT_IDLE (-1)
  7709. -static DEFINE_MUTEX(cpuidle_coupled_lock);
  7710. static DEFINE_PER_CPU(struct call_single_data, cpuidle_coupled_poke_cb);
  7711. /*
  7712. diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7713. index 6ed7d63a0688..9da7482ad256 100644
  7714. --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7715. +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  7716. @@ -1264,7 +1264,9 @@ i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  7717. if (ret)
  7718. return ret;
  7719. +#ifndef CONFIG_PREEMPT_RT_BASE
  7720. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  7721. +#endif
  7722. i915_gem_execbuffer_move_to_active(vmas, params->request);
  7723. i915_gem_execbuffer_retire_commands(params);
  7724. diff --git a/drivers/gpu/drm/i915/i915_gem_shrinker.c b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7725. index c0a96f1ee18e..deb1e207fa3c 100644
  7726. --- a/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7727. +++ b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  7728. @@ -39,7 +39,7 @@ static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  7729. if (!mutex_is_locked(mutex))
  7730. return false;
  7731. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  7732. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  7733. return mutex->owner == task;
  7734. #else
  7735. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  7736. diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
  7737. index 0f42a2782afc..80a1db09a379 100644
  7738. --- a/drivers/gpu/drm/i915/i915_irq.c
  7739. +++ b/drivers/gpu/drm/i915/i915_irq.c
  7740. @@ -812,6 +812,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7741. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7742. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  7743. + preempt_disable_rt();
  7744. /* Get optional system timestamp before query. */
  7745. if (stime)
  7746. @@ -863,6 +864,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7747. *etime = ktime_get();
  7748. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  7749. + preempt_enable_rt();
  7750. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7751. diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c
  7752. index 3292495ee10f..5c1ea4421df6 100644
  7753. --- a/drivers/gpu/drm/i915/intel_display.c
  7754. +++ b/drivers/gpu/drm/i915/intel_display.c
  7755. @@ -11398,7 +11398,7 @@ void intel_check_page_flip(struct drm_device *dev, int pipe)
  7756. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7757. struct intel_unpin_work *work;
  7758. - WARN_ON(!in_interrupt());
  7759. + WARN_ON_NONRT(!in_interrupt());
  7760. if (crtc == NULL)
  7761. return;
  7762. diff --git a/drivers/gpu/drm/i915/intel_sprite.c b/drivers/gpu/drm/i915/intel_sprite.c
  7763. index 56dc132e8e20..8771d6646e91 100644
  7764. --- a/drivers/gpu/drm/i915/intel_sprite.c
  7765. +++ b/drivers/gpu/drm/i915/intel_sprite.c
  7766. @@ -38,6 +38,7 @@
  7767. #include "intel_drv.h"
  7768. #include <drm/i915_drm.h>
  7769. #include "i915_drv.h"
  7770. +#include <linux/locallock.h>
  7771. static bool
  7772. format_is_yuv(uint32_t format)
  7773. @@ -64,6 +65,8 @@ static int usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  7774. 1000 * adjusted_mode->crtc_htotal);
  7775. }
  7776. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  7777. +
  7778. /**
  7779. * intel_pipe_update_start() - start update of a set of display registers
  7780. * @crtc: the crtc of which the registers are going to be updated
  7781. @@ -96,7 +99,7 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7782. min = vblank_start - usecs_to_scanlines(adjusted_mode, 100);
  7783. max = vblank_start - 1;
  7784. - local_irq_disable();
  7785. + local_lock_irq(pipe_update_lock);
  7786. if (min <= 0 || max <= 0)
  7787. return;
  7788. @@ -126,11 +129,11 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  7789. break;
  7790. }
  7791. - local_irq_enable();
  7792. + local_unlock_irq(pipe_update_lock);
  7793. timeout = schedule_timeout(timeout);
  7794. - local_irq_disable();
  7795. + local_lock_irq(pipe_update_lock);
  7796. }
  7797. finish_wait(wq, &wait);
  7798. @@ -164,7 +167,7 @@ void intel_pipe_update_end(struct intel_crtc *crtc)
  7799. trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
  7800. - local_irq_enable();
  7801. + local_unlock_irq(pipe_update_lock);
  7802. if (crtc->debug.start_vbl_count &&
  7803. crtc->debug.start_vbl_count != end_vbl_count) {
  7804. diff --git a/drivers/gpu/drm/radeon/radeon_display.c b/drivers/gpu/drm/radeon/radeon_display.c
  7805. index 3645b223aa37..642854b2ed2c 100644
  7806. --- a/drivers/gpu/drm/radeon/radeon_display.c
  7807. +++ b/drivers/gpu/drm/radeon/radeon_display.c
  7808. @@ -1862,6 +1862,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  7809. struct radeon_device *rdev = dev->dev_private;
  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. @@ -1954,6 +1955,7 @@ int radeon_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. /* Decode into vertical and horizontal scanout position. */
  7819. *vpos = position & 0x1fff;
  7820. diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c
  7821. index 08d26ba61ed3..46b89dd42b10 100644
  7822. --- a/drivers/i2c/busses/i2c-omap.c
  7823. +++ b/drivers/i2c/busses/i2c-omap.c
  7824. @@ -995,15 +995,12 @@ omap_i2c_isr(int irq, void *dev_id)
  7825. u16 mask;
  7826. u16 stat;
  7827. - spin_lock(&omap->lock);
  7828. - mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7829. stat = omap_i2c_read_reg(omap, OMAP_I2C_STAT_REG);
  7830. + mask = omap_i2c_read_reg(omap, OMAP_I2C_IE_REG);
  7831. if (stat & mask)
  7832. ret = IRQ_WAKE_THREAD;
  7833. - spin_unlock(&omap->lock);
  7834. -
  7835. return ret;
  7836. }
  7837. diff --git a/drivers/ide/alim15x3.c b/drivers/ide/alim15x3.c
  7838. index 36f76e28a0bf..394f142f90c7 100644
  7839. --- a/drivers/ide/alim15x3.c
  7840. +++ b/drivers/ide/alim15x3.c
  7841. @@ -234,7 +234,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  7842. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  7843. - local_irq_save(flags);
  7844. + local_irq_save_nort(flags);
  7845. if (m5229_revision < 0xC2) {
  7846. /*
  7847. @@ -325,7 +325,7 @@ out:
  7848. }
  7849. pci_dev_put(north);
  7850. pci_dev_put(isa_dev);
  7851. - local_irq_restore(flags);
  7852. + local_irq_restore_nort(flags);
  7853. return 0;
  7854. }
  7855. diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
  7856. index 696b6c1ec940..0d0a96629b73 100644
  7857. --- a/drivers/ide/hpt366.c
  7858. +++ b/drivers/ide/hpt366.c
  7859. @@ -1241,7 +1241,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7860. dma_old = inb(base + 2);
  7861. - local_irq_save(flags);
  7862. + local_irq_save_nort(flags);
  7863. dma_new = dma_old;
  7864. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  7865. @@ -1252,7 +1252,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  7866. if (dma_new != dma_old)
  7867. outb(dma_new, base + 2);
  7868. - local_irq_restore(flags);
  7869. + local_irq_restore_nort(flags);
  7870. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  7871. hwif->name, base, base + 7);
  7872. diff --git a/drivers/ide/ide-io-std.c b/drivers/ide/ide-io-std.c
  7873. index 19763977568c..4169433faab5 100644
  7874. --- a/drivers/ide/ide-io-std.c
  7875. +++ b/drivers/ide/ide-io-std.c
  7876. @@ -175,7 +175,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7877. unsigned long uninitialized_var(flags);
  7878. if ((io_32bit & 2) && !mmio) {
  7879. - local_irq_save(flags);
  7880. + local_irq_save_nort(flags);
  7881. ata_vlb_sync(io_ports->nsect_addr);
  7882. }
  7883. @@ -186,7 +186,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7884. insl(data_addr, buf, words);
  7885. if ((io_32bit & 2) && !mmio)
  7886. - local_irq_restore(flags);
  7887. + local_irq_restore_nort(flags);
  7888. if (((len + 1) & 3) < 2)
  7889. return;
  7890. @@ -219,7 +219,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7891. unsigned long uninitialized_var(flags);
  7892. if ((io_32bit & 2) && !mmio) {
  7893. - local_irq_save(flags);
  7894. + local_irq_save_nort(flags);
  7895. ata_vlb_sync(io_ports->nsect_addr);
  7896. }
  7897. @@ -230,7 +230,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  7898. outsl(data_addr, buf, words);
  7899. if ((io_32bit & 2) && !mmio)
  7900. - local_irq_restore(flags);
  7901. + local_irq_restore_nort(flags);
  7902. if (((len + 1) & 3) < 2)
  7903. return;
  7904. diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
  7905. index 669ea1e45795..e12e43e62245 100644
  7906. --- a/drivers/ide/ide-io.c
  7907. +++ b/drivers/ide/ide-io.c
  7908. @@ -659,7 +659,7 @@ void ide_timer_expiry (unsigned long data)
  7909. /* disable_irq_nosync ?? */
  7910. disable_irq(hwif->irq);
  7911. /* local CPU only, as if we were handling an interrupt */
  7912. - local_irq_disable();
  7913. + local_irq_disable_nort();
  7914. if (hwif->polling) {
  7915. startstop = handler(drive);
  7916. } else if (drive_is_ready(drive)) {
  7917. diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
  7918. index 376f2dc410c5..f014dd1b73dc 100644
  7919. --- a/drivers/ide/ide-iops.c
  7920. +++ b/drivers/ide/ide-iops.c
  7921. @@ -129,12 +129,12 @@ int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  7922. if ((stat & ATA_BUSY) == 0)
  7923. break;
  7924. - local_irq_restore(flags);
  7925. + local_irq_restore_nort(flags);
  7926. *rstat = stat;
  7927. return -EBUSY;
  7928. }
  7929. }
  7930. - local_irq_restore(flags);
  7931. + local_irq_restore_nort(flags);
  7932. }
  7933. /*
  7934. * Allow status to settle, then read it again.
  7935. diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
  7936. index 0b63facd1d87..4ceba37afc0c 100644
  7937. --- a/drivers/ide/ide-probe.c
  7938. +++ b/drivers/ide/ide-probe.c
  7939. @@ -196,10 +196,10 @@ static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  7940. int bswap = 1;
  7941. /* local CPU only; some systems need this */
  7942. - local_irq_save(flags);
  7943. + local_irq_save_nort(flags);
  7944. /* read 512 bytes of id info */
  7945. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  7946. - local_irq_restore(flags);
  7947. + local_irq_restore_nort(flags);
  7948. drive->dev_flags |= IDE_DFLAG_ID_READ;
  7949. #ifdef DEBUG
  7950. diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
  7951. index a716693417a3..be0568c722d6 100644
  7952. --- a/drivers/ide/ide-taskfile.c
  7953. +++ b/drivers/ide/ide-taskfile.c
  7954. @@ -250,7 +250,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  7955. page_is_high = PageHighMem(page);
  7956. if (page_is_high)
  7957. - local_irq_save(flags);
  7958. + local_irq_save_nort(flags);
  7959. buf = kmap_atomic(page) + offset;
  7960. @@ -271,7 +271,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  7961. kunmap_atomic(buf);
  7962. if (page_is_high)
  7963. - local_irq_restore(flags);
  7964. + local_irq_restore_nort(flags);
  7965. len -= nr_bytes;
  7966. }
  7967. @@ -414,7 +414,7 @@ static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
  7968. }
  7969. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  7970. - local_irq_disable();
  7971. + local_irq_disable_nort();
  7972. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  7973. diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  7974. index 87799de90a1d..66cdd37f8605 100644
  7975. --- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  7976. +++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  7977. @@ -857,7 +857,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  7978. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  7979. - local_irq_save(flags);
  7980. + local_irq_save_nort(flags);
  7981. netif_addr_lock(dev);
  7982. spin_lock(&priv->lock);
  7983. @@ -939,7 +939,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  7984. spin_unlock(&priv->lock);
  7985. netif_addr_unlock(dev);
  7986. - local_irq_restore(flags);
  7987. + local_irq_restore_nort(flags);
  7988. /*
  7989. * make sure the in-flight joins have finished before we attempt
  7990. diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
  7991. index 4a2a9e370be7..e970d9afd179 100644
  7992. --- a/drivers/input/gameport/gameport.c
  7993. +++ b/drivers/input/gameport/gameport.c
  7994. @@ -91,13 +91,13 @@ static int gameport_measure_speed(struct gameport *gameport)
  7995. tx = ~0;
  7996. for (i = 0; i < 50; i++) {
  7997. - local_irq_save(flags);
  7998. + local_irq_save_nort(flags);
  7999. t1 = ktime_get_ns();
  8000. for (t = 0; t < 50; t++)
  8001. gameport_read(gameport);
  8002. t2 = ktime_get_ns();
  8003. t3 = ktime_get_ns();
  8004. - local_irq_restore(flags);
  8005. + local_irq_restore_nort(flags);
  8006. udelay(i * 10);
  8007. t = (t2 - t1) - (t3 - t2);
  8008. if (t < tx)
  8009. @@ -124,12 +124,12 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8010. tx = 1 << 30;
  8011. for(i = 0; i < 50; i++) {
  8012. - local_irq_save(flags);
  8013. + local_irq_save_nort(flags);
  8014. GET_TIME(t1);
  8015. for (t = 0; t < 50; t++) gameport_read(gameport);
  8016. GET_TIME(t2);
  8017. GET_TIME(t3);
  8018. - local_irq_restore(flags);
  8019. + local_irq_restore_nort(flags);
  8020. udelay(i * 10);
  8021. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  8022. }
  8023. @@ -148,11 +148,11 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  8024. tx = 1 << 30;
  8025. for(i = 0; i < 50; i++) {
  8026. - local_irq_save(flags);
  8027. + local_irq_save_nort(flags);
  8028. t1 = rdtsc();
  8029. for (t = 0; t < 50; t++) gameport_read(gameport);
  8030. t2 = rdtsc();
  8031. - local_irq_restore(flags);
  8032. + local_irq_restore_nort(flags);
  8033. udelay(i * 10);
  8034. if (t2 - t1 < tx) tx = t2 - t1;
  8035. }
  8036. diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
  8037. index 0397985a2601..bc0e7d2c5cff 100644
  8038. --- a/drivers/iommu/amd_iommu.c
  8039. +++ b/drivers/iommu/amd_iommu.c
  8040. @@ -2019,10 +2019,10 @@ static int __attach_device(struct iommu_dev_data *dev_data,
  8041. int ret;
  8042. /*
  8043. - * Must be called with IRQs disabled. Warn here to detect early
  8044. - * when its not.
  8045. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8046. + * detect early when its not.
  8047. */
  8048. - WARN_ON(!irqs_disabled());
  8049. + WARN_ON_NONRT(!irqs_disabled());
  8050. /* lock domain */
  8051. spin_lock(&domain->lock);
  8052. @@ -2185,10 +2185,10 @@ static void __detach_device(struct iommu_dev_data *dev_data)
  8053. struct protection_domain *domain;
  8054. /*
  8055. - * Must be called with IRQs disabled. Warn here to detect early
  8056. - * when its not.
  8057. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  8058. + * detect early when its not.
  8059. */
  8060. - WARN_ON(!irqs_disabled());
  8061. + WARN_ON_NONRT(!irqs_disabled());
  8062. if (WARN_ON(!dev_data->domain))
  8063. return;
  8064. diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
  8065. index 5bda6a9b56bb..d6286584c807 100644
  8066. --- a/drivers/leds/trigger/Kconfig
  8067. +++ b/drivers/leds/trigger/Kconfig
  8068. @@ -61,7 +61,7 @@ config LEDS_TRIGGER_BACKLIGHT
  8069. config LEDS_TRIGGER_CPU
  8070. bool "LED CPU Trigger"
  8071. - depends on LEDS_TRIGGERS
  8072. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  8073. help
  8074. This allows LEDs to be controlled by active CPUs. This shows
  8075. the active CPUs across an array of LEDs so you can see which
  8076. diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
  8077. index 4d200883c505..98b64ed5cb81 100644
  8078. --- a/drivers/md/bcache/Kconfig
  8079. +++ b/drivers/md/bcache/Kconfig
  8080. @@ -1,6 +1,7 @@
  8081. config BCACHE
  8082. tristate "Block device as cache"
  8083. + depends on !PREEMPT_RT_FULL
  8084. ---help---
  8085. Allows a block device to be used as cache for other devices; uses
  8086. a btree for indexing and the layout is optimized for SSDs.
  8087. diff --git a/drivers/md/dm.c b/drivers/md/dm.c
  8088. index a42729ebf272..c717ec464459 100644
  8089. --- a/drivers/md/dm.c
  8090. +++ b/drivers/md/dm.c
  8091. @@ -2127,7 +2127,7 @@ static void dm_request_fn(struct request_queue *q)
  8092. /* Establish tio->ti before queuing work (map_tio_request) */
  8093. tio->ti = ti;
  8094. queue_kthread_work(&md->kworker, &tio->work);
  8095. - BUG_ON(!irqs_disabled());
  8096. + BUG_ON_NONRT(!irqs_disabled());
  8097. }
  8098. goto out;
  8099. diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
  8100. index 10ce885445f6..76f71791361c 100644
  8101. --- a/drivers/md/raid5.c
  8102. +++ b/drivers/md/raid5.c
  8103. @@ -1920,8 +1920,9 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8104. struct raid5_percpu *percpu;
  8105. unsigned long cpu;
  8106. - cpu = get_cpu();
  8107. + cpu = get_cpu_light();
  8108. percpu = per_cpu_ptr(conf->percpu, cpu);
  8109. + spin_lock(&percpu->lock);
  8110. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  8111. ops_run_biofill(sh);
  8112. overlap_clear++;
  8113. @@ -1977,7 +1978,8 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  8114. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  8115. wake_up(&sh->raid_conf->wait_for_overlap);
  8116. }
  8117. - put_cpu();
  8118. + spin_unlock(&percpu->lock);
  8119. + put_cpu_light();
  8120. }
  8121. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
  8122. @@ -6414,6 +6416,7 @@ static int raid5_alloc_percpu(struct r5conf *conf)
  8123. __func__, cpu);
  8124. break;
  8125. }
  8126. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  8127. }
  8128. put_online_cpus();
  8129. diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h
  8130. index 517d4b68a1be..efe91887ecd7 100644
  8131. --- a/drivers/md/raid5.h
  8132. +++ b/drivers/md/raid5.h
  8133. @@ -504,6 +504,7 @@ struct r5conf {
  8134. int recovery_disabled;
  8135. /* per cpu variables */
  8136. struct raid5_percpu {
  8137. + spinlock_t lock; /* Protection for -RT */
  8138. struct page *spare_page; /* Used when checking P/Q in raid6 */
  8139. struct flex_array *scribble; /* space for constructing buffer
  8140. * lists and performing address
  8141. diff --git a/drivers/media/platform/vsp1/vsp1_video.c b/drivers/media/platform/vsp1/vsp1_video.c
  8142. index 5ce88e1f5d71..b4f8cd74ecb8 100644
  8143. --- a/drivers/media/platform/vsp1/vsp1_video.c
  8144. +++ b/drivers/media/platform/vsp1/vsp1_video.c
  8145. @@ -520,7 +520,7 @@ static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe)
  8146. bool stopped;
  8147. spin_lock_irqsave(&pipe->irqlock, flags);
  8148. - stopped = pipe->state == VSP1_PIPELINE_STOPPED,
  8149. + stopped = pipe->state == VSP1_PIPELINE_STOPPED;
  8150. spin_unlock_irqrestore(&pipe->irqlock, flags);
  8151. return stopped;
  8152. diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
  8153. index 4bf7d50b1bc7..6f7e99ad6e29 100644
  8154. --- a/drivers/misc/Kconfig
  8155. +++ b/drivers/misc/Kconfig
  8156. @@ -54,6 +54,7 @@ config AD525X_DPOT_SPI
  8157. config ATMEL_TCLIB
  8158. bool "Atmel AT32/AT91 Timer/Counter Library"
  8159. depends on (AVR32 || ARCH_AT91)
  8160. + default y if PREEMPT_RT_FULL
  8161. help
  8162. Select this if you want a library to allocate the Timer/Counter
  8163. blocks found on many Atmel processors. This facilitates using
  8164. @@ -69,8 +70,7 @@ config ATMEL_TCB_CLKSRC
  8165. are combined to make a single 32-bit timer.
  8166. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  8167. - may be used as a clock event device supporting oneshot mode
  8168. - (delays of up to two seconds) based on the 32 KiHz clock.
  8169. + may be used as a clock event device supporting oneshot mode.
  8170. config ATMEL_TCB_CLKSRC_BLOCK
  8171. int
  8172. @@ -84,6 +84,15 @@ config ATMEL_TCB_CLKSRC_BLOCK
  8173. TC can be used for other purposes, such as PWM generation and
  8174. interval timing.
  8175. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  8176. + bool "TC Block use 32 KiHz clock"
  8177. + depends on ATMEL_TCB_CLKSRC
  8178. + default y if !PREEMPT_RT_FULL
  8179. + help
  8180. + Select this to use 32 KiHz base clock rate as TC block clock
  8181. + source for clock events.
  8182. +
  8183. +
  8184. config DUMMY_IRQ
  8185. tristate "Dummy IRQ handler"
  8186. default n
  8187. @@ -113,6 +122,35 @@ config IBM_ASM
  8188. for information on the specific driver level and support statement
  8189. for your IBM server.
  8190. +config HWLAT_DETECTOR
  8191. + tristate "Testing module to detect hardware-induced latencies"
  8192. + depends on DEBUG_FS
  8193. + depends on RING_BUFFER
  8194. + default m
  8195. + ---help---
  8196. + A simple hardware latency detector. Use this module to detect
  8197. + large latencies introduced by the behavior of the underlying
  8198. + system firmware external to Linux. We do this using periodic
  8199. + use of stop_machine to grab all available CPUs and measure
  8200. + for unexplainable gaps in the CPU timestamp counter(s). By
  8201. + default, the module is not enabled until the "enable" file
  8202. + within the "hwlat_detector" debugfs directory is toggled.
  8203. +
  8204. + This module is often used to detect SMI (System Management
  8205. + Interrupts) on x86 systems, though is not x86 specific. To
  8206. + this end, we default to using a sample window of 1 second,
  8207. + during which we will sample for 0.5 seconds. If an SMI or
  8208. + similar event occurs during that time, it is recorded
  8209. + into an 8K samples global ring buffer until retreived.
  8210. +
  8211. + WARNING: This software should never be enabled (it can be built
  8212. + but should not be turned on after it is loaded) in a production
  8213. + environment where high latencies are a concern since the
  8214. + sampling mechanism actually introduces latencies for
  8215. + regular tasks while the CPU(s) are being held.
  8216. +
  8217. + If unsure, say N
  8218. +
  8219. config PHANTOM
  8220. tristate "Sensable PHANToM (PCI)"
  8221. depends on PCI
  8222. diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
  8223. index 537d7f3b78da..ec4aecba0656 100644
  8224. --- a/drivers/misc/Makefile
  8225. +++ b/drivers/misc/Makefile
  8226. @@ -39,6 +39,7 @@ obj-$(CONFIG_C2PORT) += c2port/
  8227. obj-$(CONFIG_HMC6352) += hmc6352.o
  8228. obj-y += eeprom/
  8229. obj-y += cb710/
  8230. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  8231. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  8232. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  8233. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  8234. diff --git a/drivers/misc/hwlat_detector.c b/drivers/misc/hwlat_detector.c
  8235. new file mode 100644
  8236. index 000000000000..52f5ad5fd9c0
  8237. --- /dev/null
  8238. +++ b/drivers/misc/hwlat_detector.c
  8239. @@ -0,0 +1,1240 @@
  8240. +/*
  8241. + * hwlat_detector.c - A simple Hardware Latency detector.
  8242. + *
  8243. + * Use this module to detect large system latencies induced by the behavior of
  8244. + * certain underlying system hardware or firmware, independent of Linux itself.
  8245. + * The code was developed originally to detect the presence of SMIs on Intel
  8246. + * and AMD systems, although there is no dependency upon x86 herein.
  8247. + *
  8248. + * The classical example usage of this module is in detecting the presence of
  8249. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  8250. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  8251. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  8252. + * LPC (or other device) to generate a special interrupt under certain
  8253. + * circumstances, for example, upon expiration of a special SMI timer device,
  8254. + * due to certain external thermal readings, on certain I/O address accesses,
  8255. + * and other situations. An SMI hits a special CPU pin, triggers a special
  8256. + * SMI mode (complete with special memory map), and the OS is unaware.
  8257. + *
  8258. + * Although certain hardware-inducing latencies are necessary (for example,
  8259. + * a modern system often requires an SMI handler for correct thermal control
  8260. + * and remote management) they can wreak havoc upon any OS-level performance
  8261. + * guarantees toward low-latency, especially when the OS is not even made
  8262. + * aware of the presence of these interrupts. For this reason, we need a
  8263. + * somewhat brute force mechanism to detect these interrupts. In this case,
  8264. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  8265. + * sampling the built-in CPU timer, looking for discontiguous readings.
  8266. + *
  8267. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  8268. + * you should NEVER use this module in a production environment
  8269. + * requiring any kind of low-latency performance guarantee(s).
  8270. + *
  8271. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  8272. + *
  8273. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  8274. + *
  8275. + * This file is licensed under the terms of the GNU General Public
  8276. + * License version 2. This program is licensed "as is" without any
  8277. + * warranty of any kind, whether express or implied.
  8278. + */
  8279. +
  8280. +#include <linux/module.h>
  8281. +#include <linux/init.h>
  8282. +#include <linux/ring_buffer.h>
  8283. +#include <linux/time.h>
  8284. +#include <linux/hrtimer.h>
  8285. +#include <linux/kthread.h>
  8286. +#include <linux/debugfs.h>
  8287. +#include <linux/seq_file.h>
  8288. +#include <linux/uaccess.h>
  8289. +#include <linux/version.h>
  8290. +#include <linux/delay.h>
  8291. +#include <linux/slab.h>
  8292. +#include <linux/trace_clock.h>
  8293. +
  8294. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  8295. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  8296. +#define U64STR_SIZE 22 /* 20 digits max */
  8297. +
  8298. +#define VERSION "1.0.0"
  8299. +#define BANNER "hwlat_detector: "
  8300. +#define DRVNAME "hwlat_detector"
  8301. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  8302. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  8303. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  8304. +
  8305. +/* Module metadata */
  8306. +
  8307. +MODULE_LICENSE("GPL");
  8308. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  8309. +MODULE_DESCRIPTION("A simple hardware latency detector");
  8310. +MODULE_VERSION(VERSION);
  8311. +
  8312. +/* Module parameters */
  8313. +
  8314. +static int debug;
  8315. +static int enabled;
  8316. +static int threshold;
  8317. +
  8318. +module_param(debug, int, 0); /* enable debug */
  8319. +module_param(enabled, int, 0); /* enable detector */
  8320. +module_param(threshold, int, 0); /* latency threshold */
  8321. +
  8322. +/* Buffering and sampling */
  8323. +
  8324. +static struct ring_buffer *ring_buffer; /* sample buffer */
  8325. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  8326. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  8327. +static struct task_struct *kthread; /* sampling thread */
  8328. +
  8329. +/* DebugFS filesystem entries */
  8330. +
  8331. +static struct dentry *debug_dir; /* debugfs directory */
  8332. +static struct dentry *debug_max; /* maximum TSC delta */
  8333. +static struct dentry *debug_count; /* total detect count */
  8334. +static struct dentry *debug_sample_width; /* sample width us */
  8335. +static struct dentry *debug_sample_window; /* sample window us */
  8336. +static struct dentry *debug_sample; /* raw samples us */
  8337. +static struct dentry *debug_threshold; /* threshold us */
  8338. +static struct dentry *debug_enable; /* enable/disable */
  8339. +
  8340. +/* Individual samples and global state */
  8341. +
  8342. +struct sample; /* latency sample */
  8343. +struct data; /* Global state */
  8344. +
  8345. +/* Sampling functions */
  8346. +static int __buffer_add_sample(struct sample *sample);
  8347. +static struct sample *buffer_get_sample(struct sample *sample);
  8348. +
  8349. +/* Threading and state */
  8350. +static int kthread_fn(void *unused);
  8351. +static int start_kthread(void);
  8352. +static int stop_kthread(void);
  8353. +static void __reset_stats(void);
  8354. +static int init_stats(void);
  8355. +
  8356. +/* Debugfs interface */
  8357. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8358. + size_t cnt, loff_t *ppos, const u64 *entry);
  8359. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8360. + size_t cnt, loff_t *ppos, u64 *entry);
  8361. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  8362. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8363. + size_t cnt, loff_t *ppos);
  8364. +static int debug_sample_release(struct inode *inode, struct file *filp);
  8365. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  8366. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8367. + size_t cnt, loff_t *ppos);
  8368. +static ssize_t debug_enable_fwrite(struct file *file,
  8369. + const char __user *user_buffer,
  8370. + size_t user_size, loff_t *offset);
  8371. +
  8372. +/* Initialization functions */
  8373. +static int init_debugfs(void);
  8374. +static void free_debugfs(void);
  8375. +static int detector_init(void);
  8376. +static void detector_exit(void);
  8377. +
  8378. +/* Individual latency samples are stored here when detected and packed into
  8379. + * the ring_buffer circular buffer, where they are overwritten when
  8380. + * more than buf_size/sizeof(sample) samples are received. */
  8381. +struct sample {
  8382. + u64 seqnum; /* unique sequence */
  8383. + u64 duration; /* ktime delta */
  8384. + u64 outer_duration; /* ktime delta (outer loop) */
  8385. + struct timespec timestamp; /* wall time */
  8386. + unsigned long lost;
  8387. +};
  8388. +
  8389. +/* keep the global state somewhere. */
  8390. +static struct data {
  8391. +
  8392. + struct mutex lock; /* protect changes */
  8393. +
  8394. + u64 count; /* total since reset */
  8395. + u64 max_sample; /* max hardware latency */
  8396. + u64 threshold; /* sample threshold level */
  8397. +
  8398. + u64 sample_window; /* total sampling window (on+off) */
  8399. + u64 sample_width; /* active sampling portion of window */
  8400. +
  8401. + atomic_t sample_open; /* whether the sample file is open */
  8402. +
  8403. + wait_queue_head_t wq; /* waitqeue for new sample values */
  8404. +
  8405. +} data;
  8406. +
  8407. +/**
  8408. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  8409. + * @sample: The new latency sample value
  8410. + *
  8411. + * This receives a new latency sample and records it in a global ring buffer.
  8412. + * No additional locking is used in this case.
  8413. + */
  8414. +static int __buffer_add_sample(struct sample *sample)
  8415. +{
  8416. + return ring_buffer_write(ring_buffer,
  8417. + sizeof(struct sample), sample);
  8418. +}
  8419. +
  8420. +/**
  8421. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  8422. + * @sample: Pre-allocated storage for the sample
  8423. + *
  8424. + * This retrieves a hardware latency sample from the global circular buffer
  8425. + */
  8426. +static struct sample *buffer_get_sample(struct sample *sample)
  8427. +{
  8428. + struct ring_buffer_event *e = NULL;
  8429. + struct sample *s = NULL;
  8430. + unsigned int cpu = 0;
  8431. +
  8432. + if (!sample)
  8433. + return NULL;
  8434. +
  8435. + mutex_lock(&ring_buffer_mutex);
  8436. + for_each_online_cpu(cpu) {
  8437. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  8438. + if (e)
  8439. + break;
  8440. + }
  8441. +
  8442. + if (e) {
  8443. + s = ring_buffer_event_data(e);
  8444. + memcpy(sample, s, sizeof(struct sample));
  8445. + } else
  8446. + sample = NULL;
  8447. + mutex_unlock(&ring_buffer_mutex);
  8448. +
  8449. + return sample;
  8450. +}
  8451. +
  8452. +#ifndef CONFIG_TRACING
  8453. +#define time_type ktime_t
  8454. +#define time_get() ktime_get()
  8455. +#define time_to_us(x) ktime_to_us(x)
  8456. +#define time_sub(a, b) ktime_sub(a, b)
  8457. +#define init_time(a, b) (a).tv64 = b
  8458. +#define time_u64(a) ((a).tv64)
  8459. +#else
  8460. +#define time_type u64
  8461. +#define time_get() trace_clock_local()
  8462. +#define time_to_us(x) div_u64(x, 1000)
  8463. +#define time_sub(a, b) ((a) - (b))
  8464. +#define init_time(a, b) (a = b)
  8465. +#define time_u64(a) a
  8466. +#endif
  8467. +/**
  8468. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  8469. + *
  8470. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  8471. + * hardware-induced latency. Called with interrupts disabled and with
  8472. + * data.lock held.
  8473. + */
  8474. +static int get_sample(void)
  8475. +{
  8476. + time_type start, t1, t2, last_t2;
  8477. + s64 diff, total = 0;
  8478. + u64 sample = 0;
  8479. + u64 outer_sample = 0;
  8480. + int ret = -1;
  8481. +
  8482. + init_time(last_t2, 0);
  8483. + start = time_get(); /* start timestamp */
  8484. +
  8485. + do {
  8486. +
  8487. + t1 = time_get(); /* we'll look for a discontinuity */
  8488. + t2 = time_get();
  8489. +
  8490. + if (time_u64(last_t2)) {
  8491. + /* Check the delta from outer loop (t2 to next t1) */
  8492. + diff = time_to_us(time_sub(t1, last_t2));
  8493. + /* This shouldn't happen */
  8494. + if (diff < 0) {
  8495. + pr_err(BANNER "time running backwards\n");
  8496. + goto out;
  8497. + }
  8498. + if (diff > outer_sample)
  8499. + outer_sample = diff;
  8500. + }
  8501. + last_t2 = t2;
  8502. +
  8503. + total = time_to_us(time_sub(t2, start)); /* sample width */
  8504. +
  8505. + /* This checks the inner loop (t1 to t2) */
  8506. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  8507. +
  8508. + /* This shouldn't happen */
  8509. + if (diff < 0) {
  8510. + pr_err(BANNER "time running backwards\n");
  8511. + goto out;
  8512. + }
  8513. +
  8514. + if (diff > sample)
  8515. + sample = diff; /* only want highest value */
  8516. +
  8517. + } while (total <= data.sample_width);
  8518. +
  8519. + ret = 0;
  8520. +
  8521. + /* If we exceed the threshold value, we have found a hardware latency */
  8522. + if (sample > data.threshold || outer_sample > data.threshold) {
  8523. + struct sample s;
  8524. +
  8525. + ret = 1;
  8526. +
  8527. + data.count++;
  8528. + s.seqnum = data.count;
  8529. + s.duration = sample;
  8530. + s.outer_duration = outer_sample;
  8531. + s.timestamp = CURRENT_TIME;
  8532. + __buffer_add_sample(&s);
  8533. +
  8534. + /* Keep a running maximum ever recorded hardware latency */
  8535. + if (sample > data.max_sample)
  8536. + data.max_sample = sample;
  8537. + }
  8538. +
  8539. +out:
  8540. + return ret;
  8541. +}
  8542. +
  8543. +/*
  8544. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  8545. + * @unused: A required part of the kthread API.
  8546. + *
  8547. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  8548. + * disable interrupts, which does (intentionally) introduce latency since we
  8549. + * need to ensure nothing else might be running (and thus pre-empting).
  8550. + * Obviously this should never be used in production environments.
  8551. + *
  8552. + * Currently this runs on which ever CPU it was scheduled on, but most
  8553. + * real-worald hardware latency situations occur across several CPUs,
  8554. + * but we might later generalize this if we find there are any actualy
  8555. + * systems with alternate SMI delivery or other hardware latencies.
  8556. + */
  8557. +static int kthread_fn(void *unused)
  8558. +{
  8559. + int ret;
  8560. + u64 interval;
  8561. +
  8562. + while (!kthread_should_stop()) {
  8563. +
  8564. + mutex_lock(&data.lock);
  8565. +
  8566. + local_irq_disable();
  8567. + ret = get_sample();
  8568. + local_irq_enable();
  8569. +
  8570. + if (ret > 0)
  8571. + wake_up(&data.wq); /* wake up reader(s) */
  8572. +
  8573. + interval = data.sample_window - data.sample_width;
  8574. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  8575. +
  8576. + mutex_unlock(&data.lock);
  8577. +
  8578. + if (msleep_interruptible(interval))
  8579. + break;
  8580. + }
  8581. +
  8582. + return 0;
  8583. +}
  8584. +
  8585. +/**
  8586. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  8587. + *
  8588. + * This starts a kernel thread that will sit and sample the CPU timestamp
  8589. + * counter (TSC or similar) and look for potential hardware latencies.
  8590. + */
  8591. +static int start_kthread(void)
  8592. +{
  8593. + kthread = kthread_run(kthread_fn, NULL,
  8594. + DRVNAME);
  8595. + if (IS_ERR(kthread)) {
  8596. + pr_err(BANNER "could not start sampling thread\n");
  8597. + enabled = 0;
  8598. + return -ENOMEM;
  8599. + }
  8600. +
  8601. + return 0;
  8602. +}
  8603. +
  8604. +/**
  8605. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  8606. + *
  8607. + * This kicks the running hardware latency sampling/detector kernel thread and
  8608. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  8609. + */
  8610. +static int stop_kthread(void)
  8611. +{
  8612. + int ret;
  8613. +
  8614. + ret = kthread_stop(kthread);
  8615. +
  8616. + return ret;
  8617. +}
  8618. +
  8619. +/**
  8620. + * __reset_stats - Reset statistics for the hardware latency detector
  8621. + *
  8622. + * We use data to store various statistics and global state. We call this
  8623. + * function in order to reset those when "enable" is toggled on or off, and
  8624. + * also at initialization. Should be called with data.lock held.
  8625. + */
  8626. +static void __reset_stats(void)
  8627. +{
  8628. + data.count = 0;
  8629. + data.max_sample = 0;
  8630. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  8631. +}
  8632. +
  8633. +/**
  8634. + * init_stats - Setup global state statistics for the hardware latency detector
  8635. + *
  8636. + * We use data to store various statistics and global state. We also use
  8637. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  8638. + * induced system latencies. This function initializes these structures and
  8639. + * allocates the global ring buffer also.
  8640. + */
  8641. +static int init_stats(void)
  8642. +{
  8643. + int ret = -ENOMEM;
  8644. +
  8645. + mutex_init(&data.lock);
  8646. + init_waitqueue_head(&data.wq);
  8647. + atomic_set(&data.sample_open, 0);
  8648. +
  8649. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  8650. +
  8651. + if (WARN(!ring_buffer, KERN_ERR BANNER
  8652. + "failed to allocate ring buffer!\n"))
  8653. + goto out;
  8654. +
  8655. + __reset_stats();
  8656. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  8657. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  8658. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  8659. +
  8660. + ret = 0;
  8661. +
  8662. +out:
  8663. + return ret;
  8664. +
  8665. +}
  8666. +
  8667. +/*
  8668. + * simple_data_read - Wrapper read function for global state debugfs entries
  8669. + * @filp: The active open file structure for the debugfs "file"
  8670. + * @ubuf: The userspace provided buffer to read value into
  8671. + * @cnt: The maximum number of bytes to read
  8672. + * @ppos: The current "file" position
  8673. + * @entry: The entry to read from
  8674. + *
  8675. + * This function provides a generic read implementation for the global state
  8676. + * "data" structure debugfs filesystem entries. It would be nice to use
  8677. + * simple_attr_read directly, but we need to make sure that the data.lock
  8678. + * is held during the actual read.
  8679. + */
  8680. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  8681. + size_t cnt, loff_t *ppos, const u64 *entry)
  8682. +{
  8683. + char buf[U64STR_SIZE];
  8684. + u64 val = 0;
  8685. + int len = 0;
  8686. +
  8687. + memset(buf, 0, sizeof(buf));
  8688. +
  8689. + if (!entry)
  8690. + return -EFAULT;
  8691. +
  8692. + mutex_lock(&data.lock);
  8693. + val = *entry;
  8694. + mutex_unlock(&data.lock);
  8695. +
  8696. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  8697. +
  8698. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  8699. +
  8700. +}
  8701. +
  8702. +/*
  8703. + * simple_data_write - Wrapper write function for global state debugfs entries
  8704. + * @filp: The active open file structure for the debugfs "file"
  8705. + * @ubuf: The userspace provided buffer to write value from
  8706. + * @cnt: The maximum number of bytes to write
  8707. + * @ppos: The current "file" position
  8708. + * @entry: The entry to write to
  8709. + *
  8710. + * This function provides a generic write implementation for the global state
  8711. + * "data" structure debugfs filesystem entries. It would be nice to use
  8712. + * simple_attr_write directly, but we need to make sure that the data.lock
  8713. + * is held during the actual write.
  8714. + */
  8715. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  8716. + size_t cnt, loff_t *ppos, u64 *entry)
  8717. +{
  8718. + char buf[U64STR_SIZE];
  8719. + int csize = min(cnt, sizeof(buf));
  8720. + u64 val = 0;
  8721. + int err = 0;
  8722. +
  8723. + memset(buf, '\0', sizeof(buf));
  8724. + if (copy_from_user(buf, ubuf, csize))
  8725. + return -EFAULT;
  8726. +
  8727. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  8728. + err = kstrtoull(buf, 10, &val);
  8729. + if (err)
  8730. + return -EINVAL;
  8731. +
  8732. + mutex_lock(&data.lock);
  8733. + *entry = val;
  8734. + mutex_unlock(&data.lock);
  8735. +
  8736. + return csize;
  8737. +}
  8738. +
  8739. +/**
  8740. + * debug_count_fopen - Open function for "count" debugfs entry
  8741. + * @inode: The in-kernel inode representation of the debugfs "file"
  8742. + * @filp: The active open file structure for the debugfs "file"
  8743. + *
  8744. + * This function provides an open implementation for the "count" debugfs
  8745. + * interface to the hardware latency detector.
  8746. + */
  8747. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  8748. +{
  8749. + return 0;
  8750. +}
  8751. +
  8752. +/**
  8753. + * debug_count_fread - Read function for "count" debugfs entry
  8754. + * @filp: The active open file structure for the debugfs "file"
  8755. + * @ubuf: The userspace provided buffer to read value into
  8756. + * @cnt: The maximum number of bytes to read
  8757. + * @ppos: The current "file" position
  8758. + *
  8759. + * This function provides a read implementation for the "count" debugfs
  8760. + * interface to the hardware latency detector. Can be used to read the
  8761. + * number of latency readings exceeding the configured threshold since
  8762. + * the detector was last reset (e.g. by writing a zero into "count").
  8763. + */
  8764. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  8765. + size_t cnt, loff_t *ppos)
  8766. +{
  8767. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  8768. +}
  8769. +
  8770. +/**
  8771. + * debug_count_fwrite - Write function for "count" debugfs entry
  8772. + * @filp: The active open file structure for the debugfs "file"
  8773. + * @ubuf: The user buffer that contains the value to write
  8774. + * @cnt: The maximum number of bytes to write to "file"
  8775. + * @ppos: The current position in the debugfs "file"
  8776. + *
  8777. + * This function provides a write implementation for the "count" debugfs
  8778. + * interface to the hardware latency detector. Can be used to write a
  8779. + * desired value, especially to zero the total count.
  8780. + */
  8781. +static ssize_t debug_count_fwrite(struct file *filp,
  8782. + const char __user *ubuf,
  8783. + size_t cnt,
  8784. + loff_t *ppos)
  8785. +{
  8786. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  8787. +}
  8788. +
  8789. +/**
  8790. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  8791. + * @inode: The in-kernel inode representation of the debugfs "file"
  8792. + * @filp: The active open file structure for the debugfs "file"
  8793. + *
  8794. + * This function provides an open implementation for the "enable" debugfs
  8795. + * interface to the hardware latency detector.
  8796. + */
  8797. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  8798. +{
  8799. + return 0;
  8800. +}
  8801. +
  8802. +/**
  8803. + * debug_enable_fread - Read function for "enable" debugfs interface
  8804. + * @filp: The active open file structure for the debugfs "file"
  8805. + * @ubuf: The userspace provided buffer to read value into
  8806. + * @cnt: The maximum number of bytes to read
  8807. + * @ppos: The current "file" position
  8808. + *
  8809. + * This function provides a read implementation for the "enable" debugfs
  8810. + * interface to the hardware latency detector. Can be used to determine
  8811. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  8812. + */
  8813. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  8814. + size_t cnt, loff_t *ppos)
  8815. +{
  8816. + char buf[4];
  8817. +
  8818. + if ((cnt < sizeof(buf)) || (*ppos))
  8819. + return 0;
  8820. +
  8821. + buf[0] = enabled ? '1' : '0';
  8822. + buf[1] = '\n';
  8823. + buf[2] = '\0';
  8824. + if (copy_to_user(ubuf, buf, strlen(buf)))
  8825. + return -EFAULT;
  8826. + return *ppos = strlen(buf);
  8827. +}
  8828. +
  8829. +/**
  8830. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  8831. + * @filp: The active open file structure for the debugfs "file"
  8832. + * @ubuf: The user buffer that contains the value to write
  8833. + * @cnt: The maximum number of bytes to write to "file"
  8834. + * @ppos: The current position in the debugfs "file"
  8835. + *
  8836. + * This function provides a write implementation for the "enable" debugfs
  8837. + * interface to the hardware latency detector. Can be used to enable or
  8838. + * disable the detector, which will have the side-effect of possibly
  8839. + * also resetting the global stats and kicking off the measuring
  8840. + * kthread (on an enable) or the converse (upon a disable).
  8841. + */
  8842. +static ssize_t debug_enable_fwrite(struct file *filp,
  8843. + const char __user *ubuf,
  8844. + size_t cnt,
  8845. + loff_t *ppos)
  8846. +{
  8847. + char buf[4];
  8848. + int csize = min(cnt, sizeof(buf));
  8849. + long val = 0;
  8850. + int err = 0;
  8851. +
  8852. + memset(buf, '\0', sizeof(buf));
  8853. + if (copy_from_user(buf, ubuf, csize))
  8854. + return -EFAULT;
  8855. +
  8856. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  8857. + err = kstrtoul(buf, 10, &val);
  8858. + if (err)
  8859. + return -EINVAL;
  8860. +
  8861. + if (val) {
  8862. + if (enabled)
  8863. + goto unlock;
  8864. + enabled = 1;
  8865. + __reset_stats();
  8866. + if (start_kthread())
  8867. + return -EFAULT;
  8868. + } else {
  8869. + if (!enabled)
  8870. + goto unlock;
  8871. + enabled = 0;
  8872. + err = stop_kthread();
  8873. + if (err) {
  8874. + pr_err(BANNER "cannot stop kthread\n");
  8875. + return -EFAULT;
  8876. + }
  8877. + wake_up(&data.wq); /* reader(s) should return */
  8878. + }
  8879. +unlock:
  8880. + return csize;
  8881. +}
  8882. +
  8883. +/**
  8884. + * debug_max_fopen - Open function for "max" debugfs entry
  8885. + * @inode: The in-kernel inode representation of the debugfs "file"
  8886. + * @filp: The active open file structure for the debugfs "file"
  8887. + *
  8888. + * This function provides an open implementation for the "max" debugfs
  8889. + * interface to the hardware latency detector.
  8890. + */
  8891. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  8892. +{
  8893. + return 0;
  8894. +}
  8895. +
  8896. +/**
  8897. + * debug_max_fread - Read function for "max" debugfs entry
  8898. + * @filp: The active open file structure for the debugfs "file"
  8899. + * @ubuf: The userspace provided buffer to read value into
  8900. + * @cnt: The maximum number of bytes to read
  8901. + * @ppos: The current "file" position
  8902. + *
  8903. + * This function provides a read implementation for the "max" debugfs
  8904. + * interface to the hardware latency detector. Can be used to determine
  8905. + * the maximum latency value observed since it was last reset.
  8906. + */
  8907. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  8908. + size_t cnt, loff_t *ppos)
  8909. +{
  8910. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  8911. +}
  8912. +
  8913. +/**
  8914. + * debug_max_fwrite - Write function for "max" debugfs entry
  8915. + * @filp: The active open file structure for the debugfs "file"
  8916. + * @ubuf: The user buffer that contains the value to write
  8917. + * @cnt: The maximum number of bytes to write to "file"
  8918. + * @ppos: The current position in the debugfs "file"
  8919. + *
  8920. + * This function provides a write implementation for the "max" debugfs
  8921. + * interface to the hardware latency detector. Can be used to reset the
  8922. + * maximum or set it to some other desired value - if, then, subsequent
  8923. + * measurements exceed this value, the maximum will be updated.
  8924. + */
  8925. +static ssize_t debug_max_fwrite(struct file *filp,
  8926. + const char __user *ubuf,
  8927. + size_t cnt,
  8928. + loff_t *ppos)
  8929. +{
  8930. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  8931. +}
  8932. +
  8933. +
  8934. +/**
  8935. + * debug_sample_fopen - An open function for "sample" debugfs interface
  8936. + * @inode: The in-kernel inode representation of this debugfs "file"
  8937. + * @filp: The active open file structure for the debugfs "file"
  8938. + *
  8939. + * This function handles opening the "sample" file within the hardware
  8940. + * latency detector debugfs directory interface. This file is used to read
  8941. + * raw samples from the global ring_buffer and allows the user to see a
  8942. + * running latency history. Can be opened blocking or non-blocking,
  8943. + * affecting whether it behaves as a buffer read pipe, or does not.
  8944. + * Implements simple locking to prevent multiple simultaneous use.
  8945. + */
  8946. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  8947. +{
  8948. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  8949. + return -EBUSY;
  8950. + else
  8951. + return 0;
  8952. +}
  8953. +
  8954. +/**
  8955. + * debug_sample_fread - A read function for "sample" debugfs interface
  8956. + * @filp: The active open file structure for the debugfs "file"
  8957. + * @ubuf: The user buffer that will contain the samples read
  8958. + * @cnt: The maximum bytes to read from the debugfs "file"
  8959. + * @ppos: The current position in the debugfs "file"
  8960. + *
  8961. + * This function handles reading from the "sample" file within the hardware
  8962. + * latency detector debugfs directory interface. This file is used to read
  8963. + * raw samples from the global ring_buffer and allows the user to see a
  8964. + * running latency history. By default this will block pending a new
  8965. + * value written into the sample buffer, unless there are already a
  8966. + * number of value(s) waiting in the buffer, or the sample file was
  8967. + * previously opened in a non-blocking mode of operation.
  8968. + */
  8969. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  8970. + size_t cnt, loff_t *ppos)
  8971. +{
  8972. + int len = 0;
  8973. + char buf[64];
  8974. + struct sample *sample = NULL;
  8975. +
  8976. + if (!enabled)
  8977. + return 0;
  8978. +
  8979. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  8980. + if (!sample)
  8981. + return -ENOMEM;
  8982. +
  8983. + while (!buffer_get_sample(sample)) {
  8984. +
  8985. + DEFINE_WAIT(wait);
  8986. +
  8987. + if (filp->f_flags & O_NONBLOCK) {
  8988. + len = -EAGAIN;
  8989. + goto out;
  8990. + }
  8991. +
  8992. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  8993. + schedule();
  8994. + finish_wait(&data.wq, &wait);
  8995. +
  8996. + if (signal_pending(current)) {
  8997. + len = -EINTR;
  8998. + goto out;
  8999. + }
  9000. +
  9001. + if (!enabled) { /* enable was toggled */
  9002. + len = 0;
  9003. + goto out;
  9004. + }
  9005. + }
  9006. +
  9007. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  9008. + sample->timestamp.tv_sec,
  9009. + sample->timestamp.tv_nsec,
  9010. + sample->duration,
  9011. + sample->outer_duration);
  9012. +
  9013. +
  9014. + /* handling partial reads is more trouble than it's worth */
  9015. + if (len > cnt)
  9016. + goto out;
  9017. +
  9018. + if (copy_to_user(ubuf, buf, len))
  9019. + len = -EFAULT;
  9020. +
  9021. +out:
  9022. + kfree(sample);
  9023. + return len;
  9024. +}
  9025. +
  9026. +/**
  9027. + * debug_sample_release - Release function for "sample" debugfs interface
  9028. + * @inode: The in-kernel inode represenation of the debugfs "file"
  9029. + * @filp: The active open file structure for the debugfs "file"
  9030. + *
  9031. + * This function completes the close of the debugfs interface "sample" file.
  9032. + * Frees the sample_open "lock" so that other users may open the interface.
  9033. + */
  9034. +static int debug_sample_release(struct inode *inode, struct file *filp)
  9035. +{
  9036. + atomic_dec(&data.sample_open);
  9037. +
  9038. + return 0;
  9039. +}
  9040. +
  9041. +/**
  9042. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  9043. + * @inode: The in-kernel inode representation of the debugfs "file"
  9044. + * @filp: The active open file structure for the debugfs "file"
  9045. + *
  9046. + * This function provides an open implementation for the "threshold" debugfs
  9047. + * interface to the hardware latency detector.
  9048. + */
  9049. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  9050. +{
  9051. + return 0;
  9052. +}
  9053. +
  9054. +/**
  9055. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  9056. + * @filp: The active open file structure for the debugfs "file"
  9057. + * @ubuf: The userspace provided buffer to read value into
  9058. + * @cnt: The maximum number of bytes to read
  9059. + * @ppos: The current "file" position
  9060. + *
  9061. + * This function provides a read implementation for the "threshold" debugfs
  9062. + * interface to the hardware latency detector. It can be used to determine
  9063. + * the current threshold level at which a latency will be recorded in the
  9064. + * global ring buffer, typically on the order of 10us.
  9065. + */
  9066. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  9067. + size_t cnt, loff_t *ppos)
  9068. +{
  9069. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  9070. +}
  9071. +
  9072. +/**
  9073. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  9074. + * @filp: The active open file structure for the debugfs "file"
  9075. + * @ubuf: The user buffer that contains the value to write
  9076. + * @cnt: The maximum number of bytes to write to "file"
  9077. + * @ppos: The current position in the debugfs "file"
  9078. + *
  9079. + * This function provides a write implementation for the "threshold" debugfs
  9080. + * interface to the hardware latency detector. It can be used to configure
  9081. + * the threshold level at which any subsequently detected latencies will
  9082. + * be recorded into the global ring buffer.
  9083. + */
  9084. +static ssize_t debug_threshold_fwrite(struct file *filp,
  9085. + const char __user *ubuf,
  9086. + size_t cnt,
  9087. + loff_t *ppos)
  9088. +{
  9089. + int ret;
  9090. +
  9091. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  9092. +
  9093. + if (enabled)
  9094. + wake_up_process(kthread);
  9095. +
  9096. + return ret;
  9097. +}
  9098. +
  9099. +/**
  9100. + * debug_width_fopen - Open function for "width" debugfs entry
  9101. + * @inode: The in-kernel inode representation of the debugfs "file"
  9102. + * @filp: The active open file structure for the debugfs "file"
  9103. + *
  9104. + * This function provides an open implementation for the "width" debugfs
  9105. + * interface to the hardware latency detector.
  9106. + */
  9107. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  9108. +{
  9109. + return 0;
  9110. +}
  9111. +
  9112. +/**
  9113. + * debug_width_fread - Read function for "width" debugfs entry
  9114. + * @filp: The active open file structure for the debugfs "file"
  9115. + * @ubuf: The userspace provided buffer to read value into
  9116. + * @cnt: The maximum number of bytes to read
  9117. + * @ppos: The current "file" position
  9118. + *
  9119. + * This function provides a read implementation for the "width" debugfs
  9120. + * interface to the hardware latency detector. It can be used to determine
  9121. + * for how many us of the total window us we will actively sample for any
  9122. + * hardware-induced latecy periods. Obviously, it is not possible to
  9123. + * sample constantly and have the system respond to a sample reader, or,
  9124. + * worse, without having the system appear to have gone out to lunch.
  9125. + */
  9126. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  9127. + size_t cnt, loff_t *ppos)
  9128. +{
  9129. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  9130. +}
  9131. +
  9132. +/**
  9133. + * debug_width_fwrite - Write function for "width" debugfs entry
  9134. + * @filp: The active open file structure for the debugfs "file"
  9135. + * @ubuf: The user buffer that contains the value to write
  9136. + * @cnt: The maximum number of bytes to write to "file"
  9137. + * @ppos: The current position in the debugfs "file"
  9138. + *
  9139. + * This function provides a write implementation for the "width" debugfs
  9140. + * interface to the hardware latency detector. It can be used to configure
  9141. + * for how many us of the total window us we will actively sample for any
  9142. + * hardware-induced latency periods. Obviously, it is not possible to
  9143. + * sample constantly and have the system respond to a sample reader, or,
  9144. + * worse, without having the system appear to have gone out to lunch. It
  9145. + * is enforced that width is less that the total window size.
  9146. + */
  9147. +static ssize_t debug_width_fwrite(struct file *filp,
  9148. + const char __user *ubuf,
  9149. + size_t cnt,
  9150. + loff_t *ppos)
  9151. +{
  9152. + char buf[U64STR_SIZE];
  9153. + int csize = min(cnt, sizeof(buf));
  9154. + u64 val = 0;
  9155. + int err = 0;
  9156. +
  9157. + memset(buf, '\0', sizeof(buf));
  9158. + if (copy_from_user(buf, ubuf, csize))
  9159. + return -EFAULT;
  9160. +
  9161. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9162. + err = kstrtoull(buf, 10, &val);
  9163. + if (err)
  9164. + return -EINVAL;
  9165. +
  9166. + mutex_lock(&data.lock);
  9167. + if (val < data.sample_window)
  9168. + data.sample_width = val;
  9169. + else {
  9170. + mutex_unlock(&data.lock);
  9171. + return -EINVAL;
  9172. + }
  9173. + mutex_unlock(&data.lock);
  9174. +
  9175. + if (enabled)
  9176. + wake_up_process(kthread);
  9177. +
  9178. + return csize;
  9179. +}
  9180. +
  9181. +/**
  9182. + * debug_window_fopen - Open function for "window" debugfs entry
  9183. + * @inode: The in-kernel inode representation of the debugfs "file"
  9184. + * @filp: The active open file structure for the debugfs "file"
  9185. + *
  9186. + * This function provides an open implementation for the "window" debugfs
  9187. + * interface to the hardware latency detector. The window is the total time
  9188. + * in us that will be considered one sample period. Conceptually, windows
  9189. + * occur back-to-back and contain a sample width period during which
  9190. + * actual sampling occurs.
  9191. + */
  9192. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  9193. +{
  9194. + return 0;
  9195. +}
  9196. +
  9197. +/**
  9198. + * debug_window_fread - Read function for "window" debugfs entry
  9199. + * @filp: The active open file structure for the debugfs "file"
  9200. + * @ubuf: The userspace provided buffer to read value into
  9201. + * @cnt: The maximum number of bytes to read
  9202. + * @ppos: The current "file" position
  9203. + *
  9204. + * This function provides a read implementation for the "window" debugfs
  9205. + * interface to the hardware latency detector. The window is the total time
  9206. + * in us that will be considered one sample period. Conceptually, windows
  9207. + * occur back-to-back and contain a sample width period during which
  9208. + * actual sampling occurs. Can be used to read the total window size.
  9209. + */
  9210. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  9211. + size_t cnt, loff_t *ppos)
  9212. +{
  9213. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  9214. +}
  9215. +
  9216. +/**
  9217. + * debug_window_fwrite - Write function for "window" debugfs entry
  9218. + * @filp: The active open file structure for the debugfs "file"
  9219. + * @ubuf: The user buffer that contains the value to write
  9220. + * @cnt: The maximum number of bytes to write to "file"
  9221. + * @ppos: The current position in the debugfs "file"
  9222. + *
  9223. + * This function provides a write implementation for the "window" debufds
  9224. + * interface to the hardware latency detetector. The window is the total time
  9225. + * in us that will be considered one sample period. Conceptually, windows
  9226. + * occur back-to-back and contain a sample width period during which
  9227. + * actual sampling occurs. Can be used to write a new total window size. It
  9228. + * is enfoced that any value written must be greater than the sample width
  9229. + * size, or an error results.
  9230. + */
  9231. +static ssize_t debug_window_fwrite(struct file *filp,
  9232. + const char __user *ubuf,
  9233. + size_t cnt,
  9234. + loff_t *ppos)
  9235. +{
  9236. + char buf[U64STR_SIZE];
  9237. + int csize = min(cnt, sizeof(buf));
  9238. + u64 val = 0;
  9239. + int err = 0;
  9240. +
  9241. + memset(buf, '\0', sizeof(buf));
  9242. + if (copy_from_user(buf, ubuf, csize))
  9243. + return -EFAULT;
  9244. +
  9245. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  9246. + err = kstrtoull(buf, 10, &val);
  9247. + if (err)
  9248. + return -EINVAL;
  9249. +
  9250. + mutex_lock(&data.lock);
  9251. + if (data.sample_width < val)
  9252. + data.sample_window = val;
  9253. + else {
  9254. + mutex_unlock(&data.lock);
  9255. + return -EINVAL;
  9256. + }
  9257. + mutex_unlock(&data.lock);
  9258. +
  9259. + return csize;
  9260. +}
  9261. +
  9262. +/*
  9263. + * Function pointers for the "count" debugfs file operations
  9264. + */
  9265. +static const struct file_operations count_fops = {
  9266. + .open = debug_count_fopen,
  9267. + .read = debug_count_fread,
  9268. + .write = debug_count_fwrite,
  9269. + .owner = THIS_MODULE,
  9270. +};
  9271. +
  9272. +/*
  9273. + * Function pointers for the "enable" debugfs file operations
  9274. + */
  9275. +static const struct file_operations enable_fops = {
  9276. + .open = debug_enable_fopen,
  9277. + .read = debug_enable_fread,
  9278. + .write = debug_enable_fwrite,
  9279. + .owner = THIS_MODULE,
  9280. +};
  9281. +
  9282. +/*
  9283. + * Function pointers for the "max" debugfs file operations
  9284. + */
  9285. +static const struct file_operations max_fops = {
  9286. + .open = debug_max_fopen,
  9287. + .read = debug_max_fread,
  9288. + .write = debug_max_fwrite,
  9289. + .owner = THIS_MODULE,
  9290. +};
  9291. +
  9292. +/*
  9293. + * Function pointers for the "sample" debugfs file operations
  9294. + */
  9295. +static const struct file_operations sample_fops = {
  9296. + .open = debug_sample_fopen,
  9297. + .read = debug_sample_fread,
  9298. + .release = debug_sample_release,
  9299. + .owner = THIS_MODULE,
  9300. +};
  9301. +
  9302. +/*
  9303. + * Function pointers for the "threshold" debugfs file operations
  9304. + */
  9305. +static const struct file_operations threshold_fops = {
  9306. + .open = debug_threshold_fopen,
  9307. + .read = debug_threshold_fread,
  9308. + .write = debug_threshold_fwrite,
  9309. + .owner = THIS_MODULE,
  9310. +};
  9311. +
  9312. +/*
  9313. + * Function pointers for the "width" debugfs file operations
  9314. + */
  9315. +static const struct file_operations width_fops = {
  9316. + .open = debug_width_fopen,
  9317. + .read = debug_width_fread,
  9318. + .write = debug_width_fwrite,
  9319. + .owner = THIS_MODULE,
  9320. +};
  9321. +
  9322. +/*
  9323. + * Function pointers for the "window" debugfs file operations
  9324. + */
  9325. +static const struct file_operations window_fops = {
  9326. + .open = debug_window_fopen,
  9327. + .read = debug_window_fread,
  9328. + .write = debug_window_fwrite,
  9329. + .owner = THIS_MODULE,
  9330. +};
  9331. +
  9332. +/**
  9333. + * init_debugfs - A function to initialize the debugfs interface files
  9334. + *
  9335. + * This function creates entries in debugfs for "hwlat_detector", including
  9336. + * files to read values from the detector, current samples, and the
  9337. + * maximum sample that has been captured since the hardware latency
  9338. + * dectector was started.
  9339. + */
  9340. +static int init_debugfs(void)
  9341. +{
  9342. + int ret = -ENOMEM;
  9343. +
  9344. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  9345. + if (!debug_dir)
  9346. + goto err_debug_dir;
  9347. +
  9348. + debug_sample = debugfs_create_file("sample", 0444,
  9349. + debug_dir, NULL,
  9350. + &sample_fops);
  9351. + if (!debug_sample)
  9352. + goto err_sample;
  9353. +
  9354. + debug_count = debugfs_create_file("count", 0444,
  9355. + debug_dir, NULL,
  9356. + &count_fops);
  9357. + if (!debug_count)
  9358. + goto err_count;
  9359. +
  9360. + debug_max = debugfs_create_file("max", 0444,
  9361. + debug_dir, NULL,
  9362. + &max_fops);
  9363. + if (!debug_max)
  9364. + goto err_max;
  9365. +
  9366. + debug_sample_window = debugfs_create_file("window", 0644,
  9367. + debug_dir, NULL,
  9368. + &window_fops);
  9369. + if (!debug_sample_window)
  9370. + goto err_window;
  9371. +
  9372. + debug_sample_width = debugfs_create_file("width", 0644,
  9373. + debug_dir, NULL,
  9374. + &width_fops);
  9375. + if (!debug_sample_width)
  9376. + goto err_width;
  9377. +
  9378. + debug_threshold = debugfs_create_file("threshold", 0644,
  9379. + debug_dir, NULL,
  9380. + &threshold_fops);
  9381. + if (!debug_threshold)
  9382. + goto err_threshold;
  9383. +
  9384. + debug_enable = debugfs_create_file("enable", 0644,
  9385. + debug_dir, &enabled,
  9386. + &enable_fops);
  9387. + if (!debug_enable)
  9388. + goto err_enable;
  9389. +
  9390. + else {
  9391. + ret = 0;
  9392. + goto out;
  9393. + }
  9394. +
  9395. +err_enable:
  9396. + debugfs_remove(debug_threshold);
  9397. +err_threshold:
  9398. + debugfs_remove(debug_sample_width);
  9399. +err_width:
  9400. + debugfs_remove(debug_sample_window);
  9401. +err_window:
  9402. + debugfs_remove(debug_max);
  9403. +err_max:
  9404. + debugfs_remove(debug_count);
  9405. +err_count:
  9406. + debugfs_remove(debug_sample);
  9407. +err_sample:
  9408. + debugfs_remove(debug_dir);
  9409. +err_debug_dir:
  9410. +out:
  9411. + return ret;
  9412. +}
  9413. +
  9414. +/**
  9415. + * free_debugfs - A function to cleanup the debugfs file interface
  9416. + */
  9417. +static void free_debugfs(void)
  9418. +{
  9419. + /* could also use a debugfs_remove_recursive */
  9420. + debugfs_remove(debug_enable);
  9421. + debugfs_remove(debug_threshold);
  9422. + debugfs_remove(debug_sample_width);
  9423. + debugfs_remove(debug_sample_window);
  9424. + debugfs_remove(debug_max);
  9425. + debugfs_remove(debug_count);
  9426. + debugfs_remove(debug_sample);
  9427. + debugfs_remove(debug_dir);
  9428. +}
  9429. +
  9430. +/**
  9431. + * detector_init - Standard module initialization code
  9432. + */
  9433. +static int detector_init(void)
  9434. +{
  9435. + int ret = -ENOMEM;
  9436. +
  9437. + pr_info(BANNER "version %s\n", VERSION);
  9438. +
  9439. + ret = init_stats();
  9440. + if (ret)
  9441. + goto out;
  9442. +
  9443. + ret = init_debugfs();
  9444. + if (ret)
  9445. + goto err_stats;
  9446. +
  9447. + if (enabled)
  9448. + ret = start_kthread();
  9449. +
  9450. + goto out;
  9451. +
  9452. +err_stats:
  9453. + ring_buffer_free(ring_buffer);
  9454. +out:
  9455. + return ret;
  9456. +
  9457. +}
  9458. +
  9459. +/**
  9460. + * detector_exit - Standard module cleanup code
  9461. + */
  9462. +static void detector_exit(void)
  9463. +{
  9464. + int err;
  9465. +
  9466. + if (enabled) {
  9467. + enabled = 0;
  9468. + err = stop_kthread();
  9469. + if (err)
  9470. + pr_err(BANNER "cannot stop kthread\n");
  9471. + }
  9472. +
  9473. + free_debugfs();
  9474. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  9475. +
  9476. +}
  9477. +
  9478. +module_init(detector_init);
  9479. +module_exit(detector_exit);
  9480. diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
  9481. index acece3299756..58ea04a03fa9 100644
  9482. --- a/drivers/mmc/host/mmci.c
  9483. +++ b/drivers/mmc/host/mmci.c
  9484. @@ -1155,15 +1155,12 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9485. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  9486. struct variant_data *variant = host->variant;
  9487. void __iomem *base = host->base;
  9488. - unsigned long flags;
  9489. u32 status;
  9490. status = readl(base + MMCISTATUS);
  9491. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  9492. - local_irq_save(flags);
  9493. -
  9494. do {
  9495. unsigned int remain, len;
  9496. char *buffer;
  9497. @@ -1203,8 +1200,6 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  9498. sg_miter_stop(sg_miter);
  9499. - local_irq_restore(flags);
  9500. -
  9501. /*
  9502. * If we have less than the fifo 'half-full' threshold to transfer,
  9503. * trigger a PIO interrupt as soon as any data is available.
  9504. diff --git a/drivers/net/ethernet/3com/3c59x.c b/drivers/net/ethernet/3com/3c59x.c
  9505. index 2839af00f20c..4348b9c850d3 100644
  9506. --- a/drivers/net/ethernet/3com/3c59x.c
  9507. +++ b/drivers/net/ethernet/3com/3c59x.c
  9508. @@ -842,9 +842,9 @@ static void poll_vortex(struct net_device *dev)
  9509. {
  9510. struct vortex_private *vp = netdev_priv(dev);
  9511. unsigned long flags;
  9512. - local_irq_save(flags);
  9513. + local_irq_save_nort(flags);
  9514. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  9515. - local_irq_restore(flags);
  9516. + local_irq_restore_nort(flags);
  9517. }
  9518. #endif
  9519. @@ -1916,12 +1916,12 @@ static void vortex_tx_timeout(struct net_device *dev)
  9520. * Block interrupts because vortex_interrupt does a bare spin_lock()
  9521. */
  9522. unsigned long flags;
  9523. - local_irq_save(flags);
  9524. + local_irq_save_nort(flags);
  9525. if (vp->full_bus_master_tx)
  9526. boomerang_interrupt(dev->irq, dev);
  9527. else
  9528. vortex_interrupt(dev->irq, dev);
  9529. - local_irq_restore(flags);
  9530. + local_irq_restore_nort(flags);
  9531. }
  9532. }
  9533. diff --git a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9534. index 8b5988e210d5..cf9928ccdd7e 100644
  9535. --- a/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9536. +++ b/drivers/net/ethernet/atheros/atl1c/atl1c_main.c
  9537. @@ -2221,11 +2221,7 @@ static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
  9538. }
  9539. tpd_req = atl1c_cal_tpd_req(skb);
  9540. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  9541. - if (netif_msg_pktdata(adapter))
  9542. - dev_info(&adapter->pdev->dev, "tx locked\n");
  9543. - return NETDEV_TX_LOCKED;
  9544. - }
  9545. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9546. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  9547. /* no enough descriptor, just stop queue */
  9548. diff --git a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9549. index 59a03a193e83..734f7a7ad2c3 100644
  9550. --- a/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9551. +++ b/drivers/net/ethernet/atheros/atl1e/atl1e_main.c
  9552. @@ -1880,8 +1880,7 @@ static netdev_tx_t atl1e_xmit_frame(struct sk_buff *skb,
  9553. return NETDEV_TX_OK;
  9554. }
  9555. tpd_req = atl1e_cal_tdp_req(skb);
  9556. - if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  9557. - return NETDEV_TX_LOCKED;
  9558. + spin_lock_irqsave(&adapter->tx_lock, flags);
  9559. if (atl1e_tpd_avail(adapter) < tpd_req) {
  9560. /* no enough descriptor, just stop queue */
  9561. diff --git a/drivers/net/ethernet/chelsio/cxgb/sge.c b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9562. index 526ea74e82d9..86f467a2c485 100644
  9563. --- a/drivers/net/ethernet/chelsio/cxgb/sge.c
  9564. +++ b/drivers/net/ethernet/chelsio/cxgb/sge.c
  9565. @@ -1664,8 +1664,7 @@ static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  9566. struct cmdQ *q = &sge->cmdQ[qid];
  9567. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  9568. - if (!spin_trylock(&q->lock))
  9569. - return NETDEV_TX_LOCKED;
  9570. + spin_lock(&q->lock);
  9571. reclaim_completed_tx(sge, q);
  9572. diff --git a/drivers/net/ethernet/neterion/s2io.c b/drivers/net/ethernet/neterion/s2io.c
  9573. index 9ba975853ec6..813cfa698160 100644
  9574. --- a/drivers/net/ethernet/neterion/s2io.c
  9575. +++ b/drivers/net/ethernet/neterion/s2io.c
  9576. @@ -4084,12 +4084,7 @@ static netdev_tx_t s2io_xmit(struct sk_buff *skb, struct net_device *dev)
  9577. [skb->priority & (MAX_TX_FIFOS - 1)];
  9578. fifo = &mac_control->fifos[queue];
  9579. - if (do_spin_lock)
  9580. - spin_lock_irqsave(&fifo->tx_lock, flags);
  9581. - else {
  9582. - if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
  9583. - return NETDEV_TX_LOCKED;
  9584. - }
  9585. + spin_lock_irqsave(&fifo->tx_lock, flags);
  9586. if (sp->config.multiq) {
  9587. if (__netif_subqueue_stopped(dev, fifo->fifo_no)) {
  9588. 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
  9589. index 3b98b263bad0..ca4add749410 100644
  9590. --- a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9591. +++ b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
  9592. @@ -2137,10 +2137,8 @@ static int pch_gbe_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  9593. struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
  9594. unsigned long flags;
  9595. - if (!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
  9596. - /* Collision - tell upper layer to requeue */
  9597. - return NETDEV_TX_LOCKED;
  9598. - }
  9599. + spin_lock_irqsave(&tx_ring->tx_lock, flags);
  9600. +
  9601. if (unlikely(!PCH_GBE_DESC_UNUSED(tx_ring))) {
  9602. netif_stop_queue(netdev);
  9603. spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
  9604. diff --git a/drivers/net/ethernet/realtek/8139too.c b/drivers/net/ethernet/realtek/8139too.c
  9605. index ef668d300800..d987d571fdd6 100644
  9606. --- a/drivers/net/ethernet/realtek/8139too.c
  9607. +++ b/drivers/net/ethernet/realtek/8139too.c
  9608. @@ -2229,7 +2229,7 @@ static void rtl8139_poll_controller(struct net_device *dev)
  9609. struct rtl8139_private *tp = netdev_priv(dev);
  9610. const int irq = tp->pci_dev->irq;
  9611. - disable_irq(irq);
  9612. + disable_irq_nosync(irq);
  9613. rtl8139_interrupt(irq, dev);
  9614. enable_irq(irq);
  9615. }
  9616. diff --git a/drivers/net/ethernet/tehuti/tehuti.c b/drivers/net/ethernet/tehuti/tehuti.c
  9617. index 14c9d1baa85c..e1a5305418a8 100644
  9618. --- a/drivers/net/ethernet/tehuti/tehuti.c
  9619. +++ b/drivers/net/ethernet/tehuti/tehuti.c
  9620. @@ -1629,13 +1629,8 @@ static netdev_tx_t bdx_tx_transmit(struct sk_buff *skb,
  9621. unsigned long flags;
  9622. ENTER;
  9623. - local_irq_save(flags);
  9624. - if (!spin_trylock(&priv->tx_lock)) {
  9625. - local_irq_restore(flags);
  9626. - DBG("%s[%s]: TX locked, returning NETDEV_TX_LOCKED\n",
  9627. - BDX_DRV_NAME, ndev->name);
  9628. - return NETDEV_TX_LOCKED;
  9629. - }
  9630. +
  9631. + spin_lock_irqsave(&priv->tx_lock, flags);
  9632. /* build tx descriptor */
  9633. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  9634. diff --git a/drivers/net/rionet.c b/drivers/net/rionet.c
  9635. index e7034c55e796..2e4ee0f912bf 100644
  9636. --- a/drivers/net/rionet.c
  9637. +++ b/drivers/net/rionet.c
  9638. @@ -174,11 +174,7 @@ static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  9639. unsigned long flags;
  9640. int add_num = 1;
  9641. - local_irq_save(flags);
  9642. - if (!spin_trylock(&rnet->tx_lock)) {
  9643. - local_irq_restore(flags);
  9644. - return NETDEV_TX_LOCKED;
  9645. - }
  9646. + spin_lock_irqsave(&rnet->tx_lock, flags);
  9647. if (is_multicast_ether_addr(eth->h_dest))
  9648. add_num = nets[rnet->mport->id].nact;
  9649. diff --git a/drivers/net/wireless/orinoco/orinoco_usb.c b/drivers/net/wireless/orinoco/orinoco_usb.c
  9650. index f2cd513d54b2..6c0f4c9638a2 100644
  9651. --- a/drivers/net/wireless/orinoco/orinoco_usb.c
  9652. +++ b/drivers/net/wireless/orinoco/orinoco_usb.c
  9653. @@ -697,7 +697,7 @@ static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  9654. while (!ctx->done.done && msecs--)
  9655. udelay(1000);
  9656. } else {
  9657. - wait_event_interruptible(ctx->done.wait,
  9658. + swait_event_interruptible(ctx->done.wait,
  9659. ctx->done.done);
  9660. }
  9661. break;
  9662. diff --git a/drivers/pci/access.c b/drivers/pci/access.c
  9663. index 59ac36fe7c42..7a45a20af78a 100644
  9664. --- a/drivers/pci/access.c
  9665. +++ b/drivers/pci/access.c
  9666. @@ -561,7 +561,7 @@ void pci_cfg_access_unlock(struct pci_dev *dev)
  9667. WARN_ON(!dev->block_cfg_access);
  9668. dev->block_cfg_access = 0;
  9669. - wake_up_all(&pci_cfg_wait);
  9670. + wake_up_all_locked(&pci_cfg_wait);
  9671. raw_spin_unlock_irqrestore(&pci_lock, flags);
  9672. }
  9673. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  9674. diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
  9675. index f4424063b860..f1622a05854b 100644
  9676. --- a/drivers/scsi/fcoe/fcoe.c
  9677. +++ b/drivers/scsi/fcoe/fcoe.c
  9678. @@ -1286,7 +1286,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9679. struct sk_buff *skb;
  9680. #ifdef CONFIG_SMP
  9681. struct fcoe_percpu_s *p0;
  9682. - unsigned targ_cpu = get_cpu();
  9683. + unsigned targ_cpu = get_cpu_light();
  9684. #endif /* CONFIG_SMP */
  9685. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  9686. @@ -1342,7 +1342,7 @@ static void fcoe_percpu_thread_destroy(unsigned int cpu)
  9687. kfree_skb(skb);
  9688. spin_unlock_bh(&p->fcoe_rx_list.lock);
  9689. }
  9690. - put_cpu();
  9691. + put_cpu_light();
  9692. #else
  9693. /*
  9694. * This a non-SMP scenario where the singular Rx thread is
  9695. @@ -1566,11 +1566,11 @@ err2:
  9696. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  9697. {
  9698. struct fcoe_percpu_s *fps;
  9699. - int rc;
  9700. + int rc, cpu = get_cpu_light();
  9701. - fps = &get_cpu_var(fcoe_percpu);
  9702. + fps = &per_cpu(fcoe_percpu, cpu);
  9703. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  9704. - put_cpu_var(fcoe_percpu);
  9705. + put_cpu_light();
  9706. return rc;
  9707. }
  9708. @@ -1766,11 +1766,11 @@ static inline int fcoe_filter_frames(struct fc_lport *lport,
  9709. return 0;
  9710. }
  9711. - stats = per_cpu_ptr(lport->stats, get_cpu());
  9712. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  9713. stats->InvalidCRCCount++;
  9714. if (stats->InvalidCRCCount < 5)
  9715. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  9716. - put_cpu();
  9717. + put_cpu_light();
  9718. return -EINVAL;
  9719. }
  9720. @@ -1846,13 +1846,13 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  9721. goto drop;
  9722. if (!fcoe_filter_frames(lport, fp)) {
  9723. - put_cpu();
  9724. + put_cpu_light();
  9725. fc_exch_recv(lport, fp);
  9726. return;
  9727. }
  9728. drop:
  9729. stats->ErrorFrames++;
  9730. - put_cpu();
  9731. + put_cpu_light();
  9732. kfree_skb(skb);
  9733. }
  9734. diff --git a/drivers/scsi/fcoe/fcoe_ctlr.c b/drivers/scsi/fcoe/fcoe_ctlr.c
  9735. index 34a1b1f333b4..d91131210695 100644
  9736. --- a/drivers/scsi/fcoe/fcoe_ctlr.c
  9737. +++ b/drivers/scsi/fcoe/fcoe_ctlr.c
  9738. @@ -831,7 +831,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  9739. INIT_LIST_HEAD(&del_list);
  9740. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  9741. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  9742. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  9743. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  9744. @@ -867,7 +867,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  9745. sel_time = fcf->time;
  9746. }
  9747. }
  9748. - put_cpu();
  9749. + put_cpu_light();
  9750. list_for_each_entry_safe(fcf, next, &del_list, list) {
  9751. /* Removes fcf from current list */
  9752. diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
  9753. index 30f9ef0c0d4f..6c686bc01a82 100644
  9754. --- a/drivers/scsi/libfc/fc_exch.c
  9755. +++ b/drivers/scsi/libfc/fc_exch.c
  9756. @@ -814,10 +814,10 @@ static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  9757. }
  9758. memset(ep, 0, sizeof(*ep));
  9759. - cpu = get_cpu();
  9760. + cpu = get_cpu_light();
  9761. pool = per_cpu_ptr(mp->pool, cpu);
  9762. spin_lock_bh(&pool->lock);
  9763. - put_cpu();
  9764. + put_cpu_light();
  9765. /* peek cache of free slot */
  9766. if (pool->left != FC_XID_UNKNOWN) {
  9767. diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c
  9768. index 9c706d8c1441..d968ffc79c08 100644
  9769. --- a/drivers/scsi/libsas/sas_ata.c
  9770. +++ b/drivers/scsi/libsas/sas_ata.c
  9771. @@ -190,7 +190,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  9772. /* TODO: audit callers to ensure they are ready for qc_issue to
  9773. * unconditionally re-enable interrupts
  9774. */
  9775. - local_irq_save(flags);
  9776. + local_irq_save_nort(flags);
  9777. spin_unlock(ap->lock);
  9778. /* If the device fell off, no sense in issuing commands */
  9779. @@ -255,7 +255,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  9780. out:
  9781. spin_lock(ap->lock);
  9782. - local_irq_restore(flags);
  9783. + local_irq_restore_nort(flags);
  9784. return ret;
  9785. }
  9786. diff --git a/drivers/scsi/qla2xxx/qla_inline.h b/drivers/scsi/qla2xxx/qla_inline.h
  9787. index fee9eb7c8a60..b42d4adc42dc 100644
  9788. --- a/drivers/scsi/qla2xxx/qla_inline.h
  9789. +++ b/drivers/scsi/qla2xxx/qla_inline.h
  9790. @@ -59,12 +59,12 @@ qla2x00_poll(struct rsp_que *rsp)
  9791. {
  9792. unsigned long flags;
  9793. struct qla_hw_data *ha = rsp->hw;
  9794. - local_irq_save(flags);
  9795. + local_irq_save_nort(flags);
  9796. if (IS_P3P_TYPE(ha))
  9797. qla82xx_poll(0, rsp);
  9798. else
  9799. ha->isp_ops->intr_handler(0, rsp);
  9800. - local_irq_restore(flags);
  9801. + local_irq_restore_nort(flags);
  9802. }
  9803. static inline uint8_t *
  9804. diff --git a/drivers/thermal/x86_pkg_temp_thermal.c b/drivers/thermal/x86_pkg_temp_thermal.c
  9805. index 7fc919f7da4d..e03fa17b8670 100644
  9806. --- a/drivers/thermal/x86_pkg_temp_thermal.c
  9807. +++ b/drivers/thermal/x86_pkg_temp_thermal.c
  9808. @@ -29,6 +29,7 @@
  9809. #include <linux/pm.h>
  9810. #include <linux/thermal.h>
  9811. #include <linux/debugfs.h>
  9812. +#include <linux/swork.h>
  9813. #include <asm/cpu_device_id.h>
  9814. #include <asm/mce.h>
  9815. @@ -352,7 +353,7 @@ static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  9816. }
  9817. }
  9818. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9819. +static void platform_thermal_notify_work(struct swork_event *event)
  9820. {
  9821. unsigned long flags;
  9822. int cpu = smp_processor_id();
  9823. @@ -369,7 +370,7 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9824. pkg_work_scheduled[phy_id]) {
  9825. disable_pkg_thres_interrupt();
  9826. spin_unlock_irqrestore(&pkg_work_lock, flags);
  9827. - return -EINVAL;
  9828. + return;
  9829. }
  9830. pkg_work_scheduled[phy_id] = 1;
  9831. spin_unlock_irqrestore(&pkg_work_lock, flags);
  9832. @@ -378,9 +379,48 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9833. schedule_delayed_work_on(cpu,
  9834. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  9835. msecs_to_jiffies(notify_delay_ms));
  9836. +}
  9837. +
  9838. +#ifdef CONFIG_PREEMPT_RT_FULL
  9839. +static struct swork_event notify_work;
  9840. +
  9841. +static int thermal_notify_work_init(void)
  9842. +{
  9843. + int err;
  9844. +
  9845. + err = swork_get();
  9846. + if (err)
  9847. + return err;
  9848. +
  9849. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  9850. return 0;
  9851. }
  9852. +static void thermal_notify_work_cleanup(void)
  9853. +{
  9854. + swork_put();
  9855. +}
  9856. +
  9857. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9858. +{
  9859. + swork_queue(&notify_work);
  9860. + return 0;
  9861. +}
  9862. +
  9863. +#else /* !CONFIG_PREEMPT_RT_FULL */
  9864. +
  9865. +static int thermal_notify_work_init(void) { return 0; }
  9866. +
  9867. +static void thermal_notify_work_cleanup(void) { }
  9868. +
  9869. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  9870. +{
  9871. + platform_thermal_notify_work(NULL);
  9872. +
  9873. + return 0;
  9874. +}
  9875. +#endif /* CONFIG_PREEMPT_RT_FULL */
  9876. +
  9877. static int find_siblings_cpu(int cpu)
  9878. {
  9879. int i;
  9880. @@ -584,6 +624,9 @@ static int __init pkg_temp_thermal_init(void)
  9881. if (!x86_match_cpu(pkg_temp_thermal_ids))
  9882. return -ENODEV;
  9883. + if (!thermal_notify_work_init())
  9884. + return -ENODEV;
  9885. +
  9886. spin_lock_init(&pkg_work_lock);
  9887. platform_thermal_package_notify =
  9888. pkg_temp_thermal_platform_thermal_notify;
  9889. @@ -608,7 +651,7 @@ err_ret:
  9890. kfree(pkg_work_scheduled);
  9891. platform_thermal_package_notify = NULL;
  9892. platform_thermal_package_rate_control = NULL;
  9893. -
  9894. + thermal_notify_work_cleanup();
  9895. return -ENODEV;
  9896. }
  9897. @@ -633,6 +676,7 @@ static void __exit pkg_temp_thermal_exit(void)
  9898. mutex_unlock(&phy_dev_list_mutex);
  9899. platform_thermal_package_notify = NULL;
  9900. platform_thermal_package_rate_control = NULL;
  9901. + thermal_notify_work_cleanup();
  9902. for_each_online_cpu(i)
  9903. cancel_delayed_work_sync(
  9904. &per_cpu(pkg_temp_thermal_threshold_work, i));
  9905. diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
  9906. index 39126460c1f5..af7701ca4d48 100644
  9907. --- a/drivers/tty/serial/8250/8250_core.c
  9908. +++ b/drivers/tty/serial/8250/8250_core.c
  9909. @@ -58,7 +58,16 @@ static struct uart_driver serial8250_reg;
  9910. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  9911. -#define PASS_LIMIT 512
  9912. +/*
  9913. + * On -rt we can have a more delays, and legitimately
  9914. + * so - so don't drop work spuriously and spam the
  9915. + * syslog:
  9916. + */
  9917. +#ifdef CONFIG_PREEMPT_RT_FULL
  9918. +# define PASS_LIMIT 1000000
  9919. +#else
  9920. +# define PASS_LIMIT 512
  9921. +#endif
  9922. #include <asm/serial.h>
  9923. /*
  9924. diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
  9925. index 56ccbcefdd85..a0b9e854672c 100644
  9926. --- a/drivers/tty/serial/8250/8250_port.c
  9927. +++ b/drivers/tty/serial/8250/8250_port.c
  9928. @@ -35,6 +35,7 @@
  9929. #include <linux/nmi.h>
  9930. #include <linux/mutex.h>
  9931. #include <linux/slab.h>
  9932. +#include <linux/kdb.h>
  9933. #include <linux/uaccess.h>
  9934. #include <linux/pm_runtime.h>
  9935. @@ -2843,9 +2844,9 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
  9936. serial8250_rpm_get(up);
  9937. - if (port->sysrq)
  9938. + if (port->sysrq || oops_in_progress)
  9939. locked = 0;
  9940. - else if (oops_in_progress)
  9941. + else if (in_kdb_printk())
  9942. locked = spin_trylock_irqsave(&port->lock, flags);
  9943. else
  9944. spin_lock_irqsave(&port->lock, flags);
  9945. diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
  9946. index 899a77187bde..3ff6363b3751 100644
  9947. --- a/drivers/tty/serial/amba-pl011.c
  9948. +++ b/drivers/tty/serial/amba-pl011.c
  9949. @@ -2067,13 +2067,19 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  9950. clk_enable(uap->clk);
  9951. - local_irq_save(flags);
  9952. + /*
  9953. + * local_irq_save(flags);
  9954. + *
  9955. + * This local_irq_save() is nonsense. If we come in via sysrq
  9956. + * handling then interrupts are already disabled. Aside of
  9957. + * that the port.sysrq check is racy on SMP regardless.
  9958. + */
  9959. if (uap->port.sysrq)
  9960. locked = 0;
  9961. else if (oops_in_progress)
  9962. - locked = spin_trylock(&uap->port.lock);
  9963. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  9964. else
  9965. - spin_lock(&uap->port.lock);
  9966. + spin_lock_irqsave(&uap->port.lock, flags);
  9967. /*
  9968. * First save the CR then disable the interrupts
  9969. @@ -2098,8 +2104,7 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  9970. writew(old_cr, uap->port.membase + UART011_CR);
  9971. if (locked)
  9972. - spin_unlock(&uap->port.lock);
  9973. - local_irq_restore(flags);
  9974. + spin_unlock_irqrestore(&uap->port.lock, flags);
  9975. clk_disable(uap->clk);
  9976. }
  9977. diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
  9978. index 24280d9a05e9..9745fb8b7abb 100644
  9979. --- a/drivers/tty/serial/omap-serial.c
  9980. +++ b/drivers/tty/serial/omap-serial.c
  9981. @@ -1257,13 +1257,10 @@ serial_omap_console_write(struct console *co, const char *s,
  9982. pm_runtime_get_sync(up->dev);
  9983. - local_irq_save(flags);
  9984. - if (up->port.sysrq)
  9985. - locked = 0;
  9986. - else if (oops_in_progress)
  9987. - locked = spin_trylock(&up->port.lock);
  9988. + if (up->port.sysrq || oops_in_progress)
  9989. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  9990. else
  9991. - spin_lock(&up->port.lock);
  9992. + spin_lock_irqsave(&up->port.lock, flags);
  9993. /*
  9994. * First save the IER then disable the interrupts
  9995. @@ -1292,8 +1289,7 @@ serial_omap_console_write(struct console *co, const char *s,
  9996. pm_runtime_mark_last_busy(up->dev);
  9997. pm_runtime_put_autosuspend(up->dev);
  9998. if (locked)
  9999. - spin_unlock(&up->port.lock);
  10000. - local_irq_restore(flags);
  10001. + spin_unlock_irqrestore(&up->port.lock, flags);
  10002. }
  10003. static int __init
  10004. diff --git a/drivers/tty/serial/sc16is7xx.c b/drivers/tty/serial/sc16is7xx.c
  10005. index edb5305b9d4d..7d5ee8a13ac6 100644
  10006. --- a/drivers/tty/serial/sc16is7xx.c
  10007. +++ b/drivers/tty/serial/sc16is7xx.c
  10008. @@ -1230,7 +1230,7 @@ static int sc16is7xx_probe(struct device *dev,
  10009. /* Setup interrupt */
  10010. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  10011. - IRQF_ONESHOT | flags, dev_name(dev), s);
  10012. + flags, dev_name(dev), s);
  10013. if (!ret)
  10014. return 0;
  10015. diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
  10016. index 1c102d60cd9f..a8f2f5bf2674 100644
  10017. --- a/drivers/usb/core/hcd.c
  10018. +++ b/drivers/usb/core/hcd.c
  10019. @@ -1735,9 +1735,9 @@ static void __usb_hcd_giveback_urb(struct urb *urb)
  10020. * and no one may trigger the above deadlock situation when
  10021. * running complete() in tasklet.
  10022. */
  10023. - local_irq_save(flags);
  10024. + local_irq_save_nort(flags);
  10025. urb->complete(urb);
  10026. - local_irq_restore(flags);
  10027. + local_irq_restore_nort(flags);
  10028. usb_anchor_resume_wakeups(anchor);
  10029. atomic_dec(&urb->use_count);
  10030. diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
  10031. index 803c503a2e3d..8dd2720aab64 100644
  10032. --- a/drivers/usb/gadget/function/f_fs.c
  10033. +++ b/drivers/usb/gadget/function/f_fs.c
  10034. @@ -1404,7 +1404,7 @@ static void ffs_data_put(struct ffs_data *ffs)
  10035. pr_info("%s(): freeing\n", __func__);
  10036. ffs_data_clear(ffs);
  10037. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  10038. - waitqueue_active(&ffs->ep0req_completion.wait));
  10039. + swait_active(&ffs->ep0req_completion.wait));
  10040. kfree(ffs->dev_name);
  10041. kfree(ffs);
  10042. }
  10043. diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
  10044. index 55386619a0f1..fb2791048cdf 100644
  10045. --- a/drivers/usb/gadget/legacy/inode.c
  10046. +++ b/drivers/usb/gadget/legacy/inode.c
  10047. @@ -345,7 +345,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10048. spin_unlock_irq (&epdata->dev->lock);
  10049. if (likely (value == 0)) {
  10050. - value = wait_event_interruptible (done.wait, done.done);
  10051. + value = swait_event_interruptible (done.wait, done.done);
  10052. if (value != 0) {
  10053. spin_lock_irq (&epdata->dev->lock);
  10054. if (likely (epdata->ep != NULL)) {
  10055. @@ -354,7 +354,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  10056. usb_ep_dequeue (epdata->ep, epdata->req);
  10057. spin_unlock_irq (&epdata->dev->lock);
  10058. - wait_event (done.wait, done.done);
  10059. + swait_event (done.wait, done.done);
  10060. if (epdata->status == -ECONNRESET)
  10061. epdata->status = -EINTR;
  10062. } else {
  10063. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10064. index f92f5aff0dd5..f9bba26e3655 100644
  10065. --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
  10066. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
  10067. @@ -17,7 +17,9 @@
  10068. #include <linux/device.h>
  10069. #include <linux/dma-mapping.h>
  10070. #include <linux/list.h>
  10071. +#include <linux/mfd/syscon.h>
  10072. #include <linux/platform_device.h>
  10073. +#include <linux/regmap.h>
  10074. #include <linux/usb/ch9.h>
  10075. #include <linux/usb/gadget.h>
  10076. #include <linux/usb/atmel_usba_udc.h>
  10077. @@ -1888,20 +1890,15 @@ static int atmel_usba_stop(struct usb_gadget *gadget)
  10078. #ifdef CONFIG_OF
  10079. static void at91sam9rl_toggle_bias(struct usba_udc *udc, int is_on)
  10080. {
  10081. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10082. -
  10083. - if (is_on)
  10084. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10085. - else
  10086. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10087. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10088. + is_on ? AT91_PMC_BIASEN : 0);
  10089. }
  10090. static void at91sam9g45_pulse_bias(struct usba_udc *udc)
  10091. {
  10092. - unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  10093. -
  10094. - at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
  10095. - at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
  10096. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN, 0);
  10097. + regmap_update_bits(udc->pmc, AT91_CKGR_UCKR, AT91_PMC_BIASEN,
  10098. + AT91_PMC_BIASEN);
  10099. }
  10100. static const struct usba_udc_errata at91sam9rl_errata = {
  10101. @@ -1938,6 +1935,9 @@ static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
  10102. return ERR_PTR(-EINVAL);
  10103. udc->errata = match->data;
  10104. + udc->pmc = syscon_regmap_lookup_by_compatible("atmel,at91sam9g45-pmc");
  10105. + if (udc->errata && IS_ERR(udc->pmc))
  10106. + return ERR_CAST(udc->pmc);
  10107. udc->num_ep = 0;
  10108. diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10109. index ea448a344767..3e1c9d589dfa 100644
  10110. --- a/drivers/usb/gadget/udc/atmel_usba_udc.h
  10111. +++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
  10112. @@ -354,6 +354,8 @@ struct usba_udc {
  10113. struct dentry *debugfs_root;
  10114. struct dentry *debugfs_regs;
  10115. #endif
  10116. +
  10117. + struct regmap *pmc;
  10118. };
  10119. static inline struct usba_ep *to_usba_ep(struct usb_ep *ep)
  10120. diff --git a/fs/aio.c b/fs/aio.c
  10121. index 155f84253f33..dd8d6f234a0b 100644
  10122. --- a/fs/aio.c
  10123. +++ b/fs/aio.c
  10124. @@ -40,6 +40,7 @@
  10125. #include <linux/ramfs.h>
  10126. #include <linux/percpu-refcount.h>
  10127. #include <linux/mount.h>
  10128. +#include <linux/swork.h>
  10129. #include <asm/kmap_types.h>
  10130. #include <asm/uaccess.h>
  10131. @@ -115,7 +116,7 @@ struct kioctx {
  10132. struct page **ring_pages;
  10133. long nr_pages;
  10134. - struct work_struct free_work;
  10135. + struct swork_event free_work;
  10136. /*
  10137. * signals when all in-flight requests are done
  10138. @@ -253,6 +254,7 @@ static int __init aio_setup(void)
  10139. .mount = aio_mount,
  10140. .kill_sb = kill_anon_super,
  10141. };
  10142. + BUG_ON(swork_get());
  10143. aio_mnt = kern_mount(&aio_fs);
  10144. if (IS_ERR(aio_mnt))
  10145. panic("Failed to create aio fs mount.");
  10146. @@ -568,9 +570,9 @@ static int kiocb_cancel(struct aio_kiocb *kiocb)
  10147. return cancel(&kiocb->common);
  10148. }
  10149. -static void free_ioctx(struct work_struct *work)
  10150. +static void free_ioctx(struct swork_event *sev)
  10151. {
  10152. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  10153. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10154. pr_debug("freeing %p\n", ctx);
  10155. @@ -589,8 +591,8 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10156. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  10157. complete(&ctx->rq_wait->comp);
  10158. - INIT_WORK(&ctx->free_work, free_ioctx);
  10159. - schedule_work(&ctx->free_work);
  10160. + INIT_SWORK(&ctx->free_work, free_ioctx);
  10161. + swork_queue(&ctx->free_work);
  10162. }
  10163. /*
  10164. @@ -598,9 +600,9 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  10165. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  10166. * now it's safe to cancel any that need to be.
  10167. */
  10168. -static void free_ioctx_users(struct percpu_ref *ref)
  10169. +static void free_ioctx_users_work(struct swork_event *sev)
  10170. {
  10171. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10172. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  10173. struct aio_kiocb *req;
  10174. spin_lock_irq(&ctx->ctx_lock);
  10175. @@ -619,6 +621,14 @@ static void free_ioctx_users(struct percpu_ref *ref)
  10176. percpu_ref_put(&ctx->reqs);
  10177. }
  10178. +static void free_ioctx_users(struct percpu_ref *ref)
  10179. +{
  10180. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  10181. +
  10182. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  10183. + swork_queue(&ctx->free_work);
  10184. +}
  10185. +
  10186. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  10187. {
  10188. unsigned i, new_nr;
  10189. diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
  10190. index c37149b929be..4c541347308e 100644
  10191. --- a/fs/autofs4/autofs_i.h
  10192. +++ b/fs/autofs4/autofs_i.h
  10193. @@ -34,6 +34,7 @@
  10194. #include <linux/sched.h>
  10195. #include <linux/mount.h>
  10196. #include <linux/namei.h>
  10197. +#include <linux/delay.h>
  10198. #include <asm/current.h>
  10199. #include <asm/uaccess.h>
  10200. diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
  10201. index 1cebc3c52fa5..d487fa27add5 100644
  10202. --- a/fs/autofs4/expire.c
  10203. +++ b/fs/autofs4/expire.c
  10204. @@ -150,7 +150,7 @@ again:
  10205. parent = p->d_parent;
  10206. if (!spin_trylock(&parent->d_lock)) {
  10207. spin_unlock(&p->d_lock);
  10208. - cpu_relax();
  10209. + cpu_chill();
  10210. goto relock;
  10211. }
  10212. spin_unlock(&p->d_lock);
  10213. diff --git a/fs/buffer.c b/fs/buffer.c
  10214. index 4f4cd959da7c..72b27e17b907 100644
  10215. --- a/fs/buffer.c
  10216. +++ b/fs/buffer.c
  10217. @@ -305,8 +305,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10218. * decide that the page is now completely done.
  10219. */
  10220. first = page_buffers(page);
  10221. - local_irq_save(flags);
  10222. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10223. + flags = bh_uptodate_lock_irqsave(first);
  10224. clear_buffer_async_read(bh);
  10225. unlock_buffer(bh);
  10226. tmp = bh;
  10227. @@ -319,8 +318,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10228. }
  10229. tmp = tmp->b_this_page;
  10230. } while (tmp != bh);
  10231. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10232. - local_irq_restore(flags);
  10233. + bh_uptodate_unlock_irqrestore(first, flags);
  10234. /*
  10235. * If none of the buffers had errors and they are all
  10236. @@ -332,9 +330,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10237. return;
  10238. still_busy:
  10239. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10240. - local_irq_restore(flags);
  10241. - return;
  10242. + bh_uptodate_unlock_irqrestore(first, flags);
  10243. }
  10244. /*
  10245. @@ -362,8 +358,7 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10246. }
  10247. first = page_buffers(page);
  10248. - local_irq_save(flags);
  10249. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10250. + flags = bh_uptodate_lock_irqsave(first);
  10251. clear_buffer_async_write(bh);
  10252. unlock_buffer(bh);
  10253. @@ -375,15 +370,12 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  10254. }
  10255. tmp = tmp->b_this_page;
  10256. }
  10257. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10258. - local_irq_restore(flags);
  10259. + bh_uptodate_unlock_irqrestore(first, flags);
  10260. end_page_writeback(page);
  10261. return;
  10262. still_busy:
  10263. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10264. - local_irq_restore(flags);
  10265. - return;
  10266. + bh_uptodate_unlock_irqrestore(first, flags);
  10267. }
  10268. EXPORT_SYMBOL(end_buffer_async_write);
  10269. @@ -3325,6 +3317,7 @@ struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  10270. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  10271. if (ret) {
  10272. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  10273. + buffer_head_init_locks(ret);
  10274. preempt_disable();
  10275. __this_cpu_inc(bh_accounting.nr);
  10276. recalc_bh_state();
  10277. diff --git a/fs/dcache.c b/fs/dcache.c
  10278. index 71b6056ad35d..76f007eb28f8 100644
  10279. --- a/fs/dcache.c
  10280. +++ b/fs/dcache.c
  10281. @@ -19,6 +19,7 @@
  10282. #include <linux/mm.h>
  10283. #include <linux/fs.h>
  10284. #include <linux/fsnotify.h>
  10285. +#include <linux/delay.h>
  10286. #include <linux/slab.h>
  10287. #include <linux/init.h>
  10288. #include <linux/hash.h>
  10289. @@ -785,7 +786,12 @@ repeat:
  10290. kill_it:
  10291. dentry = dentry_kill(dentry);
  10292. if (dentry) {
  10293. - cond_resched();
  10294. + int r;
  10295. +
  10296. + /* the task with the highest priority won't schedule */
  10297. + r = cond_resched();
  10298. + if (!r)
  10299. + cpu_chill();
  10300. goto repeat;
  10301. }
  10302. }
  10303. @@ -2394,7 +2400,7 @@ again:
  10304. if (dentry->d_lockref.count == 1) {
  10305. if (!spin_trylock(&inode->i_lock)) {
  10306. spin_unlock(&dentry->d_lock);
  10307. - cpu_relax();
  10308. + cpu_chill();
  10309. goto again;
  10310. }
  10311. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  10312. diff --git a/fs/eventpoll.c b/fs/eventpoll.c
  10313. index 1e009cad8d5c..d0c12504d3b4 100644
  10314. --- a/fs/eventpoll.c
  10315. +++ b/fs/eventpoll.c
  10316. @@ -505,12 +505,12 @@ static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  10317. */
  10318. static void ep_poll_safewake(wait_queue_head_t *wq)
  10319. {
  10320. - int this_cpu = get_cpu();
  10321. + int this_cpu = get_cpu_light();
  10322. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  10323. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  10324. - put_cpu();
  10325. + put_cpu_light();
  10326. }
  10327. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  10328. diff --git a/fs/exec.c b/fs/exec.c
  10329. index b06623a9347f..e7760b7b692c 100644
  10330. --- a/fs/exec.c
  10331. +++ b/fs/exec.c
  10332. @@ -865,12 +865,14 @@ static int exec_mmap(struct mm_struct *mm)
  10333. }
  10334. }
  10335. task_lock(tsk);
  10336. + preempt_disable_rt();
  10337. active_mm = tsk->active_mm;
  10338. tsk->mm = mm;
  10339. tsk->active_mm = mm;
  10340. activate_mm(active_mm, mm);
  10341. tsk->mm->vmacache_seqnum = 0;
  10342. vmacache_flush(tsk);
  10343. + preempt_enable_rt();
  10344. task_unlock(tsk);
  10345. if (old_mm) {
  10346. up_read(&old_mm->mmap_sem);
  10347. diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
  10348. index 9db5500d63d9..5951c495d124 100644
  10349. --- a/fs/f2fs/f2fs.h
  10350. +++ b/fs/f2fs/f2fs.h
  10351. @@ -24,7 +24,6 @@
  10352. #ifdef CONFIG_F2FS_CHECK_FS
  10353. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  10354. -#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  10355. #else
  10356. #define f2fs_bug_on(sbi, condition) \
  10357. do { \
  10358. @@ -33,7 +32,6 @@
  10359. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  10360. } \
  10361. } while (0)
  10362. -#define f2fs_down_write(x, y) down_write(x)
  10363. #endif
  10364. /*
  10365. @@ -959,7 +957,7 @@ static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  10366. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  10367. {
  10368. - f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  10369. + down_write(&sbi->cp_rwsem);
  10370. }
  10371. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  10372. diff --git a/fs/jbd2/checkpoint.c b/fs/jbd2/checkpoint.c
  10373. index 684996c8a3a4..6e18a06aaabe 100644
  10374. --- a/fs/jbd2/checkpoint.c
  10375. +++ b/fs/jbd2/checkpoint.c
  10376. @@ -116,6 +116,8 @@ void __jbd2_log_wait_for_space(journal_t *journal)
  10377. nblocks = jbd2_space_needed(journal);
  10378. while (jbd2_log_space_left(journal) < nblocks) {
  10379. write_unlock(&journal->j_state_lock);
  10380. + if (current->plug)
  10381. + io_schedule();
  10382. mutex_lock(&journal->j_checkpoint_mutex);
  10383. /*
  10384. diff --git a/fs/namespace.c b/fs/namespace.c
  10385. index 5be02a0635be..1f3725bbd04b 100644
  10386. --- a/fs/namespace.c
  10387. +++ b/fs/namespace.c
  10388. @@ -14,6 +14,7 @@
  10389. #include <linux/mnt_namespace.h>
  10390. #include <linux/user_namespace.h>
  10391. #include <linux/namei.h>
  10392. +#include <linux/delay.h>
  10393. #include <linux/security.h>
  10394. #include <linux/idr.h>
  10395. #include <linux/init.h> /* init_rootfs */
  10396. @@ -353,8 +354,11 @@ int __mnt_want_write(struct vfsmount *m)
  10397. * incremented count after it has set MNT_WRITE_HOLD.
  10398. */
  10399. smp_mb();
  10400. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  10401. - cpu_relax();
  10402. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  10403. + preempt_enable();
  10404. + cpu_chill();
  10405. + preempt_disable();
  10406. + }
  10407. /*
  10408. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  10409. * be set to match its requirements. So we must not load that until
  10410. diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
  10411. index 7521e11db728..f0de4b6b8bf3 100644
  10412. --- a/fs/ntfs/aops.c
  10413. +++ b/fs/ntfs/aops.c
  10414. @@ -107,8 +107,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10415. "0x%llx.", (unsigned long long)bh->b_blocknr);
  10416. }
  10417. first = page_buffers(page);
  10418. - local_irq_save(flags);
  10419. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  10420. + flags = bh_uptodate_lock_irqsave(first);
  10421. clear_buffer_async_read(bh);
  10422. unlock_buffer(bh);
  10423. tmp = bh;
  10424. @@ -123,8 +122,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10425. }
  10426. tmp = tmp->b_this_page;
  10427. } while (tmp != bh);
  10428. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10429. - local_irq_restore(flags);
  10430. + bh_uptodate_unlock_irqrestore(first, flags);
  10431. /*
  10432. * If none of the buffers had errors then we can set the page uptodate,
  10433. * but we first have to perform the post read mst fixups, if the
  10434. @@ -145,13 +143,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10435. recs = PAGE_CACHE_SIZE / rec_size;
  10436. /* Should have been verified before we got here... */
  10437. BUG_ON(!recs);
  10438. - local_irq_save(flags);
  10439. + local_irq_save_nort(flags);
  10440. kaddr = kmap_atomic(page);
  10441. for (i = 0; i < recs; i++)
  10442. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  10443. i * rec_size), rec_size);
  10444. kunmap_atomic(kaddr);
  10445. - local_irq_restore(flags);
  10446. + local_irq_restore_nort(flags);
  10447. flush_dcache_page(page);
  10448. if (likely(page_uptodate && !PageError(page)))
  10449. SetPageUptodate(page);
  10450. @@ -159,9 +157,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  10451. unlock_page(page);
  10452. return;
  10453. still_busy:
  10454. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  10455. - local_irq_restore(flags);
  10456. - return;
  10457. + bh_uptodate_unlock_irqrestore(first, flags);
  10458. }
  10459. /**
  10460. diff --git a/fs/timerfd.c b/fs/timerfd.c
  10461. index 053818dd6c18..c4bc14fe0085 100644
  10462. --- a/fs/timerfd.c
  10463. +++ b/fs/timerfd.c
  10464. @@ -450,7 +450,10 @@ static int do_timerfd_settime(int ufd, int flags,
  10465. break;
  10466. }
  10467. spin_unlock_irq(&ctx->wqh.lock);
  10468. - cpu_relax();
  10469. + if (isalarm(ctx))
  10470. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  10471. + else
  10472. + hrtimer_wait_for_timer(&ctx->t.tmr);
  10473. }
  10474. /*
  10475. diff --git a/include/acpi/platform/aclinux.h b/include/acpi/platform/aclinux.h
  10476. index 323e5daece54..cc5fbd534fd4 100644
  10477. --- a/include/acpi/platform/aclinux.h
  10478. +++ b/include/acpi/platform/aclinux.h
  10479. @@ -127,6 +127,7 @@
  10480. #define acpi_cache_t struct kmem_cache
  10481. #define acpi_spinlock spinlock_t *
  10482. +#define acpi_raw_spinlock raw_spinlock_t *
  10483. #define acpi_cpu_flags unsigned long
  10484. /* Use native linux version of acpi_os_allocate_zeroed */
  10485. @@ -145,6 +146,20 @@
  10486. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  10487. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  10488. +#define acpi_os_create_raw_lock(__handle) \
  10489. +({ \
  10490. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  10491. + \
  10492. + if (lock) { \
  10493. + *(__handle) = lock; \
  10494. + raw_spin_lock_init(*(__handle)); \
  10495. + } \
  10496. + lock ? AE_OK : AE_NO_MEMORY; \
  10497. + })
  10498. +
  10499. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  10500. +
  10501. +
  10502. /*
  10503. * OSL interfaces used by debugger/disassembler
  10504. */
  10505. diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
  10506. index 630dd2372238..850e4d993a88 100644
  10507. --- a/include/asm-generic/bug.h
  10508. +++ b/include/asm-generic/bug.h
  10509. @@ -206,6 +206,20 @@ extern void warn_slowpath_null(const char *file, const int line);
  10510. # define WARN_ON_SMP(x) ({0;})
  10511. #endif
  10512. +#ifdef CONFIG_PREEMPT_RT_BASE
  10513. +# define BUG_ON_RT(c) BUG_ON(c)
  10514. +# define BUG_ON_NONRT(c) do { } while (0)
  10515. +# define WARN_ON_RT(condition) WARN_ON(condition)
  10516. +# define WARN_ON_NONRT(condition) do { } while (0)
  10517. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  10518. +#else
  10519. +# define BUG_ON_RT(c) do { } while (0)
  10520. +# define BUG_ON_NONRT(c) BUG_ON(c)
  10521. +# define WARN_ON_RT(condition) do { } while (0)
  10522. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  10523. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  10524. +#endif
  10525. +
  10526. #endif /* __ASSEMBLY__ */
  10527. #endif
  10528. diff --git a/include/asm-generic/preempt.h b/include/asm-generic/preempt.h
  10529. index 5d8ffa3e6f8c..c1cde3577551 100644
  10530. --- a/include/asm-generic/preempt.h
  10531. +++ b/include/asm-generic/preempt.h
  10532. @@ -7,10 +7,10 @@
  10533. static __always_inline int preempt_count(void)
  10534. {
  10535. - return current_thread_info()->preempt_count;
  10536. + return READ_ONCE(current_thread_info()->preempt_count);
  10537. }
  10538. -static __always_inline int *preempt_count_ptr(void)
  10539. +static __always_inline volatile int *preempt_count_ptr(void)
  10540. {
  10541. return &current_thread_info()->preempt_count;
  10542. }
  10543. diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
  10544. index daf17d70aeca..463df8954255 100644
  10545. --- a/include/linux/blk-mq.h
  10546. +++ b/include/linux/blk-mq.h
  10547. @@ -212,6 +212,7 @@ static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  10548. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  10549. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  10550. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  10551. int blk_mq_request_started(struct request *rq);
  10552. void blk_mq_start_request(struct request *rq);
  10553. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
  10554. index 168755791ec8..a8f18e02b73e 100644
  10555. --- a/include/linux/blkdev.h
  10556. +++ b/include/linux/blkdev.h
  10557. @@ -89,6 +89,7 @@ struct request {
  10558. struct list_head queuelist;
  10559. union {
  10560. struct call_single_data csd;
  10561. + struct work_struct work;
  10562. unsigned long fifo_time;
  10563. };
  10564. @@ -455,7 +456,7 @@ struct request_queue {
  10565. struct throtl_data *td;
  10566. #endif
  10567. struct rcu_head rcu_head;
  10568. - wait_queue_head_t mq_freeze_wq;
  10569. + struct swait_queue_head mq_freeze_wq;
  10570. struct percpu_ref q_usage_counter;
  10571. struct list_head all_q_node;
  10572. diff --git a/include/linux/bottom_half.h b/include/linux/bottom_half.h
  10573. index 8fdcb783197d..d07dbeec7bc1 100644
  10574. --- a/include/linux/bottom_half.h
  10575. +++ b/include/linux/bottom_half.h
  10576. @@ -3,6 +3,39 @@
  10577. #include <linux/preempt.h>
  10578. +#ifdef CONFIG_PREEMPT_RT_FULL
  10579. +
  10580. +extern void __local_bh_disable(void);
  10581. +extern void _local_bh_enable(void);
  10582. +extern void __local_bh_enable(void);
  10583. +
  10584. +static inline void local_bh_disable(void)
  10585. +{
  10586. + __local_bh_disable();
  10587. +}
  10588. +
  10589. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  10590. +{
  10591. + __local_bh_disable();
  10592. +}
  10593. +
  10594. +static inline void local_bh_enable(void)
  10595. +{
  10596. + __local_bh_enable();
  10597. +}
  10598. +
  10599. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  10600. +{
  10601. + __local_bh_enable();
  10602. +}
  10603. +
  10604. +static inline void local_bh_enable_ip(unsigned long ip)
  10605. +{
  10606. + __local_bh_enable();
  10607. +}
  10608. +
  10609. +#else
  10610. +
  10611. #ifdef CONFIG_TRACE_IRQFLAGS
  10612. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  10613. #else
  10614. @@ -30,5 +63,6 @@ static inline void local_bh_enable(void)
  10615. {
  10616. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  10617. }
  10618. +#endif
  10619. #endif /* _LINUX_BH_H */
  10620. diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
  10621. index 89d9aa9e79bf..4a201008b02d 100644
  10622. --- a/include/linux/buffer_head.h
  10623. +++ b/include/linux/buffer_head.h
  10624. @@ -75,8 +75,50 @@ struct buffer_head {
  10625. struct address_space *b_assoc_map; /* mapping this buffer is
  10626. associated with */
  10627. atomic_t b_count; /* users using this buffer_head */
  10628. +#ifdef CONFIG_PREEMPT_RT_BASE
  10629. + spinlock_t b_uptodate_lock;
  10630. +#if IS_ENABLED(CONFIG_JBD2)
  10631. + spinlock_t b_state_lock;
  10632. + spinlock_t b_journal_head_lock;
  10633. +#endif
  10634. +#endif
  10635. };
  10636. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  10637. +{
  10638. + unsigned long flags;
  10639. +
  10640. +#ifndef CONFIG_PREEMPT_RT_BASE
  10641. + local_irq_save(flags);
  10642. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  10643. +#else
  10644. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  10645. +#endif
  10646. + return flags;
  10647. +}
  10648. +
  10649. +static inline void
  10650. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  10651. +{
  10652. +#ifndef CONFIG_PREEMPT_RT_BASE
  10653. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  10654. + local_irq_restore(flags);
  10655. +#else
  10656. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  10657. +#endif
  10658. +}
  10659. +
  10660. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  10661. +{
  10662. +#ifdef CONFIG_PREEMPT_RT_BASE
  10663. + spin_lock_init(&bh->b_uptodate_lock);
  10664. +#if IS_ENABLED(CONFIG_JBD2)
  10665. + spin_lock_init(&bh->b_state_lock);
  10666. + spin_lock_init(&bh->b_journal_head_lock);
  10667. +#endif
  10668. +#endif
  10669. +}
  10670. +
  10671. /*
  10672. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  10673. * and buffer_foo() functions.
  10674. diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
  10675. index 8da263299754..0cc474291e08 100644
  10676. --- a/include/linux/cgroup-defs.h
  10677. +++ b/include/linux/cgroup-defs.h
  10678. @@ -16,6 +16,7 @@
  10679. #include <linux/percpu-refcount.h>
  10680. #include <linux/percpu-rwsem.h>
  10681. #include <linux/workqueue.h>
  10682. +#include <linux/swork.h>
  10683. #ifdef CONFIG_CGROUPS
  10684. @@ -142,6 +143,7 @@ struct cgroup_subsys_state {
  10685. /* percpu_ref killing and RCU release */
  10686. struct rcu_head rcu_head;
  10687. struct work_struct destroy_work;
  10688. + struct swork_event destroy_swork;
  10689. };
  10690. /*
  10691. diff --git a/include/linux/clk/at91_pmc.h b/include/linux/clk/at91_pmc.h
  10692. index 1e6932222e11..17f413bbbedf 100644
  10693. --- a/include/linux/clk/at91_pmc.h
  10694. +++ b/include/linux/clk/at91_pmc.h
  10695. @@ -16,18 +16,6 @@
  10696. #ifndef AT91_PMC_H
  10697. #define AT91_PMC_H
  10698. -#ifndef __ASSEMBLY__
  10699. -extern void __iomem *at91_pmc_base;
  10700. -
  10701. -#define at91_pmc_read(field) \
  10702. - readl_relaxed(at91_pmc_base + field)
  10703. -
  10704. -#define at91_pmc_write(field, value) \
  10705. - writel_relaxed(value, at91_pmc_base + field)
  10706. -#else
  10707. -.extern at91_pmc_base
  10708. -#endif
  10709. -
  10710. #define AT91_PMC_SCER 0x00 /* System Clock Enable Register */
  10711. #define AT91_PMC_SCDR 0x04 /* System Clock Disable Register */
  10712. diff --git a/include/linux/completion.h b/include/linux/completion.h
  10713. index 5d5aaae3af43..3bca1590e29f 100644
  10714. --- a/include/linux/completion.h
  10715. +++ b/include/linux/completion.h
  10716. @@ -7,8 +7,7 @@
  10717. * Atomic wait-for-completion handler data structures.
  10718. * See kernel/sched/completion.c for details.
  10719. */
  10720. -
  10721. -#include <linux/wait.h>
  10722. +#include <linux/swait.h>
  10723. /*
  10724. * struct completion - structure used to maintain state for a "completion"
  10725. @@ -24,11 +23,11 @@
  10726. */
  10727. struct completion {
  10728. unsigned int done;
  10729. - wait_queue_head_t wait;
  10730. + struct swait_queue_head wait;
  10731. };
  10732. #define COMPLETION_INITIALIZER(work) \
  10733. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10734. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  10735. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  10736. ({ init_completion(&work); work; })
  10737. @@ -73,7 +72,7 @@ struct completion {
  10738. static inline void init_completion(struct completion *x)
  10739. {
  10740. x->done = 0;
  10741. - init_waitqueue_head(&x->wait);
  10742. + init_swait_queue_head(&x->wait);
  10743. }
  10744. /**
  10745. diff --git a/include/linux/cpu.h b/include/linux/cpu.h
  10746. index d2ca8c38f9c4..94041d803d0b 100644
  10747. --- a/include/linux/cpu.h
  10748. +++ b/include/linux/cpu.h
  10749. @@ -231,6 +231,8 @@ extern void get_online_cpus(void);
  10750. extern void put_online_cpus(void);
  10751. extern void cpu_hotplug_disable(void);
  10752. extern void cpu_hotplug_enable(void);
  10753. +extern void pin_current_cpu(void);
  10754. +extern void unpin_current_cpu(void);
  10755. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  10756. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  10757. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  10758. @@ -248,6 +250,8 @@ static inline void cpu_hotplug_done(void) {}
  10759. #define put_online_cpus() do { } while (0)
  10760. #define cpu_hotplug_disable() do { } while (0)
  10761. #define cpu_hotplug_enable() do { } while (0)
  10762. +static inline void pin_current_cpu(void) { }
  10763. +static inline void unpin_current_cpu(void) { }
  10764. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10765. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  10766. /* These aren't inline functions due to a GCC bug. */
  10767. diff --git a/include/linux/delay.h b/include/linux/delay.h
  10768. index a6ecb34cf547..37caab306336 100644
  10769. --- a/include/linux/delay.h
  10770. +++ b/include/linux/delay.h
  10771. @@ -52,4 +52,10 @@ static inline void ssleep(unsigned int seconds)
  10772. msleep(seconds * 1000);
  10773. }
  10774. +#ifdef CONFIG_PREEMPT_RT_FULL
  10775. +extern void cpu_chill(void);
  10776. +#else
  10777. +# define cpu_chill() cpu_relax()
  10778. +#endif
  10779. +
  10780. #endif /* defined(_LINUX_DELAY_H) */
  10781. diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
  10782. index 60048c50404e..f2cd67624f18 100644
  10783. --- a/include/linux/ftrace.h
  10784. +++ b/include/linux/ftrace.h
  10785. @@ -694,6 +694,18 @@ static inline void __ftrace_enabled_restore(int enabled)
  10786. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  10787. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  10788. +static inline unsigned long get_lock_parent_ip(void)
  10789. +{
  10790. + unsigned long addr = CALLER_ADDR0;
  10791. +
  10792. + if (!in_lock_functions(addr))
  10793. + return addr;
  10794. + addr = CALLER_ADDR1;
  10795. + if (!in_lock_functions(addr))
  10796. + return addr;
  10797. + return CALLER_ADDR2;
  10798. +}
  10799. +
  10800. #ifdef CONFIG_IRQSOFF_TRACER
  10801. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  10802. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  10803. diff --git a/include/linux/highmem.h b/include/linux/highmem.h
  10804. index bb3f3297062a..a117a33ef72c 100644
  10805. --- a/include/linux/highmem.h
  10806. +++ b/include/linux/highmem.h
  10807. @@ -7,6 +7,7 @@
  10808. #include <linux/mm.h>
  10809. #include <linux/uaccess.h>
  10810. #include <linux/hardirq.h>
  10811. +#include <linux/sched.h>
  10812. #include <asm/cacheflush.h>
  10813. @@ -65,7 +66,7 @@ static inline void kunmap(struct page *page)
  10814. static inline void *kmap_atomic(struct page *page)
  10815. {
  10816. - preempt_disable();
  10817. + preempt_disable_nort();
  10818. pagefault_disable();
  10819. return page_address(page);
  10820. }
  10821. @@ -74,7 +75,7 @@ static inline void *kmap_atomic(struct page *page)
  10822. static inline void __kunmap_atomic(void *addr)
  10823. {
  10824. pagefault_enable();
  10825. - preempt_enable();
  10826. + preempt_enable_nort();
  10827. }
  10828. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  10829. @@ -86,32 +87,51 @@ static inline void __kunmap_atomic(void *addr)
  10830. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  10831. +#ifndef CONFIG_PREEMPT_RT_FULL
  10832. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  10833. +#endif
  10834. static inline int kmap_atomic_idx_push(void)
  10835. {
  10836. +#ifndef CONFIG_PREEMPT_RT_FULL
  10837. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  10838. -#ifdef CONFIG_DEBUG_HIGHMEM
  10839. +# ifdef CONFIG_DEBUG_HIGHMEM
  10840. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  10841. BUG_ON(idx >= KM_TYPE_NR);
  10842. -#endif
  10843. +# endif
  10844. return idx;
  10845. +#else
  10846. + current->kmap_idx++;
  10847. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  10848. + return current->kmap_idx - 1;
  10849. +#endif
  10850. }
  10851. static inline int kmap_atomic_idx(void)
  10852. {
  10853. +#ifndef CONFIG_PREEMPT_RT_FULL
  10854. return __this_cpu_read(__kmap_atomic_idx) - 1;
  10855. +#else
  10856. + return current->kmap_idx - 1;
  10857. +#endif
  10858. }
  10859. static inline void kmap_atomic_idx_pop(void)
  10860. {
  10861. -#ifdef CONFIG_DEBUG_HIGHMEM
  10862. +#ifndef CONFIG_PREEMPT_RT_FULL
  10863. +# ifdef CONFIG_DEBUG_HIGHMEM
  10864. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  10865. BUG_ON(idx < 0);
  10866. -#else
  10867. +# else
  10868. __this_cpu_dec(__kmap_atomic_idx);
  10869. +# endif
  10870. +#else
  10871. + current->kmap_idx--;
  10872. +# ifdef CONFIG_DEBUG_HIGHMEM
  10873. + BUG_ON(current->kmap_idx < 0);
  10874. +# endif
  10875. #endif
  10876. }
  10877. diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
  10878. index 2ead22dd74a0..8fbcdfa5dc77 100644
  10879. --- a/include/linux/hrtimer.h
  10880. +++ b/include/linux/hrtimer.h
  10881. @@ -87,6 +87,9 @@ enum hrtimer_restart {
  10882. * @function: timer expiry callback function
  10883. * @base: pointer to the timer base (per cpu and per clock)
  10884. * @state: state information (See bit values above)
  10885. + * @cb_entry: list entry to defer timers from hardirq context
  10886. + * @irqsafe: timer can run in hardirq context
  10887. + * @praecox: timer expiry time if expired at the time of programming
  10888. * @is_rel: Set if the timer was armed relative
  10889. * @start_pid: timer statistics field to store the pid of the task which
  10890. * started the timer
  10891. @@ -103,6 +106,11 @@ struct hrtimer {
  10892. enum hrtimer_restart (*function)(struct hrtimer *);
  10893. struct hrtimer_clock_base *base;
  10894. u8 state;
  10895. + struct list_head cb_entry;
  10896. + int irqsafe;
  10897. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  10898. + ktime_t praecox;
  10899. +#endif
  10900. u8 is_rel;
  10901. #ifdef CONFIG_TIMER_STATS
  10902. int start_pid;
  10903. @@ -123,11 +131,7 @@ struct hrtimer_sleeper {
  10904. struct task_struct *task;
  10905. };
  10906. -#ifdef CONFIG_64BIT
  10907. # define HRTIMER_CLOCK_BASE_ALIGN 64
  10908. -#else
  10909. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  10910. -#endif
  10911. /**
  10912. * struct hrtimer_clock_base - the timer base for a specific clock
  10913. @@ -136,6 +140,7 @@ struct hrtimer_sleeper {
  10914. * timer to a base on another cpu.
  10915. * @clockid: clock id for per_cpu support
  10916. * @active: red black tree root node for the active timers
  10917. + * @expired: list head for deferred timers.
  10918. * @get_time: function to retrieve the current time of the clock
  10919. * @offset: offset of this clock to the monotonic base
  10920. */
  10921. @@ -144,6 +149,7 @@ struct hrtimer_clock_base {
  10922. int index;
  10923. clockid_t clockid;
  10924. struct timerqueue_head active;
  10925. + struct list_head expired;
  10926. ktime_t (*get_time)(void);
  10927. ktime_t offset;
  10928. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  10929. @@ -187,6 +193,7 @@ struct hrtimer_cpu_base {
  10930. raw_spinlock_t lock;
  10931. seqcount_t seq;
  10932. struct hrtimer *running;
  10933. + struct hrtimer *running_soft;
  10934. unsigned int cpu;
  10935. unsigned int active_bases;
  10936. unsigned int clock_was_set_seq;
  10937. @@ -203,6 +210,9 @@ struct hrtimer_cpu_base {
  10938. unsigned int nr_hangs;
  10939. unsigned int max_hang_time;
  10940. #endif
  10941. +#ifdef CONFIG_PREEMPT_RT_BASE
  10942. + wait_queue_head_t wait;
  10943. +#endif
  10944. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  10945. } ____cacheline_aligned;
  10946. @@ -412,6 +422,13 @@ static inline void hrtimer_restart(struct hrtimer *timer)
  10947. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  10948. }
  10949. +/* Softirq preemption could deadlock timer removal */
  10950. +#ifdef CONFIG_PREEMPT_RT_BASE
  10951. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  10952. +#else
  10953. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  10954. +#endif
  10955. +
  10956. /* Query timers: */
  10957. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  10958. @@ -436,7 +453,7 @@ static inline int hrtimer_is_queued(struct hrtimer *timer)
  10959. * Helper function to check, whether the timer is running the callback
  10960. * function
  10961. */
  10962. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  10963. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  10964. {
  10965. return timer->base->cpu_base->running == timer;
  10966. }
  10967. diff --git a/include/linux/idr.h b/include/linux/idr.h
  10968. index 013fd9bc4cb6..f62be0aec911 100644
  10969. --- a/include/linux/idr.h
  10970. +++ b/include/linux/idr.h
  10971. @@ -95,10 +95,14 @@ bool idr_is_empty(struct idr *idp);
  10972. * Each idr_preload() should be matched with an invocation of this
  10973. * function. See idr_preload() for details.
  10974. */
  10975. +#ifdef CONFIG_PREEMPT_RT_FULL
  10976. +void idr_preload_end(void);
  10977. +#else
  10978. static inline void idr_preload_end(void)
  10979. {
  10980. preempt_enable();
  10981. }
  10982. +#endif
  10983. /**
  10984. * idr_find - return pointer for given id
  10985. diff --git a/include/linux/init_task.h b/include/linux/init_task.h
  10986. index 1c1ff7e4faa4..60fadde71a44 100644
  10987. --- a/include/linux/init_task.h
  10988. +++ b/include/linux/init_task.h
  10989. @@ -148,9 +148,15 @@ extern struct task_group root_task_group;
  10990. # define INIT_PERF_EVENTS(tsk)
  10991. #endif
  10992. +#ifdef CONFIG_PREEMPT_RT_BASE
  10993. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  10994. +#else
  10995. +# define INIT_TIMER_LIST
  10996. +#endif
  10997. +
  10998. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  10999. # define INIT_VTIME(tsk) \
  11000. - .vtime_seqlock = __SEQLOCK_UNLOCKED(tsk.vtime_seqlock), \
  11001. + .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  11002. .vtime_snap = 0, \
  11003. .vtime_snap_whence = VTIME_SYS,
  11004. #else
  11005. @@ -239,6 +245,7 @@ extern struct task_group root_task_group;
  11006. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  11007. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  11008. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  11009. + INIT_TIMER_LIST \
  11010. .pids = { \
  11011. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  11012. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  11013. diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
  11014. index ad16809c8596..655cee096aed 100644
  11015. --- a/include/linux/interrupt.h
  11016. +++ b/include/linux/interrupt.h
  11017. @@ -61,6 +61,7 @@
  11018. * interrupt handler after suspending interrupts. For system
  11019. * wakeup devices users need to implement wakeup detection in
  11020. * their interrupt handlers.
  11021. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  11022. */
  11023. #define IRQF_SHARED 0x00000080
  11024. #define IRQF_PROBE_SHARED 0x00000100
  11025. @@ -74,6 +75,7 @@
  11026. #define IRQF_NO_THREAD 0x00010000
  11027. #define IRQF_EARLY_RESUME 0x00020000
  11028. #define IRQF_COND_SUSPEND 0x00040000
  11029. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  11030. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  11031. @@ -186,7 +188,7 @@ extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  11032. #ifdef CONFIG_LOCKDEP
  11033. # define local_irq_enable_in_hardirq() do { } while (0)
  11034. #else
  11035. -# define local_irq_enable_in_hardirq() local_irq_enable()
  11036. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  11037. #endif
  11038. extern void disable_irq_nosync(unsigned int irq);
  11039. @@ -206,6 +208,7 @@ extern void resume_device_irqs(void);
  11040. * @irq: Interrupt to which notification applies
  11041. * @kref: Reference count, for internal use
  11042. * @work: Work item, for internal use
  11043. + * @list: List item for deferred callbacks
  11044. * @notify: Function to be called on change. This will be
  11045. * called in process context.
  11046. * @release: Function to be called on release. This will be
  11047. @@ -217,6 +220,7 @@ struct irq_affinity_notify {
  11048. unsigned int irq;
  11049. struct kref kref;
  11050. struct work_struct work;
  11051. + struct list_head list;
  11052. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  11053. void (*release)(struct kref *ref);
  11054. };
  11055. @@ -379,9 +383,13 @@ extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  11056. bool state);
  11057. #ifdef CONFIG_IRQ_FORCED_THREADING
  11058. +# ifndef CONFIG_PREEMPT_RT_BASE
  11059. extern bool force_irqthreads;
  11060. +# else
  11061. +# define force_irqthreads (true)
  11062. +# endif
  11063. #else
  11064. -#define force_irqthreads (0)
  11065. +#define force_irqthreads (false)
  11066. #endif
  11067. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  11068. @@ -438,9 +446,10 @@ struct softirq_action
  11069. void (*action)(struct softirq_action *);
  11070. };
  11071. +#ifndef CONFIG_PREEMPT_RT_FULL
  11072. asmlinkage void do_softirq(void);
  11073. asmlinkage void __do_softirq(void);
  11074. -
  11075. +static inline void thread_do_softirq(void) { do_softirq(); }
  11076. #ifdef __ARCH_HAS_DO_SOFTIRQ
  11077. void do_softirq_own_stack(void);
  11078. #else
  11079. @@ -449,13 +458,25 @@ static inline void do_softirq_own_stack(void)
  11080. __do_softirq();
  11081. }
  11082. #endif
  11083. +#else
  11084. +extern void thread_do_softirq(void);
  11085. +#endif
  11086. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  11087. extern void softirq_init(void);
  11088. extern void __raise_softirq_irqoff(unsigned int nr);
  11089. +#ifdef CONFIG_PREEMPT_RT_FULL
  11090. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  11091. +#else
  11092. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  11093. +{
  11094. + __raise_softirq_irqoff(nr);
  11095. +}
  11096. +#endif
  11097. extern void raise_softirq_irqoff(unsigned int nr);
  11098. extern void raise_softirq(unsigned int nr);
  11099. +extern void softirq_check_pending_idle(void);
  11100. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  11101. @@ -477,8 +498,9 @@ static inline struct task_struct *this_cpu_ksoftirqd(void)
  11102. to be executed on some cpu at least once after this.
  11103. * If the tasklet is already scheduled, but its execution is still not
  11104. started, it will be executed only once.
  11105. - * If this tasklet is already running on another CPU (or schedule is called
  11106. - from tasklet itself), it is rescheduled for later.
  11107. + * If this tasklet is already running on another CPU, it is rescheduled
  11108. + for later.
  11109. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  11110. * Tasklet is strictly serialized wrt itself, but not
  11111. wrt another tasklets. If client needs some intertask synchronization,
  11112. he makes it with spinlocks.
  11113. @@ -503,27 +525,36 @@ struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
  11114. enum
  11115. {
  11116. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  11117. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  11118. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  11119. + TASKLET_STATE_PENDING /* Tasklet is pending */
  11120. };
  11121. -#ifdef CONFIG_SMP
  11122. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  11123. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  11124. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  11125. +
  11126. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  11127. static inline int tasklet_trylock(struct tasklet_struct *t)
  11128. {
  11129. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  11130. }
  11131. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  11132. +{
  11133. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  11134. +}
  11135. +
  11136. static inline void tasklet_unlock(struct tasklet_struct *t)
  11137. {
  11138. smp_mb__before_atomic();
  11139. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  11140. }
  11141. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  11142. -{
  11143. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  11144. -}
  11145. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  11146. +
  11147. #else
  11148. #define tasklet_trylock(t) 1
  11149. +#define tasklet_tryunlock(t) 1
  11150. #define tasklet_unlock_wait(t) do { } while (0)
  11151. #define tasklet_unlock(t) do { } while (0)
  11152. #endif
  11153. @@ -572,12 +603,7 @@ static inline void tasklet_disable(struct tasklet_struct *t)
  11154. smp_mb();
  11155. }
  11156. -static inline void tasklet_enable(struct tasklet_struct *t)
  11157. -{
  11158. - smp_mb__before_atomic();
  11159. - atomic_dec(&t->count);
  11160. -}
  11161. -
  11162. +extern void tasklet_enable(struct tasklet_struct *t);
  11163. extern void tasklet_kill(struct tasklet_struct *t);
  11164. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  11165. extern void tasklet_init(struct tasklet_struct *t,
  11166. @@ -608,6 +634,12 @@ void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
  11167. tasklet_kill(&ttimer->tasklet);
  11168. }
  11169. +#ifdef CONFIG_PREEMPT_RT_FULL
  11170. +extern void softirq_early_init(void);
  11171. +#else
  11172. +static inline void softirq_early_init(void) { }
  11173. +#endif
  11174. +
  11175. /*
  11176. * Autoprobing for irqs:
  11177. *
  11178. diff --git a/include/linux/irq.h b/include/linux/irq.h
  11179. index 3c1c96786248..311d3f061452 100644
  11180. --- a/include/linux/irq.h
  11181. +++ b/include/linux/irq.h
  11182. @@ -72,6 +72,7 @@ enum irqchip_irq_state;
  11183. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  11184. * it from the spurious interrupt detection
  11185. * mechanism and from core side polling.
  11186. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  11187. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  11188. */
  11189. enum {
  11190. @@ -99,13 +100,14 @@ enum {
  11191. IRQ_PER_CPU_DEVID = (1 << 17),
  11192. IRQ_IS_POLLED = (1 << 18),
  11193. IRQ_DISABLE_UNLAZY = (1 << 19),
  11194. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  11195. };
  11196. #define IRQF_MODIFY_MASK \
  11197. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  11198. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  11199. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  11200. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  11201. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  11202. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  11203. diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
  11204. index 47b9ebd4a74f..2543aab05daa 100644
  11205. --- a/include/linux/irq_work.h
  11206. +++ b/include/linux/irq_work.h
  11207. @@ -16,6 +16,7 @@
  11208. #define IRQ_WORK_BUSY 2UL
  11209. #define IRQ_WORK_FLAGS 3UL
  11210. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  11211. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  11212. struct irq_work {
  11213. unsigned long flags;
  11214. @@ -51,4 +52,10 @@ static inline bool irq_work_needs_cpu(void) { return false; }
  11215. static inline void irq_work_run(void) { }
  11216. #endif
  11217. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  11218. +void irq_work_tick_soft(void);
  11219. +#else
  11220. +static inline void irq_work_tick_soft(void) { }
  11221. +#endif
  11222. +
  11223. #endif /* _LINUX_IRQ_WORK_H */
  11224. diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
  11225. index a587a33363c7..ad57402a242d 100644
  11226. --- a/include/linux/irqdesc.h
  11227. +++ b/include/linux/irqdesc.h
  11228. @@ -61,6 +61,7 @@ struct irq_desc {
  11229. unsigned int irqs_unhandled;
  11230. atomic_t threads_handled;
  11231. int threads_handled_last;
  11232. + u64 random_ip;
  11233. raw_spinlock_t lock;
  11234. struct cpumask *percpu_enabled;
  11235. #ifdef CONFIG_SMP
  11236. diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
  11237. index 5dd1272d1ab2..9b77034f7c5e 100644
  11238. --- a/include/linux/irqflags.h
  11239. +++ b/include/linux/irqflags.h
  11240. @@ -25,8 +25,6 @@
  11241. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  11242. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  11243. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  11244. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11245. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11246. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  11247. #else
  11248. # define trace_hardirqs_on() do { } while (0)
  11249. @@ -39,9 +37,15 @@
  11250. # define trace_softirqs_enabled(p) 0
  11251. # define trace_hardirq_enter() do { } while (0)
  11252. # define trace_hardirq_exit() do { } while (0)
  11253. +# define INIT_TRACE_IRQFLAGS
  11254. +#endif
  11255. +
  11256. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  11257. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  11258. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  11259. +#else
  11260. # define lockdep_softirq_enter() do { } while (0)
  11261. # define lockdep_softirq_exit() do { } while (0)
  11262. -# define INIT_TRACE_IRQFLAGS
  11263. #endif
  11264. #if defined(CONFIG_IRQSOFF_TRACER) || \
  11265. @@ -148,4 +152,23 @@
  11266. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  11267. +/*
  11268. + * local_irq* variants depending on RT/!RT
  11269. + */
  11270. +#ifdef CONFIG_PREEMPT_RT_FULL
  11271. +# define local_irq_disable_nort() do { } while (0)
  11272. +# define local_irq_enable_nort() do { } while (0)
  11273. +# define local_irq_save_nort(flags) local_save_flags(flags)
  11274. +# define local_irq_restore_nort(flags) (void)(flags)
  11275. +# define local_irq_disable_rt() local_irq_disable()
  11276. +# define local_irq_enable_rt() local_irq_enable()
  11277. +#else
  11278. +# define local_irq_disable_nort() local_irq_disable()
  11279. +# define local_irq_enable_nort() local_irq_enable()
  11280. +# define local_irq_save_nort(flags) local_irq_save(flags)
  11281. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  11282. +# define local_irq_disable_rt() do { } while (0)
  11283. +# define local_irq_enable_rt() do { } while (0)
  11284. +#endif
  11285. +
  11286. #endif
  11287. diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
  11288. index 65407f6c9120..eb5aabe4e18c 100644
  11289. --- a/include/linux/jbd2.h
  11290. +++ b/include/linux/jbd2.h
  11291. @@ -352,32 +352,56 @@ static inline struct journal_head *bh2jh(struct buffer_head *bh)
  11292. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  11293. {
  11294. +#ifndef CONFIG_PREEMPT_RT_BASE
  11295. bit_spin_lock(BH_State, &bh->b_state);
  11296. +#else
  11297. + spin_lock(&bh->b_state_lock);
  11298. +#endif
  11299. }
  11300. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  11301. {
  11302. +#ifndef CONFIG_PREEMPT_RT_BASE
  11303. return bit_spin_trylock(BH_State, &bh->b_state);
  11304. +#else
  11305. + return spin_trylock(&bh->b_state_lock);
  11306. +#endif
  11307. }
  11308. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  11309. {
  11310. +#ifndef CONFIG_PREEMPT_RT_BASE
  11311. return bit_spin_is_locked(BH_State, &bh->b_state);
  11312. +#else
  11313. + return spin_is_locked(&bh->b_state_lock);
  11314. +#endif
  11315. }
  11316. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  11317. {
  11318. +#ifndef CONFIG_PREEMPT_RT_BASE
  11319. bit_spin_unlock(BH_State, &bh->b_state);
  11320. +#else
  11321. + spin_unlock(&bh->b_state_lock);
  11322. +#endif
  11323. }
  11324. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  11325. {
  11326. +#ifndef CONFIG_PREEMPT_RT_BASE
  11327. bit_spin_lock(BH_JournalHead, &bh->b_state);
  11328. +#else
  11329. + spin_lock(&bh->b_journal_head_lock);
  11330. +#endif
  11331. }
  11332. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  11333. {
  11334. +#ifndef CONFIG_PREEMPT_RT_BASE
  11335. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  11336. +#else
  11337. + spin_unlock(&bh->b_journal_head_lock);
  11338. +#endif
  11339. }
  11340. #define J_ASSERT(assert) BUG_ON(!(assert))
  11341. diff --git a/include/linux/kdb.h b/include/linux/kdb.h
  11342. index a19bcf9e762e..897495386446 100644
  11343. --- a/include/linux/kdb.h
  11344. +++ b/include/linux/kdb.h
  11345. @@ -167,6 +167,7 @@ extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
  11346. extern __printf(1, 2) int kdb_printf(const char *, ...);
  11347. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  11348. +#define in_kdb_printk() (kdb_trap_printk)
  11349. extern void kdb_init(int level);
  11350. /* Access to kdb specific polling devices */
  11351. @@ -201,6 +202,7 @@ extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
  11352. extern int kdb_unregister(char *);
  11353. #else /* ! CONFIG_KGDB_KDB */
  11354. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  11355. +#define in_kdb_printk() (0)
  11356. static inline void kdb_init(int level) {}
  11357. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  11358. char *help, short minlen) { return 0; }
  11359. diff --git a/include/linux/kernel.h b/include/linux/kernel.h
  11360. index 924853d33a13..c84b10d6527d 100644
  11361. --- a/include/linux/kernel.h
  11362. +++ b/include/linux/kernel.h
  11363. @@ -188,6 +188,9 @@ extern int _cond_resched(void);
  11364. */
  11365. # define might_sleep() \
  11366. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11367. +
  11368. +# define might_sleep_no_state_check() \
  11369. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  11370. # define sched_annotate_sleep() (current->task_state_change = 0)
  11371. #else
  11372. static inline void ___might_sleep(const char *file, int line,
  11373. @@ -195,6 +198,7 @@ extern int _cond_resched(void);
  11374. static inline void __might_sleep(const char *file, int line,
  11375. int preempt_offset) { }
  11376. # define might_sleep() do { might_resched(); } while (0)
  11377. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  11378. # define sched_annotate_sleep() do { } while (0)
  11379. #endif
  11380. @@ -255,6 +259,7 @@ extern long (*panic_blink)(int state);
  11381. __printf(1, 2)
  11382. void panic(const char *fmt, ...)
  11383. __noreturn __cold;
  11384. +void nmi_panic(struct pt_regs *regs, const char *msg);
  11385. extern void oops_enter(void);
  11386. extern void oops_exit(void);
  11387. void print_oops_end_marker(void);
  11388. @@ -446,6 +451,14 @@ extern int sysctl_panic_on_stackoverflow;
  11389. extern bool crash_kexec_post_notifiers;
  11390. /*
  11391. + * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
  11392. + * holds a CPU number which is executing panic() currently. A value of
  11393. + * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
  11394. + */
  11395. +extern atomic_t panic_cpu;
  11396. +#define PANIC_CPU_INVALID -1
  11397. +
  11398. +/*
  11399. * Only to be used by arch init code. If the user over-wrote the default
  11400. * CONFIG_PANIC_TIMEOUT, honor it.
  11401. */
  11402. @@ -473,6 +486,7 @@ extern enum system_states {
  11403. SYSTEM_HALT,
  11404. SYSTEM_POWER_OFF,
  11405. SYSTEM_RESTART,
  11406. + SYSTEM_SUSPEND,
  11407. } system_state;
  11408. #define TAINT_PROPRIETARY_MODULE 0
  11409. diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
  11410. index c923350ca20a..c690acc6900e 100644
  11411. --- a/include/linux/kvm_host.h
  11412. +++ b/include/linux/kvm_host.h
  11413. @@ -25,6 +25,7 @@
  11414. #include <linux/irqflags.h>
  11415. #include <linux/context_tracking.h>
  11416. #include <linux/irqbypass.h>
  11417. +#include <linux/swait.h>
  11418. #include <asm/signal.h>
  11419. #include <linux/kvm.h>
  11420. @@ -243,7 +244,7 @@ struct kvm_vcpu {
  11421. int fpu_active;
  11422. int guest_fpu_loaded, guest_xcr0_loaded;
  11423. unsigned char fpu_counter;
  11424. - wait_queue_head_t wq;
  11425. + struct swait_queue_head wq;
  11426. struct pid *pid;
  11427. int sigset_active;
  11428. sigset_t sigset;
  11429. @@ -794,7 +795,7 @@ static inline bool kvm_arch_has_assigned_device(struct kvm *kvm)
  11430. }
  11431. #endif
  11432. -static inline wait_queue_head_t *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11433. +static inline struct swait_queue_head *kvm_arch_vcpu_wq(struct kvm_vcpu *vcpu)
  11434. {
  11435. #ifdef __KVM_HAVE_ARCH_WQP
  11436. return vcpu->arch.wqp;
  11437. diff --git a/include/linux/lglock.h b/include/linux/lglock.h
  11438. index c92ebd100d9b..6f035f635d0e 100644
  11439. --- a/include/linux/lglock.h
  11440. +++ b/include/linux/lglock.h
  11441. @@ -34,13 +34,30 @@
  11442. #endif
  11443. struct lglock {
  11444. +#ifdef CONFIG_PREEMPT_RT_FULL
  11445. + struct rt_mutex __percpu *lock;
  11446. +#else
  11447. arch_spinlock_t __percpu *lock;
  11448. +#endif
  11449. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11450. struct lock_class_key lock_key;
  11451. struct lockdep_map lock_dep_map;
  11452. #endif
  11453. };
  11454. +#ifdef CONFIG_PREEMPT_RT_FULL
  11455. +# define DEFINE_LGLOCK(name) \
  11456. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11457. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11458. + struct lglock name = { .lock = &name ## _lock }
  11459. +
  11460. +# define DEFINE_STATIC_LGLOCK(name) \
  11461. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  11462. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  11463. + static struct lglock name = { .lock = &name ## _lock }
  11464. +
  11465. +#else
  11466. +
  11467. #define DEFINE_LGLOCK(name) \
  11468. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11469. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11470. @@ -50,6 +67,7 @@ struct lglock {
  11471. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  11472. = __ARCH_SPIN_LOCK_UNLOCKED; \
  11473. static struct lglock name = { .lock = &name ## _lock }
  11474. +#endif
  11475. void lg_lock_init(struct lglock *lg, char *name);
  11476. @@ -64,6 +82,12 @@ void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
  11477. void lg_global_lock(struct lglock *lg);
  11478. void lg_global_unlock(struct lglock *lg);
  11479. +#ifndef CONFIG_PREEMPT_RT_FULL
  11480. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  11481. +#else
  11482. +void lg_global_trylock_relax(struct lglock *lg);
  11483. +#endif
  11484. +
  11485. #else
  11486. /* When !CONFIG_SMP, map lglock to spinlock */
  11487. #define lglock spinlock
  11488. diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h
  11489. index 8132214e8efd..89ffaa7bd342 100644
  11490. --- a/include/linux/list_bl.h
  11491. +++ b/include/linux/list_bl.h
  11492. @@ -2,6 +2,7 @@
  11493. #define _LINUX_LIST_BL_H
  11494. #include <linux/list.h>
  11495. +#include <linux/spinlock.h>
  11496. #include <linux/bit_spinlock.h>
  11497. /*
  11498. @@ -32,13 +33,24 @@
  11499. struct hlist_bl_head {
  11500. struct hlist_bl_node *first;
  11501. +#ifdef CONFIG_PREEMPT_RT_BASE
  11502. + raw_spinlock_t lock;
  11503. +#endif
  11504. };
  11505. struct hlist_bl_node {
  11506. struct hlist_bl_node *next, **pprev;
  11507. };
  11508. -#define INIT_HLIST_BL_HEAD(ptr) \
  11509. - ((ptr)->first = NULL)
  11510. +
  11511. +#ifdef CONFIG_PREEMPT_RT_BASE
  11512. +#define INIT_HLIST_BL_HEAD(h) \
  11513. +do { \
  11514. + (h)->first = NULL; \
  11515. + raw_spin_lock_init(&(h)->lock); \
  11516. +} while (0)
  11517. +#else
  11518. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  11519. +#endif
  11520. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  11521. {
  11522. @@ -118,12 +130,26 @@ static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  11523. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  11524. {
  11525. +#ifndef CONFIG_PREEMPT_RT_BASE
  11526. bit_spin_lock(0, (unsigned long *)b);
  11527. +#else
  11528. + raw_spin_lock(&b->lock);
  11529. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11530. + __set_bit(0, (unsigned long *)b);
  11531. +#endif
  11532. +#endif
  11533. }
  11534. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  11535. {
  11536. +#ifndef CONFIG_PREEMPT_RT_BASE
  11537. __bit_spin_unlock(0, (unsigned long *)b);
  11538. +#else
  11539. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  11540. + __clear_bit(0, (unsigned long *)b);
  11541. +#endif
  11542. + raw_spin_unlock(&b->lock);
  11543. +#endif
  11544. }
  11545. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  11546. diff --git a/include/linux/locallock.h b/include/linux/locallock.h
  11547. new file mode 100644
  11548. index 000000000000..e572a3971631
  11549. --- /dev/null
  11550. +++ b/include/linux/locallock.h
  11551. @@ -0,0 +1,276 @@
  11552. +#ifndef _LINUX_LOCALLOCK_H
  11553. +#define _LINUX_LOCALLOCK_H
  11554. +
  11555. +#include <linux/percpu.h>
  11556. +#include <linux/spinlock.h>
  11557. +
  11558. +#ifdef CONFIG_PREEMPT_RT_BASE
  11559. +
  11560. +#ifdef CONFIG_DEBUG_SPINLOCK
  11561. +# define LL_WARN(cond) WARN_ON(cond)
  11562. +#else
  11563. +# define LL_WARN(cond) do { } while (0)
  11564. +#endif
  11565. +
  11566. +/*
  11567. + * per cpu lock based substitute for local_irq_*()
  11568. + */
  11569. +struct local_irq_lock {
  11570. + spinlock_t lock;
  11571. + struct task_struct *owner;
  11572. + int nestcnt;
  11573. + unsigned long flags;
  11574. +};
  11575. +
  11576. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  11577. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  11578. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  11579. +
  11580. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  11581. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  11582. +
  11583. +#define local_irq_lock_init(lvar) \
  11584. + do { \
  11585. + int __cpu; \
  11586. + for_each_possible_cpu(__cpu) \
  11587. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  11588. + } while (0)
  11589. +
  11590. +/*
  11591. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  11592. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  11593. + * already takes care of the migrate_disable/enable
  11594. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  11595. + */
  11596. +#ifdef CONFIG_PREEMPT_RT_FULL
  11597. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  11598. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  11599. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  11600. +#else
  11601. +# define spin_lock_local(lock) spin_lock(lock)
  11602. +# define spin_trylock_local(lock) spin_trylock(lock)
  11603. +# define spin_unlock_local(lock) spin_unlock(lock)
  11604. +#endif
  11605. +
  11606. +static inline void __local_lock(struct local_irq_lock *lv)
  11607. +{
  11608. + if (lv->owner != current) {
  11609. + spin_lock_local(&lv->lock);
  11610. + LL_WARN(lv->owner);
  11611. + LL_WARN(lv->nestcnt);
  11612. + lv->owner = current;
  11613. + }
  11614. + lv->nestcnt++;
  11615. +}
  11616. +
  11617. +#define local_lock(lvar) \
  11618. + do { __local_lock(&get_local_var(lvar)); } while (0)
  11619. +
  11620. +#define local_lock_on(lvar, cpu) \
  11621. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  11622. +
  11623. +static inline int __local_trylock(struct local_irq_lock *lv)
  11624. +{
  11625. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  11626. + LL_WARN(lv->owner);
  11627. + LL_WARN(lv->nestcnt);
  11628. + lv->owner = current;
  11629. + lv->nestcnt = 1;
  11630. + return 1;
  11631. + }
  11632. + return 0;
  11633. +}
  11634. +
  11635. +#define local_trylock(lvar) \
  11636. + ({ \
  11637. + int __locked; \
  11638. + __locked = __local_trylock(&get_local_var(lvar)); \
  11639. + if (!__locked) \
  11640. + put_local_var(lvar); \
  11641. + __locked; \
  11642. + })
  11643. +
  11644. +static inline void __local_unlock(struct local_irq_lock *lv)
  11645. +{
  11646. + LL_WARN(lv->nestcnt == 0);
  11647. + LL_WARN(lv->owner != current);
  11648. + if (--lv->nestcnt)
  11649. + return;
  11650. +
  11651. + lv->owner = NULL;
  11652. + spin_unlock_local(&lv->lock);
  11653. +}
  11654. +
  11655. +#define local_unlock(lvar) \
  11656. + do { \
  11657. + __local_unlock(this_cpu_ptr(&lvar)); \
  11658. + put_local_var(lvar); \
  11659. + } while (0)
  11660. +
  11661. +#define local_unlock_on(lvar, cpu) \
  11662. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  11663. +
  11664. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  11665. +{
  11666. + spin_lock_irqsave(&lv->lock, lv->flags);
  11667. + LL_WARN(lv->owner);
  11668. + LL_WARN(lv->nestcnt);
  11669. + lv->owner = current;
  11670. + lv->nestcnt = 1;
  11671. +}
  11672. +
  11673. +#define local_lock_irq(lvar) \
  11674. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  11675. +
  11676. +#define local_lock_irq_on(lvar, cpu) \
  11677. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  11678. +
  11679. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  11680. +{
  11681. + LL_WARN(!lv->nestcnt);
  11682. + LL_WARN(lv->owner != current);
  11683. + lv->owner = NULL;
  11684. + lv->nestcnt = 0;
  11685. + spin_unlock_irq(&lv->lock);
  11686. +}
  11687. +
  11688. +#define local_unlock_irq(lvar) \
  11689. + do { \
  11690. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  11691. + put_local_var(lvar); \
  11692. + } while (0)
  11693. +
  11694. +#define local_unlock_irq_on(lvar, cpu) \
  11695. + do { \
  11696. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  11697. + } while (0)
  11698. +
  11699. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  11700. +{
  11701. + if (lv->owner != current) {
  11702. + __local_lock_irq(lv);
  11703. + return 0;
  11704. + } else {
  11705. + lv->nestcnt++;
  11706. + return 1;
  11707. + }
  11708. +}
  11709. +
  11710. +#define local_lock_irqsave(lvar, _flags) \
  11711. + do { \
  11712. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  11713. + put_local_var(lvar); \
  11714. + _flags = __this_cpu_read(lvar.flags); \
  11715. + } while (0)
  11716. +
  11717. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  11718. + do { \
  11719. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  11720. + _flags = per_cpu(lvar, cpu).flags; \
  11721. + } while (0)
  11722. +
  11723. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  11724. + unsigned long flags)
  11725. +{
  11726. + LL_WARN(!lv->nestcnt);
  11727. + LL_WARN(lv->owner != current);
  11728. + if (--lv->nestcnt)
  11729. + return 0;
  11730. +
  11731. + lv->owner = NULL;
  11732. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  11733. + return 1;
  11734. +}
  11735. +
  11736. +#define local_unlock_irqrestore(lvar, flags) \
  11737. + do { \
  11738. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  11739. + put_local_var(lvar); \
  11740. + } while (0)
  11741. +
  11742. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  11743. + do { \
  11744. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  11745. + } while (0)
  11746. +
  11747. +#define local_spin_trylock_irq(lvar, lock) \
  11748. + ({ \
  11749. + int __locked; \
  11750. + local_lock_irq(lvar); \
  11751. + __locked = spin_trylock(lock); \
  11752. + if (!__locked) \
  11753. + local_unlock_irq(lvar); \
  11754. + __locked; \
  11755. + })
  11756. +
  11757. +#define local_spin_lock_irq(lvar, lock) \
  11758. + do { \
  11759. + local_lock_irq(lvar); \
  11760. + spin_lock(lock); \
  11761. + } while (0)
  11762. +
  11763. +#define local_spin_unlock_irq(lvar, lock) \
  11764. + do { \
  11765. + spin_unlock(lock); \
  11766. + local_unlock_irq(lvar); \
  11767. + } while (0)
  11768. +
  11769. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11770. + do { \
  11771. + local_lock_irqsave(lvar, flags); \
  11772. + spin_lock(lock); \
  11773. + } while (0)
  11774. +
  11775. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11776. + do { \
  11777. + spin_unlock(lock); \
  11778. + local_unlock_irqrestore(lvar, flags); \
  11779. + } while (0)
  11780. +
  11781. +#define get_locked_var(lvar, var) \
  11782. + (*({ \
  11783. + local_lock(lvar); \
  11784. + this_cpu_ptr(&var); \
  11785. + }))
  11786. +
  11787. +#define put_locked_var(lvar, var) local_unlock(lvar);
  11788. +
  11789. +#define local_lock_cpu(lvar) \
  11790. + ({ \
  11791. + local_lock(lvar); \
  11792. + smp_processor_id(); \
  11793. + })
  11794. +
  11795. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  11796. +
  11797. +#else /* PREEMPT_RT_BASE */
  11798. +
  11799. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  11800. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  11801. +
  11802. +static inline void local_irq_lock_init(int lvar) { }
  11803. +
  11804. +#define local_lock(lvar) preempt_disable()
  11805. +#define local_unlock(lvar) preempt_enable()
  11806. +#define local_lock_irq(lvar) local_irq_disable()
  11807. +#define local_unlock_irq(lvar) local_irq_enable()
  11808. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  11809. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  11810. +
  11811. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  11812. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  11813. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  11814. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  11815. + spin_lock_irqsave(lock, flags)
  11816. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  11817. + spin_unlock_irqrestore(lock, flags)
  11818. +
  11819. +#define get_locked_var(lvar, var) get_cpu_var(var)
  11820. +#define put_locked_var(lvar, var) put_cpu_var(var)
  11821. +
  11822. +#define local_lock_cpu(lvar) get_cpu()
  11823. +#define local_unlock_cpu(lvar) put_cpu()
  11824. +
  11825. +#endif
  11826. +
  11827. +#endif
  11828. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  11829. index f8d1492a114f..b238ebfbb4d6 100644
  11830. --- a/include/linux/mm_types.h
  11831. +++ b/include/linux/mm_types.h
  11832. @@ -11,6 +11,7 @@
  11833. #include <linux/completion.h>
  11834. #include <linux/cpumask.h>
  11835. #include <linux/uprobes.h>
  11836. +#include <linux/rcupdate.h>
  11837. #include <linux/page-flags-layout.h>
  11838. #include <asm/page.h>
  11839. #include <asm/mmu.h>
  11840. @@ -504,6 +505,9 @@ struct mm_struct {
  11841. bool tlb_flush_pending;
  11842. #endif
  11843. struct uprobes_state uprobes_state;
  11844. +#ifdef CONFIG_PREEMPT_RT_BASE
  11845. + struct rcu_head delayed_drop;
  11846. +#endif
  11847. #ifdef CONFIG_X86_INTEL_MPX
  11848. /* address of the bounds directory */
  11849. void __user *bd_addr;
  11850. diff --git a/include/linux/mutex.h b/include/linux/mutex.h
  11851. index 2cb7531e7d7a..b3fdfc820216 100644
  11852. --- a/include/linux/mutex.h
  11853. +++ b/include/linux/mutex.h
  11854. @@ -19,6 +19,17 @@
  11855. #include <asm/processor.h>
  11856. #include <linux/osq_lock.h>
  11857. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11858. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11859. + , .dep_map = { .name = #lockname }
  11860. +#else
  11861. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11862. +#endif
  11863. +
  11864. +#ifdef CONFIG_PREEMPT_RT_FULL
  11865. +# include <linux/mutex_rt.h>
  11866. +#else
  11867. +
  11868. /*
  11869. * Simple, straightforward mutexes with strict semantics:
  11870. *
  11871. @@ -99,13 +110,6 @@ do { \
  11872. static inline void mutex_destroy(struct mutex *lock) {}
  11873. #endif
  11874. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11875. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  11876. - , .dep_map = { .name = #lockname }
  11877. -#else
  11878. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  11879. -#endif
  11880. -
  11881. #define __MUTEX_INITIALIZER(lockname) \
  11882. { .count = ATOMIC_INIT(1) \
  11883. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  11884. @@ -173,6 +177,8 @@ extern int __must_check mutex_lock_killable(struct mutex *lock);
  11885. extern int mutex_trylock(struct mutex *lock);
  11886. extern void mutex_unlock(struct mutex *lock);
  11887. +#endif /* !PREEMPT_RT_FULL */
  11888. +
  11889. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  11890. #endif /* __LINUX_MUTEX_H */
  11891. diff --git a/include/linux/mutex_rt.h b/include/linux/mutex_rt.h
  11892. new file mode 100644
  11893. index 000000000000..c38a44b14da5
  11894. --- /dev/null
  11895. +++ b/include/linux/mutex_rt.h
  11896. @@ -0,0 +1,84 @@
  11897. +#ifndef __LINUX_MUTEX_RT_H
  11898. +#define __LINUX_MUTEX_RT_H
  11899. +
  11900. +#ifndef __LINUX_MUTEX_H
  11901. +#error "Please include mutex.h"
  11902. +#endif
  11903. +
  11904. +#include <linux/rtmutex.h>
  11905. +
  11906. +/* FIXME: Just for __lockfunc */
  11907. +#include <linux/spinlock.h>
  11908. +
  11909. +struct mutex {
  11910. + struct rt_mutex lock;
  11911. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11912. + struct lockdep_map dep_map;
  11913. +#endif
  11914. +};
  11915. +
  11916. +#define __MUTEX_INITIALIZER(mutexname) \
  11917. + { \
  11918. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  11919. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  11920. + }
  11921. +
  11922. +#define DEFINE_MUTEX(mutexname) \
  11923. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  11924. +
  11925. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  11926. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  11927. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  11928. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  11929. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  11930. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  11931. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  11932. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  11933. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  11934. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  11935. +
  11936. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  11937. +#define mutex_lock(l) _mutex_lock(l)
  11938. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  11939. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  11940. +#define mutex_trylock(l) _mutex_trylock(l)
  11941. +#define mutex_unlock(l) _mutex_unlock(l)
  11942. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  11943. +
  11944. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  11945. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  11946. +# define mutex_lock_interruptible_nested(l, s) \
  11947. + _mutex_lock_interruptible_nested(l, s)
  11948. +# define mutex_lock_killable_nested(l, s) \
  11949. + _mutex_lock_killable_nested(l, s)
  11950. +
  11951. +# define mutex_lock_nest_lock(lock, nest_lock) \
  11952. +do { \
  11953. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  11954. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  11955. +} while (0)
  11956. +
  11957. +#else
  11958. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  11959. +# define mutex_lock_interruptible_nested(l, s) \
  11960. + _mutex_lock_interruptible(l)
  11961. +# define mutex_lock_killable_nested(l, s) \
  11962. + _mutex_lock_killable(l)
  11963. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  11964. +#endif
  11965. +
  11966. +# define mutex_init(mutex) \
  11967. +do { \
  11968. + static struct lock_class_key __key; \
  11969. + \
  11970. + rt_mutex_init(&(mutex)->lock); \
  11971. + __mutex_do_init((mutex), #mutex, &__key); \
  11972. +} while (0)
  11973. +
  11974. +# define __mutex_init(mutex, name, key) \
  11975. +do { \
  11976. + rt_mutex_init(&(mutex)->lock); \
  11977. + __mutex_do_init((mutex), name, key); \
  11978. +} while (0)
  11979. +
  11980. +#endif
  11981. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  11982. index 04c068e55353..530529e820f3 100644
  11983. --- a/include/linux/netdevice.h
  11984. +++ b/include/linux/netdevice.h
  11985. @@ -2249,11 +2249,20 @@ void netdev_freemem(struct net_device *dev);
  11986. void synchronize_net(void);
  11987. int init_dummy_netdev(struct net_device *dev);
  11988. +#ifdef CONFIG_PREEMPT_RT_FULL
  11989. +static inline int dev_recursion_level(void)
  11990. +{
  11991. + return current->xmit_recursion;
  11992. +}
  11993. +
  11994. +#else
  11995. +
  11996. DECLARE_PER_CPU(int, xmit_recursion);
  11997. static inline int dev_recursion_level(void)
  11998. {
  11999. return this_cpu_read(xmit_recursion);
  12000. }
  12001. +#endif
  12002. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  12003. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  12004. @@ -2564,6 +2573,7 @@ struct softnet_data {
  12005. unsigned int dropped;
  12006. struct sk_buff_head input_pkt_queue;
  12007. struct napi_struct backlog;
  12008. + struct sk_buff_head tofree_queue;
  12009. };
  12010. diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
  12011. index 04078e8a4803..a61c9609e32f 100644
  12012. --- a/include/linux/netfilter/x_tables.h
  12013. +++ b/include/linux/netfilter/x_tables.h
  12014. @@ -4,6 +4,7 @@
  12015. #include <linux/netdevice.h>
  12016. #include <linux/static_key.h>
  12017. +#include <linux/locallock.h>
  12018. #include <uapi/linux/netfilter/x_tables.h>
  12019. /**
  12020. @@ -289,6 +290,8 @@ void xt_free_table_info(struct xt_table_info *info);
  12021. */
  12022. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  12023. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  12024. +
  12025. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  12026. *
  12027. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  12028. @@ -309,6 +312,9 @@ static inline unsigned int xt_write_recseq_begin(void)
  12029. {
  12030. unsigned int addend;
  12031. + /* RT protection */
  12032. + local_lock(xt_write_lock);
  12033. +
  12034. /*
  12035. * Low order bit of sequence is set if we already
  12036. * called xt_write_recseq_begin().
  12037. @@ -339,6 +345,7 @@ static inline void xt_write_recseq_end(unsigned int addend)
  12038. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  12039. smp_wmb();
  12040. __this_cpu_add(xt_recseq.sequence, addend);
  12041. + local_unlock(xt_write_lock);
  12042. }
  12043. /*
  12044. diff --git a/include/linux/notifier.h b/include/linux/notifier.h
  12045. index d14a4c362465..2e4414a0c1c4 100644
  12046. --- a/include/linux/notifier.h
  12047. +++ b/include/linux/notifier.h
  12048. @@ -6,7 +6,7 @@
  12049. *
  12050. * Alan Cox <Alan.Cox@linux.org>
  12051. */
  12052. -
  12053. +
  12054. #ifndef _LINUX_NOTIFIER_H
  12055. #define _LINUX_NOTIFIER_H
  12056. #include <linux/errno.h>
  12057. @@ -42,9 +42,7 @@
  12058. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  12059. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  12060. * SRCU notifier chains should be used when the chain will be called very
  12061. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  12062. - * chains are slightly more difficult to use because they require special
  12063. - * runtime initialization.
  12064. + * often but notifier_blocks will seldom be removed.
  12065. */
  12066. typedef int (*notifier_fn_t)(struct notifier_block *nb,
  12067. @@ -88,7 +86,7 @@ struct srcu_notifier_head {
  12068. (name)->head = NULL; \
  12069. } while (0)
  12070. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  12071. +/* srcu_notifier_heads must be cleaned up dynamically */
  12072. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12073. #define srcu_cleanup_notifier_head(name) \
  12074. cleanup_srcu_struct(&(name)->srcu);
  12075. @@ -101,7 +99,13 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12076. .head = NULL }
  12077. #define RAW_NOTIFIER_INIT(name) { \
  12078. .head = NULL }
  12079. -/* srcu_notifier_heads cannot be initialized statically */
  12080. +
  12081. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  12082. + { \
  12083. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  12084. + .head = NULL, \
  12085. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  12086. + }
  12087. #define ATOMIC_NOTIFIER_HEAD(name) \
  12088. struct atomic_notifier_head name = \
  12089. @@ -113,6 +117,18 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  12090. struct raw_notifier_head name = \
  12091. RAW_NOTIFIER_INIT(name)
  12092. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  12093. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  12094. + name##_head_srcu_array); \
  12095. + mod struct srcu_notifier_head name = \
  12096. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  12097. +
  12098. +#define SRCU_NOTIFIER_HEAD(name) \
  12099. + _SRCU_NOTIFIER_HEAD(name, )
  12100. +
  12101. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  12102. + _SRCU_NOTIFIER_HEAD(name, static)
  12103. +
  12104. #ifdef __KERNEL__
  12105. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  12106. @@ -182,12 +198,12 @@ static inline int notifier_to_errno(int ret)
  12107. /*
  12108. * Declared notifiers so far. I can imagine quite a few more chains
  12109. - * over time (eg laptop power reset chains, reboot chain (to clean
  12110. + * over time (eg laptop power reset chains, reboot chain (to clean
  12111. * device units up), device [un]mount chain, module load/unload chain,
  12112. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  12113. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  12114. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  12115. */
  12116. -
  12117. +
  12118. /* CPU notfiers are defined in include/linux/cpu.h. */
  12119. /* netdevice notifiers are defined in include/linux/netdevice.h */
  12120. diff --git a/include/linux/percpu.h b/include/linux/percpu.h
  12121. index caebf2a758dc..53a60a51c758 100644
  12122. --- a/include/linux/percpu.h
  12123. +++ b/include/linux/percpu.h
  12124. @@ -24,6 +24,35 @@
  12125. PERCPU_MODULE_RESERVE)
  12126. #endif
  12127. +#ifdef CONFIG_PREEMPT_RT_FULL
  12128. +
  12129. +#define get_local_var(var) (*({ \
  12130. + migrate_disable(); \
  12131. + this_cpu_ptr(&var); }))
  12132. +
  12133. +#define put_local_var(var) do { \
  12134. + (void)&(var); \
  12135. + migrate_enable(); \
  12136. +} while (0)
  12137. +
  12138. +# define get_local_ptr(var) ({ \
  12139. + migrate_disable(); \
  12140. + this_cpu_ptr(var); })
  12141. +
  12142. +# define put_local_ptr(var) do { \
  12143. + (void)(var); \
  12144. + migrate_enable(); \
  12145. +} while (0)
  12146. +
  12147. +#else
  12148. +
  12149. +#define get_local_var(var) get_cpu_var(var)
  12150. +#define put_local_var(var) put_cpu_var(var)
  12151. +#define get_local_ptr(var) get_cpu_ptr(var)
  12152. +#define put_local_ptr(var) put_cpu_ptr(var)
  12153. +
  12154. +#endif
  12155. +
  12156. /* minimum unit size, also is the maximum supported allocation size */
  12157. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  12158. diff --git a/include/linux/pid.h b/include/linux/pid.h
  12159. index 23705a53abba..2cc64b779f03 100644
  12160. --- a/include/linux/pid.h
  12161. +++ b/include/linux/pid.h
  12162. @@ -2,6 +2,7 @@
  12163. #define _LINUX_PID_H
  12164. #include <linux/rcupdate.h>
  12165. +#include <linux/atomic.h>
  12166. enum pid_type
  12167. {
  12168. diff --git a/include/linux/preempt.h b/include/linux/preempt.h
  12169. index 75e4e30677f1..1cfb1cb72354 100644
  12170. --- a/include/linux/preempt.h
  12171. +++ b/include/linux/preempt.h
  12172. @@ -50,7 +50,11 @@
  12173. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  12174. #define NMI_OFFSET (1UL << NMI_SHIFT)
  12175. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12176. +#ifndef CONFIG_PREEMPT_RT_FULL
  12177. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  12178. +#else
  12179. +# define SOFTIRQ_DISABLE_OFFSET (0)
  12180. +#endif
  12181. /* We use the MSB mostly because its available */
  12182. #define PREEMPT_NEED_RESCHED 0x80000000
  12183. @@ -59,9 +63,15 @@
  12184. #include <asm/preempt.h>
  12185. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  12186. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12187. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  12188. | NMI_MASK))
  12189. +#ifndef CONFIG_PREEMPT_RT_FULL
  12190. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  12191. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12192. +#else
  12193. +# define softirq_count() (0UL)
  12194. +extern int in_serving_softirq(void);
  12195. +#endif
  12196. /*
  12197. * Are we doing bottom half or hardware interrupt processing?
  12198. @@ -72,7 +82,6 @@
  12199. #define in_irq() (hardirq_count())
  12200. #define in_softirq() (softirq_count())
  12201. #define in_interrupt() (irq_count())
  12202. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  12203. /*
  12204. * Are we in NMI context?
  12205. @@ -91,7 +100,11 @@
  12206. /*
  12207. * The preempt_count offset after spin_lock()
  12208. */
  12209. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  12210. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  12211. +#else
  12212. +#define PREEMPT_LOCK_OFFSET 0
  12213. +#endif
  12214. /*
  12215. * The preempt_count offset needed for things like:
  12216. @@ -140,6 +153,20 @@ extern void preempt_count_sub(int val);
  12217. #define preempt_count_inc() preempt_count_add(1)
  12218. #define preempt_count_dec() preempt_count_sub(1)
  12219. +#ifdef CONFIG_PREEMPT_LAZY
  12220. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  12221. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  12222. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  12223. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  12224. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  12225. +#else
  12226. +#define add_preempt_lazy_count(val) do { } while (0)
  12227. +#define sub_preempt_lazy_count(val) do { } while (0)
  12228. +#define inc_preempt_lazy_count() do { } while (0)
  12229. +#define dec_preempt_lazy_count() do { } while (0)
  12230. +#define preempt_lazy_count() (0)
  12231. +#endif
  12232. +
  12233. #ifdef CONFIG_PREEMPT_COUNT
  12234. #define preempt_disable() \
  12235. @@ -148,13 +175,25 @@ do { \
  12236. barrier(); \
  12237. } while (0)
  12238. +#define preempt_lazy_disable() \
  12239. +do { \
  12240. + inc_preempt_lazy_count(); \
  12241. + barrier(); \
  12242. +} while (0)
  12243. +
  12244. #define sched_preempt_enable_no_resched() \
  12245. do { \
  12246. barrier(); \
  12247. preempt_count_dec(); \
  12248. } while (0)
  12249. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12250. +#ifdef CONFIG_PREEMPT_RT_BASE
  12251. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  12252. +# define preempt_check_resched_rt() preempt_check_resched()
  12253. +#else
  12254. +# define preempt_enable_no_resched() preempt_enable()
  12255. +# define preempt_check_resched_rt() barrier();
  12256. +#endif
  12257. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  12258. @@ -179,6 +218,13 @@ do { \
  12259. __preempt_schedule(); \
  12260. } while (0)
  12261. +#define preempt_lazy_enable() \
  12262. +do { \
  12263. + dec_preempt_lazy_count(); \
  12264. + barrier(); \
  12265. + preempt_check_resched(); \
  12266. +} while (0)
  12267. +
  12268. #else /* !CONFIG_PREEMPT */
  12269. #define preempt_enable() \
  12270. do { \
  12271. @@ -224,6 +270,7 @@ do { \
  12272. #define preempt_disable_notrace() barrier()
  12273. #define preempt_enable_no_resched_notrace() barrier()
  12274. #define preempt_enable_notrace() barrier()
  12275. +#define preempt_check_resched_rt() barrier()
  12276. #define preemptible() 0
  12277. #endif /* CONFIG_PREEMPT_COUNT */
  12278. @@ -244,10 +291,31 @@ do { \
  12279. } while (0)
  12280. #define preempt_fold_need_resched() \
  12281. do { \
  12282. - if (tif_need_resched()) \
  12283. + if (tif_need_resched_now()) \
  12284. set_preempt_need_resched(); \
  12285. } while (0)
  12286. +#ifdef CONFIG_PREEMPT_RT_FULL
  12287. +# define preempt_disable_rt() preempt_disable()
  12288. +# define preempt_enable_rt() preempt_enable()
  12289. +# define preempt_disable_nort() barrier()
  12290. +# define preempt_enable_nort() barrier()
  12291. +# ifdef CONFIG_SMP
  12292. + extern void migrate_disable(void);
  12293. + extern void migrate_enable(void);
  12294. +# else /* CONFIG_SMP */
  12295. +# define migrate_disable() barrier()
  12296. +# define migrate_enable() barrier()
  12297. +# endif /* CONFIG_SMP */
  12298. +#else
  12299. +# define preempt_disable_rt() barrier()
  12300. +# define preempt_enable_rt() barrier()
  12301. +# define preempt_disable_nort() preempt_disable()
  12302. +# define preempt_enable_nort() preempt_enable()
  12303. +# define migrate_disable() preempt_disable()
  12304. +# define migrate_enable() preempt_enable()
  12305. +#endif
  12306. +
  12307. #ifdef CONFIG_PREEMPT_NOTIFIERS
  12308. struct preempt_notifier;
  12309. diff --git a/include/linux/printk.h b/include/linux/printk.h
  12310. index 9729565c25ff..9cdca696b718 100644
  12311. --- a/include/linux/printk.h
  12312. +++ b/include/linux/printk.h
  12313. @@ -117,9 +117,11 @@ int no_printk(const char *fmt, ...)
  12314. #ifdef CONFIG_EARLY_PRINTK
  12315. extern asmlinkage __printf(1, 2)
  12316. void early_printk(const char *fmt, ...);
  12317. +extern void printk_kill(void);
  12318. #else
  12319. static inline __printf(1, 2) __cold
  12320. void early_printk(const char *s, ...) { }
  12321. +static inline void printk_kill(void) { }
  12322. #endif
  12323. typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
  12324. diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h
  12325. index 5d5174b59802..8ddbd6e15a3c 100644
  12326. --- a/include/linux/radix-tree.h
  12327. +++ b/include/linux/radix-tree.h
  12328. @@ -277,8 +277,13 @@ radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  12329. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  12330. void ***results, unsigned long *indices,
  12331. unsigned long first_index, unsigned int max_items);
  12332. +#ifndef CONFIG_PREEMPT_RT_FULL
  12333. int radix_tree_preload(gfp_t gfp_mask);
  12334. int radix_tree_maybe_preload(gfp_t gfp_mask);
  12335. +#else
  12336. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  12337. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  12338. +#endif
  12339. void radix_tree_init(void);
  12340. void *radix_tree_tag_set(struct radix_tree_root *root,
  12341. unsigned long index, unsigned int tag);
  12342. @@ -303,7 +308,7 @@ unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  12343. static inline void radix_tree_preload_end(void)
  12344. {
  12345. - preempt_enable();
  12346. + preempt_enable_nort();
  12347. }
  12348. /**
  12349. diff --git a/include/linux/random.h b/include/linux/random.h
  12350. index a75840c1aa71..1a804361670c 100644
  12351. --- a/include/linux/random.h
  12352. +++ b/include/linux/random.h
  12353. @@ -20,7 +20,7 @@ struct random_ready_callback {
  12354. extern void add_device_randomness(const void *, unsigned int);
  12355. extern void add_input_randomness(unsigned int type, unsigned int code,
  12356. unsigned int value);
  12357. -extern void add_interrupt_randomness(int irq, int irq_flags);
  12358. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  12359. extern void get_random_bytes(void *buf, int nbytes);
  12360. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  12361. diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
  12362. index a5aa7ae671f4..24ddffd25492 100644
  12363. --- a/include/linux/rbtree.h
  12364. +++ b/include/linux/rbtree.h
  12365. @@ -31,7 +31,6 @@
  12366. #include <linux/kernel.h>
  12367. #include <linux/stddef.h>
  12368. -#include <linux/rcupdate.h>
  12369. struct rb_node {
  12370. unsigned long __rb_parent_color;
  12371. @@ -86,14 +85,8 @@ static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  12372. *rb_link = node;
  12373. }
  12374. -static inline void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12375. - struct rb_node **rb_link)
  12376. -{
  12377. - node->__rb_parent_color = (unsigned long)parent;
  12378. - node->rb_left = node->rb_right = NULL;
  12379. -
  12380. - rcu_assign_pointer(*rb_link, node);
  12381. -}
  12382. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  12383. + struct rb_node **rb_link);
  12384. #define rb_entry_safe(ptr, type, member) \
  12385. ({ typeof(ptr) ____ptr = (ptr); \
  12386. diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
  12387. index a0189ba67fde..c2f5f955163d 100644
  12388. --- a/include/linux/rcupdate.h
  12389. +++ b/include/linux/rcupdate.h
  12390. @@ -169,6 +169,9 @@ void call_rcu(struct rcu_head *head,
  12391. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12392. +#ifdef CONFIG_PREEMPT_RT_FULL
  12393. +#define call_rcu_bh call_rcu
  12394. +#else
  12395. /**
  12396. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  12397. * @head: structure to be used for queueing the RCU updates.
  12398. @@ -192,6 +195,7 @@ void call_rcu(struct rcu_head *head,
  12399. */
  12400. void call_rcu_bh(struct rcu_head *head,
  12401. rcu_callback_t func);
  12402. +#endif
  12403. /**
  12404. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  12405. @@ -292,6 +296,11 @@ void synchronize_rcu(void);
  12406. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  12407. */
  12408. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  12409. +#ifndef CONFIG_PREEMPT_RT_FULL
  12410. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12411. +#else
  12412. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  12413. +#endif
  12414. #else /* #ifdef CONFIG_PREEMPT_RCU */
  12415. @@ -317,6 +326,8 @@ static inline int rcu_preempt_depth(void)
  12416. return 0;
  12417. }
  12418. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  12419. +
  12420. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  12421. /* Internal to kernel */
  12422. @@ -489,7 +500,14 @@ extern struct lockdep_map rcu_callback_map;
  12423. int debug_lockdep_rcu_enabled(void);
  12424. int rcu_read_lock_held(void);
  12425. +#ifdef CONFIG_PREEMPT_RT_FULL
  12426. +static inline int rcu_read_lock_bh_held(void)
  12427. +{
  12428. + return rcu_read_lock_held();
  12429. +}
  12430. +#else
  12431. int rcu_read_lock_bh_held(void);
  12432. +#endif
  12433. /**
  12434. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  12435. @@ -937,10 +955,14 @@ static inline void rcu_read_unlock(void)
  12436. static inline void rcu_read_lock_bh(void)
  12437. {
  12438. local_bh_disable();
  12439. +#ifdef CONFIG_PREEMPT_RT_FULL
  12440. + rcu_read_lock();
  12441. +#else
  12442. __acquire(RCU_BH);
  12443. rcu_lock_acquire(&rcu_bh_lock_map);
  12444. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12445. "rcu_read_lock_bh() used illegally while idle");
  12446. +#endif
  12447. }
  12448. /*
  12449. @@ -950,10 +972,14 @@ static inline void rcu_read_lock_bh(void)
  12450. */
  12451. static inline void rcu_read_unlock_bh(void)
  12452. {
  12453. +#ifdef CONFIG_PREEMPT_RT_FULL
  12454. + rcu_read_unlock();
  12455. +#else
  12456. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  12457. "rcu_read_unlock_bh() used illegally while idle");
  12458. rcu_lock_release(&rcu_bh_lock_map);
  12459. __release(RCU_BH);
  12460. +#endif
  12461. local_bh_enable();
  12462. }
  12463. diff --git a/include/linux/rcutree.h b/include/linux/rcutree.h
  12464. index 60d15a080d7c..436c9e62bfc6 100644
  12465. --- a/include/linux/rcutree.h
  12466. +++ b/include/linux/rcutree.h
  12467. @@ -44,7 +44,11 @@ static inline void rcu_virt_note_context_switch(int cpu)
  12468. rcu_note_context_switch();
  12469. }
  12470. +#ifdef CONFIG_PREEMPT_RT_FULL
  12471. +# define synchronize_rcu_bh synchronize_rcu
  12472. +#else
  12473. void synchronize_rcu_bh(void);
  12474. +#endif
  12475. void synchronize_sched_expedited(void);
  12476. void synchronize_rcu_expedited(void);
  12477. @@ -72,7 +76,11 @@ static inline void synchronize_rcu_bh_expedited(void)
  12478. }
  12479. void rcu_barrier(void);
  12480. +#ifdef CONFIG_PREEMPT_RT_FULL
  12481. +# define rcu_barrier_bh rcu_barrier
  12482. +#else
  12483. void rcu_barrier_bh(void);
  12484. +#endif
  12485. void rcu_barrier_sched(void);
  12486. unsigned long get_state_synchronize_rcu(void);
  12487. void cond_synchronize_rcu(unsigned long oldstate);
  12488. @@ -85,12 +93,10 @@ unsigned long rcu_batches_started(void);
  12489. unsigned long rcu_batches_started_bh(void);
  12490. unsigned long rcu_batches_started_sched(void);
  12491. unsigned long rcu_batches_completed(void);
  12492. -unsigned long rcu_batches_completed_bh(void);
  12493. unsigned long rcu_batches_completed_sched(void);
  12494. void show_rcu_gp_kthreads(void);
  12495. void rcu_force_quiescent_state(void);
  12496. -void rcu_bh_force_quiescent_state(void);
  12497. void rcu_sched_force_quiescent_state(void);
  12498. void rcu_idle_enter(void);
  12499. @@ -105,6 +111,14 @@ extern int rcu_scheduler_active __read_mostly;
  12500. bool rcu_is_watching(void);
  12501. +#ifndef CONFIG_PREEMPT_RT_FULL
  12502. +void rcu_bh_force_quiescent_state(void);
  12503. +unsigned long rcu_batches_completed_bh(void);
  12504. +#else
  12505. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  12506. +# define rcu_batches_completed_bh rcu_batches_completed
  12507. +#endif
  12508. +
  12509. void rcu_all_qs(void);
  12510. #endif /* __LINUX_RCUTREE_H */
  12511. diff --git a/include/linux/rtmutex.h b/include/linux/rtmutex.h
  12512. index 1abba5ce2a2f..30211c627511 100644
  12513. --- a/include/linux/rtmutex.h
  12514. +++ b/include/linux/rtmutex.h
  12515. @@ -13,11 +13,15 @@
  12516. #define __LINUX_RT_MUTEX_H
  12517. #include <linux/linkage.h>
  12518. +#include <linux/spinlock_types_raw.h>
  12519. #include <linux/rbtree.h>
  12520. -#include <linux/spinlock_types.h>
  12521. extern int max_lock_depth; /* for sysctl */
  12522. +#ifdef CONFIG_DEBUG_MUTEXES
  12523. +#include <linux/debug_locks.h>
  12524. +#endif
  12525. +
  12526. /**
  12527. * The rt_mutex structure
  12528. *
  12529. @@ -31,8 +35,8 @@ struct rt_mutex {
  12530. struct rb_root waiters;
  12531. struct rb_node *waiters_leftmost;
  12532. struct task_struct *owner;
  12533. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  12534. int save_state;
  12535. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  12536. const char *name, *file;
  12537. int line;
  12538. void *magic;
  12539. @@ -55,22 +59,33 @@ struct hrtimer_sleeper;
  12540. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  12541. #endif
  12542. +# define rt_mutex_init(mutex) \
  12543. + do { \
  12544. + raw_spin_lock_init(&(mutex)->wait_lock); \
  12545. + __rt_mutex_init(mutex, #mutex); \
  12546. + } while (0)
  12547. +
  12548. #ifdef CONFIG_DEBUG_RT_MUTEXES
  12549. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  12550. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  12551. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  12552. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  12553. #else
  12554. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12555. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  12556. # define rt_mutex_debug_task_free(t) do { } while (0)
  12557. #endif
  12558. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  12559. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12560. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12561. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  12562. , .waiters = RB_ROOT \
  12563. , .owner = NULL \
  12564. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  12565. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  12566. +
  12567. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  12568. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  12569. +
  12570. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  12571. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  12572. + , .save_state = 1 }
  12573. #define DEFINE_RT_MUTEX(mutexname) \
  12574. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  12575. @@ -91,6 +106,7 @@ extern void rt_mutex_destroy(struct rt_mutex *lock);
  12576. extern void rt_mutex_lock(struct rt_mutex *lock);
  12577. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  12578. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  12579. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  12580. struct hrtimer_sleeper *timeout);
  12581. diff --git a/include/linux/rwlock_rt.h b/include/linux/rwlock_rt.h
  12582. new file mode 100644
  12583. index 000000000000..49ed2d45d3be
  12584. --- /dev/null
  12585. +++ b/include/linux/rwlock_rt.h
  12586. @@ -0,0 +1,99 @@
  12587. +#ifndef __LINUX_RWLOCK_RT_H
  12588. +#define __LINUX_RWLOCK_RT_H
  12589. +
  12590. +#ifndef __LINUX_SPINLOCK_H
  12591. +#error Do not include directly. Use spinlock.h
  12592. +#endif
  12593. +
  12594. +#define rwlock_init(rwl) \
  12595. +do { \
  12596. + static struct lock_class_key __key; \
  12597. + \
  12598. + rt_mutex_init(&(rwl)->lock); \
  12599. + __rt_rwlock_init(rwl, #rwl, &__key); \
  12600. +} while (0)
  12601. +
  12602. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  12603. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  12604. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  12605. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  12606. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  12607. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  12608. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  12609. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  12610. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  12611. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  12612. +
  12613. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  12614. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  12615. +
  12616. +#define write_trylock_irqsave(lock, flags) \
  12617. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  12618. +
  12619. +#define read_lock_irqsave(lock, flags) \
  12620. + do { \
  12621. + typecheck(unsigned long, flags); \
  12622. + flags = rt_read_lock_irqsave(lock); \
  12623. + } while (0)
  12624. +
  12625. +#define write_lock_irqsave(lock, flags) \
  12626. + do { \
  12627. + typecheck(unsigned long, flags); \
  12628. + flags = rt_write_lock_irqsave(lock); \
  12629. + } while (0)
  12630. +
  12631. +#define read_lock(lock) rt_read_lock(lock)
  12632. +
  12633. +#define read_lock_bh(lock) \
  12634. + do { \
  12635. + local_bh_disable(); \
  12636. + rt_read_lock(lock); \
  12637. + } while (0)
  12638. +
  12639. +#define read_lock_irq(lock) read_lock(lock)
  12640. +
  12641. +#define write_lock(lock) rt_write_lock(lock)
  12642. +
  12643. +#define write_lock_bh(lock) \
  12644. + do { \
  12645. + local_bh_disable(); \
  12646. + rt_write_lock(lock); \
  12647. + } while (0)
  12648. +
  12649. +#define write_lock_irq(lock) write_lock(lock)
  12650. +
  12651. +#define read_unlock(lock) rt_read_unlock(lock)
  12652. +
  12653. +#define read_unlock_bh(lock) \
  12654. + do { \
  12655. + rt_read_unlock(lock); \
  12656. + local_bh_enable(); \
  12657. + } while (0)
  12658. +
  12659. +#define read_unlock_irq(lock) read_unlock(lock)
  12660. +
  12661. +#define write_unlock(lock) rt_write_unlock(lock)
  12662. +
  12663. +#define write_unlock_bh(lock) \
  12664. + do { \
  12665. + rt_write_unlock(lock); \
  12666. + local_bh_enable(); \
  12667. + } while (0)
  12668. +
  12669. +#define write_unlock_irq(lock) write_unlock(lock)
  12670. +
  12671. +#define read_unlock_irqrestore(lock, flags) \
  12672. + do { \
  12673. + typecheck(unsigned long, flags); \
  12674. + (void) flags; \
  12675. + rt_read_unlock(lock); \
  12676. + } while (0)
  12677. +
  12678. +#define write_unlock_irqrestore(lock, flags) \
  12679. + do { \
  12680. + typecheck(unsigned long, flags); \
  12681. + (void) flags; \
  12682. + rt_write_unlock(lock); \
  12683. + } while (0)
  12684. +
  12685. +#endif
  12686. diff --git a/include/linux/rwlock_types.h b/include/linux/rwlock_types.h
  12687. index cc0072e93e36..d0da966ad7a0 100644
  12688. --- a/include/linux/rwlock_types.h
  12689. +++ b/include/linux/rwlock_types.h
  12690. @@ -1,6 +1,10 @@
  12691. #ifndef __LINUX_RWLOCK_TYPES_H
  12692. #define __LINUX_RWLOCK_TYPES_H
  12693. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  12694. +# error "Do not include directly, include spinlock_types.h"
  12695. +#endif
  12696. +
  12697. /*
  12698. * include/linux/rwlock_types.h - generic rwlock type definitions
  12699. * and initializers
  12700. @@ -43,6 +47,7 @@ typedef struct {
  12701. RW_DEP_MAP_INIT(lockname) }
  12702. #endif
  12703. -#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
  12704. +#define DEFINE_RWLOCK(name) \
  12705. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12706. #endif /* __LINUX_RWLOCK_TYPES_H */
  12707. diff --git a/include/linux/rwlock_types_rt.h b/include/linux/rwlock_types_rt.h
  12708. new file mode 100644
  12709. index 000000000000..b13832119591
  12710. --- /dev/null
  12711. +++ b/include/linux/rwlock_types_rt.h
  12712. @@ -0,0 +1,33 @@
  12713. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  12714. +#define __LINUX_RWLOCK_TYPES_RT_H
  12715. +
  12716. +#ifndef __LINUX_SPINLOCK_TYPES_H
  12717. +#error "Do not include directly. Include spinlock_types.h instead"
  12718. +#endif
  12719. +
  12720. +/*
  12721. + * rwlocks - rtmutex which allows single reader recursion
  12722. + */
  12723. +typedef struct {
  12724. + struct rt_mutex lock;
  12725. + int read_depth;
  12726. + unsigned int break_lock;
  12727. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12728. + struct lockdep_map dep_map;
  12729. +#endif
  12730. +} rwlock_t;
  12731. +
  12732. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12733. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  12734. +#else
  12735. +# define RW_DEP_MAP_INIT(lockname)
  12736. +#endif
  12737. +
  12738. +#define __RW_LOCK_UNLOCKED(name) \
  12739. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  12740. + RW_DEP_MAP_INIT(name) }
  12741. +
  12742. +#define DEFINE_RWLOCK(name) \
  12743. + rwlock_t name __cacheline_aligned_in_smp = __RW_LOCK_UNLOCKED(name)
  12744. +
  12745. +#endif
  12746. diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
  12747. index 8f498cdde280..2b2148431f14 100644
  12748. --- a/include/linux/rwsem.h
  12749. +++ b/include/linux/rwsem.h
  12750. @@ -18,6 +18,10 @@
  12751. #include <linux/osq_lock.h>
  12752. #endif
  12753. +#ifdef CONFIG_PREEMPT_RT_FULL
  12754. +#include <linux/rwsem_rt.h>
  12755. +#else /* PREEMPT_RT_FULL */
  12756. +
  12757. struct rw_semaphore;
  12758. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  12759. @@ -177,4 +181,6 @@ extern void up_read_non_owner(struct rw_semaphore *sem);
  12760. # define up_read_non_owner(sem) up_read(sem)
  12761. #endif
  12762. +#endif /* !PREEMPT_RT_FULL */
  12763. +
  12764. #endif /* _LINUX_RWSEM_H */
  12765. diff --git a/include/linux/rwsem_rt.h b/include/linux/rwsem_rt.h
  12766. new file mode 100644
  12767. index 000000000000..f97860b2e2a4
  12768. --- /dev/null
  12769. +++ b/include/linux/rwsem_rt.h
  12770. @@ -0,0 +1,152 @@
  12771. +#ifndef _LINUX_RWSEM_RT_H
  12772. +#define _LINUX_RWSEM_RT_H
  12773. +
  12774. +#ifndef _LINUX_RWSEM_H
  12775. +#error "Include rwsem.h"
  12776. +#endif
  12777. +
  12778. +/*
  12779. + * RW-semaphores are a spinlock plus a reader-depth count.
  12780. + *
  12781. + * Note that the semantics are different from the usual
  12782. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  12783. + * multiple readers to hold the lock at once, we only allow
  12784. + * a read-lock owner to read-lock recursively. This is
  12785. + * better for latency, makes the implementation inherently
  12786. + * fair and makes it simpler as well.
  12787. + */
  12788. +
  12789. +#include <linux/rtmutex.h>
  12790. +
  12791. +struct rw_semaphore {
  12792. + struct rt_mutex lock;
  12793. + int read_depth;
  12794. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12795. + struct lockdep_map dep_map;
  12796. +#endif
  12797. +};
  12798. +
  12799. +#define __RWSEM_INITIALIZER(name) \
  12800. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  12801. + RW_DEP_MAP_INIT(name) }
  12802. +
  12803. +#define DECLARE_RWSEM(lockname) \
  12804. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  12805. +
  12806. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  12807. + struct lock_class_key *key);
  12808. +
  12809. +#define __rt_init_rwsem(sem, name, key) \
  12810. + do { \
  12811. + rt_mutex_init(&(sem)->lock); \
  12812. + __rt_rwsem_init((sem), (name), (key));\
  12813. + } while (0)
  12814. +
  12815. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  12816. +
  12817. +# define rt_init_rwsem(sem) \
  12818. +do { \
  12819. + static struct lock_class_key __key; \
  12820. + \
  12821. + __rt_init_rwsem((sem), #sem, &__key); \
  12822. +} while (0)
  12823. +
  12824. +extern void rt_down_write(struct rw_semaphore *rwsem);
  12825. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  12826. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  12827. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  12828. + struct lockdep_map *nest);
  12829. +extern void rt__down_read(struct rw_semaphore *rwsem);
  12830. +extern void rt_down_read(struct rw_semaphore *rwsem);
  12831. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  12832. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  12833. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  12834. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  12835. +extern void rt_up_read(struct rw_semaphore *rwsem);
  12836. +extern void rt_up_write(struct rw_semaphore *rwsem);
  12837. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  12838. +
  12839. +#define init_rwsem(sem) rt_init_rwsem(sem)
  12840. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  12841. +
  12842. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  12843. +{
  12844. + /* rt_mutex_has_waiters() */
  12845. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  12846. +}
  12847. +
  12848. +static inline void __down_read(struct rw_semaphore *sem)
  12849. +{
  12850. + rt__down_read(sem);
  12851. +}
  12852. +
  12853. +static inline void down_read(struct rw_semaphore *sem)
  12854. +{
  12855. + rt_down_read(sem);
  12856. +}
  12857. +
  12858. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  12859. +{
  12860. + return rt__down_read_trylock(sem);
  12861. +}
  12862. +
  12863. +static inline int down_read_trylock(struct rw_semaphore *sem)
  12864. +{
  12865. + return rt_down_read_trylock(sem);
  12866. +}
  12867. +
  12868. +static inline void down_write(struct rw_semaphore *sem)
  12869. +{
  12870. + rt_down_write(sem);
  12871. +}
  12872. +
  12873. +static inline int down_write_trylock(struct rw_semaphore *sem)
  12874. +{
  12875. + return rt_down_write_trylock(sem);
  12876. +}
  12877. +
  12878. +static inline void __up_read(struct rw_semaphore *sem)
  12879. +{
  12880. + __rt_up_read(sem);
  12881. +}
  12882. +
  12883. +static inline void up_read(struct rw_semaphore *sem)
  12884. +{
  12885. + rt_up_read(sem);
  12886. +}
  12887. +
  12888. +static inline void up_write(struct rw_semaphore *sem)
  12889. +{
  12890. + rt_up_write(sem);
  12891. +}
  12892. +
  12893. +static inline void downgrade_write(struct rw_semaphore *sem)
  12894. +{
  12895. + rt_downgrade_write(sem);
  12896. +}
  12897. +
  12898. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  12899. +{
  12900. + return rt_down_read_nested(sem, subclass);
  12901. +}
  12902. +
  12903. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  12904. +{
  12905. + rt_down_write_nested(sem, subclass);
  12906. +}
  12907. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12908. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12909. + struct rw_semaphore *nest_lock)
  12910. +{
  12911. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  12912. +}
  12913. +
  12914. +#else
  12915. +
  12916. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  12917. + struct rw_semaphore *nest_lock)
  12918. +{
  12919. + rt_down_write_nested_lock(sem, NULL);
  12920. +}
  12921. +#endif
  12922. +#endif
  12923. diff --git a/include/linux/sched.h b/include/linux/sched.h
  12924. index 1c0193baea2a..0570d8e022ec 100644
  12925. --- a/include/linux/sched.h
  12926. +++ b/include/linux/sched.h
  12927. @@ -26,6 +26,7 @@ struct sched_param {
  12928. #include <linux/nodemask.h>
  12929. #include <linux/mm_types.h>
  12930. #include <linux/preempt.h>
  12931. +#include <asm/kmap_types.h>
  12932. #include <asm/page.h>
  12933. #include <asm/ptrace.h>
  12934. @@ -182,8 +183,6 @@ extern void update_cpu_load_nohz(void);
  12935. static inline void update_cpu_load_nohz(void) { }
  12936. #endif
  12937. -extern unsigned long get_parent_ip(unsigned long addr);
  12938. -
  12939. extern void dump_cpu_task(int cpu);
  12940. struct seq_file;
  12941. @@ -242,10 +241,7 @@ extern char ___assert_task_state[1 - 2*!!(
  12942. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  12943. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  12944. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  12945. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  12946. -#define task_is_stopped_or_traced(task) \
  12947. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  12948. #define task_contributes_to_load(task) \
  12949. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  12950. (task->flags & PF_FROZEN) == 0 && \
  12951. @@ -311,6 +307,11 @@ extern char ___assert_task_state[1 - 2*!!(
  12952. #endif
  12953. +#define __set_current_state_no_track(state_value) \
  12954. + do { current->state = (state_value); } while (0)
  12955. +#define set_current_state_no_track(state_value) \
  12956. + set_mb(current->state, (state_value))
  12957. +
  12958. /* Task command name length */
  12959. #define TASK_COMM_LEN 16
  12960. @@ -970,8 +971,18 @@ struct wake_q_head {
  12961. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  12962. extern void wake_q_add(struct wake_q_head *head,
  12963. - struct task_struct *task);
  12964. -extern void wake_up_q(struct wake_q_head *head);
  12965. + struct task_struct *task);
  12966. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  12967. +
  12968. +static inline void wake_up_q(struct wake_q_head *head)
  12969. +{
  12970. + __wake_up_q(head, false);
  12971. +}
  12972. +
  12973. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  12974. +{
  12975. + __wake_up_q(head, true);
  12976. +}
  12977. /*
  12978. * sched-domains (multiprocessor balancing) declarations:
  12979. @@ -1379,6 +1390,7 @@ struct tlbflush_unmap_batch {
  12980. struct task_struct {
  12981. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  12982. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  12983. void *stack;
  12984. atomic_t usage;
  12985. unsigned int flags; /* per process flags, defined below */
  12986. @@ -1415,6 +1427,12 @@ struct task_struct {
  12987. #endif
  12988. unsigned int policy;
  12989. +#ifdef CONFIG_PREEMPT_RT_FULL
  12990. + int migrate_disable;
  12991. +# ifdef CONFIG_SCHED_DEBUG
  12992. + int migrate_disable_atomic;
  12993. +# endif
  12994. +#endif
  12995. int nr_cpus_allowed;
  12996. cpumask_t cpus_allowed;
  12997. @@ -1522,11 +1540,14 @@ struct task_struct {
  12998. cputime_t gtime;
  12999. struct prev_cputime prev_cputime;
  13000. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13001. - seqlock_t vtime_seqlock;
  13002. + seqcount_t vtime_seqcount;
  13003. unsigned long long vtime_snap;
  13004. enum {
  13005. - VTIME_SLEEPING = 0,
  13006. + /* Task is sleeping or running in a CPU with VTIME inactive */
  13007. + VTIME_INACTIVE = 0,
  13008. + /* Task runs in userspace in a CPU with VTIME active */
  13009. VTIME_USER,
  13010. + /* Task runs in kernelspace in a CPU with VTIME active */
  13011. VTIME_SYS,
  13012. } vtime_snap_whence;
  13013. #endif
  13014. @@ -1538,6 +1559,9 @@ struct task_struct {
  13015. struct task_cputime cputime_expires;
  13016. struct list_head cpu_timers[3];
  13017. +#ifdef CONFIG_PREEMPT_RT_BASE
  13018. + struct task_struct *posix_timer_list;
  13019. +#endif
  13020. /* process credentials */
  13021. const struct cred __rcu *real_cred; /* objective and real subjective task
  13022. @@ -1568,10 +1592,15 @@ struct task_struct {
  13023. /* signal handlers */
  13024. struct signal_struct *signal;
  13025. struct sighand_struct *sighand;
  13026. + struct sigqueue *sigqueue_cache;
  13027. sigset_t blocked, real_blocked;
  13028. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  13029. struct sigpending pending;
  13030. +#ifdef CONFIG_PREEMPT_RT_FULL
  13031. + /* TODO: move me into ->restart_block ? */
  13032. + struct siginfo forced_info;
  13033. +#endif
  13034. unsigned long sas_ss_sp;
  13035. size_t sas_ss_size;
  13036. @@ -1795,6 +1824,12 @@ struct task_struct {
  13037. unsigned long trace;
  13038. /* bitmask and counter of trace recursion */
  13039. unsigned long trace_recursion;
  13040. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  13041. + u64 preempt_timestamp_hist;
  13042. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  13043. + long timer_offset;
  13044. +#endif
  13045. +#endif
  13046. #endif /* CONFIG_TRACING */
  13047. #ifdef CONFIG_MEMCG
  13048. struct mem_cgroup *memcg_in_oom;
  13049. @@ -1811,9 +1846,23 @@ struct task_struct {
  13050. unsigned int sequential_io;
  13051. unsigned int sequential_io_avg;
  13052. #endif
  13053. +#ifdef CONFIG_PREEMPT_RT_BASE
  13054. + struct rcu_head put_rcu;
  13055. + int softirq_nestcnt;
  13056. + unsigned int softirqs_raised;
  13057. +#endif
  13058. +#ifdef CONFIG_PREEMPT_RT_FULL
  13059. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  13060. + int kmap_idx;
  13061. + pte_t kmap_pte[KM_TYPE_NR];
  13062. +# endif
  13063. +#endif
  13064. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  13065. unsigned long task_state_change;
  13066. #endif
  13067. +#ifdef CONFIG_PREEMPT_RT_FULL
  13068. + int xmit_recursion;
  13069. +#endif
  13070. int pagefault_disabled;
  13071. /* CPU-specific state of this task */
  13072. struct thread_struct thread;
  13073. @@ -1831,9 +1880,6 @@ extern int arch_task_struct_size __read_mostly;
  13074. # define arch_task_struct_size (sizeof(struct task_struct))
  13075. #endif
  13076. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13077. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  13078. -
  13079. #define TNF_MIGRATED 0x01
  13080. #define TNF_NO_GROUP 0x02
  13081. #define TNF_SHARED 0x04
  13082. @@ -2023,6 +2069,15 @@ extern struct pid *cad_pid;
  13083. extern void free_task(struct task_struct *tsk);
  13084. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  13085. +#ifdef CONFIG_PREEMPT_RT_BASE
  13086. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  13087. +
  13088. +static inline void put_task_struct(struct task_struct *t)
  13089. +{
  13090. + if (atomic_dec_and_test(&t->usage))
  13091. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  13092. +}
  13093. +#else
  13094. extern void __put_task_struct(struct task_struct *t);
  13095. static inline void put_task_struct(struct task_struct *t)
  13096. @@ -2030,6 +2085,7 @@ static inline void put_task_struct(struct task_struct *t)
  13097. if (atomic_dec_and_test(&t->usage))
  13098. __put_task_struct(t);
  13099. }
  13100. +#endif
  13101. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  13102. extern void task_cputime(struct task_struct *t,
  13103. @@ -2068,6 +2124,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut,
  13104. /*
  13105. * Per process flags
  13106. */
  13107. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  13108. #define PF_EXITING 0x00000004 /* getting shut down */
  13109. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  13110. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  13111. @@ -2232,6 +2289,10 @@ extern void do_set_cpus_allowed(struct task_struct *p,
  13112. extern int set_cpus_allowed_ptr(struct task_struct *p,
  13113. const struct cpumask *new_mask);
  13114. +int migrate_me(void);
  13115. +void tell_sched_cpu_down_begin(int cpu);
  13116. +void tell_sched_cpu_down_done(int cpu);
  13117. +
  13118. #else
  13119. static inline void do_set_cpus_allowed(struct task_struct *p,
  13120. const struct cpumask *new_mask)
  13121. @@ -2244,6 +2305,9 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p,
  13122. return -EINVAL;
  13123. return 0;
  13124. }
  13125. +static inline int migrate_me(void) { return 0; }
  13126. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  13127. +static inline void tell_sched_cpu_down_done(int cpu) { }
  13128. #endif
  13129. #ifdef CONFIG_NO_HZ_COMMON
  13130. @@ -2453,6 +2517,7 @@ extern void xtime_update(unsigned long ticks);
  13131. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  13132. extern int wake_up_process(struct task_struct *tsk);
  13133. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  13134. extern void wake_up_new_task(struct task_struct *tsk);
  13135. #ifdef CONFIG_SMP
  13136. extern void kick_process(struct task_struct *tsk);
  13137. @@ -2576,12 +2641,24 @@ extern struct mm_struct * mm_alloc(void);
  13138. /* mmdrop drops the mm and the page tables */
  13139. extern void __mmdrop(struct mm_struct *);
  13140. +
  13141. static inline void mmdrop(struct mm_struct * mm)
  13142. {
  13143. if (unlikely(atomic_dec_and_test(&mm->mm_count)))
  13144. __mmdrop(mm);
  13145. }
  13146. +#ifdef CONFIG_PREEMPT_RT_BASE
  13147. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  13148. +static inline void mmdrop_delayed(struct mm_struct *mm)
  13149. +{
  13150. + if (atomic_dec_and_test(&mm->mm_count))
  13151. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  13152. +}
  13153. +#else
  13154. +# define mmdrop_delayed(mm) mmdrop(mm)
  13155. +#endif
  13156. +
  13157. /* mmput gets rid of the mappings and all user-space */
  13158. extern void mmput(struct mm_struct *);
  13159. /* Grab a reference to a task's mm, if it is not already going away */
  13160. @@ -2891,6 +2968,43 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
  13161. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  13162. }
  13163. +#ifdef CONFIG_PREEMPT_LAZY
  13164. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  13165. +{
  13166. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13167. +}
  13168. +
  13169. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  13170. +{
  13171. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  13172. +}
  13173. +
  13174. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  13175. +{
  13176. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  13177. +}
  13178. +
  13179. +static inline int need_resched_lazy(void)
  13180. +{
  13181. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  13182. +}
  13183. +
  13184. +static inline int need_resched_now(void)
  13185. +{
  13186. + return test_thread_flag(TIF_NEED_RESCHED);
  13187. +}
  13188. +
  13189. +#else
  13190. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  13191. +static inline int need_resched_lazy(void) { return 0; }
  13192. +
  13193. +static inline int need_resched_now(void)
  13194. +{
  13195. + return test_thread_flag(TIF_NEED_RESCHED);
  13196. +}
  13197. +
  13198. +#endif
  13199. +
  13200. static inline int restart_syscall(void)
  13201. {
  13202. set_tsk_thread_flag(current, TIF_SIGPENDING);
  13203. @@ -2922,6 +3036,51 @@ static inline int signal_pending_state(long state, struct task_struct *p)
  13204. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  13205. }
  13206. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  13207. +{
  13208. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  13209. + return true;
  13210. +#ifdef CONFIG_PREEMPT_RT_FULL
  13211. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  13212. + return true;
  13213. +#endif
  13214. + return false;
  13215. +}
  13216. +
  13217. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  13218. +{
  13219. + bool traced_stopped;
  13220. +
  13221. +#ifdef CONFIG_PREEMPT_RT_FULL
  13222. + unsigned long flags;
  13223. +
  13224. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  13225. + traced_stopped = __task_is_stopped_or_traced(task);
  13226. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13227. +#else
  13228. + traced_stopped = __task_is_stopped_or_traced(task);
  13229. +#endif
  13230. + return traced_stopped;
  13231. +}
  13232. +
  13233. +static inline bool task_is_traced(struct task_struct *task)
  13234. +{
  13235. + bool traced = false;
  13236. +
  13237. + if (task->state & __TASK_TRACED)
  13238. + return true;
  13239. +#ifdef CONFIG_PREEMPT_RT_FULL
  13240. + /* in case the task is sleeping on tasklist_lock */
  13241. + raw_spin_lock_irq(&task->pi_lock);
  13242. + if (task->state & __TASK_TRACED)
  13243. + traced = true;
  13244. + else if (task->saved_state & __TASK_TRACED)
  13245. + traced = true;
  13246. + raw_spin_unlock_irq(&task->pi_lock);
  13247. +#endif
  13248. + return traced;
  13249. +}
  13250. +
  13251. /*
  13252. * cond_resched() and cond_resched_lock(): latency reduction via
  13253. * explicit rescheduling in places that are safe. The return
  13254. @@ -2943,12 +3102,16 @@ extern int __cond_resched_lock(spinlock_t *lock);
  13255. __cond_resched_lock(lock); \
  13256. })
  13257. +#ifndef CONFIG_PREEMPT_RT_FULL
  13258. extern int __cond_resched_softirq(void);
  13259. #define cond_resched_softirq() ({ \
  13260. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  13261. __cond_resched_softirq(); \
  13262. })
  13263. +#else
  13264. +# define cond_resched_softirq() cond_resched()
  13265. +#endif
  13266. static inline void cond_resched_rcu(void)
  13267. {
  13268. @@ -3110,6 +3273,31 @@ static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  13269. #endif /* CONFIG_SMP */
  13270. +static inline int __migrate_disabled(struct task_struct *p)
  13271. +{
  13272. +#ifdef CONFIG_PREEMPT_RT_FULL
  13273. + return p->migrate_disable;
  13274. +#else
  13275. + return 0;
  13276. +#endif
  13277. +}
  13278. +
  13279. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  13280. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  13281. +{
  13282. + if (__migrate_disabled(p))
  13283. + return cpumask_of(task_cpu(p));
  13284. +
  13285. + return &p->cpus_allowed;
  13286. +}
  13287. +
  13288. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  13289. +{
  13290. + if (__migrate_disabled(p))
  13291. + return 1;
  13292. + return p->nr_cpus_allowed;
  13293. +}
  13294. +
  13295. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  13296. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  13297. diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
  13298. index e0582106ef4f..b14f4d2368aa 100644
  13299. --- a/include/linux/seqlock.h
  13300. +++ b/include/linux/seqlock.h
  13301. @@ -220,20 +220,30 @@ static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  13302. return __read_seqcount_retry(s, start);
  13303. }
  13304. -
  13305. -
  13306. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  13307. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  13308. {
  13309. s->sequence++;
  13310. smp_wmb();
  13311. }
  13312. -static inline void raw_write_seqcount_end(seqcount_t *s)
  13313. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  13314. +{
  13315. + preempt_disable_rt();
  13316. + __raw_write_seqcount_begin(s);
  13317. +}
  13318. +
  13319. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  13320. {
  13321. smp_wmb();
  13322. s->sequence++;
  13323. }
  13324. +static inline void raw_write_seqcount_end(seqcount_t *s)
  13325. +{
  13326. + __raw_write_seqcount_end(s);
  13327. + preempt_enable_rt();
  13328. +}
  13329. +
  13330. /**
  13331. * raw_write_seqcount_barrier - do a seq write barrier
  13332. * @s: pointer to seqcount_t
  13333. @@ -425,10 +435,32 @@ typedef struct {
  13334. /*
  13335. * Read side functions for starting and finalizing a read side section.
  13336. */
  13337. +#ifndef CONFIG_PREEMPT_RT_FULL
  13338. static inline unsigned read_seqbegin(const seqlock_t *sl)
  13339. {
  13340. return read_seqcount_begin(&sl->seqcount);
  13341. }
  13342. +#else
  13343. +/*
  13344. + * Starvation safe read side for RT
  13345. + */
  13346. +static inline unsigned read_seqbegin(seqlock_t *sl)
  13347. +{
  13348. + unsigned ret;
  13349. +
  13350. +repeat:
  13351. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  13352. + if (unlikely(ret & 1)) {
  13353. + /*
  13354. + * Take the lock and let the writer proceed (i.e. evtl
  13355. + * boost it), otherwise we could loop here forever.
  13356. + */
  13357. + spin_unlock_wait(&sl->lock);
  13358. + goto repeat;
  13359. + }
  13360. + return ret;
  13361. +}
  13362. +#endif
  13363. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13364. {
  13365. @@ -443,36 +475,36 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  13366. static inline void write_seqlock(seqlock_t *sl)
  13367. {
  13368. spin_lock(&sl->lock);
  13369. - write_seqcount_begin(&sl->seqcount);
  13370. + __raw_write_seqcount_begin(&sl->seqcount);
  13371. }
  13372. static inline void write_sequnlock(seqlock_t *sl)
  13373. {
  13374. - write_seqcount_end(&sl->seqcount);
  13375. + __raw_write_seqcount_end(&sl->seqcount);
  13376. spin_unlock(&sl->lock);
  13377. }
  13378. static inline void write_seqlock_bh(seqlock_t *sl)
  13379. {
  13380. spin_lock_bh(&sl->lock);
  13381. - write_seqcount_begin(&sl->seqcount);
  13382. + __raw_write_seqcount_begin(&sl->seqcount);
  13383. }
  13384. static inline void write_sequnlock_bh(seqlock_t *sl)
  13385. {
  13386. - write_seqcount_end(&sl->seqcount);
  13387. + __raw_write_seqcount_end(&sl->seqcount);
  13388. spin_unlock_bh(&sl->lock);
  13389. }
  13390. static inline void write_seqlock_irq(seqlock_t *sl)
  13391. {
  13392. spin_lock_irq(&sl->lock);
  13393. - write_seqcount_begin(&sl->seqcount);
  13394. + __raw_write_seqcount_begin(&sl->seqcount);
  13395. }
  13396. static inline void write_sequnlock_irq(seqlock_t *sl)
  13397. {
  13398. - write_seqcount_end(&sl->seqcount);
  13399. + __raw_write_seqcount_end(&sl->seqcount);
  13400. spin_unlock_irq(&sl->lock);
  13401. }
  13402. @@ -481,7 +513,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13403. unsigned long flags;
  13404. spin_lock_irqsave(&sl->lock, flags);
  13405. - write_seqcount_begin(&sl->seqcount);
  13406. + __raw_write_seqcount_begin(&sl->seqcount);
  13407. return flags;
  13408. }
  13409. @@ -491,7 +523,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  13410. static inline void
  13411. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  13412. {
  13413. - write_seqcount_end(&sl->seqcount);
  13414. + __raw_write_seqcount_end(&sl->seqcount);
  13415. spin_unlock_irqrestore(&sl->lock, flags);
  13416. }
  13417. diff --git a/include/linux/signal.h b/include/linux/signal.h
  13418. index d80259afb9e5..ddd1e6866a54 100644
  13419. --- a/include/linux/signal.h
  13420. +++ b/include/linux/signal.h
  13421. @@ -233,6 +233,7 @@ static inline void init_sigpending(struct sigpending *sig)
  13422. }
  13423. extern void flush_sigqueue(struct sigpending *queue);
  13424. +extern void flush_task_sigqueue(struct task_struct *tsk);
  13425. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  13426. static inline int valid_signal(unsigned long sig)
  13427. diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
  13428. index d443d9ab0236..2d1c7f9b7fd0 100644
  13429. --- a/include/linux/skbuff.h
  13430. +++ b/include/linux/skbuff.h
  13431. @@ -203,6 +203,7 @@ struct sk_buff_head {
  13432. __u32 qlen;
  13433. spinlock_t lock;
  13434. + raw_spinlock_t raw_lock;
  13435. };
  13436. struct sk_buff;
  13437. @@ -1465,6 +1466,12 @@ static inline void skb_queue_head_init(struct sk_buff_head *list)
  13438. __skb_queue_head_init(list);
  13439. }
  13440. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  13441. +{
  13442. + raw_spin_lock_init(&list->raw_lock);
  13443. + __skb_queue_head_init(list);
  13444. +}
  13445. +
  13446. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  13447. struct lock_class_key *class)
  13448. {
  13449. diff --git a/include/linux/smp.h b/include/linux/smp.h
  13450. index c4414074bd88..e6ab36aeaaab 100644
  13451. --- a/include/linux/smp.h
  13452. +++ b/include/linux/smp.h
  13453. @@ -185,6 +185,9 @@ static inline void smp_init(void) { }
  13454. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  13455. #define put_cpu() preempt_enable()
  13456. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  13457. +#define put_cpu_light() migrate_enable()
  13458. +
  13459. /*
  13460. * Callback to arch code if there's nosmp or maxcpus=0 on the
  13461. * boot command line:
  13462. diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
  13463. index 47dd0cebd204..b241cc044bd3 100644
  13464. --- a/include/linux/spinlock.h
  13465. +++ b/include/linux/spinlock.h
  13466. @@ -271,7 +271,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13467. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  13468. /* Include rwlock functions */
  13469. -#include <linux/rwlock.h>
  13470. +#ifdef CONFIG_PREEMPT_RT_FULL
  13471. +# include <linux/rwlock_rt.h>
  13472. +#else
  13473. +# include <linux/rwlock.h>
  13474. +#endif
  13475. /*
  13476. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  13477. @@ -282,6 +286,10 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  13478. # include <linux/spinlock_api_up.h>
  13479. #endif
  13480. +#ifdef CONFIG_PREEMPT_RT_FULL
  13481. +# include <linux/spinlock_rt.h>
  13482. +#else /* PREEMPT_RT_FULL */
  13483. +
  13484. /*
  13485. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  13486. */
  13487. @@ -416,4 +424,6 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  13488. #define atomic_dec_and_lock(atomic, lock) \
  13489. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  13490. +#endif /* !PREEMPT_RT_FULL */
  13491. +
  13492. #endif /* __LINUX_SPINLOCK_H */
  13493. diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_smp.h
  13494. index 5344268e6e62..043263f30e81 100644
  13495. --- a/include/linux/spinlock_api_smp.h
  13496. +++ b/include/linux/spinlock_api_smp.h
  13497. @@ -189,6 +189,8 @@ static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
  13498. return 0;
  13499. }
  13500. -#include <linux/rwlock_api_smp.h>
  13501. +#ifndef CONFIG_PREEMPT_RT_FULL
  13502. +# include <linux/rwlock_api_smp.h>
  13503. +#endif
  13504. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  13505. diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h
  13506. new file mode 100644
  13507. index 000000000000..3b2825537531
  13508. --- /dev/null
  13509. +++ b/include/linux/spinlock_rt.h
  13510. @@ -0,0 +1,163 @@
  13511. +#ifndef __LINUX_SPINLOCK_RT_H
  13512. +#define __LINUX_SPINLOCK_RT_H
  13513. +
  13514. +#ifndef __LINUX_SPINLOCK_H
  13515. +#error Do not include directly. Use spinlock.h
  13516. +#endif
  13517. +
  13518. +#include <linux/bug.h>
  13519. +
  13520. +extern void
  13521. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  13522. +
  13523. +#define spin_lock_init(slock) \
  13524. +do { \
  13525. + static struct lock_class_key __key; \
  13526. + \
  13527. + rt_mutex_init(&(slock)->lock); \
  13528. + __rt_spin_lock_init(slock, #slock, &__key); \
  13529. +} while (0)
  13530. +
  13531. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  13532. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  13533. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  13534. +
  13535. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  13536. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  13537. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  13538. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  13539. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  13540. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  13541. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  13542. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  13543. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  13544. +
  13545. +/*
  13546. + * lockdep-less calls, for derived types like rwlock:
  13547. + * (for trylock they can use rt_mutex_trylock() directly.
  13548. + */
  13549. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  13550. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  13551. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  13552. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  13553. +
  13554. +#define spin_lock(lock) rt_spin_lock(lock)
  13555. +
  13556. +#define spin_lock_bh(lock) \
  13557. + do { \
  13558. + local_bh_disable(); \
  13559. + rt_spin_lock(lock); \
  13560. + } while (0)
  13561. +
  13562. +#define spin_lock_irq(lock) spin_lock(lock)
  13563. +
  13564. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  13565. +
  13566. +#define spin_trylock(lock) \
  13567. +({ \
  13568. + int __locked; \
  13569. + __locked = spin_do_trylock(lock); \
  13570. + __locked; \
  13571. +})
  13572. +
  13573. +#ifdef CONFIG_LOCKDEP
  13574. +# define spin_lock_nested(lock, subclass) \
  13575. + do { \
  13576. + rt_spin_lock_nested(lock, subclass); \
  13577. + } while (0)
  13578. +
  13579. +#define spin_lock_bh_nested(lock, subclass) \
  13580. + do { \
  13581. + local_bh_disable(); \
  13582. + rt_spin_lock_nested(lock, subclass); \
  13583. + } while (0)
  13584. +
  13585. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13586. + do { \
  13587. + typecheck(unsigned long, flags); \
  13588. + flags = 0; \
  13589. + rt_spin_lock_nested(lock, subclass); \
  13590. + } while (0)
  13591. +#else
  13592. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  13593. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  13594. +
  13595. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  13596. + do { \
  13597. + typecheck(unsigned long, flags); \
  13598. + flags = 0; \
  13599. + spin_lock(lock); \
  13600. + } while (0)
  13601. +#endif
  13602. +
  13603. +#define spin_lock_irqsave(lock, flags) \
  13604. + do { \
  13605. + typecheck(unsigned long, flags); \
  13606. + flags = 0; \
  13607. + spin_lock(lock); \
  13608. + } while (0)
  13609. +
  13610. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  13611. +{
  13612. + unsigned long flags = 0;
  13613. +#ifdef CONFIG_TRACE_IRQFLAGS
  13614. + flags = rt_spin_lock_trace_flags(lock);
  13615. +#else
  13616. + spin_lock(lock); /* lock_local */
  13617. +#endif
  13618. + return flags;
  13619. +}
  13620. +
  13621. +/* FIXME: we need rt_spin_lock_nest_lock */
  13622. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  13623. +
  13624. +#define spin_unlock(lock) rt_spin_unlock(lock)
  13625. +
  13626. +#define spin_unlock_bh(lock) \
  13627. + do { \
  13628. + rt_spin_unlock(lock); \
  13629. + local_bh_enable(); \
  13630. + } while (0)
  13631. +
  13632. +#define spin_unlock_irq(lock) spin_unlock(lock)
  13633. +
  13634. +#define spin_unlock_irqrestore(lock, flags) \
  13635. + do { \
  13636. + typecheck(unsigned long, flags); \
  13637. + (void) flags; \
  13638. + spin_unlock(lock); \
  13639. + } while (0)
  13640. +
  13641. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  13642. +#define spin_trylock_irq(lock) spin_trylock(lock)
  13643. +
  13644. +#define spin_trylock_irqsave(lock, flags) \
  13645. + rt_spin_trylock_irqsave(lock, &(flags))
  13646. +
  13647. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  13648. +
  13649. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13650. +# define spin_is_contended(lock) ((lock)->break_lock)
  13651. +#else
  13652. +# define spin_is_contended(lock) (((void)(lock), 0))
  13653. +#endif
  13654. +
  13655. +static inline int spin_can_lock(spinlock_t *lock)
  13656. +{
  13657. + return !rt_mutex_is_locked(&lock->lock);
  13658. +}
  13659. +
  13660. +static inline int spin_is_locked(spinlock_t *lock)
  13661. +{
  13662. + return rt_mutex_is_locked(&lock->lock);
  13663. +}
  13664. +
  13665. +static inline void assert_spin_locked(spinlock_t *lock)
  13666. +{
  13667. + BUG_ON(!spin_is_locked(lock));
  13668. +}
  13669. +
  13670. +#define atomic_dec_and_lock(atomic, lock) \
  13671. + atomic_dec_and_spin_lock(atomic, lock)
  13672. +
  13673. +#endif
  13674. diff --git a/include/linux/spinlock_types.h b/include/linux/spinlock_types.h
  13675. index 73548eb13a5d..10bac715ea96 100644
  13676. --- a/include/linux/spinlock_types.h
  13677. +++ b/include/linux/spinlock_types.h
  13678. @@ -9,80 +9,15 @@
  13679. * Released under the General Public License (GPL).
  13680. */
  13681. -#if defined(CONFIG_SMP)
  13682. -# include <asm/spinlock_types.h>
  13683. -#else
  13684. -# include <linux/spinlock_types_up.h>
  13685. -#endif
  13686. -
  13687. -#include <linux/lockdep.h>
  13688. -
  13689. -typedef struct raw_spinlock {
  13690. - arch_spinlock_t raw_lock;
  13691. -#ifdef CONFIG_GENERIC_LOCKBREAK
  13692. - unsigned int break_lock;
  13693. -#endif
  13694. -#ifdef CONFIG_DEBUG_SPINLOCK
  13695. - unsigned int magic, owner_cpu;
  13696. - void *owner;
  13697. -#endif
  13698. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13699. - struct lockdep_map dep_map;
  13700. -#endif
  13701. -} raw_spinlock_t;
  13702. -
  13703. -#define SPINLOCK_MAGIC 0xdead4ead
  13704. -
  13705. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13706. -
  13707. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13708. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13709. -#else
  13710. -# define SPIN_DEP_MAP_INIT(lockname)
  13711. -#endif
  13712. +#include <linux/spinlock_types_raw.h>
  13713. -#ifdef CONFIG_DEBUG_SPINLOCK
  13714. -# define SPIN_DEBUG_INIT(lockname) \
  13715. - .magic = SPINLOCK_MAGIC, \
  13716. - .owner_cpu = -1, \
  13717. - .owner = SPINLOCK_OWNER_INIT,
  13718. +#ifndef CONFIG_PREEMPT_RT_FULL
  13719. +# include <linux/spinlock_types_nort.h>
  13720. +# include <linux/rwlock_types.h>
  13721. #else
  13722. -# define SPIN_DEBUG_INIT(lockname)
  13723. +# include <linux/rtmutex.h>
  13724. +# include <linux/spinlock_types_rt.h>
  13725. +# include <linux/rwlock_types_rt.h>
  13726. #endif
  13727. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13728. - { \
  13729. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13730. - SPIN_DEBUG_INIT(lockname) \
  13731. - SPIN_DEP_MAP_INIT(lockname) }
  13732. -
  13733. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13734. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13735. -
  13736. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13737. -
  13738. -typedef struct spinlock {
  13739. - union {
  13740. - struct raw_spinlock rlock;
  13741. -
  13742. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13743. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13744. - struct {
  13745. - u8 __padding[LOCK_PADSIZE];
  13746. - struct lockdep_map dep_map;
  13747. - };
  13748. -#endif
  13749. - };
  13750. -} spinlock_t;
  13751. -
  13752. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  13753. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13754. -
  13755. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  13756. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13757. -
  13758. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13759. -
  13760. -#include <linux/rwlock_types.h>
  13761. -
  13762. #endif /* __LINUX_SPINLOCK_TYPES_H */
  13763. diff --git a/include/linux/spinlock_types_nort.h b/include/linux/spinlock_types_nort.h
  13764. new file mode 100644
  13765. index 000000000000..f1dac1fb1d6a
  13766. --- /dev/null
  13767. +++ b/include/linux/spinlock_types_nort.h
  13768. @@ -0,0 +1,33 @@
  13769. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  13770. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  13771. +
  13772. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13773. +#error "Do not include directly. Include spinlock_types.h instead"
  13774. +#endif
  13775. +
  13776. +/*
  13777. + * The non RT version maps spinlocks to raw_spinlocks
  13778. + */
  13779. +typedef struct spinlock {
  13780. + union {
  13781. + struct raw_spinlock rlock;
  13782. +
  13783. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13784. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  13785. + struct {
  13786. + u8 __padding[LOCK_PADSIZE];
  13787. + struct lockdep_map dep_map;
  13788. + };
  13789. +#endif
  13790. + };
  13791. +} spinlock_t;
  13792. +
  13793. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  13794. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  13795. +
  13796. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  13797. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  13798. +
  13799. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  13800. +
  13801. +#endif
  13802. diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h
  13803. new file mode 100644
  13804. index 000000000000..edffc4d53fc9
  13805. --- /dev/null
  13806. +++ b/include/linux/spinlock_types_raw.h
  13807. @@ -0,0 +1,56 @@
  13808. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  13809. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  13810. +
  13811. +#if defined(CONFIG_SMP)
  13812. +# include <asm/spinlock_types.h>
  13813. +#else
  13814. +# include <linux/spinlock_types_up.h>
  13815. +#endif
  13816. +
  13817. +#include <linux/lockdep.h>
  13818. +
  13819. +typedef struct raw_spinlock {
  13820. + arch_spinlock_t raw_lock;
  13821. +#ifdef CONFIG_GENERIC_LOCKBREAK
  13822. + unsigned int break_lock;
  13823. +#endif
  13824. +#ifdef CONFIG_DEBUG_SPINLOCK
  13825. + unsigned int magic, owner_cpu;
  13826. + void *owner;
  13827. +#endif
  13828. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13829. + struct lockdep_map dep_map;
  13830. +#endif
  13831. +} raw_spinlock_t;
  13832. +
  13833. +#define SPINLOCK_MAGIC 0xdead4ead
  13834. +
  13835. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  13836. +
  13837. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13838. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  13839. +#else
  13840. +# define SPIN_DEP_MAP_INIT(lockname)
  13841. +#endif
  13842. +
  13843. +#ifdef CONFIG_DEBUG_SPINLOCK
  13844. +# define SPIN_DEBUG_INIT(lockname) \
  13845. + .magic = SPINLOCK_MAGIC, \
  13846. + .owner_cpu = -1, \
  13847. + .owner = SPINLOCK_OWNER_INIT,
  13848. +#else
  13849. +# define SPIN_DEBUG_INIT(lockname)
  13850. +#endif
  13851. +
  13852. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  13853. + { \
  13854. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  13855. + SPIN_DEBUG_INIT(lockname) \
  13856. + SPIN_DEP_MAP_INIT(lockname) }
  13857. +
  13858. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  13859. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  13860. +
  13861. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  13862. +
  13863. +#endif
  13864. diff --git a/include/linux/spinlock_types_rt.h b/include/linux/spinlock_types_rt.h
  13865. new file mode 100644
  13866. index 000000000000..9fd431967abc
  13867. --- /dev/null
  13868. +++ b/include/linux/spinlock_types_rt.h
  13869. @@ -0,0 +1,51 @@
  13870. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  13871. +#define __LINUX_SPINLOCK_TYPES_RT_H
  13872. +
  13873. +#ifndef __LINUX_SPINLOCK_TYPES_H
  13874. +#error "Do not include directly. Include spinlock_types.h instead"
  13875. +#endif
  13876. +
  13877. +#include <linux/cache.h>
  13878. +
  13879. +/*
  13880. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  13881. + */
  13882. +typedef struct spinlock {
  13883. + struct rt_mutex lock;
  13884. + unsigned int break_lock;
  13885. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13886. + struct lockdep_map dep_map;
  13887. +#endif
  13888. +} spinlock_t;
  13889. +
  13890. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  13891. +# define __RT_SPIN_INITIALIZER(name) \
  13892. + { \
  13893. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13894. + .save_state = 1, \
  13895. + .file = __FILE__, \
  13896. + .line = __LINE__ , \
  13897. + }
  13898. +#else
  13899. +# define __RT_SPIN_INITIALIZER(name) \
  13900. + { \
  13901. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  13902. + .save_state = 1, \
  13903. + }
  13904. +#endif
  13905. +
  13906. +/*
  13907. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  13908. +*/
  13909. +
  13910. +#define __SPIN_LOCK_UNLOCKED(name) \
  13911. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  13912. + SPIN_DEP_MAP_INIT(name) }
  13913. +
  13914. +#define __DEFINE_SPINLOCK(name) \
  13915. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  13916. +
  13917. +#define DEFINE_SPINLOCK(name) \
  13918. + spinlock_t name __cacheline_aligned_in_smp = __SPIN_LOCK_UNLOCKED(name)
  13919. +
  13920. +#endif
  13921. diff --git a/include/linux/srcu.h b/include/linux/srcu.h
  13922. index f5f80c5643ac..ec1a8f01563c 100644
  13923. --- a/include/linux/srcu.h
  13924. +++ b/include/linux/srcu.h
  13925. @@ -84,10 +84,10 @@ int init_srcu_struct(struct srcu_struct *sp);
  13926. void process_srcu(struct work_struct *work);
  13927. -#define __SRCU_STRUCT_INIT(name) \
  13928. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  13929. { \
  13930. .completed = -300, \
  13931. - .per_cpu_ref = &name##_srcu_array, \
  13932. + .per_cpu_ref = &pcpu_name, \
  13933. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  13934. .running = false, \
  13935. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  13936. @@ -104,7 +104,7 @@ void process_srcu(struct work_struct *work);
  13937. */
  13938. #define __DEFINE_SRCU(name, is_static) \
  13939. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  13940. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  13941. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  13942. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  13943. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  13944. diff --git a/include/linux/suspend.h b/include/linux/suspend.h
  13945. index 8b6ec7ef0854..9b77d4cc929f 100644
  13946. --- a/include/linux/suspend.h
  13947. +++ b/include/linux/suspend.h
  13948. @@ -194,6 +194,12 @@ struct platform_freeze_ops {
  13949. void (*end)(void);
  13950. };
  13951. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  13952. +extern bool pm_in_action;
  13953. +#else
  13954. +# define pm_in_action false
  13955. +#endif
  13956. +
  13957. #ifdef CONFIG_SUSPEND
  13958. /**
  13959. * suspend_set_ops - set platform dependent suspend operations
  13960. diff --git a/include/linux/swait.h b/include/linux/swait.h
  13961. new file mode 100644
  13962. index 000000000000..83f004a72320
  13963. --- /dev/null
  13964. +++ b/include/linux/swait.h
  13965. @@ -0,0 +1,173 @@
  13966. +#ifndef _LINUX_SWAIT_H
  13967. +#define _LINUX_SWAIT_H
  13968. +
  13969. +#include <linux/list.h>
  13970. +#include <linux/stddef.h>
  13971. +#include <linux/spinlock.h>
  13972. +#include <asm/current.h>
  13973. +
  13974. +/*
  13975. + * Simple wait queues
  13976. + *
  13977. + * While these are very similar to the other/complex wait queues (wait.h) the
  13978. + * most important difference is that the simple waitqueue allows for
  13979. + * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  13980. + * times.
  13981. + *
  13982. + * In order to make this so, we had to drop a fair number of features of the
  13983. + * other waitqueue code; notably:
  13984. + *
  13985. + * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  13986. + * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  13987. + * sleeper state.
  13988. + *
  13989. + * - the exclusive mode; because this requires preserving the list order
  13990. + * and this is hard.
  13991. + *
  13992. + * - custom wake functions; because you cannot give any guarantees about
  13993. + * random code.
  13994. + *
  13995. + * As a side effect of this; the data structures are slimmer.
  13996. + *
  13997. + * One would recommend using this wait queue where possible.
  13998. + */
  13999. +
  14000. +struct task_struct;
  14001. +
  14002. +struct swait_queue_head {
  14003. + raw_spinlock_t lock;
  14004. + struct list_head task_list;
  14005. +};
  14006. +
  14007. +struct swait_queue {
  14008. + struct task_struct *task;
  14009. + struct list_head task_list;
  14010. +};
  14011. +
  14012. +#define __SWAITQUEUE_INITIALIZER(name) { \
  14013. + .task = current, \
  14014. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14015. +}
  14016. +
  14017. +#define DECLARE_SWAITQUEUE(name) \
  14018. + struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  14019. +
  14020. +#define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  14021. + .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  14022. + .task_list = LIST_HEAD_INIT((name).task_list), \
  14023. +}
  14024. +
  14025. +#define DECLARE_SWAIT_QUEUE_HEAD(name) \
  14026. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  14027. +
  14028. +extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  14029. + struct lock_class_key *key);
  14030. +
  14031. +#define init_swait_queue_head(q) \
  14032. + do { \
  14033. + static struct lock_class_key __key; \
  14034. + __init_swait_queue_head((q), #q, &__key); \
  14035. + } while (0)
  14036. +
  14037. +#ifdef CONFIG_LOCKDEP
  14038. +# define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  14039. + ({ init_swait_queue_head(&name); name; })
  14040. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14041. + struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  14042. +#else
  14043. +# define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  14044. + DECLARE_SWAIT_QUEUE_HEAD(name)
  14045. +#endif
  14046. +
  14047. +static inline int swait_active(struct swait_queue_head *q)
  14048. +{
  14049. + return !list_empty(&q->task_list);
  14050. +}
  14051. +
  14052. +extern void swake_up(struct swait_queue_head *q);
  14053. +extern void swake_up_all(struct swait_queue_head *q);
  14054. +extern void swake_up_locked(struct swait_queue_head *q);
  14055. +extern void swake_up_all_locked(struct swait_queue_head *q);
  14056. +
  14057. +extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14058. +extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14059. +extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  14060. +
  14061. +extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14062. +extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  14063. +
  14064. +/* as per ___wait_event() but for swait, therefore "exclusive == 0" */
  14065. +#define ___swait_event(wq, condition, state, ret, cmd) \
  14066. +({ \
  14067. + struct swait_queue __wait; \
  14068. + long __ret = ret; \
  14069. + \
  14070. + INIT_LIST_HEAD(&__wait.task_list); \
  14071. + for (;;) { \
  14072. + long __int = prepare_to_swait_event(&wq, &__wait, state);\
  14073. + \
  14074. + if (condition) \
  14075. + break; \
  14076. + \
  14077. + if (___wait_is_interruptible(state) && __int) { \
  14078. + __ret = __int; \
  14079. + break; \
  14080. + } \
  14081. + \
  14082. + cmd; \
  14083. + } \
  14084. + finish_swait(&wq, &__wait); \
  14085. + __ret; \
  14086. +})
  14087. +
  14088. +#define __swait_event(wq, condition) \
  14089. + (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  14090. + schedule())
  14091. +
  14092. +#define swait_event(wq, condition) \
  14093. +do { \
  14094. + if (condition) \
  14095. + break; \
  14096. + __swait_event(wq, condition); \
  14097. +} while (0)
  14098. +
  14099. +#define __swait_event_timeout(wq, condition, timeout) \
  14100. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14101. + TASK_UNINTERRUPTIBLE, timeout, \
  14102. + __ret = schedule_timeout(__ret))
  14103. +
  14104. +#define swait_event_timeout(wq, condition, timeout) \
  14105. +({ \
  14106. + long __ret = timeout; \
  14107. + if (!___wait_cond_timeout(condition)) \
  14108. + __ret = __swait_event_timeout(wq, condition, timeout); \
  14109. + __ret; \
  14110. +})
  14111. +
  14112. +#define __swait_event_interruptible(wq, condition) \
  14113. + ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  14114. + schedule())
  14115. +
  14116. +#define swait_event_interruptible(wq, condition) \
  14117. +({ \
  14118. + int __ret = 0; \
  14119. + if (!(condition)) \
  14120. + __ret = __swait_event_interruptible(wq, condition); \
  14121. + __ret; \
  14122. +})
  14123. +
  14124. +#define __swait_event_interruptible_timeout(wq, condition, timeout) \
  14125. + ___swait_event(wq, ___wait_cond_timeout(condition), \
  14126. + TASK_INTERRUPTIBLE, timeout, \
  14127. + __ret = schedule_timeout(__ret))
  14128. +
  14129. +#define swait_event_interruptible_timeout(wq, condition, timeout) \
  14130. +({ \
  14131. + long __ret = timeout; \
  14132. + if (!___wait_cond_timeout(condition)) \
  14133. + __ret = __swait_event_interruptible_timeout(wq, \
  14134. + condition, timeout); \
  14135. + __ret; \
  14136. +})
  14137. +
  14138. +#endif /* _LINUX_SWAIT_H */
  14139. diff --git a/include/linux/swap.h b/include/linux/swap.h
  14140. index 7ba7dccaf0e7..da646f2eb3c6 100644
  14141. --- a/include/linux/swap.h
  14142. +++ b/include/linux/swap.h
  14143. @@ -11,6 +11,7 @@
  14144. #include <linux/fs.h>
  14145. #include <linux/atomic.h>
  14146. #include <linux/page-flags.h>
  14147. +#include <linux/locallock.h>
  14148. #include <asm/page.h>
  14149. struct notifier_block;
  14150. @@ -252,7 +253,8 @@ struct swap_info_struct {
  14151. void *workingset_eviction(struct address_space *mapping, struct page *page);
  14152. bool workingset_refault(void *shadow);
  14153. void workingset_activation(struct page *page);
  14154. -extern struct list_lru workingset_shadow_nodes;
  14155. +extern struct list_lru __workingset_shadow_nodes;
  14156. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  14157. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  14158. {
  14159. @@ -296,6 +298,7 @@ extern unsigned long nr_free_pagecache_pages(void);
  14160. /* linux/mm/swap.c */
  14161. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  14162. extern void lru_cache_add(struct page *);
  14163. extern void lru_cache_add_anon(struct page *page);
  14164. extern void lru_cache_add_file(struct page *page);
  14165. diff --git a/include/linux/swork.h b/include/linux/swork.h
  14166. new file mode 100644
  14167. index 000000000000..f175fa9a6016
  14168. --- /dev/null
  14169. +++ b/include/linux/swork.h
  14170. @@ -0,0 +1,24 @@
  14171. +#ifndef _LINUX_SWORK_H
  14172. +#define _LINUX_SWORK_H
  14173. +
  14174. +#include <linux/list.h>
  14175. +
  14176. +struct swork_event {
  14177. + struct list_head item;
  14178. + unsigned long flags;
  14179. + void (*func)(struct swork_event *);
  14180. +};
  14181. +
  14182. +static inline void INIT_SWORK(struct swork_event *event,
  14183. + void (*func)(struct swork_event *))
  14184. +{
  14185. + event->flags = 0;
  14186. + event->func = func;
  14187. +}
  14188. +
  14189. +bool swork_queue(struct swork_event *sev);
  14190. +
  14191. +int swork_get(void);
  14192. +void swork_put(void);
  14193. +
  14194. +#endif /* _LINUX_SWORK_H */
  14195. diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
  14196. index ff307b548ed3..be9f9dc6a4e1 100644
  14197. --- a/include/linux/thread_info.h
  14198. +++ b/include/linux/thread_info.h
  14199. @@ -102,7 +102,17 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  14200. #define test_thread_flag(flag) \
  14201. test_ti_thread_flag(current_thread_info(), flag)
  14202. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14203. +#ifdef CONFIG_PREEMPT_LAZY
  14204. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  14205. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14206. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  14207. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  14208. +
  14209. +#else
  14210. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  14211. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  14212. +#define tif_need_resched_lazy() 0
  14213. +#endif
  14214. #if defined TIF_RESTORE_SIGMASK && !defined HAVE_SET_RESTORE_SIGMASK
  14215. /*
  14216. diff --git a/include/linux/timer.h b/include/linux/timer.h
  14217. index 61aa61dc410c..299d2b78591f 100644
  14218. --- a/include/linux/timer.h
  14219. +++ b/include/linux/timer.h
  14220. @@ -225,7 +225,7 @@ extern void add_timer(struct timer_list *timer);
  14221. extern int try_to_del_timer_sync(struct timer_list *timer);
  14222. -#ifdef CONFIG_SMP
  14223. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  14224. extern int del_timer_sync(struct timer_list *timer);
  14225. #else
  14226. # define del_timer_sync(t) del_timer(t)
  14227. diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
  14228. index 925730bc9fc1..a591f414da6f 100644
  14229. --- a/include/linux/trace_events.h
  14230. +++ b/include/linux/trace_events.h
  14231. @@ -66,6 +66,9 @@ struct trace_entry {
  14232. unsigned char flags;
  14233. unsigned char preempt_count;
  14234. int pid;
  14235. + unsigned short migrate_disable;
  14236. + unsigned short padding;
  14237. + unsigned char preempt_lazy_count;
  14238. };
  14239. #define TRACE_EVENT_TYPE_MAX \
  14240. diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
  14241. index 558129af828a..cf5c472bbc79 100644
  14242. --- a/include/linux/uaccess.h
  14243. +++ b/include/linux/uaccess.h
  14244. @@ -24,6 +24,7 @@ static __always_inline void pagefault_disabled_dec(void)
  14245. */
  14246. static inline void pagefault_disable(void)
  14247. {
  14248. + migrate_disable();
  14249. pagefault_disabled_inc();
  14250. /*
  14251. * make sure to have issued the store before a pagefault
  14252. @@ -40,6 +41,7 @@ static inline void pagefault_enable(void)
  14253. */
  14254. barrier();
  14255. pagefault_disabled_dec();
  14256. + migrate_enable();
  14257. }
  14258. /*
  14259. diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
  14260. index 4a29c75b146e..0a294e950df8 100644
  14261. --- a/include/linux/uprobes.h
  14262. +++ b/include/linux/uprobes.h
  14263. @@ -27,6 +27,7 @@
  14264. #include <linux/errno.h>
  14265. #include <linux/rbtree.h>
  14266. #include <linux/types.h>
  14267. +#include <linux/wait.h>
  14268. struct vm_area_struct;
  14269. struct mm_struct;
  14270. diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
  14271. index 3e5d9075960f..7eaa847cd5a5 100644
  14272. --- a/include/linux/vmstat.h
  14273. +++ b/include/linux/vmstat.h
  14274. @@ -33,7 +33,9 @@ DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
  14275. */
  14276. static inline void __count_vm_event(enum vm_event_item item)
  14277. {
  14278. + preempt_disable_rt();
  14279. raw_cpu_inc(vm_event_states.event[item]);
  14280. + preempt_enable_rt();
  14281. }
  14282. static inline void count_vm_event(enum vm_event_item item)
  14283. @@ -43,7 +45,9 @@ static inline void count_vm_event(enum vm_event_item item)
  14284. static inline void __count_vm_events(enum vm_event_item item, long delta)
  14285. {
  14286. + preempt_disable_rt();
  14287. raw_cpu_add(vm_event_states.event[item], delta);
  14288. + preempt_enable_rt();
  14289. }
  14290. static inline void count_vm_events(enum vm_event_item item, long delta)
  14291. diff --git a/include/linux/wait.h b/include/linux/wait.h
  14292. index 513b36f04dfd..981c8a840f96 100644
  14293. --- a/include/linux/wait.h
  14294. +++ b/include/linux/wait.h
  14295. @@ -8,6 +8,7 @@
  14296. #include <linux/spinlock.h>
  14297. #include <asm/current.h>
  14298. #include <uapi/linux/wait.h>
  14299. +#include <linux/atomic.h>
  14300. typedef struct __wait_queue wait_queue_t;
  14301. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  14302. diff --git a/include/net/dst.h b/include/net/dst.h
  14303. index c7329dcd90cc..35c3dba16728 100644
  14304. --- a/include/net/dst.h
  14305. +++ b/include/net/dst.h
  14306. @@ -437,7 +437,7 @@ static inline void dst_confirm(struct dst_entry *dst)
  14307. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  14308. struct sk_buff *skb)
  14309. {
  14310. - const struct hh_cache *hh;
  14311. + struct hh_cache *hh;
  14312. if (dst->pending_confirm) {
  14313. unsigned long now = jiffies;
  14314. diff --git a/include/net/neighbour.h b/include/net/neighbour.h
  14315. index 8b683841e574..bf656008f6e7 100644
  14316. --- a/include/net/neighbour.h
  14317. +++ b/include/net/neighbour.h
  14318. @@ -446,7 +446,7 @@ static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  14319. }
  14320. #endif
  14321. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  14322. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  14323. {
  14324. unsigned int seq;
  14325. int hh_len;
  14326. @@ -501,7 +501,7 @@ struct neighbour_cb {
  14327. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  14328. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  14329. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  14330. const struct net_device *dev)
  14331. {
  14332. unsigned int seq;
  14333. diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
  14334. index c68926b4899c..dd0751e76065 100644
  14335. --- a/include/net/netns/ipv4.h
  14336. +++ b/include/net/netns/ipv4.h
  14337. @@ -70,6 +70,7 @@ struct netns_ipv4 {
  14338. int sysctl_icmp_echo_ignore_all;
  14339. int sysctl_icmp_echo_ignore_broadcasts;
  14340. + int sysctl_icmp_echo_sysrq;
  14341. int sysctl_icmp_ignore_bogus_error_responses;
  14342. int sysctl_icmp_ratelimit;
  14343. int sysctl_icmp_ratemask;
  14344. diff --git a/include/trace/events/hist.h b/include/trace/events/hist.h
  14345. new file mode 100644
  14346. index 000000000000..f7710de1b1f3
  14347. --- /dev/null
  14348. +++ b/include/trace/events/hist.h
  14349. @@ -0,0 +1,73 @@
  14350. +#undef TRACE_SYSTEM
  14351. +#define TRACE_SYSTEM hist
  14352. +
  14353. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  14354. +#define _TRACE_HIST_H
  14355. +
  14356. +#include "latency_hist.h"
  14357. +#include <linux/tracepoint.h>
  14358. +
  14359. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  14360. +#define trace_preemptirqsoff_hist(a, b)
  14361. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  14362. +#else
  14363. +TRACE_EVENT(preemptirqsoff_hist,
  14364. +
  14365. + TP_PROTO(int reason, int starthist),
  14366. +
  14367. + TP_ARGS(reason, starthist),
  14368. +
  14369. + TP_STRUCT__entry(
  14370. + __field(int, reason)
  14371. + __field(int, starthist)
  14372. + ),
  14373. +
  14374. + TP_fast_assign(
  14375. + __entry->reason = reason;
  14376. + __entry->starthist = starthist;
  14377. + ),
  14378. +
  14379. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  14380. + __entry->starthist ? "start" : "stop")
  14381. +);
  14382. +#endif
  14383. +
  14384. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  14385. +#define trace_hrtimer_interrupt(a, b, c, d)
  14386. +#else
  14387. +TRACE_EVENT(hrtimer_interrupt,
  14388. +
  14389. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  14390. + struct task_struct *task),
  14391. +
  14392. + TP_ARGS(cpu, offset, curr, task),
  14393. +
  14394. + TP_STRUCT__entry(
  14395. + __field(int, cpu)
  14396. + __field(long long, offset)
  14397. + __array(char, ccomm, TASK_COMM_LEN)
  14398. + __field(int, cprio)
  14399. + __array(char, tcomm, TASK_COMM_LEN)
  14400. + __field(int, tprio)
  14401. + ),
  14402. +
  14403. + TP_fast_assign(
  14404. + __entry->cpu = cpu;
  14405. + __entry->offset = offset;
  14406. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  14407. + __entry->cprio = curr->prio;
  14408. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  14409. + task != NULL ? TASK_COMM_LEN : 7);
  14410. + __entry->tprio = task != NULL ? task->prio : -1;
  14411. + ),
  14412. +
  14413. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  14414. + __entry->cpu, __entry->offset, __entry->ccomm,
  14415. + __entry->cprio, __entry->tcomm, __entry->tprio)
  14416. +);
  14417. +#endif
  14418. +
  14419. +#endif /* _TRACE_HIST_H */
  14420. +
  14421. +/* This part must be outside protection */
  14422. +#include <trace/define_trace.h>
  14423. diff --git a/include/trace/events/latency_hist.h b/include/trace/events/latency_hist.h
  14424. new file mode 100644
  14425. index 000000000000..d3f2fbd560b1
  14426. --- /dev/null
  14427. +++ b/include/trace/events/latency_hist.h
  14428. @@ -0,0 +1,29 @@
  14429. +#ifndef _LATENCY_HIST_H
  14430. +#define _LATENCY_HIST_H
  14431. +
  14432. +enum hist_action {
  14433. + IRQS_ON,
  14434. + PREEMPT_ON,
  14435. + TRACE_STOP,
  14436. + IRQS_OFF,
  14437. + PREEMPT_OFF,
  14438. + TRACE_START,
  14439. +};
  14440. +
  14441. +static char *actions[] = {
  14442. + "IRQS_ON",
  14443. + "PREEMPT_ON",
  14444. + "TRACE_STOP",
  14445. + "IRQS_OFF",
  14446. + "PREEMPT_OFF",
  14447. + "TRACE_START",
  14448. +};
  14449. +
  14450. +static inline char *getaction(int action)
  14451. +{
  14452. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  14453. + return actions[action];
  14454. + return "unknown";
  14455. +}
  14456. +
  14457. +#endif /* _LATENCY_HIST_H */
  14458. diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
  14459. index fff846b512e6..73614ce1d204 100644
  14460. --- a/include/trace/events/writeback.h
  14461. +++ b/include/trace/events/writeback.h
  14462. @@ -134,58 +134,28 @@ DEFINE_EVENT(writeback_dirty_inode_template, writeback_dirty_inode,
  14463. #ifdef CREATE_TRACE_POINTS
  14464. #ifdef CONFIG_CGROUP_WRITEBACK
  14465. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14466. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14467. {
  14468. - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1;
  14469. + return wb->memcg_css->cgroup->kn->ino;
  14470. }
  14471. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14472. -{
  14473. - struct cgroup *cgrp = wb->memcg_css->cgroup;
  14474. - char *path;
  14475. -
  14476. - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1);
  14477. - WARN_ON_ONCE(path != buf);
  14478. -}
  14479. -
  14480. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14481. -{
  14482. - if (wbc->wb)
  14483. - return __trace_wb_cgroup_size(wbc->wb);
  14484. - else
  14485. - return 2;
  14486. -}
  14487. -
  14488. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14489. - struct writeback_control *wbc)
  14490. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14491. {
  14492. if (wbc->wb)
  14493. - __trace_wb_assign_cgroup(buf, wbc->wb);
  14494. + return __trace_wb_assign_cgroup(wbc->wb);
  14495. else
  14496. - strcpy(buf, "/");
  14497. + return -1U;
  14498. }
  14499. -
  14500. #else /* CONFIG_CGROUP_WRITEBACK */
  14501. -static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb)
  14502. -{
  14503. - return 2;
  14504. -}
  14505. -
  14506. -static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb)
  14507. -{
  14508. - strcpy(buf, "/");
  14509. -}
  14510. -
  14511. -static inline size_t __trace_wbc_cgroup_size(struct writeback_control *wbc)
  14512. +static inline unsigned int __trace_wb_assign_cgroup(struct bdi_writeback *wb)
  14513. {
  14514. - return 2;
  14515. + return -1U;
  14516. }
  14517. -static inline void __trace_wbc_assign_cgroup(char *buf,
  14518. - struct writeback_control *wbc)
  14519. +static inline unsigned int __trace_wbc_assign_cgroup(struct writeback_control *wbc)
  14520. {
  14521. - strcpy(buf, "/");
  14522. + return -1U;
  14523. }
  14524. #endif /* CONFIG_CGROUP_WRITEBACK */
  14525. @@ -201,7 +171,7 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14526. __array(char, name, 32)
  14527. __field(unsigned long, ino)
  14528. __field(int, sync_mode)
  14529. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14530. + __field(unsigned int, cgroup_ino)
  14531. ),
  14532. TP_fast_assign(
  14533. @@ -209,14 +179,14 @@ DECLARE_EVENT_CLASS(writeback_write_inode_template,
  14534. dev_name(inode_to_bdi(inode)->dev), 32);
  14535. __entry->ino = inode->i_ino;
  14536. __entry->sync_mode = wbc->sync_mode;
  14537. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14538. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14539. ),
  14540. - TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup=%s",
  14541. + TP_printk("bdi %s: ino=%lu sync_mode=%d cgroup_ino=%u",
  14542. __entry->name,
  14543. __entry->ino,
  14544. __entry->sync_mode,
  14545. - __get_str(cgroup)
  14546. + __entry->cgroup_ino
  14547. )
  14548. );
  14549. @@ -246,7 +216,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14550. __field(int, range_cyclic)
  14551. __field(int, for_background)
  14552. __field(int, reason)
  14553. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14554. + __field(unsigned int, cgroup_ino)
  14555. ),
  14556. TP_fast_assign(
  14557. strncpy(__entry->name,
  14558. @@ -258,10 +228,10 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14559. __entry->range_cyclic = work->range_cyclic;
  14560. __entry->for_background = work->for_background;
  14561. __entry->reason = work->reason;
  14562. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14563. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14564. ),
  14565. TP_printk("bdi %s: sb_dev %d:%d nr_pages=%ld sync_mode=%d "
  14566. - "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup=%s",
  14567. + "kupdate=%d range_cyclic=%d background=%d reason=%s cgroup_ino=%u",
  14568. __entry->name,
  14569. MAJOR(__entry->sb_dev), MINOR(__entry->sb_dev),
  14570. __entry->nr_pages,
  14571. @@ -270,7 +240,7 @@ DECLARE_EVENT_CLASS(writeback_work_class,
  14572. __entry->range_cyclic,
  14573. __entry->for_background,
  14574. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14575. - __get_str(cgroup)
  14576. + __entry->cgroup_ino
  14577. )
  14578. );
  14579. #define DEFINE_WRITEBACK_WORK_EVENT(name) \
  14580. @@ -300,15 +270,15 @@ DECLARE_EVENT_CLASS(writeback_class,
  14581. TP_ARGS(wb),
  14582. TP_STRUCT__entry(
  14583. __array(char, name, 32)
  14584. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14585. + __field(unsigned int, cgroup_ino)
  14586. ),
  14587. TP_fast_assign(
  14588. strncpy(__entry->name, dev_name(wb->bdi->dev), 32);
  14589. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14590. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14591. ),
  14592. - TP_printk("bdi %s: cgroup=%s",
  14593. + TP_printk("bdi %s: cgroup_ino=%u",
  14594. __entry->name,
  14595. - __get_str(cgroup)
  14596. + __entry->cgroup_ino
  14597. )
  14598. );
  14599. #define DEFINE_WRITEBACK_EVENT(name) \
  14600. @@ -347,7 +317,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  14601. __field(int, range_cyclic)
  14602. __field(long, range_start)
  14603. __field(long, range_end)
  14604. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14605. + __field(unsigned int, cgroup_ino)
  14606. ),
  14607. TP_fast_assign(
  14608. @@ -361,12 +331,12 @@ DECLARE_EVENT_CLASS(wbc_class,
  14609. __entry->range_cyclic = wbc->range_cyclic;
  14610. __entry->range_start = (long)wbc->range_start;
  14611. __entry->range_end = (long)wbc->range_end;
  14612. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14613. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14614. ),
  14615. TP_printk("bdi %s: towrt=%ld skip=%ld mode=%d kupd=%d "
  14616. "bgrd=%d reclm=%d cyclic=%d "
  14617. - "start=0x%lx end=0x%lx cgroup=%s",
  14618. + "start=0x%lx end=0x%lx cgroup_ino=%u",
  14619. __entry->name,
  14620. __entry->nr_to_write,
  14621. __entry->pages_skipped,
  14622. @@ -377,7 +347,7 @@ DECLARE_EVENT_CLASS(wbc_class,
  14623. __entry->range_cyclic,
  14624. __entry->range_start,
  14625. __entry->range_end,
  14626. - __get_str(cgroup)
  14627. + __entry->cgroup_ino
  14628. )
  14629. )
  14630. @@ -398,7 +368,7 @@ TRACE_EVENT(writeback_queue_io,
  14631. __field(long, age)
  14632. __field(int, moved)
  14633. __field(int, reason)
  14634. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14635. + __field(unsigned int, cgroup_ino)
  14636. ),
  14637. TP_fast_assign(
  14638. unsigned long *older_than_this = work->older_than_this;
  14639. @@ -408,15 +378,15 @@ TRACE_EVENT(writeback_queue_io,
  14640. (jiffies - *older_than_this) * 1000 / HZ : -1;
  14641. __entry->moved = moved;
  14642. __entry->reason = work->reason;
  14643. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14644. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14645. ),
  14646. - TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup=%s",
  14647. + TP_printk("bdi %s: older=%lu age=%ld enqueue=%d reason=%s cgroup_ino=%u",
  14648. __entry->name,
  14649. __entry->older, /* older_than_this in jiffies */
  14650. __entry->age, /* older_than_this in relative milliseconds */
  14651. __entry->moved,
  14652. __print_symbolic(__entry->reason, WB_WORK_REASON),
  14653. - __get_str(cgroup)
  14654. + __entry->cgroup_ino
  14655. )
  14656. );
  14657. @@ -484,7 +454,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14658. __field(unsigned long, dirty_ratelimit)
  14659. __field(unsigned long, task_ratelimit)
  14660. __field(unsigned long, balanced_dirty_ratelimit)
  14661. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14662. + __field(unsigned int, cgroup_ino)
  14663. ),
  14664. TP_fast_assign(
  14665. @@ -496,13 +466,13 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14666. __entry->task_ratelimit = KBps(task_ratelimit);
  14667. __entry->balanced_dirty_ratelimit =
  14668. KBps(wb->balanced_dirty_ratelimit);
  14669. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14670. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14671. ),
  14672. TP_printk("bdi %s: "
  14673. "write_bw=%lu awrite_bw=%lu dirty_rate=%lu "
  14674. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14675. - "balanced_dirty_ratelimit=%lu cgroup=%s",
  14676. + "balanced_dirty_ratelimit=%lu cgroup_ino=%u",
  14677. __entry->bdi,
  14678. __entry->write_bw, /* write bandwidth */
  14679. __entry->avg_write_bw, /* avg write bandwidth */
  14680. @@ -510,7 +480,7 @@ TRACE_EVENT(bdi_dirty_ratelimit,
  14681. __entry->dirty_ratelimit, /* base ratelimit */
  14682. __entry->task_ratelimit, /* ratelimit with position control */
  14683. __entry->balanced_dirty_ratelimit, /* the balanced ratelimit */
  14684. - __get_str(cgroup)
  14685. + __entry->cgroup_ino
  14686. )
  14687. );
  14688. @@ -548,7 +518,7 @@ TRACE_EVENT(balance_dirty_pages,
  14689. __field( long, pause)
  14690. __field(unsigned long, period)
  14691. __field( long, think)
  14692. - __dynamic_array(char, cgroup, __trace_wb_cgroup_size(wb))
  14693. + __field(unsigned int, cgroup_ino)
  14694. ),
  14695. TP_fast_assign(
  14696. @@ -571,7 +541,7 @@ TRACE_EVENT(balance_dirty_pages,
  14697. __entry->period = period * 1000 / HZ;
  14698. __entry->pause = pause * 1000 / HZ;
  14699. __entry->paused = (jiffies - start_time) * 1000 / HZ;
  14700. - __trace_wb_assign_cgroup(__get_str(cgroup), wb);
  14701. + __entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
  14702. ),
  14703. @@ -580,7 +550,7 @@ TRACE_EVENT(balance_dirty_pages,
  14704. "bdi_setpoint=%lu bdi_dirty=%lu "
  14705. "dirty_ratelimit=%lu task_ratelimit=%lu "
  14706. "dirtied=%u dirtied_pause=%u "
  14707. - "paused=%lu pause=%ld period=%lu think=%ld cgroup=%s",
  14708. + "paused=%lu pause=%ld period=%lu think=%ld cgroup_ino=%u",
  14709. __entry->bdi,
  14710. __entry->limit,
  14711. __entry->setpoint,
  14712. @@ -595,7 +565,7 @@ TRACE_EVENT(balance_dirty_pages,
  14713. __entry->pause, /* ms */
  14714. __entry->period, /* ms */
  14715. __entry->think, /* ms */
  14716. - __get_str(cgroup)
  14717. + __entry->cgroup_ino
  14718. )
  14719. );
  14720. @@ -609,8 +579,7 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  14721. __field(unsigned long, ino)
  14722. __field(unsigned long, state)
  14723. __field(unsigned long, dirtied_when)
  14724. - __dynamic_array(char, cgroup,
  14725. - __trace_wb_cgroup_size(inode_to_wb(inode)))
  14726. + __field(unsigned int, cgroup_ino)
  14727. ),
  14728. TP_fast_assign(
  14729. @@ -619,16 +588,16 @@ TRACE_EVENT(writeback_sb_inodes_requeue,
  14730. __entry->ino = inode->i_ino;
  14731. __entry->state = inode->i_state;
  14732. __entry->dirtied_when = inode->dirtied_when;
  14733. - __trace_wb_assign_cgroup(__get_str(cgroup), inode_to_wb(inode));
  14734. + __entry->cgroup_ino = __trace_wb_assign_cgroup(inode_to_wb(inode));
  14735. ),
  14736. - TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup=%s",
  14737. + TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu cgroup_ino=%u",
  14738. __entry->name,
  14739. __entry->ino,
  14740. show_inode_state(__entry->state),
  14741. __entry->dirtied_when,
  14742. (jiffies - __entry->dirtied_when) / HZ,
  14743. - __get_str(cgroup)
  14744. + __entry->cgroup_ino
  14745. )
  14746. );
  14747. @@ -684,7 +653,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  14748. __field(unsigned long, writeback_index)
  14749. __field(long, nr_to_write)
  14750. __field(unsigned long, wrote)
  14751. - __dynamic_array(char, cgroup, __trace_wbc_cgroup_size(wbc))
  14752. + __field(unsigned int, cgroup_ino)
  14753. ),
  14754. TP_fast_assign(
  14755. @@ -696,11 +665,11 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  14756. __entry->writeback_index = inode->i_mapping->writeback_index;
  14757. __entry->nr_to_write = nr_to_write;
  14758. __entry->wrote = nr_to_write - wbc->nr_to_write;
  14759. - __trace_wbc_assign_cgroup(__get_str(cgroup), wbc);
  14760. + __entry->cgroup_ino = __trace_wbc_assign_cgroup(wbc);
  14761. ),
  14762. TP_printk("bdi %s: ino=%lu state=%s dirtied_when=%lu age=%lu "
  14763. - "index=%lu to_write=%ld wrote=%lu cgroup=%s",
  14764. + "index=%lu to_write=%ld wrote=%lu cgroup_ino=%u",
  14765. __entry->name,
  14766. __entry->ino,
  14767. show_inode_state(__entry->state),
  14768. @@ -709,7 +678,7 @@ DECLARE_EVENT_CLASS(writeback_single_inode_template,
  14769. __entry->writeback_index,
  14770. __entry->nr_to_write,
  14771. __entry->wrote,
  14772. - __get_str(cgroup)
  14773. + __entry->cgroup_ino
  14774. )
  14775. );
  14776. diff --git a/init/Kconfig b/init/Kconfig
  14777. index 235c7a2c0d20..a7c81c0911da 100644
  14778. --- a/init/Kconfig
  14779. +++ b/init/Kconfig
  14780. @@ -498,7 +498,7 @@ config TINY_RCU
  14781. config RCU_EXPERT
  14782. bool "Make expert-level adjustments to RCU configuration"
  14783. - default n
  14784. + default y if PREEMPT_RT_FULL
  14785. help
  14786. This option needs to be enabled if you wish to make
  14787. expert-level adjustments to RCU configuration. By default,
  14788. @@ -614,7 +614,7 @@ config RCU_FANOUT_LEAF
  14789. config RCU_FAST_NO_HZ
  14790. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  14791. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  14792. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  14793. default n
  14794. help
  14795. This option permits CPUs to enter dynticks-idle state even if
  14796. @@ -641,7 +641,7 @@ config TREE_RCU_TRACE
  14797. config RCU_BOOST
  14798. bool "Enable RCU priority boosting"
  14799. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  14800. - default n
  14801. + default y if PREEMPT_RT_FULL
  14802. help
  14803. This option boosts the priority of preempted RCU readers that
  14804. block the current preemptible RCU grace period for too long.
  14805. @@ -1106,6 +1106,7 @@ config CFS_BANDWIDTH
  14806. config RT_GROUP_SCHED
  14807. bool "Group scheduling for SCHED_RR/FIFO"
  14808. depends on CGROUP_SCHED
  14809. + depends on !PREEMPT_RT_FULL
  14810. default n
  14811. help
  14812. This feature lets you explicitly allocate real CPU bandwidth
  14813. @@ -1719,6 +1720,7 @@ choice
  14814. config SLAB
  14815. bool "SLAB"
  14816. + depends on !PREEMPT_RT_FULL
  14817. help
  14818. The regular slab allocator that is established and known to work
  14819. well in all environments. It organizes cache hot objects in
  14820. @@ -1737,6 +1739,7 @@ config SLUB
  14821. config SLOB
  14822. depends on EXPERT
  14823. bool "SLOB (Simple Allocator)"
  14824. + depends on !PREEMPT_RT_FULL
  14825. help
  14826. SLOB replaces the stock allocator with a drastically simpler
  14827. allocator. SLOB is generally more space efficient but
  14828. @@ -1746,7 +1749,7 @@ endchoice
  14829. config SLUB_CPU_PARTIAL
  14830. default y
  14831. - depends on SLUB && SMP
  14832. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  14833. bool "SLUB per cpu partial cache"
  14834. help
  14835. Per cpu partial caches accellerate objects allocation and freeing
  14836. diff --git a/init/Makefile b/init/Makefile
  14837. index 7bc47ee31c36..88cf473554e0 100644
  14838. --- a/init/Makefile
  14839. +++ b/init/Makefile
  14840. @@ -33,4 +33,4 @@ silent_chk_compile.h = :
  14841. include/generated/compile.h: FORCE
  14842. @$($(quiet)chk_compile.h)
  14843. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  14844. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  14845. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  14846. diff --git a/init/main.c b/init/main.c
  14847. index 9e64d7097f1a..4a76e629c137 100644
  14848. --- a/init/main.c
  14849. +++ b/init/main.c
  14850. @@ -530,6 +530,7 @@ asmlinkage __visible void __init start_kernel(void)
  14851. setup_command_line(command_line);
  14852. setup_nr_cpu_ids();
  14853. setup_per_cpu_areas();
  14854. + softirq_early_init();
  14855. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  14856. build_all_zonelists(NULL, NULL);
  14857. diff --git a/ipc/msg.c b/ipc/msg.c
  14858. index c6521c205cb4..996d89023552 100644
  14859. --- a/ipc/msg.c
  14860. +++ b/ipc/msg.c
  14861. @@ -183,20 +183,14 @@ static void ss_wakeup(struct list_head *h, int kill)
  14862. }
  14863. }
  14864. -static void expunge_all(struct msg_queue *msq, int res)
  14865. +static void expunge_all(struct msg_queue *msq, int res,
  14866. + struct wake_q_head *wake_q)
  14867. {
  14868. struct msg_receiver *msr, *t;
  14869. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  14870. - msr->r_msg = NULL; /* initialize expunge ordering */
  14871. - wake_up_process(msr->r_tsk);
  14872. - /*
  14873. - * Ensure that the wakeup is visible before setting r_msg as
  14874. - * the receiving end depends on it: either spinning on a nil,
  14875. - * or dealing with -EAGAIN cases. See lockless receive part 1
  14876. - * and 2 in do_msgrcv().
  14877. - */
  14878. - smp_wmb(); /* barrier (B) */
  14879. +
  14880. + wake_q_add(wake_q, msr->r_tsk);
  14881. msr->r_msg = ERR_PTR(res);
  14882. }
  14883. }
  14884. @@ -213,11 +207,13 @@ static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  14885. {
  14886. struct msg_msg *msg, *t;
  14887. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  14888. + WAKE_Q(wake_q);
  14889. - expunge_all(msq, -EIDRM);
  14890. + expunge_all(msq, -EIDRM, &wake_q);
  14891. ss_wakeup(&msq->q_senders, 1);
  14892. msg_rmid(ns, msq);
  14893. ipc_unlock_object(&msq->q_perm);
  14894. + wake_up_q(&wake_q);
  14895. rcu_read_unlock();
  14896. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  14897. @@ -342,6 +338,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  14898. struct kern_ipc_perm *ipcp;
  14899. struct msqid64_ds uninitialized_var(msqid64);
  14900. struct msg_queue *msq;
  14901. + WAKE_Q(wake_q);
  14902. int err;
  14903. if (cmd == IPC_SET) {
  14904. @@ -389,7 +386,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  14905. /* sleeping receivers might be excluded by
  14906. * stricter permissions.
  14907. */
  14908. - expunge_all(msq, -EAGAIN);
  14909. + expunge_all(msq, -EAGAIN, &wake_q);
  14910. /* sleeping senders might be able to send
  14911. * due to a larger queue size.
  14912. */
  14913. @@ -402,6 +399,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  14914. out_unlock0:
  14915. ipc_unlock_object(&msq->q_perm);
  14916. + wake_up_q(&wake_q);
  14917. out_unlock1:
  14918. rcu_read_unlock();
  14919. out_up:
  14920. @@ -566,7 +564,8 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
  14921. return 0;
  14922. }
  14923. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  14924. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  14925. + struct wake_q_head *wake_q)
  14926. {
  14927. struct msg_receiver *msr, *t;
  14928. @@ -577,27 +576,13 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  14929. list_del(&msr->r_list);
  14930. if (msr->r_maxsize < msg->m_ts) {
  14931. - /* initialize pipelined send ordering */
  14932. - msr->r_msg = NULL;
  14933. - wake_up_process(msr->r_tsk);
  14934. - /* barrier (B) see barrier comment below */
  14935. - smp_wmb();
  14936. + wake_q_add(wake_q, msr->r_tsk);
  14937. msr->r_msg = ERR_PTR(-E2BIG);
  14938. } else {
  14939. - msr->r_msg = NULL;
  14940. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  14941. msq->q_rtime = get_seconds();
  14942. - wake_up_process(msr->r_tsk);
  14943. - /*
  14944. - * Ensure that the wakeup is visible before
  14945. - * setting r_msg, as the receiving can otherwise
  14946. - * exit - once r_msg is set, the receiver can
  14947. - * continue. See lockless receive part 1 and 2
  14948. - * in do_msgrcv(). Barrier (B).
  14949. - */
  14950. - smp_wmb();
  14951. + wake_q_add(wake_q, msr->r_tsk);
  14952. msr->r_msg = msg;
  14953. -
  14954. return 1;
  14955. }
  14956. }
  14957. @@ -613,6 +598,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  14958. struct msg_msg *msg;
  14959. int err;
  14960. struct ipc_namespace *ns;
  14961. + WAKE_Q(wake_q);
  14962. ns = current->nsproxy->ipc_ns;
  14963. @@ -698,7 +684,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  14964. msq->q_lspid = task_tgid_vnr(current);
  14965. msq->q_stime = get_seconds();
  14966. - if (!pipelined_send(msq, msg)) {
  14967. + if (!pipelined_send(msq, msg, &wake_q)) {
  14968. /* no one is waiting for this message, enqueue it */
  14969. list_add_tail(&msg->m_list, &msq->q_messages);
  14970. msq->q_cbytes += msgsz;
  14971. @@ -712,6 +698,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  14972. out_unlock0:
  14973. ipc_unlock_object(&msq->q_perm);
  14974. + wake_up_q(&wake_q);
  14975. out_unlock1:
  14976. rcu_read_unlock();
  14977. if (msg != NULL)
  14978. @@ -932,57 +919,25 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgfl
  14979. rcu_read_lock();
  14980. /* Lockless receive, part 2:
  14981. - * Wait until pipelined_send or expunge_all are outside of
  14982. - * wake_up_process(). There is a race with exit(), see
  14983. - * ipc/mqueue.c for the details. The correct serialization
  14984. - * ensures that a receiver cannot continue without the wakeup
  14985. - * being visibible _before_ setting r_msg:
  14986. + * The work in pipelined_send() and expunge_all():
  14987. + * - Set pointer to message
  14988. + * - Queue the receiver task for later wakeup
  14989. + * - Wake up the process after the lock is dropped.
  14990. *
  14991. - * CPU 0 CPU 1
  14992. - * <loop receiver>
  14993. - * smp_rmb(); (A) <-- pair -. <waker thread>
  14994. - * <load ->r_msg> | msr->r_msg = NULL;
  14995. - * | wake_up_process();
  14996. - * <continue> `------> smp_wmb(); (B)
  14997. - * msr->r_msg = msg;
  14998. - *
  14999. - * Where (A) orders the message value read and where (B) orders
  15000. - * the write to the r_msg -- done in both pipelined_send and
  15001. - * expunge_all.
  15002. + * Should the process wake up before this wakeup (due to a
  15003. + * signal) it will either see the message and continue …
  15004. */
  15005. - for (;;) {
  15006. - /*
  15007. - * Pairs with writer barrier in pipelined_send
  15008. - * or expunge_all.
  15009. - */
  15010. - smp_rmb(); /* barrier (A) */
  15011. - msg = (struct msg_msg *)msr_d.r_msg;
  15012. - if (msg)
  15013. - break;
  15014. - /*
  15015. - * The cpu_relax() call is a compiler barrier
  15016. - * which forces everything in this loop to be
  15017. - * re-loaded.
  15018. - */
  15019. - cpu_relax();
  15020. - }
  15021. -
  15022. - /* Lockless receive, part 3:
  15023. - * If there is a message or an error then accept it without
  15024. - * locking.
  15025. - */
  15026. + msg = (struct msg_msg *)msr_d.r_msg;
  15027. if (msg != ERR_PTR(-EAGAIN))
  15028. goto out_unlock1;
  15029. - /* Lockless receive, part 3:
  15030. - * Acquire the queue spinlock.
  15031. - */
  15032. + /*
  15033. + * … or see -EAGAIN, acquire the lock to check the message
  15034. + * again.
  15035. + */
  15036. ipc_lock_object(&msq->q_perm);
  15037. - /* Lockless receive, part 4:
  15038. - * Repeat test after acquiring the spinlock.
  15039. - */
  15040. msg = (struct msg_msg *)msr_d.r_msg;
  15041. if (msg != ERR_PTR(-EAGAIN))
  15042. goto out_unlock0;
  15043. diff --git a/ipc/sem.c b/ipc/sem.c
  15044. index 20d07008ad5e..40b5cc070720 100644
  15045. --- a/ipc/sem.c
  15046. +++ b/ipc/sem.c
  15047. @@ -690,6 +690,13 @@ undo:
  15048. static void wake_up_sem_queue_prepare(struct list_head *pt,
  15049. struct sem_queue *q, int error)
  15050. {
  15051. +#ifdef CONFIG_PREEMPT_RT_BASE
  15052. + struct task_struct *p = q->sleeper;
  15053. + get_task_struct(p);
  15054. + q->status = error;
  15055. + wake_up_process(p);
  15056. + put_task_struct(p);
  15057. +#else
  15058. if (list_empty(pt)) {
  15059. /*
  15060. * Hold preempt off so that we don't get preempted and have the
  15061. @@ -701,6 +708,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15062. q->pid = error;
  15063. list_add_tail(&q->list, pt);
  15064. +#endif
  15065. }
  15066. /**
  15067. @@ -714,6 +722,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  15068. */
  15069. static void wake_up_sem_queue_do(struct list_head *pt)
  15070. {
  15071. +#ifndef CONFIG_PREEMPT_RT_BASE
  15072. struct sem_queue *q, *t;
  15073. int did_something;
  15074. @@ -726,6 +735,7 @@ static void wake_up_sem_queue_do(struct list_head *pt)
  15075. }
  15076. if (did_something)
  15077. preempt_enable();
  15078. +#endif
  15079. }
  15080. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  15081. diff --git a/kernel/Kconfig.locks b/kernel/Kconfig.locks
  15082. index ebdb0043203a..b9e6aa7e5aa6 100644
  15083. --- a/kernel/Kconfig.locks
  15084. +++ b/kernel/Kconfig.locks
  15085. @@ -225,11 +225,11 @@ config ARCH_SUPPORTS_ATOMIC_RMW
  15086. config MUTEX_SPIN_ON_OWNER
  15087. def_bool y
  15088. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  15089. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15090. config RWSEM_SPIN_ON_OWNER
  15091. def_bool y
  15092. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  15093. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  15094. config LOCK_SPIN_ON_OWNER
  15095. def_bool y
  15096. diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
  15097. index 3f9c97419f02..11dbe26a8279 100644
  15098. --- a/kernel/Kconfig.preempt
  15099. +++ b/kernel/Kconfig.preempt
  15100. @@ -1,3 +1,16 @@
  15101. +config PREEMPT
  15102. + bool
  15103. + select PREEMPT_COUNT
  15104. +
  15105. +config PREEMPT_RT_BASE
  15106. + bool
  15107. + select PREEMPT
  15108. +
  15109. +config HAVE_PREEMPT_LAZY
  15110. + bool
  15111. +
  15112. +config PREEMPT_LAZY
  15113. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  15114. choice
  15115. prompt "Preemption Model"
  15116. @@ -33,9 +46,9 @@ config PREEMPT_VOLUNTARY
  15117. Select this if you are building a kernel for a desktop system.
  15118. -config PREEMPT
  15119. +config PREEMPT__LL
  15120. bool "Preemptible Kernel (Low-Latency Desktop)"
  15121. - select PREEMPT_COUNT
  15122. + select PREEMPT
  15123. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  15124. help
  15125. This option reduces the latency of the kernel by making
  15126. @@ -52,6 +65,22 @@ config PREEMPT
  15127. embedded system with latency requirements in the milliseconds
  15128. range.
  15129. +config PREEMPT_RTB
  15130. + bool "Preemptible Kernel (Basic RT)"
  15131. + select PREEMPT_RT_BASE
  15132. + help
  15133. + This option is basically the same as (Low-Latency Desktop) but
  15134. + enables changes which are preliminary for the full preemptible
  15135. + RT kernel.
  15136. +
  15137. +config PREEMPT_RT_FULL
  15138. + bool "Fully Preemptible Kernel (RT)"
  15139. + depends on IRQ_FORCED_THREADING
  15140. + select PREEMPT_RT_BASE
  15141. + select PREEMPT_RCU
  15142. + help
  15143. + All and everything
  15144. +
  15145. endchoice
  15146. config PREEMPT_COUNT
  15147. diff --git a/kernel/cgroup.c b/kernel/cgroup.c
  15148. index a3424f28aaf4..69434d231e21 100644
  15149. --- a/kernel/cgroup.c
  15150. +++ b/kernel/cgroup.c
  15151. @@ -4737,10 +4737,10 @@ static void css_free_rcu_fn(struct rcu_head *rcu_head)
  15152. queue_work(cgroup_destroy_wq, &css->destroy_work);
  15153. }
  15154. -static void css_release_work_fn(struct work_struct *work)
  15155. +static void css_release_work_fn(struct swork_event *sev)
  15156. {
  15157. struct cgroup_subsys_state *css =
  15158. - container_of(work, struct cgroup_subsys_state, destroy_work);
  15159. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  15160. struct cgroup_subsys *ss = css->ss;
  15161. struct cgroup *cgrp = css->cgroup;
  15162. @@ -4779,8 +4779,8 @@ static void css_release(struct percpu_ref *ref)
  15163. struct cgroup_subsys_state *css =
  15164. container_of(ref, struct cgroup_subsys_state, refcnt);
  15165. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  15166. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  15167. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  15168. + swork_queue(&css->destroy_swork);
  15169. }
  15170. static void init_and_link_css(struct cgroup_subsys_state *css,
  15171. @@ -5397,6 +5397,7 @@ static int __init cgroup_wq_init(void)
  15172. */
  15173. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  15174. BUG_ON(!cgroup_destroy_wq);
  15175. + BUG_ON(swork_get());
  15176. /*
  15177. * Used to destroy pidlists and separate to serve as flush domain.
  15178. diff --git a/kernel/cpu.c b/kernel/cpu.c
  15179. index 85ff5e26e23b..8edd3c716092 100644
  15180. --- a/kernel/cpu.c
  15181. +++ b/kernel/cpu.c
  15182. @@ -75,8 +75,8 @@ static struct {
  15183. #endif
  15184. } cpu_hotplug = {
  15185. .active_writer = NULL,
  15186. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15187. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  15188. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  15189. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  15190. .dep_map = {.name = "cpu_hotplug.lock" },
  15191. #endif
  15192. @@ -89,6 +89,289 @@ static struct {
  15193. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  15194. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  15195. +/**
  15196. + * hotplug_pcp - per cpu hotplug descriptor
  15197. + * @unplug: set when pin_current_cpu() needs to sync tasks
  15198. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  15199. + * @refcount: counter of tasks in pinned sections
  15200. + * @grab_lock: set when the tasks entering pinned sections should wait
  15201. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  15202. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  15203. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  15204. + *
  15205. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  15206. + * is used as a flag and still exists after @sync_tsk has exited and
  15207. + * @sync_tsk set to NULL.
  15208. + */
  15209. +struct hotplug_pcp {
  15210. + struct task_struct *unplug;
  15211. + struct task_struct *sync_tsk;
  15212. + int refcount;
  15213. + int grab_lock;
  15214. + struct completion synced;
  15215. + struct completion unplug_wait;
  15216. +#ifdef CONFIG_PREEMPT_RT_FULL
  15217. + /*
  15218. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  15219. + * the task, otherwise the mutex will cause the task to fail
  15220. + * to sleep when required. (Because it's called from migrate_disable())
  15221. + *
  15222. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  15223. + * state.
  15224. + */
  15225. + spinlock_t lock;
  15226. +#else
  15227. + struct mutex mutex;
  15228. +#endif
  15229. + int mutex_init;
  15230. +};
  15231. +
  15232. +#ifdef CONFIG_PREEMPT_RT_FULL
  15233. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  15234. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  15235. +#else
  15236. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  15237. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  15238. +#endif
  15239. +
  15240. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  15241. +
  15242. +/**
  15243. + * pin_current_cpu - Prevent the current cpu from being unplugged
  15244. + *
  15245. + * Lightweight version of get_online_cpus() to prevent cpu from being
  15246. + * unplugged when code runs in a migration disabled region.
  15247. + *
  15248. + * Must be called with preemption disabled (preempt_count = 1)!
  15249. + */
  15250. +void pin_current_cpu(void)
  15251. +{
  15252. + struct hotplug_pcp *hp;
  15253. + int force = 0;
  15254. +
  15255. +retry:
  15256. + hp = this_cpu_ptr(&hotplug_pcp);
  15257. +
  15258. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  15259. + hp->unplug == current) {
  15260. + hp->refcount++;
  15261. + return;
  15262. + }
  15263. + if (hp->grab_lock) {
  15264. + preempt_enable();
  15265. + hotplug_lock(hp);
  15266. + hotplug_unlock(hp);
  15267. + } else {
  15268. + preempt_enable();
  15269. + /*
  15270. + * Try to push this task off of this CPU.
  15271. + */
  15272. + if (!migrate_me()) {
  15273. + preempt_disable();
  15274. + hp = this_cpu_ptr(&hotplug_pcp);
  15275. + if (!hp->grab_lock) {
  15276. + /*
  15277. + * Just let it continue it's already pinned
  15278. + * or about to sleep.
  15279. + */
  15280. + force = 1;
  15281. + goto retry;
  15282. + }
  15283. + preempt_enable();
  15284. + }
  15285. + }
  15286. + preempt_disable();
  15287. + goto retry;
  15288. +}
  15289. +
  15290. +/**
  15291. + * unpin_current_cpu - Allow unplug of current cpu
  15292. + *
  15293. + * Must be called with preemption or interrupts disabled!
  15294. + */
  15295. +void unpin_current_cpu(void)
  15296. +{
  15297. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  15298. +
  15299. + WARN_ON(hp->refcount <= 0);
  15300. +
  15301. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  15302. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  15303. + wake_up_process(hp->unplug);
  15304. +}
  15305. +
  15306. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  15307. +{
  15308. + set_current_state(TASK_UNINTERRUPTIBLE);
  15309. + while (hp->refcount) {
  15310. + schedule_preempt_disabled();
  15311. + set_current_state(TASK_UNINTERRUPTIBLE);
  15312. + }
  15313. +}
  15314. +
  15315. +static int sync_unplug_thread(void *data)
  15316. +{
  15317. + struct hotplug_pcp *hp = data;
  15318. +
  15319. + wait_for_completion(&hp->unplug_wait);
  15320. + preempt_disable();
  15321. + hp->unplug = current;
  15322. + wait_for_pinned_cpus(hp);
  15323. +
  15324. + /*
  15325. + * This thread will synchronize the cpu_down() with threads
  15326. + * that have pinned the CPU. When the pinned CPU count reaches
  15327. + * zero, we inform the cpu_down code to continue to the next step.
  15328. + */
  15329. + set_current_state(TASK_UNINTERRUPTIBLE);
  15330. + preempt_enable();
  15331. + complete(&hp->synced);
  15332. +
  15333. + /*
  15334. + * If all succeeds, the next step will need tasks to wait till
  15335. + * the CPU is offline before continuing. To do this, the grab_lock
  15336. + * is set and tasks going into pin_current_cpu() will block on the
  15337. + * mutex. But we still need to wait for those that are already in
  15338. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  15339. + * will kick this thread out.
  15340. + */
  15341. + while (!hp->grab_lock && !kthread_should_stop()) {
  15342. + schedule();
  15343. + set_current_state(TASK_UNINTERRUPTIBLE);
  15344. + }
  15345. +
  15346. + /* Make sure grab_lock is seen before we see a stale completion */
  15347. + smp_mb();
  15348. +
  15349. + /*
  15350. + * Now just before cpu_down() enters stop machine, we need to make
  15351. + * sure all tasks that are in pinned CPU sections are out, and new
  15352. + * tasks will now grab the lock, keeping them from entering pinned
  15353. + * CPU sections.
  15354. + */
  15355. + if (!kthread_should_stop()) {
  15356. + preempt_disable();
  15357. + wait_for_pinned_cpus(hp);
  15358. + preempt_enable();
  15359. + complete(&hp->synced);
  15360. + }
  15361. +
  15362. + set_current_state(TASK_UNINTERRUPTIBLE);
  15363. + while (!kthread_should_stop()) {
  15364. + schedule();
  15365. + set_current_state(TASK_UNINTERRUPTIBLE);
  15366. + }
  15367. + set_current_state(TASK_RUNNING);
  15368. +
  15369. + /*
  15370. + * Force this thread off this CPU as it's going down and
  15371. + * we don't want any more work on this CPU.
  15372. + */
  15373. + current->flags &= ~PF_NO_SETAFFINITY;
  15374. + set_cpus_allowed_ptr(current, cpu_present_mask);
  15375. + migrate_me();
  15376. + return 0;
  15377. +}
  15378. +
  15379. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  15380. +{
  15381. + wake_up_process(hp->sync_tsk);
  15382. + wait_for_completion(&hp->synced);
  15383. +}
  15384. +
  15385. +static void __cpu_unplug_wait(unsigned int cpu)
  15386. +{
  15387. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15388. +
  15389. + complete(&hp->unplug_wait);
  15390. + wait_for_completion(&hp->synced);
  15391. +}
  15392. +
  15393. +/*
  15394. + * Start the sync_unplug_thread on the target cpu and wait for it to
  15395. + * complete.
  15396. + */
  15397. +static int cpu_unplug_begin(unsigned int cpu)
  15398. +{
  15399. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15400. + int err;
  15401. +
  15402. + /* Protected by cpu_hotplug.lock */
  15403. + if (!hp->mutex_init) {
  15404. +#ifdef CONFIG_PREEMPT_RT_FULL
  15405. + spin_lock_init(&hp->lock);
  15406. +#else
  15407. + mutex_init(&hp->mutex);
  15408. +#endif
  15409. + hp->mutex_init = 1;
  15410. + }
  15411. +
  15412. + /* Inform the scheduler to migrate tasks off this CPU */
  15413. + tell_sched_cpu_down_begin(cpu);
  15414. +
  15415. + init_completion(&hp->synced);
  15416. + init_completion(&hp->unplug_wait);
  15417. +
  15418. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  15419. + if (IS_ERR(hp->sync_tsk)) {
  15420. + err = PTR_ERR(hp->sync_tsk);
  15421. + hp->sync_tsk = NULL;
  15422. + return err;
  15423. + }
  15424. + kthread_bind(hp->sync_tsk, cpu);
  15425. +
  15426. + /*
  15427. + * Wait for tasks to get out of the pinned sections,
  15428. + * it's still OK if new tasks enter. Some CPU notifiers will
  15429. + * wait for tasks that are going to enter these sections and
  15430. + * we must not have them block.
  15431. + */
  15432. + wake_up_process(hp->sync_tsk);
  15433. + return 0;
  15434. +}
  15435. +
  15436. +static void cpu_unplug_sync(unsigned int cpu)
  15437. +{
  15438. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15439. +
  15440. + init_completion(&hp->synced);
  15441. + /* The completion needs to be initialzied before setting grab_lock */
  15442. + smp_wmb();
  15443. +
  15444. + /* Grab the mutex before setting grab_lock */
  15445. + hotplug_lock(hp);
  15446. + hp->grab_lock = 1;
  15447. +
  15448. + /*
  15449. + * The CPU notifiers have been completed.
  15450. + * Wait for tasks to get out of pinned CPU sections and have new
  15451. + * tasks block until the CPU is completely down.
  15452. + */
  15453. + __cpu_unplug_sync(hp);
  15454. +
  15455. + /* All done with the sync thread */
  15456. + kthread_stop(hp->sync_tsk);
  15457. + hp->sync_tsk = NULL;
  15458. +}
  15459. +
  15460. +static void cpu_unplug_done(unsigned int cpu)
  15461. +{
  15462. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  15463. +
  15464. + hp->unplug = NULL;
  15465. + /* Let all tasks know cpu unplug is finished before cleaning up */
  15466. + smp_wmb();
  15467. +
  15468. + if (hp->sync_tsk)
  15469. + kthread_stop(hp->sync_tsk);
  15470. +
  15471. + if (hp->grab_lock) {
  15472. + hotplug_unlock(hp);
  15473. + /* protected by cpu_hotplug.lock */
  15474. + hp->grab_lock = 0;
  15475. + }
  15476. + tell_sched_cpu_down_done(cpu);
  15477. +}
  15478. void get_online_cpus(void)
  15479. {
  15480. @@ -338,13 +621,15 @@ static int take_cpu_down(void *_param)
  15481. /* Requires cpu_add_remove_lock to be held */
  15482. static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15483. {
  15484. - int err, nr_calls = 0;
  15485. + int mycpu, err, nr_calls = 0;
  15486. void *hcpu = (void *)(long)cpu;
  15487. unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
  15488. struct take_cpu_down_param tcd_param = {
  15489. .mod = mod,
  15490. .hcpu = hcpu,
  15491. };
  15492. + cpumask_var_t cpumask;
  15493. + cpumask_var_t cpumask_org;
  15494. if (num_online_cpus() == 1)
  15495. return -EBUSY;
  15496. @@ -352,7 +637,34 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15497. if (!cpu_online(cpu))
  15498. return -EINVAL;
  15499. + /* Move the downtaker off the unplug cpu */
  15500. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  15501. + return -ENOMEM;
  15502. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  15503. + free_cpumask_var(cpumask);
  15504. + return -ENOMEM;
  15505. + }
  15506. +
  15507. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  15508. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  15509. + set_cpus_allowed_ptr(current, cpumask);
  15510. + free_cpumask_var(cpumask);
  15511. + migrate_disable();
  15512. + mycpu = smp_processor_id();
  15513. + if (mycpu == cpu) {
  15514. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  15515. + migrate_enable();
  15516. + err = -EBUSY;
  15517. + goto restore_cpus;
  15518. + }
  15519. + migrate_enable();
  15520. +
  15521. cpu_hotplug_begin();
  15522. + err = cpu_unplug_begin(cpu);
  15523. + if (err) {
  15524. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  15525. + goto out_cancel;
  15526. + }
  15527. err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
  15528. if (err) {
  15529. @@ -378,8 +690,12 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15530. else
  15531. synchronize_rcu();
  15532. + __cpu_unplug_wait(cpu);
  15533. smpboot_park_threads(cpu);
  15534. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  15535. + cpu_unplug_sync(cpu);
  15536. +
  15537. /*
  15538. * Prevent irq alloc/free while the dying cpu reorganizes the
  15539. * interrupt affinities.
  15540. @@ -424,9 +740,14 @@ static int _cpu_down(unsigned int cpu, int tasks_frozen)
  15541. check_for_tasks(cpu);
  15542. out_release:
  15543. + cpu_unplug_done(cpu);
  15544. +out_cancel:
  15545. cpu_hotplug_done();
  15546. if (!err)
  15547. cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
  15548. +restore_cpus:
  15549. + set_cpus_allowed_ptr(current, cpumask_org);
  15550. + free_cpumask_var(cpumask_org);
  15551. return err;
  15552. }
  15553. diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
  15554. index fc1ef736253c..83c666537a7a 100644
  15555. --- a/kernel/debug/kdb/kdb_io.c
  15556. +++ b/kernel/debug/kdb/kdb_io.c
  15557. @@ -554,7 +554,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  15558. int linecount;
  15559. int colcount;
  15560. int logging, saved_loglevel = 0;
  15561. - int saved_trap_printk;
  15562. int got_printf_lock = 0;
  15563. int retlen = 0;
  15564. int fnd, len;
  15565. @@ -565,8 +564,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  15566. unsigned long uninitialized_var(flags);
  15567. preempt_disable();
  15568. - saved_trap_printk = kdb_trap_printk;
  15569. - kdb_trap_printk = 0;
  15570. /* Serialize kdb_printf if multiple cpus try to write at once.
  15571. * But if any cpu goes recursive in kdb, just print the output,
  15572. @@ -855,7 +852,6 @@ kdb_print_out:
  15573. } else {
  15574. __release(kdb_printf_lock);
  15575. }
  15576. - kdb_trap_printk = saved_trap_printk;
  15577. preempt_enable();
  15578. return retlen;
  15579. }
  15580. @@ -865,9 +861,11 @@ int kdb_printf(const char *fmt, ...)
  15581. va_list ap;
  15582. int r;
  15583. + kdb_trap_printk++;
  15584. va_start(ap, fmt);
  15585. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  15586. va_end(ap);
  15587. + kdb_trap_printk--;
  15588. return r;
  15589. }
  15590. diff --git a/kernel/events/core.c b/kernel/events/core.c
  15591. index 12ecd4f0329f..560ea99875d3 100644
  15592. --- a/kernel/events/core.c
  15593. +++ b/kernel/events/core.c
  15594. @@ -802,6 +802,7 @@ static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  15595. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  15596. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  15597. timer->function = perf_mux_hrtimer_handler;
  15598. + timer->irqsafe = 1;
  15599. }
  15600. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  15601. @@ -7219,6 +7220,7 @@ static void perf_swevent_init_hrtimer(struct perf_event *event)
  15602. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  15603. hwc->hrtimer.function = perf_swevent_hrtimer;
  15604. + hwc->hrtimer.irqsafe = 1;
  15605. /*
  15606. * Since hrtimers have a fixed rate, we can do a static freq->period
  15607. diff --git a/kernel/exit.c b/kernel/exit.c
  15608. index ffba5df4abd5..e199407f8831 100644
  15609. --- a/kernel/exit.c
  15610. +++ b/kernel/exit.c
  15611. @@ -144,7 +144,7 @@ static void __exit_signal(struct task_struct *tsk)
  15612. * Do this under ->siglock, we can race with another thread
  15613. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  15614. */
  15615. - flush_sigqueue(&tsk->pending);
  15616. + flush_task_sigqueue(tsk);
  15617. tsk->sighand = NULL;
  15618. spin_unlock(&sighand->siglock);
  15619. diff --git a/kernel/fork.c b/kernel/fork.c
  15620. index 1155eac61687..4e93b4ea33f7 100644
  15621. --- a/kernel/fork.c
  15622. +++ b/kernel/fork.c
  15623. @@ -108,7 +108,7 @@ int max_threads; /* tunable limit on nr_threads */
  15624. DEFINE_PER_CPU(unsigned long, process_counts) = 0;
  15625. -__cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */
  15626. +DEFINE_RWLOCK(tasklist_lock); /* outer */
  15627. #ifdef CONFIG_PROVE_RCU
  15628. int lockdep_tasklist_lock_is_held(void)
  15629. @@ -244,7 +244,9 @@ static inline void put_signal_struct(struct signal_struct *sig)
  15630. if (atomic_dec_and_test(&sig->sigcnt))
  15631. free_signal_struct(sig);
  15632. }
  15633. -
  15634. +#ifdef CONFIG_PREEMPT_RT_BASE
  15635. +static
  15636. +#endif
  15637. void __put_task_struct(struct task_struct *tsk)
  15638. {
  15639. WARN_ON(!tsk->exit_state);
  15640. @@ -261,7 +263,18 @@ void __put_task_struct(struct task_struct *tsk)
  15641. if (!profile_handoff_task(tsk))
  15642. free_task(tsk);
  15643. }
  15644. +#ifndef CONFIG_PREEMPT_RT_BASE
  15645. EXPORT_SYMBOL_GPL(__put_task_struct);
  15646. +#else
  15647. +void __put_task_struct_cb(struct rcu_head *rhp)
  15648. +{
  15649. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  15650. +
  15651. + __put_task_struct(tsk);
  15652. +
  15653. +}
  15654. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  15655. +#endif
  15656. void __init __weak arch_task_cache_init(void) { }
  15657. @@ -689,6 +702,19 @@ void __mmdrop(struct mm_struct *mm)
  15658. }
  15659. EXPORT_SYMBOL_GPL(__mmdrop);
  15660. +#ifdef CONFIG_PREEMPT_RT_BASE
  15661. +/*
  15662. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  15663. + * want another facility to make this work.
  15664. + */
  15665. +void __mmdrop_delayed(struct rcu_head *rhp)
  15666. +{
  15667. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  15668. +
  15669. + __mmdrop(mm);
  15670. +}
  15671. +#endif
  15672. +
  15673. /*
  15674. * Decrement the use count and release all resources for an mm.
  15675. */
  15676. @@ -1218,6 +1244,9 @@ static void rt_mutex_init_task(struct task_struct *p)
  15677. */
  15678. static void posix_cpu_timers_init(struct task_struct *tsk)
  15679. {
  15680. +#ifdef CONFIG_PREEMPT_RT_BASE
  15681. + tsk->posix_timer_list = NULL;
  15682. +#endif
  15683. tsk->cputime_expires.prof_exp = 0;
  15684. tsk->cputime_expires.virt_exp = 0;
  15685. tsk->cputime_expires.sched_exp = 0;
  15686. @@ -1343,15 +1372,16 @@ static struct task_struct *copy_process(unsigned long clone_flags,
  15687. spin_lock_init(&p->alloc_lock);
  15688. init_sigpending(&p->pending);
  15689. + p->sigqueue_cache = NULL;
  15690. p->utime = p->stime = p->gtime = 0;
  15691. p->utimescaled = p->stimescaled = 0;
  15692. prev_cputime_init(&p->prev_cputime);
  15693. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  15694. - seqlock_init(&p->vtime_seqlock);
  15695. + seqcount_init(&p->vtime_seqcount);
  15696. p->vtime_snap = 0;
  15697. - p->vtime_snap_whence = VTIME_SLEEPING;
  15698. + p->vtime_snap_whence = VTIME_INACTIVE;
  15699. #endif
  15700. #if defined(SPLIT_RSS_COUNTING)
  15701. diff --git a/kernel/futex.c b/kernel/futex.c
  15702. index 9d8163afd87c..ad38af0bcff3 100644
  15703. --- a/kernel/futex.c
  15704. +++ b/kernel/futex.c
  15705. @@ -815,7 +815,9 @@ void exit_pi_state_list(struct task_struct *curr)
  15706. * task still owns the PI-state:
  15707. */
  15708. if (head->next != next) {
  15709. + raw_spin_unlock_irq(&curr->pi_lock);
  15710. spin_unlock(&hb->lock);
  15711. + raw_spin_lock_irq(&curr->pi_lock);
  15712. continue;
  15713. }
  15714. @@ -1210,6 +1212,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  15715. struct futex_pi_state *pi_state = this->pi_state;
  15716. u32 uninitialized_var(curval), newval;
  15717. WAKE_Q(wake_q);
  15718. + WAKE_Q(wake_sleeper_q);
  15719. bool deboost;
  15720. int ret = 0;
  15721. @@ -1223,7 +1226,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  15722. if (pi_state->owner != current)
  15723. return -EINVAL;
  15724. - raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  15725. + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock);
  15726. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  15727. /*
  15728. @@ -1259,24 +1262,25 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  15729. ret = -EINVAL;
  15730. }
  15731. if (ret) {
  15732. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15733. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15734. return ret;
  15735. }
  15736. - raw_spin_lock_irq(&pi_state->owner->pi_lock);
  15737. + raw_spin_lock(&pi_state->owner->pi_lock);
  15738. WARN_ON(list_empty(&pi_state->list));
  15739. list_del_init(&pi_state->list);
  15740. - raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  15741. + raw_spin_unlock(&pi_state->owner->pi_lock);
  15742. - raw_spin_lock_irq(&new_owner->pi_lock);
  15743. + raw_spin_lock(&new_owner->pi_lock);
  15744. WARN_ON(!list_empty(&pi_state->list));
  15745. list_add(&pi_state->list, &new_owner->pi_state_list);
  15746. pi_state->owner = new_owner;
  15747. - raw_spin_unlock_irq(&new_owner->pi_lock);
  15748. + raw_spin_unlock(&new_owner->pi_lock);
  15749. - raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  15750. + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  15751. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  15752. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  15753. + &wake_sleeper_q);
  15754. /*
  15755. * First unlock HB so the waiter does not spin on it once he got woken
  15756. @@ -1286,6 +1290,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  15757. */
  15758. spin_unlock(&hb->lock);
  15759. wake_up_q(&wake_q);
  15760. + wake_up_q_sleeper(&wake_sleeper_q);
  15761. if (deboost)
  15762. rt_mutex_adjust_prio(current);
  15763. @@ -1822,6 +1827,16 @@ retry_private:
  15764. requeue_pi_wake_futex(this, &key2, hb2);
  15765. drop_count++;
  15766. continue;
  15767. + } else if (ret == -EAGAIN) {
  15768. + /*
  15769. + * Waiter was woken by timeout or
  15770. + * signal and has set pi_blocked_on to
  15771. + * PI_WAKEUP_INPROGRESS before we
  15772. + * tried to enqueue it on the rtmutex.
  15773. + */
  15774. + this->pi_state = NULL;
  15775. + free_pi_state(pi_state);
  15776. + continue;
  15777. } else if (ret) {
  15778. /* -EDEADLK */
  15779. this->pi_state = NULL;
  15780. @@ -2139,11 +2154,11 @@ static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  15781. * we returned due to timeout or signal without taking the
  15782. * rt_mutex. Too late.
  15783. */
  15784. - raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  15785. + raw_spin_lock_irq(&q->pi_state->pi_mutex.wait_lock);
  15786. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  15787. if (!owner)
  15788. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  15789. - raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  15790. + raw_spin_unlock_irq(&q->pi_state->pi_mutex.wait_lock);
  15791. ret = fixup_pi_state_owner(uaddr, q, owner);
  15792. goto out;
  15793. }
  15794. @@ -2691,7 +2706,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  15795. struct hrtimer_sleeper timeout, *to = NULL;
  15796. struct rt_mutex_waiter rt_waiter;
  15797. struct rt_mutex *pi_mutex = NULL;
  15798. - struct futex_hash_bucket *hb;
  15799. + struct futex_hash_bucket *hb, *hb2;
  15800. union futex_key key2 = FUTEX_KEY_INIT;
  15801. struct futex_q q = futex_q_init;
  15802. int res, ret;
  15803. @@ -2716,10 +2731,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  15804. * The waiter is allocated on our stack, manipulated by the requeue
  15805. * code while we sleep on uaddr.
  15806. */
  15807. - debug_rt_mutex_init_waiter(&rt_waiter);
  15808. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  15809. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  15810. - rt_waiter.task = NULL;
  15811. + rt_mutex_init_waiter(&rt_waiter, false);
  15812. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  15813. if (unlikely(ret != 0))
  15814. @@ -2750,20 +2762,55 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  15815. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  15816. futex_wait_queue_me(hb, &q, to);
  15817. - spin_lock(&hb->lock);
  15818. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15819. - spin_unlock(&hb->lock);
  15820. - if (ret)
  15821. - goto out_put_keys;
  15822. + /*
  15823. + * On RT we must avoid races with requeue and trying to block
  15824. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  15825. + * serializing access to pi_blocked_on with pi_lock.
  15826. + */
  15827. + raw_spin_lock_irq(&current->pi_lock);
  15828. + if (current->pi_blocked_on) {
  15829. + /*
  15830. + * We have been requeued or are in the process of
  15831. + * being requeued.
  15832. + */
  15833. + raw_spin_unlock_irq(&current->pi_lock);
  15834. + } else {
  15835. + /*
  15836. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  15837. + * prevents a concurrent requeue from moving us to the
  15838. + * uaddr2 rtmutex. After that we can safely acquire
  15839. + * (and possibly block on) hb->lock.
  15840. + */
  15841. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  15842. + raw_spin_unlock_irq(&current->pi_lock);
  15843. +
  15844. + spin_lock(&hb->lock);
  15845. +
  15846. + /*
  15847. + * Clean up pi_blocked_on. We might leak it otherwise
  15848. + * when we succeeded with the hb->lock in the fast
  15849. + * path.
  15850. + */
  15851. + raw_spin_lock_irq(&current->pi_lock);
  15852. + current->pi_blocked_on = NULL;
  15853. + raw_spin_unlock_irq(&current->pi_lock);
  15854. +
  15855. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  15856. + spin_unlock(&hb->lock);
  15857. + if (ret)
  15858. + goto out_put_keys;
  15859. + }
  15860. /*
  15861. - * In order for us to be here, we know our q.key == key2, and since
  15862. - * we took the hb->lock above, we also know that futex_requeue() has
  15863. - * completed and we no longer have to concern ourselves with a wakeup
  15864. - * race with the atomic proxy lock acquisition by the requeue code. The
  15865. - * futex_requeue dropped our key1 reference and incremented our key2
  15866. - * reference count.
  15867. + * In order to be here, we have either been requeued, are in
  15868. + * the process of being requeued, or requeue successfully
  15869. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  15870. + * non-null above, we may be racing with a requeue. Do not
  15871. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  15872. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  15873. + * reference and incremented our key2 reference count.
  15874. */
  15875. + hb2 = hash_futex(&key2);
  15876. /* Check if the requeue code acquired the second futex for us. */
  15877. if (!q.rt_waiter) {
  15878. @@ -2772,14 +2819,15 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  15879. * did a lock-steal - fix up the PI-state in that case.
  15880. */
  15881. if (q.pi_state && (q.pi_state->owner != current)) {
  15882. - spin_lock(q.lock_ptr);
  15883. + spin_lock(&hb2->lock);
  15884. + BUG_ON(&hb2->lock != q.lock_ptr);
  15885. ret = fixup_pi_state_owner(uaddr2, &q, current);
  15886. /*
  15887. * Drop the reference to the pi state which
  15888. * the requeue_pi() code acquired for us.
  15889. */
  15890. free_pi_state(q.pi_state);
  15891. - spin_unlock(q.lock_ptr);
  15892. + spin_unlock(&hb2->lock);
  15893. }
  15894. } else {
  15895. /*
  15896. @@ -2792,7 +2840,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  15897. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  15898. debug_rt_mutex_free_waiter(&rt_waiter);
  15899. - spin_lock(q.lock_ptr);
  15900. + spin_lock(&hb2->lock);
  15901. + BUG_ON(&hb2->lock != q.lock_ptr);
  15902. /*
  15903. * Fixup the pi_state owner and possibly acquire the lock if we
  15904. * haven't already.
  15905. diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c
  15906. index 57bff7857e87..6c65c9252991 100644
  15907. --- a/kernel/irq/handle.c
  15908. +++ b/kernel/irq/handle.c
  15909. @@ -134,6 +134,8 @@ void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  15910. irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  15911. {
  15912. + struct pt_regs *regs = get_irq_regs();
  15913. + u64 ip = regs ? instruction_pointer(regs) : 0;
  15914. irqreturn_t retval = IRQ_NONE;
  15915. unsigned int flags = 0, irq = desc->irq_data.irq;
  15916. struct irqaction *action = desc->action;
  15917. @@ -176,7 +178,11 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  15918. action = action->next;
  15919. }
  15920. - add_interrupt_randomness(irq, flags);
  15921. +#ifdef CONFIG_PREEMPT_RT_FULL
  15922. + desc->random_ip = ip;
  15923. +#else
  15924. + add_interrupt_randomness(irq, flags, ip);
  15925. +#endif
  15926. if (!noirqdebug)
  15927. note_interrupt(desc, retval);
  15928. diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
  15929. index 239e2ae2c947..0b73349a42d5 100644
  15930. --- a/kernel/irq/irqdesc.c
  15931. +++ b/kernel/irq/irqdesc.c
  15932. @@ -24,10 +24,27 @@
  15933. static struct lock_class_key irq_desc_lock_class;
  15934. #if defined(CONFIG_SMP)
  15935. +static int __init irq_affinity_setup(char *str)
  15936. +{
  15937. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15938. + cpulist_parse(str, irq_default_affinity);
  15939. + /*
  15940. + * Set at least the boot cpu. We don't want to end up with
  15941. + * bugreports caused by random comandline masks
  15942. + */
  15943. + cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
  15944. + return 1;
  15945. +}
  15946. +__setup("irqaffinity=", irq_affinity_setup);
  15947. +
  15948. static void __init init_irq_default_affinity(void)
  15949. {
  15950. - alloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15951. - cpumask_setall(irq_default_affinity);
  15952. +#ifdef CONFIG_CPUMASK_OFFSTACK
  15953. + if (!irq_default_affinity)
  15954. + zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
  15955. +#endif
  15956. + if (cpumask_empty(irq_default_affinity))
  15957. + cpumask_setall(irq_default_affinity);
  15958. }
  15959. #else
  15960. static void __init init_irq_default_affinity(void)
  15961. diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
  15962. index 6ead200370da..8e89554aa345 100644
  15963. --- a/kernel/irq/manage.c
  15964. +++ b/kernel/irq/manage.c
  15965. @@ -22,6 +22,7 @@
  15966. #include "internals.h"
  15967. #ifdef CONFIG_IRQ_FORCED_THREADING
  15968. +# ifndef CONFIG_PREEMPT_RT_BASE
  15969. __read_mostly bool force_irqthreads;
  15970. static int __init setup_forced_irqthreads(char *arg)
  15971. @@ -30,6 +31,7 @@ static int __init setup_forced_irqthreads(char *arg)
  15972. return 0;
  15973. }
  15974. early_param("threadirqs", setup_forced_irqthreads);
  15975. +# endif
  15976. #endif
  15977. static void __synchronize_hardirq(struct irq_desc *desc)
  15978. @@ -181,6 +183,62 @@ static inline void
  15979. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  15980. #endif
  15981. +#ifdef CONFIG_PREEMPT_RT_FULL
  15982. +static void _irq_affinity_notify(struct irq_affinity_notify *notify);
  15983. +static struct task_struct *set_affinity_helper;
  15984. +static LIST_HEAD(affinity_list);
  15985. +static DEFINE_RAW_SPINLOCK(affinity_list_lock);
  15986. +
  15987. +static int set_affinity_thread(void *unused)
  15988. +{
  15989. + while (1) {
  15990. + struct irq_affinity_notify *notify;
  15991. + int empty;
  15992. +
  15993. + set_current_state(TASK_INTERRUPTIBLE);
  15994. +
  15995. + raw_spin_lock_irq(&affinity_list_lock);
  15996. + empty = list_empty(&affinity_list);
  15997. + raw_spin_unlock_irq(&affinity_list_lock);
  15998. +
  15999. + if (empty)
  16000. + schedule();
  16001. + if (kthread_should_stop())
  16002. + break;
  16003. + set_current_state(TASK_RUNNING);
  16004. +try_next:
  16005. + notify = NULL;
  16006. +
  16007. + raw_spin_lock_irq(&affinity_list_lock);
  16008. + if (!list_empty(&affinity_list)) {
  16009. + notify = list_first_entry(&affinity_list,
  16010. + struct irq_affinity_notify, list);
  16011. + list_del_init(&notify->list);
  16012. + }
  16013. + raw_spin_unlock_irq(&affinity_list_lock);
  16014. +
  16015. + if (!notify)
  16016. + continue;
  16017. + _irq_affinity_notify(notify);
  16018. + goto try_next;
  16019. + }
  16020. + return 0;
  16021. +}
  16022. +
  16023. +static void init_helper_thread(void)
  16024. +{
  16025. + if (set_affinity_helper)
  16026. + return;
  16027. + set_affinity_helper = kthread_run(set_affinity_thread, NULL,
  16028. + "affinity-cb");
  16029. + WARN_ON(IS_ERR(set_affinity_helper));
  16030. +}
  16031. +#else
  16032. +
  16033. +static inline void init_helper_thread(void) { }
  16034. +
  16035. +#endif
  16036. +
  16037. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  16038. bool force)
  16039. {
  16040. @@ -220,7 +278,17 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
  16041. if (desc->affinity_notify) {
  16042. kref_get(&desc->affinity_notify->kref);
  16043. +
  16044. +#ifdef CONFIG_PREEMPT_RT_FULL
  16045. + raw_spin_lock(&affinity_list_lock);
  16046. + if (list_empty(&desc->affinity_notify->list))
  16047. + list_add_tail(&affinity_list,
  16048. + &desc->affinity_notify->list);
  16049. + raw_spin_unlock(&affinity_list_lock);
  16050. + wake_up_process(set_affinity_helper);
  16051. +#else
  16052. schedule_work(&desc->affinity_notify->work);
  16053. +#endif
  16054. }
  16055. irqd_set(data, IRQD_AFFINITY_SET);
  16056. @@ -258,10 +326,8 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  16057. }
  16058. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  16059. -static void irq_affinity_notify(struct work_struct *work)
  16060. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  16061. {
  16062. - struct irq_affinity_notify *notify =
  16063. - container_of(work, struct irq_affinity_notify, work);
  16064. struct irq_desc *desc = irq_to_desc(notify->irq);
  16065. cpumask_var_t cpumask;
  16066. unsigned long flags;
  16067. @@ -283,6 +349,13 @@ out:
  16068. kref_put(&notify->kref, notify->release);
  16069. }
  16070. +static void irq_affinity_notify(struct work_struct *work)
  16071. +{
  16072. + struct irq_affinity_notify *notify =
  16073. + container_of(work, struct irq_affinity_notify, work);
  16074. + _irq_affinity_notify(notify);
  16075. +}
  16076. +
  16077. /**
  16078. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  16079. * @irq: Interrupt for which to enable/disable notification
  16080. @@ -312,6 +385,8 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  16081. notify->irq = irq;
  16082. kref_init(&notify->kref);
  16083. INIT_WORK(&notify->work, irq_affinity_notify);
  16084. + INIT_LIST_HEAD(&notify->list);
  16085. + init_helper_thread();
  16086. }
  16087. raw_spin_lock_irqsave(&desc->lock, flags);
  16088. @@ -865,7 +940,15 @@ irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  16089. local_bh_disable();
  16090. ret = action->thread_fn(action->irq, action->dev_id);
  16091. irq_finalize_oneshot(desc, action);
  16092. - local_bh_enable();
  16093. + /*
  16094. + * Interrupts which have real time requirements can be set up
  16095. + * to avoid softirq processing in the thread handler. This is
  16096. + * safe as these interrupts do not raise soft interrupts.
  16097. + */
  16098. + if (irq_settings_no_softirq_call(desc))
  16099. + _local_bh_enable();
  16100. + else
  16101. + local_bh_enable();
  16102. return ret;
  16103. }
  16104. @@ -962,6 +1045,12 @@ static int irq_thread(void *data)
  16105. if (action_ret == IRQ_WAKE_THREAD)
  16106. irq_wake_secondary(desc, action);
  16107. +#ifdef CONFIG_PREEMPT_RT_FULL
  16108. + migrate_disable();
  16109. + add_interrupt_randomness(action->irq, 0,
  16110. + desc->random_ip ^ (unsigned long) action);
  16111. + migrate_enable();
  16112. +#endif
  16113. wake_threads_waitq(desc);
  16114. }
  16115. @@ -1315,6 +1404,9 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  16116. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  16117. }
  16118. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  16119. + irq_settings_set_no_softirq_call(desc);
  16120. +
  16121. /* Set default affinity mask once everything is setup */
  16122. setup_affinity(desc, mask);
  16123. @@ -1968,7 +2060,7 @@ EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
  16124. * This call sets the internal irqchip state of an interrupt,
  16125. * depending on the value of @which.
  16126. *
  16127. - * This function should be called with preemption disabled if the
  16128. + * This function should be called with migration disabled if the
  16129. * interrupt controller has per-cpu registers.
  16130. */
  16131. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  16132. diff --git a/kernel/irq/settings.h b/kernel/irq/settings.h
  16133. index 320579d89091..2df2d4445b1e 100644
  16134. --- a/kernel/irq/settings.h
  16135. +++ b/kernel/irq/settings.h
  16136. @@ -16,6 +16,7 @@ enum {
  16137. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  16138. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  16139. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  16140. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  16141. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  16142. };
  16143. @@ -30,6 +31,7 @@ enum {
  16144. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  16145. #define IRQ_IS_POLLED GOT_YOU_MORON
  16146. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  16147. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  16148. #undef IRQF_MODIFY_MASK
  16149. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  16150. @@ -40,6 +42,16 @@ irq_settings_clr_and_set(struct irq_desc *desc, u32 clr, u32 set)
  16151. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  16152. }
  16153. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  16154. +{
  16155. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  16156. +}
  16157. +
  16158. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  16159. +{
  16160. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  16161. +}
  16162. +
  16163. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  16164. {
  16165. return desc->status_use_accessors & _IRQ_PER_CPU;
  16166. diff --git a/kernel/irq/spurious.c b/kernel/irq/spurious.c
  16167. index 32144175458d..ed26f2554972 100644
  16168. --- a/kernel/irq/spurious.c
  16169. +++ b/kernel/irq/spurious.c
  16170. @@ -444,6 +444,10 @@ MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  16171. static int __init irqfixup_setup(char *str)
  16172. {
  16173. +#ifdef CONFIG_PREEMPT_RT_BASE
  16174. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16175. + return 1;
  16176. +#endif
  16177. irqfixup = 1;
  16178. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  16179. printk(KERN_WARNING "This may impact system performance.\n");
  16180. @@ -456,6 +460,10 @@ module_param(irqfixup, int, 0644);
  16181. static int __init irqpoll_setup(char *str)
  16182. {
  16183. +#ifdef CONFIG_PREEMPT_RT_BASE
  16184. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  16185. + return 1;
  16186. +#endif
  16187. irqfixup = 2;
  16188. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  16189. "enabled\n");
  16190. diff --git a/kernel/irq_work.c b/kernel/irq_work.c
  16191. index bcf107ce0854..2899ba0d23d1 100644
  16192. --- a/kernel/irq_work.c
  16193. +++ b/kernel/irq_work.c
  16194. @@ -17,6 +17,7 @@
  16195. #include <linux/cpu.h>
  16196. #include <linux/notifier.h>
  16197. #include <linux/smp.h>
  16198. +#include <linux/interrupt.h>
  16199. #include <asm/processor.h>
  16200. @@ -65,6 +66,8 @@ void __weak arch_irq_work_raise(void)
  16201. */
  16202. bool irq_work_queue_on(struct irq_work *work, int cpu)
  16203. {
  16204. + struct llist_head *list;
  16205. +
  16206. /* All work should have been flushed before going offline */
  16207. WARN_ON_ONCE(cpu_is_offline(cpu));
  16208. @@ -75,7 +78,12 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
  16209. if (!irq_work_claim(work))
  16210. return false;
  16211. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  16212. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  16213. + list = &per_cpu(lazy_list, cpu);
  16214. + else
  16215. + list = &per_cpu(raised_list, cpu);
  16216. +
  16217. + if (llist_add(&work->llnode, list))
  16218. arch_send_call_function_single_ipi(cpu);
  16219. return true;
  16220. @@ -86,6 +94,9 @@ EXPORT_SYMBOL_GPL(irq_work_queue_on);
  16221. /* Enqueue the irq work @work on the current CPU */
  16222. bool irq_work_queue(struct irq_work *work)
  16223. {
  16224. + struct llist_head *list;
  16225. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  16226. +
  16227. /* Only queue if not already pending */
  16228. if (!irq_work_claim(work))
  16229. return false;
  16230. @@ -93,13 +104,15 @@ bool irq_work_queue(struct irq_work *work)
  16231. /* Queue the entry and raise the IPI if needed. */
  16232. preempt_disable();
  16233. - /* If the work is "lazy", handle it from next tick if any */
  16234. - if (work->flags & IRQ_WORK_LAZY) {
  16235. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  16236. - tick_nohz_tick_stopped())
  16237. - arch_irq_work_raise();
  16238. - } else {
  16239. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  16240. + lazy_work = work->flags & IRQ_WORK_LAZY;
  16241. +
  16242. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  16243. + list = this_cpu_ptr(&lazy_list);
  16244. + else
  16245. + list = this_cpu_ptr(&raised_list);
  16246. +
  16247. + if (llist_add(&work->llnode, list)) {
  16248. + if (!lazy_work || tick_nohz_tick_stopped())
  16249. arch_irq_work_raise();
  16250. }
  16251. @@ -116,9 +129,8 @@ bool irq_work_needs_cpu(void)
  16252. raised = this_cpu_ptr(&raised_list);
  16253. lazy = this_cpu_ptr(&lazy_list);
  16254. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  16255. - if (llist_empty(lazy))
  16256. - return false;
  16257. + if (llist_empty(raised) && llist_empty(lazy))
  16258. + return false;
  16259. /* All work should have been flushed before going offline */
  16260. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  16261. @@ -132,7 +144,7 @@ static void irq_work_run_list(struct llist_head *list)
  16262. struct irq_work *work;
  16263. struct llist_node *llnode;
  16264. - BUG_ON(!irqs_disabled());
  16265. + BUG_ON_NONRT(!irqs_disabled());
  16266. if (llist_empty(list))
  16267. return;
  16268. @@ -169,7 +181,16 @@ static void irq_work_run_list(struct llist_head *list)
  16269. void irq_work_run(void)
  16270. {
  16271. irq_work_run_list(this_cpu_ptr(&raised_list));
  16272. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  16273. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  16274. + /*
  16275. + * NOTE: we raise softirq via IPI for safety,
  16276. + * and execute in irq_work_tick() to move the
  16277. + * overhead from hard to soft irq context.
  16278. + */
  16279. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  16280. + raise_softirq(TIMER_SOFTIRQ);
  16281. + } else
  16282. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  16283. }
  16284. EXPORT_SYMBOL_GPL(irq_work_run);
  16285. @@ -179,8 +200,17 @@ void irq_work_tick(void)
  16286. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  16287. irq_work_run_list(raised);
  16288. +
  16289. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  16290. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  16291. +}
  16292. +
  16293. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  16294. +void irq_work_tick_soft(void)
  16295. +{
  16296. irq_work_run_list(this_cpu_ptr(&lazy_list));
  16297. }
  16298. +#endif
  16299. /*
  16300. * Synchronize against the irq_work @entry, ensures the entry is not
  16301. diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c
  16302. index e83b26464061..c0e08d1cf33e 100644
  16303. --- a/kernel/ksysfs.c
  16304. +++ b/kernel/ksysfs.c
  16305. @@ -136,6 +136,15 @@ KERNEL_ATTR_RO(vmcoreinfo);
  16306. #endif /* CONFIG_KEXEC_CORE */
  16307. +#if defined(CONFIG_PREEMPT_RT_FULL)
  16308. +static ssize_t realtime_show(struct kobject *kobj,
  16309. + struct kobj_attribute *attr, char *buf)
  16310. +{
  16311. + return sprintf(buf, "%d\n", 1);
  16312. +}
  16313. +KERNEL_ATTR_RO(realtime);
  16314. +#endif
  16315. +
  16316. /* whether file capabilities are enabled */
  16317. static ssize_t fscaps_show(struct kobject *kobj,
  16318. struct kobj_attribute *attr, char *buf)
  16319. @@ -203,6 +212,9 @@ static struct attribute * kernel_attrs[] = {
  16320. &vmcoreinfo_attr.attr,
  16321. #endif
  16322. &rcu_expedited_attr.attr,
  16323. +#ifdef CONFIG_PREEMPT_RT_FULL
  16324. + &realtime_attr.attr,
  16325. +#endif
  16326. NULL
  16327. };
  16328. diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile
  16329. index 8e96f6cc2a4a..447b03082d88 100644
  16330. --- a/kernel/locking/Makefile
  16331. +++ b/kernel/locking/Makefile
  16332. @@ -1,5 +1,5 @@
  16333. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  16334. +obj-y += semaphore.o percpu-rwsem.o
  16335. ifdef CONFIG_FUNCTION_TRACER
  16336. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  16337. @@ -8,7 +8,11 @@ CFLAGS_REMOVE_mutex-debug.o = $(CC_FLAGS_FTRACE)
  16338. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  16339. endif
  16340. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16341. +obj-y += mutex.o
  16342. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  16343. +obj-y += rwsem.o
  16344. +endif
  16345. obj-$(CONFIG_LOCKDEP) += lockdep.o
  16346. ifeq ($(CONFIG_PROC_FS),y)
  16347. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  16348. @@ -22,7 +26,10 @@ obj-$(CONFIG_RT_MUTEXES) += rtmutex.o
  16349. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  16350. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  16351. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  16352. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  16353. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  16354. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  16355. +endif
  16356. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  16357. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  16358. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  16359. diff --git a/kernel/locking/lglock.c b/kernel/locking/lglock.c
  16360. index 951cfcd10b4a..57e0ea72c28a 100644
  16361. --- a/kernel/locking/lglock.c
  16362. +++ b/kernel/locking/lglock.c
  16363. @@ -4,6 +4,15 @@
  16364. #include <linux/cpu.h>
  16365. #include <linux/string.h>
  16366. +#ifndef CONFIG_PREEMPT_RT_FULL
  16367. +# define lg_lock_ptr arch_spinlock_t
  16368. +# define lg_do_lock(l) arch_spin_lock(l)
  16369. +# define lg_do_unlock(l) arch_spin_unlock(l)
  16370. +#else
  16371. +# define lg_lock_ptr struct rt_mutex
  16372. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  16373. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  16374. +#endif
  16375. /*
  16376. * Note there is no uninit, so lglocks cannot be defined in
  16377. * modules (but it's fine to use them from there)
  16378. @@ -12,51 +21,60 @@
  16379. void lg_lock_init(struct lglock *lg, char *name)
  16380. {
  16381. +#ifdef CONFIG_PREEMPT_RT_FULL
  16382. + int i;
  16383. +
  16384. + for_each_possible_cpu(i) {
  16385. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  16386. +
  16387. + rt_mutex_init(lock);
  16388. + }
  16389. +#endif
  16390. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  16391. }
  16392. EXPORT_SYMBOL(lg_lock_init);
  16393. void lg_local_lock(struct lglock *lg)
  16394. {
  16395. - arch_spinlock_t *lock;
  16396. + lg_lock_ptr *lock;
  16397. - preempt_disable();
  16398. + migrate_disable();
  16399. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16400. lock = this_cpu_ptr(lg->lock);
  16401. - arch_spin_lock(lock);
  16402. + lg_do_lock(lock);
  16403. }
  16404. EXPORT_SYMBOL(lg_local_lock);
  16405. void lg_local_unlock(struct lglock *lg)
  16406. {
  16407. - arch_spinlock_t *lock;
  16408. + lg_lock_ptr *lock;
  16409. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16410. lock = this_cpu_ptr(lg->lock);
  16411. - arch_spin_unlock(lock);
  16412. - preempt_enable();
  16413. + lg_do_unlock(lock);
  16414. + migrate_enable();
  16415. }
  16416. EXPORT_SYMBOL(lg_local_unlock);
  16417. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  16418. {
  16419. - arch_spinlock_t *lock;
  16420. + lg_lock_ptr *lock;
  16421. - preempt_disable();
  16422. + preempt_disable_nort();
  16423. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16424. lock = per_cpu_ptr(lg->lock, cpu);
  16425. - arch_spin_lock(lock);
  16426. + lg_do_lock(lock);
  16427. }
  16428. EXPORT_SYMBOL(lg_local_lock_cpu);
  16429. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  16430. {
  16431. - arch_spinlock_t *lock;
  16432. + lg_lock_ptr *lock;
  16433. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16434. lock = per_cpu_ptr(lg->lock, cpu);
  16435. - arch_spin_unlock(lock);
  16436. - preempt_enable();
  16437. + lg_do_unlock(lock);
  16438. + preempt_enable_nort();
  16439. }
  16440. EXPORT_SYMBOL(lg_local_unlock_cpu);
  16441. @@ -68,30 +86,30 @@ void lg_double_lock(struct lglock *lg, int cpu1, int cpu2)
  16442. if (cpu2 < cpu1)
  16443. swap(cpu1, cpu2);
  16444. - preempt_disable();
  16445. + preempt_disable_nort();
  16446. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16447. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  16448. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  16449. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  16450. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  16451. }
  16452. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  16453. {
  16454. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16455. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  16456. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  16457. - preempt_enable();
  16458. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  16459. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  16460. + preempt_enable_nort();
  16461. }
  16462. void lg_global_lock(struct lglock *lg)
  16463. {
  16464. int i;
  16465. - preempt_disable();
  16466. + preempt_disable_nort();
  16467. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16468. for_each_possible_cpu(i) {
  16469. - arch_spinlock_t *lock;
  16470. + lg_lock_ptr *lock;
  16471. lock = per_cpu_ptr(lg->lock, i);
  16472. - arch_spin_lock(lock);
  16473. + lg_do_lock(lock);
  16474. }
  16475. }
  16476. EXPORT_SYMBOL(lg_global_lock);
  16477. @@ -102,10 +120,35 @@ void lg_global_unlock(struct lglock *lg)
  16478. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  16479. for_each_possible_cpu(i) {
  16480. - arch_spinlock_t *lock;
  16481. + lg_lock_ptr *lock;
  16482. lock = per_cpu_ptr(lg->lock, i);
  16483. - arch_spin_unlock(lock);
  16484. + lg_do_unlock(lock);
  16485. }
  16486. - preempt_enable();
  16487. + preempt_enable_nort();
  16488. }
  16489. EXPORT_SYMBOL(lg_global_unlock);
  16490. +
  16491. +#ifdef CONFIG_PREEMPT_RT_FULL
  16492. +/*
  16493. + * HACK: If you use this, you get to keep the pieces.
  16494. + * Used in queue_stop_cpus_work() when stop machinery
  16495. + * is called from inactive CPU, so we can't schedule.
  16496. + */
  16497. +# define lg_do_trylock_relax(l) \
  16498. + do { \
  16499. + while (!__rt_spin_trylock(l)) \
  16500. + cpu_relax(); \
  16501. + } while (0)
  16502. +
  16503. +void lg_global_trylock_relax(struct lglock *lg)
  16504. +{
  16505. + int i;
  16506. +
  16507. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  16508. + for_each_possible_cpu(i) {
  16509. + lg_lock_ptr *lock;
  16510. + lock = per_cpu_ptr(lg->lock, i);
  16511. + lg_do_trylock_relax(lock);
  16512. + }
  16513. +}
  16514. +#endif
  16515. diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
  16516. index 60ace56618f6..e98ee958a353 100644
  16517. --- a/kernel/locking/lockdep.c
  16518. +++ b/kernel/locking/lockdep.c
  16519. @@ -3525,6 +3525,7 @@ static void check_flags(unsigned long flags)
  16520. }
  16521. }
  16522. +#ifndef CONFIG_PREEMPT_RT_FULL
  16523. /*
  16524. * We dont accurately track softirq state in e.g.
  16525. * hardirq contexts (such as on 4KSTACKS), so only
  16526. @@ -3539,6 +3540,7 @@ static void check_flags(unsigned long flags)
  16527. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  16528. }
  16529. }
  16530. +#endif
  16531. if (!debug_locks)
  16532. print_irqtrace_events(current);
  16533. diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
  16534. index 8ef1919d63b2..291fc19e28e0 100644
  16535. --- a/kernel/locking/locktorture.c
  16536. +++ b/kernel/locking/locktorture.c
  16537. @@ -26,7 +26,6 @@
  16538. #include <linux/kthread.h>
  16539. #include <linux/sched/rt.h>
  16540. #include <linux/spinlock.h>
  16541. -#include <linux/rwlock.h>
  16542. #include <linux/mutex.h>
  16543. #include <linux/rwsem.h>
  16544. #include <linux/smp.h>
  16545. diff --git a/kernel/locking/rt.c b/kernel/locking/rt.c
  16546. new file mode 100644
  16547. index 000000000000..d4ab61c1848b
  16548. --- /dev/null
  16549. +++ b/kernel/locking/rt.c
  16550. @@ -0,0 +1,474 @@
  16551. +/*
  16552. + * kernel/rt.c
  16553. + *
  16554. + * Real-Time Preemption Support
  16555. + *
  16556. + * started by Ingo Molnar:
  16557. + *
  16558. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  16559. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16560. + *
  16561. + * historic credit for proving that Linux spinlocks can be implemented via
  16562. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  16563. + * and others) who prototyped it on 2.4 and did lots of comparative
  16564. + * research and analysis; TimeSys, for proving that you can implement a
  16565. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  16566. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  16567. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  16568. + *
  16569. + * This code is a from-scratch implementation and is not based on pmutexes,
  16570. + * but the idea of converting spinlocks to mutexes is used here too.
  16571. + *
  16572. + * lock debugging, locking tree, deadlock detection:
  16573. + *
  16574. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  16575. + * Released under the General Public License (GPL).
  16576. + *
  16577. + * Includes portions of the generic R/W semaphore implementation from:
  16578. + *
  16579. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  16580. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  16581. + * - Derived also from comments by Linus
  16582. + *
  16583. + * Pending ownership of locks and ownership stealing:
  16584. + *
  16585. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  16586. + *
  16587. + * (also by Steven Rostedt)
  16588. + * - Converted single pi_lock to individual task locks.
  16589. + *
  16590. + * By Esben Nielsen:
  16591. + * Doing priority inheritance with help of the scheduler.
  16592. + *
  16593. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  16594. + * - major rework based on Esben Nielsens initial patch
  16595. + * - replaced thread_info references by task_struct refs
  16596. + * - removed task->pending_owner dependency
  16597. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  16598. + * in the scheduler return path as discussed with Steven Rostedt
  16599. + *
  16600. + * Copyright (C) 2006, Kihon Technologies Inc.
  16601. + * Steven Rostedt <rostedt@goodmis.org>
  16602. + * - debugged and patched Thomas Gleixner's rework.
  16603. + * - added back the cmpxchg to the rework.
  16604. + * - turned atomic require back on for SMP.
  16605. + */
  16606. +
  16607. +#include <linux/spinlock.h>
  16608. +#include <linux/rtmutex.h>
  16609. +#include <linux/sched.h>
  16610. +#include <linux/delay.h>
  16611. +#include <linux/module.h>
  16612. +#include <linux/kallsyms.h>
  16613. +#include <linux/syscalls.h>
  16614. +#include <linux/interrupt.h>
  16615. +#include <linux/plist.h>
  16616. +#include <linux/fs.h>
  16617. +#include <linux/futex.h>
  16618. +#include <linux/hrtimer.h>
  16619. +
  16620. +#include "rtmutex_common.h"
  16621. +
  16622. +/*
  16623. + * struct mutex functions
  16624. + */
  16625. +void __mutex_do_init(struct mutex *mutex, const char *name,
  16626. + struct lock_class_key *key)
  16627. +{
  16628. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16629. + /*
  16630. + * Make sure we are not reinitializing a held lock:
  16631. + */
  16632. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  16633. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  16634. +#endif
  16635. + mutex->lock.save_state = 0;
  16636. +}
  16637. +EXPORT_SYMBOL(__mutex_do_init);
  16638. +
  16639. +void __lockfunc _mutex_lock(struct mutex *lock)
  16640. +{
  16641. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16642. + rt_mutex_lock(&lock->lock);
  16643. +}
  16644. +EXPORT_SYMBOL(_mutex_lock);
  16645. +
  16646. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  16647. +{
  16648. + int ret;
  16649. +
  16650. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16651. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16652. + if (ret)
  16653. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16654. + return ret;
  16655. +}
  16656. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  16657. +
  16658. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  16659. +{
  16660. + int ret;
  16661. +
  16662. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  16663. + ret = rt_mutex_lock_killable(&lock->lock);
  16664. + if (ret)
  16665. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16666. + return ret;
  16667. +}
  16668. +EXPORT_SYMBOL(_mutex_lock_killable);
  16669. +
  16670. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16671. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  16672. +{
  16673. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16674. + rt_mutex_lock(&lock->lock);
  16675. +}
  16676. +EXPORT_SYMBOL(_mutex_lock_nested);
  16677. +
  16678. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  16679. +{
  16680. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  16681. + rt_mutex_lock(&lock->lock);
  16682. +}
  16683. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  16684. +
  16685. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  16686. +{
  16687. + int ret;
  16688. +
  16689. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  16690. + ret = rt_mutex_lock_interruptible(&lock->lock);
  16691. + if (ret)
  16692. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16693. + return ret;
  16694. +}
  16695. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  16696. +
  16697. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  16698. +{
  16699. + int ret;
  16700. +
  16701. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  16702. + ret = rt_mutex_lock_killable(&lock->lock);
  16703. + if (ret)
  16704. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16705. + return ret;
  16706. +}
  16707. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  16708. +#endif
  16709. +
  16710. +int __lockfunc _mutex_trylock(struct mutex *lock)
  16711. +{
  16712. + int ret = rt_mutex_trylock(&lock->lock);
  16713. +
  16714. + if (ret)
  16715. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  16716. +
  16717. + return ret;
  16718. +}
  16719. +EXPORT_SYMBOL(_mutex_trylock);
  16720. +
  16721. +void __lockfunc _mutex_unlock(struct mutex *lock)
  16722. +{
  16723. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  16724. + rt_mutex_unlock(&lock->lock);
  16725. +}
  16726. +EXPORT_SYMBOL(_mutex_unlock);
  16727. +
  16728. +/*
  16729. + * rwlock_t functions
  16730. + */
  16731. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  16732. +{
  16733. + int ret;
  16734. +
  16735. + migrate_disable();
  16736. + ret = rt_mutex_trylock(&rwlock->lock);
  16737. + if (ret)
  16738. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16739. + else
  16740. + migrate_enable();
  16741. +
  16742. + return ret;
  16743. +}
  16744. +EXPORT_SYMBOL(rt_write_trylock);
  16745. +
  16746. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  16747. +{
  16748. + int ret;
  16749. +
  16750. + *flags = 0;
  16751. + ret = rt_write_trylock(rwlock);
  16752. + return ret;
  16753. +}
  16754. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  16755. +
  16756. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  16757. +{
  16758. + struct rt_mutex *lock = &rwlock->lock;
  16759. + int ret = 1;
  16760. +
  16761. + /*
  16762. + * recursive read locks succeed when current owns the lock,
  16763. + * but not when read_depth == 0 which means that the lock is
  16764. + * write locked.
  16765. + */
  16766. + if (rt_mutex_owner(lock) != current) {
  16767. + migrate_disable();
  16768. + ret = rt_mutex_trylock(lock);
  16769. + if (ret)
  16770. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  16771. + else
  16772. + migrate_enable();
  16773. +
  16774. + } else if (!rwlock->read_depth) {
  16775. + ret = 0;
  16776. + }
  16777. +
  16778. + if (ret)
  16779. + rwlock->read_depth++;
  16780. +
  16781. + return ret;
  16782. +}
  16783. +EXPORT_SYMBOL(rt_read_trylock);
  16784. +
  16785. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  16786. +{
  16787. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16788. + __rt_spin_lock(&rwlock->lock);
  16789. +}
  16790. +EXPORT_SYMBOL(rt_write_lock);
  16791. +
  16792. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  16793. +{
  16794. + struct rt_mutex *lock = &rwlock->lock;
  16795. +
  16796. +
  16797. + /*
  16798. + * recursive read locks succeed when current owns the lock
  16799. + */
  16800. + if (rt_mutex_owner(lock) != current) {
  16801. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  16802. + __rt_spin_lock(lock);
  16803. + }
  16804. + rwlock->read_depth++;
  16805. +}
  16806. +
  16807. +EXPORT_SYMBOL(rt_read_lock);
  16808. +
  16809. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  16810. +{
  16811. + /* NOTE: we always pass in '1' for nested, for simplicity */
  16812. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16813. + __rt_spin_unlock(&rwlock->lock);
  16814. + migrate_enable();
  16815. +}
  16816. +EXPORT_SYMBOL(rt_write_unlock);
  16817. +
  16818. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  16819. +{
  16820. + /* Release the lock only when read_depth is down to 0 */
  16821. + if (--rwlock->read_depth == 0) {
  16822. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  16823. + __rt_spin_unlock(&rwlock->lock);
  16824. + migrate_enable();
  16825. + }
  16826. +}
  16827. +EXPORT_SYMBOL(rt_read_unlock);
  16828. +
  16829. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  16830. +{
  16831. + rt_write_lock(rwlock);
  16832. +
  16833. + return 0;
  16834. +}
  16835. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  16836. +
  16837. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  16838. +{
  16839. + rt_read_lock(rwlock);
  16840. +
  16841. + return 0;
  16842. +}
  16843. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  16844. +
  16845. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  16846. +{
  16847. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16848. + /*
  16849. + * Make sure we are not reinitializing a held lock:
  16850. + */
  16851. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  16852. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  16853. +#endif
  16854. + rwlock->lock.save_state = 1;
  16855. + rwlock->read_depth = 0;
  16856. +}
  16857. +EXPORT_SYMBOL(__rt_rwlock_init);
  16858. +
  16859. +/*
  16860. + * rw_semaphores
  16861. + */
  16862. +
  16863. +void rt_up_write(struct rw_semaphore *rwsem)
  16864. +{
  16865. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16866. + rt_mutex_unlock(&rwsem->lock);
  16867. +}
  16868. +EXPORT_SYMBOL(rt_up_write);
  16869. +
  16870. +void __rt_up_read(struct rw_semaphore *rwsem)
  16871. +{
  16872. + if (--rwsem->read_depth == 0)
  16873. + rt_mutex_unlock(&rwsem->lock);
  16874. +}
  16875. +
  16876. +void rt_up_read(struct rw_semaphore *rwsem)
  16877. +{
  16878. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  16879. + __rt_up_read(rwsem);
  16880. +}
  16881. +EXPORT_SYMBOL(rt_up_read);
  16882. +
  16883. +/*
  16884. + * downgrade a write lock into a read lock
  16885. + * - just wake up any readers at the front of the queue
  16886. + */
  16887. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  16888. +{
  16889. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  16890. + rwsem->read_depth = 1;
  16891. +}
  16892. +EXPORT_SYMBOL(rt_downgrade_write);
  16893. +
  16894. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  16895. +{
  16896. + int ret = rt_mutex_trylock(&rwsem->lock);
  16897. +
  16898. + if (ret)
  16899. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16900. + return ret;
  16901. +}
  16902. +EXPORT_SYMBOL(rt_down_write_trylock);
  16903. +
  16904. +void rt_down_write(struct rw_semaphore *rwsem)
  16905. +{
  16906. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  16907. + rt_mutex_lock(&rwsem->lock);
  16908. +}
  16909. +EXPORT_SYMBOL(rt_down_write);
  16910. +
  16911. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  16912. +{
  16913. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16914. + rt_mutex_lock(&rwsem->lock);
  16915. +}
  16916. +EXPORT_SYMBOL(rt_down_write_nested);
  16917. +
  16918. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  16919. + struct lockdep_map *nest)
  16920. +{
  16921. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  16922. + rt_mutex_lock(&rwsem->lock);
  16923. +}
  16924. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  16925. +
  16926. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  16927. +{
  16928. + struct rt_mutex *lock = &rwsem->lock;
  16929. + int ret = 1;
  16930. +
  16931. + /*
  16932. + * recursive read locks succeed when current owns the rwsem,
  16933. + * but not when read_depth == 0 which means that the rwsem is
  16934. + * write locked.
  16935. + */
  16936. + if (rt_mutex_owner(lock) != current)
  16937. + ret = rt_mutex_trylock(&rwsem->lock);
  16938. + else if (!rwsem->read_depth)
  16939. + ret = 0;
  16940. +
  16941. + if (ret)
  16942. + rwsem->read_depth++;
  16943. + return ret;
  16944. +
  16945. +}
  16946. +
  16947. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  16948. +{
  16949. + int ret;
  16950. +
  16951. + ret = rt__down_read_trylock(rwsem);
  16952. + if (ret)
  16953. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  16954. +
  16955. + return ret;
  16956. +}
  16957. +EXPORT_SYMBOL(rt_down_read_trylock);
  16958. +
  16959. +void rt__down_read(struct rw_semaphore *rwsem)
  16960. +{
  16961. + struct rt_mutex *lock = &rwsem->lock;
  16962. +
  16963. + if (rt_mutex_owner(lock) != current)
  16964. + rt_mutex_lock(&rwsem->lock);
  16965. + rwsem->read_depth++;
  16966. +}
  16967. +EXPORT_SYMBOL(rt__down_read);
  16968. +
  16969. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  16970. +{
  16971. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  16972. + rt__down_read(rwsem);
  16973. +}
  16974. +
  16975. +void rt_down_read(struct rw_semaphore *rwsem)
  16976. +{
  16977. + __rt_down_read(rwsem, 0);
  16978. +}
  16979. +EXPORT_SYMBOL(rt_down_read);
  16980. +
  16981. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  16982. +{
  16983. + __rt_down_read(rwsem, subclass);
  16984. +}
  16985. +EXPORT_SYMBOL(rt_down_read_nested);
  16986. +
  16987. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  16988. + struct lock_class_key *key)
  16989. +{
  16990. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  16991. + /*
  16992. + * Make sure we are not reinitializing a held lock:
  16993. + */
  16994. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  16995. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  16996. +#endif
  16997. + rwsem->read_depth = 0;
  16998. + rwsem->lock.save_state = 0;
  16999. +}
  17000. +EXPORT_SYMBOL(__rt_rwsem_init);
  17001. +
  17002. +/**
  17003. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  17004. + * @cnt: the atomic which we are to dec
  17005. + * @lock: the mutex to return holding if we dec to 0
  17006. + *
  17007. + * return true and hold lock if we dec to 0, return false otherwise
  17008. + */
  17009. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  17010. +{
  17011. + /* dec if we can't possibly hit 0 */
  17012. + if (atomic_add_unless(cnt, -1, 1))
  17013. + return 0;
  17014. + /* we might hit 0, so take the lock */
  17015. + mutex_lock(lock);
  17016. + if (!atomic_dec_and_test(cnt)) {
  17017. + /* when we actually did the dec, we didn't hit 0 */
  17018. + mutex_unlock(lock);
  17019. + return 0;
  17020. + }
  17021. + /* we hit 0, and we hold the lock */
  17022. + return 1;
  17023. +}
  17024. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  17025. diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
  17026. index 8251e75dd9c0..fde5e54f1096 100644
  17027. --- a/kernel/locking/rtmutex.c
  17028. +++ b/kernel/locking/rtmutex.c
  17029. @@ -7,6 +7,11 @@
  17030. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  17031. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  17032. * Copyright (C) 2006 Esben Nielsen
  17033. + * Adaptive Spinlocks:
  17034. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  17035. + * and Peter Morreale,
  17036. + * Adaptive Spinlocks simplification:
  17037. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  17038. *
  17039. * See Documentation/locking/rt-mutex-design.txt for details.
  17040. */
  17041. @@ -16,6 +21,7 @@
  17042. #include <linux/sched/rt.h>
  17043. #include <linux/sched/deadline.h>
  17044. #include <linux/timer.h>
  17045. +#include <linux/ww_mutex.h>
  17046. #include "rtmutex_common.h"
  17047. @@ -69,6 +75,12 @@ static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  17048. clear_rt_mutex_waiters(lock);
  17049. }
  17050. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  17051. +{
  17052. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  17053. + waiter != PI_REQUEUE_INPROGRESS;
  17054. +}
  17055. +
  17056. /*
  17057. * We can speed up the acquire/release, if there's no debugging state to be
  17058. * set up.
  17059. @@ -99,13 +111,14 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17060. * 2) Drop lock->wait_lock
  17061. * 3) Try to unlock the lock with cmpxchg
  17062. */
  17063. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17064. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17065. + unsigned long flags)
  17066. __releases(lock->wait_lock)
  17067. {
  17068. struct task_struct *owner = rt_mutex_owner(lock);
  17069. clear_rt_mutex_waiters(lock);
  17070. - raw_spin_unlock(&lock->wait_lock);
  17071. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17072. /*
  17073. * If a new waiter comes in between the unlock and the cmpxchg
  17074. * we have two situations:
  17075. @@ -147,11 +160,12 @@ static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  17076. /*
  17077. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  17078. */
  17079. -static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  17080. +static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  17081. + unsigned long flags)
  17082. __releases(lock->wait_lock)
  17083. {
  17084. lock->owner = NULL;
  17085. - raw_spin_unlock(&lock->wait_lock);
  17086. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17087. return true;
  17088. }
  17089. #endif
  17090. @@ -348,6 +362,14 @@ static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  17091. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  17092. }
  17093. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  17094. +{
  17095. + if (waiter->savestate)
  17096. + wake_up_lock_sleeper(waiter->task);
  17097. + else
  17098. + wake_up_process(waiter->task);
  17099. +}
  17100. +
  17101. /*
  17102. * Max number of times we'll walk the boosting chain:
  17103. */
  17104. @@ -355,7 +377,8 @@ int max_lock_depth = 1024;
  17105. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  17106. {
  17107. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  17108. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  17109. + p->pi_blocked_on->lock : NULL;
  17110. }
  17111. /*
  17112. @@ -433,7 +456,6 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17113. int ret = 0, depth = 0;
  17114. struct rt_mutex *lock;
  17115. bool detect_deadlock;
  17116. - unsigned long flags;
  17117. bool requeue = true;
  17118. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  17119. @@ -476,7 +498,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17120. /*
  17121. * [1] Task cannot go away as we did a get_task() before !
  17122. */
  17123. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17124. + raw_spin_lock_irq(&task->pi_lock);
  17125. /*
  17126. * [2] Get the waiter on which @task is blocked on.
  17127. @@ -492,7 +514,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17128. * reached or the state of the chain has changed while we
  17129. * dropped the locks.
  17130. */
  17131. - if (!waiter)
  17132. + if (!rt_mutex_real_waiter(waiter))
  17133. goto out_unlock_pi;
  17134. /*
  17135. @@ -560,7 +582,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17136. * operations.
  17137. */
  17138. if (!raw_spin_trylock(&lock->wait_lock)) {
  17139. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17140. + raw_spin_unlock_irq(&task->pi_lock);
  17141. cpu_relax();
  17142. goto retry;
  17143. }
  17144. @@ -591,7 +613,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17145. /*
  17146. * No requeue[7] here. Just release @task [8]
  17147. */
  17148. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17149. + raw_spin_unlock(&task->pi_lock);
  17150. put_task_struct(task);
  17151. /*
  17152. @@ -599,14 +621,14 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17153. * If there is no owner of the lock, end of chain.
  17154. */
  17155. if (!rt_mutex_owner(lock)) {
  17156. - raw_spin_unlock(&lock->wait_lock);
  17157. + raw_spin_unlock_irq(&lock->wait_lock);
  17158. return 0;
  17159. }
  17160. /* [10] Grab the next task, i.e. owner of @lock */
  17161. task = rt_mutex_owner(lock);
  17162. get_task_struct(task);
  17163. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17164. + raw_spin_lock(&task->pi_lock);
  17165. /*
  17166. * No requeue [11] here. We just do deadlock detection.
  17167. @@ -621,8 +643,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17168. top_waiter = rt_mutex_top_waiter(lock);
  17169. /* [13] Drop locks */
  17170. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17171. - raw_spin_unlock(&lock->wait_lock);
  17172. + raw_spin_unlock(&task->pi_lock);
  17173. + raw_spin_unlock_irq(&lock->wait_lock);
  17174. /* If owner is not blocked, end of chain. */
  17175. if (!next_lock)
  17176. @@ -643,7 +665,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17177. rt_mutex_enqueue(lock, waiter);
  17178. /* [8] Release the task */
  17179. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17180. + raw_spin_unlock(&task->pi_lock);
  17181. put_task_struct(task);
  17182. /*
  17183. @@ -654,21 +676,24 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17184. * follow here. This is the end of the chain we are walking.
  17185. */
  17186. if (!rt_mutex_owner(lock)) {
  17187. + struct rt_mutex_waiter *lock_top_waiter;
  17188. +
  17189. /*
  17190. * If the requeue [7] above changed the top waiter,
  17191. * then we need to wake the new top waiter up to try
  17192. * to get the lock.
  17193. */
  17194. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  17195. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  17196. - raw_spin_unlock(&lock->wait_lock);
  17197. + lock_top_waiter = rt_mutex_top_waiter(lock);
  17198. + if (prerequeue_top_waiter != lock_top_waiter)
  17199. + rt_mutex_wake_waiter(lock_top_waiter);
  17200. + raw_spin_unlock_irq(&lock->wait_lock);
  17201. return 0;
  17202. }
  17203. /* [10] Grab the next task, i.e. the owner of @lock */
  17204. task = rt_mutex_owner(lock);
  17205. get_task_struct(task);
  17206. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17207. + raw_spin_lock(&task->pi_lock);
  17208. /* [11] requeue the pi waiters if necessary */
  17209. if (waiter == rt_mutex_top_waiter(lock)) {
  17210. @@ -722,8 +747,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17211. top_waiter = rt_mutex_top_waiter(lock);
  17212. /* [13] Drop the locks */
  17213. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17214. - raw_spin_unlock(&lock->wait_lock);
  17215. + raw_spin_unlock(&task->pi_lock);
  17216. + raw_spin_unlock_irq(&lock->wait_lock);
  17217. /*
  17218. * Make the actual exit decisions [12], based on the stored
  17219. @@ -746,28 +771,46 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  17220. goto again;
  17221. out_unlock_pi:
  17222. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17223. + raw_spin_unlock_irq(&task->pi_lock);
  17224. out_put_task:
  17225. put_task_struct(task);
  17226. return ret;
  17227. }
  17228. +
  17229. +#define STEAL_NORMAL 0
  17230. +#define STEAL_LATERAL 1
  17231. +
  17232. +/*
  17233. + * Note that RT tasks are excluded from lateral-steals to prevent the
  17234. + * introduction of an unbounded latency
  17235. + */
  17236. +static inline int lock_is_stealable(struct task_struct *task,
  17237. + struct task_struct *pendowner, int mode)
  17238. +{
  17239. + if (mode == STEAL_NORMAL || rt_task(task)) {
  17240. + if (task->prio >= pendowner->prio)
  17241. + return 0;
  17242. + } else if (task->prio > pendowner->prio)
  17243. + return 0;
  17244. + return 1;
  17245. +}
  17246. +
  17247. /*
  17248. * Try to take an rt-mutex
  17249. *
  17250. - * Must be called with lock->wait_lock held.
  17251. + * Must be called with lock->wait_lock held and interrupts disabled
  17252. *
  17253. * @lock: The lock to be acquired.
  17254. * @task: The task which wants to acquire the lock
  17255. * @waiter: The waiter that is queued to the lock's wait tree if the
  17256. * callsite called task_blocked_on_lock(), otherwise NULL
  17257. */
  17258. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17259. - struct rt_mutex_waiter *waiter)
  17260. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  17261. + struct task_struct *task,
  17262. + struct rt_mutex_waiter *waiter, int mode)
  17263. {
  17264. - unsigned long flags;
  17265. -
  17266. /*
  17267. * Before testing whether we can acquire @lock, we set the
  17268. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  17269. @@ -803,8 +846,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17270. * If waiter is not the highest priority waiter of
  17271. * @lock, give up.
  17272. */
  17273. - if (waiter != rt_mutex_top_waiter(lock))
  17274. + if (waiter != rt_mutex_top_waiter(lock)) {
  17275. + /* XXX lock_is_stealable() ? */
  17276. return 0;
  17277. + }
  17278. /*
  17279. * We can acquire the lock. Remove the waiter from the
  17280. @@ -822,14 +867,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17281. * not need to be dequeued.
  17282. */
  17283. if (rt_mutex_has_waiters(lock)) {
  17284. - /*
  17285. - * If @task->prio is greater than or equal to
  17286. - * the top waiter priority (kernel view),
  17287. - * @task lost.
  17288. - */
  17289. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  17290. - return 0;
  17291. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  17292. + if (task != pown && !lock_is_stealable(task, pown, mode))
  17293. + return 0;
  17294. /*
  17295. * The current top waiter stays enqueued. We
  17296. * don't have to change anything in the lock
  17297. @@ -852,7 +893,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17298. * case, but conditionals are more expensive than a redundant
  17299. * store.
  17300. */
  17301. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17302. + raw_spin_lock(&task->pi_lock);
  17303. task->pi_blocked_on = NULL;
  17304. /*
  17305. * Finish the lock acquisition. @task is the new owner. If
  17306. @@ -861,7 +902,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17307. */
  17308. if (rt_mutex_has_waiters(lock))
  17309. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  17310. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17311. + raw_spin_unlock(&task->pi_lock);
  17312. takeit:
  17313. /* We got the lock. */
  17314. @@ -878,12 +919,405 @@ takeit:
  17315. return 1;
  17316. }
  17317. +#ifdef CONFIG_PREEMPT_RT_FULL
  17318. +/*
  17319. + * preemptible spin_lock functions:
  17320. + */
  17321. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  17322. + void (*slowfn)(struct rt_mutex *lock,
  17323. + bool mg_off),
  17324. + bool do_mig_dis)
  17325. +{
  17326. + might_sleep_no_state_check();
  17327. +
  17328. + if (do_mig_dis)
  17329. + migrate_disable();
  17330. +
  17331. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  17332. + rt_mutex_deadlock_account_lock(lock, current);
  17333. + else
  17334. + slowfn(lock, do_mig_dis);
  17335. +}
  17336. +
  17337. +static inline void rt_spin_lock_fastunlock(struct rt_mutex *lock,
  17338. + void (*slowfn)(struct rt_mutex *lock))
  17339. +{
  17340. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
  17341. + rt_mutex_deadlock_account_unlock(current);
  17342. + else
  17343. + slowfn(lock);
  17344. +}
  17345. +#ifdef CONFIG_SMP
  17346. +/*
  17347. + * Note that owner is a speculative pointer and dereferencing relies
  17348. + * on rcu_read_lock() and the check against the lock owner.
  17349. + */
  17350. +static int adaptive_wait(struct rt_mutex *lock,
  17351. + struct task_struct *owner)
  17352. +{
  17353. + int res = 0;
  17354. +
  17355. + rcu_read_lock();
  17356. + for (;;) {
  17357. + if (owner != rt_mutex_owner(lock))
  17358. + break;
  17359. + /*
  17360. + * Ensure that owner->on_cpu is dereferenced _after_
  17361. + * checking the above to be valid.
  17362. + */
  17363. + barrier();
  17364. + if (!owner->on_cpu) {
  17365. + res = 1;
  17366. + break;
  17367. + }
  17368. + cpu_relax();
  17369. + }
  17370. + rcu_read_unlock();
  17371. + return res;
  17372. +}
  17373. +#else
  17374. +static int adaptive_wait(struct rt_mutex *lock,
  17375. + struct task_struct *orig_owner)
  17376. +{
  17377. + return 1;
  17378. +}
  17379. +#endif
  17380. +
  17381. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17382. + struct rt_mutex_waiter *waiter,
  17383. + struct task_struct *task,
  17384. + enum rtmutex_chainwalk chwalk);
  17385. +/*
  17386. + * Slow path lock function spin_lock style: this variant is very
  17387. + * careful not to miss any non-lock wakeups.
  17388. + *
  17389. + * We store the current state under p->pi_lock in p->saved_state and
  17390. + * the try_to_wake_up() code handles this accordingly.
  17391. + */
  17392. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  17393. + bool mg_off)
  17394. +{
  17395. + struct task_struct *lock_owner, *self = current;
  17396. + struct rt_mutex_waiter waiter, *top_waiter;
  17397. + unsigned long flags;
  17398. + int ret;
  17399. +
  17400. + rt_mutex_init_waiter(&waiter, true);
  17401. +
  17402. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17403. +
  17404. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  17405. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17406. + return;
  17407. + }
  17408. +
  17409. + BUG_ON(rt_mutex_owner(lock) == self);
  17410. +
  17411. + /*
  17412. + * We save whatever state the task is in and we'll restore it
  17413. + * after acquiring the lock taking real wakeups into account
  17414. + * as well. We are serialized via pi_lock against wakeups. See
  17415. + * try_to_wake_up().
  17416. + */
  17417. + raw_spin_lock(&self->pi_lock);
  17418. + self->saved_state = self->state;
  17419. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17420. + raw_spin_unlock(&self->pi_lock);
  17421. +
  17422. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  17423. + BUG_ON(ret);
  17424. +
  17425. + for (;;) {
  17426. + /* Try to acquire the lock again. */
  17427. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  17428. + break;
  17429. +
  17430. + top_waiter = rt_mutex_top_waiter(lock);
  17431. + lock_owner = rt_mutex_owner(lock);
  17432. +
  17433. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17434. +
  17435. + debug_rt_mutex_print_deadlock(&waiter);
  17436. +
  17437. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  17438. + if (mg_off)
  17439. + migrate_enable();
  17440. + schedule();
  17441. + if (mg_off)
  17442. + migrate_disable();
  17443. + }
  17444. +
  17445. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17446. +
  17447. + raw_spin_lock(&self->pi_lock);
  17448. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  17449. + raw_spin_unlock(&self->pi_lock);
  17450. + }
  17451. +
  17452. + /*
  17453. + * Restore the task state to current->saved_state. We set it
  17454. + * to the original state above and the try_to_wake_up() code
  17455. + * has possibly updated it when a real (non-rtmutex) wakeup
  17456. + * happened while we were blocked. Clear saved_state so
  17457. + * try_to_wakeup() does not get confused.
  17458. + */
  17459. + raw_spin_lock(&self->pi_lock);
  17460. + __set_current_state_no_track(self->saved_state);
  17461. + self->saved_state = TASK_RUNNING;
  17462. + raw_spin_unlock(&self->pi_lock);
  17463. +
  17464. + /*
  17465. + * try_to_take_rt_mutex() sets the waiter bit
  17466. + * unconditionally. We might have to fix that up:
  17467. + */
  17468. + fixup_rt_mutex_waiters(lock);
  17469. +
  17470. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  17471. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  17472. +
  17473. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17474. +
  17475. + debug_rt_mutex_free_waiter(&waiter);
  17476. +}
  17477. +
  17478. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17479. + struct wake_q_head *wake_sleeper_q,
  17480. + struct rt_mutex *lock);
  17481. +/*
  17482. + * Slow path to release a rt_mutex spin_lock style
  17483. + */
  17484. +static void noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  17485. +{
  17486. + unsigned long flags;
  17487. + WAKE_Q(wake_q);
  17488. + WAKE_Q(wake_sleeper_q);
  17489. +
  17490. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  17491. +
  17492. + debug_rt_mutex_unlock(lock);
  17493. +
  17494. + rt_mutex_deadlock_account_unlock(current);
  17495. +
  17496. + if (!rt_mutex_has_waiters(lock)) {
  17497. + lock->owner = NULL;
  17498. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17499. + return;
  17500. + }
  17501. +
  17502. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  17503. +
  17504. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  17505. + wake_up_q(&wake_q);
  17506. + wake_up_q_sleeper(&wake_sleeper_q);
  17507. +
  17508. + /* Undo pi boosting.when necessary */
  17509. + rt_mutex_adjust_prio(current);
  17510. +}
  17511. +
  17512. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  17513. +{
  17514. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  17515. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17516. +}
  17517. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  17518. +
  17519. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  17520. +{
  17521. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17522. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  17523. +}
  17524. +EXPORT_SYMBOL(rt_spin_lock);
  17525. +
  17526. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  17527. +{
  17528. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  17529. +}
  17530. +EXPORT_SYMBOL(__rt_spin_lock);
  17531. +
  17532. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  17533. +{
  17534. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  17535. +}
  17536. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  17537. +
  17538. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17539. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  17540. +{
  17541. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  17542. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  17543. +}
  17544. +EXPORT_SYMBOL(rt_spin_lock_nested);
  17545. +#endif
  17546. +
  17547. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  17548. +{
  17549. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17550. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17551. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17552. +}
  17553. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  17554. +
  17555. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  17556. +{
  17557. + /* NOTE: we always pass in '1' for nested, for simplicity */
  17558. + spin_release(&lock->dep_map, 1, _RET_IP_);
  17559. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  17560. + migrate_enable();
  17561. +}
  17562. +EXPORT_SYMBOL(rt_spin_unlock);
  17563. +
  17564. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  17565. +{
  17566. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  17567. +}
  17568. +EXPORT_SYMBOL(__rt_spin_unlock);
  17569. +
  17570. +/*
  17571. + * Wait for the lock to get unlocked: instead of polling for an unlock
  17572. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  17573. + * schedule if there's contention:
  17574. + */
  17575. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  17576. +{
  17577. + spin_lock(lock);
  17578. + spin_unlock(lock);
  17579. +}
  17580. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  17581. +
  17582. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  17583. +{
  17584. + return rt_mutex_trylock(lock);
  17585. +}
  17586. +
  17587. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  17588. +{
  17589. + int ret;
  17590. +
  17591. + ret = rt_mutex_trylock(&lock->lock);
  17592. + if (ret)
  17593. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17594. + return ret;
  17595. +}
  17596. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  17597. +
  17598. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  17599. +{
  17600. + int ret;
  17601. +
  17602. + migrate_disable();
  17603. + ret = rt_mutex_trylock(&lock->lock);
  17604. + if (ret)
  17605. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17606. + else
  17607. + migrate_enable();
  17608. + return ret;
  17609. +}
  17610. +EXPORT_SYMBOL(rt_spin_trylock);
  17611. +
  17612. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  17613. +{
  17614. + int ret;
  17615. +
  17616. + local_bh_disable();
  17617. + ret = rt_mutex_trylock(&lock->lock);
  17618. + if (ret) {
  17619. + migrate_disable();
  17620. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17621. + } else
  17622. + local_bh_enable();
  17623. + return ret;
  17624. +}
  17625. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  17626. +
  17627. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  17628. +{
  17629. + int ret;
  17630. +
  17631. + *flags = 0;
  17632. + ret = rt_mutex_trylock(&lock->lock);
  17633. + if (ret) {
  17634. + migrate_disable();
  17635. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  17636. + }
  17637. + return ret;
  17638. +}
  17639. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  17640. +
  17641. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  17642. +{
  17643. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  17644. + if (atomic_add_unless(atomic, -1, 1))
  17645. + return 0;
  17646. + rt_spin_lock(lock);
  17647. + if (atomic_dec_and_test(atomic))
  17648. + return 1;
  17649. + rt_spin_unlock(lock);
  17650. + return 0;
  17651. +}
  17652. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  17653. +
  17654. + void
  17655. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  17656. +{
  17657. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  17658. + /*
  17659. + * Make sure we are not reinitializing a held lock:
  17660. + */
  17661. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  17662. + lockdep_init_map(&lock->dep_map, name, key, 0);
  17663. +#endif
  17664. +}
  17665. +EXPORT_SYMBOL(__rt_spin_lock_init);
  17666. +
  17667. +#endif /* PREEMPT_RT_FULL */
  17668. +
  17669. +#ifdef CONFIG_PREEMPT_RT_FULL
  17670. + static inline int __sched
  17671. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17672. +{
  17673. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17674. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  17675. +
  17676. + if (!hold_ctx)
  17677. + return 0;
  17678. +
  17679. + if (unlikely(ctx == hold_ctx))
  17680. + return -EALREADY;
  17681. +
  17682. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  17683. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  17684. +#ifdef CONFIG_DEBUG_MUTEXES
  17685. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  17686. + ctx->contending_lock = ww;
  17687. +#endif
  17688. + return -EDEADLK;
  17689. + }
  17690. +
  17691. + return 0;
  17692. +}
  17693. +#else
  17694. + static inline int __sched
  17695. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  17696. +{
  17697. + BUG();
  17698. + return 0;
  17699. +}
  17700. +
  17701. +#endif
  17702. +
  17703. +static inline int
  17704. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  17705. + struct rt_mutex_waiter *waiter)
  17706. +{
  17707. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  17708. +}
  17709. +
  17710. /*
  17711. * Task blocks on lock.
  17712. *
  17713. * Prepare waiter and propagate pi chain
  17714. *
  17715. - * This must be called with lock->wait_lock held.
  17716. + * This must be called with lock->wait_lock held and interrupts disabled
  17717. */
  17718. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17719. struct rt_mutex_waiter *waiter,
  17720. @@ -894,7 +1328,6 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17721. struct rt_mutex_waiter *top_waiter = waiter;
  17722. struct rt_mutex *next_lock;
  17723. int chain_walk = 0, res;
  17724. - unsigned long flags;
  17725. /*
  17726. * Early deadlock detection. We really don't want the task to
  17727. @@ -908,7 +1341,24 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17728. if (owner == task)
  17729. return -EDEADLK;
  17730. - raw_spin_lock_irqsave(&task->pi_lock, flags);
  17731. + raw_spin_lock(&task->pi_lock);
  17732. +
  17733. + /*
  17734. + * In the case of futex requeue PI, this will be a proxy
  17735. + * lock. The task will wake unaware that it is enqueueed on
  17736. + * this lock. Avoid blocking on two locks and corrupting
  17737. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  17738. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  17739. + * before requeue (due to a signal or timeout). Do not enqueue
  17740. + * the task if PI_WAKEUP_INPROGRESS is set.
  17741. + */
  17742. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  17743. + raw_spin_unlock(&task->pi_lock);
  17744. + return -EAGAIN;
  17745. + }
  17746. +
  17747. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  17748. +
  17749. __rt_mutex_adjust_prio(task);
  17750. waiter->task = task;
  17751. waiter->lock = lock;
  17752. @@ -921,18 +1371,18 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17753. task->pi_blocked_on = waiter;
  17754. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17755. + raw_spin_unlock(&task->pi_lock);
  17756. if (!owner)
  17757. return 0;
  17758. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17759. + raw_spin_lock(&owner->pi_lock);
  17760. if (waiter == rt_mutex_top_waiter(lock)) {
  17761. rt_mutex_dequeue_pi(owner, top_waiter);
  17762. rt_mutex_enqueue_pi(owner, waiter);
  17763. __rt_mutex_adjust_prio(owner);
  17764. - if (owner->pi_blocked_on)
  17765. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17766. chain_walk = 1;
  17767. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  17768. chain_walk = 1;
  17769. @@ -941,7 +1391,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17770. /* Store the lock on which owner is blocked or NULL */
  17771. next_lock = task_blocked_on_lock(owner);
  17772. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17773. + raw_spin_unlock(&owner->pi_lock);
  17774. /*
  17775. * Even if full deadlock detection is on, if the owner is not
  17776. * blocked itself, we can avoid finding this out in the chain
  17777. @@ -957,12 +1407,12 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17778. */
  17779. get_task_struct(owner);
  17780. - raw_spin_unlock(&lock->wait_lock);
  17781. + raw_spin_unlock_irq(&lock->wait_lock);
  17782. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  17783. next_lock, waiter, task);
  17784. - raw_spin_lock(&lock->wait_lock);
  17785. + raw_spin_lock_irq(&lock->wait_lock);
  17786. return res;
  17787. }
  17788. @@ -971,15 +1421,15 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  17789. * Remove the top waiter from the current tasks pi waiter tree and
  17790. * queue it up.
  17791. *
  17792. - * Called with lock->wait_lock held.
  17793. + * Called with lock->wait_lock held and interrupts disabled.
  17794. */
  17795. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17796. + struct wake_q_head *wake_sleeper_q,
  17797. struct rt_mutex *lock)
  17798. {
  17799. struct rt_mutex_waiter *waiter;
  17800. - unsigned long flags;
  17801. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17802. + raw_spin_lock(&current->pi_lock);
  17803. waiter = rt_mutex_top_waiter(lock);
  17804. @@ -1001,15 +1451,18 @@ static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  17805. */
  17806. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  17807. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17808. + raw_spin_unlock(&current->pi_lock);
  17809. - wake_q_add(wake_q, waiter->task);
  17810. + if (waiter->savestate)
  17811. + wake_q_add(wake_sleeper_q, waiter->task);
  17812. + else
  17813. + wake_q_add(wake_q, waiter->task);
  17814. }
  17815. /*
  17816. * Remove a waiter from a lock and give up
  17817. *
  17818. - * Must be called with lock->wait_lock held and
  17819. + * Must be called with lock->wait_lock held and interrupts disabled. I must
  17820. * have just failed to try_to_take_rt_mutex().
  17821. */
  17822. static void remove_waiter(struct rt_mutex *lock,
  17823. @@ -1017,13 +1470,12 @@ static void remove_waiter(struct rt_mutex *lock,
  17824. {
  17825. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  17826. struct task_struct *owner = rt_mutex_owner(lock);
  17827. - struct rt_mutex *next_lock;
  17828. - unsigned long flags;
  17829. + struct rt_mutex *next_lock = NULL;
  17830. - raw_spin_lock_irqsave(&current->pi_lock, flags);
  17831. + raw_spin_lock(&current->pi_lock);
  17832. rt_mutex_dequeue(lock, waiter);
  17833. current->pi_blocked_on = NULL;
  17834. - raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  17835. + raw_spin_unlock(&current->pi_lock);
  17836. /*
  17837. * Only update priority if the waiter was the highest priority
  17838. @@ -1032,7 +1484,7 @@ static void remove_waiter(struct rt_mutex *lock,
  17839. if (!owner || !is_top_waiter)
  17840. return;
  17841. - raw_spin_lock_irqsave(&owner->pi_lock, flags);
  17842. + raw_spin_lock(&owner->pi_lock);
  17843. rt_mutex_dequeue_pi(owner, waiter);
  17844. @@ -1042,9 +1494,10 @@ static void remove_waiter(struct rt_mutex *lock,
  17845. __rt_mutex_adjust_prio(owner);
  17846. /* Store the lock on which owner is blocked or NULL */
  17847. - next_lock = task_blocked_on_lock(owner);
  17848. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  17849. + next_lock = task_blocked_on_lock(owner);
  17850. - raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  17851. + raw_spin_unlock(&owner->pi_lock);
  17852. /*
  17853. * Don't walk the chain, if the owner task is not blocked
  17854. @@ -1056,12 +1509,12 @@ static void remove_waiter(struct rt_mutex *lock,
  17855. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17856. get_task_struct(owner);
  17857. - raw_spin_unlock(&lock->wait_lock);
  17858. + raw_spin_unlock_irq(&lock->wait_lock);
  17859. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  17860. next_lock, NULL, current);
  17861. - raw_spin_lock(&lock->wait_lock);
  17862. + raw_spin_lock_irq(&lock->wait_lock);
  17863. }
  17864. /*
  17865. @@ -1078,17 +1531,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  17866. raw_spin_lock_irqsave(&task->pi_lock, flags);
  17867. waiter = task->pi_blocked_on;
  17868. - if (!waiter || (waiter->prio == task->prio &&
  17869. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  17870. !dl_prio(task->prio))) {
  17871. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17872. return;
  17873. }
  17874. next_lock = waiter->lock;
  17875. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17876. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  17877. get_task_struct(task);
  17878. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  17879. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  17880. next_lock, NULL, task);
  17881. }
  17882. @@ -1097,16 +1550,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  17883. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  17884. * @lock: the rt_mutex to take
  17885. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  17886. - * or TASK_UNINTERRUPTIBLE)
  17887. + * or TASK_UNINTERRUPTIBLE)
  17888. * @timeout: the pre-initialized and started timer, or NULL for none
  17889. * @waiter: the pre-initialized rt_mutex_waiter
  17890. *
  17891. - * lock->wait_lock must be held by the caller.
  17892. + * Must be called with lock->wait_lock held and interrupts disabled
  17893. */
  17894. static int __sched
  17895. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17896. struct hrtimer_sleeper *timeout,
  17897. - struct rt_mutex_waiter *waiter)
  17898. + struct rt_mutex_waiter *waiter,
  17899. + struct ww_acquire_ctx *ww_ctx)
  17900. {
  17901. int ret = 0;
  17902. @@ -1129,13 +1583,19 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  17903. break;
  17904. }
  17905. - raw_spin_unlock(&lock->wait_lock);
  17906. + if (ww_ctx && ww_ctx->acquired > 0) {
  17907. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  17908. + if (ret)
  17909. + break;
  17910. + }
  17911. +
  17912. + raw_spin_unlock_irq(&lock->wait_lock);
  17913. debug_rt_mutex_print_deadlock(waiter);
  17914. schedule();
  17915. - raw_spin_lock(&lock->wait_lock);
  17916. + raw_spin_lock_irq(&lock->wait_lock);
  17917. set_current_state(state);
  17918. }
  17919. @@ -1163,26 +1623,112 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  17920. }
  17921. }
  17922. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  17923. + struct ww_acquire_ctx *ww_ctx)
  17924. +{
  17925. +#ifdef CONFIG_DEBUG_MUTEXES
  17926. + /*
  17927. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  17928. + * but released with a normal mutex_unlock in this call.
  17929. + *
  17930. + * This should never happen, always use ww_mutex_unlock.
  17931. + */
  17932. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  17933. +
  17934. + /*
  17935. + * Not quite done after calling ww_acquire_done() ?
  17936. + */
  17937. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  17938. +
  17939. + if (ww_ctx->contending_lock) {
  17940. + /*
  17941. + * After -EDEADLK you tried to
  17942. + * acquire a different ww_mutex? Bad!
  17943. + */
  17944. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  17945. +
  17946. + /*
  17947. + * You called ww_mutex_lock after receiving -EDEADLK,
  17948. + * but 'forgot' to unlock everything else first?
  17949. + */
  17950. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  17951. + ww_ctx->contending_lock = NULL;
  17952. + }
  17953. +
  17954. + /*
  17955. + * Naughty, using a different class will lead to undefined behavior!
  17956. + */
  17957. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  17958. +#endif
  17959. + ww_ctx->acquired++;
  17960. +}
  17961. +
  17962. +#ifdef CONFIG_PREEMPT_RT_FULL
  17963. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17964. + struct ww_acquire_ctx *ww_ctx)
  17965. +{
  17966. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  17967. + struct rt_mutex_waiter *waiter, *n;
  17968. +
  17969. + /*
  17970. + * This branch gets optimized out for the common case,
  17971. + * and is only important for ww_mutex_lock.
  17972. + */
  17973. + ww_mutex_lock_acquired(ww, ww_ctx);
  17974. + ww->ctx = ww_ctx;
  17975. +
  17976. + /*
  17977. + * Give any possible sleeping processes the chance to wake up,
  17978. + * so they can recheck if they have to back off.
  17979. + */
  17980. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  17981. + tree_entry) {
  17982. + /* XXX debug rt mutex waiter wakeup */
  17983. +
  17984. + BUG_ON(waiter->lock != lock);
  17985. + rt_mutex_wake_waiter(waiter);
  17986. + }
  17987. +}
  17988. +
  17989. +#else
  17990. +
  17991. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  17992. + struct ww_acquire_ctx *ww_ctx)
  17993. +{
  17994. + BUG();
  17995. +}
  17996. +#endif
  17997. +
  17998. /*
  17999. * Slow path lock function:
  18000. */
  18001. static int __sched
  18002. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18003. struct hrtimer_sleeper *timeout,
  18004. - enum rtmutex_chainwalk chwalk)
  18005. + enum rtmutex_chainwalk chwalk,
  18006. + struct ww_acquire_ctx *ww_ctx)
  18007. {
  18008. struct rt_mutex_waiter waiter;
  18009. + unsigned long flags;
  18010. int ret = 0;
  18011. - debug_rt_mutex_init_waiter(&waiter);
  18012. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  18013. - RB_CLEAR_NODE(&waiter.tree_entry);
  18014. + rt_mutex_init_waiter(&waiter, false);
  18015. - raw_spin_lock(&lock->wait_lock);
  18016. + /*
  18017. + * Technically we could use raw_spin_[un]lock_irq() here, but this can
  18018. + * be called in early boot if the cmpxchg() fast path is disabled
  18019. + * (debug, no architecture support). In this case we will acquire the
  18020. + * rtmutex with lock->wait_lock held. But we cannot unconditionally
  18021. + * enable interrupts in that early boot case. So we need to use the
  18022. + * irqsave/restore variants.
  18023. + */
  18024. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18025. /* Try to acquire the lock again: */
  18026. if (try_to_take_rt_mutex(lock, current, NULL)) {
  18027. - raw_spin_unlock(&lock->wait_lock);
  18028. + if (ww_ctx)
  18029. + ww_mutex_account_lock(lock, ww_ctx);
  18030. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18031. return 0;
  18032. }
  18033. @@ -1196,13 +1742,23 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18034. if (likely(!ret))
  18035. /* sleep on the mutex */
  18036. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  18037. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  18038. + ww_ctx);
  18039. + else if (ww_ctx) {
  18040. + /* ww_mutex received EDEADLK, let it become EALREADY */
  18041. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  18042. + BUG_ON(!ret);
  18043. + }
  18044. if (unlikely(ret)) {
  18045. __set_current_state(TASK_RUNNING);
  18046. if (rt_mutex_has_waiters(lock))
  18047. remove_waiter(lock, &waiter);
  18048. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18049. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  18050. + if (!ww_ctx)
  18051. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  18052. + } else if (ww_ctx) {
  18053. + ww_mutex_account_lock(lock, ww_ctx);
  18054. }
  18055. /*
  18056. @@ -1211,7 +1767,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18057. */
  18058. fixup_rt_mutex_waiters(lock);
  18059. - raw_spin_unlock(&lock->wait_lock);
  18060. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18061. /* Remove pending timer: */
  18062. if (unlikely(timeout))
  18063. @@ -1227,6 +1783,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  18064. */
  18065. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18066. {
  18067. + unsigned long flags;
  18068. int ret;
  18069. /*
  18070. @@ -1238,10 +1795,10 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18071. return 0;
  18072. /*
  18073. - * The mutex has currently no owner. Lock the wait lock and
  18074. - * try to acquire the lock.
  18075. + * The mutex has currently no owner. Lock the wait lock and try to
  18076. + * acquire the lock. We use irqsave here to support early boot calls.
  18077. */
  18078. - raw_spin_lock(&lock->wait_lock);
  18079. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18080. ret = try_to_take_rt_mutex(lock, current, NULL);
  18081. @@ -1251,7 +1808,7 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18082. */
  18083. fixup_rt_mutex_waiters(lock);
  18084. - raw_spin_unlock(&lock->wait_lock);
  18085. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18086. return ret;
  18087. }
  18088. @@ -1261,9 +1818,13 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  18089. * Return whether the current task needs to undo a potential priority boosting.
  18090. */
  18091. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18092. - struct wake_q_head *wake_q)
  18093. + struct wake_q_head *wake_q,
  18094. + struct wake_q_head *wake_sleeper_q)
  18095. {
  18096. - raw_spin_lock(&lock->wait_lock);
  18097. + unsigned long flags;
  18098. +
  18099. + /* irqsave required to support early boot calls */
  18100. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18101. debug_rt_mutex_unlock(lock);
  18102. @@ -1302,10 +1863,10 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18103. */
  18104. while (!rt_mutex_has_waiters(lock)) {
  18105. /* Drops lock->wait_lock ! */
  18106. - if (unlock_rt_mutex_safe(lock) == true)
  18107. + if (unlock_rt_mutex_safe(lock, flags) == true)
  18108. return false;
  18109. /* Relock the rtmutex and try again */
  18110. - raw_spin_lock(&lock->wait_lock);
  18111. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  18112. }
  18113. /*
  18114. @@ -1314,9 +1875,9 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18115. *
  18116. * Queue the next waiter for wakeup once we release the wait_lock.
  18117. */
  18118. - mark_wakeup_next_waiter(wake_q, lock);
  18119. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  18120. - raw_spin_unlock(&lock->wait_lock);
  18121. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  18122. /* check PI boosting */
  18123. return true;
  18124. @@ -1330,31 +1891,36 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  18125. */
  18126. static inline int
  18127. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  18128. + struct ww_acquire_ctx *ww_ctx,
  18129. int (*slowfn)(struct rt_mutex *lock, int state,
  18130. struct hrtimer_sleeper *timeout,
  18131. - enum rtmutex_chainwalk chwalk))
  18132. + enum rtmutex_chainwalk chwalk,
  18133. + struct ww_acquire_ctx *ww_ctx))
  18134. {
  18135. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  18136. rt_mutex_deadlock_account_lock(lock, current);
  18137. return 0;
  18138. } else
  18139. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  18140. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  18141. + ww_ctx);
  18142. }
  18143. static inline int
  18144. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  18145. struct hrtimer_sleeper *timeout,
  18146. enum rtmutex_chainwalk chwalk,
  18147. + struct ww_acquire_ctx *ww_ctx,
  18148. int (*slowfn)(struct rt_mutex *lock, int state,
  18149. struct hrtimer_sleeper *timeout,
  18150. - enum rtmutex_chainwalk chwalk))
  18151. + enum rtmutex_chainwalk chwalk,
  18152. + struct ww_acquire_ctx *ww_ctx))
  18153. {
  18154. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  18155. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  18156. rt_mutex_deadlock_account_lock(lock, current);
  18157. return 0;
  18158. } else
  18159. - return slowfn(lock, state, timeout, chwalk);
  18160. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  18161. }
  18162. static inline int
  18163. @@ -1371,17 +1937,20 @@ rt_mutex_fasttrylock(struct rt_mutex *lock,
  18164. static inline void
  18165. rt_mutex_fastunlock(struct rt_mutex *lock,
  18166. bool (*slowfn)(struct rt_mutex *lock,
  18167. - struct wake_q_head *wqh))
  18168. + struct wake_q_head *wqh,
  18169. + struct wake_q_head *wq_sleeper))
  18170. {
  18171. WAKE_Q(wake_q);
  18172. + WAKE_Q(wake_sleeper_q);
  18173. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18174. rt_mutex_deadlock_account_unlock(current);
  18175. } else {
  18176. - bool deboost = slowfn(lock, &wake_q);
  18177. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  18178. wake_up_q(&wake_q);
  18179. + wake_up_q_sleeper(&wake_sleeper_q);
  18180. /* Undo pi boosting if necessary: */
  18181. if (deboost)
  18182. @@ -1398,7 +1967,7 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
  18183. {
  18184. might_sleep();
  18185. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  18186. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  18187. }
  18188. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  18189. @@ -1415,7 +1984,7 @@ int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  18190. {
  18191. might_sleep();
  18192. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  18193. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  18194. }
  18195. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  18196. @@ -1428,11 +1997,30 @@ int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  18197. might_sleep();
  18198. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18199. - RT_MUTEX_FULL_CHAINWALK,
  18200. + RT_MUTEX_FULL_CHAINWALK, NULL,
  18201. rt_mutex_slowlock);
  18202. }
  18203. /**
  18204. + * rt_mutex_lock_killable - lock a rt_mutex killable
  18205. + *
  18206. + * @lock: the rt_mutex to be locked
  18207. + * @detect_deadlock: deadlock detection on/off
  18208. + *
  18209. + * Returns:
  18210. + * 0 on success
  18211. + * -EINTR when interrupted by a signal
  18212. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  18213. + */
  18214. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  18215. +{
  18216. + might_sleep();
  18217. +
  18218. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  18219. +}
  18220. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  18221. +
  18222. +/**
  18223. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  18224. * the timeout structure is provided
  18225. * by the caller
  18226. @@ -1452,6 +2040,7 @@ rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  18227. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  18228. RT_MUTEX_MIN_CHAINWALK,
  18229. + NULL,
  18230. rt_mutex_slowlock);
  18231. }
  18232. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18233. @@ -1469,7 +2058,11 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  18234. */
  18235. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  18236. {
  18237. +#ifdef CONFIG_PREEMPT_RT_FULL
  18238. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  18239. +#else
  18240. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  18241. +#endif
  18242. return 0;
  18243. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  18244. @@ -1495,13 +2088,14 @@ EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  18245. * required or not.
  18246. */
  18247. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  18248. - struct wake_q_head *wqh)
  18249. + struct wake_q_head *wqh,
  18250. + struct wake_q_head *wq_sleeper)
  18251. {
  18252. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  18253. rt_mutex_deadlock_account_unlock(current);
  18254. return false;
  18255. }
  18256. - return rt_mutex_slowunlock(lock, wqh);
  18257. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  18258. }
  18259. /**
  18260. @@ -1534,13 +2128,12 @@ EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  18261. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  18262. {
  18263. lock->owner = NULL;
  18264. - raw_spin_lock_init(&lock->wait_lock);
  18265. lock->waiters = RB_ROOT;
  18266. lock->waiters_leftmost = NULL;
  18267. debug_rt_mutex_init(lock, name);
  18268. }
  18269. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18270. +EXPORT_SYMBOL(__rt_mutex_init);
  18271. /**
  18272. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  18273. @@ -1555,7 +2148,7 @@ EXPORT_SYMBOL_GPL(__rt_mutex_init);
  18274. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18275. struct task_struct *proxy_owner)
  18276. {
  18277. - __rt_mutex_init(lock, NULL);
  18278. + rt_mutex_init(lock);
  18279. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  18280. rt_mutex_set_owner(lock, proxy_owner);
  18281. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  18282. @@ -1596,13 +2189,42 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  18283. {
  18284. int ret;
  18285. - raw_spin_lock(&lock->wait_lock);
  18286. + raw_spin_lock_irq(&lock->wait_lock);
  18287. if (try_to_take_rt_mutex(lock, task, NULL)) {
  18288. - raw_spin_unlock(&lock->wait_lock);
  18289. + raw_spin_unlock_irq(&lock->wait_lock);
  18290. return 1;
  18291. }
  18292. +#ifdef CONFIG_PREEMPT_RT_FULL
  18293. + /*
  18294. + * In PREEMPT_RT there's an added race.
  18295. + * If the task, that we are about to requeue, times out,
  18296. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  18297. + * to skip this task. But right after the task sets
  18298. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  18299. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  18300. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  18301. + * lock that it blocks on. We *must not* place this task
  18302. + * on this proxy lock in that case.
  18303. + *
  18304. + * To prevent this race, we first take the task's pi_lock
  18305. + * and check if it has updated its pi_blocked_on. If it has,
  18306. + * we assume that it woke up and we return -EAGAIN.
  18307. + * Otherwise, we set the task's pi_blocked_on to
  18308. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  18309. + * it will know that we are in the process of requeuing it.
  18310. + */
  18311. + raw_spin_lock(&task->pi_lock);
  18312. + if (task->pi_blocked_on) {
  18313. + raw_spin_unlock(&task->pi_lock);
  18314. + raw_spin_unlock_irq(&lock->wait_lock);
  18315. + return -EAGAIN;
  18316. + }
  18317. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  18318. + raw_spin_unlock(&task->pi_lock);
  18319. +#endif
  18320. +
  18321. /* We enforce deadlock detection for futexes */
  18322. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  18323. RT_MUTEX_FULL_CHAINWALK);
  18324. @@ -1617,10 +2239,10 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  18325. ret = 0;
  18326. }
  18327. - if (unlikely(ret))
  18328. + if (ret && rt_mutex_has_waiters(lock))
  18329. remove_waiter(lock, waiter);
  18330. - raw_spin_unlock(&lock->wait_lock);
  18331. + raw_spin_unlock_irq(&lock->wait_lock);
  18332. debug_rt_mutex_print_deadlock(waiter);
  18333. @@ -1668,12 +2290,12 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  18334. {
  18335. int ret;
  18336. - raw_spin_lock(&lock->wait_lock);
  18337. + raw_spin_lock_irq(&lock->wait_lock);
  18338. set_current_state(TASK_INTERRUPTIBLE);
  18339. /* sleep on the mutex */
  18340. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  18341. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  18342. if (unlikely(ret))
  18343. remove_waiter(lock, waiter);
  18344. @@ -1684,7 +2306,93 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  18345. */
  18346. fixup_rt_mutex_waiters(lock);
  18347. - raw_spin_unlock(&lock->wait_lock);
  18348. + raw_spin_unlock_irq(&lock->wait_lock);
  18349. return ret;
  18350. }
  18351. +
  18352. +static inline int
  18353. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  18354. +{
  18355. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  18356. + unsigned tmp;
  18357. +
  18358. + if (ctx->deadlock_inject_countdown-- == 0) {
  18359. + tmp = ctx->deadlock_inject_interval;
  18360. + if (tmp > UINT_MAX/4)
  18361. + tmp = UINT_MAX;
  18362. + else
  18363. + tmp = tmp*2 + tmp + tmp/2;
  18364. +
  18365. + ctx->deadlock_inject_interval = tmp;
  18366. + ctx->deadlock_inject_countdown = tmp;
  18367. + ctx->contending_lock = lock;
  18368. +
  18369. + ww_mutex_unlock(lock);
  18370. +
  18371. + return -EDEADLK;
  18372. + }
  18373. +#endif
  18374. +
  18375. + return 0;
  18376. +}
  18377. +
  18378. +#ifdef CONFIG_PREEMPT_RT_FULL
  18379. +int __sched
  18380. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18381. +{
  18382. + int ret;
  18383. +
  18384. + might_sleep();
  18385. +
  18386. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18387. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  18388. + if (ret)
  18389. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18390. + else if (!ret && ww_ctx->acquired > 1)
  18391. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18392. +
  18393. + return ret;
  18394. +}
  18395. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  18396. +
  18397. +int __sched
  18398. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  18399. +{
  18400. + int ret;
  18401. +
  18402. + might_sleep();
  18403. +
  18404. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  18405. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  18406. + if (ret)
  18407. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  18408. + else if (!ret && ww_ctx->acquired > 1)
  18409. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  18410. +
  18411. + return ret;
  18412. +}
  18413. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  18414. +
  18415. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  18416. +{
  18417. + int nest = !!lock->ctx;
  18418. +
  18419. + /*
  18420. + * The unlocking fastpath is the 0->1 transition from 'locked'
  18421. + * into 'unlocked' state:
  18422. + */
  18423. + if (nest) {
  18424. +#ifdef CONFIG_DEBUG_MUTEXES
  18425. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  18426. +#endif
  18427. + if (lock->ctx->acquired > 0)
  18428. + lock->ctx->acquired--;
  18429. + lock->ctx = NULL;
  18430. + }
  18431. +
  18432. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  18433. + rt_mutex_unlock(&lock->base.lock);
  18434. +}
  18435. +EXPORT_SYMBOL(ww_mutex_unlock);
  18436. +#endif
  18437. diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
  18438. index 4f5f83c7d2d3..289f062f26cd 100644
  18439. --- a/kernel/locking/rtmutex_common.h
  18440. +++ b/kernel/locking/rtmutex_common.h
  18441. @@ -27,6 +27,7 @@ struct rt_mutex_waiter {
  18442. struct rb_node pi_tree_entry;
  18443. struct task_struct *task;
  18444. struct rt_mutex *lock;
  18445. + bool savestate;
  18446. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18447. unsigned long ip;
  18448. struct pid *deadlock_task_pid;
  18449. @@ -97,6 +98,9 @@ enum rtmutex_chainwalk {
  18450. /*
  18451. * PI-futex support (proxy locking functions, etc.):
  18452. */
  18453. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  18454. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  18455. +
  18456. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  18457. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  18458. struct task_struct *proxy_owner);
  18459. @@ -110,7 +114,8 @@ extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  18460. struct rt_mutex_waiter *waiter);
  18461. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  18462. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  18463. - struct wake_q_head *wqh);
  18464. + struct wake_q_head *wqh,
  18465. + struct wake_q_head *wq_sleeper);
  18466. extern void rt_mutex_adjust_prio(struct task_struct *task);
  18467. #ifdef CONFIG_DEBUG_RT_MUTEXES
  18468. @@ -119,4 +124,14 @@ extern void rt_mutex_adjust_prio(struct task_struct *task);
  18469. # include "rtmutex.h"
  18470. #endif
  18471. +static inline void
  18472. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  18473. +{
  18474. + debug_rt_mutex_init_waiter(waiter);
  18475. + waiter->task = NULL;
  18476. + waiter->savestate = savestate;
  18477. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  18478. + RB_CLEAR_NODE(&waiter->tree_entry);
  18479. +}
  18480. +
  18481. #endif
  18482. diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c
  18483. index db3ccb1dd614..909779647bd1 100644
  18484. --- a/kernel/locking/spinlock.c
  18485. +++ b/kernel/locking/spinlock.c
  18486. @@ -124,8 +124,11 @@ void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
  18487. * __[spin|read|write]_lock_bh()
  18488. */
  18489. BUILD_LOCK_OPS(spin, raw_spinlock);
  18490. +
  18491. +#ifndef CONFIG_PREEMPT_RT_FULL
  18492. BUILD_LOCK_OPS(read, rwlock);
  18493. BUILD_LOCK_OPS(write, rwlock);
  18494. +#endif
  18495. #endif
  18496. @@ -209,6 +212,8 @@ void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
  18497. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  18498. #endif
  18499. +#ifndef CONFIG_PREEMPT_RT_FULL
  18500. +
  18501. #ifndef CONFIG_INLINE_READ_TRYLOCK
  18502. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  18503. {
  18504. @@ -353,6 +358,8 @@ void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
  18505. EXPORT_SYMBOL(_raw_write_unlock_bh);
  18506. #endif
  18507. +#endif /* !PREEMPT_RT_FULL */
  18508. +
  18509. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  18510. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  18511. diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
  18512. index 0374a596cffa..94970338d518 100644
  18513. --- a/kernel/locking/spinlock_debug.c
  18514. +++ b/kernel/locking/spinlock_debug.c
  18515. @@ -31,6 +31,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  18516. EXPORT_SYMBOL(__raw_spin_lock_init);
  18517. +#ifndef CONFIG_PREEMPT_RT_FULL
  18518. void __rwlock_init(rwlock_t *lock, const char *name,
  18519. struct lock_class_key *key)
  18520. {
  18521. @@ -48,6 +49,7 @@ void __rwlock_init(rwlock_t *lock, const char *name,
  18522. }
  18523. EXPORT_SYMBOL(__rwlock_init);
  18524. +#endif
  18525. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  18526. {
  18527. @@ -159,6 +161,7 @@ void do_raw_spin_unlock(raw_spinlock_t *lock)
  18528. arch_spin_unlock(&lock->raw_lock);
  18529. }
  18530. +#ifndef CONFIG_PREEMPT_RT_FULL
  18531. static void rwlock_bug(rwlock_t *lock, const char *msg)
  18532. {
  18533. if (!debug_locks_off())
  18534. @@ -300,3 +303,5 @@ void do_raw_write_unlock(rwlock_t *lock)
  18535. debug_write_unlock(lock);
  18536. arch_write_unlock(&lock->raw_lock);
  18537. }
  18538. +
  18539. +#endif
  18540. diff --git a/kernel/panic.c b/kernel/panic.c
  18541. index 41e2b54f36b5..3535f802953a 100644
  18542. --- a/kernel/panic.c
  18543. +++ b/kernel/panic.c
  18544. @@ -61,6 +61,37 @@ void __weak panic_smp_self_stop(void)
  18545. cpu_relax();
  18546. }
  18547. +/*
  18548. + * Stop ourselves in NMI context if another CPU has already panicked. Arch code
  18549. + * may override this to prepare for crash dumping, e.g. save regs info.
  18550. + */
  18551. +void __weak nmi_panic_self_stop(struct pt_regs *regs)
  18552. +{
  18553. + panic_smp_self_stop();
  18554. +}
  18555. +
  18556. +atomic_t panic_cpu = ATOMIC_INIT(PANIC_CPU_INVALID);
  18557. +
  18558. +/*
  18559. + * A variant of panic() called from NMI context. We return if we've already
  18560. + * panicked on this CPU. If another CPU already panicked, loop in
  18561. + * nmi_panic_self_stop() which can provide architecture dependent code such
  18562. + * as saving register state for crash dump.
  18563. + */
  18564. +void nmi_panic(struct pt_regs *regs, const char *msg)
  18565. +{
  18566. + int old_cpu, cpu;
  18567. +
  18568. + cpu = raw_smp_processor_id();
  18569. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, cpu);
  18570. +
  18571. + if (old_cpu == PANIC_CPU_INVALID)
  18572. + panic("%s", msg);
  18573. + else if (old_cpu != cpu)
  18574. + nmi_panic_self_stop(regs);
  18575. +}
  18576. +EXPORT_SYMBOL(nmi_panic);
  18577. +
  18578. /**
  18579. * panic - halt the system
  18580. * @fmt: The text string to print
  18581. @@ -71,17 +102,17 @@ void __weak panic_smp_self_stop(void)
  18582. */
  18583. void panic(const char *fmt, ...)
  18584. {
  18585. - static DEFINE_SPINLOCK(panic_lock);
  18586. static char buf[1024];
  18587. va_list args;
  18588. long i, i_next = 0;
  18589. int state = 0;
  18590. + int old_cpu, this_cpu;
  18591. /*
  18592. * Disable local interrupts. This will prevent panic_smp_self_stop
  18593. * from deadlocking the first cpu that invokes the panic, since
  18594. * there is nothing to prevent an interrupt handler (that runs
  18595. - * after the panic_lock is acquired) from invoking panic again.
  18596. + * after setting panic_cpu) from invoking panic() again.
  18597. */
  18598. local_irq_disable();
  18599. @@ -94,8 +125,16 @@ void panic(const char *fmt, ...)
  18600. * multiple parallel invocations of panic, all other CPUs either
  18601. * stop themself or will wait until they are stopped by the 1st CPU
  18602. * with smp_send_stop().
  18603. + *
  18604. + * `old_cpu == PANIC_CPU_INVALID' means this is the 1st CPU which
  18605. + * comes here, so go ahead.
  18606. + * `old_cpu == this_cpu' means we came from nmi_panic() which sets
  18607. + * panic_cpu to this CPU. In this case, this is also the 1st CPU.
  18608. */
  18609. - if (!spin_trylock(&panic_lock))
  18610. + this_cpu = raw_smp_processor_id();
  18611. + old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  18612. +
  18613. + if (old_cpu != PANIC_CPU_INVALID && old_cpu != this_cpu)
  18614. panic_smp_self_stop();
  18615. console_verbose();
  18616. @@ -400,9 +439,11 @@ static u64 oops_id;
  18617. static int init_oops_id(void)
  18618. {
  18619. +#ifndef CONFIG_PREEMPT_RT_FULL
  18620. if (!oops_id)
  18621. get_random_bytes(&oops_id, sizeof(oops_id));
  18622. else
  18623. +#endif
  18624. oops_id++;
  18625. return 0;
  18626. diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
  18627. index b7dd5718836e..908ee58cc11c 100644
  18628. --- a/kernel/power/hibernate.c
  18629. +++ b/kernel/power/hibernate.c
  18630. @@ -285,6 +285,8 @@ static int create_image(int platform_mode)
  18631. local_irq_disable();
  18632. + system_state = SYSTEM_SUSPEND;
  18633. +
  18634. error = syscore_suspend();
  18635. if (error) {
  18636. printk(KERN_ERR "PM: Some system devices failed to power down, "
  18637. @@ -314,6 +316,7 @@ static int create_image(int platform_mode)
  18638. syscore_resume();
  18639. Enable_irqs:
  18640. + system_state = SYSTEM_RUNNING;
  18641. local_irq_enable();
  18642. Enable_cpus:
  18643. @@ -438,6 +441,7 @@ static int resume_target_kernel(bool platform_mode)
  18644. goto Enable_cpus;
  18645. local_irq_disable();
  18646. + system_state = SYSTEM_SUSPEND;
  18647. error = syscore_suspend();
  18648. if (error)
  18649. @@ -471,6 +475,7 @@ static int resume_target_kernel(bool platform_mode)
  18650. syscore_resume();
  18651. Enable_irqs:
  18652. + system_state = SYSTEM_RUNNING;
  18653. local_irq_enable();
  18654. Enable_cpus:
  18655. @@ -556,6 +561,7 @@ int hibernation_platform_enter(void)
  18656. goto Enable_cpus;
  18657. local_irq_disable();
  18658. + system_state = SYSTEM_SUSPEND;
  18659. syscore_suspend();
  18660. if (pm_wakeup_pending()) {
  18661. error = -EAGAIN;
  18662. @@ -568,6 +574,7 @@ int hibernation_platform_enter(void)
  18663. Power_up:
  18664. syscore_resume();
  18665. + system_state = SYSTEM_RUNNING;
  18666. local_irq_enable();
  18667. Enable_cpus:
  18668. @@ -642,6 +649,10 @@ static void power_down(void)
  18669. cpu_relax();
  18670. }
  18671. +#ifndef CONFIG_SUSPEND
  18672. +bool pm_in_action;
  18673. +#endif
  18674. +
  18675. /**
  18676. * hibernate - Carry out system hibernation, including saving the image.
  18677. */
  18678. @@ -654,6 +665,8 @@ int hibernate(void)
  18679. return -EPERM;
  18680. }
  18681. + pm_in_action = true;
  18682. +
  18683. lock_system_sleep();
  18684. /* The snapshot device should not be opened while we're running */
  18685. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  18686. @@ -719,6 +732,7 @@ int hibernate(void)
  18687. atomic_inc(&snapshot_device_available);
  18688. Unlock:
  18689. unlock_system_sleep();
  18690. + pm_in_action = false;
  18691. return error;
  18692. }
  18693. diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
  18694. index f9fe133c13e2..393bc342c586 100644
  18695. --- a/kernel/power/suspend.c
  18696. +++ b/kernel/power/suspend.c
  18697. @@ -359,6 +359,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  18698. arch_suspend_disable_irqs();
  18699. BUG_ON(!irqs_disabled());
  18700. + system_state = SYSTEM_SUSPEND;
  18701. +
  18702. error = syscore_suspend();
  18703. if (!error) {
  18704. *wakeup = pm_wakeup_pending();
  18705. @@ -375,6 +377,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  18706. syscore_resume();
  18707. }
  18708. + system_state = SYSTEM_RUNNING;
  18709. +
  18710. arch_suspend_enable_irqs();
  18711. BUG_ON(irqs_disabled());
  18712. @@ -518,6 +522,8 @@ static int enter_state(suspend_state_t state)
  18713. return error;
  18714. }
  18715. +bool pm_in_action;
  18716. +
  18717. /**
  18718. * pm_suspend - Externally visible function for suspending the system.
  18719. * @state: System sleep state to enter.
  18720. @@ -532,6 +538,8 @@ int pm_suspend(suspend_state_t state)
  18721. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  18722. return -EINVAL;
  18723. + pm_in_action = true;
  18724. +
  18725. error = enter_state(state);
  18726. if (error) {
  18727. suspend_stats.fail++;
  18728. @@ -539,6 +547,7 @@ int pm_suspend(suspend_state_t state)
  18729. } else {
  18730. suspend_stats.success++;
  18731. }
  18732. + pm_in_action = false;
  18733. return error;
  18734. }
  18735. EXPORT_SYMBOL(pm_suspend);
  18736. diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
  18737. index c048e34b177f..c747bdfa199e 100644
  18738. --- a/kernel/printk/printk.c
  18739. +++ b/kernel/printk/printk.c
  18740. @@ -241,6 +241,65 @@ struct printk_log {
  18741. */
  18742. static DEFINE_RAW_SPINLOCK(logbuf_lock);
  18743. +#ifdef CONFIG_EARLY_PRINTK
  18744. +struct console *early_console;
  18745. +
  18746. +static void early_vprintk(const char *fmt, va_list ap)
  18747. +{
  18748. + if (early_console) {
  18749. + char buf[512];
  18750. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18751. +
  18752. + early_console->write(early_console, buf, n);
  18753. + }
  18754. +}
  18755. +
  18756. +asmlinkage void early_printk(const char *fmt, ...)
  18757. +{
  18758. + va_list ap;
  18759. +
  18760. + va_start(ap, fmt);
  18761. + early_vprintk(fmt, ap);
  18762. + va_end(ap);
  18763. +}
  18764. +
  18765. +/*
  18766. + * This is independent of any log levels - a global
  18767. + * kill switch that turns off all of printk.
  18768. + *
  18769. + * Used by the NMI watchdog if early-printk is enabled.
  18770. + */
  18771. +static bool __read_mostly printk_killswitch;
  18772. +
  18773. +static int __init force_early_printk_setup(char *str)
  18774. +{
  18775. + printk_killswitch = true;
  18776. + return 0;
  18777. +}
  18778. +early_param("force_early_printk", force_early_printk_setup);
  18779. +
  18780. +void printk_kill(void)
  18781. +{
  18782. + printk_killswitch = true;
  18783. +}
  18784. +
  18785. +#ifdef CONFIG_PRINTK
  18786. +static int forced_early_printk(const char *fmt, va_list ap)
  18787. +{
  18788. + if (!printk_killswitch)
  18789. + return 0;
  18790. + early_vprintk(fmt, ap);
  18791. + return 1;
  18792. +}
  18793. +#endif
  18794. +
  18795. +#else
  18796. +static inline int forced_early_printk(const char *fmt, va_list ap)
  18797. +{
  18798. + return 0;
  18799. +}
  18800. +#endif
  18801. +
  18802. #ifdef CONFIG_PRINTK
  18803. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  18804. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  18805. @@ -1203,6 +1262,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  18806. {
  18807. char *text;
  18808. int len = 0;
  18809. + int attempts = 0;
  18810. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  18811. if (!text)
  18812. @@ -1214,7 +1274,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  18813. u64 seq;
  18814. u32 idx;
  18815. enum log_flags prev;
  18816. -
  18817. + int num_msg;
  18818. +try_again:
  18819. + attempts++;
  18820. + if (attempts > 10) {
  18821. + len = -EBUSY;
  18822. + goto out;
  18823. + }
  18824. + num_msg = 0;
  18825. if (clear_seq < log_first_seq) {
  18826. /* messages are gone, move to first available one */
  18827. clear_seq = log_first_seq;
  18828. @@ -1235,6 +1302,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  18829. prev = msg->flags;
  18830. idx = log_next(idx);
  18831. seq++;
  18832. + num_msg++;
  18833. + if (num_msg > 5) {
  18834. + num_msg = 0;
  18835. + raw_spin_unlock_irq(&logbuf_lock);
  18836. + raw_spin_lock_irq(&logbuf_lock);
  18837. + if (clear_seq < log_first_seq)
  18838. + goto try_again;
  18839. + }
  18840. }
  18841. /* move first record forward until length fits into the buffer */
  18842. @@ -1248,6 +1323,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  18843. prev = msg->flags;
  18844. idx = log_next(idx);
  18845. seq++;
  18846. + num_msg++;
  18847. + if (num_msg > 5) {
  18848. + num_msg = 0;
  18849. + raw_spin_unlock_irq(&logbuf_lock);
  18850. + raw_spin_lock_irq(&logbuf_lock);
  18851. + if (clear_seq < log_first_seq)
  18852. + goto try_again;
  18853. + }
  18854. }
  18855. /* last message fitting into this dump */
  18856. @@ -1288,6 +1371,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  18857. clear_seq = log_next_seq;
  18858. clear_idx = log_next_idx;
  18859. }
  18860. +out:
  18861. raw_spin_unlock_irq(&logbuf_lock);
  18862. kfree(text);
  18863. @@ -1443,6 +1527,12 @@ static void call_console_drivers(int level,
  18864. if (!console_drivers)
  18865. return;
  18866. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18867. + if (in_irq() || in_nmi())
  18868. + return;
  18869. + }
  18870. +
  18871. + migrate_disable();
  18872. for_each_console(con) {
  18873. if (exclusive_console && con != exclusive_console)
  18874. continue;
  18875. @@ -1458,6 +1548,7 @@ static void call_console_drivers(int level,
  18876. else
  18877. con->write(con, text, len);
  18878. }
  18879. + migrate_enable();
  18880. }
  18881. /*
  18882. @@ -1518,6 +1609,15 @@ static inline int can_use_console(unsigned int cpu)
  18883. static int console_trylock_for_printk(void)
  18884. {
  18885. unsigned int cpu = smp_processor_id();
  18886. +#ifdef CONFIG_PREEMPT_RT_FULL
  18887. + int lock = !early_boot_irqs_disabled && (preempt_count() == 0) &&
  18888. + !irqs_disabled();
  18889. +#else
  18890. + int lock = 1;
  18891. +#endif
  18892. +
  18893. + if (!lock)
  18894. + return 0;
  18895. if (!console_trylock())
  18896. return 0;
  18897. @@ -1672,6 +1772,13 @@ asmlinkage int vprintk_emit(int facility, int level,
  18898. /* cpu currently holding logbuf_lock in this function */
  18899. static unsigned int logbuf_cpu = UINT_MAX;
  18900. + /*
  18901. + * Fall back to early_printk if a debugging subsystem has
  18902. + * killed printk output
  18903. + */
  18904. + if (unlikely(forced_early_printk(fmt, args)))
  18905. + return 1;
  18906. +
  18907. if (level == LOGLEVEL_SCHED) {
  18908. level = LOGLEVEL_DEFAULT;
  18909. in_sched = true;
  18910. @@ -1813,8 +1920,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  18911. * console_sem which would prevent anyone from printing to
  18912. * console
  18913. */
  18914. - preempt_disable();
  18915. -
  18916. + migrate_disable();
  18917. /*
  18918. * Try to acquire and then immediately release the console
  18919. * semaphore. The release will print out buffers and wake up
  18920. @@ -1822,7 +1928,7 @@ asmlinkage int vprintk_emit(int facility, int level,
  18921. */
  18922. if (console_trylock_for_printk())
  18923. console_unlock();
  18924. - preempt_enable();
  18925. + migrate_enable();
  18926. lockdep_on();
  18927. }
  18928. @@ -1961,26 +2067,6 @@ DEFINE_PER_CPU(printk_func_t, printk_func);
  18929. #endif /* CONFIG_PRINTK */
  18930. -#ifdef CONFIG_EARLY_PRINTK
  18931. -struct console *early_console;
  18932. -
  18933. -asmlinkage __visible void early_printk(const char *fmt, ...)
  18934. -{
  18935. - va_list ap;
  18936. - char buf[512];
  18937. - int n;
  18938. -
  18939. - if (!early_console)
  18940. - return;
  18941. -
  18942. - va_start(ap, fmt);
  18943. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  18944. - va_end(ap);
  18945. -
  18946. - early_console->write(early_console, buf, n);
  18947. -}
  18948. -#endif
  18949. -
  18950. static int __add_preferred_console(char *name, int idx, char *options,
  18951. char *brl_options)
  18952. {
  18953. @@ -2202,11 +2288,16 @@ static void console_cont_flush(char *text, size_t size)
  18954. goto out;
  18955. len = cont_print_text(text, size);
  18956. +#ifdef CONFIG_PREEMPT_RT_FULL
  18957. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18958. + call_console_drivers(cont.level, NULL, 0, text, len);
  18959. +#else
  18960. raw_spin_unlock(&logbuf_lock);
  18961. stop_critical_timings();
  18962. call_console_drivers(cont.level, NULL, 0, text, len);
  18963. start_critical_timings();
  18964. local_irq_restore(flags);
  18965. +#endif
  18966. return;
  18967. out:
  18968. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18969. @@ -2316,13 +2407,17 @@ skip:
  18970. console_idx = log_next(console_idx);
  18971. console_seq++;
  18972. console_prev = msg->flags;
  18973. +#ifdef CONFIG_PREEMPT_RT_FULL
  18974. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  18975. + call_console_drivers(level, ext_text, ext_len, text, len);
  18976. +#else
  18977. raw_spin_unlock(&logbuf_lock);
  18978. stop_critical_timings(); /* don't trace print latency */
  18979. call_console_drivers(level, ext_text, ext_len, text, len);
  18980. start_critical_timings();
  18981. local_irq_restore(flags);
  18982. -
  18983. +#endif
  18984. if (do_cond_resched)
  18985. cond_resched();
  18986. }
  18987. @@ -2374,6 +2469,11 @@ void console_unblank(void)
  18988. {
  18989. struct console *c;
  18990. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  18991. + if (in_irq() || in_nmi())
  18992. + return;
  18993. + }
  18994. +
  18995. /*
  18996. * console_unblank can no longer be called in interrupt context unless
  18997. * oops_in_progress is set to 1..
  18998. diff --git a/kernel/ptrace.c b/kernel/ptrace.c
  18999. index 3189e51db7e8..1004af706be7 100644
  19000. --- a/kernel/ptrace.c
  19001. +++ b/kernel/ptrace.c
  19002. @@ -129,7 +129,14 @@ static bool ptrace_freeze_traced(struct task_struct *task)
  19003. spin_lock_irq(&task->sighand->siglock);
  19004. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  19005. - task->state = __TASK_TRACED;
  19006. + unsigned long flags;
  19007. +
  19008. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  19009. + if (task->state & __TASK_TRACED)
  19010. + task->state = __TASK_TRACED;
  19011. + else
  19012. + task->saved_state = __TASK_TRACED;
  19013. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  19014. ret = true;
  19015. }
  19016. spin_unlock_irq(&task->sighand->siglock);
  19017. diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
  19018. index d89328e260df..5bb3364a6284 100644
  19019. --- a/kernel/rcu/rcutorture.c
  19020. +++ b/kernel/rcu/rcutorture.c
  19021. @@ -390,6 +390,7 @@ static struct rcu_torture_ops rcu_ops = {
  19022. .name = "rcu"
  19023. };
  19024. +#ifndef CONFIG_PREEMPT_RT_FULL
  19025. /*
  19026. * Definitions for rcu_bh torture testing.
  19027. */
  19028. @@ -429,6 +430,12 @@ static struct rcu_torture_ops rcu_bh_ops = {
  19029. .name = "rcu_bh"
  19030. };
  19031. +#else
  19032. +static struct rcu_torture_ops rcu_bh_ops = {
  19033. + .ttype = INVALID_RCU_FLAVOR,
  19034. +};
  19035. +#endif
  19036. +
  19037. /*
  19038. * Don't even think about trying any of these in real life!!!
  19039. * The names includes "busted", and they really means it!
  19040. diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
  19041. index f07343b54fe5..d862a203fce0 100644
  19042. --- a/kernel/rcu/tree.c
  19043. +++ b/kernel/rcu/tree.c
  19044. @@ -56,6 +56,11 @@
  19045. #include <linux/random.h>
  19046. #include <linux/trace_events.h>
  19047. #include <linux/suspend.h>
  19048. +#include <linux/delay.h>
  19049. +#include <linux/gfp.h>
  19050. +#include <linux/oom.h>
  19051. +#include <linux/smpboot.h>
  19052. +#include "../time/tick-internal.h"
  19053. #include "tree.h"
  19054. #include "rcu.h"
  19055. @@ -266,6 +271,19 @@ void rcu_sched_qs(void)
  19056. }
  19057. }
  19058. +#ifdef CONFIG_PREEMPT_RT_FULL
  19059. +static void rcu_preempt_qs(void);
  19060. +
  19061. +void rcu_bh_qs(void)
  19062. +{
  19063. + unsigned long flags;
  19064. +
  19065. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  19066. + local_irq_save(flags);
  19067. + rcu_preempt_qs();
  19068. + local_irq_restore(flags);
  19069. +}
  19070. +#else
  19071. void rcu_bh_qs(void)
  19072. {
  19073. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  19074. @@ -275,6 +293,7 @@ void rcu_bh_qs(void)
  19075. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  19076. }
  19077. }
  19078. +#endif
  19079. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  19080. @@ -435,11 +454,13 @@ EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  19081. /*
  19082. * Return the number of RCU BH batches started thus far for debug & stats.
  19083. */
  19084. +#ifndef CONFIG_PREEMPT_RT_FULL
  19085. unsigned long rcu_batches_started_bh(void)
  19086. {
  19087. return rcu_bh_state.gpnum;
  19088. }
  19089. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  19090. +#endif
  19091. /*
  19092. * Return the number of RCU batches completed thus far for debug & stats.
  19093. @@ -459,6 +480,7 @@ unsigned long rcu_batches_completed_sched(void)
  19094. }
  19095. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  19096. +#ifndef CONFIG_PREEMPT_RT_FULL
  19097. /*
  19098. * Return the number of RCU BH batches completed thus far for debug & stats.
  19099. */
  19100. @@ -486,6 +508,13 @@ void rcu_bh_force_quiescent_state(void)
  19101. }
  19102. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  19103. +#else
  19104. +void rcu_force_quiescent_state(void)
  19105. +{
  19106. +}
  19107. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  19108. +#endif
  19109. +
  19110. /*
  19111. * Force a quiescent state for RCU-sched.
  19112. */
  19113. @@ -536,9 +565,11 @@ void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  19114. case RCU_FLAVOR:
  19115. rsp = rcu_state_p;
  19116. break;
  19117. +#ifndef CONFIG_PREEMPT_RT_FULL
  19118. case RCU_BH_FLAVOR:
  19119. rsp = &rcu_bh_state;
  19120. break;
  19121. +#endif
  19122. case RCU_SCHED_FLAVOR:
  19123. rsp = &rcu_sched_state;
  19124. break;
  19125. @@ -1590,7 +1621,6 @@ static int rcu_future_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19126. int needmore;
  19127. struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
  19128. - rcu_nocb_gp_cleanup(rsp, rnp);
  19129. rnp->need_future_gp[c & 0x1] = 0;
  19130. needmore = rnp->need_future_gp[(c + 1) & 0x1];
  19131. trace_rcu_future_gp(rnp, rdp, c,
  19132. @@ -1611,7 +1641,7 @@ static void rcu_gp_kthread_wake(struct rcu_state *rsp)
  19133. !READ_ONCE(rsp->gp_flags) ||
  19134. !rsp->gp_kthread)
  19135. return;
  19136. - wake_up(&rsp->gp_wq);
  19137. + swake_up(&rsp->gp_wq);
  19138. }
  19139. /*
  19140. @@ -1991,6 +2021,7 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  19141. int nocb = 0;
  19142. struct rcu_data *rdp;
  19143. struct rcu_node *rnp = rcu_get_root(rsp);
  19144. + struct swait_queue_head *sq;
  19145. WRITE_ONCE(rsp->gp_activity, jiffies);
  19146. raw_spin_lock_irq(&rnp->lock);
  19147. @@ -2029,7 +2060,9 @@ static void rcu_gp_cleanup(struct rcu_state *rsp)
  19148. needgp = __note_gp_changes(rsp, rnp, rdp) || needgp;
  19149. /* smp_mb() provided by prior unlock-lock pair. */
  19150. nocb += rcu_future_gp_cleanup(rsp, rnp);
  19151. + sq = rcu_nocb_gp_get(rnp);
  19152. raw_spin_unlock_irq(&rnp->lock);
  19153. + rcu_nocb_gp_cleanup(sq);
  19154. cond_resched_rcu_qs();
  19155. WRITE_ONCE(rsp->gp_activity, jiffies);
  19156. rcu_gp_slow(rsp, gp_cleanup_delay);
  19157. @@ -2076,7 +2109,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  19158. READ_ONCE(rsp->gpnum),
  19159. TPS("reqwait"));
  19160. rsp->gp_state = RCU_GP_WAIT_GPS;
  19161. - wait_event_interruptible(rsp->gp_wq,
  19162. + swait_event_interruptible(rsp->gp_wq,
  19163. READ_ONCE(rsp->gp_flags) &
  19164. RCU_GP_FLAG_INIT);
  19165. rsp->gp_state = RCU_GP_DONE_GPS;
  19166. @@ -2106,7 +2139,7 @@ static int __noreturn rcu_gp_kthread(void *arg)
  19167. READ_ONCE(rsp->gpnum),
  19168. TPS("fqswait"));
  19169. rsp->gp_state = RCU_GP_WAIT_FQS;
  19170. - ret = wait_event_interruptible_timeout(rsp->gp_wq,
  19171. + ret = swait_event_interruptible_timeout(rsp->gp_wq,
  19172. rcu_gp_fqs_check_wake(rsp, &gf), j);
  19173. rsp->gp_state = RCU_GP_DOING_FQS;
  19174. /* Locking provides needed memory barriers. */
  19175. @@ -2230,7 +2263,7 @@ static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags)
  19176. WARN_ON_ONCE(!rcu_gp_in_progress(rsp));
  19177. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  19178. raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags);
  19179. - rcu_gp_kthread_wake(rsp);
  19180. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  19181. }
  19182. /*
  19183. @@ -2891,7 +2924,7 @@ static void force_quiescent_state(struct rcu_state *rsp)
  19184. }
  19185. WRITE_ONCE(rsp->gp_flags, READ_ONCE(rsp->gp_flags) | RCU_GP_FLAG_FQS);
  19186. raw_spin_unlock_irqrestore(&rnp_old->lock, flags);
  19187. - rcu_gp_kthread_wake(rsp);
  19188. + swake_up(&rsp->gp_wq); /* Memory barrier implied by swake_up() path. */
  19189. }
  19190. /*
  19191. @@ -2934,18 +2967,17 @@ __rcu_process_callbacks(struct rcu_state *rsp)
  19192. /*
  19193. * Do RCU core processing for the current CPU.
  19194. */
  19195. -static void rcu_process_callbacks(struct softirq_action *unused)
  19196. +static void rcu_process_callbacks(void)
  19197. {
  19198. struct rcu_state *rsp;
  19199. if (cpu_is_offline(smp_processor_id()))
  19200. return;
  19201. - trace_rcu_utilization(TPS("Start RCU core"));
  19202. for_each_rcu_flavor(rsp)
  19203. __rcu_process_callbacks(rsp);
  19204. - trace_rcu_utilization(TPS("End RCU core"));
  19205. }
  19206. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  19207. /*
  19208. * Schedule RCU callback invocation. If the specified type of RCU
  19209. * does not support RCU priority boosting, just do a direct call,
  19210. @@ -2957,18 +2989,105 @@ static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  19211. {
  19212. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  19213. return;
  19214. - if (likely(!rsp->boost)) {
  19215. - rcu_do_batch(rsp, rdp);
  19216. - return;
  19217. - }
  19218. - invoke_rcu_callbacks_kthread();
  19219. + rcu_do_batch(rsp, rdp);
  19220. }
  19221. +static void rcu_wake_cond(struct task_struct *t, int status)
  19222. +{
  19223. + /*
  19224. + * If the thread is yielding, only wake it when this
  19225. + * is invoked from idle
  19226. + */
  19227. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  19228. + wake_up_process(t);
  19229. +}
  19230. +
  19231. +/*
  19232. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  19233. + */
  19234. static void invoke_rcu_core(void)
  19235. {
  19236. - if (cpu_online(smp_processor_id()))
  19237. - raise_softirq(RCU_SOFTIRQ);
  19238. + unsigned long flags;
  19239. + struct task_struct *t;
  19240. +
  19241. + if (!cpu_online(smp_processor_id()))
  19242. + return;
  19243. + local_irq_save(flags);
  19244. + __this_cpu_write(rcu_cpu_has_work, 1);
  19245. + t = __this_cpu_read(rcu_cpu_kthread_task);
  19246. + if (t != NULL && current != t)
  19247. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  19248. + local_irq_restore(flags);
  19249. +}
  19250. +
  19251. +static void rcu_cpu_kthread_park(unsigned int cpu)
  19252. +{
  19253. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19254. +}
  19255. +
  19256. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19257. +{
  19258. + return __this_cpu_read(rcu_cpu_has_work);
  19259. +}
  19260. +
  19261. +/*
  19262. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19263. + * RCU softirq used in flavors and configurations of RCU that do not
  19264. + * support RCU priority boosting.
  19265. + */
  19266. +static void rcu_cpu_kthread(unsigned int cpu)
  19267. +{
  19268. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19269. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19270. + int spincnt;
  19271. +
  19272. + for (spincnt = 0; spincnt < 10; spincnt++) {
  19273. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19274. + local_bh_disable();
  19275. + *statusp = RCU_KTHREAD_RUNNING;
  19276. + this_cpu_inc(rcu_cpu_kthread_loops);
  19277. + local_irq_disable();
  19278. + work = *workp;
  19279. + *workp = 0;
  19280. + local_irq_enable();
  19281. + if (work)
  19282. + rcu_process_callbacks();
  19283. + local_bh_enable();
  19284. + if (*workp == 0) {
  19285. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19286. + *statusp = RCU_KTHREAD_WAITING;
  19287. + return;
  19288. + }
  19289. + }
  19290. + *statusp = RCU_KTHREAD_YIELDING;
  19291. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19292. + schedule_timeout_interruptible(2);
  19293. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19294. + *statusp = RCU_KTHREAD_WAITING;
  19295. +}
  19296. +
  19297. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19298. + .store = &rcu_cpu_kthread_task,
  19299. + .thread_should_run = rcu_cpu_kthread_should_run,
  19300. + .thread_fn = rcu_cpu_kthread,
  19301. + .thread_comm = "rcuc/%u",
  19302. + .setup = rcu_cpu_kthread_setup,
  19303. + .park = rcu_cpu_kthread_park,
  19304. +};
  19305. +
  19306. +/*
  19307. + * Spawn per-CPU RCU core processing kthreads.
  19308. + */
  19309. +static int __init rcu_spawn_core_kthreads(void)
  19310. +{
  19311. + int cpu;
  19312. +
  19313. + for_each_possible_cpu(cpu)
  19314. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  19315. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19316. + return 0;
  19317. }
  19318. +early_initcall(rcu_spawn_core_kthreads);
  19319. /*
  19320. * Handle any core-RCU processing required by a call_rcu() invocation.
  19321. @@ -3114,6 +3233,7 @@ void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  19322. }
  19323. EXPORT_SYMBOL_GPL(call_rcu_sched);
  19324. +#ifndef CONFIG_PREEMPT_RT_FULL
  19325. /*
  19326. * Queue an RCU callback for invocation after a quicker grace period.
  19327. */
  19328. @@ -3122,6 +3242,7 @@ void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  19329. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  19330. }
  19331. EXPORT_SYMBOL_GPL(call_rcu_bh);
  19332. +#endif
  19333. /*
  19334. * Queue an RCU callback for lazy invocation after a grace period.
  19335. @@ -3213,6 +3334,7 @@ void synchronize_sched(void)
  19336. }
  19337. EXPORT_SYMBOL_GPL(synchronize_sched);
  19338. +#ifndef CONFIG_PREEMPT_RT_FULL
  19339. /**
  19340. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  19341. *
  19342. @@ -3239,6 +3361,7 @@ void synchronize_rcu_bh(void)
  19343. wait_rcu_gp(call_rcu_bh);
  19344. }
  19345. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  19346. +#endif
  19347. /**
  19348. * get_state_synchronize_rcu - Snapshot current RCU state
  19349. @@ -3524,7 +3647,7 @@ static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
  19350. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19351. if (wake) {
  19352. smp_mb(); /* EGP done before wake_up(). */
  19353. - wake_up(&rsp->expedited_wq);
  19354. + swake_up(&rsp->expedited_wq);
  19355. }
  19356. break;
  19357. }
  19358. @@ -3781,7 +3904,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  19359. jiffies_start = jiffies;
  19360. for (;;) {
  19361. - ret = wait_event_interruptible_timeout(
  19362. + ret = swait_event_timeout(
  19363. rsp->expedited_wq,
  19364. sync_rcu_preempt_exp_done(rnp_root),
  19365. jiffies_stall);
  19366. @@ -3789,7 +3912,7 @@ static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
  19367. return;
  19368. if (ret < 0) {
  19369. /* Hit a signal, disable CPU stall warnings. */
  19370. - wait_event(rsp->expedited_wq,
  19371. + swait_event(rsp->expedited_wq,
  19372. sync_rcu_preempt_exp_done(rnp_root));
  19373. return;
  19374. }
  19375. @@ -4101,6 +4224,7 @@ static void _rcu_barrier(struct rcu_state *rsp)
  19376. mutex_unlock(&rsp->barrier_mutex);
  19377. }
  19378. +#ifndef CONFIG_PREEMPT_RT_FULL
  19379. /**
  19380. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  19381. */
  19382. @@ -4109,6 +4233,7 @@ void rcu_barrier_bh(void)
  19383. _rcu_barrier(&rcu_bh_state);
  19384. }
  19385. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  19386. +#endif
  19387. /**
  19388. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  19389. @@ -4455,8 +4580,8 @@ static void __init rcu_init_one(struct rcu_state *rsp,
  19390. }
  19391. }
  19392. - init_waitqueue_head(&rsp->gp_wq);
  19393. - init_waitqueue_head(&rsp->expedited_wq);
  19394. + init_swait_queue_head(&rsp->gp_wq);
  19395. + init_swait_queue_head(&rsp->expedited_wq);
  19396. rnp = rsp->level[rcu_num_lvls - 1];
  19397. for_each_possible_cpu(i) {
  19398. while (i > rnp->grphi)
  19399. @@ -4576,12 +4701,13 @@ void __init rcu_init(void)
  19400. rcu_bootup_announce();
  19401. rcu_init_geometry();
  19402. +#ifndef CONFIG_PREEMPT_RT_FULL
  19403. rcu_init_one(&rcu_bh_state, &rcu_bh_data);
  19404. +#endif
  19405. rcu_init_one(&rcu_sched_state, &rcu_sched_data);
  19406. if (dump_tree)
  19407. rcu_dump_rcu_node_tree(&rcu_sched_state);
  19408. __rcu_init_preempt();
  19409. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  19410. /*
  19411. * We don't need protection against CPU-hotplug here because
  19412. diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
  19413. index 9fb4e238d4dc..c75834d8de24 100644
  19414. --- a/kernel/rcu/tree.h
  19415. +++ b/kernel/rcu/tree.h
  19416. @@ -27,6 +27,7 @@
  19417. #include <linux/threads.h>
  19418. #include <linux/cpumask.h>
  19419. #include <linux/seqlock.h>
  19420. +#include <linux/swait.h>
  19421. #include <linux/stop_machine.h>
  19422. /*
  19423. @@ -241,7 +242,7 @@ struct rcu_node {
  19424. /* Refused to boost: not sure why, though. */
  19425. /* This can happen due to race conditions. */
  19426. #ifdef CONFIG_RCU_NOCB_CPU
  19427. - wait_queue_head_t nocb_gp_wq[2];
  19428. + struct swait_queue_head nocb_gp_wq[2];
  19429. /* Place for rcu_nocb_kthread() to wait GP. */
  19430. #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
  19431. int need_future_gp[2];
  19432. @@ -393,7 +394,7 @@ struct rcu_data {
  19433. atomic_long_t nocb_q_count_lazy; /* invocation (all stages). */
  19434. struct rcu_head *nocb_follower_head; /* CBs ready to invoke. */
  19435. struct rcu_head **nocb_follower_tail;
  19436. - wait_queue_head_t nocb_wq; /* For nocb kthreads to sleep on. */
  19437. + struct swait_queue_head nocb_wq; /* For nocb kthreads to sleep on. */
  19438. struct task_struct *nocb_kthread;
  19439. int nocb_defer_wakeup; /* Defer wakeup of nocb_kthread. */
  19440. @@ -472,7 +473,7 @@ struct rcu_state {
  19441. unsigned long gpnum; /* Current gp number. */
  19442. unsigned long completed; /* # of last completed gp. */
  19443. struct task_struct *gp_kthread; /* Task for grace periods. */
  19444. - wait_queue_head_t gp_wq; /* Where GP task waits. */
  19445. + struct swait_queue_head gp_wq; /* Where GP task waits. */
  19446. short gp_flags; /* Commands for GP task. */
  19447. short gp_state; /* GP kthread sleep state. */
  19448. @@ -504,7 +505,7 @@ struct rcu_state {
  19449. atomic_long_t expedited_workdone3; /* # done by others #3. */
  19450. atomic_long_t expedited_normal; /* # fallbacks to normal. */
  19451. atomic_t expedited_need_qs; /* # CPUs left to check in. */
  19452. - wait_queue_head_t expedited_wq; /* Wait for check-ins. */
  19453. + struct swait_queue_head expedited_wq; /* Wait for check-ins. */
  19454. int ncpus_snap; /* # CPUs seen last time. */
  19455. unsigned long jiffies_force_qs; /* Time at which to invoke */
  19456. @@ -556,18 +557,18 @@ extern struct list_head rcu_struct_flavors;
  19457. */
  19458. extern struct rcu_state rcu_sched_state;
  19459. +#ifndef CONFIG_PREEMPT_RT_FULL
  19460. extern struct rcu_state rcu_bh_state;
  19461. +#endif
  19462. #ifdef CONFIG_PREEMPT_RCU
  19463. extern struct rcu_state rcu_preempt_state;
  19464. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  19465. -#ifdef CONFIG_RCU_BOOST
  19466. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19467. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  19468. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19469. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  19470. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19471. #ifndef RCU_TREE_NONCORE
  19472. @@ -587,10 +588,9 @@ void call_rcu(struct rcu_head *head, rcu_callback_t func);
  19473. static void __init __rcu_init_preempt(void);
  19474. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  19475. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  19476. -static void invoke_rcu_callbacks_kthread(void);
  19477. static bool rcu_is_callbacks_kthread(void);
  19478. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  19479. #ifdef CONFIG_RCU_BOOST
  19480. -static void rcu_preempt_do_callbacks(void);
  19481. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  19482. struct rcu_node *rnp);
  19483. #endif /* #ifdef CONFIG_RCU_BOOST */
  19484. @@ -607,7 +607,8 @@ static void zero_cpu_stall_ticks(struct rcu_data *rdp);
  19485. static void increment_cpu_stall_ticks(void);
  19486. static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu);
  19487. static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq);
  19488. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp);
  19489. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp);
  19490. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq);
  19491. static void rcu_init_one_nocb(struct rcu_node *rnp);
  19492. static bool __call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *rhp,
  19493. bool lazy, unsigned long flags);
  19494. diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
  19495. index 630c19772630..8e119cf647ba 100644
  19496. --- a/kernel/rcu/tree_plugin.h
  19497. +++ b/kernel/rcu/tree_plugin.h
  19498. @@ -24,25 +24,10 @@
  19499. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  19500. */
  19501. -#include <linux/delay.h>
  19502. -#include <linux/gfp.h>
  19503. -#include <linux/oom.h>
  19504. -#include <linux/smpboot.h>
  19505. -#include "../time/tick-internal.h"
  19506. -
  19507. #ifdef CONFIG_RCU_BOOST
  19508. #include "../locking/rtmutex_common.h"
  19509. -/*
  19510. - * Control variables for per-CPU and per-rcu_node kthreads. These
  19511. - * handle all flavors of RCU.
  19512. - */
  19513. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  19514. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19515. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19516. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19517. -
  19518. #else /* #ifdef CONFIG_RCU_BOOST */
  19519. /*
  19520. @@ -55,6 +40,14 @@ DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19521. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19522. +/*
  19523. + * Control variables for per-CPU and per-rcu_node kthreads. These
  19524. + * handle all flavors of RCU.
  19525. + */
  19526. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  19527. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  19528. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  19529. +
  19530. #ifdef CONFIG_RCU_NOCB_CPU
  19531. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  19532. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  19533. @@ -432,7 +425,7 @@ void rcu_read_unlock_special(struct task_struct *t)
  19534. }
  19535. /* Hardware IRQ handlers cannot block, complain if they get here. */
  19536. - if (in_irq() || in_serving_softirq()) {
  19537. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  19538. lockdep_rcu_suspicious(__FILE__, __LINE__,
  19539. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  19540. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  19541. @@ -645,15 +638,6 @@ static void rcu_preempt_check_callbacks(void)
  19542. t->rcu_read_unlock_special.b.need_qs = true;
  19543. }
  19544. -#ifdef CONFIG_RCU_BOOST
  19545. -
  19546. -static void rcu_preempt_do_callbacks(void)
  19547. -{
  19548. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  19549. -}
  19550. -
  19551. -#endif /* #ifdef CONFIG_RCU_BOOST */
  19552. -
  19553. /*
  19554. * Queue a preemptible-RCU callback for invocation after a grace period.
  19555. */
  19556. @@ -930,6 +914,19 @@ void exit_rcu(void)
  19557. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  19558. +/*
  19559. + * If boosting, set rcuc kthreads to realtime priority.
  19560. + */
  19561. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  19562. +{
  19563. +#ifdef CONFIG_RCU_BOOST
  19564. + struct sched_param sp;
  19565. +
  19566. + sp.sched_priority = kthread_prio;
  19567. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19568. +#endif /* #ifdef CONFIG_RCU_BOOST */
  19569. +}
  19570. +
  19571. #ifdef CONFIG_RCU_BOOST
  19572. #include "../locking/rtmutex_common.h"
  19573. @@ -961,16 +958,6 @@ static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  19574. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  19575. -static void rcu_wake_cond(struct task_struct *t, int status)
  19576. -{
  19577. - /*
  19578. - * If the thread is yielding, only wake it when this
  19579. - * is invoked from idle
  19580. - */
  19581. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  19582. - wake_up_process(t);
  19583. -}
  19584. -
  19585. /*
  19586. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  19587. * or ->boost_tasks, advancing the pointer to the next task in the
  19588. @@ -1115,23 +1102,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  19589. }
  19590. /*
  19591. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  19592. - */
  19593. -static void invoke_rcu_callbacks_kthread(void)
  19594. -{
  19595. - unsigned long flags;
  19596. -
  19597. - local_irq_save(flags);
  19598. - __this_cpu_write(rcu_cpu_has_work, 1);
  19599. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  19600. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  19601. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  19602. - __this_cpu_read(rcu_cpu_kthread_status));
  19603. - }
  19604. - local_irq_restore(flags);
  19605. -}
  19606. -
  19607. -/*
  19608. * Is the current CPU running the RCU-callbacks kthread?
  19609. * Caller must have preemption disabled.
  19610. */
  19611. @@ -1186,67 +1156,6 @@ static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  19612. return 0;
  19613. }
  19614. -static void rcu_kthread_do_work(void)
  19615. -{
  19616. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  19617. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  19618. - rcu_preempt_do_callbacks();
  19619. -}
  19620. -
  19621. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  19622. -{
  19623. - struct sched_param sp;
  19624. -
  19625. - sp.sched_priority = kthread_prio;
  19626. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  19627. -}
  19628. -
  19629. -static void rcu_cpu_kthread_park(unsigned int cpu)
  19630. -{
  19631. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  19632. -}
  19633. -
  19634. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  19635. -{
  19636. - return __this_cpu_read(rcu_cpu_has_work);
  19637. -}
  19638. -
  19639. -/*
  19640. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  19641. - * RCU softirq used in flavors and configurations of RCU that do not
  19642. - * support RCU priority boosting.
  19643. - */
  19644. -static void rcu_cpu_kthread(unsigned int cpu)
  19645. -{
  19646. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  19647. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  19648. - int spincnt;
  19649. -
  19650. - for (spincnt = 0; spincnt < 10; spincnt++) {
  19651. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  19652. - local_bh_disable();
  19653. - *statusp = RCU_KTHREAD_RUNNING;
  19654. - this_cpu_inc(rcu_cpu_kthread_loops);
  19655. - local_irq_disable();
  19656. - work = *workp;
  19657. - *workp = 0;
  19658. - local_irq_enable();
  19659. - if (work)
  19660. - rcu_kthread_do_work();
  19661. - local_bh_enable();
  19662. - if (*workp == 0) {
  19663. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  19664. - *statusp = RCU_KTHREAD_WAITING;
  19665. - return;
  19666. - }
  19667. - }
  19668. - *statusp = RCU_KTHREAD_YIELDING;
  19669. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  19670. - schedule_timeout_interruptible(2);
  19671. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  19672. - *statusp = RCU_KTHREAD_WAITING;
  19673. -}
  19674. -
  19675. /*
  19676. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  19677. * served by the rcu_node in question. The CPU hotplug lock is still
  19678. @@ -1276,26 +1185,12 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  19679. free_cpumask_var(cm);
  19680. }
  19681. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  19682. - .store = &rcu_cpu_kthread_task,
  19683. - .thread_should_run = rcu_cpu_kthread_should_run,
  19684. - .thread_fn = rcu_cpu_kthread,
  19685. - .thread_comm = "rcuc/%u",
  19686. - .setup = rcu_cpu_kthread_setup,
  19687. - .park = rcu_cpu_kthread_park,
  19688. -};
  19689. -
  19690. /*
  19691. * Spawn boost kthreads -- called as soon as the scheduler is running.
  19692. */
  19693. static void __init rcu_spawn_boost_kthreads(void)
  19694. {
  19695. struct rcu_node *rnp;
  19696. - int cpu;
  19697. -
  19698. - for_each_possible_cpu(cpu)
  19699. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  19700. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  19701. rcu_for_each_leaf_node(rcu_state_p, rnp)
  19702. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  19703. }
  19704. @@ -1318,11 +1213,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  19705. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  19706. }
  19707. -static void invoke_rcu_callbacks_kthread(void)
  19708. -{
  19709. - WARN_ON_ONCE(1);
  19710. -}
  19711. -
  19712. static bool rcu_is_callbacks_kthread(void)
  19713. {
  19714. return false;
  19715. @@ -1346,7 +1236,7 @@ static void rcu_prepare_kthreads(int cpu)
  19716. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  19717. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19718. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  19719. /*
  19720. * Check to see if any future RCU-related work will need to be done
  19721. @@ -1363,7 +1253,9 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  19722. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  19723. ? 0 : rcu_cpu_has_callbacks(NULL);
  19724. }
  19725. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  19726. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  19727. /*
  19728. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  19729. * after it.
  19730. @@ -1459,6 +1351,8 @@ static bool __maybe_unused rcu_try_advance_all_cbs(void)
  19731. return cbs_ready;
  19732. }
  19733. +#ifndef CONFIG_PREEMPT_RT_FULL
  19734. +
  19735. /*
  19736. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  19737. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  19738. @@ -1504,6 +1398,7 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  19739. *nextevt = basemono + dj * TICK_NSEC;
  19740. return 0;
  19741. }
  19742. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  19743. /*
  19744. * Prepare a CPU for idle from an RCU perspective. The first major task
  19745. @@ -1822,9 +1717,9 @@ early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
  19746. * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
  19747. * grace period.
  19748. */
  19749. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19750. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19751. {
  19752. - wake_up_all(&rnp->nocb_gp_wq[rnp->completed & 0x1]);
  19753. + swake_up_all(sq);
  19754. }
  19755. /*
  19756. @@ -1840,10 +1735,15 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  19757. rnp->need_future_gp[(rnp->completed + 1) & 0x1] += nrq;
  19758. }
  19759. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19760. +{
  19761. + return &rnp->nocb_gp_wq[rnp->completed & 0x1];
  19762. +}
  19763. +
  19764. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19765. {
  19766. - init_waitqueue_head(&rnp->nocb_gp_wq[0]);
  19767. - init_waitqueue_head(&rnp->nocb_gp_wq[1]);
  19768. + init_swait_queue_head(&rnp->nocb_gp_wq[0]);
  19769. + init_swait_queue_head(&rnp->nocb_gp_wq[1]);
  19770. }
  19771. #ifndef CONFIG_RCU_NOCB_CPU_ALL
  19772. @@ -1868,7 +1768,7 @@ static void wake_nocb_leader(struct rcu_data *rdp, bool force)
  19773. if (READ_ONCE(rdp_leader->nocb_leader_sleep) || force) {
  19774. /* Prior smp_mb__after_atomic() orders against prior enqueue. */
  19775. WRITE_ONCE(rdp_leader->nocb_leader_sleep, false);
  19776. - wake_up(&rdp_leader->nocb_wq);
  19777. + swake_up(&rdp_leader->nocb_wq);
  19778. }
  19779. }
  19780. @@ -2081,7 +1981,7 @@ static void rcu_nocb_wait_gp(struct rcu_data *rdp)
  19781. */
  19782. trace_rcu_future_gp(rnp, rdp, c, TPS("StartWait"));
  19783. for (;;) {
  19784. - wait_event_interruptible(
  19785. + swait_event_interruptible(
  19786. rnp->nocb_gp_wq[c & 0x1],
  19787. (d = ULONG_CMP_GE(READ_ONCE(rnp->completed), c)));
  19788. if (likely(d))
  19789. @@ -2109,7 +2009,7 @@ wait_again:
  19790. /* Wait for callbacks to appear. */
  19791. if (!rcu_nocb_poll) {
  19792. trace_rcu_nocb_wake(my_rdp->rsp->name, my_rdp->cpu, "Sleep");
  19793. - wait_event_interruptible(my_rdp->nocb_wq,
  19794. + swait_event_interruptible(my_rdp->nocb_wq,
  19795. !READ_ONCE(my_rdp->nocb_leader_sleep));
  19796. /* Memory barrier handled by smp_mb() calls below and repoll. */
  19797. } else if (firsttime) {
  19798. @@ -2184,7 +2084,7 @@ wait_again:
  19799. * List was empty, wake up the follower.
  19800. * Memory barriers supplied by atomic_long_add().
  19801. */
  19802. - wake_up(&rdp->nocb_wq);
  19803. + swake_up(&rdp->nocb_wq);
  19804. }
  19805. }
  19806. @@ -2205,7 +2105,7 @@ static void nocb_follower_wait(struct rcu_data *rdp)
  19807. if (!rcu_nocb_poll) {
  19808. trace_rcu_nocb_wake(rdp->rsp->name, rdp->cpu,
  19809. "FollowerSleep");
  19810. - wait_event_interruptible(rdp->nocb_wq,
  19811. + swait_event_interruptible(rdp->nocb_wq,
  19812. READ_ONCE(rdp->nocb_follower_head));
  19813. } else if (firsttime) {
  19814. /* Don't drown trace log with "Poll"! */
  19815. @@ -2364,7 +2264,7 @@ void __init rcu_init_nohz(void)
  19816. static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
  19817. {
  19818. rdp->nocb_tail = &rdp->nocb_head;
  19819. - init_waitqueue_head(&rdp->nocb_wq);
  19820. + init_swait_queue_head(&rdp->nocb_wq);
  19821. rdp->nocb_follower_tail = &rdp->nocb_follower_head;
  19822. }
  19823. @@ -2514,7 +2414,7 @@ static bool rcu_nocb_cpu_needs_barrier(struct rcu_state *rsp, int cpu)
  19824. return false;
  19825. }
  19826. -static void rcu_nocb_gp_cleanup(struct rcu_state *rsp, struct rcu_node *rnp)
  19827. +static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
  19828. {
  19829. }
  19830. @@ -2522,6 +2422,11 @@ static void rcu_nocb_gp_set(struct rcu_node *rnp, int nrq)
  19831. {
  19832. }
  19833. +static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
  19834. +{
  19835. + return NULL;
  19836. +}
  19837. +
  19838. static void rcu_init_one_nocb(struct rcu_node *rnp)
  19839. {
  19840. }
  19841. diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
  19842. index 5f748c5a40f0..9a3904603ff6 100644
  19843. --- a/kernel/rcu/update.c
  19844. +++ b/kernel/rcu/update.c
  19845. @@ -276,6 +276,7 @@ int rcu_read_lock_held(void)
  19846. }
  19847. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  19848. +#ifndef CONFIG_PREEMPT_RT_FULL
  19849. /**
  19850. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  19851. *
  19852. @@ -302,6 +303,7 @@ int rcu_read_lock_bh_held(void)
  19853. return in_softirq() || irqs_disabled();
  19854. }
  19855. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  19856. +#endif
  19857. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  19858. diff --git a/kernel/relay.c b/kernel/relay.c
  19859. index 0b4570cfacae..60684be39f22 100644
  19860. --- a/kernel/relay.c
  19861. +++ b/kernel/relay.c
  19862. @@ -336,6 +336,10 @@ static void wakeup_readers(unsigned long data)
  19863. {
  19864. struct rchan_buf *buf = (struct rchan_buf *)data;
  19865. wake_up_interruptible(&buf->read_wait);
  19866. + /*
  19867. + * Stupid polling for now:
  19868. + */
  19869. + mod_timer(&buf->timer, jiffies + 1);
  19870. }
  19871. /**
  19872. @@ -353,6 +357,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  19873. init_waitqueue_head(&buf->read_wait);
  19874. kref_init(&buf->kref);
  19875. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  19876. + mod_timer(&buf->timer, jiffies + 1);
  19877. } else
  19878. del_timer_sync(&buf->timer);
  19879. @@ -736,15 +741,6 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  19880. else
  19881. buf->early_bytes += buf->chan->subbuf_size -
  19882. buf->padding[old_subbuf];
  19883. - smp_mb();
  19884. - if (waitqueue_active(&buf->read_wait))
  19885. - /*
  19886. - * Calling wake_up_interruptible() from here
  19887. - * will deadlock if we happen to be logging
  19888. - * from the scheduler (trying to re-grab
  19889. - * rq->lock), so defer it.
  19890. - */
  19891. - mod_timer(&buf->timer, jiffies + 1);
  19892. }
  19893. old = buf->data;
  19894. diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
  19895. index 67687973ce80..01b9994b367a 100644
  19896. --- a/kernel/sched/Makefile
  19897. +++ b/kernel/sched/Makefile
  19898. @@ -13,7 +13,7 @@ endif
  19899. obj-y += core.o loadavg.o clock.o cputime.o
  19900. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  19901. -obj-y += wait.o completion.o idle.o
  19902. +obj-y += wait.o swait.o swork.o completion.o idle.o
  19903. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  19904. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  19905. obj-$(CONFIG_SCHEDSTATS) += stats.o
  19906. diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
  19907. index 8d0f35debf35..b62cf6400fe0 100644
  19908. --- a/kernel/sched/completion.c
  19909. +++ b/kernel/sched/completion.c
  19910. @@ -30,10 +30,10 @@ void complete(struct completion *x)
  19911. {
  19912. unsigned long flags;
  19913. - spin_lock_irqsave(&x->wait.lock, flags);
  19914. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19915. x->done++;
  19916. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  19917. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19918. + swake_up_locked(&x->wait);
  19919. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19920. }
  19921. EXPORT_SYMBOL(complete);
  19922. @@ -50,10 +50,10 @@ void complete_all(struct completion *x)
  19923. {
  19924. unsigned long flags;
  19925. - spin_lock_irqsave(&x->wait.lock, flags);
  19926. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19927. x->done += UINT_MAX/2;
  19928. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  19929. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19930. + swake_up_all_locked(&x->wait);
  19931. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19932. }
  19933. EXPORT_SYMBOL(complete_all);
  19934. @@ -62,20 +62,20 @@ do_wait_for_common(struct completion *x,
  19935. long (*action)(long), long timeout, int state)
  19936. {
  19937. if (!x->done) {
  19938. - DECLARE_WAITQUEUE(wait, current);
  19939. + DECLARE_SWAITQUEUE(wait);
  19940. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  19941. + __prepare_to_swait(&x->wait, &wait);
  19942. do {
  19943. if (signal_pending_state(state, current)) {
  19944. timeout = -ERESTARTSYS;
  19945. break;
  19946. }
  19947. __set_current_state(state);
  19948. - spin_unlock_irq(&x->wait.lock);
  19949. + raw_spin_unlock_irq(&x->wait.lock);
  19950. timeout = action(timeout);
  19951. - spin_lock_irq(&x->wait.lock);
  19952. + raw_spin_lock_irq(&x->wait.lock);
  19953. } while (!x->done && timeout);
  19954. - __remove_wait_queue(&x->wait, &wait);
  19955. + __finish_swait(&x->wait, &wait);
  19956. if (!x->done)
  19957. return timeout;
  19958. }
  19959. @@ -89,9 +89,9 @@ __wait_for_common(struct completion *x,
  19960. {
  19961. might_sleep();
  19962. - spin_lock_irq(&x->wait.lock);
  19963. + raw_spin_lock_irq(&x->wait.lock);
  19964. timeout = do_wait_for_common(x, action, timeout, state);
  19965. - spin_unlock_irq(&x->wait.lock);
  19966. + raw_spin_unlock_irq(&x->wait.lock);
  19967. return timeout;
  19968. }
  19969. @@ -277,12 +277,12 @@ bool try_wait_for_completion(struct completion *x)
  19970. if (!READ_ONCE(x->done))
  19971. return 0;
  19972. - spin_lock_irqsave(&x->wait.lock, flags);
  19973. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  19974. if (!x->done)
  19975. ret = 0;
  19976. else
  19977. x->done--;
  19978. - spin_unlock_irqrestore(&x->wait.lock, flags);
  19979. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  19980. return ret;
  19981. }
  19982. EXPORT_SYMBOL(try_wait_for_completion);
  19983. @@ -311,7 +311,7 @@ bool completion_done(struct completion *x)
  19984. * after it's acquired the lock.
  19985. */
  19986. smp_rmb();
  19987. - spin_unlock_wait(&x->wait.lock);
  19988. + raw_spin_unlock_wait(&x->wait.lock);
  19989. return true;
  19990. }
  19991. EXPORT_SYMBOL(completion_done);
  19992. diff --git a/kernel/sched/core.c b/kernel/sched/core.c
  19993. index 67d1e1597d9c..42ce49aa3ad5 100644
  19994. --- a/kernel/sched/core.c
  19995. +++ b/kernel/sched/core.c
  19996. @@ -260,7 +260,11 @@ late_initcall(sched_init_debug);
  19997. * Number of tasks to iterate in a single balance run.
  19998. * Limited because this is done with IRQs disabled.
  19999. */
  20000. +#ifndef CONFIG_PREEMPT_RT_FULL
  20001. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  20002. +#else
  20003. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  20004. +#endif
  20005. /*
  20006. * period over which we average the RT time consumption, measured
  20007. @@ -438,6 +442,7 @@ static void init_rq_hrtick(struct rq *rq)
  20008. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20009. rq->hrtick_timer.function = hrtick;
  20010. + rq->hrtick_timer.irqsafe = 1;
  20011. }
  20012. #else /* CONFIG_SCHED_HRTICK */
  20013. static inline void hrtick_clear(struct rq *rq)
  20014. @@ -542,7 +547,7 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  20015. head->lastp = &node->next;
  20016. }
  20017. -void wake_up_q(struct wake_q_head *head)
  20018. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  20019. {
  20020. struct wake_q_node *node = head->first;
  20021. @@ -559,7 +564,10 @@ void wake_up_q(struct wake_q_head *head)
  20022. * wake_up_process() implies a wmb() to pair with the queueing
  20023. * in wake_q_add() so as not to miss wakeups.
  20024. */
  20025. - wake_up_process(task);
  20026. + if (sleeper)
  20027. + wake_up_lock_sleeper(task);
  20028. + else
  20029. + wake_up_process(task);
  20030. put_task_struct(task);
  20031. }
  20032. }
  20033. @@ -595,6 +603,38 @@ void resched_curr(struct rq *rq)
  20034. trace_sched_wake_idle_without_ipi(cpu);
  20035. }
  20036. +#ifdef CONFIG_PREEMPT_LAZY
  20037. +void resched_curr_lazy(struct rq *rq)
  20038. +{
  20039. + struct task_struct *curr = rq->curr;
  20040. + int cpu;
  20041. +
  20042. + if (!sched_feat(PREEMPT_LAZY)) {
  20043. + resched_curr(rq);
  20044. + return;
  20045. + }
  20046. +
  20047. + lockdep_assert_held(&rq->lock);
  20048. +
  20049. + if (test_tsk_need_resched(curr))
  20050. + return;
  20051. +
  20052. + if (test_tsk_need_resched_lazy(curr))
  20053. + return;
  20054. +
  20055. + set_tsk_need_resched_lazy(curr);
  20056. +
  20057. + cpu = cpu_of(rq);
  20058. + if (cpu == smp_processor_id())
  20059. + return;
  20060. +
  20061. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  20062. + smp_mb();
  20063. + if (!tsk_is_polling(curr))
  20064. + smp_send_reschedule(cpu);
  20065. +}
  20066. +#endif
  20067. +
  20068. void resched_cpu(int cpu)
  20069. {
  20070. struct rq *rq = cpu_rq(cpu);
  20071. @@ -618,11 +658,14 @@ void resched_cpu(int cpu)
  20072. */
  20073. int get_nohz_timer_target(void)
  20074. {
  20075. - int i, cpu = smp_processor_id();
  20076. + int i, cpu;
  20077. struct sched_domain *sd;
  20078. + preempt_disable_rt();
  20079. + cpu = smp_processor_id();
  20080. +
  20081. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  20082. - return cpu;
  20083. + goto preempt_en_rt;
  20084. rcu_read_lock();
  20085. for_each_domain(cpu, sd) {
  20086. @@ -638,6 +681,8 @@ int get_nohz_timer_target(void)
  20087. cpu = housekeeping_any_cpu();
  20088. unlock:
  20089. rcu_read_unlock();
  20090. +preempt_en_rt:
  20091. + preempt_enable_rt();
  20092. return cpu;
  20093. }
  20094. /*
  20095. @@ -1171,6 +1216,11 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  20096. lockdep_assert_held(&p->pi_lock);
  20097. + if (__migrate_disabled(p)) {
  20098. + cpumask_copy(&p->cpus_allowed, new_mask);
  20099. + return;
  20100. + }
  20101. +
  20102. queued = task_on_rq_queued(p);
  20103. running = task_current(rq, p);
  20104. @@ -1193,6 +1243,84 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  20105. enqueue_task(rq, p, ENQUEUE_RESTORE);
  20106. }
  20107. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  20108. +static DEFINE_MUTEX(sched_down_mutex);
  20109. +static cpumask_t sched_down_cpumask;
  20110. +
  20111. +void tell_sched_cpu_down_begin(int cpu)
  20112. +{
  20113. + mutex_lock(&sched_down_mutex);
  20114. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  20115. + mutex_unlock(&sched_down_mutex);
  20116. +}
  20117. +
  20118. +void tell_sched_cpu_down_done(int cpu)
  20119. +{
  20120. + mutex_lock(&sched_down_mutex);
  20121. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  20122. + mutex_unlock(&sched_down_mutex);
  20123. +}
  20124. +
  20125. +/**
  20126. + * migrate_me - try to move the current task off this cpu
  20127. + *
  20128. + * Used by the pin_current_cpu() code to try to get tasks
  20129. + * to move off the current CPU as it is going down.
  20130. + * It will only move the task if the task isn't pinned to
  20131. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  20132. + * and the task has to be in a RUNNING state. Otherwise the
  20133. + * movement of the task will wake it up (change its state
  20134. + * to running) when the task did not expect it.
  20135. + *
  20136. + * Returns 1 if it succeeded in moving the current task
  20137. + * 0 otherwise.
  20138. + */
  20139. +int migrate_me(void)
  20140. +{
  20141. + struct task_struct *p = current;
  20142. + struct migration_arg arg;
  20143. + struct cpumask *cpumask;
  20144. + struct cpumask *mask;
  20145. + unsigned long flags;
  20146. + unsigned int dest_cpu;
  20147. + struct rq *rq;
  20148. +
  20149. + /*
  20150. + * We can not migrate tasks bounded to a CPU or tasks not
  20151. + * running. The movement of the task will wake it up.
  20152. + */
  20153. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  20154. + return 0;
  20155. +
  20156. + mutex_lock(&sched_down_mutex);
  20157. + rq = task_rq_lock(p, &flags);
  20158. +
  20159. + cpumask = this_cpu_ptr(&sched_cpumasks);
  20160. + mask = &p->cpus_allowed;
  20161. +
  20162. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  20163. +
  20164. + if (!cpumask_weight(cpumask)) {
  20165. + /* It's only on this CPU? */
  20166. + task_rq_unlock(rq, p, &flags);
  20167. + mutex_unlock(&sched_down_mutex);
  20168. + return 0;
  20169. + }
  20170. +
  20171. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  20172. +
  20173. + arg.task = p;
  20174. + arg.dest_cpu = dest_cpu;
  20175. +
  20176. + task_rq_unlock(rq, p, &flags);
  20177. +
  20178. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  20179. + tlb_migrate_finish(p->mm);
  20180. + mutex_unlock(&sched_down_mutex);
  20181. +
  20182. + return 1;
  20183. +}
  20184. +
  20185. /*
  20186. * Change a given task's CPU affinity. Migrate the thread to a
  20187. * proper CPU and schedule it away if the CPU it's executing on
  20188. @@ -1232,7 +1360,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
  20189. do_set_cpus_allowed(p, new_mask);
  20190. /* Can the task run on the task's current CPU? If so, we're done */
  20191. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  20192. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  20193. goto out;
  20194. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  20195. @@ -1408,6 +1536,18 @@ out:
  20196. return ret;
  20197. }
  20198. +static bool check_task_state(struct task_struct *p, long match_state)
  20199. +{
  20200. + bool match = false;
  20201. +
  20202. + raw_spin_lock_irq(&p->pi_lock);
  20203. + if (p->state == match_state || p->saved_state == match_state)
  20204. + match = true;
  20205. + raw_spin_unlock_irq(&p->pi_lock);
  20206. +
  20207. + return match;
  20208. +}
  20209. +
  20210. /*
  20211. * wait_task_inactive - wait for a thread to unschedule.
  20212. *
  20213. @@ -1452,7 +1592,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  20214. * is actually now running somewhere else!
  20215. */
  20216. while (task_running(rq, p)) {
  20217. - if (match_state && unlikely(p->state != match_state))
  20218. + if (match_state && !check_task_state(p, match_state))
  20219. return 0;
  20220. cpu_relax();
  20221. }
  20222. @@ -1467,7 +1607,8 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  20223. running = task_running(rq, p);
  20224. queued = task_on_rq_queued(p);
  20225. ncsw = 0;
  20226. - if (!match_state || p->state == match_state)
  20227. + if (!match_state || p->state == match_state ||
  20228. + p->saved_state == match_state)
  20229. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  20230. task_rq_unlock(rq, p, &flags);
  20231. @@ -1624,7 +1765,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
  20232. {
  20233. lockdep_assert_held(&p->pi_lock);
  20234. - if (p->nr_cpus_allowed > 1)
  20235. + if (tsk_nr_cpus_allowed(p) > 1)
  20236. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  20237. /*
  20238. @@ -1704,10 +1845,6 @@ static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_fl
  20239. {
  20240. activate_task(rq, p, en_flags);
  20241. p->on_rq = TASK_ON_RQ_QUEUED;
  20242. -
  20243. - /* if a worker is waking up, notify workqueue */
  20244. - if (p->flags & PF_WQ_WORKER)
  20245. - wq_worker_waking_up(p, cpu_of(rq));
  20246. }
  20247. /*
  20248. @@ -1934,8 +2071,27 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  20249. */
  20250. smp_mb__before_spinlock();
  20251. raw_spin_lock_irqsave(&p->pi_lock, flags);
  20252. - if (!(p->state & state))
  20253. + if (!(p->state & state)) {
  20254. + /*
  20255. + * The task might be running due to a spinlock sleeper
  20256. + * wakeup. Check the saved state and set it to running
  20257. + * if the wakeup condition is true.
  20258. + */
  20259. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  20260. + if (p->saved_state & state) {
  20261. + p->saved_state = TASK_RUNNING;
  20262. + success = 1;
  20263. + }
  20264. + }
  20265. goto out;
  20266. + }
  20267. +
  20268. + /*
  20269. + * If this is a regular wakeup, then we can unconditionally
  20270. + * clear the saved state of a "lock sleeper".
  20271. + */
  20272. + if (!(wake_flags & WF_LOCK_SLEEPER))
  20273. + p->saved_state = TASK_RUNNING;
  20274. trace_sched_waking(p);
  20275. @@ -2005,52 +2161,6 @@ out:
  20276. }
  20277. /**
  20278. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  20279. - * @p: the thread to be awakened
  20280. - *
  20281. - * Put @p on the run-queue if it's not already there. The caller must
  20282. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  20283. - * the current task.
  20284. - */
  20285. -static void try_to_wake_up_local(struct task_struct *p)
  20286. -{
  20287. - struct rq *rq = task_rq(p);
  20288. -
  20289. - if (WARN_ON_ONCE(rq != this_rq()) ||
  20290. - WARN_ON_ONCE(p == current))
  20291. - return;
  20292. -
  20293. - lockdep_assert_held(&rq->lock);
  20294. -
  20295. - if (!raw_spin_trylock(&p->pi_lock)) {
  20296. - /*
  20297. - * This is OK, because current is on_cpu, which avoids it being
  20298. - * picked for load-balance and preemption/IRQs are still
  20299. - * disabled avoiding further scheduler activity on it and we've
  20300. - * not yet picked a replacement task.
  20301. - */
  20302. - lockdep_unpin_lock(&rq->lock);
  20303. - raw_spin_unlock(&rq->lock);
  20304. - raw_spin_lock(&p->pi_lock);
  20305. - raw_spin_lock(&rq->lock);
  20306. - lockdep_pin_lock(&rq->lock);
  20307. - }
  20308. -
  20309. - if (!(p->state & TASK_NORMAL))
  20310. - goto out;
  20311. -
  20312. - trace_sched_waking(p);
  20313. -
  20314. - if (!task_on_rq_queued(p))
  20315. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  20316. -
  20317. - ttwu_do_wakeup(rq, p, 0);
  20318. - ttwu_stat(p, smp_processor_id(), 0);
  20319. -out:
  20320. - raw_spin_unlock(&p->pi_lock);
  20321. -}
  20322. -
  20323. -/**
  20324. * wake_up_process - Wake up a specific process
  20325. * @p: The process to be woken up.
  20326. *
  20327. @@ -2068,6 +2178,18 @@ int wake_up_process(struct task_struct *p)
  20328. }
  20329. EXPORT_SYMBOL(wake_up_process);
  20330. +/**
  20331. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  20332. + * @p: The process to be woken up.
  20333. + *
  20334. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  20335. + * the nature of the wakeup.
  20336. + */
  20337. +int wake_up_lock_sleeper(struct task_struct *p)
  20338. +{
  20339. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  20340. +}
  20341. +
  20342. int wake_up_state(struct task_struct *p, unsigned int state)
  20343. {
  20344. return try_to_wake_up(p, state, 0);
  20345. @@ -2254,6 +2376,9 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  20346. p->on_cpu = 0;
  20347. #endif
  20348. init_task_preempt_count(p);
  20349. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20350. + task_thread_info(p)->preempt_lazy_count = 0;
  20351. +#endif
  20352. #ifdef CONFIG_SMP
  20353. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  20354. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  20355. @@ -2578,8 +2703,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)
  20356. finish_arch_post_lock_switch();
  20357. fire_sched_in_preempt_notifiers(current);
  20358. + /*
  20359. + * We use mmdrop_delayed() here so we don't have to do the
  20360. + * full __mmdrop() when we are the last user.
  20361. + */
  20362. if (mm)
  20363. - mmdrop(mm);
  20364. + mmdrop_delayed(mm);
  20365. if (unlikely(prev_state == TASK_DEAD)) {
  20366. if (prev->sched_class->task_dead)
  20367. prev->sched_class->task_dead(prev);
  20368. @@ -2910,16 +3039,6 @@ u64 scheduler_tick_max_deferment(void)
  20369. }
  20370. #endif
  20371. -notrace unsigned long get_parent_ip(unsigned long addr)
  20372. -{
  20373. - if (in_lock_functions(addr)) {
  20374. - addr = CALLER_ADDR2;
  20375. - if (in_lock_functions(addr))
  20376. - addr = CALLER_ADDR3;
  20377. - }
  20378. - return addr;
  20379. -}
  20380. -
  20381. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  20382. defined(CONFIG_PREEMPT_TRACER))
  20383. @@ -2941,7 +3060,7 @@ void preempt_count_add(int val)
  20384. PREEMPT_MASK - 10);
  20385. #endif
  20386. if (preempt_count() == val) {
  20387. - unsigned long ip = get_parent_ip(CALLER_ADDR1);
  20388. + unsigned long ip = get_lock_parent_ip();
  20389. #ifdef CONFIG_DEBUG_PREEMPT
  20390. current->preempt_disable_ip = ip;
  20391. #endif
  20392. @@ -2968,7 +3087,7 @@ void preempt_count_sub(int val)
  20393. #endif
  20394. if (preempt_count() == val)
  20395. - trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  20396. + trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  20397. __preempt_count_sub(val);
  20398. }
  20399. EXPORT_SYMBOL(preempt_count_sub);
  20400. @@ -3023,6 +3142,77 @@ static inline void schedule_debug(struct task_struct *prev)
  20401. schedstat_inc(this_rq(), sched_count);
  20402. }
  20403. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  20404. +
  20405. +void migrate_disable(void)
  20406. +{
  20407. + struct task_struct *p = current;
  20408. +
  20409. + if (in_atomic() || irqs_disabled()) {
  20410. +#ifdef CONFIG_SCHED_DEBUG
  20411. + p->migrate_disable_atomic++;
  20412. +#endif
  20413. + return;
  20414. + }
  20415. +
  20416. +#ifdef CONFIG_SCHED_DEBUG
  20417. + if (unlikely(p->migrate_disable_atomic)) {
  20418. + tracing_off();
  20419. + WARN_ON_ONCE(1);
  20420. + }
  20421. +#endif
  20422. +
  20423. + if (p->migrate_disable) {
  20424. + p->migrate_disable++;
  20425. + return;
  20426. + }
  20427. +
  20428. + preempt_disable();
  20429. + preempt_lazy_disable();
  20430. + pin_current_cpu();
  20431. + p->migrate_disable = 1;
  20432. + preempt_enable();
  20433. +}
  20434. +EXPORT_SYMBOL(migrate_disable);
  20435. +
  20436. +void migrate_enable(void)
  20437. +{
  20438. + struct task_struct *p = current;
  20439. +
  20440. + if (in_atomic() || irqs_disabled()) {
  20441. +#ifdef CONFIG_SCHED_DEBUG
  20442. + p->migrate_disable_atomic--;
  20443. +#endif
  20444. + return;
  20445. + }
  20446. +
  20447. +#ifdef CONFIG_SCHED_DEBUG
  20448. + if (unlikely(p->migrate_disable_atomic)) {
  20449. + tracing_off();
  20450. + WARN_ON_ONCE(1);
  20451. + }
  20452. +#endif
  20453. + WARN_ON_ONCE(p->migrate_disable <= 0);
  20454. +
  20455. + if (p->migrate_disable > 1) {
  20456. + p->migrate_disable--;
  20457. + return;
  20458. + }
  20459. +
  20460. + preempt_disable();
  20461. + /*
  20462. + * Clearing migrate_disable causes tsk_cpus_allowed to
  20463. + * show the tasks original cpu affinity.
  20464. + */
  20465. + p->migrate_disable = 0;
  20466. +
  20467. + unpin_current_cpu();
  20468. + preempt_enable();
  20469. + preempt_lazy_enable();
  20470. +}
  20471. +EXPORT_SYMBOL(migrate_enable);
  20472. +#endif
  20473. +
  20474. /*
  20475. * Pick up the highest-prio task:
  20476. */
  20477. @@ -3147,19 +3337,6 @@ static void __sched notrace __schedule(bool preempt)
  20478. } else {
  20479. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  20480. prev->on_rq = 0;
  20481. -
  20482. - /*
  20483. - * If a worker went to sleep, notify and ask workqueue
  20484. - * whether it wants to wake up a task to maintain
  20485. - * concurrency.
  20486. - */
  20487. - if (prev->flags & PF_WQ_WORKER) {
  20488. - struct task_struct *to_wakeup;
  20489. -
  20490. - to_wakeup = wq_worker_sleeping(prev, cpu);
  20491. - if (to_wakeup)
  20492. - try_to_wake_up_local(to_wakeup);
  20493. - }
  20494. }
  20495. switch_count = &prev->nvcsw;
  20496. }
  20497. @@ -3169,6 +3346,7 @@ static void __sched notrace __schedule(bool preempt)
  20498. next = pick_next_task(rq, prev);
  20499. clear_tsk_need_resched(prev);
  20500. + clear_tsk_need_resched_lazy(prev);
  20501. clear_preempt_need_resched();
  20502. rq->clock_skip_update = 0;
  20503. @@ -3190,9 +3368,20 @@ static void __sched notrace __schedule(bool preempt)
  20504. static inline void sched_submit_work(struct task_struct *tsk)
  20505. {
  20506. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  20507. + if (!tsk->state)
  20508. return;
  20509. /*
  20510. + * If a worker went to sleep, notify and ask workqueue whether
  20511. + * it wants to wake up a task to maintain concurrency.
  20512. + */
  20513. + if (tsk->flags & PF_WQ_WORKER)
  20514. + wq_worker_sleeping(tsk);
  20515. +
  20516. +
  20517. + if (tsk_is_pi_blocked(tsk))
  20518. + return;
  20519. +
  20520. + /*
  20521. * If we are going to sleep and we have plugged IO queued,
  20522. * make sure to submit it to avoid deadlocks.
  20523. */
  20524. @@ -3200,6 +3389,12 @@ static inline void sched_submit_work(struct task_struct *tsk)
  20525. blk_schedule_flush_plug(tsk);
  20526. }
  20527. +static void sched_update_worker(struct task_struct *tsk)
  20528. +{
  20529. + if (tsk->flags & PF_WQ_WORKER)
  20530. + wq_worker_running(tsk);
  20531. +}
  20532. +
  20533. asmlinkage __visible void __sched schedule(void)
  20534. {
  20535. struct task_struct *tsk = current;
  20536. @@ -3210,6 +3405,7 @@ asmlinkage __visible void __sched schedule(void)
  20537. __schedule(false);
  20538. sched_preempt_enable_no_resched();
  20539. } while (need_resched());
  20540. + sched_update_worker(tsk);
  20541. }
  20542. EXPORT_SYMBOL(schedule);
  20543. @@ -3258,6 +3454,30 @@ static void __sched notrace preempt_schedule_common(void)
  20544. } while (need_resched());
  20545. }
  20546. +#ifdef CONFIG_PREEMPT_LAZY
  20547. +/*
  20548. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  20549. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  20550. + * preempt_lazy_count counter >0.
  20551. + */
  20552. +static __always_inline int preemptible_lazy(void)
  20553. +{
  20554. + if (test_thread_flag(TIF_NEED_RESCHED))
  20555. + return 1;
  20556. + if (current_thread_info()->preempt_lazy_count)
  20557. + return 0;
  20558. + return 1;
  20559. +}
  20560. +
  20561. +#else
  20562. +
  20563. +static int preemptible_lazy(void)
  20564. +{
  20565. + return 1;
  20566. +}
  20567. +
  20568. +#endif
  20569. +
  20570. #ifdef CONFIG_PREEMPT
  20571. /*
  20572. * this is the entry point to schedule() from in-kernel preemption
  20573. @@ -3272,6 +3492,8 @@ asmlinkage __visible void __sched notrace preempt_schedule(void)
  20574. */
  20575. if (likely(!preemptible()))
  20576. return;
  20577. + if (!preemptible_lazy())
  20578. + return;
  20579. preempt_schedule_common();
  20580. }
  20581. @@ -3298,6 +3520,8 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  20582. if (likely(!preemptible()))
  20583. return;
  20584. + if (!preemptible_lazy())
  20585. + return;
  20586. do {
  20587. preempt_disable_notrace();
  20588. @@ -3307,7 +3531,16 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  20589. * an infinite recursion.
  20590. */
  20591. prev_ctx = exception_enter();
  20592. + /*
  20593. + * The add/subtract must not be traced by the function
  20594. + * tracer. But we still want to account for the
  20595. + * preempt off latency tracer. Since the _notrace versions
  20596. + * of add/subtract skip the accounting for latency tracer
  20597. + * we must force it manually.
  20598. + */
  20599. + start_critical_timings();
  20600. __schedule(true);
  20601. + stop_critical_timings();
  20602. exception_exit(prev_ctx);
  20603. preempt_enable_no_resched_notrace();
  20604. @@ -4651,6 +4884,7 @@ int __cond_resched_lock(spinlock_t *lock)
  20605. }
  20606. EXPORT_SYMBOL(__cond_resched_lock);
  20607. +#ifndef CONFIG_PREEMPT_RT_FULL
  20608. int __sched __cond_resched_softirq(void)
  20609. {
  20610. BUG_ON(!in_softirq());
  20611. @@ -4664,6 +4898,7 @@ int __sched __cond_resched_softirq(void)
  20612. return 0;
  20613. }
  20614. EXPORT_SYMBOL(__cond_resched_softirq);
  20615. +#endif
  20616. /**
  20617. * yield - yield the current processor to other threads.
  20618. @@ -5030,7 +5265,9 @@ void init_idle(struct task_struct *idle, int cpu)
  20619. /* Set the preempt count _outside_ the spinlocks! */
  20620. init_idle_preempt_count(idle, cpu);
  20621. -
  20622. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  20623. + task_thread_info(idle)->preempt_lazy_count = 0;
  20624. +#endif
  20625. /*
  20626. * The idle tasks have their own, simple scheduling class:
  20627. */
  20628. @@ -5171,6 +5408,8 @@ void sched_setnuma(struct task_struct *p, int nid)
  20629. #endif /* CONFIG_NUMA_BALANCING */
  20630. #ifdef CONFIG_HOTPLUG_CPU
  20631. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  20632. +
  20633. /*
  20634. * Ensures that the idle task is using init_mm right before its cpu goes
  20635. * offline.
  20636. @@ -5185,7 +5424,11 @@ void idle_task_exit(void)
  20637. switch_mm(mm, &init_mm, current);
  20638. finish_arch_post_lock_switch();
  20639. }
  20640. - mmdrop(mm);
  20641. + /*
  20642. + * Defer the cleanup to an alive cpu. On RT we can neither
  20643. + * call mmdrop() nor mmdrop_delayed() from here.
  20644. + */
  20645. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  20646. }
  20647. /*
  20648. @@ -5558,6 +5801,10 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  20649. case CPU_DEAD:
  20650. calc_load_migrate(rq);
  20651. + if (per_cpu(idle_last_mm, cpu)) {
  20652. + mmdrop(per_cpu(idle_last_mm, cpu));
  20653. + per_cpu(idle_last_mm, cpu) = NULL;
  20654. + }
  20655. break;
  20656. #endif
  20657. }
  20658. @@ -7541,7 +7788,7 @@ void __init sched_init(void)
  20659. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  20660. static inline int preempt_count_equals(int preempt_offset)
  20661. {
  20662. - int nested = preempt_count() + rcu_preempt_depth();
  20663. + int nested = preempt_count() + sched_rcu_preempt_depth();
  20664. return (nested == preempt_offset);
  20665. }
  20666. diff --git a/kernel/sched/cpudeadline.c b/kernel/sched/cpudeadline.c
  20667. index 5a75b08cfd85..5be58820465c 100644
  20668. --- a/kernel/sched/cpudeadline.c
  20669. +++ b/kernel/sched/cpudeadline.c
  20670. @@ -103,10 +103,10 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p,
  20671. const struct sched_dl_entity *dl_se = &p->dl;
  20672. if (later_mask &&
  20673. - cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) {
  20674. + cpumask_and(later_mask, cp->free_cpus, tsk_cpus_allowed(p))) {
  20675. best_cpu = cpumask_any(later_mask);
  20676. goto out;
  20677. - } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  20678. + } else if (cpumask_test_cpu(cpudl_maximum(cp), tsk_cpus_allowed(p)) &&
  20679. dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  20680. best_cpu = cpudl_maximum(cp);
  20681. if (later_mask)
  20682. diff --git a/kernel/sched/cpupri.c b/kernel/sched/cpupri.c
  20683. index 981fcd7dc394..11e9705bf937 100644
  20684. --- a/kernel/sched/cpupri.c
  20685. +++ b/kernel/sched/cpupri.c
  20686. @@ -103,11 +103,11 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
  20687. if (skip)
  20688. continue;
  20689. - if (cpumask_any_and(&p->cpus_allowed, vec->mask) >= nr_cpu_ids)
  20690. + if (cpumask_any_and(tsk_cpus_allowed(p), vec->mask) >= nr_cpu_ids)
  20691. continue;
  20692. if (lowest_mask) {
  20693. - cpumask_and(lowest_mask, &p->cpus_allowed, vec->mask);
  20694. + cpumask_and(lowest_mask, tsk_cpus_allowed(p), vec->mask);
  20695. /*
  20696. * We have to ensure that we have at least one bit
  20697. diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
  20698. index f74ea89e77a8..4611b1c1cb12 100644
  20699. --- a/kernel/sched/cputime.c
  20700. +++ b/kernel/sched/cputime.c
  20701. @@ -680,7 +680,7 @@ static cputime_t get_vtime_delta(struct task_struct *tsk)
  20702. {
  20703. unsigned long long delta = vtime_delta(tsk);
  20704. - WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_SLEEPING);
  20705. + WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
  20706. tsk->vtime_snap += delta;
  20707. /* CHECKME: always safe to convert nsecs to cputime? */
  20708. @@ -696,37 +696,37 @@ static void __vtime_account_system(struct task_struct *tsk)
  20709. void vtime_account_system(struct task_struct *tsk)
  20710. {
  20711. - write_seqlock(&tsk->vtime_seqlock);
  20712. + write_seqcount_begin(&tsk->vtime_seqcount);
  20713. __vtime_account_system(tsk);
  20714. - write_sequnlock(&tsk->vtime_seqlock);
  20715. + write_seqcount_end(&tsk->vtime_seqcount);
  20716. }
  20717. void vtime_gen_account_irq_exit(struct task_struct *tsk)
  20718. {
  20719. - write_seqlock(&tsk->vtime_seqlock);
  20720. + write_seqcount_begin(&tsk->vtime_seqcount);
  20721. __vtime_account_system(tsk);
  20722. if (context_tracking_in_user())
  20723. tsk->vtime_snap_whence = VTIME_USER;
  20724. - write_sequnlock(&tsk->vtime_seqlock);
  20725. + write_seqcount_end(&tsk->vtime_seqcount);
  20726. }
  20727. void vtime_account_user(struct task_struct *tsk)
  20728. {
  20729. cputime_t delta_cpu;
  20730. - write_seqlock(&tsk->vtime_seqlock);
  20731. + write_seqcount_begin(&tsk->vtime_seqcount);
  20732. delta_cpu = get_vtime_delta(tsk);
  20733. tsk->vtime_snap_whence = VTIME_SYS;
  20734. account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
  20735. - write_sequnlock(&tsk->vtime_seqlock);
  20736. + write_seqcount_end(&tsk->vtime_seqcount);
  20737. }
  20738. void vtime_user_enter(struct task_struct *tsk)
  20739. {
  20740. - write_seqlock(&tsk->vtime_seqlock);
  20741. + write_seqcount_begin(&tsk->vtime_seqcount);
  20742. __vtime_account_system(tsk);
  20743. tsk->vtime_snap_whence = VTIME_USER;
  20744. - write_sequnlock(&tsk->vtime_seqlock);
  20745. + write_seqcount_end(&tsk->vtime_seqcount);
  20746. }
  20747. void vtime_guest_enter(struct task_struct *tsk)
  20748. @@ -738,19 +738,19 @@ void vtime_guest_enter(struct task_struct *tsk)
  20749. * synchronization against the reader (task_gtime())
  20750. * that can thus safely catch up with a tickless delta.
  20751. */
  20752. - write_seqlock(&tsk->vtime_seqlock);
  20753. + write_seqcount_begin(&tsk->vtime_seqcount);
  20754. __vtime_account_system(tsk);
  20755. current->flags |= PF_VCPU;
  20756. - write_sequnlock(&tsk->vtime_seqlock);
  20757. + write_seqcount_end(&tsk->vtime_seqcount);
  20758. }
  20759. EXPORT_SYMBOL_GPL(vtime_guest_enter);
  20760. void vtime_guest_exit(struct task_struct *tsk)
  20761. {
  20762. - write_seqlock(&tsk->vtime_seqlock);
  20763. + write_seqcount_begin(&tsk->vtime_seqcount);
  20764. __vtime_account_system(tsk);
  20765. current->flags &= ~PF_VCPU;
  20766. - write_sequnlock(&tsk->vtime_seqlock);
  20767. + write_seqcount_end(&tsk->vtime_seqcount);
  20768. }
  20769. EXPORT_SYMBOL_GPL(vtime_guest_exit);
  20770. @@ -763,24 +763,26 @@ void vtime_account_idle(struct task_struct *tsk)
  20771. void arch_vtime_task_switch(struct task_struct *prev)
  20772. {
  20773. - write_seqlock(&prev->vtime_seqlock);
  20774. - prev->vtime_snap_whence = VTIME_SLEEPING;
  20775. - write_sequnlock(&prev->vtime_seqlock);
  20776. + write_seqcount_begin(&prev->vtime_seqcount);
  20777. + prev->vtime_snap_whence = VTIME_INACTIVE;
  20778. + write_seqcount_end(&prev->vtime_seqcount);
  20779. - write_seqlock(&current->vtime_seqlock);
  20780. + write_seqcount_begin(&current->vtime_seqcount);
  20781. current->vtime_snap_whence = VTIME_SYS;
  20782. current->vtime_snap = sched_clock_cpu(smp_processor_id());
  20783. - write_sequnlock(&current->vtime_seqlock);
  20784. + write_seqcount_end(&current->vtime_seqcount);
  20785. }
  20786. void vtime_init_idle(struct task_struct *t, int cpu)
  20787. {
  20788. unsigned long flags;
  20789. - write_seqlock_irqsave(&t->vtime_seqlock, flags);
  20790. + local_irq_save(flags);
  20791. + write_seqcount_begin(&t->vtime_seqcount);
  20792. t->vtime_snap_whence = VTIME_SYS;
  20793. t->vtime_snap = sched_clock_cpu(cpu);
  20794. - write_sequnlock_irqrestore(&t->vtime_seqlock, flags);
  20795. + write_seqcount_end(&t->vtime_seqcount);
  20796. + local_irq_restore(flags);
  20797. }
  20798. cputime_t task_gtime(struct task_struct *t)
  20799. @@ -792,13 +794,13 @@ cputime_t task_gtime(struct task_struct *t)
  20800. return t->gtime;
  20801. do {
  20802. - seq = read_seqbegin(&t->vtime_seqlock);
  20803. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20804. gtime = t->gtime;
  20805. if (t->flags & PF_VCPU)
  20806. gtime += vtime_delta(t);
  20807. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20808. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20809. return gtime;
  20810. }
  20811. @@ -821,7 +823,7 @@ fetch_task_cputime(struct task_struct *t,
  20812. *udelta = 0;
  20813. *sdelta = 0;
  20814. - seq = read_seqbegin(&t->vtime_seqlock);
  20815. + seq = read_seqcount_begin(&t->vtime_seqcount);
  20816. if (u_dst)
  20817. *u_dst = *u_src;
  20818. @@ -829,7 +831,7 @@ fetch_task_cputime(struct task_struct *t,
  20819. *s_dst = *s_src;
  20820. /* Task is sleeping, nothing to add */
  20821. - if (t->vtime_snap_whence == VTIME_SLEEPING ||
  20822. + if (t->vtime_snap_whence == VTIME_INACTIVE ||
  20823. is_idle_task(t))
  20824. continue;
  20825. @@ -845,7 +847,7 @@ fetch_task_cputime(struct task_struct *t,
  20826. if (t->vtime_snap_whence == VTIME_SYS)
  20827. *sdelta = delta;
  20828. }
  20829. - } while (read_seqretry(&t->vtime_seqlock, seq));
  20830. + } while (read_seqcount_retry(&t->vtime_seqcount, seq));
  20831. }
  20832. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
  20833. index 8b0a15e285f9..7a72e69fcf65 100644
  20834. --- a/kernel/sched/deadline.c
  20835. +++ b/kernel/sched/deadline.c
  20836. @@ -134,7 +134,7 @@ static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  20837. {
  20838. struct task_struct *p = dl_task_of(dl_se);
  20839. - if (p->nr_cpus_allowed > 1)
  20840. + if (tsk_nr_cpus_allowed(p) > 1)
  20841. dl_rq->dl_nr_migratory++;
  20842. update_dl_migration(dl_rq);
  20843. @@ -144,7 +144,7 @@ static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  20844. {
  20845. struct task_struct *p = dl_task_of(dl_se);
  20846. - if (p->nr_cpus_allowed > 1)
  20847. + if (tsk_nr_cpus_allowed(p) > 1)
  20848. dl_rq->dl_nr_migratory--;
  20849. update_dl_migration(dl_rq);
  20850. @@ -697,6 +697,7 @@ void init_dl_task_timer(struct sched_dl_entity *dl_se)
  20851. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20852. timer->function = dl_task_timer;
  20853. + timer->irqsafe = 1;
  20854. }
  20855. static
  20856. @@ -989,7 +990,7 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  20857. enqueue_dl_entity(&p->dl, pi_se, flags);
  20858. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  20859. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  20860. enqueue_pushable_dl_task(rq, p);
  20861. }
  20862. @@ -1067,9 +1068,9 @@ select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  20863. * try to make it stay here, it might be important.
  20864. */
  20865. if (unlikely(dl_task(curr)) &&
  20866. - (curr->nr_cpus_allowed < 2 ||
  20867. + (tsk_nr_cpus_allowed(curr) < 2 ||
  20868. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  20869. - (p->nr_cpus_allowed > 1)) {
  20870. + (tsk_nr_cpus_allowed(p) > 1)) {
  20871. int target = find_later_rq(p);
  20872. if (target != -1 &&
  20873. @@ -1090,7 +1091,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  20874. * Current can't be migrated, useless to reschedule,
  20875. * let's hope p can move out.
  20876. */
  20877. - if (rq->curr->nr_cpus_allowed == 1 ||
  20878. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  20879. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  20880. return;
  20881. @@ -1098,7 +1099,7 @@ static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  20882. * p is migratable, so let's not schedule it and
  20883. * see if it is pushed or pulled somewhere else.
  20884. */
  20885. - if (p->nr_cpus_allowed != 1 &&
  20886. + if (tsk_nr_cpus_allowed(p) != 1 &&
  20887. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  20888. return;
  20889. @@ -1212,7 +1213,7 @@ static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  20890. {
  20891. update_curr_dl(rq);
  20892. - if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  20893. + if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  20894. enqueue_pushable_dl_task(rq, p);
  20895. }
  20896. @@ -1335,7 +1336,7 @@ static int find_later_rq(struct task_struct *task)
  20897. if (unlikely(!later_mask))
  20898. return -1;
  20899. - if (task->nr_cpus_allowed == 1)
  20900. + if (tsk_nr_cpus_allowed(task) == 1)
  20901. return -1;
  20902. /*
  20903. @@ -1441,7 +1442,7 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  20904. if (double_lock_balance(rq, later_rq)) {
  20905. if (unlikely(task_rq(task) != rq ||
  20906. !cpumask_test_cpu(later_rq->cpu,
  20907. - &task->cpus_allowed) ||
  20908. + tsk_cpus_allowed(task)) ||
  20909. task_running(rq, task) ||
  20910. !task_on_rq_queued(task))) {
  20911. double_unlock_balance(rq, later_rq);
  20912. @@ -1480,7 +1481,7 @@ static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  20913. BUG_ON(rq->cpu != task_cpu(p));
  20914. BUG_ON(task_current(rq, p));
  20915. - BUG_ON(p->nr_cpus_allowed <= 1);
  20916. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  20917. BUG_ON(!task_on_rq_queued(p));
  20918. BUG_ON(!dl_task(p));
  20919. @@ -1519,7 +1520,7 @@ retry:
  20920. */
  20921. if (dl_task(rq->curr) &&
  20922. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  20923. - rq->curr->nr_cpus_allowed > 1) {
  20924. + tsk_nr_cpus_allowed(rq->curr) > 1) {
  20925. resched_curr(rq);
  20926. return 0;
  20927. }
  20928. @@ -1666,9 +1667,9 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
  20929. {
  20930. if (!task_running(rq, p) &&
  20931. !test_tsk_need_resched(rq->curr) &&
  20932. - p->nr_cpus_allowed > 1 &&
  20933. + tsk_nr_cpus_allowed(p) > 1 &&
  20934. dl_task(rq->curr) &&
  20935. - (rq->curr->nr_cpus_allowed < 2 ||
  20936. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  20937. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  20938. push_dl_tasks(rq);
  20939. }
  20940. @@ -1769,7 +1770,7 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
  20941. {
  20942. if (task_on_rq_queued(p) && rq->curr != p) {
  20943. #ifdef CONFIG_SMP
  20944. - if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  20945. + if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  20946. queue_push_tasks(rq);
  20947. #else
  20948. if (dl_task(rq->curr))
  20949. diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
  20950. index 641511771ae6..a2d69b883623 100644
  20951. --- a/kernel/sched/debug.c
  20952. +++ b/kernel/sched/debug.c
  20953. @@ -251,6 +251,9 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  20954. P(rt_throttled);
  20955. PN(rt_time);
  20956. PN(rt_runtime);
  20957. +#ifdef CONFIG_SMP
  20958. + P(rt_nr_migratory);
  20959. +#endif
  20960. #undef PN
  20961. #undef P
  20962. @@ -635,6 +638,10 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  20963. #endif
  20964. P(policy);
  20965. P(prio);
  20966. +#ifdef CONFIG_PREEMPT_RT_FULL
  20967. + P(migrate_disable);
  20968. +#endif
  20969. + P(nr_cpus_allowed);
  20970. #undef PN
  20971. #undef __PN
  20972. #undef P
  20973. diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
  20974. index b8b516c37bf1..5e8dcb4615f4 100644
  20975. --- a/kernel/sched/fair.c
  20976. +++ b/kernel/sched/fair.c
  20977. @@ -3150,7 +3150,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  20978. ideal_runtime = sched_slice(cfs_rq, curr);
  20979. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  20980. if (delta_exec > ideal_runtime) {
  20981. - resched_curr(rq_of(cfs_rq));
  20982. + resched_curr_lazy(rq_of(cfs_rq));
  20983. /*
  20984. * The current task ran long enough, ensure it doesn't get
  20985. * re-elected due to buddy favours.
  20986. @@ -3174,7 +3174,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  20987. return;
  20988. if (delta > ideal_runtime)
  20989. - resched_curr(rq_of(cfs_rq));
  20990. + resched_curr_lazy(rq_of(cfs_rq));
  20991. }
  20992. static void
  20993. @@ -3314,7 +3314,7 @@ entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  20994. * validating it and just reschedule.
  20995. */
  20996. if (queued) {
  20997. - resched_curr(rq_of(cfs_rq));
  20998. + resched_curr_lazy(rq_of(cfs_rq));
  20999. return;
  21000. }
  21001. /*
  21002. @@ -3496,7 +3496,7 @@ static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
  21003. * hierarchy can be throttled
  21004. */
  21005. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  21006. - resched_curr(rq_of(cfs_rq));
  21007. + resched_curr_lazy(rq_of(cfs_rq));
  21008. }
  21009. static __always_inline
  21010. @@ -4108,7 +4108,7 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  21011. if (delta < 0) {
  21012. if (rq->curr == p)
  21013. - resched_curr(rq);
  21014. + resched_curr_lazy(rq);
  21015. return;
  21016. }
  21017. hrtick_start(rq, delta);
  21018. @@ -5197,7 +5197,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
  21019. return;
  21020. preempt:
  21021. - resched_curr(rq);
  21022. + resched_curr_lazy(rq);
  21023. /*
  21024. * Only set the backward buddy when the current task is still
  21025. * on the rq. This can happen when a wakeup gets interleaved
  21026. @@ -7948,7 +7948,7 @@ static void task_fork_fair(struct task_struct *p)
  21027. * 'current' within the tree based on its new key value.
  21028. */
  21029. swap(curr->vruntime, se->vruntime);
  21030. - resched_curr(rq);
  21031. + resched_curr_lazy(rq);
  21032. }
  21033. se->vruntime -= cfs_rq->min_vruntime;
  21034. @@ -7973,7 +7973,7 @@ prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  21035. */
  21036. if (rq->curr == p) {
  21037. if (p->prio > oldprio)
  21038. - resched_curr(rq);
  21039. + resched_curr_lazy(rq);
  21040. } else
  21041. check_preempt_curr(rq, p, 0);
  21042. }
  21043. diff --git a/kernel/sched/features.h b/kernel/sched/features.h
  21044. index 69631fa46c2f..6d28fcd08872 100644
  21045. --- a/kernel/sched/features.h
  21046. +++ b/kernel/sched/features.h
  21047. @@ -45,11 +45,19 @@ SCHED_FEAT(LB_BIAS, true)
  21048. */
  21049. SCHED_FEAT(NONTASK_CAPACITY, true)
  21050. +#ifdef CONFIG_PREEMPT_RT_FULL
  21051. +SCHED_FEAT(TTWU_QUEUE, false)
  21052. +# ifdef CONFIG_PREEMPT_LAZY
  21053. +SCHED_FEAT(PREEMPT_LAZY, true)
  21054. +# endif
  21055. +#else
  21056. +
  21057. /*
  21058. * Queue remote wakeups on the target CPU and process them
  21059. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  21060. */
  21061. SCHED_FEAT(TTWU_QUEUE, true)
  21062. +#endif
  21063. #ifdef HAVE_RT_PUSH_IPI
  21064. /*
  21065. diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
  21066. index 8ec86abe0ea1..8cf360d309ec 100644
  21067. --- a/kernel/sched/rt.c
  21068. +++ b/kernel/sched/rt.c
  21069. @@ -47,6 +47,7 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  21070. hrtimer_init(&rt_b->rt_period_timer,
  21071. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  21072. + rt_b->rt_period_timer.irqsafe = 1;
  21073. rt_b->rt_period_timer.function = sched_rt_period_timer;
  21074. }
  21075. @@ -93,6 +94,7 @@ void init_rt_rq(struct rt_rq *rt_rq)
  21076. rt_rq->push_cpu = nr_cpu_ids;
  21077. raw_spin_lock_init(&rt_rq->push_lock);
  21078. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  21079. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  21080. #endif
  21081. #endif /* CONFIG_SMP */
  21082. /* We start is dequeued state, because no RT tasks are queued */
  21083. @@ -326,7 +328,7 @@ static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  21084. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  21085. rt_rq->rt_nr_total++;
  21086. - if (p->nr_cpus_allowed > 1)
  21087. + if (tsk_nr_cpus_allowed(p) > 1)
  21088. rt_rq->rt_nr_migratory++;
  21089. update_rt_migration(rt_rq);
  21090. @@ -343,7 +345,7 @@ static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  21091. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  21092. rt_rq->rt_nr_total--;
  21093. - if (p->nr_cpus_allowed > 1)
  21094. + if (tsk_nr_cpus_allowed(p) > 1)
  21095. rt_rq->rt_nr_migratory--;
  21096. update_rt_migration(rt_rq);
  21097. @@ -1262,7 +1264,7 @@ enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  21098. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  21099. - if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  21100. + if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  21101. enqueue_pushable_task(rq, p);
  21102. }
  21103. @@ -1351,7 +1353,7 @@ select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  21104. * will have to sort it out.
  21105. */
  21106. if (curr && unlikely(rt_task(curr)) &&
  21107. - (curr->nr_cpus_allowed < 2 ||
  21108. + (tsk_nr_cpus_allowed(curr) < 2 ||
  21109. curr->prio <= p->prio)) {
  21110. int target = find_lowest_rq(p);
  21111. @@ -1375,7 +1377,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  21112. * Current can't be migrated, useless to reschedule,
  21113. * let's hope p can move out.
  21114. */
  21115. - if (rq->curr->nr_cpus_allowed == 1 ||
  21116. + if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  21117. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  21118. return;
  21119. @@ -1383,7 +1385,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  21120. * p is migratable, so let's not schedule it and
  21121. * see if it is pushed or pulled somewhere else.
  21122. */
  21123. - if (p->nr_cpus_allowed != 1
  21124. + if (tsk_nr_cpus_allowed(p) != 1
  21125. && cpupri_find(&rq->rd->cpupri, p, NULL))
  21126. return;
  21127. @@ -1517,7 +1519,7 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  21128. * The previous task needs to be made eligible for pushing
  21129. * if it is still active
  21130. */
  21131. - if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  21132. + if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1)
  21133. enqueue_pushable_task(rq, p);
  21134. }
  21135. @@ -1567,7 +1569,7 @@ static int find_lowest_rq(struct task_struct *task)
  21136. if (unlikely(!lowest_mask))
  21137. return -1;
  21138. - if (task->nr_cpus_allowed == 1)
  21139. + if (tsk_nr_cpus_allowed(task) == 1)
  21140. return -1; /* No other targets possible */
  21141. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  21142. @@ -1699,7 +1701,7 @@ static struct task_struct *pick_next_pushable_task(struct rq *rq)
  21143. BUG_ON(rq->cpu != task_cpu(p));
  21144. BUG_ON(task_current(rq, p));
  21145. - BUG_ON(p->nr_cpus_allowed <= 1);
  21146. + BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  21147. BUG_ON(!task_on_rq_queued(p));
  21148. BUG_ON(!rt_task(p));
  21149. @@ -2059,9 +2061,9 @@ static void task_woken_rt(struct rq *rq, struct task_struct *p)
  21150. {
  21151. if (!task_running(rq, p) &&
  21152. !test_tsk_need_resched(rq->curr) &&
  21153. - p->nr_cpus_allowed > 1 &&
  21154. + tsk_nr_cpus_allowed(p) > 1 &&
  21155. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  21156. - (rq->curr->nr_cpus_allowed < 2 ||
  21157. + (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  21158. rq->curr->prio <= p->prio))
  21159. push_rt_tasks(rq);
  21160. }
  21161. @@ -2134,7 +2136,7 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)
  21162. */
  21163. if (task_on_rq_queued(p) && rq->curr != p) {
  21164. #ifdef CONFIG_SMP
  21165. - if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  21166. + if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded)
  21167. queue_push_tasks(rq);
  21168. #else
  21169. if (p->prio < rq->curr->prio)
  21170. diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
  21171. index 0517abd7dd73..a8a9b156ea15 100644
  21172. --- a/kernel/sched/sched.h
  21173. +++ b/kernel/sched/sched.h
  21174. @@ -1100,6 +1100,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  21175. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  21176. #define WF_FORK 0x02 /* child wakeup after fork */
  21177. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  21178. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  21179. /*
  21180. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  21181. @@ -1299,6 +1300,15 @@ extern void init_sched_fair_class(void);
  21182. extern void resched_curr(struct rq *rq);
  21183. extern void resched_cpu(int cpu);
  21184. +#ifdef CONFIG_PREEMPT_LAZY
  21185. +extern void resched_curr_lazy(struct rq *rq);
  21186. +#else
  21187. +static inline void resched_curr_lazy(struct rq *rq)
  21188. +{
  21189. + resched_curr(rq);
  21190. +}
  21191. +#endif
  21192. +
  21193. extern struct rt_bandwidth def_rt_bandwidth;
  21194. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  21195. diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
  21196. new file mode 100644
  21197. index 000000000000..205fe36868f9
  21198. --- /dev/null
  21199. +++ b/kernel/sched/swait.c
  21200. @@ -0,0 +1,143 @@
  21201. +#include <linux/sched.h>
  21202. +#include <linux/swait.h>
  21203. +#include <linux/suspend.h>
  21204. +
  21205. +void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  21206. + struct lock_class_key *key)
  21207. +{
  21208. + raw_spin_lock_init(&q->lock);
  21209. + lockdep_set_class_and_name(&q->lock, key, name);
  21210. + INIT_LIST_HEAD(&q->task_list);
  21211. +}
  21212. +EXPORT_SYMBOL(__init_swait_queue_head);
  21213. +
  21214. +/*
  21215. + * The thing about the wake_up_state() return value; I think we can ignore it.
  21216. + *
  21217. + * If for some reason it would return 0, that means the previously waiting
  21218. + * task is already running, so it will observe condition true (or has already).
  21219. + */
  21220. +void swake_up_locked(struct swait_queue_head *q)
  21221. +{
  21222. + struct swait_queue *curr;
  21223. +
  21224. + if (list_empty(&q->task_list))
  21225. + return;
  21226. +
  21227. + curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
  21228. + wake_up_process(curr->task);
  21229. + list_del_init(&curr->task_list);
  21230. +}
  21231. +EXPORT_SYMBOL(swake_up_locked);
  21232. +
  21233. +void swake_up_all_locked(struct swait_queue_head *q)
  21234. +{
  21235. + struct swait_queue *curr;
  21236. + int wakes = 0;
  21237. +
  21238. + while (!list_empty(&q->task_list)) {
  21239. +
  21240. + curr = list_first_entry(&q->task_list, typeof(*curr),
  21241. + task_list);
  21242. + wake_up_process(curr->task);
  21243. + list_del_init(&curr->task_list);
  21244. + wakes++;
  21245. + }
  21246. + if (pm_in_action)
  21247. + return;
  21248. + WARN(wakes > 2, "complate_all() with %d waiters\n", wakes);
  21249. +}
  21250. +EXPORT_SYMBOL(swake_up_all_locked);
  21251. +
  21252. +void swake_up(struct swait_queue_head *q)
  21253. +{
  21254. + unsigned long flags;
  21255. +
  21256. + if (!swait_active(q))
  21257. + return;
  21258. +
  21259. + raw_spin_lock_irqsave(&q->lock, flags);
  21260. + swake_up_locked(q);
  21261. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21262. +}
  21263. +EXPORT_SYMBOL(swake_up);
  21264. +
  21265. +/*
  21266. + * Does not allow usage from IRQ disabled, since we must be able to
  21267. + * release IRQs to guarantee bounded hold time.
  21268. + */
  21269. +void swake_up_all(struct swait_queue_head *q)
  21270. +{
  21271. + struct swait_queue *curr;
  21272. + LIST_HEAD(tmp);
  21273. +
  21274. + if (!swait_active(q))
  21275. + return;
  21276. +
  21277. + raw_spin_lock_irq(&q->lock);
  21278. + list_splice_init(&q->task_list, &tmp);
  21279. + while (!list_empty(&tmp)) {
  21280. + curr = list_first_entry(&tmp, typeof(*curr), task_list);
  21281. +
  21282. + wake_up_state(curr->task, TASK_NORMAL);
  21283. + list_del_init(&curr->task_list);
  21284. +
  21285. + if (list_empty(&tmp))
  21286. + break;
  21287. +
  21288. + raw_spin_unlock_irq(&q->lock);
  21289. + raw_spin_lock_irq(&q->lock);
  21290. + }
  21291. + raw_spin_unlock_irq(&q->lock);
  21292. +}
  21293. +EXPORT_SYMBOL(swake_up_all);
  21294. +
  21295. +void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21296. +{
  21297. + wait->task = current;
  21298. + if (list_empty(&wait->task_list))
  21299. + list_add(&wait->task_list, &q->task_list);
  21300. +}
  21301. +
  21302. +void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21303. +{
  21304. + unsigned long flags;
  21305. +
  21306. + raw_spin_lock_irqsave(&q->lock, flags);
  21307. + __prepare_to_swait(q, wait);
  21308. + set_current_state(state);
  21309. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21310. +}
  21311. +EXPORT_SYMBOL(prepare_to_swait);
  21312. +
  21313. +long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state)
  21314. +{
  21315. + if (signal_pending_state(state, current))
  21316. + return -ERESTARTSYS;
  21317. +
  21318. + prepare_to_swait(q, wait, state);
  21319. +
  21320. + return 0;
  21321. +}
  21322. +EXPORT_SYMBOL(prepare_to_swait_event);
  21323. +
  21324. +void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21325. +{
  21326. + __set_current_state(TASK_RUNNING);
  21327. + if (!list_empty(&wait->task_list))
  21328. + list_del_init(&wait->task_list);
  21329. +}
  21330. +
  21331. +void finish_swait(struct swait_queue_head *q, struct swait_queue *wait)
  21332. +{
  21333. + unsigned long flags;
  21334. +
  21335. + __set_current_state(TASK_RUNNING);
  21336. +
  21337. + if (!list_empty_careful(&wait->task_list)) {
  21338. + raw_spin_lock_irqsave(&q->lock, flags);
  21339. + list_del_init(&wait->task_list);
  21340. + raw_spin_unlock_irqrestore(&q->lock, flags);
  21341. + }
  21342. +}
  21343. +EXPORT_SYMBOL(finish_swait);
  21344. diff --git a/kernel/sched/swork.c b/kernel/sched/swork.c
  21345. new file mode 100644
  21346. index 000000000000..1950f40ca725
  21347. --- /dev/null
  21348. +++ b/kernel/sched/swork.c
  21349. @@ -0,0 +1,173 @@
  21350. +/*
  21351. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  21352. + *
  21353. + * Provides a framework for enqueuing callbacks from irq context
  21354. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  21355. + */
  21356. +
  21357. +#include <linux/swait.h>
  21358. +#include <linux/swork.h>
  21359. +#include <linux/kthread.h>
  21360. +#include <linux/slab.h>
  21361. +#include <linux/spinlock.h>
  21362. +#include <linux/export.h>
  21363. +
  21364. +#define SWORK_EVENT_PENDING (1 << 0)
  21365. +
  21366. +static DEFINE_MUTEX(worker_mutex);
  21367. +static struct sworker *glob_worker;
  21368. +
  21369. +struct sworker {
  21370. + struct list_head events;
  21371. + struct swait_queue_head wq;
  21372. +
  21373. + raw_spinlock_t lock;
  21374. +
  21375. + struct task_struct *task;
  21376. + int refs;
  21377. +};
  21378. +
  21379. +static bool swork_readable(struct sworker *worker)
  21380. +{
  21381. + bool r;
  21382. +
  21383. + if (kthread_should_stop())
  21384. + return true;
  21385. +
  21386. + raw_spin_lock_irq(&worker->lock);
  21387. + r = !list_empty(&worker->events);
  21388. + raw_spin_unlock_irq(&worker->lock);
  21389. +
  21390. + return r;
  21391. +}
  21392. +
  21393. +static int swork_kthread(void *arg)
  21394. +{
  21395. + struct sworker *worker = arg;
  21396. +
  21397. + for (;;) {
  21398. + swait_event_interruptible(worker->wq,
  21399. + swork_readable(worker));
  21400. + if (kthread_should_stop())
  21401. + break;
  21402. +
  21403. + raw_spin_lock_irq(&worker->lock);
  21404. + while (!list_empty(&worker->events)) {
  21405. + struct swork_event *sev;
  21406. +
  21407. + sev = list_first_entry(&worker->events,
  21408. + struct swork_event, item);
  21409. + list_del(&sev->item);
  21410. + raw_spin_unlock_irq(&worker->lock);
  21411. +
  21412. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  21413. + &sev->flags));
  21414. + sev->func(sev);
  21415. + raw_spin_lock_irq(&worker->lock);
  21416. + }
  21417. + raw_spin_unlock_irq(&worker->lock);
  21418. + }
  21419. + return 0;
  21420. +}
  21421. +
  21422. +static struct sworker *swork_create(void)
  21423. +{
  21424. + struct sworker *worker;
  21425. +
  21426. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  21427. + if (!worker)
  21428. + return ERR_PTR(-ENOMEM);
  21429. +
  21430. + INIT_LIST_HEAD(&worker->events);
  21431. + raw_spin_lock_init(&worker->lock);
  21432. + init_swait_queue_head(&worker->wq);
  21433. +
  21434. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  21435. + if (IS_ERR(worker->task)) {
  21436. + kfree(worker);
  21437. + return ERR_PTR(-ENOMEM);
  21438. + }
  21439. +
  21440. + return worker;
  21441. +}
  21442. +
  21443. +static void swork_destroy(struct sworker *worker)
  21444. +{
  21445. + kthread_stop(worker->task);
  21446. +
  21447. + WARN_ON(!list_empty(&worker->events));
  21448. + kfree(worker);
  21449. +}
  21450. +
  21451. +/**
  21452. + * swork_queue - queue swork
  21453. + *
  21454. + * Returns %false if @work was already on a queue, %true otherwise.
  21455. + *
  21456. + * The work is queued and processed on a random CPU
  21457. + */
  21458. +bool swork_queue(struct swork_event *sev)
  21459. +{
  21460. + unsigned long flags;
  21461. +
  21462. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  21463. + return false;
  21464. +
  21465. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  21466. + list_add_tail(&sev->item, &glob_worker->events);
  21467. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  21468. +
  21469. + swake_up(&glob_worker->wq);
  21470. + return true;
  21471. +}
  21472. +EXPORT_SYMBOL_GPL(swork_queue);
  21473. +
  21474. +/**
  21475. + * swork_get - get an instance of the sworker
  21476. + *
  21477. + * Returns an negative error code if the initialization if the worker did not
  21478. + * work, %0 otherwise.
  21479. + *
  21480. + */
  21481. +int swork_get(void)
  21482. +{
  21483. + struct sworker *worker;
  21484. +
  21485. + mutex_lock(&worker_mutex);
  21486. + if (!glob_worker) {
  21487. + worker = swork_create();
  21488. + if (IS_ERR(worker)) {
  21489. + mutex_unlock(&worker_mutex);
  21490. + return -ENOMEM;
  21491. + }
  21492. +
  21493. + glob_worker = worker;
  21494. + }
  21495. +
  21496. + glob_worker->refs++;
  21497. + mutex_unlock(&worker_mutex);
  21498. +
  21499. + return 0;
  21500. +}
  21501. +EXPORT_SYMBOL_GPL(swork_get);
  21502. +
  21503. +/**
  21504. + * swork_put - puts an instance of the sworker
  21505. + *
  21506. + * Will destroy the sworker thread. This function must not be called until all
  21507. + * queued events have been completed.
  21508. + */
  21509. +void swork_put(void)
  21510. +{
  21511. + mutex_lock(&worker_mutex);
  21512. +
  21513. + glob_worker->refs--;
  21514. + if (glob_worker->refs > 0)
  21515. + goto out;
  21516. +
  21517. + swork_destroy(glob_worker);
  21518. + glob_worker = NULL;
  21519. +out:
  21520. + mutex_unlock(&worker_mutex);
  21521. +}
  21522. +EXPORT_SYMBOL_GPL(swork_put);
  21523. diff --git a/kernel/signal.c b/kernel/signal.c
  21524. index f3f1f7a972fd..bc2c990f3f63 100644
  21525. --- a/kernel/signal.c
  21526. +++ b/kernel/signal.c
  21527. @@ -14,6 +14,7 @@
  21528. #include <linux/export.h>
  21529. #include <linux/init.h>
  21530. #include <linux/sched.h>
  21531. +#include <linux/sched/rt.h>
  21532. #include <linux/fs.h>
  21533. #include <linux/tty.h>
  21534. #include <linux/binfmts.h>
  21535. @@ -352,13 +353,30 @@ static bool task_participate_group_stop(struct task_struct *task)
  21536. return false;
  21537. }
  21538. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  21539. +{
  21540. + struct sigqueue *q = t->sigqueue_cache;
  21541. +
  21542. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  21543. + return NULL;
  21544. + return q;
  21545. +}
  21546. +
  21547. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  21548. +{
  21549. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  21550. + return 0;
  21551. + return 1;
  21552. +}
  21553. +
  21554. /*
  21555. * allocate a new signal queue record
  21556. * - this may be called without locks if and only if t == current, otherwise an
  21557. * appropriate lock must be held to stop the target task from exiting
  21558. */
  21559. static struct sigqueue *
  21560. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  21561. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  21562. + int override_rlimit, int fromslab)
  21563. {
  21564. struct sigqueue *q = NULL;
  21565. struct user_struct *user;
  21566. @@ -375,7 +393,10 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  21567. if (override_rlimit ||
  21568. atomic_read(&user->sigpending) <=
  21569. task_rlimit(t, RLIMIT_SIGPENDING)) {
  21570. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  21571. + if (!fromslab)
  21572. + q = get_task_cache(t);
  21573. + if (!q)
  21574. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  21575. } else {
  21576. print_dropped_signal(sig);
  21577. }
  21578. @@ -392,6 +413,13 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  21579. return q;
  21580. }
  21581. +static struct sigqueue *
  21582. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  21583. + int override_rlimit)
  21584. +{
  21585. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  21586. +}
  21587. +
  21588. static void __sigqueue_free(struct sigqueue *q)
  21589. {
  21590. if (q->flags & SIGQUEUE_PREALLOC)
  21591. @@ -401,6 +429,21 @@ static void __sigqueue_free(struct sigqueue *q)
  21592. kmem_cache_free(sigqueue_cachep, q);
  21593. }
  21594. +static void sigqueue_free_current(struct sigqueue *q)
  21595. +{
  21596. + struct user_struct *up;
  21597. +
  21598. + if (q->flags & SIGQUEUE_PREALLOC)
  21599. + return;
  21600. +
  21601. + up = q->user;
  21602. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  21603. + atomic_dec(&up->sigpending);
  21604. + free_uid(up);
  21605. + } else
  21606. + __sigqueue_free(q);
  21607. +}
  21608. +
  21609. void flush_sigqueue(struct sigpending *queue)
  21610. {
  21611. struct sigqueue *q;
  21612. @@ -414,6 +457,21 @@ void flush_sigqueue(struct sigpending *queue)
  21613. }
  21614. /*
  21615. + * Called from __exit_signal. Flush tsk->pending and
  21616. + * tsk->sigqueue_cache
  21617. + */
  21618. +void flush_task_sigqueue(struct task_struct *tsk)
  21619. +{
  21620. + struct sigqueue *q;
  21621. +
  21622. + flush_sigqueue(&tsk->pending);
  21623. +
  21624. + q = get_task_cache(tsk);
  21625. + if (q)
  21626. + kmem_cache_free(sigqueue_cachep, q);
  21627. +}
  21628. +
  21629. +/*
  21630. * Flush all pending signals for this kthread.
  21631. */
  21632. void flush_signals(struct task_struct *t)
  21633. @@ -525,7 +583,7 @@ static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  21634. still_pending:
  21635. list_del_init(&first->list);
  21636. copy_siginfo(info, &first->info);
  21637. - __sigqueue_free(first);
  21638. + sigqueue_free_current(first);
  21639. } else {
  21640. /*
  21641. * Ok, it wasn't in the queue. This must be
  21642. @@ -560,6 +618,8 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  21643. {
  21644. int signr;
  21645. + WARN_ON_ONCE(tsk != current);
  21646. +
  21647. /* We only dequeue private signals from ourselves, we don't let
  21648. * signalfd steal them
  21649. */
  21650. @@ -1156,8 +1216,8 @@ int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  21651. * We don't want to have recursive SIGSEGV's etc, for example,
  21652. * that is why we also clear SIGNAL_UNKILLABLE.
  21653. */
  21654. -int
  21655. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21656. +static int
  21657. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21658. {
  21659. unsigned long int flags;
  21660. int ret, blocked, ignored;
  21661. @@ -1182,6 +1242,39 @@ force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21662. return ret;
  21663. }
  21664. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  21665. +{
  21666. +/*
  21667. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  21668. + * since it can not enable preemption, and the signal code's spin_locks
  21669. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  21670. + * send the signal on exit of the trap.
  21671. + */
  21672. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  21673. + if (in_atomic()) {
  21674. + if (WARN_ON_ONCE(t != current))
  21675. + return 0;
  21676. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  21677. + return 0;
  21678. +
  21679. + if (is_si_special(info)) {
  21680. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  21681. + t->forced_info.si_signo = sig;
  21682. + t->forced_info.si_errno = 0;
  21683. + t->forced_info.si_code = SI_KERNEL;
  21684. + t->forced_info.si_pid = 0;
  21685. + t->forced_info.si_uid = 0;
  21686. + } else {
  21687. + t->forced_info = *info;
  21688. + }
  21689. +
  21690. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  21691. + return 0;
  21692. + }
  21693. +#endif
  21694. + return do_force_sig_info(sig, info, t);
  21695. +}
  21696. +
  21697. /*
  21698. * Nuke all other threads in the group.
  21699. */
  21700. @@ -1216,12 +1309,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  21701. * Disable interrupts early to avoid deadlocks.
  21702. * See rcu_read_unlock() comment header for details.
  21703. */
  21704. - local_irq_save(*flags);
  21705. + local_irq_save_nort(*flags);
  21706. rcu_read_lock();
  21707. sighand = rcu_dereference(tsk->sighand);
  21708. if (unlikely(sighand == NULL)) {
  21709. rcu_read_unlock();
  21710. - local_irq_restore(*flags);
  21711. + local_irq_restore_nort(*flags);
  21712. break;
  21713. }
  21714. /*
  21715. @@ -1242,7 +1335,7 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  21716. }
  21717. spin_unlock(&sighand->siglock);
  21718. rcu_read_unlock();
  21719. - local_irq_restore(*flags);
  21720. + local_irq_restore_nort(*flags);
  21721. }
  21722. return sighand;
  21723. @@ -1485,7 +1578,8 @@ EXPORT_SYMBOL(kill_pid);
  21724. */
  21725. struct sigqueue *sigqueue_alloc(void)
  21726. {
  21727. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  21728. + /* Preallocated sigqueue objects always from the slabcache ! */
  21729. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  21730. if (q)
  21731. q->flags |= SIGQUEUE_PREALLOC;
  21732. @@ -1846,15 +1940,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  21733. if (gstop_done && ptrace_reparented(current))
  21734. do_notify_parent_cldstop(current, false, why);
  21735. - /*
  21736. - * Don't want to allow preemption here, because
  21737. - * sys_ptrace() needs this task to be inactive.
  21738. - *
  21739. - * XXX: implement read_unlock_no_resched().
  21740. - */
  21741. - preempt_disable();
  21742. read_unlock(&tasklist_lock);
  21743. - preempt_enable_no_resched();
  21744. freezable_schedule();
  21745. } else {
  21746. /*
  21747. diff --git a/kernel/softirq.c b/kernel/softirq.c
  21748. index 479e4436f787..cb9c1d5dee10 100644
  21749. --- a/kernel/softirq.c
  21750. +++ b/kernel/softirq.c
  21751. @@ -21,10 +21,12 @@
  21752. #include <linux/freezer.h>
  21753. #include <linux/kthread.h>
  21754. #include <linux/rcupdate.h>
  21755. +#include <linux/delay.h>
  21756. #include <linux/ftrace.h>
  21757. #include <linux/smp.h>
  21758. #include <linux/smpboot.h>
  21759. #include <linux/tick.h>
  21760. +#include <linux/locallock.h>
  21761. #include <linux/irq.h>
  21762. #define CREATE_TRACE_POINTS
  21763. @@ -56,12 +58,108 @@ EXPORT_SYMBOL(irq_stat);
  21764. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  21765. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  21766. +#ifdef CONFIG_PREEMPT_RT_FULL
  21767. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  21768. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  21769. +#endif
  21770. const char * const softirq_to_name[NR_SOFTIRQS] = {
  21771. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  21772. "TASKLET", "SCHED", "HRTIMER", "RCU"
  21773. };
  21774. +#ifdef CONFIG_NO_HZ_COMMON
  21775. +# ifdef CONFIG_PREEMPT_RT_FULL
  21776. +
  21777. +struct softirq_runner {
  21778. + struct task_struct *runner[NR_SOFTIRQS];
  21779. +};
  21780. +
  21781. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  21782. +
  21783. +static inline void softirq_set_runner(unsigned int sirq)
  21784. +{
  21785. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21786. +
  21787. + sr->runner[sirq] = current;
  21788. +}
  21789. +
  21790. +static inline void softirq_clr_runner(unsigned int sirq)
  21791. +{
  21792. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21793. +
  21794. + sr->runner[sirq] = NULL;
  21795. +}
  21796. +
  21797. +/*
  21798. + * On preempt-rt a softirq running context might be blocked on a
  21799. + * lock. There might be no other runnable task on this CPU because the
  21800. + * lock owner runs on some other CPU. So we have to go into idle with
  21801. + * the pending bit set. Therefor we need to check this otherwise we
  21802. + * warn about false positives which confuses users and defeats the
  21803. + * whole purpose of this test.
  21804. + *
  21805. + * This code is called with interrupts disabled.
  21806. + */
  21807. +void softirq_check_pending_idle(void)
  21808. +{
  21809. + static int rate_limit;
  21810. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  21811. + u32 warnpending;
  21812. + int i;
  21813. +
  21814. + if (rate_limit >= 10)
  21815. + return;
  21816. +
  21817. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  21818. + for (i = 0; i < NR_SOFTIRQS; i++) {
  21819. + struct task_struct *tsk = sr->runner[i];
  21820. +
  21821. + /*
  21822. + * The wakeup code in rtmutex.c wakes up the task
  21823. + * _before_ it sets pi_blocked_on to NULL under
  21824. + * tsk->pi_lock. So we need to check for both: state
  21825. + * and pi_blocked_on.
  21826. + */
  21827. + if (tsk) {
  21828. + raw_spin_lock(&tsk->pi_lock);
  21829. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  21830. + /* Clear all bits pending in that task */
  21831. + warnpending &= ~(tsk->softirqs_raised);
  21832. + warnpending &= ~(1 << i);
  21833. + }
  21834. + raw_spin_unlock(&tsk->pi_lock);
  21835. + }
  21836. + }
  21837. +
  21838. + if (warnpending) {
  21839. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21840. + warnpending);
  21841. + rate_limit++;
  21842. + }
  21843. +}
  21844. +# else
  21845. +/*
  21846. + * On !PREEMPT_RT we just printk rate limited:
  21847. + */
  21848. +void softirq_check_pending_idle(void)
  21849. +{
  21850. + static int rate_limit;
  21851. +
  21852. + if (rate_limit < 10 &&
  21853. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  21854. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  21855. + local_softirq_pending());
  21856. + rate_limit++;
  21857. + }
  21858. +}
  21859. +# endif
  21860. +
  21861. +#else /* !CONFIG_NO_HZ_COMMON */
  21862. +static inline void softirq_set_runner(unsigned int sirq) { }
  21863. +static inline void softirq_clr_runner(unsigned int sirq) { }
  21864. +#endif
  21865. +
  21866. /*
  21867. * we cannot loop indefinitely here to avoid userspace starvation,
  21868. * but we also don't want to introduce a worst case 1/HZ latency
  21869. @@ -77,6 +175,79 @@ static void wakeup_softirqd(void)
  21870. wake_up_process(tsk);
  21871. }
  21872. +#ifdef CONFIG_PREEMPT_RT_FULL
  21873. +static void wakeup_timer_softirqd(void)
  21874. +{
  21875. + /* Interrupts are disabled: no need to stop preemption */
  21876. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  21877. +
  21878. + if (tsk && tsk->state != TASK_RUNNING)
  21879. + wake_up_process(tsk);
  21880. +}
  21881. +#endif
  21882. +
  21883. +static void handle_softirq(unsigned int vec_nr)
  21884. +{
  21885. + struct softirq_action *h = softirq_vec + vec_nr;
  21886. + int prev_count;
  21887. +
  21888. + prev_count = preempt_count();
  21889. +
  21890. + kstat_incr_softirqs_this_cpu(vec_nr);
  21891. +
  21892. + trace_softirq_entry(vec_nr);
  21893. + h->action(h);
  21894. + trace_softirq_exit(vec_nr);
  21895. + if (unlikely(prev_count != preempt_count())) {
  21896. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21897. + vec_nr, softirq_to_name[vec_nr], h->action,
  21898. + prev_count, preempt_count());
  21899. + preempt_count_set(prev_count);
  21900. + }
  21901. +}
  21902. +
  21903. +#ifndef CONFIG_PREEMPT_RT_FULL
  21904. +static inline int ksoftirqd_softirq_pending(void)
  21905. +{
  21906. + return local_softirq_pending();
  21907. +}
  21908. +
  21909. +static void handle_pending_softirqs(u32 pending)
  21910. +{
  21911. + struct softirq_action *h = softirq_vec;
  21912. + int softirq_bit;
  21913. +
  21914. + local_irq_enable();
  21915. +
  21916. + h = softirq_vec;
  21917. +
  21918. + while ((softirq_bit = ffs(pending))) {
  21919. + unsigned int vec_nr;
  21920. +
  21921. + h += softirq_bit - 1;
  21922. + vec_nr = h - softirq_vec;
  21923. + handle_softirq(vec_nr);
  21924. +
  21925. + h++;
  21926. + pending >>= softirq_bit;
  21927. + }
  21928. +
  21929. + rcu_bh_qs();
  21930. + local_irq_disable();
  21931. +}
  21932. +
  21933. +static void run_ksoftirqd(unsigned int cpu)
  21934. +{
  21935. + local_irq_disable();
  21936. + if (ksoftirqd_softirq_pending()) {
  21937. + __do_softirq();
  21938. + local_irq_enable();
  21939. + cond_resched_rcu_qs();
  21940. + return;
  21941. + }
  21942. + local_irq_enable();
  21943. +}
  21944. +
  21945. /*
  21946. * preempt_count and SOFTIRQ_OFFSET usage:
  21947. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  21948. @@ -116,9 +287,9 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  21949. if (preempt_count() == cnt) {
  21950. #ifdef CONFIG_DEBUG_PREEMPT
  21951. - current->preempt_disable_ip = get_parent_ip(CALLER_ADDR1);
  21952. + current->preempt_disable_ip = get_lock_parent_ip();
  21953. #endif
  21954. - trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  21955. + trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  21956. }
  21957. }
  21958. EXPORT_SYMBOL(__local_bh_disable_ip);
  21959. @@ -232,10 +403,8 @@ asmlinkage __visible void __do_softirq(void)
  21960. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  21961. unsigned long old_flags = current->flags;
  21962. int max_restart = MAX_SOFTIRQ_RESTART;
  21963. - struct softirq_action *h;
  21964. bool in_hardirq;
  21965. __u32 pending;
  21966. - int softirq_bit;
  21967. /*
  21968. * Mask out PF_MEMALLOC s current task context is borrowed for the
  21969. @@ -254,36 +423,7 @@ restart:
  21970. /* Reset the pending bitmask before enabling irqs */
  21971. set_softirq_pending(0);
  21972. - local_irq_enable();
  21973. -
  21974. - h = softirq_vec;
  21975. -
  21976. - while ((softirq_bit = ffs(pending))) {
  21977. - unsigned int vec_nr;
  21978. - int prev_count;
  21979. -
  21980. - h += softirq_bit - 1;
  21981. -
  21982. - vec_nr = h - softirq_vec;
  21983. - prev_count = preempt_count();
  21984. -
  21985. - kstat_incr_softirqs_this_cpu(vec_nr);
  21986. -
  21987. - trace_softirq_entry(vec_nr);
  21988. - h->action(h);
  21989. - trace_softirq_exit(vec_nr);
  21990. - if (unlikely(prev_count != preempt_count())) {
  21991. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  21992. - vec_nr, softirq_to_name[vec_nr], h->action,
  21993. - prev_count, preempt_count());
  21994. - preempt_count_set(prev_count);
  21995. - }
  21996. - h++;
  21997. - pending >>= softirq_bit;
  21998. - }
  21999. -
  22000. - rcu_bh_qs();
  22001. - local_irq_disable();
  22002. + handle_pending_softirqs(pending);
  22003. pending = local_softirq_pending();
  22004. if (pending) {
  22005. @@ -320,6 +460,310 @@ asmlinkage __visible void do_softirq(void)
  22006. }
  22007. /*
  22008. + * This function must run with irqs disabled!
  22009. + */
  22010. +void raise_softirq_irqoff(unsigned int nr)
  22011. +{
  22012. + __raise_softirq_irqoff(nr);
  22013. +
  22014. + /*
  22015. + * If we're in an interrupt or softirq, we're done
  22016. + * (this also catches softirq-disabled code). We will
  22017. + * actually run the softirq once we return from
  22018. + * the irq or softirq.
  22019. + *
  22020. + * Otherwise we wake up ksoftirqd to make sure we
  22021. + * schedule the softirq soon.
  22022. + */
  22023. + if (!in_interrupt())
  22024. + wakeup_softirqd();
  22025. +}
  22026. +
  22027. +void __raise_softirq_irqoff(unsigned int nr)
  22028. +{
  22029. + trace_softirq_raise(nr);
  22030. + or_softirq_pending(1UL << nr);
  22031. +}
  22032. +
  22033. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  22034. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  22035. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  22036. +
  22037. +#else /* !PREEMPT_RT_FULL */
  22038. +
  22039. +/*
  22040. + * On RT we serialize softirq execution with a cpu local lock per softirq
  22041. + */
  22042. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  22043. +
  22044. +void __init softirq_early_init(void)
  22045. +{
  22046. + int i;
  22047. +
  22048. + for (i = 0; i < NR_SOFTIRQS; i++)
  22049. + local_irq_lock_init(local_softirq_locks[i]);
  22050. +}
  22051. +
  22052. +static void lock_softirq(int which)
  22053. +{
  22054. + local_lock(local_softirq_locks[which]);
  22055. +}
  22056. +
  22057. +static void unlock_softirq(int which)
  22058. +{
  22059. + local_unlock(local_softirq_locks[which]);
  22060. +}
  22061. +
  22062. +static void do_single_softirq(int which)
  22063. +{
  22064. + unsigned long old_flags = current->flags;
  22065. +
  22066. + current->flags &= ~PF_MEMALLOC;
  22067. + vtime_account_irq_enter(current);
  22068. + current->flags |= PF_IN_SOFTIRQ;
  22069. + lockdep_softirq_enter();
  22070. + local_irq_enable();
  22071. + handle_softirq(which);
  22072. + local_irq_disable();
  22073. + lockdep_softirq_exit();
  22074. + current->flags &= ~PF_IN_SOFTIRQ;
  22075. + vtime_account_irq_enter(current);
  22076. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  22077. +}
  22078. +
  22079. +/*
  22080. + * Called with interrupts disabled. Process softirqs which were raised
  22081. + * in current context (or on behalf of ksoftirqd).
  22082. + */
  22083. +static void do_current_softirqs(void)
  22084. +{
  22085. + while (current->softirqs_raised) {
  22086. + int i = __ffs(current->softirqs_raised);
  22087. + unsigned int pending, mask = (1U << i);
  22088. +
  22089. + current->softirqs_raised &= ~mask;
  22090. + local_irq_enable();
  22091. +
  22092. + /*
  22093. + * If the lock is contended, we boost the owner to
  22094. + * process the softirq or leave the critical section
  22095. + * now.
  22096. + */
  22097. + lock_softirq(i);
  22098. + local_irq_disable();
  22099. + softirq_set_runner(i);
  22100. + /*
  22101. + * Check with the local_softirq_pending() bits,
  22102. + * whether we need to process this still or if someone
  22103. + * else took care of it.
  22104. + */
  22105. + pending = local_softirq_pending();
  22106. + if (pending & mask) {
  22107. + set_softirq_pending(pending & ~mask);
  22108. + do_single_softirq(i);
  22109. + }
  22110. + softirq_clr_runner(i);
  22111. + WARN_ON(current->softirq_nestcnt != 1);
  22112. + local_irq_enable();
  22113. + unlock_softirq(i);
  22114. + local_irq_disable();
  22115. + }
  22116. +}
  22117. +
  22118. +void __local_bh_disable(void)
  22119. +{
  22120. + if (++current->softirq_nestcnt == 1)
  22121. + migrate_disable();
  22122. +}
  22123. +EXPORT_SYMBOL(__local_bh_disable);
  22124. +
  22125. +void __local_bh_enable(void)
  22126. +{
  22127. + if (WARN_ON(current->softirq_nestcnt == 0))
  22128. + return;
  22129. +
  22130. + local_irq_disable();
  22131. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  22132. + do_current_softirqs();
  22133. + local_irq_enable();
  22134. +
  22135. + if (--current->softirq_nestcnt == 0)
  22136. + migrate_enable();
  22137. +}
  22138. +EXPORT_SYMBOL(__local_bh_enable);
  22139. +
  22140. +void _local_bh_enable(void)
  22141. +{
  22142. + if (WARN_ON(current->softirq_nestcnt == 0))
  22143. + return;
  22144. + if (--current->softirq_nestcnt == 0)
  22145. + migrate_enable();
  22146. +}
  22147. +EXPORT_SYMBOL(_local_bh_enable);
  22148. +
  22149. +int in_serving_softirq(void)
  22150. +{
  22151. + return current->flags & PF_IN_SOFTIRQ;
  22152. +}
  22153. +EXPORT_SYMBOL(in_serving_softirq);
  22154. +
  22155. +/* Called with preemption disabled */
  22156. +static void run_ksoftirqd(unsigned int cpu)
  22157. +{
  22158. + local_irq_disable();
  22159. + current->softirq_nestcnt++;
  22160. +
  22161. + do_current_softirqs();
  22162. + current->softirq_nestcnt--;
  22163. + local_irq_enable();
  22164. + cond_resched_rcu_qs();
  22165. +}
  22166. +
  22167. +/*
  22168. + * Called from netif_rx_ni(). Preemption enabled, but migration
  22169. + * disabled. So the cpu can't go away under us.
  22170. + */
  22171. +void thread_do_softirq(void)
  22172. +{
  22173. + if (!in_serving_softirq() && current->softirqs_raised) {
  22174. + current->softirq_nestcnt++;
  22175. + do_current_softirqs();
  22176. + current->softirq_nestcnt--;
  22177. + }
  22178. +}
  22179. +
  22180. +static void do_raise_softirq_irqoff(unsigned int nr)
  22181. +{
  22182. + unsigned int mask;
  22183. +
  22184. + mask = 1UL << nr;
  22185. +
  22186. + trace_softirq_raise(nr);
  22187. + or_softirq_pending(mask);
  22188. +
  22189. + /*
  22190. + * If we are not in a hard interrupt and inside a bh disabled
  22191. + * region, we simply raise the flag on current. local_bh_enable()
  22192. + * will make sure that the softirq is executed. Otherwise we
  22193. + * delegate it to ksoftirqd.
  22194. + */
  22195. + if (!in_irq() && current->softirq_nestcnt)
  22196. + current->softirqs_raised |= mask;
  22197. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  22198. + return;
  22199. +
  22200. + if (mask & TIMER_SOFTIRQS)
  22201. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  22202. + else
  22203. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  22204. +}
  22205. +
  22206. +static void wakeup_proper_softirq(unsigned int nr)
  22207. +{
  22208. + if ((1UL << nr) & TIMER_SOFTIRQS)
  22209. + wakeup_timer_softirqd();
  22210. + else
  22211. + wakeup_softirqd();
  22212. +}
  22213. +
  22214. +
  22215. +void __raise_softirq_irqoff(unsigned int nr)
  22216. +{
  22217. + do_raise_softirq_irqoff(nr);
  22218. + if (!in_irq() && !current->softirq_nestcnt)
  22219. + wakeup_proper_softirq(nr);
  22220. +}
  22221. +
  22222. +/*
  22223. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  22224. + */
  22225. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  22226. +{
  22227. + unsigned int mask;
  22228. +
  22229. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  22230. + !__this_cpu_read(ktimer_softirqd)))
  22231. + return;
  22232. + mask = 1UL << nr;
  22233. +
  22234. + trace_softirq_raise(nr);
  22235. + or_softirq_pending(mask);
  22236. + if (mask & TIMER_SOFTIRQS)
  22237. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  22238. + else
  22239. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  22240. + wakeup_proper_softirq(nr);
  22241. +}
  22242. +
  22243. +/*
  22244. + * This function must run with irqs disabled!
  22245. + */
  22246. +void raise_softirq_irqoff(unsigned int nr)
  22247. +{
  22248. + do_raise_softirq_irqoff(nr);
  22249. +
  22250. + /*
  22251. + * If we're in an hard interrupt we let irq return code deal
  22252. + * with the wakeup of ksoftirqd.
  22253. + */
  22254. + if (in_irq())
  22255. + return;
  22256. + /*
  22257. + * If we are in thread context but outside of a bh disabled
  22258. + * region, we need to wake ksoftirqd as well.
  22259. + *
  22260. + * CHECKME: Some of the places which do that could be wrapped
  22261. + * into local_bh_disable/enable pairs. Though it's unclear
  22262. + * whether this is worth the effort. To find those places just
  22263. + * raise a WARN() if the condition is met.
  22264. + */
  22265. + if (!current->softirq_nestcnt)
  22266. + wakeup_proper_softirq(nr);
  22267. +}
  22268. +
  22269. +static inline int ksoftirqd_softirq_pending(void)
  22270. +{
  22271. + return current->softirqs_raised;
  22272. +}
  22273. +
  22274. +static inline void local_bh_disable_nort(void) { }
  22275. +static inline void _local_bh_enable_nort(void) { }
  22276. +
  22277. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  22278. +{
  22279. + /* Take over all but timer pending softirqs when starting */
  22280. + local_irq_disable();
  22281. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  22282. + local_irq_enable();
  22283. +}
  22284. +
  22285. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  22286. +{
  22287. + struct sched_param param = { .sched_priority = 1 };
  22288. +
  22289. + sched_setscheduler(current, SCHED_FIFO, &param);
  22290. +
  22291. + /* Take over timer pending softirqs when starting */
  22292. + local_irq_disable();
  22293. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  22294. + local_irq_enable();
  22295. +}
  22296. +
  22297. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  22298. + bool online)
  22299. +{
  22300. + struct sched_param param = { .sched_priority = 0 };
  22301. +
  22302. + sched_setscheduler(current, SCHED_NORMAL, &param);
  22303. +}
  22304. +
  22305. +static int ktimer_softirqd_should_run(unsigned int cpu)
  22306. +{
  22307. + return current->softirqs_raised;
  22308. +}
  22309. +
  22310. +#endif /* PREEMPT_RT_FULL */
  22311. +/*
  22312. * Enter an interrupt context.
  22313. */
  22314. void irq_enter(void)
  22315. @@ -330,9 +774,9 @@ void irq_enter(void)
  22316. * Prevent raise_softirq from needlessly waking up ksoftirqd
  22317. * here, as softirq will be serviced on return from interrupt.
  22318. */
  22319. - local_bh_disable();
  22320. + local_bh_disable_nort();
  22321. tick_irq_enter();
  22322. - _local_bh_enable();
  22323. + _local_bh_enable_nort();
  22324. }
  22325. __irq_enter();
  22326. @@ -340,6 +784,7 @@ void irq_enter(void)
  22327. static inline void invoke_softirq(void)
  22328. {
  22329. +#ifndef CONFIG_PREEMPT_RT_FULL
  22330. if (!force_irqthreads) {
  22331. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  22332. /*
  22333. @@ -359,6 +804,18 @@ static inline void invoke_softirq(void)
  22334. } else {
  22335. wakeup_softirqd();
  22336. }
  22337. +#else /* PREEMPT_RT_FULL */
  22338. + unsigned long flags;
  22339. +
  22340. + local_irq_save(flags);
  22341. + if (__this_cpu_read(ksoftirqd) &&
  22342. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  22343. + wakeup_softirqd();
  22344. + if (__this_cpu_read(ktimer_softirqd) &&
  22345. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  22346. + wakeup_timer_softirqd();
  22347. + local_irq_restore(flags);
  22348. +#endif
  22349. }
  22350. static inline void tick_irq_exit(void)
  22351. @@ -395,26 +852,6 @@ void irq_exit(void)
  22352. trace_hardirq_exit(); /* must be last! */
  22353. }
  22354. -/*
  22355. - * This function must run with irqs disabled!
  22356. - */
  22357. -inline void raise_softirq_irqoff(unsigned int nr)
  22358. -{
  22359. - __raise_softirq_irqoff(nr);
  22360. -
  22361. - /*
  22362. - * If we're in an interrupt or softirq, we're done
  22363. - * (this also catches softirq-disabled code). We will
  22364. - * actually run the softirq once we return from
  22365. - * the irq or softirq.
  22366. - *
  22367. - * Otherwise we wake up ksoftirqd to make sure we
  22368. - * schedule the softirq soon.
  22369. - */
  22370. - if (!in_interrupt())
  22371. - wakeup_softirqd();
  22372. -}
  22373. -
  22374. void raise_softirq(unsigned int nr)
  22375. {
  22376. unsigned long flags;
  22377. @@ -424,12 +861,6 @@ void raise_softirq(unsigned int nr)
  22378. local_irq_restore(flags);
  22379. }
  22380. -void __raise_softirq_irqoff(unsigned int nr)
  22381. -{
  22382. - trace_softirq_raise(nr);
  22383. - or_softirq_pending(1UL << nr);
  22384. -}
  22385. -
  22386. void open_softirq(int nr, void (*action)(struct softirq_action *))
  22387. {
  22388. softirq_vec[nr].action = action;
  22389. @@ -446,15 +877,45 @@ struct tasklet_head {
  22390. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  22391. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  22392. +static void inline
  22393. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  22394. +{
  22395. + if (tasklet_trylock(t)) {
  22396. +again:
  22397. + /* We may have been preempted before tasklet_trylock
  22398. + * and __tasklet_action may have already run.
  22399. + * So double check the sched bit while the takslet
  22400. + * is locked before adding it to the list.
  22401. + */
  22402. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  22403. + t->next = NULL;
  22404. + *head->tail = t;
  22405. + head->tail = &(t->next);
  22406. + raise_softirq_irqoff(nr);
  22407. + tasklet_unlock(t);
  22408. + } else {
  22409. + /* This is subtle. If we hit the corner case above
  22410. + * It is possible that we get preempted right here,
  22411. + * and another task has successfully called
  22412. + * tasklet_schedule(), then this function, and
  22413. + * failed on the trylock. Thus we must be sure
  22414. + * before releasing the tasklet lock, that the
  22415. + * SCHED_BIT is clear. Otherwise the tasklet
  22416. + * may get its SCHED_BIT set, but not added to the
  22417. + * list
  22418. + */
  22419. + if (!tasklet_tryunlock(t))
  22420. + goto again;
  22421. + }
  22422. + }
  22423. +}
  22424. +
  22425. void __tasklet_schedule(struct tasklet_struct *t)
  22426. {
  22427. unsigned long flags;
  22428. local_irq_save(flags);
  22429. - t->next = NULL;
  22430. - *__this_cpu_read(tasklet_vec.tail) = t;
  22431. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22432. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22433. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  22434. local_irq_restore(flags);
  22435. }
  22436. EXPORT_SYMBOL(__tasklet_schedule);
  22437. @@ -464,10 +925,7 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)
  22438. unsigned long flags;
  22439. local_irq_save(flags);
  22440. - t->next = NULL;
  22441. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22442. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22443. - raise_softirq_irqoff(HI_SOFTIRQ);
  22444. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  22445. local_irq_restore(flags);
  22446. }
  22447. EXPORT_SYMBOL(__tasklet_hi_schedule);
  22448. @@ -476,82 +934,122 @@ void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  22449. {
  22450. BUG_ON(!irqs_disabled());
  22451. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  22452. - __this_cpu_write(tasklet_hi_vec.head, t);
  22453. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22454. + __tasklet_hi_schedule(t);
  22455. }
  22456. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  22457. -static void tasklet_action(struct softirq_action *a)
  22458. +void tasklet_enable(struct tasklet_struct *t)
  22459. {
  22460. - struct tasklet_struct *list;
  22461. + if (!atomic_dec_and_test(&t->count))
  22462. + return;
  22463. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  22464. + tasklet_schedule(t);
  22465. +}
  22466. +EXPORT_SYMBOL(tasklet_enable);
  22467. - local_irq_disable();
  22468. - list = __this_cpu_read(tasklet_vec.head);
  22469. - __this_cpu_write(tasklet_vec.head, NULL);
  22470. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22471. - local_irq_enable();
  22472. +static void __tasklet_action(struct softirq_action *a,
  22473. + struct tasklet_struct *list)
  22474. +{
  22475. + int loops = 1000000;
  22476. while (list) {
  22477. struct tasklet_struct *t = list;
  22478. list = list->next;
  22479. - if (tasklet_trylock(t)) {
  22480. - if (!atomic_read(&t->count)) {
  22481. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22482. - &t->state))
  22483. - BUG();
  22484. - t->func(t->data);
  22485. - tasklet_unlock(t);
  22486. - continue;
  22487. - }
  22488. - tasklet_unlock(t);
  22489. + /*
  22490. + * Should always succeed - after a tasklist got on the
  22491. + * list (after getting the SCHED bit set from 0 to 1),
  22492. + * nothing but the tasklet softirq it got queued to can
  22493. + * lock it:
  22494. + */
  22495. + if (!tasklet_trylock(t)) {
  22496. + WARN_ON(1);
  22497. + continue;
  22498. }
  22499. - local_irq_disable();
  22500. t->next = NULL;
  22501. - *__this_cpu_read(tasklet_vec.tail) = t;
  22502. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  22503. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  22504. - local_irq_enable();
  22505. +
  22506. + /*
  22507. + * If we cannot handle the tasklet because it's disabled,
  22508. + * mark it as pending. tasklet_enable() will later
  22509. + * re-schedule the tasklet.
  22510. + */
  22511. + if (unlikely(atomic_read(&t->count))) {
  22512. +out_disabled:
  22513. + /* implicit unlock: */
  22514. + wmb();
  22515. + t->state = TASKLET_STATEF_PENDING;
  22516. + continue;
  22517. + }
  22518. +
  22519. + /*
  22520. + * After this point on the tasklet might be rescheduled
  22521. + * on another CPU, but it can only be added to another
  22522. + * CPU's tasklet list if we unlock the tasklet (which we
  22523. + * dont do yet).
  22524. + */
  22525. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22526. + WARN_ON(1);
  22527. +
  22528. +again:
  22529. + t->func(t->data);
  22530. +
  22531. + /*
  22532. + * Try to unlock the tasklet. We must use cmpxchg, because
  22533. + * another CPU might have scheduled or disabled the tasklet.
  22534. + * We only allow the STATE_RUN -> 0 transition here.
  22535. + */
  22536. + while (!tasklet_tryunlock(t)) {
  22537. + /*
  22538. + * If it got disabled meanwhile, bail out:
  22539. + */
  22540. + if (atomic_read(&t->count))
  22541. + goto out_disabled;
  22542. + /*
  22543. + * If it got scheduled meanwhile, re-execute
  22544. + * the tasklet function:
  22545. + */
  22546. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  22547. + goto again;
  22548. + if (!--loops) {
  22549. + printk("hm, tasklet state: %08lx\n", t->state);
  22550. + WARN_ON(1);
  22551. + tasklet_unlock(t);
  22552. + break;
  22553. + }
  22554. + }
  22555. }
  22556. }
  22557. +static void tasklet_action(struct softirq_action *a)
  22558. +{
  22559. + struct tasklet_struct *list;
  22560. +
  22561. + local_irq_disable();
  22562. +
  22563. + list = __this_cpu_read(tasklet_vec.head);
  22564. + __this_cpu_write(tasklet_vec.head, NULL);
  22565. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  22566. +
  22567. + local_irq_enable();
  22568. +
  22569. + __tasklet_action(a, list);
  22570. +}
  22571. +
  22572. static void tasklet_hi_action(struct softirq_action *a)
  22573. {
  22574. struct tasklet_struct *list;
  22575. local_irq_disable();
  22576. +
  22577. list = __this_cpu_read(tasklet_hi_vec.head);
  22578. __this_cpu_write(tasklet_hi_vec.head, NULL);
  22579. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  22580. - local_irq_enable();
  22581. - while (list) {
  22582. - struct tasklet_struct *t = list;
  22583. -
  22584. - list = list->next;
  22585. -
  22586. - if (tasklet_trylock(t)) {
  22587. - if (!atomic_read(&t->count)) {
  22588. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  22589. - &t->state))
  22590. - BUG();
  22591. - t->func(t->data);
  22592. - tasklet_unlock(t);
  22593. - continue;
  22594. - }
  22595. - tasklet_unlock(t);
  22596. - }
  22597. + local_irq_enable();
  22598. - local_irq_disable();
  22599. - t->next = NULL;
  22600. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  22601. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  22602. - __raise_softirq_irqoff(HI_SOFTIRQ);
  22603. - local_irq_enable();
  22604. - }
  22605. + __tasklet_action(a, list);
  22606. }
  22607. void tasklet_init(struct tasklet_struct *t,
  22608. @@ -572,7 +1070,7 @@ void tasklet_kill(struct tasklet_struct *t)
  22609. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  22610. do {
  22611. - yield();
  22612. + msleep(1);
  22613. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  22614. }
  22615. tasklet_unlock_wait(t);
  22616. @@ -646,25 +1144,26 @@ void __init softirq_init(void)
  22617. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  22618. }
  22619. -static int ksoftirqd_should_run(unsigned int cpu)
  22620. -{
  22621. - return local_softirq_pending();
  22622. -}
  22623. -
  22624. -static void run_ksoftirqd(unsigned int cpu)
  22625. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  22626. +void tasklet_unlock_wait(struct tasklet_struct *t)
  22627. {
  22628. - local_irq_disable();
  22629. - if (local_softirq_pending()) {
  22630. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  22631. /*
  22632. - * We can safely run softirq on inline stack, as we are not deep
  22633. - * in the task stack here.
  22634. + * Hack for now to avoid this busy-loop:
  22635. */
  22636. - __do_softirq();
  22637. - local_irq_enable();
  22638. - cond_resched_rcu_qs();
  22639. - return;
  22640. +#ifdef CONFIG_PREEMPT_RT_FULL
  22641. + msleep(1);
  22642. +#else
  22643. + barrier();
  22644. +#endif
  22645. }
  22646. - local_irq_enable();
  22647. +}
  22648. +EXPORT_SYMBOL(tasklet_unlock_wait);
  22649. +#endif
  22650. +
  22651. +static int ksoftirqd_should_run(unsigned int cpu)
  22652. +{
  22653. + return ksoftirqd_softirq_pending();
  22654. }
  22655. #ifdef CONFIG_HOTPLUG_CPU
  22656. @@ -746,16 +1245,31 @@ static struct notifier_block cpu_nfb = {
  22657. static struct smp_hotplug_thread softirq_threads = {
  22658. .store = &ksoftirqd,
  22659. + .setup = ksoftirqd_set_sched_params,
  22660. .thread_should_run = ksoftirqd_should_run,
  22661. .thread_fn = run_ksoftirqd,
  22662. .thread_comm = "ksoftirqd/%u",
  22663. };
  22664. +#ifdef CONFIG_PREEMPT_RT_FULL
  22665. +static struct smp_hotplug_thread softirq_timer_threads = {
  22666. + .store = &ktimer_softirqd,
  22667. + .setup = ktimer_softirqd_set_sched_params,
  22668. + .cleanup = ktimer_softirqd_clr_sched_params,
  22669. + .thread_should_run = ktimer_softirqd_should_run,
  22670. + .thread_fn = run_ksoftirqd,
  22671. + .thread_comm = "ktimersoftd/%u",
  22672. +};
  22673. +#endif
  22674. +
  22675. static __init int spawn_ksoftirqd(void)
  22676. {
  22677. register_cpu_notifier(&cpu_nfb);
  22678. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  22679. +#ifdef CONFIG_PREEMPT_RT_FULL
  22680. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  22681. +#endif
  22682. return 0;
  22683. }
  22684. diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
  22685. index a3bbaee77c58..f84d3b45cda7 100644
  22686. --- a/kernel/stop_machine.c
  22687. +++ b/kernel/stop_machine.c
  22688. @@ -37,7 +37,7 @@ struct cpu_stop_done {
  22689. struct cpu_stopper {
  22690. struct task_struct *thread;
  22691. - spinlock_t lock;
  22692. + raw_spinlock_t lock;
  22693. bool enabled; /* is this stopper enabled? */
  22694. struct list_head works; /* list of pending works */
  22695. @@ -86,12 +86,12 @@ static void cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  22696. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22697. unsigned long flags;
  22698. - spin_lock_irqsave(&stopper->lock, flags);
  22699. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22700. if (stopper->enabled)
  22701. __cpu_stop_queue_work(stopper, work);
  22702. else
  22703. cpu_stop_signal_done(work->done, false);
  22704. - spin_unlock_irqrestore(&stopper->lock, flags);
  22705. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22706. }
  22707. /**
  22708. @@ -224,8 +224,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  22709. int err;
  22710. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  22711. - spin_lock_irq(&stopper1->lock);
  22712. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22713. + raw_spin_lock_irq(&stopper1->lock);
  22714. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  22715. err = -ENOENT;
  22716. if (!stopper1->enabled || !stopper2->enabled)
  22717. @@ -235,8 +235,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  22718. __cpu_stop_queue_work(stopper1, work1);
  22719. __cpu_stop_queue_work(stopper2, work2);
  22720. unlock:
  22721. - spin_unlock(&stopper2->lock);
  22722. - spin_unlock_irq(&stopper1->lock);
  22723. + raw_spin_unlock(&stopper2->lock);
  22724. + raw_spin_unlock_irq(&stopper1->lock);
  22725. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  22726. return err;
  22727. @@ -258,7 +258,7 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  22728. struct cpu_stop_work work1, work2;
  22729. struct multi_stop_data msdata;
  22730. - preempt_disable();
  22731. + preempt_disable_nort();
  22732. msdata = (struct multi_stop_data){
  22733. .fn = fn,
  22734. .data = arg,
  22735. @@ -278,11 +278,11 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *
  22736. if (cpu1 > cpu2)
  22737. swap(cpu1, cpu2);
  22738. if (cpu_stop_queue_two_works(cpu1, &work1, cpu2, &work2)) {
  22739. - preempt_enable();
  22740. + preempt_enable_nort();
  22741. return -ENOENT;
  22742. }
  22743. - preempt_enable();
  22744. + preempt_enable_nort();
  22745. wait_for_completion(&done.completion);
  22746. @@ -315,17 +315,20 @@ static DEFINE_MUTEX(stop_cpus_mutex);
  22747. static void queue_stop_cpus_work(const struct cpumask *cpumask,
  22748. cpu_stop_fn_t fn, void *arg,
  22749. - struct cpu_stop_done *done)
  22750. + struct cpu_stop_done *done, bool inactive)
  22751. {
  22752. struct cpu_stop_work *work;
  22753. unsigned int cpu;
  22754. /*
  22755. - * Disable preemption while queueing to avoid getting
  22756. - * preempted by a stopper which might wait for other stoppers
  22757. - * to enter @fn which can lead to deadlock.
  22758. + * Make sure that all work is queued on all cpus before
  22759. + * any of the cpus can execute it.
  22760. */
  22761. - lg_global_lock(&stop_cpus_lock);
  22762. + if (!inactive)
  22763. + lg_global_lock(&stop_cpus_lock);
  22764. + else
  22765. + lg_global_trylock_relax(&stop_cpus_lock);
  22766. +
  22767. for_each_cpu(cpu, cpumask) {
  22768. work = &per_cpu(cpu_stopper.stop_work, cpu);
  22769. work->fn = fn;
  22770. @@ -342,7 +345,7 @@ static int __stop_cpus(const struct cpumask *cpumask,
  22771. struct cpu_stop_done done;
  22772. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  22773. - queue_stop_cpus_work(cpumask, fn, arg, &done);
  22774. + queue_stop_cpus_work(cpumask, fn, arg, &done, false);
  22775. wait_for_completion(&done.completion);
  22776. return done.executed ? done.ret : -ENOENT;
  22777. }
  22778. @@ -422,9 +425,9 @@ static int cpu_stop_should_run(unsigned int cpu)
  22779. unsigned long flags;
  22780. int run;
  22781. - spin_lock_irqsave(&stopper->lock, flags);
  22782. + raw_spin_lock_irqsave(&stopper->lock, flags);
  22783. run = !list_empty(&stopper->works);
  22784. - spin_unlock_irqrestore(&stopper->lock, flags);
  22785. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  22786. return run;
  22787. }
  22788. @@ -436,13 +439,13 @@ static void cpu_stopper_thread(unsigned int cpu)
  22789. repeat:
  22790. work = NULL;
  22791. - spin_lock_irq(&stopper->lock);
  22792. + raw_spin_lock_irq(&stopper->lock);
  22793. if (!list_empty(&stopper->works)) {
  22794. work = list_first_entry(&stopper->works,
  22795. struct cpu_stop_work, list);
  22796. list_del_init(&work->list);
  22797. }
  22798. - spin_unlock_irq(&stopper->lock);
  22799. + raw_spin_unlock_irq(&stopper->lock);
  22800. if (work) {
  22801. cpu_stop_fn_t fn = work->fn;
  22802. @@ -450,6 +453,16 @@ repeat:
  22803. struct cpu_stop_done *done = work->done;
  22804. char ksym_buf[KSYM_NAME_LEN] __maybe_unused;
  22805. + /*
  22806. + * Wait until the stopper finished scheduling on all
  22807. + * cpus
  22808. + */
  22809. + lg_global_lock(&stop_cpus_lock);
  22810. + /*
  22811. + * Let other cpu threads continue as well
  22812. + */
  22813. + lg_global_unlock(&stop_cpus_lock);
  22814. +
  22815. /* cpu stop callbacks are not allowed to sleep */
  22816. preempt_disable();
  22817. @@ -520,10 +533,12 @@ static int __init cpu_stop_init(void)
  22818. for_each_possible_cpu(cpu) {
  22819. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  22820. - spin_lock_init(&stopper->lock);
  22821. + raw_spin_lock_init(&stopper->lock);
  22822. INIT_LIST_HEAD(&stopper->works);
  22823. }
  22824. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  22825. +
  22826. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  22827. stop_machine_unpark(raw_smp_processor_id());
  22828. stop_machine_initialized = true;
  22829. @@ -620,7 +635,7 @@ int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  22830. set_state(&msdata, MULTI_STOP_PREPARE);
  22831. cpu_stop_init_done(&done, num_active_cpus());
  22832. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  22833. - &done);
  22834. + &done, true);
  22835. ret = multi_cpu_stop(&msdata);
  22836. /* Busy wait for completion. */
  22837. diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
  22838. index fa909f9fd559..2cc3675e6864 100644
  22839. --- a/kernel/time/hrtimer.c
  22840. +++ b/kernel/time/hrtimer.c
  22841. @@ -48,11 +48,13 @@
  22842. #include <linux/sched/rt.h>
  22843. #include <linux/sched/deadline.h>
  22844. #include <linux/timer.h>
  22845. +#include <linux/kthread.h>
  22846. #include <linux/freezer.h>
  22847. #include <asm/uaccess.h>
  22848. #include <trace/events/timer.h>
  22849. +#include <trace/events/hist.h>
  22850. #include "tick-internal.h"
  22851. @@ -712,6 +714,44 @@ static void clock_was_set_work(struct work_struct *work)
  22852. static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  22853. +#ifdef CONFIG_PREEMPT_RT_FULL
  22854. +/*
  22855. + * RT can not call schedule_work from real interrupt context.
  22856. + * Need to make a thread to do the real work.
  22857. + */
  22858. +static struct task_struct *clock_set_delay_thread;
  22859. +static bool do_clock_set_delay;
  22860. +
  22861. +static int run_clock_set_delay(void *ignore)
  22862. +{
  22863. + while (!kthread_should_stop()) {
  22864. + set_current_state(TASK_INTERRUPTIBLE);
  22865. + if (do_clock_set_delay) {
  22866. + do_clock_set_delay = false;
  22867. + schedule_work(&hrtimer_work);
  22868. + }
  22869. + schedule();
  22870. + }
  22871. + __set_current_state(TASK_RUNNING);
  22872. + return 0;
  22873. +}
  22874. +
  22875. +void clock_was_set_delayed(void)
  22876. +{
  22877. + do_clock_set_delay = true;
  22878. + /* Make visible before waking up process */
  22879. + smp_wmb();
  22880. + wake_up_process(clock_set_delay_thread);
  22881. +}
  22882. +
  22883. +static __init int create_clock_set_delay_thread(void)
  22884. +{
  22885. + clock_set_delay_thread = kthread_run(run_clock_set_delay, NULL, "kclksetdelayd");
  22886. + BUG_ON(!clock_set_delay_thread);
  22887. + return 0;
  22888. +}
  22889. +early_initcall(create_clock_set_delay_thread);
  22890. +#else /* PREEMPT_RT_FULL */
  22891. /*
  22892. * Called from timekeeping and resume code to reprogramm the hrtimer
  22893. * interrupt device on all cpus.
  22894. @@ -720,6 +760,7 @@ void clock_was_set_delayed(void)
  22895. {
  22896. schedule_work(&hrtimer_work);
  22897. }
  22898. +#endif
  22899. #else
  22900. @@ -729,11 +770,8 @@ static inline int hrtimer_is_hres_enabled(void) { return 0; }
  22901. static inline void hrtimer_switch_to_hres(void) { }
  22902. static inline void
  22903. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  22904. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  22905. - struct hrtimer_clock_base *base)
  22906. -{
  22907. - return 0;
  22908. -}
  22909. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  22910. + struct hrtimer_clock_base *base) { }
  22911. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  22912. static inline void retrigger_next_event(void *arg) { }
  22913. @@ -865,6 +903,32 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  22914. }
  22915. EXPORT_SYMBOL_GPL(hrtimer_forward);
  22916. +#ifdef CONFIG_PREEMPT_RT_BASE
  22917. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  22918. +
  22919. +/**
  22920. + * hrtimer_wait_for_timer - Wait for a running timer
  22921. + *
  22922. + * @timer: timer to wait for
  22923. + *
  22924. + * The function waits in case the timers callback function is
  22925. + * currently executed on the waitqueue of the timer base. The
  22926. + * waitqueue is woken up after the timer callback function has
  22927. + * finished execution.
  22928. + */
  22929. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  22930. +{
  22931. + struct hrtimer_clock_base *base = timer->base;
  22932. +
  22933. + if (base && base->cpu_base && !timer->irqsafe)
  22934. + wait_event(base->cpu_base->wait,
  22935. + !(hrtimer_callback_running(timer)));
  22936. +}
  22937. +
  22938. +#else
  22939. +# define wake_up_timer_waiters(b) do { } while (0)
  22940. +#endif
  22941. +
  22942. /*
  22943. * enqueue_hrtimer - internal function to (re)start a timer
  22944. *
  22945. @@ -906,6 +970,11 @@ static void __remove_hrtimer(struct hrtimer *timer,
  22946. if (!(state & HRTIMER_STATE_ENQUEUED))
  22947. return;
  22948. + if (unlikely(!list_empty(&timer->cb_entry))) {
  22949. + list_del_init(&timer->cb_entry);
  22950. + return;
  22951. + }
  22952. +
  22953. if (!timerqueue_del(&base->active, &timer->node))
  22954. cpu_base->active_bases &= ~(1 << base->index);
  22955. @@ -1001,7 +1070,16 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  22956. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  22957. timer_stats_hrtimer_set_start_info(timer);
  22958. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  22959. + {
  22960. + ktime_t now = new_base->get_time();
  22961. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  22962. + timer->praecox = now;
  22963. + else
  22964. + timer->praecox = ktime_set(0, 0);
  22965. + }
  22966. +#endif
  22967. leftmost = enqueue_hrtimer(timer, new_base);
  22968. if (!leftmost)
  22969. goto unlock;
  22970. @@ -1073,7 +1151,7 @@ int hrtimer_cancel(struct hrtimer *timer)
  22971. if (ret >= 0)
  22972. return ret;
  22973. - cpu_relax();
  22974. + hrtimer_wait_for_timer(timer);
  22975. }
  22976. }
  22977. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  22978. @@ -1137,6 +1215,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  22979. base = hrtimer_clockid_to_base(clock_id);
  22980. timer->base = &cpu_base->clock_base[base];
  22981. + INIT_LIST_HEAD(&timer->cb_entry);
  22982. timerqueue_init(&timer->node);
  22983. #ifdef CONFIG_TIMER_STATS
  22984. @@ -1177,6 +1256,7 @@ bool hrtimer_active(const struct hrtimer *timer)
  22985. seq = raw_read_seqcount_begin(&cpu_base->seq);
  22986. if (timer->state != HRTIMER_STATE_INACTIVE ||
  22987. + cpu_base->running_soft == timer ||
  22988. cpu_base->running == timer)
  22989. return true;
  22990. @@ -1275,10 +1355,112 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  22991. cpu_base->running = NULL;
  22992. }
  22993. +#ifdef CONFIG_PREEMPT_RT_BASE
  22994. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  22995. + struct hrtimer_clock_base *base)
  22996. +{
  22997. + int leftmost;
  22998. +
  22999. + if (restart != HRTIMER_NORESTART &&
  23000. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  23001. +
  23002. + leftmost = enqueue_hrtimer(timer, base);
  23003. + if (!leftmost)
  23004. + return;
  23005. +#ifdef CONFIG_HIGH_RES_TIMERS
  23006. + if (!hrtimer_is_hres_active(timer)) {
  23007. + /*
  23008. + * Kick to reschedule the next tick to handle the new timer
  23009. + * on dynticks target.
  23010. + */
  23011. + if (base->cpu_base->nohz_active)
  23012. + wake_up_nohz_cpu(base->cpu_base->cpu);
  23013. + } else {
  23014. +
  23015. + hrtimer_reprogram(timer, base);
  23016. + }
  23017. +#endif
  23018. + }
  23019. +}
  23020. +
  23021. +/*
  23022. + * The changes in mainline which removed the callback modes from
  23023. + * hrtimer are not yet working with -rt. The non wakeup_process()
  23024. + * based callbacks which involve sleeping locks need to be treated
  23025. + * seperately.
  23026. + */
  23027. +static void hrtimer_rt_run_pending(void)
  23028. +{
  23029. + enum hrtimer_restart (*fn)(struct hrtimer *);
  23030. + struct hrtimer_cpu_base *cpu_base;
  23031. + struct hrtimer_clock_base *base;
  23032. + struct hrtimer *timer;
  23033. + int index, restart;
  23034. +
  23035. + local_irq_disable();
  23036. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  23037. +
  23038. + raw_spin_lock(&cpu_base->lock);
  23039. +
  23040. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  23041. + base = &cpu_base->clock_base[index];
  23042. +
  23043. + while (!list_empty(&base->expired)) {
  23044. + timer = list_first_entry(&base->expired,
  23045. + struct hrtimer, cb_entry);
  23046. +
  23047. + /*
  23048. + * Same as the above __run_hrtimer function
  23049. + * just we run with interrupts enabled.
  23050. + */
  23051. + debug_deactivate(timer);
  23052. + cpu_base->running_soft = timer;
  23053. + raw_write_seqcount_barrier(&cpu_base->seq);
  23054. +
  23055. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  23056. + timer_stats_account_hrtimer(timer);
  23057. + fn = timer->function;
  23058. +
  23059. + raw_spin_unlock_irq(&cpu_base->lock);
  23060. + restart = fn(timer);
  23061. + raw_spin_lock_irq(&cpu_base->lock);
  23062. +
  23063. + hrtimer_rt_reprogram(restart, timer, base);
  23064. + raw_write_seqcount_barrier(&cpu_base->seq);
  23065. +
  23066. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  23067. + cpu_base->running_soft = NULL;
  23068. + }
  23069. + }
  23070. +
  23071. + raw_spin_unlock_irq(&cpu_base->lock);
  23072. +
  23073. + wake_up_timer_waiters(cpu_base);
  23074. +}
  23075. +
  23076. +static int hrtimer_rt_defer(struct hrtimer *timer)
  23077. +{
  23078. + if (timer->irqsafe)
  23079. + return 0;
  23080. +
  23081. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  23082. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  23083. + return 1;
  23084. +}
  23085. +
  23086. +#else
  23087. +
  23088. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  23089. +
  23090. +#endif
  23091. +
  23092. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  23093. +
  23094. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23095. {
  23096. struct hrtimer_clock_base *base = cpu_base->clock_base;
  23097. unsigned int active = cpu_base->active_bases;
  23098. + int raise = 0;
  23099. for (; active; base++, active >>= 1) {
  23100. struct timerqueue_node *node;
  23101. @@ -1294,6 +1476,15 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23102. timer = container_of(node, struct hrtimer, node);
  23103. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  23104. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  23105. + timer->praecox : hrtimer_get_expires(timer),
  23106. + basenow)),
  23107. + current,
  23108. + timer->function == hrtimer_wakeup ?
  23109. + container_of(timer, struct hrtimer_sleeper,
  23110. + timer)->task : NULL);
  23111. +
  23112. /*
  23113. * The immediate goal for using the softexpires is
  23114. * minimizing wakeups, not running timers at the
  23115. @@ -1309,9 +1500,14 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  23116. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  23117. break;
  23118. - __run_hrtimer(cpu_base, base, timer, &basenow);
  23119. + if (!hrtimer_rt_defer(timer))
  23120. + __run_hrtimer(cpu_base, base, timer, &basenow);
  23121. + else
  23122. + raise = 1;
  23123. }
  23124. }
  23125. + if (raise)
  23126. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  23127. }
  23128. #ifdef CONFIG_HIGH_RES_TIMERS
  23129. @@ -1474,16 +1670,18 @@ static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  23130. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  23131. {
  23132. sl->timer.function = hrtimer_wakeup;
  23133. + sl->timer.irqsafe = 1;
  23134. sl->task = task;
  23135. }
  23136. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  23137. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  23138. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  23139. + unsigned long state)
  23140. {
  23141. hrtimer_init_sleeper(t, current);
  23142. do {
  23143. - set_current_state(TASK_INTERRUPTIBLE);
  23144. + set_current_state(state);
  23145. hrtimer_start_expires(&t->timer, mode);
  23146. if (likely(t->task))
  23147. @@ -1525,7 +1723,8 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  23148. HRTIMER_MODE_ABS);
  23149. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  23150. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  23151. + /* cpu_chill() does not care about restart state. */
  23152. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  23153. goto out;
  23154. rmtp = restart->nanosleep.rmtp;
  23155. @@ -1542,8 +1741,10 @@ out:
  23156. return ret;
  23157. }
  23158. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23159. - const enum hrtimer_mode mode, const clockid_t clockid)
  23160. +static long
  23161. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23162. + const enum hrtimer_mode mode, const clockid_t clockid,
  23163. + unsigned long state)
  23164. {
  23165. struct restart_block *restart;
  23166. struct hrtimer_sleeper t;
  23167. @@ -1556,7 +1757,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23168. hrtimer_init_on_stack(&t.timer, clockid, mode);
  23169. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  23170. - if (do_nanosleep(&t, mode))
  23171. + if (do_nanosleep(&t, mode, state))
  23172. goto out;
  23173. /* Absolute timers do not update the rmtp value and restart: */
  23174. @@ -1583,6 +1784,12 @@ out:
  23175. return ret;
  23176. }
  23177. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  23178. + const enum hrtimer_mode mode, const clockid_t clockid)
  23179. +{
  23180. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  23181. +}
  23182. +
  23183. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  23184. struct timespec __user *, rmtp)
  23185. {
  23186. @@ -1597,6 +1804,26 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  23187. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  23188. }
  23189. +#ifdef CONFIG_PREEMPT_RT_FULL
  23190. +/*
  23191. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  23192. + */
  23193. +void cpu_chill(void)
  23194. +{
  23195. + struct timespec tu = {
  23196. + .tv_nsec = NSEC_PER_MSEC,
  23197. + };
  23198. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  23199. +
  23200. + current->flags |= PF_NOFREEZE;
  23201. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  23202. + TASK_UNINTERRUPTIBLE);
  23203. + if (!freeze_flag)
  23204. + current->flags &= ~PF_NOFREEZE;
  23205. +}
  23206. +EXPORT_SYMBOL(cpu_chill);
  23207. +#endif
  23208. +
  23209. /*
  23210. * Functions related to boot-time initialization:
  23211. */
  23212. @@ -1608,10 +1835,14 @@ static void init_hrtimers_cpu(int cpu)
  23213. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  23214. cpu_base->clock_base[i].cpu_base = cpu_base;
  23215. timerqueue_init_head(&cpu_base->clock_base[i].active);
  23216. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  23217. }
  23218. cpu_base->cpu = cpu;
  23219. hrtimer_init_hres(cpu_base);
  23220. +#ifdef CONFIG_PREEMPT_RT_BASE
  23221. + init_waitqueue_head(&cpu_base->wait);
  23222. +#endif
  23223. }
  23224. #ifdef CONFIG_HOTPLUG_CPU
  23225. @@ -1709,11 +1940,21 @@ static struct notifier_block hrtimers_nb = {
  23226. .notifier_call = hrtimer_cpu_notify,
  23227. };
  23228. +#ifdef CONFIG_PREEMPT_RT_BASE
  23229. +static void run_hrtimer_softirq(struct softirq_action *h)
  23230. +{
  23231. + hrtimer_rt_run_pending();
  23232. +}
  23233. +#endif
  23234. +
  23235. void __init hrtimers_init(void)
  23236. {
  23237. hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
  23238. (void *)(long)smp_processor_id());
  23239. register_cpu_notifier(&hrtimers_nb);
  23240. +#ifdef CONFIG_PREEMPT_RT_BASE
  23241. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  23242. +#endif
  23243. }
  23244. /**
  23245. diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
  23246. index 1d5c7204ddc9..184de6751180 100644
  23247. --- a/kernel/time/itimer.c
  23248. +++ b/kernel/time/itimer.c
  23249. @@ -213,6 +213,7 @@ again:
  23250. /* We are sharing ->siglock with it_real_fn() */
  23251. if (hrtimer_try_to_cancel(timer) < 0) {
  23252. spin_unlock_irq(&tsk->sighand->siglock);
  23253. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  23254. goto again;
  23255. }
  23256. expires = timeval_to_ktime(value->it_value);
  23257. diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
  23258. index 347fecf86a3f..2ede47408a3e 100644
  23259. --- a/kernel/time/jiffies.c
  23260. +++ b/kernel/time/jiffies.c
  23261. @@ -74,7 +74,8 @@ static struct clocksource clocksource_jiffies = {
  23262. .max_cycles = 10,
  23263. };
  23264. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  23265. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  23266. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  23267. #if (BITS_PER_LONG < 64)
  23268. u64 get_jiffies_64(void)
  23269. @@ -83,9 +84,9 @@ u64 get_jiffies_64(void)
  23270. u64 ret;
  23271. do {
  23272. - seq = read_seqbegin(&jiffies_lock);
  23273. + seq = read_seqcount_begin(&jiffies_seq);
  23274. ret = jiffies_64;
  23275. - } while (read_seqretry(&jiffies_lock, seq));
  23276. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23277. return ret;
  23278. }
  23279. EXPORT_SYMBOL(get_jiffies_64);
  23280. diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
  23281. index 149cc8086aea..cd925efb239a 100644
  23282. --- a/kernel/time/ntp.c
  23283. +++ b/kernel/time/ntp.c
  23284. @@ -10,6 +10,7 @@
  23285. #include <linux/workqueue.h>
  23286. #include <linux/hrtimer.h>
  23287. #include <linux/jiffies.h>
  23288. +#include <linux/kthread.h>
  23289. #include <linux/math64.h>
  23290. #include <linux/timex.h>
  23291. #include <linux/time.h>
  23292. @@ -562,10 +563,52 @@ static void sync_cmos_clock(struct work_struct *work)
  23293. &sync_cmos_work, timespec64_to_jiffies(&next));
  23294. }
  23295. +#ifdef CONFIG_PREEMPT_RT_FULL
  23296. +/*
  23297. + * RT can not call schedule_delayed_work from real interrupt context.
  23298. + * Need to make a thread to do the real work.
  23299. + */
  23300. +static struct task_struct *cmos_delay_thread;
  23301. +static bool do_cmos_delay;
  23302. +
  23303. +static int run_cmos_delay(void *ignore)
  23304. +{
  23305. + while (!kthread_should_stop()) {
  23306. + set_current_state(TASK_INTERRUPTIBLE);
  23307. + if (do_cmos_delay) {
  23308. + do_cmos_delay = false;
  23309. + queue_delayed_work(system_power_efficient_wq,
  23310. + &sync_cmos_work, 0);
  23311. + }
  23312. + schedule();
  23313. + }
  23314. + __set_current_state(TASK_RUNNING);
  23315. + return 0;
  23316. +}
  23317. +
  23318. +void ntp_notify_cmos_timer(void)
  23319. +{
  23320. + do_cmos_delay = true;
  23321. + /* Make visible before waking up process */
  23322. + smp_wmb();
  23323. + wake_up_process(cmos_delay_thread);
  23324. +}
  23325. +
  23326. +static __init int create_cmos_delay_thread(void)
  23327. +{
  23328. + cmos_delay_thread = kthread_run(run_cmos_delay, NULL, "kcmosdelayd");
  23329. + BUG_ON(!cmos_delay_thread);
  23330. + return 0;
  23331. +}
  23332. +early_initcall(create_cmos_delay_thread);
  23333. +
  23334. +#else
  23335. +
  23336. void ntp_notify_cmos_timer(void)
  23337. {
  23338. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  23339. }
  23340. +#endif /* CONFIG_PREEMPT_RT_FULL */
  23341. #else
  23342. void ntp_notify_cmos_timer(void) { }
  23343. diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
  23344. index 80016b329d94..b7342b6e6a5a 100644
  23345. --- a/kernel/time/posix-cpu-timers.c
  23346. +++ b/kernel/time/posix-cpu-timers.c
  23347. @@ -3,6 +3,7 @@
  23348. */
  23349. #include <linux/sched.h>
  23350. +#include <linux/sched/rt.h>
  23351. #include <linux/posix-timers.h>
  23352. #include <linux/errno.h>
  23353. #include <linux/math64.h>
  23354. @@ -650,7 +651,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  23355. /*
  23356. * Disarm any old timer after extracting its expiry time.
  23357. */
  23358. - WARN_ON_ONCE(!irqs_disabled());
  23359. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23360. ret = 0;
  23361. old_incr = timer->it.cpu.incr;
  23362. @@ -1092,7 +1093,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
  23363. /*
  23364. * Now re-arm for the new expiry time.
  23365. */
  23366. - WARN_ON_ONCE(!irqs_disabled());
  23367. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23368. arm_timer(timer);
  23369. unlock_task_sighand(p, &flags);
  23370. @@ -1183,13 +1184,13 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
  23371. * already updated our counts. We need to check if any timers fire now.
  23372. * Interrupts are disabled.
  23373. */
  23374. -void run_posix_cpu_timers(struct task_struct *tsk)
  23375. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  23376. {
  23377. LIST_HEAD(firing);
  23378. struct k_itimer *timer, *next;
  23379. unsigned long flags;
  23380. - WARN_ON_ONCE(!irqs_disabled());
  23381. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  23382. /*
  23383. * The fast path checks that there are no expired thread or thread
  23384. @@ -1243,6 +1244,190 @@ void run_posix_cpu_timers(struct task_struct *tsk)
  23385. }
  23386. }
  23387. +#ifdef CONFIG_PREEMPT_RT_BASE
  23388. +#include <linux/kthread.h>
  23389. +#include <linux/cpu.h>
  23390. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  23391. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  23392. +
  23393. +static int posix_cpu_timers_thread(void *data)
  23394. +{
  23395. + int cpu = (long)data;
  23396. +
  23397. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  23398. +
  23399. + while (!kthread_should_stop()) {
  23400. + struct task_struct *tsk = NULL;
  23401. + struct task_struct *next = NULL;
  23402. +
  23403. + if (cpu_is_offline(cpu))
  23404. + goto wait_to_die;
  23405. +
  23406. + /* grab task list */
  23407. + raw_local_irq_disable();
  23408. + tsk = per_cpu(posix_timer_tasklist, cpu);
  23409. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23410. + raw_local_irq_enable();
  23411. +
  23412. + /* its possible the list is empty, just return */
  23413. + if (!tsk) {
  23414. + set_current_state(TASK_INTERRUPTIBLE);
  23415. + schedule();
  23416. + __set_current_state(TASK_RUNNING);
  23417. + continue;
  23418. + }
  23419. +
  23420. + /* Process task list */
  23421. + while (1) {
  23422. + /* save next */
  23423. + next = tsk->posix_timer_list;
  23424. +
  23425. + /* run the task timers, clear its ptr and
  23426. + * unreference it
  23427. + */
  23428. + __run_posix_cpu_timers(tsk);
  23429. + tsk->posix_timer_list = NULL;
  23430. + put_task_struct(tsk);
  23431. +
  23432. + /* check if this is the last on the list */
  23433. + if (next == tsk)
  23434. + break;
  23435. + tsk = next;
  23436. + }
  23437. + }
  23438. + return 0;
  23439. +
  23440. +wait_to_die:
  23441. + /* Wait for kthread_stop */
  23442. + set_current_state(TASK_INTERRUPTIBLE);
  23443. + while (!kthread_should_stop()) {
  23444. + schedule();
  23445. + set_current_state(TASK_INTERRUPTIBLE);
  23446. + }
  23447. + __set_current_state(TASK_RUNNING);
  23448. + return 0;
  23449. +}
  23450. +
  23451. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  23452. +{
  23453. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  23454. + if (unlikely(tsk->exit_state))
  23455. + return 0;
  23456. +
  23457. + if (!task_cputime_zero(&tsk->cputime_expires))
  23458. + return 1;
  23459. +
  23460. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  23461. + return 1;
  23462. +
  23463. + return 0;
  23464. +}
  23465. +
  23466. +void run_posix_cpu_timers(struct task_struct *tsk)
  23467. +{
  23468. + unsigned long cpu = smp_processor_id();
  23469. + struct task_struct *tasklist;
  23470. +
  23471. + BUG_ON(!irqs_disabled());
  23472. + if(!per_cpu(posix_timer_task, cpu))
  23473. + return;
  23474. + /* get per-cpu references */
  23475. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  23476. +
  23477. + /* check to see if we're already queued */
  23478. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  23479. + get_task_struct(tsk);
  23480. + if (tasklist) {
  23481. + tsk->posix_timer_list = tasklist;
  23482. + } else {
  23483. + /*
  23484. + * The list is terminated by a self-pointing
  23485. + * task_struct
  23486. + */
  23487. + tsk->posix_timer_list = tsk;
  23488. + }
  23489. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  23490. +
  23491. + wake_up_process(per_cpu(posix_timer_task, cpu));
  23492. + }
  23493. +}
  23494. +
  23495. +/*
  23496. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  23497. + * Here we can start up the necessary migration thread for the new CPU.
  23498. + */
  23499. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  23500. + unsigned long action, void *hcpu)
  23501. +{
  23502. + int cpu = (long)hcpu;
  23503. + struct task_struct *p;
  23504. + struct sched_param param;
  23505. +
  23506. + switch (action) {
  23507. + case CPU_UP_PREPARE:
  23508. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  23509. + "posixcputmr/%d",cpu);
  23510. + if (IS_ERR(p))
  23511. + return NOTIFY_BAD;
  23512. + p->flags |= PF_NOFREEZE;
  23513. + kthread_bind(p, cpu);
  23514. + /* Must be high prio to avoid getting starved */
  23515. + param.sched_priority = MAX_RT_PRIO-1;
  23516. + sched_setscheduler(p, SCHED_FIFO, &param);
  23517. + per_cpu(posix_timer_task,cpu) = p;
  23518. + break;
  23519. + case CPU_ONLINE:
  23520. + /* Strictly unneccessary, as first user will wake it. */
  23521. + wake_up_process(per_cpu(posix_timer_task,cpu));
  23522. + break;
  23523. +#ifdef CONFIG_HOTPLUG_CPU
  23524. + case CPU_UP_CANCELED:
  23525. + /* Unbind it from offline cpu so it can run. Fall thru. */
  23526. + kthread_bind(per_cpu(posix_timer_task, cpu),
  23527. + cpumask_any(cpu_online_mask));
  23528. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23529. + per_cpu(posix_timer_task,cpu) = NULL;
  23530. + break;
  23531. + case CPU_DEAD:
  23532. + kthread_stop(per_cpu(posix_timer_task,cpu));
  23533. + per_cpu(posix_timer_task,cpu) = NULL;
  23534. + break;
  23535. +#endif
  23536. + }
  23537. + return NOTIFY_OK;
  23538. +}
  23539. +
  23540. +/* Register at highest priority so that task migration (migrate_all_tasks)
  23541. + * happens before everything else.
  23542. + */
  23543. +static struct notifier_block posix_cpu_thread_notifier = {
  23544. + .notifier_call = posix_cpu_thread_call,
  23545. + .priority = 10
  23546. +};
  23547. +
  23548. +static int __init posix_cpu_thread_init(void)
  23549. +{
  23550. + void *hcpu = (void *)(long)smp_processor_id();
  23551. + /* Start one for boot CPU. */
  23552. + unsigned long cpu;
  23553. +
  23554. + /* init the per-cpu posix_timer_tasklets */
  23555. + for_each_possible_cpu(cpu)
  23556. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  23557. +
  23558. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  23559. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  23560. + register_cpu_notifier(&posix_cpu_thread_notifier);
  23561. + return 0;
  23562. +}
  23563. +early_initcall(posix_cpu_thread_init);
  23564. +#else /* CONFIG_PREEMPT_RT_BASE */
  23565. +void run_posix_cpu_timers(struct task_struct *tsk)
  23566. +{
  23567. + __run_posix_cpu_timers(tsk);
  23568. +}
  23569. +#endif /* CONFIG_PREEMPT_RT_BASE */
  23570. +
  23571. /*
  23572. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  23573. * The tsk->sighand->siglock must be held by the caller.
  23574. diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
  23575. index f2826c35e918..464a98155a0e 100644
  23576. --- a/kernel/time/posix-timers.c
  23577. +++ b/kernel/time/posix-timers.c
  23578. @@ -506,6 +506,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  23579. static struct pid *good_sigevent(sigevent_t * event)
  23580. {
  23581. struct task_struct *rtn = current->group_leader;
  23582. + int sig = event->sigev_signo;
  23583. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  23584. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  23585. @@ -514,7 +515,8 @@ static struct pid *good_sigevent(sigevent_t * event)
  23586. return NULL;
  23587. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  23588. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  23589. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  23590. + sig_kernel_coredump(sig)))
  23591. return NULL;
  23592. return task_pid(rtn);
  23593. @@ -826,6 +828,20 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  23594. return overrun;
  23595. }
  23596. +/*
  23597. + * Protected by RCU!
  23598. + */
  23599. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  23600. +{
  23601. +#ifdef CONFIG_PREEMPT_RT_FULL
  23602. + if (kc->timer_set == common_timer_set)
  23603. + hrtimer_wait_for_timer(&timr->it.real.timer);
  23604. + else
  23605. + /* FIXME: Whacky hack for posix-cpu-timers */
  23606. + schedule_timeout(1);
  23607. +#endif
  23608. +}
  23609. +
  23610. /* Set a POSIX.1b interval timer. */
  23611. /* timr->it_lock is taken. */
  23612. static int
  23613. @@ -903,6 +919,7 @@ retry:
  23614. if (!timr)
  23615. return -EINVAL;
  23616. + rcu_read_lock();
  23617. kc = clockid_to_kclock(timr->it_clock);
  23618. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  23619. error = -EINVAL;
  23620. @@ -911,9 +928,12 @@ retry:
  23621. unlock_timer(timr, flag);
  23622. if (error == TIMER_RETRY) {
  23623. + timer_wait_for_callback(kc, timr);
  23624. rtn = NULL; // We already got the old time...
  23625. + rcu_read_unlock();
  23626. goto retry;
  23627. }
  23628. + rcu_read_unlock();
  23629. if (old_setting && !error &&
  23630. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  23631. @@ -951,10 +971,15 @@ retry_delete:
  23632. if (!timer)
  23633. return -EINVAL;
  23634. + rcu_read_lock();
  23635. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23636. unlock_timer(timer, flags);
  23637. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23638. + timer);
  23639. + rcu_read_unlock();
  23640. goto retry_delete;
  23641. }
  23642. + rcu_read_unlock();
  23643. spin_lock(&current->sighand->siglock);
  23644. list_del(&timer->list);
  23645. @@ -980,8 +1005,18 @@ static void itimer_delete(struct k_itimer *timer)
  23646. retry_delete:
  23647. spin_lock_irqsave(&timer->it_lock, flags);
  23648. + /* On RT we can race with a deletion */
  23649. + if (!timer->it_signal) {
  23650. + unlock_timer(timer, flags);
  23651. + return;
  23652. + }
  23653. +
  23654. if (timer_delete_hook(timer) == TIMER_RETRY) {
  23655. + rcu_read_lock();
  23656. unlock_timer(timer, flags);
  23657. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  23658. + timer);
  23659. + rcu_read_unlock();
  23660. goto retry_delete;
  23661. }
  23662. list_del(&timer->list);
  23663. diff --git a/kernel/time/tick-broadcast-hrtimer.c b/kernel/time/tick-broadcast-hrtimer.c
  23664. index 53d7184da0be..1b4ac3361c3f 100644
  23665. --- a/kernel/time/tick-broadcast-hrtimer.c
  23666. +++ b/kernel/time/tick-broadcast-hrtimer.c
  23667. @@ -106,5 +106,6 @@ void tick_setup_hrtimer_broadcast(void)
  23668. {
  23669. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23670. bctimer.function = bc_handler;
  23671. + bctimer.irqsafe = true;
  23672. clockevents_register_device(&ce_broadcast_hrtimer);
  23673. }
  23674. diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
  23675. index 4fcd99e12aa0..5a47f2e98faf 100644
  23676. --- a/kernel/time/tick-common.c
  23677. +++ b/kernel/time/tick-common.c
  23678. @@ -79,13 +79,15 @@ int tick_is_oneshot_available(void)
  23679. static void tick_periodic(int cpu)
  23680. {
  23681. if (tick_do_timer_cpu == cpu) {
  23682. - write_seqlock(&jiffies_lock);
  23683. + raw_spin_lock(&jiffies_lock);
  23684. + write_seqcount_begin(&jiffies_seq);
  23685. /* Keep track of the next tick event */
  23686. tick_next_period = ktime_add(tick_next_period, tick_period);
  23687. do_timer(1);
  23688. - write_sequnlock(&jiffies_lock);
  23689. + write_seqcount_end(&jiffies_seq);
  23690. + raw_spin_unlock(&jiffies_lock);
  23691. update_wall_time();
  23692. }
  23693. @@ -157,9 +159,9 @@ void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  23694. ktime_t next;
  23695. do {
  23696. - seq = read_seqbegin(&jiffies_lock);
  23697. + seq = read_seqcount_begin(&jiffies_seq);
  23698. next = tick_next_period;
  23699. - } while (read_seqretry(&jiffies_lock, seq));
  23700. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23701. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  23702. diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
  23703. index 22c57e191a23..d536824cbd36 100644
  23704. --- a/kernel/time/tick-sched.c
  23705. +++ b/kernel/time/tick-sched.c
  23706. @@ -62,7 +62,8 @@ static void tick_do_update_jiffies64(ktime_t now)
  23707. return;
  23708. /* Reevalute with jiffies_lock held */
  23709. - write_seqlock(&jiffies_lock);
  23710. + raw_spin_lock(&jiffies_lock);
  23711. + write_seqcount_begin(&jiffies_seq);
  23712. delta = ktime_sub(now, last_jiffies_update);
  23713. if (delta.tv64 >= tick_period.tv64) {
  23714. @@ -85,10 +86,12 @@ static void tick_do_update_jiffies64(ktime_t now)
  23715. /* Keep the tick_next_period variable up to date */
  23716. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  23717. } else {
  23718. - write_sequnlock(&jiffies_lock);
  23719. + write_seqcount_end(&jiffies_seq);
  23720. + raw_spin_unlock(&jiffies_lock);
  23721. return;
  23722. }
  23723. - write_sequnlock(&jiffies_lock);
  23724. + write_seqcount_end(&jiffies_seq);
  23725. + raw_spin_unlock(&jiffies_lock);
  23726. update_wall_time();
  23727. }
  23728. @@ -99,12 +102,14 @@ static ktime_t tick_init_jiffy_update(void)
  23729. {
  23730. ktime_t period;
  23731. - write_seqlock(&jiffies_lock);
  23732. + raw_spin_lock(&jiffies_lock);
  23733. + write_seqcount_begin(&jiffies_seq);
  23734. /* Did we start the jiffies update yet ? */
  23735. if (last_jiffies_update.tv64 == 0)
  23736. last_jiffies_update = tick_next_period;
  23737. period = last_jiffies_update;
  23738. - write_sequnlock(&jiffies_lock);
  23739. + write_seqcount_end(&jiffies_seq);
  23740. + raw_spin_unlock(&jiffies_lock);
  23741. return period;
  23742. }
  23743. @@ -176,6 +181,11 @@ static bool can_stop_full_tick(void)
  23744. return false;
  23745. }
  23746. + if (!arch_irq_work_has_interrupt()) {
  23747. + trace_tick_stop(0, "missing irq work interrupt\n");
  23748. + return false;
  23749. + }
  23750. +
  23751. /* sched_clock_tick() needs us? */
  23752. #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
  23753. /*
  23754. @@ -204,6 +214,7 @@ static void nohz_full_kick_work_func(struct irq_work *work)
  23755. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  23756. .func = nohz_full_kick_work_func,
  23757. + .flags = IRQ_WORK_HARD_IRQ,
  23758. };
  23759. /*
  23760. @@ -578,10 +589,10 @@ static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  23761. /* Read jiffies and the time when jiffies were updated last */
  23762. do {
  23763. - seq = read_seqbegin(&jiffies_lock);
  23764. + seq = read_seqcount_begin(&jiffies_seq);
  23765. basemono = last_jiffies_update.tv64;
  23766. basejiff = jiffies;
  23767. - } while (read_seqretry(&jiffies_lock, seq));
  23768. + } while (read_seqcount_retry(&jiffies_seq, seq));
  23769. ts->last_jiffies = basejiff;
  23770. if (rcu_needs_cpu(basemono, &next_rcu) ||
  23771. @@ -753,14 +764,7 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  23772. return false;
  23773. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  23774. - static int ratelimit;
  23775. -
  23776. - if (ratelimit < 10 &&
  23777. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  23778. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  23779. - (unsigned int) local_softirq_pending());
  23780. - ratelimit++;
  23781. - }
  23782. + softirq_check_pending_idle();
  23783. return false;
  23784. }
  23785. @@ -1100,6 +1104,7 @@ void tick_setup_sched_timer(void)
  23786. * Emulate tick processing via per-CPU hrtimers:
  23787. */
  23788. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  23789. + ts->sched_timer.irqsafe = 1;
  23790. ts->sched_timer.function = tick_sched_timer;
  23791. /* Get the next period (per cpu) */
  23792. diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
  23793. index 99188ee5d9d0..a1c5c6fc233a 100644
  23794. --- a/kernel/time/timekeeping.c
  23795. +++ b/kernel/time/timekeeping.c
  23796. @@ -2047,8 +2047,10 @@ EXPORT_SYMBOL(hardpps);
  23797. */
  23798. void xtime_update(unsigned long ticks)
  23799. {
  23800. - write_seqlock(&jiffies_lock);
  23801. + raw_spin_lock(&jiffies_lock);
  23802. + write_seqcount_begin(&jiffies_seq);
  23803. do_timer(ticks);
  23804. - write_sequnlock(&jiffies_lock);
  23805. + write_seqcount_end(&jiffies_seq);
  23806. + raw_spin_unlock(&jiffies_lock);
  23807. update_wall_time();
  23808. }
  23809. diff --git a/kernel/time/timekeeping.h b/kernel/time/timekeeping.h
  23810. index 704f595ce83f..763a3e5121ff 100644
  23811. --- a/kernel/time/timekeeping.h
  23812. +++ b/kernel/time/timekeeping.h
  23813. @@ -19,7 +19,8 @@ extern void timekeeping_resume(void);
  23814. extern void do_timer(unsigned long ticks);
  23815. extern void update_wall_time(void);
  23816. -extern seqlock_t jiffies_lock;
  23817. +extern raw_spinlock_t jiffies_lock;
  23818. +extern seqcount_t jiffies_seq;
  23819. #define CS_NAME_LEN 32
  23820. diff --git a/kernel/time/timer.c b/kernel/time/timer.c
  23821. index bbc5d1114583..fee8682c209e 100644
  23822. --- a/kernel/time/timer.c
  23823. +++ b/kernel/time/timer.c
  23824. @@ -80,6 +80,9 @@ struct tvec_root {
  23825. struct tvec_base {
  23826. spinlock_t lock;
  23827. struct timer_list *running_timer;
  23828. +#ifdef CONFIG_PREEMPT_RT_FULL
  23829. + wait_queue_head_t wait_for_running_timer;
  23830. +#endif
  23831. unsigned long timer_jiffies;
  23832. unsigned long next_timer;
  23833. unsigned long active_timers;
  23834. @@ -777,6 +780,39 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer,
  23835. cpu_relax();
  23836. }
  23837. }
  23838. +#ifdef CONFIG_PREEMPT_RT_FULL
  23839. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23840. + struct tvec_base *old,
  23841. + struct tvec_base *new)
  23842. +{
  23843. + /*
  23844. + * We cannot do the below because we might be preempted and
  23845. + * then the preempter would see NULL and loop forever.
  23846. + */
  23847. + if (spin_trylock(&new->lock)) {
  23848. + WRITE_ONCE(timer->flags,
  23849. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23850. + spin_unlock(&old->lock);
  23851. + return new;
  23852. + }
  23853. + return old;
  23854. +}
  23855. +
  23856. +#else
  23857. +static inline struct tvec_base *switch_timer_base(struct timer_list *timer,
  23858. + struct tvec_base *old,
  23859. + struct tvec_base *new)
  23860. +{
  23861. + /* See the comment in lock_timer_base() */
  23862. + timer->flags |= TIMER_MIGRATING;
  23863. +
  23864. + spin_unlock(&old->lock);
  23865. + spin_lock(&new->lock);
  23866. + WRITE_ONCE(timer->flags,
  23867. + (timer->flags & ~TIMER_BASEMASK) | new->cpu);
  23868. + return new;
  23869. +}
  23870. +#endif
  23871. static inline int
  23872. __mod_timer(struct timer_list *timer, unsigned long expires,
  23873. @@ -807,16 +843,8 @@ __mod_timer(struct timer_list *timer, unsigned long expires,
  23874. * handler yet has not finished. This also guarantees that
  23875. * the timer is serialized wrt itself.
  23876. */
  23877. - if (likely(base->running_timer != timer)) {
  23878. - /* See the comment in lock_timer_base() */
  23879. - timer->flags |= TIMER_MIGRATING;
  23880. -
  23881. - spin_unlock(&base->lock);
  23882. - base = new_base;
  23883. - spin_lock(&base->lock);
  23884. - WRITE_ONCE(timer->flags,
  23885. - (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  23886. - }
  23887. + if (likely(base->running_timer != timer))
  23888. + base = switch_timer_base(timer, base, new_base);
  23889. }
  23890. timer->expires = expires;
  23891. @@ -1006,6 +1034,33 @@ void add_timer_on(struct timer_list *timer, int cpu)
  23892. }
  23893. EXPORT_SYMBOL_GPL(add_timer_on);
  23894. +#ifdef CONFIG_PREEMPT_RT_FULL
  23895. +/*
  23896. + * Wait for a running timer
  23897. + */
  23898. +static void wait_for_running_timer(struct timer_list *timer)
  23899. +{
  23900. + struct tvec_base *base;
  23901. + u32 tf = timer->flags;
  23902. +
  23903. + if (tf & TIMER_MIGRATING)
  23904. + return;
  23905. +
  23906. + base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK);
  23907. + wait_event(base->wait_for_running_timer,
  23908. + base->running_timer != timer);
  23909. +}
  23910. +
  23911. +# define wakeup_timer_waiters(b) wake_up(&(b)->wait_for_running_timer)
  23912. +#else
  23913. +static inline void wait_for_running_timer(struct timer_list *timer)
  23914. +{
  23915. + cpu_relax();
  23916. +}
  23917. +
  23918. +# define wakeup_timer_waiters(b) do { } while (0)
  23919. +#endif
  23920. +
  23921. /**
  23922. * del_timer - deactive a timer.
  23923. * @timer: the timer to be deactivated
  23924. @@ -1063,7 +1118,7 @@ int try_to_del_timer_sync(struct timer_list *timer)
  23925. }
  23926. EXPORT_SYMBOL(try_to_del_timer_sync);
  23927. -#ifdef CONFIG_SMP
  23928. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  23929. /**
  23930. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  23931. * @timer: the timer to be deactivated
  23932. @@ -1123,7 +1178,7 @@ int del_timer_sync(struct timer_list *timer)
  23933. int ret = try_to_del_timer_sync(timer);
  23934. if (ret >= 0)
  23935. return ret;
  23936. - cpu_relax();
  23937. + wait_for_running_timer(timer);
  23938. }
  23939. }
  23940. EXPORT_SYMBOL(del_timer_sync);
  23941. @@ -1248,15 +1303,17 @@ static inline void __run_timers(struct tvec_base *base)
  23942. if (irqsafe) {
  23943. spin_unlock(&base->lock);
  23944. call_timer_fn(timer, fn, data);
  23945. + base->running_timer = NULL;
  23946. spin_lock(&base->lock);
  23947. } else {
  23948. spin_unlock_irq(&base->lock);
  23949. call_timer_fn(timer, fn, data);
  23950. + base->running_timer = NULL;
  23951. spin_lock_irq(&base->lock);
  23952. }
  23953. }
  23954. }
  23955. - base->running_timer = NULL;
  23956. + wakeup_timer_waiters(base);
  23957. spin_unlock_irq(&base->lock);
  23958. }
  23959. @@ -1390,6 +1447,14 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  23960. if (cpu_is_offline(smp_processor_id()))
  23961. return expires;
  23962. +#ifdef CONFIG_PREEMPT_RT_FULL
  23963. + /*
  23964. + * On PREEMPT_RT we cannot sleep here. As a result we can't take
  23965. + * the base lock to check when the next timer is pending and so
  23966. + * we assume the next jiffy.
  23967. + */
  23968. + return basem + TICK_NSEC;
  23969. +#endif
  23970. spin_lock(&base->lock);
  23971. if (base->active_timers) {
  23972. if (time_before_eq(base->next_timer, base->timer_jiffies))
  23973. @@ -1416,13 +1481,13 @@ void update_process_times(int user_tick)
  23974. /* Note: this timer irq context must be accounted for as well. */
  23975. account_process_tick(p, user_tick);
  23976. + scheduler_tick();
  23977. run_local_timers();
  23978. rcu_check_callbacks(user_tick);
  23979. -#ifdef CONFIG_IRQ_WORK
  23980. +#if defined(CONFIG_IRQ_WORK)
  23981. if (in_irq())
  23982. irq_work_tick();
  23983. #endif
  23984. - scheduler_tick();
  23985. run_posix_cpu_timers(p);
  23986. }
  23987. @@ -1433,6 +1498,8 @@ static void run_timer_softirq(struct softirq_action *h)
  23988. {
  23989. struct tvec_base *base = this_cpu_ptr(&tvec_bases);
  23990. + irq_work_tick_soft();
  23991. +
  23992. if (time_after_eq(jiffies, base->timer_jiffies))
  23993. __run_timers(base);
  23994. }
  23995. @@ -1589,7 +1656,7 @@ static void migrate_timers(int cpu)
  23996. BUG_ON(cpu_online(cpu));
  23997. old_base = per_cpu_ptr(&tvec_bases, cpu);
  23998. - new_base = get_cpu_ptr(&tvec_bases);
  23999. + new_base = get_local_ptr(&tvec_bases);
  24000. /*
  24001. * The caller is globally serialized and nobody else
  24002. * takes two locks at once, deadlock is not possible.
  24003. @@ -1613,7 +1680,7 @@ static void migrate_timers(int cpu)
  24004. spin_unlock(&old_base->lock);
  24005. spin_unlock_irq(&new_base->lock);
  24006. - put_cpu_ptr(&tvec_bases);
  24007. + put_local_ptr(&tvec_bases);
  24008. }
  24009. static int timer_cpu_notify(struct notifier_block *self,
  24010. @@ -1645,6 +1712,9 @@ static void __init init_timer_cpu(int cpu)
  24011. base->cpu = cpu;
  24012. spin_lock_init(&base->lock);
  24013. +#ifdef CONFIG_PREEMPT_RT_FULL
  24014. + init_waitqueue_head(&base->wait_for_running_timer);
  24015. +#endif
  24016. base->timer_jiffies = jiffies;
  24017. base->next_timer = base->timer_jiffies;
  24018. diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
  24019. index e45db6b0d878..364ccd0eb57b 100644
  24020. --- a/kernel/trace/Kconfig
  24021. +++ b/kernel/trace/Kconfig
  24022. @@ -187,6 +187,24 @@ config IRQSOFF_TRACER
  24023. enabled. This option and the preempt-off timing option can be
  24024. used together or separately.)
  24025. +config INTERRUPT_OFF_HIST
  24026. + bool "Interrupts-off Latency Histogram"
  24027. + depends on IRQSOFF_TRACER
  24028. + help
  24029. + This option generates continuously updated histograms (one per cpu)
  24030. + of the duration of time periods with interrupts disabled. The
  24031. + histograms are disabled by default. To enable them, write a non-zero
  24032. + number to
  24033. +
  24034. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  24035. +
  24036. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  24037. + per cpu) are generated that accumulate the duration of time periods
  24038. + when both interrupts and preemption are disabled. The histogram data
  24039. + will be located in the debug file system at
  24040. +
  24041. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  24042. +
  24043. config PREEMPT_TRACER
  24044. bool "Preemption-off Latency Tracer"
  24045. default n
  24046. @@ -211,6 +229,24 @@ config PREEMPT_TRACER
  24047. enabled. This option and the irqs-off timing option can be
  24048. used together or separately.)
  24049. +config PREEMPT_OFF_HIST
  24050. + bool "Preemption-off Latency Histogram"
  24051. + depends on PREEMPT_TRACER
  24052. + help
  24053. + This option generates continuously updated histograms (one per cpu)
  24054. + of the duration of time periods with preemption disabled. The
  24055. + histograms are disabled by default. To enable them, write a non-zero
  24056. + number to
  24057. +
  24058. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  24059. +
  24060. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  24061. + per cpu) are generated that accumulate the duration of time periods
  24062. + when both interrupts and preemption are disabled. The histogram data
  24063. + will be located in the debug file system at
  24064. +
  24065. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  24066. +
  24067. config SCHED_TRACER
  24068. bool "Scheduling Latency Tracer"
  24069. select GENERIC_TRACER
  24070. @@ -221,6 +257,74 @@ config SCHED_TRACER
  24071. This tracer tracks the latency of the highest priority task
  24072. to be scheduled in, starting from the point it has woken up.
  24073. +config WAKEUP_LATENCY_HIST
  24074. + bool "Scheduling Latency Histogram"
  24075. + depends on SCHED_TRACER
  24076. + help
  24077. + This option generates continuously updated histograms (one per cpu)
  24078. + of the scheduling latency of the highest priority task.
  24079. + The histograms are disabled by default. To enable them, write a
  24080. + non-zero number to
  24081. +
  24082. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  24083. +
  24084. + Two different algorithms are used, one to determine the latency of
  24085. + processes that exclusively use the highest priority of the system and
  24086. + another one to determine the latency of processes that share the
  24087. + highest system priority with other processes. The former is used to
  24088. + improve hardware and system software, the latter to optimize the
  24089. + priority design of a given system. The histogram data will be
  24090. + located in the debug file system at
  24091. +
  24092. + /sys/kernel/debug/tracing/latency_hist/wakeup
  24093. +
  24094. + and
  24095. +
  24096. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  24097. +
  24098. + If both Scheduling Latency Histogram and Missed Timer Offsets
  24099. + Histogram are selected, additional histogram data will be collected
  24100. + that contain, in addition to the wakeup latency, the timer latency, in
  24101. + case the wakeup was triggered by an expired timer. These histograms
  24102. + are available in the
  24103. +
  24104. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  24105. +
  24106. + directory. They reflect the apparent interrupt and scheduling latency
  24107. + and are best suitable to determine the worst-case latency of a given
  24108. + system. To enable these histograms, write a non-zero number to
  24109. +
  24110. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  24111. +
  24112. +config MISSED_TIMER_OFFSETS_HIST
  24113. + depends on HIGH_RES_TIMERS
  24114. + select GENERIC_TRACER
  24115. + bool "Missed Timer Offsets Histogram"
  24116. + help
  24117. + Generate a histogram of missed timer offsets in microseconds. The
  24118. + histograms are disabled by default. To enable them, write a non-zero
  24119. + number to
  24120. +
  24121. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  24122. +
  24123. + The histogram data will be located in the debug file system at
  24124. +
  24125. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  24126. +
  24127. + If both Scheduling Latency Histogram and Missed Timer Offsets
  24128. + Histogram are selected, additional histogram data will be collected
  24129. + that contain, in addition to the wakeup latency, the timer latency, in
  24130. + case the wakeup was triggered by an expired timer. These histograms
  24131. + are available in the
  24132. +
  24133. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  24134. +
  24135. + directory. They reflect the apparent interrupt and scheduling latency
  24136. + and are best suitable to determine the worst-case latency of a given
  24137. + system. To enable these histograms, write a non-zero number to
  24138. +
  24139. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  24140. +
  24141. config ENABLE_DEFAULT_TRACERS
  24142. bool "Trace process context switches and events"
  24143. depends on !GENERIC_TRACER
  24144. diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
  24145. index 9b1044e936a6..3bbaea06824a 100644
  24146. --- a/kernel/trace/Makefile
  24147. +++ b/kernel/trace/Makefile
  24148. @@ -36,6 +36,10 @@ obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
  24149. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  24150. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  24151. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  24152. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  24153. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  24154. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  24155. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  24156. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  24157. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  24158. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  24159. diff --git a/kernel/trace/latency_hist.c b/kernel/trace/latency_hist.c
  24160. new file mode 100644
  24161. index 000000000000..7f6ee70dea41
  24162. --- /dev/null
  24163. +++ b/kernel/trace/latency_hist.c
  24164. @@ -0,0 +1,1178 @@
  24165. +/*
  24166. + * kernel/trace/latency_hist.c
  24167. + *
  24168. + * Add support for histograms of preemption-off latency and
  24169. + * interrupt-off latency and wakeup latency, it depends on
  24170. + * Real-Time Preemption Support.
  24171. + *
  24172. + * Copyright (C) 2005 MontaVista Software, Inc.
  24173. + * Yi Yang <yyang@ch.mvista.com>
  24174. + *
  24175. + * Converted to work with the new latency tracer.
  24176. + * Copyright (C) 2008 Red Hat, Inc.
  24177. + * Steven Rostedt <srostedt@redhat.com>
  24178. + *
  24179. + */
  24180. +#include <linux/module.h>
  24181. +#include <linux/debugfs.h>
  24182. +#include <linux/seq_file.h>
  24183. +#include <linux/percpu.h>
  24184. +#include <linux/kallsyms.h>
  24185. +#include <linux/uaccess.h>
  24186. +#include <linux/sched.h>
  24187. +#include <linux/sched/rt.h>
  24188. +#include <linux/slab.h>
  24189. +#include <linux/atomic.h>
  24190. +#include <asm/div64.h>
  24191. +
  24192. +#include "trace.h"
  24193. +#include <trace/events/sched.h>
  24194. +
  24195. +#define NSECS_PER_USECS 1000L
  24196. +
  24197. +#define CREATE_TRACE_POINTS
  24198. +#include <trace/events/hist.h>
  24199. +
  24200. +enum {
  24201. + IRQSOFF_LATENCY = 0,
  24202. + PREEMPTOFF_LATENCY,
  24203. + PREEMPTIRQSOFF_LATENCY,
  24204. + WAKEUP_LATENCY,
  24205. + WAKEUP_LATENCY_SHAREDPRIO,
  24206. + MISSED_TIMER_OFFSETS,
  24207. + TIMERANDWAKEUP_LATENCY,
  24208. + MAX_LATENCY_TYPE,
  24209. +};
  24210. +
  24211. +#define MAX_ENTRY_NUM 10240
  24212. +
  24213. +struct hist_data {
  24214. + atomic_t hist_mode; /* 0 log, 1 don't log */
  24215. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  24216. + long min_lat;
  24217. + long max_lat;
  24218. + unsigned long long below_hist_bound_samples;
  24219. + unsigned long long above_hist_bound_samples;
  24220. + long long accumulate_lat;
  24221. + unsigned long long total_samples;
  24222. + unsigned long long hist_array[MAX_ENTRY_NUM];
  24223. +};
  24224. +
  24225. +struct enable_data {
  24226. + int latency_type;
  24227. + int enabled;
  24228. +};
  24229. +
  24230. +static char *latency_hist_dir_root = "latency_hist";
  24231. +
  24232. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24233. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  24234. +static char *irqsoff_hist_dir = "irqsoff";
  24235. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  24236. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  24237. +#endif
  24238. +
  24239. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24240. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  24241. +static char *preemptoff_hist_dir = "preemptoff";
  24242. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  24243. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  24244. +#endif
  24245. +
  24246. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  24247. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  24248. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  24249. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  24250. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  24251. +#endif
  24252. +
  24253. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  24254. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  24255. +static struct enable_data preemptirqsoff_enabled_data = {
  24256. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  24257. + .enabled = 0,
  24258. +};
  24259. +#endif
  24260. +
  24261. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24262. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24263. +struct maxlatproc_data {
  24264. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  24265. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  24266. + int pid;
  24267. + int current_pid;
  24268. + int prio;
  24269. + int current_prio;
  24270. + long latency;
  24271. + long timeroffset;
  24272. + cycle_t timestamp;
  24273. +};
  24274. +#endif
  24275. +
  24276. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24277. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  24278. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  24279. +static char *wakeup_latency_hist_dir = "wakeup";
  24280. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  24281. +static notrace void probe_wakeup_latency_hist_start(void *v,
  24282. + struct task_struct *p);
  24283. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  24284. + bool preempt, struct task_struct *prev, struct task_struct *next);
  24285. +static notrace void probe_sched_migrate_task(void *,
  24286. + struct task_struct *task, int cpu);
  24287. +static struct enable_data wakeup_latency_enabled_data = {
  24288. + .latency_type = WAKEUP_LATENCY,
  24289. + .enabled = 0,
  24290. +};
  24291. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  24292. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  24293. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  24294. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  24295. +static unsigned long wakeup_pid;
  24296. +#endif
  24297. +
  24298. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24299. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  24300. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  24301. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  24302. + long long offset, struct task_struct *curr, struct task_struct *task);
  24303. +static struct enable_data missed_timer_offsets_enabled_data = {
  24304. + .latency_type = MISSED_TIMER_OFFSETS,
  24305. + .enabled = 0,
  24306. +};
  24307. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  24308. +static unsigned long missed_timer_offsets_pid;
  24309. +#endif
  24310. +
  24311. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24312. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24313. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  24314. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  24315. +static struct enable_data timerandwakeup_enabled_data = {
  24316. + .latency_type = TIMERANDWAKEUP_LATENCY,
  24317. + .enabled = 0,
  24318. +};
  24319. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  24320. +#endif
  24321. +
  24322. +void notrace latency_hist(int latency_type, int cpu, long latency,
  24323. + long timeroffset, cycle_t stop,
  24324. + struct task_struct *p)
  24325. +{
  24326. + struct hist_data *my_hist;
  24327. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24328. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24329. + struct maxlatproc_data *mp = NULL;
  24330. +#endif
  24331. +
  24332. + if (!cpu_possible(cpu) || latency_type < 0 ||
  24333. + latency_type >= MAX_LATENCY_TYPE)
  24334. + return;
  24335. +
  24336. + switch (latency_type) {
  24337. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24338. + case IRQSOFF_LATENCY:
  24339. + my_hist = &per_cpu(irqsoff_hist, cpu);
  24340. + break;
  24341. +#endif
  24342. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24343. + case PREEMPTOFF_LATENCY:
  24344. + my_hist = &per_cpu(preemptoff_hist, cpu);
  24345. + break;
  24346. +#endif
  24347. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  24348. + case PREEMPTIRQSOFF_LATENCY:
  24349. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  24350. + break;
  24351. +#endif
  24352. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24353. + case WAKEUP_LATENCY:
  24354. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  24355. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24356. + break;
  24357. + case WAKEUP_LATENCY_SHAREDPRIO:
  24358. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24359. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24360. + break;
  24361. +#endif
  24362. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24363. + case MISSED_TIMER_OFFSETS:
  24364. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  24365. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24366. + break;
  24367. +#endif
  24368. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24369. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24370. + case TIMERANDWAKEUP_LATENCY:
  24371. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24372. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24373. + break;
  24374. +#endif
  24375. +
  24376. + default:
  24377. + return;
  24378. + }
  24379. +
  24380. + latency += my_hist->offset;
  24381. +
  24382. + if (atomic_read(&my_hist->hist_mode) == 0)
  24383. + return;
  24384. +
  24385. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  24386. + if (latency < 0)
  24387. + my_hist->below_hist_bound_samples++;
  24388. + else
  24389. + my_hist->above_hist_bound_samples++;
  24390. + } else
  24391. + my_hist->hist_array[latency]++;
  24392. +
  24393. + if (unlikely(latency > my_hist->max_lat ||
  24394. + my_hist->min_lat == LONG_MAX)) {
  24395. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24396. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24397. + if (latency_type == WAKEUP_LATENCY ||
  24398. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24399. + latency_type == MISSED_TIMER_OFFSETS ||
  24400. + latency_type == TIMERANDWAKEUP_LATENCY) {
  24401. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  24402. + strncpy(mp->current_comm, current->comm,
  24403. + sizeof(mp->current_comm));
  24404. + mp->pid = task_pid_nr(p);
  24405. + mp->current_pid = task_pid_nr(current);
  24406. + mp->prio = p->prio;
  24407. + mp->current_prio = current->prio;
  24408. + mp->latency = latency;
  24409. + mp->timeroffset = timeroffset;
  24410. + mp->timestamp = stop;
  24411. + }
  24412. +#endif
  24413. + my_hist->max_lat = latency;
  24414. + }
  24415. + if (unlikely(latency < my_hist->min_lat))
  24416. + my_hist->min_lat = latency;
  24417. + my_hist->total_samples++;
  24418. + my_hist->accumulate_lat += latency;
  24419. +}
  24420. +
  24421. +static void *l_start(struct seq_file *m, loff_t *pos)
  24422. +{
  24423. + loff_t *index_ptr = NULL;
  24424. + loff_t index = *pos;
  24425. + struct hist_data *my_hist = m->private;
  24426. +
  24427. + if (index == 0) {
  24428. + char minstr[32], avgstr[32], maxstr[32];
  24429. +
  24430. + atomic_dec(&my_hist->hist_mode);
  24431. +
  24432. + if (likely(my_hist->total_samples)) {
  24433. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  24434. + my_hist->total_samples);
  24435. + snprintf(minstr, sizeof(minstr), "%ld",
  24436. + my_hist->min_lat - my_hist->offset);
  24437. + snprintf(avgstr, sizeof(avgstr), "%ld",
  24438. + avg - my_hist->offset);
  24439. + snprintf(maxstr, sizeof(maxstr), "%ld",
  24440. + my_hist->max_lat - my_hist->offset);
  24441. + } else {
  24442. + strcpy(minstr, "<undef>");
  24443. + strcpy(avgstr, minstr);
  24444. + strcpy(maxstr, minstr);
  24445. + }
  24446. +
  24447. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  24448. + "#Average latency: %s microseconds\n"
  24449. + "#Maximum latency: %s microseconds\n"
  24450. + "#Total samples: %llu\n"
  24451. + "#There are %llu samples lower than %ld"
  24452. + " microseconds.\n"
  24453. + "#There are %llu samples greater or equal"
  24454. + " than %ld microseconds.\n"
  24455. + "#usecs\t%16s\n",
  24456. + minstr, avgstr, maxstr,
  24457. + my_hist->total_samples,
  24458. + my_hist->below_hist_bound_samples,
  24459. + -my_hist->offset,
  24460. + my_hist->above_hist_bound_samples,
  24461. + MAX_ENTRY_NUM - my_hist->offset,
  24462. + "samples");
  24463. + }
  24464. + if (index < MAX_ENTRY_NUM) {
  24465. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  24466. + if (index_ptr)
  24467. + *index_ptr = index;
  24468. + }
  24469. +
  24470. + return index_ptr;
  24471. +}
  24472. +
  24473. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  24474. +{
  24475. + loff_t *index_ptr = p;
  24476. + struct hist_data *my_hist = m->private;
  24477. +
  24478. + if (++*pos >= MAX_ENTRY_NUM) {
  24479. + atomic_inc(&my_hist->hist_mode);
  24480. + return NULL;
  24481. + }
  24482. + *index_ptr = *pos;
  24483. + return index_ptr;
  24484. +}
  24485. +
  24486. +static void l_stop(struct seq_file *m, void *p)
  24487. +{
  24488. + kfree(p);
  24489. +}
  24490. +
  24491. +static int l_show(struct seq_file *m, void *p)
  24492. +{
  24493. + int index = *(loff_t *) p;
  24494. + struct hist_data *my_hist = m->private;
  24495. +
  24496. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  24497. + my_hist->hist_array[index]);
  24498. + return 0;
  24499. +}
  24500. +
  24501. +static const struct seq_operations latency_hist_seq_op = {
  24502. + .start = l_start,
  24503. + .next = l_next,
  24504. + .stop = l_stop,
  24505. + .show = l_show
  24506. +};
  24507. +
  24508. +static int latency_hist_open(struct inode *inode, struct file *file)
  24509. +{
  24510. + int ret;
  24511. +
  24512. + ret = seq_open(file, &latency_hist_seq_op);
  24513. + if (!ret) {
  24514. + struct seq_file *seq = file->private_data;
  24515. + seq->private = inode->i_private;
  24516. + }
  24517. + return ret;
  24518. +}
  24519. +
  24520. +static const struct file_operations latency_hist_fops = {
  24521. + .open = latency_hist_open,
  24522. + .read = seq_read,
  24523. + .llseek = seq_lseek,
  24524. + .release = seq_release,
  24525. +};
  24526. +
  24527. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24528. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24529. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  24530. +{
  24531. + mp->comm[0] = mp->current_comm[0] = '\0';
  24532. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  24533. + mp->latency = mp->timeroffset = -1;
  24534. + mp->timestamp = 0;
  24535. +}
  24536. +#endif
  24537. +
  24538. +static void hist_reset(struct hist_data *hist)
  24539. +{
  24540. + atomic_dec(&hist->hist_mode);
  24541. +
  24542. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  24543. + hist->below_hist_bound_samples = 0ULL;
  24544. + hist->above_hist_bound_samples = 0ULL;
  24545. + hist->min_lat = LONG_MAX;
  24546. + hist->max_lat = LONG_MIN;
  24547. + hist->total_samples = 0ULL;
  24548. + hist->accumulate_lat = 0LL;
  24549. +
  24550. + atomic_inc(&hist->hist_mode);
  24551. +}
  24552. +
  24553. +static ssize_t
  24554. +latency_hist_reset(struct file *file, const char __user *a,
  24555. + size_t size, loff_t *off)
  24556. +{
  24557. + int cpu;
  24558. + struct hist_data *hist = NULL;
  24559. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24560. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24561. + struct maxlatproc_data *mp = NULL;
  24562. +#endif
  24563. + off_t latency_type = (off_t) file->private_data;
  24564. +
  24565. + for_each_online_cpu(cpu) {
  24566. +
  24567. + switch (latency_type) {
  24568. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24569. + case PREEMPTOFF_LATENCY:
  24570. + hist = &per_cpu(preemptoff_hist, cpu);
  24571. + break;
  24572. +#endif
  24573. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24574. + case IRQSOFF_LATENCY:
  24575. + hist = &per_cpu(irqsoff_hist, cpu);
  24576. + break;
  24577. +#endif
  24578. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24579. + case PREEMPTIRQSOFF_LATENCY:
  24580. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  24581. + break;
  24582. +#endif
  24583. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24584. + case WAKEUP_LATENCY:
  24585. + hist = &per_cpu(wakeup_latency_hist, cpu);
  24586. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  24587. + break;
  24588. + case WAKEUP_LATENCY_SHAREDPRIO:
  24589. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  24590. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  24591. + break;
  24592. +#endif
  24593. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24594. + case MISSED_TIMER_OFFSETS:
  24595. + hist = &per_cpu(missed_timer_offsets, cpu);
  24596. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  24597. + break;
  24598. +#endif
  24599. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24600. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24601. + case TIMERANDWAKEUP_LATENCY:
  24602. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  24603. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  24604. + break;
  24605. +#endif
  24606. + }
  24607. +
  24608. + hist_reset(hist);
  24609. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24610. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24611. + if (latency_type == WAKEUP_LATENCY ||
  24612. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  24613. + latency_type == MISSED_TIMER_OFFSETS ||
  24614. + latency_type == TIMERANDWAKEUP_LATENCY)
  24615. + clear_maxlatprocdata(mp);
  24616. +#endif
  24617. + }
  24618. +
  24619. + return size;
  24620. +}
  24621. +
  24622. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24623. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24624. +static ssize_t
  24625. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24626. +{
  24627. + char buf[64];
  24628. + int r;
  24629. + unsigned long *this_pid = file->private_data;
  24630. +
  24631. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  24632. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24633. +}
  24634. +
  24635. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  24636. + size_t cnt, loff_t *ppos)
  24637. +{
  24638. + char buf[64];
  24639. + unsigned long pid;
  24640. + unsigned long *this_pid = file->private_data;
  24641. +
  24642. + if (cnt >= sizeof(buf))
  24643. + return -EINVAL;
  24644. +
  24645. + if (copy_from_user(&buf, ubuf, cnt))
  24646. + return -EFAULT;
  24647. +
  24648. + buf[cnt] = '\0';
  24649. +
  24650. + if (kstrtoul(buf, 10, &pid))
  24651. + return -EINVAL;
  24652. +
  24653. + *this_pid = pid;
  24654. +
  24655. + return cnt;
  24656. +}
  24657. +#endif
  24658. +
  24659. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24660. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24661. +static ssize_t
  24662. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24663. +{
  24664. + int r;
  24665. + struct maxlatproc_data *mp = file->private_data;
  24666. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  24667. + unsigned long long t;
  24668. + unsigned long usecs, secs;
  24669. + char *buf;
  24670. +
  24671. + if (mp->pid == -1 || mp->current_pid == -1) {
  24672. + buf = "(none)\n";
  24673. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  24674. + strlen(buf));
  24675. + }
  24676. +
  24677. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  24678. + if (buf == NULL)
  24679. + return -ENOMEM;
  24680. +
  24681. + t = ns2usecs(mp->timestamp);
  24682. + usecs = do_div(t, USEC_PER_SEC);
  24683. + secs = (unsigned long) t;
  24684. + r = snprintf(buf, strmaxlen,
  24685. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  24686. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  24687. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  24688. + secs, usecs);
  24689. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24690. + kfree(buf);
  24691. + return r;
  24692. +}
  24693. +#endif
  24694. +
  24695. +static ssize_t
  24696. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  24697. +{
  24698. + char buf[64];
  24699. + struct enable_data *ed = file->private_data;
  24700. + int r;
  24701. +
  24702. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  24703. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  24704. +}
  24705. +
  24706. +static ssize_t
  24707. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  24708. +{
  24709. + char buf[64];
  24710. + long enable;
  24711. + struct enable_data *ed = file->private_data;
  24712. +
  24713. + if (cnt >= sizeof(buf))
  24714. + return -EINVAL;
  24715. +
  24716. + if (copy_from_user(&buf, ubuf, cnt))
  24717. + return -EFAULT;
  24718. +
  24719. + buf[cnt] = 0;
  24720. +
  24721. + if (kstrtoul(buf, 10, &enable))
  24722. + return -EINVAL;
  24723. +
  24724. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  24725. + return cnt;
  24726. +
  24727. + if (enable) {
  24728. + int ret;
  24729. +
  24730. + switch (ed->latency_type) {
  24731. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24732. + case PREEMPTIRQSOFF_LATENCY:
  24733. + ret = register_trace_preemptirqsoff_hist(
  24734. + probe_preemptirqsoff_hist, NULL);
  24735. + if (ret) {
  24736. + pr_info("wakeup trace: Couldn't assign "
  24737. + "probe_preemptirqsoff_hist "
  24738. + "to trace_preemptirqsoff_hist\n");
  24739. + return ret;
  24740. + }
  24741. + break;
  24742. +#endif
  24743. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24744. + case WAKEUP_LATENCY:
  24745. + ret = register_trace_sched_wakeup(
  24746. + probe_wakeup_latency_hist_start, NULL);
  24747. + if (ret) {
  24748. + pr_info("wakeup trace: Couldn't assign "
  24749. + "probe_wakeup_latency_hist_start "
  24750. + "to trace_sched_wakeup\n");
  24751. + return ret;
  24752. + }
  24753. + ret = register_trace_sched_wakeup_new(
  24754. + probe_wakeup_latency_hist_start, NULL);
  24755. + if (ret) {
  24756. + pr_info("wakeup trace: Couldn't assign "
  24757. + "probe_wakeup_latency_hist_start "
  24758. + "to trace_sched_wakeup_new\n");
  24759. + unregister_trace_sched_wakeup(
  24760. + probe_wakeup_latency_hist_start, NULL);
  24761. + return ret;
  24762. + }
  24763. + ret = register_trace_sched_switch(
  24764. + probe_wakeup_latency_hist_stop, NULL);
  24765. + if (ret) {
  24766. + pr_info("wakeup trace: Couldn't assign "
  24767. + "probe_wakeup_latency_hist_stop "
  24768. + "to trace_sched_switch\n");
  24769. + unregister_trace_sched_wakeup(
  24770. + probe_wakeup_latency_hist_start, NULL);
  24771. + unregister_trace_sched_wakeup_new(
  24772. + probe_wakeup_latency_hist_start, NULL);
  24773. + return ret;
  24774. + }
  24775. + ret = register_trace_sched_migrate_task(
  24776. + probe_sched_migrate_task, NULL);
  24777. + if (ret) {
  24778. + pr_info("wakeup trace: Couldn't assign "
  24779. + "probe_sched_migrate_task "
  24780. + "to trace_sched_migrate_task\n");
  24781. + unregister_trace_sched_wakeup(
  24782. + probe_wakeup_latency_hist_start, NULL);
  24783. + unregister_trace_sched_wakeup_new(
  24784. + probe_wakeup_latency_hist_start, NULL);
  24785. + unregister_trace_sched_switch(
  24786. + probe_wakeup_latency_hist_stop, NULL);
  24787. + return ret;
  24788. + }
  24789. + break;
  24790. +#endif
  24791. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24792. + case MISSED_TIMER_OFFSETS:
  24793. + ret = register_trace_hrtimer_interrupt(
  24794. + probe_hrtimer_interrupt, NULL);
  24795. + if (ret) {
  24796. + pr_info("wakeup trace: Couldn't assign "
  24797. + "probe_hrtimer_interrupt "
  24798. + "to trace_hrtimer_interrupt\n");
  24799. + return ret;
  24800. + }
  24801. + break;
  24802. +#endif
  24803. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  24804. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24805. + case TIMERANDWAKEUP_LATENCY:
  24806. + if (!wakeup_latency_enabled_data.enabled ||
  24807. + !missed_timer_offsets_enabled_data.enabled)
  24808. + return -EINVAL;
  24809. + break;
  24810. +#endif
  24811. + default:
  24812. + break;
  24813. + }
  24814. + } else {
  24815. + switch (ed->latency_type) {
  24816. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24817. + case PREEMPTIRQSOFF_LATENCY:
  24818. + {
  24819. + int cpu;
  24820. +
  24821. + unregister_trace_preemptirqsoff_hist(
  24822. + probe_preemptirqsoff_hist, NULL);
  24823. + for_each_online_cpu(cpu) {
  24824. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24825. + per_cpu(hist_irqsoff_counting,
  24826. + cpu) = 0;
  24827. +#endif
  24828. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24829. + per_cpu(hist_preemptoff_counting,
  24830. + cpu) = 0;
  24831. +#endif
  24832. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24833. + per_cpu(hist_preemptirqsoff_counting,
  24834. + cpu) = 0;
  24835. +#endif
  24836. + }
  24837. + }
  24838. + break;
  24839. +#endif
  24840. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24841. + case WAKEUP_LATENCY:
  24842. + {
  24843. + int cpu;
  24844. +
  24845. + unregister_trace_sched_wakeup(
  24846. + probe_wakeup_latency_hist_start, NULL);
  24847. + unregister_trace_sched_wakeup_new(
  24848. + probe_wakeup_latency_hist_start, NULL);
  24849. + unregister_trace_sched_switch(
  24850. + probe_wakeup_latency_hist_stop, NULL);
  24851. + unregister_trace_sched_migrate_task(
  24852. + probe_sched_migrate_task, NULL);
  24853. +
  24854. + for_each_online_cpu(cpu) {
  24855. + per_cpu(wakeup_task, cpu) = NULL;
  24856. + per_cpu(wakeup_sharedprio, cpu) = 0;
  24857. + }
  24858. + }
  24859. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24860. + timerandwakeup_enabled_data.enabled = 0;
  24861. +#endif
  24862. + break;
  24863. +#endif
  24864. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  24865. + case MISSED_TIMER_OFFSETS:
  24866. + unregister_trace_hrtimer_interrupt(
  24867. + probe_hrtimer_interrupt, NULL);
  24868. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  24869. + timerandwakeup_enabled_data.enabled = 0;
  24870. +#endif
  24871. + break;
  24872. +#endif
  24873. + default:
  24874. + break;
  24875. + }
  24876. + }
  24877. + ed->enabled = enable;
  24878. + return cnt;
  24879. +}
  24880. +
  24881. +static const struct file_operations latency_hist_reset_fops = {
  24882. + .open = tracing_open_generic,
  24883. + .write = latency_hist_reset,
  24884. +};
  24885. +
  24886. +static const struct file_operations enable_fops = {
  24887. + .open = tracing_open_generic,
  24888. + .read = show_enable,
  24889. + .write = do_enable,
  24890. +};
  24891. +
  24892. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  24893. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  24894. +static const struct file_operations pid_fops = {
  24895. + .open = tracing_open_generic,
  24896. + .read = show_pid,
  24897. + .write = do_pid,
  24898. +};
  24899. +
  24900. +static const struct file_operations maxlatproc_fops = {
  24901. + .open = tracing_open_generic,
  24902. + .read = show_maxlatproc,
  24903. +};
  24904. +#endif
  24905. +
  24906. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  24907. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  24908. + int starthist)
  24909. +{
  24910. + int cpu = raw_smp_processor_id();
  24911. + int time_set = 0;
  24912. +
  24913. + if (starthist) {
  24914. + cycle_t uninitialized_var(start);
  24915. +
  24916. + if (!preempt_count() && !irqs_disabled())
  24917. + return;
  24918. +
  24919. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24920. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  24921. + !per_cpu(hist_irqsoff_counting, cpu)) {
  24922. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  24923. + start = ftrace_now(cpu);
  24924. + time_set++;
  24925. + per_cpu(hist_irqsoff_start, cpu) = start;
  24926. + }
  24927. +#endif
  24928. +
  24929. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24930. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  24931. + !per_cpu(hist_preemptoff_counting, cpu)) {
  24932. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  24933. + if (!(time_set++))
  24934. + start = ftrace_now(cpu);
  24935. + per_cpu(hist_preemptoff_start, cpu) = start;
  24936. + }
  24937. +#endif
  24938. +
  24939. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24940. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  24941. + per_cpu(hist_preemptoff_counting, cpu) &&
  24942. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24943. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  24944. + if (!time_set)
  24945. + start = ftrace_now(cpu);
  24946. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  24947. + }
  24948. +#endif
  24949. + } else {
  24950. + cycle_t uninitialized_var(stop);
  24951. +
  24952. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  24953. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  24954. + per_cpu(hist_irqsoff_counting, cpu)) {
  24955. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  24956. +
  24957. + stop = ftrace_now(cpu);
  24958. + time_set++;
  24959. + if (start) {
  24960. + long latency = ((long) (stop - start)) /
  24961. + NSECS_PER_USECS;
  24962. +
  24963. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  24964. + stop, NULL);
  24965. + }
  24966. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  24967. + }
  24968. +#endif
  24969. +
  24970. +#ifdef CONFIG_PREEMPT_OFF_HIST
  24971. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  24972. + per_cpu(hist_preemptoff_counting, cpu)) {
  24973. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  24974. +
  24975. + if (!(time_set++))
  24976. + stop = ftrace_now(cpu);
  24977. + if (start) {
  24978. + long latency = ((long) (stop - start)) /
  24979. + NSECS_PER_USECS;
  24980. +
  24981. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  24982. + 0, stop, NULL);
  24983. + }
  24984. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  24985. + }
  24986. +#endif
  24987. +
  24988. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  24989. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  24990. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  24991. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  24992. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  24993. +
  24994. + if (!time_set)
  24995. + stop = ftrace_now(cpu);
  24996. + if (start) {
  24997. + long latency = ((long) (stop - start)) /
  24998. + NSECS_PER_USECS;
  24999. +
  25000. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  25001. + latency, 0, stop, NULL);
  25002. + }
  25003. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  25004. + }
  25005. +#endif
  25006. + }
  25007. +}
  25008. +#endif
  25009. +
  25010. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25011. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  25012. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  25013. + int cpu)
  25014. +{
  25015. + int old_cpu = task_cpu(task);
  25016. +
  25017. + if (cpu != old_cpu) {
  25018. + unsigned long flags;
  25019. + struct task_struct *cpu_wakeup_task;
  25020. +
  25021. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25022. +
  25023. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  25024. + if (task == cpu_wakeup_task) {
  25025. + put_task_struct(cpu_wakeup_task);
  25026. + per_cpu(wakeup_task, old_cpu) = NULL;
  25027. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  25028. + get_task_struct(cpu_wakeup_task);
  25029. + }
  25030. +
  25031. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25032. + }
  25033. +}
  25034. +
  25035. +static notrace void probe_wakeup_latency_hist_start(void *v,
  25036. + struct task_struct *p)
  25037. +{
  25038. + unsigned long flags;
  25039. + struct task_struct *curr = current;
  25040. + int cpu = task_cpu(p);
  25041. + struct task_struct *cpu_wakeup_task;
  25042. +
  25043. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25044. +
  25045. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  25046. +
  25047. + if (wakeup_pid) {
  25048. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  25049. + p->prio == curr->prio)
  25050. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25051. + if (likely(wakeup_pid != task_pid_nr(p)))
  25052. + goto out;
  25053. + } else {
  25054. + if (likely(!rt_task(p)) ||
  25055. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  25056. + p->prio > curr->prio)
  25057. + goto out;
  25058. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  25059. + p->prio == curr->prio)
  25060. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25061. + }
  25062. +
  25063. + if (cpu_wakeup_task)
  25064. + put_task_struct(cpu_wakeup_task);
  25065. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  25066. + get_task_struct(cpu_wakeup_task);
  25067. + cpu_wakeup_task->preempt_timestamp_hist =
  25068. + ftrace_now(raw_smp_processor_id());
  25069. +out:
  25070. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25071. +}
  25072. +
  25073. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  25074. + bool preempt, struct task_struct *prev, struct task_struct *next)
  25075. +{
  25076. + unsigned long flags;
  25077. + int cpu = task_cpu(next);
  25078. + long latency;
  25079. + cycle_t stop;
  25080. + struct task_struct *cpu_wakeup_task;
  25081. +
  25082. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  25083. +
  25084. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  25085. +
  25086. + if (cpu_wakeup_task == NULL)
  25087. + goto out;
  25088. +
  25089. + /* Already running? */
  25090. + if (unlikely(current == cpu_wakeup_task))
  25091. + goto out_reset;
  25092. +
  25093. + if (next != cpu_wakeup_task) {
  25094. + if (next->prio < cpu_wakeup_task->prio)
  25095. + goto out_reset;
  25096. +
  25097. + if (next->prio == cpu_wakeup_task->prio)
  25098. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25099. +
  25100. + goto out;
  25101. + }
  25102. +
  25103. + if (current->prio == cpu_wakeup_task->prio)
  25104. + per_cpu(wakeup_sharedprio, cpu) = 1;
  25105. +
  25106. + /*
  25107. + * The task we are waiting for is about to be switched to.
  25108. + * Calculate latency and store it in histogram.
  25109. + */
  25110. + stop = ftrace_now(raw_smp_processor_id());
  25111. +
  25112. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  25113. + NSECS_PER_USECS;
  25114. +
  25115. + if (per_cpu(wakeup_sharedprio, cpu)) {
  25116. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  25117. + next);
  25118. + per_cpu(wakeup_sharedprio, cpu) = 0;
  25119. + } else {
  25120. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  25121. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25122. + if (timerandwakeup_enabled_data.enabled) {
  25123. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  25124. + next->timer_offset + latency, next->timer_offset,
  25125. + stop, next);
  25126. + }
  25127. +#endif
  25128. + }
  25129. +
  25130. +out_reset:
  25131. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25132. + next->timer_offset = 0;
  25133. +#endif
  25134. + put_task_struct(cpu_wakeup_task);
  25135. + per_cpu(wakeup_task, cpu) = NULL;
  25136. +out:
  25137. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  25138. +}
  25139. +#endif
  25140. +
  25141. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25142. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  25143. + long long latency_ns, struct task_struct *curr,
  25144. + struct task_struct *task)
  25145. +{
  25146. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  25147. + (task->prio < curr->prio ||
  25148. + (task->prio == curr->prio &&
  25149. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  25150. + long latency;
  25151. + cycle_t now;
  25152. +
  25153. + if (missed_timer_offsets_pid) {
  25154. + if (likely(missed_timer_offsets_pid !=
  25155. + task_pid_nr(task)))
  25156. + return;
  25157. + }
  25158. +
  25159. + now = ftrace_now(cpu);
  25160. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  25161. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  25162. + task);
  25163. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25164. + task->timer_offset = latency;
  25165. +#endif
  25166. + }
  25167. +}
  25168. +#endif
  25169. +
  25170. +static __init int latency_hist_init(void)
  25171. +{
  25172. + struct dentry *latency_hist_root = NULL;
  25173. + struct dentry *dentry;
  25174. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25175. + struct dentry *dentry_sharedprio;
  25176. +#endif
  25177. + struct dentry *entry;
  25178. + struct dentry *enable_root;
  25179. + int i = 0;
  25180. + struct hist_data *my_hist;
  25181. + char name[64];
  25182. + char *cpufmt = "CPU%d";
  25183. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  25184. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25185. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  25186. + struct maxlatproc_data *mp = NULL;
  25187. +#endif
  25188. +
  25189. + dentry = tracing_init_dentry();
  25190. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  25191. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  25192. +
  25193. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  25194. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  25195. + for_each_possible_cpu(i) {
  25196. + sprintf(name, cpufmt, i);
  25197. + entry = debugfs_create_file(name, 0444, dentry,
  25198. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  25199. + my_hist = &per_cpu(irqsoff_hist, i);
  25200. + atomic_set(&my_hist->hist_mode, 1);
  25201. + my_hist->min_lat = LONG_MAX;
  25202. + }
  25203. + entry = debugfs_create_file("reset", 0644, dentry,
  25204. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  25205. +#endif
  25206. +
  25207. +#ifdef CONFIG_PREEMPT_OFF_HIST
  25208. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  25209. + latency_hist_root);
  25210. + for_each_possible_cpu(i) {
  25211. + sprintf(name, cpufmt, i);
  25212. + entry = debugfs_create_file(name, 0444, dentry,
  25213. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  25214. + my_hist = &per_cpu(preemptoff_hist, i);
  25215. + atomic_set(&my_hist->hist_mode, 1);
  25216. + my_hist->min_lat = LONG_MAX;
  25217. + }
  25218. + entry = debugfs_create_file("reset", 0644, dentry,
  25219. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  25220. +#endif
  25221. +
  25222. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  25223. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  25224. + latency_hist_root);
  25225. + for_each_possible_cpu(i) {
  25226. + sprintf(name, cpufmt, i);
  25227. + entry = debugfs_create_file(name, 0444, dentry,
  25228. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  25229. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  25230. + atomic_set(&my_hist->hist_mode, 1);
  25231. + my_hist->min_lat = LONG_MAX;
  25232. + }
  25233. + entry = debugfs_create_file("reset", 0644, dentry,
  25234. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  25235. +#endif
  25236. +
  25237. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  25238. + entry = debugfs_create_file("preemptirqsoff", 0644,
  25239. + enable_root, (void *)&preemptirqsoff_enabled_data,
  25240. + &enable_fops);
  25241. +#endif
  25242. +
  25243. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  25244. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  25245. + latency_hist_root);
  25246. + dentry_sharedprio = debugfs_create_dir(
  25247. + wakeup_latency_hist_dir_sharedprio, dentry);
  25248. + for_each_possible_cpu(i) {
  25249. + sprintf(name, cpufmt, i);
  25250. +
  25251. + entry = debugfs_create_file(name, 0444, dentry,
  25252. + &per_cpu(wakeup_latency_hist, i),
  25253. + &latency_hist_fops);
  25254. + my_hist = &per_cpu(wakeup_latency_hist, i);
  25255. + atomic_set(&my_hist->hist_mode, 1);
  25256. + my_hist->min_lat = LONG_MAX;
  25257. +
  25258. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  25259. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  25260. + &latency_hist_fops);
  25261. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  25262. + atomic_set(&my_hist->hist_mode, 1);
  25263. + my_hist->min_lat = LONG_MAX;
  25264. +
  25265. + sprintf(name, cpufmt_maxlatproc, i);
  25266. +
  25267. + mp = &per_cpu(wakeup_maxlatproc, i);
  25268. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25269. + &maxlatproc_fops);
  25270. + clear_maxlatprocdata(mp);
  25271. +
  25272. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  25273. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  25274. + &maxlatproc_fops);
  25275. + clear_maxlatprocdata(mp);
  25276. + }
  25277. + entry = debugfs_create_file("pid", 0644, dentry,
  25278. + (void *)&wakeup_pid, &pid_fops);
  25279. + entry = debugfs_create_file("reset", 0644, dentry,
  25280. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  25281. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  25282. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  25283. + entry = debugfs_create_file("wakeup", 0644,
  25284. + enable_root, (void *)&wakeup_latency_enabled_data,
  25285. + &enable_fops);
  25286. +#endif
  25287. +
  25288. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  25289. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  25290. + latency_hist_root);
  25291. + for_each_possible_cpu(i) {
  25292. + sprintf(name, cpufmt, i);
  25293. + entry = debugfs_create_file(name, 0444, dentry,
  25294. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  25295. + my_hist = &per_cpu(missed_timer_offsets, i);
  25296. + atomic_set(&my_hist->hist_mode, 1);
  25297. + my_hist->min_lat = LONG_MAX;
  25298. +
  25299. + sprintf(name, cpufmt_maxlatproc, i);
  25300. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  25301. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25302. + &maxlatproc_fops);
  25303. + clear_maxlatprocdata(mp);
  25304. + }
  25305. + entry = debugfs_create_file("pid", 0644, dentry,
  25306. + (void *)&missed_timer_offsets_pid, &pid_fops);
  25307. + entry = debugfs_create_file("reset", 0644, dentry,
  25308. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  25309. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  25310. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  25311. + &enable_fops);
  25312. +#endif
  25313. +
  25314. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  25315. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  25316. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  25317. + latency_hist_root);
  25318. + for_each_possible_cpu(i) {
  25319. + sprintf(name, cpufmt, i);
  25320. + entry = debugfs_create_file(name, 0444, dentry,
  25321. + &per_cpu(timerandwakeup_latency_hist, i),
  25322. + &latency_hist_fops);
  25323. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  25324. + atomic_set(&my_hist->hist_mode, 1);
  25325. + my_hist->min_lat = LONG_MAX;
  25326. +
  25327. + sprintf(name, cpufmt_maxlatproc, i);
  25328. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  25329. + entry = debugfs_create_file(name, 0444, dentry, mp,
  25330. + &maxlatproc_fops);
  25331. + clear_maxlatprocdata(mp);
  25332. + }
  25333. + entry = debugfs_create_file("reset", 0644, dentry,
  25334. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  25335. + entry = debugfs_create_file("timerandwakeup", 0644,
  25336. + enable_root, (void *)&timerandwakeup_enabled_data,
  25337. + &enable_fops);
  25338. +#endif
  25339. + return 0;
  25340. +}
  25341. +
  25342. +device_initcall(latency_hist_init);
  25343. diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
  25344. index 8305cbb2d5a2..8b0ae7d893cc 100644
  25345. --- a/kernel/trace/trace.c
  25346. +++ b/kernel/trace/trace.c
  25347. @@ -1652,6 +1652,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  25348. struct task_struct *tsk = current;
  25349. entry->preempt_count = pc & 0xff;
  25350. + entry->preempt_lazy_count = preempt_lazy_count();
  25351. entry->pid = (tsk) ? tsk->pid : 0;
  25352. entry->flags =
  25353. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  25354. @@ -1661,8 +1662,11 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  25355. #endif
  25356. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  25357. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  25358. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25359. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  25360. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  25361. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  25362. +
  25363. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  25364. }
  25365. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  25366. @@ -2555,14 +2559,17 @@ get_total_entries(struct trace_buffer *buf,
  25367. static void print_lat_help_header(struct seq_file *m)
  25368. {
  25369. - seq_puts(m, "# _------=> CPU# \n"
  25370. - "# / _-----=> irqs-off \n"
  25371. - "# | / _----=> need-resched \n"
  25372. - "# || / _---=> hardirq/softirq \n"
  25373. - "# ||| / _--=> preempt-depth \n"
  25374. - "# |||| / delay \n"
  25375. - "# cmd pid ||||| time | caller \n"
  25376. - "# \\ / ||||| \\ | / \n");
  25377. + seq_puts(m, "# _--------=> CPU# \n"
  25378. + "# / _-------=> irqs-off \n"
  25379. + "# | / _------=> need-resched \n"
  25380. + "# || / _-----=> need-resched_lazy \n"
  25381. + "# ||| / _----=> hardirq/softirq \n"
  25382. + "# |||| / _---=> preempt-depth \n"
  25383. + "# ||||| / _--=> preempt-lazy-depth\n"
  25384. + "# |||||| / _-=> migrate-disable \n"
  25385. + "# ||||||| / delay \n"
  25386. + "# cmd pid |||||||| time | caller \n"
  25387. + "# \\ / |||||||| \\ | / \n");
  25388. }
  25389. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  25390. @@ -2588,11 +2595,14 @@ static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file
  25391. print_event_info(buf, m);
  25392. seq_puts(m, "# _-----=> irqs-off\n"
  25393. "# / _----=> need-resched\n"
  25394. - "# | / _---=> hardirq/softirq\n"
  25395. - "# || / _--=> preempt-depth\n"
  25396. - "# ||| / delay\n"
  25397. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  25398. - "# | | | |||| | |\n");
  25399. + "# |/ _-----=> need-resched_lazy\n"
  25400. + "# || / _---=> hardirq/softirq\n"
  25401. + "# ||| / _--=> preempt-depth\n"
  25402. + "# |||| /_--=> preempt-lazy-depth\n"
  25403. + "# ||||| _-=> migrate-disable \n"
  25404. + "# ||||| / delay\n"
  25405. + "# TASK-PID CPU# |||||| TIMESTAMP FUNCTION\n"
  25406. + "# | | | |||||| | |\n");
  25407. }
  25408. void
  25409. diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
  25410. index 919d9d07686f..3bf86ece683c 100644
  25411. --- a/kernel/trace/trace.h
  25412. +++ b/kernel/trace/trace.h
  25413. @@ -117,6 +117,7 @@ struct kretprobe_trace_entry_head {
  25414. * NEED_RESCHED - reschedule is requested
  25415. * HARDIRQ - inside an interrupt handler
  25416. * SOFTIRQ - inside a softirq handler
  25417. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  25418. */
  25419. enum trace_flag_type {
  25420. TRACE_FLAG_IRQS_OFF = 0x01,
  25421. @@ -125,6 +126,7 @@ enum trace_flag_type {
  25422. TRACE_FLAG_HARDIRQ = 0x08,
  25423. TRACE_FLAG_SOFTIRQ = 0x10,
  25424. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  25425. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x40,
  25426. };
  25427. #define TRACE_BUF_SIZE 1024
  25428. diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
  25429. index 996f0fd34312..5bd79b347398 100644
  25430. --- a/kernel/trace/trace_events.c
  25431. +++ b/kernel/trace/trace_events.c
  25432. @@ -188,6 +188,8 @@ static int trace_define_common_fields(void)
  25433. __common_field(unsigned char, flags);
  25434. __common_field(unsigned char, preempt_count);
  25435. __common_field(int, pid);
  25436. + __common_field(unsigned short, migrate_disable);
  25437. + __common_field(unsigned short, padding);
  25438. return ret;
  25439. }
  25440. @@ -244,6 +246,14 @@ void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  25441. local_save_flags(fbuffer->flags);
  25442. fbuffer->pc = preempt_count();
  25443. + /*
  25444. + * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
  25445. + * preemption (adding one to the preempt_count). Since we are
  25446. + * interested in the preempt_count at the time the tracepoint was
  25447. + * hit, we need to subtract one to offset the increment.
  25448. + */
  25449. + if (IS_ENABLED(CONFIG_PREEMPT))
  25450. + fbuffer->pc--;
  25451. fbuffer->trace_file = trace_file;
  25452. fbuffer->event =
  25453. diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c
  25454. index be3222b7d72e..553e71254ad6 100644
  25455. --- a/kernel/trace/trace_irqsoff.c
  25456. +++ b/kernel/trace/trace_irqsoff.c
  25457. @@ -13,6 +13,7 @@
  25458. #include <linux/uaccess.h>
  25459. #include <linux/module.h>
  25460. #include <linux/ftrace.h>
  25461. +#include <trace/events/hist.h>
  25462. #include "trace.h"
  25463. @@ -424,11 +425,13 @@ void start_critical_timings(void)
  25464. {
  25465. if (preempt_trace() || irq_trace())
  25466. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25467. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  25468. }
  25469. EXPORT_SYMBOL_GPL(start_critical_timings);
  25470. void stop_critical_timings(void)
  25471. {
  25472. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  25473. if (preempt_trace() || irq_trace())
  25474. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25475. }
  25476. @@ -438,6 +441,7 @@ EXPORT_SYMBOL_GPL(stop_critical_timings);
  25477. #ifdef CONFIG_PROVE_LOCKING
  25478. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  25479. {
  25480. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  25481. if (!preempt_trace() && irq_trace())
  25482. stop_critical_timing(a0, a1);
  25483. }
  25484. @@ -446,6 +450,7 @@ void time_hardirqs_off(unsigned long a0, unsigned long a1)
  25485. {
  25486. if (!preempt_trace() && irq_trace())
  25487. start_critical_timing(a0, a1);
  25488. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  25489. }
  25490. #else /* !CONFIG_PROVE_LOCKING */
  25491. @@ -471,6 +476,7 @@ inline void print_irqtrace_events(struct task_struct *curr)
  25492. */
  25493. void trace_hardirqs_on(void)
  25494. {
  25495. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25496. if (!preempt_trace() && irq_trace())
  25497. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25498. }
  25499. @@ -480,11 +486,13 @@ void trace_hardirqs_off(void)
  25500. {
  25501. if (!preempt_trace() && irq_trace())
  25502. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  25503. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25504. }
  25505. EXPORT_SYMBOL(trace_hardirqs_off);
  25506. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  25507. {
  25508. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  25509. if (!preempt_trace() && irq_trace())
  25510. stop_critical_timing(CALLER_ADDR0, caller_addr);
  25511. }
  25512. @@ -494,6 +502,7 @@ __visible void trace_hardirqs_off_caller(unsigned long caller_addr)
  25513. {
  25514. if (!preempt_trace() && irq_trace())
  25515. start_critical_timing(CALLER_ADDR0, caller_addr);
  25516. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  25517. }
  25518. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  25519. @@ -503,12 +512,14 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller);
  25520. #ifdef CONFIG_PREEMPT_TRACER
  25521. void trace_preempt_on(unsigned long a0, unsigned long a1)
  25522. {
  25523. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  25524. if (preempt_trace() && !irq_trace())
  25525. stop_critical_timing(a0, a1);
  25526. }
  25527. void trace_preempt_off(unsigned long a0, unsigned long a1)
  25528. {
  25529. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  25530. if (preempt_trace() && !irq_trace())
  25531. start_critical_timing(a0, a1);
  25532. }
  25533. diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
  25534. index 282982195e09..9f19d839a756 100644
  25535. --- a/kernel/trace/trace_output.c
  25536. +++ b/kernel/trace/trace_output.c
  25537. @@ -386,6 +386,7 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  25538. {
  25539. char hardsoft_irq;
  25540. char need_resched;
  25541. + char need_resched_lazy;
  25542. char irqs_off;
  25543. int hardirq;
  25544. int softirq;
  25545. @@ -413,6 +414,8 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  25546. need_resched = '.';
  25547. break;
  25548. }
  25549. + need_resched_lazy =
  25550. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  25551. hardsoft_irq =
  25552. (hardirq && softirq) ? 'H' :
  25553. @@ -420,14 +423,25 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  25554. softirq ? 's' :
  25555. '.';
  25556. - trace_seq_printf(s, "%c%c%c",
  25557. - irqs_off, need_resched, hardsoft_irq);
  25558. + trace_seq_printf(s, "%c%c%c%c",
  25559. + irqs_off, need_resched, need_resched_lazy,
  25560. + hardsoft_irq);
  25561. if (entry->preempt_count)
  25562. trace_seq_printf(s, "%x", entry->preempt_count);
  25563. else
  25564. trace_seq_putc(s, '.');
  25565. + if (entry->preempt_lazy_count)
  25566. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  25567. + else
  25568. + trace_seq_putc(s, '.');
  25569. +
  25570. + if (entry->migrate_disable)
  25571. + trace_seq_printf(s, "%x", entry->migrate_disable);
  25572. + else
  25573. + trace_seq_putc(s, '.');
  25574. +
  25575. return !trace_seq_has_overflowed(s);
  25576. }
  25577. diff --git a/kernel/user.c b/kernel/user.c
  25578. index b069ccbfb0b0..1a2e88e98b5e 100644
  25579. --- a/kernel/user.c
  25580. +++ b/kernel/user.c
  25581. @@ -161,11 +161,11 @@ void free_uid(struct user_struct *up)
  25582. if (!up)
  25583. return;
  25584. - local_irq_save(flags);
  25585. + local_irq_save_nort(flags);
  25586. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  25587. free_user(up, flags);
  25588. else
  25589. - local_irq_restore(flags);
  25590. + local_irq_restore_nort(flags);
  25591. }
  25592. struct user_struct *alloc_uid(kuid_t uid)
  25593. diff --git a/kernel/watchdog.c b/kernel/watchdog.c
  25594. index 198137b1cadc..47d143740774 100644
  25595. --- a/kernel/watchdog.c
  25596. +++ b/kernel/watchdog.c
  25597. @@ -299,6 +299,8 @@ static int is_softlockup(unsigned long touch_ts)
  25598. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  25599. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  25600. +
  25601. static struct perf_event_attr wd_hw_attr = {
  25602. .type = PERF_TYPE_HARDWARE,
  25603. .config = PERF_COUNT_HW_CPU_CYCLES,
  25604. @@ -333,6 +335,13 @@ static void watchdog_overflow_callback(struct perf_event *event,
  25605. /* only print hardlockups once */
  25606. if (__this_cpu_read(hard_watchdog_warn) == true)
  25607. return;
  25608. + /*
  25609. + * If early-printk is enabled then make sure we do not
  25610. + * lock up in printk() and kill console logging:
  25611. + */
  25612. + printk_kill();
  25613. +
  25614. + raw_spin_lock(&watchdog_output_lock);
  25615. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  25616. print_modules();
  25617. @@ -350,8 +359,9 @@ static void watchdog_overflow_callback(struct perf_event *event,
  25618. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  25619. trigger_allbutself_cpu_backtrace();
  25620. + raw_spin_unlock(&watchdog_output_lock);
  25621. if (hardlockup_panic)
  25622. - panic("Hard LOCKUP");
  25623. + nmi_panic(regs, "Hard LOCKUP");
  25624. __this_cpu_write(hard_watchdog_warn, true);
  25625. return;
  25626. @@ -497,6 +507,7 @@ static void watchdog_enable(unsigned int cpu)
  25627. /* kick off the timer for the hardlockup detector */
  25628. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  25629. hrtimer->function = watchdog_timer_fn;
  25630. + hrtimer->irqsafe = 1;
  25631. /* Enable the perf event */
  25632. watchdog_nmi_enable(cpu);
  25633. diff --git a/kernel/workqueue.c b/kernel/workqueue.c
  25634. index 2c2f971f3e75..965d5f65e847 100644
  25635. --- a/kernel/workqueue.c
  25636. +++ b/kernel/workqueue.c
  25637. @@ -48,6 +48,8 @@
  25638. #include <linux/nodemask.h>
  25639. #include <linux/moduleparam.h>
  25640. #include <linux/uaccess.h>
  25641. +#include <linux/locallock.h>
  25642. +#include <linux/delay.h>
  25643. #include "workqueue_internal.h"
  25644. @@ -121,11 +123,16 @@ enum {
  25645. * cpu or grabbing pool->lock is enough for read access. If
  25646. * POOL_DISASSOCIATED is set, it's identical to L.
  25647. *
  25648. + * On RT we need the extra protection via rt_lock_idle_list() for
  25649. + * the list manipulations against read access from
  25650. + * wq_worker_sleeping(). All other places are nicely serialized via
  25651. + * pool->lock.
  25652. + *
  25653. * A: pool->attach_mutex protected.
  25654. *
  25655. * PL: wq_pool_mutex protected.
  25656. *
  25657. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  25658. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  25659. *
  25660. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  25661. *
  25662. @@ -134,7 +141,7 @@ enum {
  25663. *
  25664. * WQ: wq->mutex protected.
  25665. *
  25666. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  25667. + * WR: wq->mutex protected for writes. RCU protected for reads.
  25668. *
  25669. * MD: wq_mayday_lock protected.
  25670. */
  25671. @@ -183,7 +190,7 @@ struct worker_pool {
  25672. atomic_t nr_running ____cacheline_aligned_in_smp;
  25673. /*
  25674. - * Destruction of pool is sched-RCU protected to allow dereferences
  25675. + * Destruction of pool is RCU protected to allow dereferences
  25676. * from get_work_pool().
  25677. */
  25678. struct rcu_head rcu;
  25679. @@ -212,7 +219,7 @@ struct pool_workqueue {
  25680. /*
  25681. * Release of unbound pwq is punted to system_wq. See put_pwq()
  25682. * and pwq_unbound_release_workfn() for details. pool_workqueue
  25683. - * itself is also sched-RCU protected so that the first pwq can be
  25684. + * itself is also RCU protected so that the first pwq can be
  25685. * determined without grabbing wq->mutex.
  25686. */
  25687. struct work_struct unbound_release_work;
  25688. @@ -331,6 +338,8 @@ EXPORT_SYMBOL_GPL(system_power_efficient_wq);
  25689. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  25690. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  25691. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  25692. +
  25693. static int worker_thread(void *__worker);
  25694. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25695. @@ -338,20 +347,20 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25696. #include <trace/events/workqueue.h>
  25697. #define assert_rcu_or_pool_mutex() \
  25698. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25699. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25700. !lockdep_is_held(&wq_pool_mutex), \
  25701. - "sched RCU or wq_pool_mutex should be held")
  25702. + "RCU or wq_pool_mutex should be held")
  25703. #define assert_rcu_or_wq_mutex(wq) \
  25704. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25705. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25706. !lockdep_is_held(&wq->mutex), \
  25707. - "sched RCU or wq->mutex should be held")
  25708. + "RCU or wq->mutex should be held")
  25709. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  25710. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  25711. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  25712. !lockdep_is_held(&wq->mutex) && \
  25713. !lockdep_is_held(&wq_pool_mutex), \
  25714. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  25715. + "RCU, wq->mutex or wq_pool_mutex should be held")
  25716. #define for_each_cpu_worker_pool(pool, cpu) \
  25717. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  25718. @@ -363,7 +372,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25719. * @pool: iteration cursor
  25720. * @pi: integer used for iteration
  25721. *
  25722. - * This must be called either with wq_pool_mutex held or sched RCU read
  25723. + * This must be called either with wq_pool_mutex held or RCU read
  25724. * locked. If the pool needs to be used beyond the locking in effect, the
  25725. * caller is responsible for guaranteeing that the pool stays online.
  25726. *
  25727. @@ -395,7 +404,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25728. * @pwq: iteration cursor
  25729. * @wq: the target workqueue
  25730. *
  25731. - * This must be called either with wq->mutex held or sched RCU read locked.
  25732. + * This must be called either with wq->mutex held or RCU read locked.
  25733. * If the pwq needs to be used beyond the locking in effect, the caller is
  25734. * responsible for guaranteeing that the pwq stays online.
  25735. *
  25736. @@ -407,6 +416,31 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  25737. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  25738. else
  25739. +#ifdef CONFIG_PREEMPT_RT_BASE
  25740. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  25741. +{
  25742. + preempt_disable();
  25743. +}
  25744. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  25745. +{
  25746. + preempt_enable();
  25747. +}
  25748. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  25749. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  25750. +#else
  25751. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  25752. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  25753. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  25754. +{
  25755. + spin_lock_irq(&pool->lock);
  25756. +}
  25757. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  25758. +{
  25759. + spin_unlock_irq(&pool->lock);
  25760. +}
  25761. +#endif
  25762. +
  25763. +
  25764. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  25765. static struct debug_obj_descr work_debug_descr;
  25766. @@ -557,7 +591,7 @@ static int worker_pool_assign_id(struct worker_pool *pool)
  25767. * @wq: the target workqueue
  25768. * @node: the node ID
  25769. *
  25770. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  25771. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  25772. * read locked.
  25773. * If the pwq needs to be used beyond the locking in effect, the caller is
  25774. * responsible for guaranteeing that the pwq stays online.
  25775. @@ -701,8 +735,8 @@ static struct pool_workqueue *get_work_pwq(struct work_struct *work)
  25776. * @work: the work item of interest
  25777. *
  25778. * Pools are created and destroyed under wq_pool_mutex, and allows read
  25779. - * access under sched-RCU read lock. As such, this function should be
  25780. - * called under wq_pool_mutex or with preemption disabled.
  25781. + * access under RCU read lock. As such, this function should be
  25782. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  25783. *
  25784. * All fields of the returned pool are accessible as long as the above
  25785. * mentioned locking is in effect. If the returned pool needs to be used
  25786. @@ -839,51 +873,44 @@ static struct worker *first_idle_worker(struct worker_pool *pool)
  25787. */
  25788. static void wake_up_worker(struct worker_pool *pool)
  25789. {
  25790. - struct worker *worker = first_idle_worker(pool);
  25791. + struct worker *worker;
  25792. +
  25793. + rt_lock_idle_list(pool);
  25794. +
  25795. + worker = first_idle_worker(pool);
  25796. if (likely(worker))
  25797. wake_up_process(worker->task);
  25798. +
  25799. + rt_unlock_idle_list(pool);
  25800. }
  25801. /**
  25802. - * wq_worker_waking_up - a worker is waking up
  25803. - * @task: task waking up
  25804. - * @cpu: CPU @task is waking up to
  25805. + * wq_worker_running - a worker is running again
  25806. + * @task: task returning from sleep
  25807. *
  25808. - * This function is called during try_to_wake_up() when a worker is
  25809. - * being awoken.
  25810. - *
  25811. - * CONTEXT:
  25812. - * spin_lock_irq(rq->lock)
  25813. + * This function is called when a worker returns from schedule()
  25814. */
  25815. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  25816. +void wq_worker_running(struct task_struct *task)
  25817. {
  25818. struct worker *worker = kthread_data(task);
  25819. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  25820. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  25821. + if (!worker->sleeping)
  25822. + return;
  25823. + if (!(worker->flags & WORKER_NOT_RUNNING))
  25824. atomic_inc(&worker->pool->nr_running);
  25825. - }
  25826. + worker->sleeping = 0;
  25827. }
  25828. /**
  25829. * wq_worker_sleeping - a worker is going to sleep
  25830. * @task: task going to sleep
  25831. - * @cpu: CPU in question, must be the current CPU number
  25832. - *
  25833. - * This function is called during schedule() when a busy worker is
  25834. - * going to sleep. Worker on the same cpu can be woken up by
  25835. - * returning pointer to its task.
  25836. - *
  25837. - * CONTEXT:
  25838. - * spin_lock_irq(rq->lock)
  25839. - *
  25840. - * Return:
  25841. - * Worker task on @cpu to wake up, %NULL if none.
  25842. + * This function is called from schedule() when a busy worker is
  25843. + * going to sleep.
  25844. */
  25845. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  25846. +void wq_worker_sleeping(struct task_struct *task)
  25847. {
  25848. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  25849. + struct worker *worker = kthread_data(task);
  25850. struct worker_pool *pool;
  25851. /*
  25852. @@ -892,29 +919,26 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
  25853. * checking NOT_RUNNING.
  25854. */
  25855. if (worker->flags & WORKER_NOT_RUNNING)
  25856. - return NULL;
  25857. + return;
  25858. pool = worker->pool;
  25859. - /* this can only happen on the local cpu */
  25860. - if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu))
  25861. - return NULL;
  25862. + if (WARN_ON_ONCE(worker->sleeping))
  25863. + return;
  25864. +
  25865. + worker->sleeping = 1;
  25866. /*
  25867. * The counterpart of the following dec_and_test, implied mb,
  25868. * worklist not empty test sequence is in insert_work().
  25869. * Please read comment there.
  25870. - *
  25871. - * NOT_RUNNING is clear. This means that we're bound to and
  25872. - * running on the local cpu w/ rq lock held and preemption
  25873. - * disabled, which in turn means that none else could be
  25874. - * manipulating idle_list, so dereferencing idle_list without pool
  25875. - * lock is safe.
  25876. */
  25877. if (atomic_dec_and_test(&pool->nr_running) &&
  25878. - !list_empty(&pool->worklist))
  25879. - to_wakeup = first_idle_worker(pool);
  25880. - return to_wakeup ? to_wakeup->task : NULL;
  25881. + !list_empty(&pool->worklist)) {
  25882. + sched_lock_idle_list(pool);
  25883. + wake_up_worker(pool);
  25884. + sched_unlock_idle_list(pool);
  25885. + }
  25886. }
  25887. /**
  25888. @@ -1108,12 +1132,12 @@ static void put_pwq_unlocked(struct pool_workqueue *pwq)
  25889. {
  25890. if (pwq) {
  25891. /*
  25892. - * As both pwqs and pools are sched-RCU protected, the
  25893. + * As both pwqs and pools are RCU protected, the
  25894. * following lock operations are safe.
  25895. */
  25896. - spin_lock_irq(&pwq->pool->lock);
  25897. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  25898. put_pwq(pwq);
  25899. - spin_unlock_irq(&pwq->pool->lock);
  25900. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  25901. }
  25902. }
  25903. @@ -1215,7 +1239,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  25904. struct worker_pool *pool;
  25905. struct pool_workqueue *pwq;
  25906. - local_irq_save(*flags);
  25907. + local_lock_irqsave(pendingb_lock, *flags);
  25908. /* try to steal the timer if it exists */
  25909. if (is_dwork) {
  25910. @@ -1234,6 +1258,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  25911. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  25912. return 0;
  25913. + rcu_read_lock();
  25914. /*
  25915. * The queueing is in progress, or it is already queued. Try to
  25916. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  25917. @@ -1272,14 +1297,16 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  25918. set_work_pool_and_keep_pending(work, pool->id);
  25919. spin_unlock(&pool->lock);
  25920. + rcu_read_unlock();
  25921. return 1;
  25922. }
  25923. spin_unlock(&pool->lock);
  25924. fail:
  25925. - local_irq_restore(*flags);
  25926. + rcu_read_unlock();
  25927. + local_unlock_irqrestore(pendingb_lock, *flags);
  25928. if (work_is_canceling(work))
  25929. return -ENOENT;
  25930. - cpu_relax();
  25931. + cpu_chill();
  25932. return -EAGAIN;
  25933. }
  25934. @@ -1348,7 +1375,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  25935. * queued or lose PENDING. Grabbing PENDING and queueing should
  25936. * happen with IRQ disabled.
  25937. */
  25938. - WARN_ON_ONCE(!irqs_disabled());
  25939. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  25940. debug_work_activate(work);
  25941. @@ -1356,6 +1383,8 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  25942. if (unlikely(wq->flags & __WQ_DRAINING) &&
  25943. WARN_ON_ONCE(!is_chained_work(wq)))
  25944. return;
  25945. +
  25946. + rcu_read_lock();
  25947. retry:
  25948. if (req_cpu == WORK_CPU_UNBOUND)
  25949. cpu = raw_smp_processor_id();
  25950. @@ -1412,10 +1441,8 @@ retry:
  25951. /* pwq determined, queue */
  25952. trace_workqueue_queue_work(req_cpu, pwq, work);
  25953. - if (WARN_ON(!list_empty(&work->entry))) {
  25954. - spin_unlock(&pwq->pool->lock);
  25955. - return;
  25956. - }
  25957. + if (WARN_ON(!list_empty(&work->entry)))
  25958. + goto out;
  25959. pwq->nr_in_flight[pwq->work_color]++;
  25960. work_flags = work_color_to_flags(pwq->work_color);
  25961. @@ -1431,7 +1458,9 @@ retry:
  25962. insert_work(pwq, work, worklist, work_flags);
  25963. +out:
  25964. spin_unlock(&pwq->pool->lock);
  25965. + rcu_read_unlock();
  25966. }
  25967. /**
  25968. @@ -1451,14 +1480,14 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
  25969. bool ret = false;
  25970. unsigned long flags;
  25971. - local_irq_save(flags);
  25972. + local_lock_irqsave(pendingb_lock,flags);
  25973. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25974. __queue_work(cpu, wq, work);
  25975. ret = true;
  25976. }
  25977. - local_irq_restore(flags);
  25978. + local_unlock_irqrestore(pendingb_lock, flags);
  25979. return ret;
  25980. }
  25981. EXPORT_SYMBOL(queue_work_on);
  25982. @@ -1525,14 +1554,14 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  25983. unsigned long flags;
  25984. /* read the comment in __queue_work() */
  25985. - local_irq_save(flags);
  25986. + local_lock_irqsave(pendingb_lock, flags);
  25987. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  25988. __queue_delayed_work(cpu, wq, dwork, delay);
  25989. ret = true;
  25990. }
  25991. - local_irq_restore(flags);
  25992. + local_unlock_irqrestore(pendingb_lock, flags);
  25993. return ret;
  25994. }
  25995. EXPORT_SYMBOL(queue_delayed_work_on);
  25996. @@ -1567,7 +1596,7 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  25997. if (likely(ret >= 0)) {
  25998. __queue_delayed_work(cpu, wq, dwork, delay);
  25999. - local_irq_restore(flags);
  26000. + local_unlock_irqrestore(pendingb_lock, flags);
  26001. }
  26002. /* -ENOENT from try_to_grab_pending() becomes %true */
  26003. @@ -1600,7 +1629,9 @@ static void worker_enter_idle(struct worker *worker)
  26004. worker->last_active = jiffies;
  26005. /* idle_list is LIFO */
  26006. + rt_lock_idle_list(pool);
  26007. list_add(&worker->entry, &pool->idle_list);
  26008. + rt_unlock_idle_list(pool);
  26009. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  26010. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  26011. @@ -1633,7 +1664,9 @@ static void worker_leave_idle(struct worker *worker)
  26012. return;
  26013. worker_clr_flags(worker, WORKER_IDLE);
  26014. pool->nr_idle--;
  26015. + rt_lock_idle_list(pool);
  26016. list_del_init(&worker->entry);
  26017. + rt_unlock_idle_list(pool);
  26018. }
  26019. static struct worker *alloc_worker(int node)
  26020. @@ -1799,7 +1832,9 @@ static void destroy_worker(struct worker *worker)
  26021. pool->nr_workers--;
  26022. pool->nr_idle--;
  26023. + rt_lock_idle_list(pool);
  26024. list_del_init(&worker->entry);
  26025. + rt_unlock_idle_list(pool);
  26026. worker->flags |= WORKER_DIE;
  26027. wake_up_process(worker->task);
  26028. }
  26029. @@ -2716,14 +2751,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  26030. might_sleep();
  26031. - local_irq_disable();
  26032. + rcu_read_lock();
  26033. pool = get_work_pool(work);
  26034. if (!pool) {
  26035. - local_irq_enable();
  26036. + rcu_read_unlock();
  26037. return false;
  26038. }
  26039. - spin_lock(&pool->lock);
  26040. + spin_lock_irq(&pool->lock);
  26041. /* see the comment in try_to_grab_pending() with the same code */
  26042. pwq = get_work_pwq(work);
  26043. if (pwq) {
  26044. @@ -2750,10 +2785,11 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  26045. else
  26046. lock_map_acquire_read(&pwq->wq->lockdep_map);
  26047. lock_map_release(&pwq->wq->lockdep_map);
  26048. -
  26049. + rcu_read_unlock();
  26050. return true;
  26051. already_gone:
  26052. spin_unlock_irq(&pool->lock);
  26053. + rcu_read_unlock();
  26054. return false;
  26055. }
  26056. @@ -2840,7 +2876,7 @@ static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
  26057. /* tell other tasks trying to grab @work to back off */
  26058. mark_work_canceling(work);
  26059. - local_irq_restore(flags);
  26060. + local_unlock_irqrestore(pendingb_lock, flags);
  26061. flush_work(work);
  26062. clear_work_data(work);
  26063. @@ -2895,10 +2931,10 @@ EXPORT_SYMBOL_GPL(cancel_work_sync);
  26064. */
  26065. bool flush_delayed_work(struct delayed_work *dwork)
  26066. {
  26067. - local_irq_disable();
  26068. + local_lock_irq(pendingb_lock);
  26069. if (del_timer_sync(&dwork->timer))
  26070. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  26071. - local_irq_enable();
  26072. + local_unlock_irq(pendingb_lock);
  26073. return flush_work(&dwork->work);
  26074. }
  26075. EXPORT_SYMBOL(flush_delayed_work);
  26076. @@ -2933,7 +2969,7 @@ bool cancel_delayed_work(struct delayed_work *dwork)
  26077. set_work_pool_and_clear_pending(&dwork->work,
  26078. get_work_pool_id(&dwork->work));
  26079. - local_irq_restore(flags);
  26080. + local_unlock_irqrestore(pendingb_lock, flags);
  26081. return ret;
  26082. }
  26083. EXPORT_SYMBOL(cancel_delayed_work);
  26084. @@ -3161,7 +3197,7 @@ static void rcu_free_pool(struct rcu_head *rcu)
  26085. * put_unbound_pool - put a worker_pool
  26086. * @pool: worker_pool to put
  26087. *
  26088. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  26089. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  26090. * safe manner. get_unbound_pool() calls this function on its failure path
  26091. * and this function should be able to release pools which went through,
  26092. * successfully or not, init_worker_pool().
  26093. @@ -3215,8 +3251,8 @@ static void put_unbound_pool(struct worker_pool *pool)
  26094. del_timer_sync(&pool->idle_timer);
  26095. del_timer_sync(&pool->mayday_timer);
  26096. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  26097. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  26098. + /* RCU protected to allow dereferences from get_work_pool() */
  26099. + call_rcu(&pool->rcu, rcu_free_pool);
  26100. }
  26101. /**
  26102. @@ -3323,14 +3359,14 @@ static void pwq_unbound_release_workfn(struct work_struct *work)
  26103. put_unbound_pool(pool);
  26104. mutex_unlock(&wq_pool_mutex);
  26105. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  26106. + call_rcu(&pwq->rcu, rcu_free_pwq);
  26107. /*
  26108. * If we're the last pwq going away, @wq is already dead and no one
  26109. * is gonna access it anymore. Schedule RCU free.
  26110. */
  26111. if (is_last)
  26112. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  26113. + call_rcu(&wq->rcu, rcu_free_wq);
  26114. }
  26115. /**
  26116. @@ -3983,7 +4019,7 @@ void destroy_workqueue(struct workqueue_struct *wq)
  26117. * The base ref is never dropped on per-cpu pwqs. Directly
  26118. * schedule RCU free.
  26119. */
  26120. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  26121. + call_rcu(&wq->rcu, rcu_free_wq);
  26122. } else {
  26123. /*
  26124. * We're the sole accessor of @wq at this point. Directly
  26125. @@ -4076,7 +4112,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  26126. struct pool_workqueue *pwq;
  26127. bool ret;
  26128. - rcu_read_lock_sched();
  26129. + rcu_read_lock();
  26130. + preempt_disable();
  26131. if (cpu == WORK_CPU_UNBOUND)
  26132. cpu = smp_processor_id();
  26133. @@ -4087,7 +4124,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  26134. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  26135. ret = !list_empty(&pwq->delayed_works);
  26136. - rcu_read_unlock_sched();
  26137. + preempt_enable();
  26138. + rcu_read_unlock();
  26139. return ret;
  26140. }
  26141. @@ -4113,15 +4151,15 @@ unsigned int work_busy(struct work_struct *work)
  26142. if (work_pending(work))
  26143. ret |= WORK_BUSY_PENDING;
  26144. - local_irq_save(flags);
  26145. + rcu_read_lock();
  26146. pool = get_work_pool(work);
  26147. if (pool) {
  26148. - spin_lock(&pool->lock);
  26149. + spin_lock_irqsave(&pool->lock, flags);
  26150. if (find_worker_executing_work(pool, work))
  26151. ret |= WORK_BUSY_RUNNING;
  26152. - spin_unlock(&pool->lock);
  26153. + spin_unlock_irqrestore(&pool->lock, flags);
  26154. }
  26155. - local_irq_restore(flags);
  26156. + rcu_read_unlock();
  26157. return ret;
  26158. }
  26159. @@ -4310,7 +4348,7 @@ void show_workqueue_state(void)
  26160. unsigned long flags;
  26161. int pi;
  26162. - rcu_read_lock_sched();
  26163. + rcu_read_lock();
  26164. pr_info("Showing busy workqueues and worker pools:\n");
  26165. @@ -4361,7 +4399,7 @@ void show_workqueue_state(void)
  26166. spin_unlock_irqrestore(&pool->lock, flags);
  26167. }
  26168. - rcu_read_unlock_sched();
  26169. + rcu_read_unlock();
  26170. }
  26171. /*
  26172. @@ -4722,16 +4760,16 @@ bool freeze_workqueues_busy(void)
  26173. * nr_active is monotonically decreasing. It's safe
  26174. * to peek without lock.
  26175. */
  26176. - rcu_read_lock_sched();
  26177. + rcu_read_lock();
  26178. for_each_pwq(pwq, wq) {
  26179. WARN_ON_ONCE(pwq->nr_active < 0);
  26180. if (pwq->nr_active) {
  26181. busy = true;
  26182. - rcu_read_unlock_sched();
  26183. + rcu_read_unlock();
  26184. goto out_unlock;
  26185. }
  26186. }
  26187. - rcu_read_unlock_sched();
  26188. + rcu_read_unlock();
  26189. }
  26190. out_unlock:
  26191. mutex_unlock(&wq_pool_mutex);
  26192. @@ -4921,7 +4959,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  26193. const char *delim = "";
  26194. int node, written = 0;
  26195. - rcu_read_lock_sched();
  26196. + get_online_cpus();
  26197. + rcu_read_lock();
  26198. for_each_node(node) {
  26199. written += scnprintf(buf + written, PAGE_SIZE - written,
  26200. "%s%d:%d", delim, node,
  26201. @@ -4929,7 +4968,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  26202. delim = " ";
  26203. }
  26204. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  26205. - rcu_read_unlock_sched();
  26206. + rcu_read_unlock();
  26207. + put_online_cpus();
  26208. return written;
  26209. }
  26210. diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h
  26211. index 45215870ac6c..f000c4d6917e 100644
  26212. --- a/kernel/workqueue_internal.h
  26213. +++ b/kernel/workqueue_internal.h
  26214. @@ -43,6 +43,7 @@ struct worker {
  26215. unsigned long last_active; /* L: last active timestamp */
  26216. unsigned int flags; /* X: flags */
  26217. int id; /* I: worker id */
  26218. + int sleeping; /* None */
  26219. /*
  26220. * Opaque string set with work_set_desc(). Printed out with task
  26221. @@ -68,7 +69,7 @@ static inline struct worker *current_wq_worker(void)
  26222. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  26223. * sched/core.c and workqueue.c.
  26224. */
  26225. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  26226. -struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu);
  26227. +void wq_worker_running(struct task_struct *task);
  26228. +void wq_worker_sleeping(struct task_struct *task);
  26229. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  26230. diff --git a/lib/Kconfig b/lib/Kconfig
  26231. index 1a48744253d7..f75de578cca8 100644
  26232. --- a/lib/Kconfig
  26233. +++ b/lib/Kconfig
  26234. @@ -397,6 +397,7 @@ config CHECK_SIGNATURE
  26235. config CPUMASK_OFFSTACK
  26236. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  26237. + depends on !PREEMPT_RT_FULL
  26238. help
  26239. Use dynamic allocation for cpumask_var_t, instead of putting
  26240. them on the stack. This is a bit more expensive, but avoids
  26241. diff --git a/lib/debugobjects.c b/lib/debugobjects.c
  26242. index 547f7f923dbc..8fcdbc2fc6d0 100644
  26243. --- a/lib/debugobjects.c
  26244. +++ b/lib/debugobjects.c
  26245. @@ -309,7 +309,10 @@ __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  26246. struct debug_obj *obj;
  26247. unsigned long flags;
  26248. - fill_pool();
  26249. +#ifdef CONFIG_PREEMPT_RT_FULL
  26250. + if (preempt_count() == 0 && !irqs_disabled())
  26251. +#endif
  26252. + fill_pool();
  26253. db = get_bucket((unsigned long) addr);
  26254. diff --git a/lib/idr.c b/lib/idr.c
  26255. index 6098336df267..9decbe914595 100644
  26256. --- a/lib/idr.c
  26257. +++ b/lib/idr.c
  26258. @@ -30,6 +30,7 @@
  26259. #include <linux/idr.h>
  26260. #include <linux/spinlock.h>
  26261. #include <linux/percpu.h>
  26262. +#include <linux/locallock.h>
  26263. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  26264. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  26265. @@ -45,6 +46,37 @@ static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
  26266. static DEFINE_PER_CPU(int, idr_preload_cnt);
  26267. static DEFINE_SPINLOCK(simple_ida_lock);
  26268. +#ifdef CONFIG_PREEMPT_RT_FULL
  26269. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  26270. +
  26271. +static inline void idr_preload_lock(void)
  26272. +{
  26273. + local_lock(idr_lock);
  26274. +}
  26275. +
  26276. +static inline void idr_preload_unlock(void)
  26277. +{
  26278. + local_unlock(idr_lock);
  26279. +}
  26280. +
  26281. +void idr_preload_end(void)
  26282. +{
  26283. + idr_preload_unlock();
  26284. +}
  26285. +EXPORT_SYMBOL(idr_preload_end);
  26286. +#else
  26287. +static inline void idr_preload_lock(void)
  26288. +{
  26289. + preempt_disable();
  26290. +}
  26291. +
  26292. +static inline void idr_preload_unlock(void)
  26293. +{
  26294. + preempt_enable();
  26295. +}
  26296. +#endif
  26297. +
  26298. +
  26299. /* the maximum ID which can be allocated given idr->layers */
  26300. static int idr_max(int layers)
  26301. {
  26302. @@ -115,14 +147,14 @@ static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
  26303. * context. See idr_preload() for details.
  26304. */
  26305. if (!in_interrupt()) {
  26306. - preempt_disable();
  26307. + idr_preload_lock();
  26308. new = __this_cpu_read(idr_preload_head);
  26309. if (new) {
  26310. __this_cpu_write(idr_preload_head, new->ary[0]);
  26311. __this_cpu_dec(idr_preload_cnt);
  26312. new->ary[0] = NULL;
  26313. }
  26314. - preempt_enable();
  26315. + idr_preload_unlock();
  26316. if (new)
  26317. return new;
  26318. }
  26319. @@ -366,7 +398,6 @@ static void idr_fill_slot(struct idr *idr, void *ptr, int id,
  26320. idr_mark_full(pa, id);
  26321. }
  26322. -
  26323. /**
  26324. * idr_preload - preload for idr_alloc()
  26325. * @gfp_mask: allocation mask to use for preloading
  26326. @@ -401,7 +432,7 @@ void idr_preload(gfp_t gfp_mask)
  26327. WARN_ON_ONCE(in_interrupt());
  26328. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  26329. - preempt_disable();
  26330. + idr_preload_lock();
  26331. /*
  26332. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  26333. @@ -413,9 +444,9 @@ void idr_preload(gfp_t gfp_mask)
  26334. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  26335. struct idr_layer *new;
  26336. - preempt_enable();
  26337. + idr_preload_unlock();
  26338. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  26339. - preempt_disable();
  26340. + idr_preload_lock();
  26341. if (!new)
  26342. break;
  26343. diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c
  26344. index 872a15a2a637..b93a6103fa4d 100644
  26345. --- a/lib/locking-selftest.c
  26346. +++ b/lib/locking-selftest.c
  26347. @@ -590,6 +590,8 @@ GENERATE_TESTCASE(init_held_rsem)
  26348. #include "locking-selftest-spin-hardirq.h"
  26349. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  26350. +#ifndef CONFIG_PREEMPT_RT_FULL
  26351. +
  26352. #include "locking-selftest-rlock-hardirq.h"
  26353. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  26354. @@ -605,9 +607,12 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  26355. #include "locking-selftest-wlock-softirq.h"
  26356. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  26357. +#endif
  26358. +
  26359. #undef E1
  26360. #undef E2
  26361. +#ifndef CONFIG_PREEMPT_RT_FULL
  26362. /*
  26363. * Enabling hardirqs with a softirq-safe lock held:
  26364. */
  26365. @@ -640,6 +645,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  26366. #undef E1
  26367. #undef E2
  26368. +#endif
  26369. +
  26370. /*
  26371. * Enabling irqs with an irq-safe lock held:
  26372. */
  26373. @@ -663,6 +670,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  26374. #include "locking-selftest-spin-hardirq.h"
  26375. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  26376. +#ifndef CONFIG_PREEMPT_RT_FULL
  26377. +
  26378. #include "locking-selftest-rlock-hardirq.h"
  26379. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  26380. @@ -678,6 +687,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  26381. #include "locking-selftest-wlock-softirq.h"
  26382. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  26383. +#endif
  26384. +
  26385. #undef E1
  26386. #undef E2
  26387. @@ -709,6 +720,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  26388. #include "locking-selftest-spin-hardirq.h"
  26389. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  26390. +#ifndef CONFIG_PREEMPT_RT_FULL
  26391. +
  26392. #include "locking-selftest-rlock-hardirq.h"
  26393. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  26394. @@ -724,6 +737,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  26395. #include "locking-selftest-wlock-softirq.h"
  26396. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  26397. +#endif
  26398. +
  26399. #undef E1
  26400. #undef E2
  26401. #undef E3
  26402. @@ -757,6 +772,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  26403. #include "locking-selftest-spin-hardirq.h"
  26404. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  26405. +#ifndef CONFIG_PREEMPT_RT_FULL
  26406. +
  26407. #include "locking-selftest-rlock-hardirq.h"
  26408. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  26409. @@ -772,10 +789,14 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  26410. #include "locking-selftest-wlock-softirq.h"
  26411. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  26412. +#endif
  26413. +
  26414. #undef E1
  26415. #undef E2
  26416. #undef E3
  26417. +#ifndef CONFIG_PREEMPT_RT_FULL
  26418. +
  26419. /*
  26420. * read-lock / write-lock irq inversion.
  26421. *
  26422. @@ -838,6 +859,10 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  26423. #undef E2
  26424. #undef E3
  26425. +#endif
  26426. +
  26427. +#ifndef CONFIG_PREEMPT_RT_FULL
  26428. +
  26429. /*
  26430. * read-lock / write-lock recursion that is actually safe.
  26431. */
  26432. @@ -876,6 +901,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
  26433. #undef E2
  26434. #undef E3
  26435. +#endif
  26436. +
  26437. /*
  26438. * read-lock / write-lock recursion that is unsafe.
  26439. */
  26440. @@ -1858,6 +1885,7 @@ void locking_selftest(void)
  26441. printk(" --------------------------------------------------------------------------\n");
  26442. +#ifndef CONFIG_PREEMPT_RT_FULL
  26443. /*
  26444. * irq-context testcases:
  26445. */
  26446. @@ -1870,6 +1898,28 @@ void locking_selftest(void)
  26447. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  26448. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  26449. +#else
  26450. + /* On -rt, we only do hardirq context test for raw spinlock */
  26451. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  26452. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  26453. +
  26454. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  26455. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  26456. +
  26457. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  26458. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  26459. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  26460. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  26461. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  26462. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  26463. +
  26464. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  26465. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  26466. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  26467. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  26468. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  26469. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  26470. +#endif
  26471. ww_tests();
  26472. diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
  26473. index 6d40944960de..822a2c027e72 100644
  26474. --- a/lib/percpu_ida.c
  26475. +++ b/lib/percpu_ida.c
  26476. @@ -26,6 +26,9 @@
  26477. #include <linux/string.h>
  26478. #include <linux/spinlock.h>
  26479. #include <linux/percpu_ida.h>
  26480. +#include <linux/locallock.h>
  26481. +
  26482. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  26483. struct percpu_ida_cpu {
  26484. /*
  26485. @@ -148,13 +151,13 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  26486. unsigned long flags;
  26487. int tag;
  26488. - local_irq_save(flags);
  26489. + local_lock_irqsave(irq_off_lock, flags);
  26490. tags = this_cpu_ptr(pool->tag_cpu);
  26491. /* Fastpath */
  26492. tag = alloc_local_tag(tags);
  26493. if (likely(tag >= 0)) {
  26494. - local_irq_restore(flags);
  26495. + local_unlock_irqrestore(irq_off_lock, flags);
  26496. return tag;
  26497. }
  26498. @@ -173,6 +176,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  26499. if (!tags->nr_free)
  26500. alloc_global_tags(pool, tags);
  26501. +
  26502. if (!tags->nr_free)
  26503. steal_tags(pool, tags);
  26504. @@ -184,7 +188,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  26505. }
  26506. spin_unlock(&pool->lock);
  26507. - local_irq_restore(flags);
  26508. + local_unlock_irqrestore(irq_off_lock, flags);
  26509. if (tag >= 0 || state == TASK_RUNNING)
  26510. break;
  26511. @@ -196,7 +200,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  26512. schedule();
  26513. - local_irq_save(flags);
  26514. + local_lock_irqsave(irq_off_lock, flags);
  26515. tags = this_cpu_ptr(pool->tag_cpu);
  26516. }
  26517. if (state != TASK_RUNNING)
  26518. @@ -221,7 +225,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  26519. BUG_ON(tag >= pool->nr_tags);
  26520. - local_irq_save(flags);
  26521. + local_lock_irqsave(irq_off_lock, flags);
  26522. tags = this_cpu_ptr(pool->tag_cpu);
  26523. spin_lock(&tags->lock);
  26524. @@ -253,7 +257,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  26525. spin_unlock(&pool->lock);
  26526. }
  26527. - local_irq_restore(flags);
  26528. + local_unlock_irqrestore(irq_off_lock, flags);
  26529. }
  26530. EXPORT_SYMBOL_GPL(percpu_ida_free);
  26531. @@ -345,7 +349,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  26532. struct percpu_ida_cpu *remote;
  26533. unsigned cpu, i, err = 0;
  26534. - local_irq_save(flags);
  26535. + local_lock_irqsave(irq_off_lock, flags);
  26536. for_each_possible_cpu(cpu) {
  26537. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  26538. spin_lock(&remote->lock);
  26539. @@ -367,7 +371,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  26540. }
  26541. spin_unlock(&pool->lock);
  26542. out:
  26543. - local_irq_restore(flags);
  26544. + local_unlock_irqrestore(irq_off_lock, flags);
  26545. return err;
  26546. }
  26547. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  26548. diff --git a/lib/radix-tree.c b/lib/radix-tree.c
  26549. index 6b79e9026e24..f27e0bcb74f7 100644
  26550. --- a/lib/radix-tree.c
  26551. +++ b/lib/radix-tree.c
  26552. @@ -196,13 +196,14 @@ radix_tree_node_alloc(struct radix_tree_root *root)
  26553. * succeed in getting a node here (and never reach
  26554. * kmem_cache_alloc)
  26555. */
  26556. - rtp = this_cpu_ptr(&radix_tree_preloads);
  26557. + rtp = &get_cpu_var(radix_tree_preloads);
  26558. if (rtp->nr) {
  26559. ret = rtp->nodes;
  26560. rtp->nodes = ret->private_data;
  26561. ret->private_data = NULL;
  26562. rtp->nr--;
  26563. }
  26564. + put_cpu_var(radix_tree_preloads);
  26565. /*
  26566. * Update the allocation stack trace as this is more useful
  26567. * for debugging.
  26568. @@ -242,6 +243,7 @@ radix_tree_node_free(struct radix_tree_node *node)
  26569. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  26570. }
  26571. +#ifndef CONFIG_PREEMPT_RT_FULL
  26572. /*
  26573. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  26574. * ensure that the addition of a single element in the tree cannot fail. On
  26575. @@ -310,6 +312,7 @@ int radix_tree_maybe_preload(gfp_t gfp_mask)
  26576. return 0;
  26577. }
  26578. EXPORT_SYMBOL(radix_tree_maybe_preload);
  26579. +#endif
  26580. /*
  26581. * Return the maximum key which can be store into a
  26582. diff --git a/lib/rbtree.c b/lib/rbtree.c
  26583. index 1356454e36de..d15d6c4327f1 100644
  26584. --- a/lib/rbtree.c
  26585. +++ b/lib/rbtree.c
  26586. @@ -23,6 +23,7 @@
  26587. #include <linux/rbtree_augmented.h>
  26588. #include <linux/export.h>
  26589. +#include <linux/rcupdate.h>
  26590. /*
  26591. * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
  26592. @@ -590,3 +591,13 @@ struct rb_node *rb_first_postorder(const struct rb_root *root)
  26593. return rb_left_deepest_node(root->rb_node);
  26594. }
  26595. EXPORT_SYMBOL(rb_first_postorder);
  26596. +
  26597. +void rb_link_node_rcu(struct rb_node *node, struct rb_node *parent,
  26598. + struct rb_node **rb_link)
  26599. +{
  26600. + node->__rb_parent_color = (unsigned long)parent;
  26601. + node->rb_left = node->rb_right = NULL;
  26602. +
  26603. + rcu_assign_pointer(*rb_link, node);
  26604. +}
  26605. +EXPORT_SYMBOL(rb_link_node_rcu);
  26606. diff --git a/lib/scatterlist.c b/lib/scatterlist.c
  26607. index bafa9933fa76..ebe3b7edd086 100644
  26608. --- a/lib/scatterlist.c
  26609. +++ b/lib/scatterlist.c
  26610. @@ -620,7 +620,7 @@ void sg_miter_stop(struct sg_mapping_iter *miter)
  26611. flush_kernel_dcache_page(miter->page);
  26612. if (miter->__flags & SG_MITER_ATOMIC) {
  26613. - WARN_ON_ONCE(preemptible());
  26614. + WARN_ON_ONCE(!pagefault_disabled());
  26615. kunmap_atomic(miter->addr);
  26616. } else
  26617. kunmap(miter->page);
  26618. @@ -664,7 +664,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  26619. if (!sg_miter_skip(&miter, skip))
  26620. return false;
  26621. - local_irq_save(flags);
  26622. + local_irq_save_nort(flags);
  26623. while (sg_miter_next(&miter) && offset < buflen) {
  26624. unsigned int len;
  26625. @@ -681,7 +681,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  26626. sg_miter_stop(&miter);
  26627. - local_irq_restore(flags);
  26628. + local_irq_restore_nort(flags);
  26629. return offset;
  26630. }
  26631. EXPORT_SYMBOL(sg_copy_buffer);
  26632. diff --git a/lib/smp_processor_id.c b/lib/smp_processor_id.c
  26633. index 1afec32de6f2..11fa431046a8 100644
  26634. --- a/lib/smp_processor_id.c
  26635. +++ b/lib/smp_processor_id.c
  26636. @@ -39,8 +39,9 @@ notrace static unsigned int check_preemption_disabled(const char *what1,
  26637. if (!printk_ratelimit())
  26638. goto out_enable;
  26639. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  26640. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  26641. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  26642. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  26643. + current->comm, current->pid);
  26644. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  26645. dump_stack();
  26646. diff --git a/mm/Kconfig b/mm/Kconfig
  26647. index 97a4e06b15c0..9614351e68b8 100644
  26648. --- a/mm/Kconfig
  26649. +++ b/mm/Kconfig
  26650. @@ -392,7 +392,7 @@ config NOMMU_INITIAL_TRIM_EXCESS
  26651. config TRANSPARENT_HUGEPAGE
  26652. bool "Transparent Hugepage Support"
  26653. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  26654. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  26655. select COMPACTION
  26656. help
  26657. Transparent Hugepages allows the kernel to use huge pages and
  26658. diff --git a/mm/backing-dev.c b/mm/backing-dev.c
  26659. index 9ef80bf441b3..826fed55c1cc 100644
  26660. --- a/mm/backing-dev.c
  26661. +++ b/mm/backing-dev.c
  26662. @@ -457,9 +457,9 @@ void wb_congested_put(struct bdi_writeback_congested *congested)
  26663. {
  26664. unsigned long flags;
  26665. - local_irq_save(flags);
  26666. + local_irq_save_nort(flags);
  26667. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  26668. - local_irq_restore(flags);
  26669. + local_irq_restore_nort(flags);
  26670. return;
  26671. }
  26672. diff --git a/mm/compaction.c b/mm/compaction.c
  26673. index dba02dec7195..51963f58a29b 100644
  26674. --- a/mm/compaction.c
  26675. +++ b/mm/compaction.c
  26676. @@ -1430,10 +1430,12 @@ check_drain:
  26677. cc->migrate_pfn & ~((1UL << cc->order) - 1);
  26678. if (cc->last_migrated_pfn < current_block_start) {
  26679. - cpu = get_cpu();
  26680. + cpu = get_cpu_light();
  26681. + local_lock_irq(swapvec_lock);
  26682. lru_add_drain_cpu(cpu);
  26683. + local_unlock_irq(swapvec_lock);
  26684. drain_local_pages(zone);
  26685. - put_cpu();
  26686. + put_cpu_light();
  26687. /* No more flushing until we migrate again */
  26688. cc->last_migrated_pfn = 0;
  26689. }
  26690. diff --git a/mm/filemap.c b/mm/filemap.c
  26691. index 1bb007624b53..44301361c100 100644
  26692. --- a/mm/filemap.c
  26693. +++ b/mm/filemap.c
  26694. @@ -168,7 +168,9 @@ static void page_cache_tree_delete(struct address_space *mapping,
  26695. if (!workingset_node_pages(node) &&
  26696. list_empty(&node->private_list)) {
  26697. node->private_data = mapping;
  26698. - list_lru_add(&workingset_shadow_nodes, &node->private_list);
  26699. + local_lock(workingset_shadow_lock);
  26700. + list_lru_add(&__workingset_shadow_nodes, &node->private_list);
  26701. + local_unlock(workingset_shadow_lock);
  26702. }
  26703. }
  26704. @@ -597,9 +599,12 @@ static int page_cache_tree_insert(struct address_space *mapping,
  26705. * node->private_list is protected by
  26706. * mapping->tree_lock.
  26707. */
  26708. - if (!list_empty(&node->private_list))
  26709. - list_lru_del(&workingset_shadow_nodes,
  26710. + if (!list_empty(&node->private_list)) {
  26711. + local_lock(workingset_shadow_lock);
  26712. + list_lru_del(&__workingset_shadow_nodes,
  26713. &node->private_list);
  26714. + local_unlock(workingset_shadow_lock);
  26715. + }
  26716. }
  26717. return 0;
  26718. }
  26719. diff --git a/mm/highmem.c b/mm/highmem.c
  26720. index 123bcd3ed4f2..16e8cf26d38a 100644
  26721. --- a/mm/highmem.c
  26722. +++ b/mm/highmem.c
  26723. @@ -29,10 +29,11 @@
  26724. #include <linux/kgdb.h>
  26725. #include <asm/tlbflush.h>
  26726. -
  26727. +#ifndef CONFIG_PREEMPT_RT_FULL
  26728. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  26729. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  26730. #endif
  26731. +#endif
  26732. /*
  26733. * Virtual_count is not a pure "count".
  26734. @@ -107,8 +108,9 @@ static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
  26735. unsigned long totalhigh_pages __read_mostly;
  26736. EXPORT_SYMBOL(totalhigh_pages);
  26737. -
  26738. +#ifndef CONFIG_PREEMPT_RT_FULL
  26739. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  26740. +#endif
  26741. unsigned int nr_free_highpages (void)
  26742. {
  26743. diff --git a/mm/memcontrol.c b/mm/memcontrol.c
  26744. index 6b90d184e9c0..ed7aa011ad70 100644
  26745. --- a/mm/memcontrol.c
  26746. +++ b/mm/memcontrol.c
  26747. @@ -67,6 +67,8 @@
  26748. #include <net/sock.h>
  26749. #include <net/ip.h>
  26750. #include <net/tcp_memcontrol.h>
  26751. +#include <linux/locallock.h>
  26752. +
  26753. #include "slab.h"
  26754. #include <asm/uaccess.h>
  26755. @@ -87,6 +89,7 @@ int do_swap_account __read_mostly;
  26756. #define do_swap_account 0
  26757. #endif
  26758. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  26759. static const char * const mem_cgroup_stat_names[] = {
  26760. "cache",
  26761. "rss",
  26762. @@ -1922,14 +1925,17 @@ static void drain_local_stock(struct work_struct *dummy)
  26763. */
  26764. static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  26765. {
  26766. - struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
  26767. + struct memcg_stock_pcp *stock;
  26768. + int cpu = get_cpu_light();
  26769. +
  26770. + stock = &per_cpu(memcg_stock, cpu);
  26771. if (stock->cached != memcg) { /* reset if necessary */
  26772. drain_stock(stock);
  26773. stock->cached = memcg;
  26774. }
  26775. stock->nr_pages += nr_pages;
  26776. - put_cpu_var(memcg_stock);
  26777. + put_cpu_light();
  26778. }
  26779. /*
  26780. @@ -1945,7 +1951,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  26781. return;
  26782. /* Notify other cpus that system-wide "drain" is running */
  26783. get_online_cpus();
  26784. - curcpu = get_cpu();
  26785. + curcpu = get_cpu_light();
  26786. for_each_online_cpu(cpu) {
  26787. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  26788. struct mem_cgroup *memcg;
  26789. @@ -1962,7 +1968,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  26790. schedule_work_on(cpu, &stock->work);
  26791. }
  26792. }
  26793. - put_cpu();
  26794. + put_cpu_light();
  26795. put_online_cpus();
  26796. mutex_unlock(&percpu_charge_mutex);
  26797. }
  26798. @@ -4700,12 +4706,12 @@ static int mem_cgroup_move_account(struct page *page,
  26799. ret = 0;
  26800. - local_irq_disable();
  26801. + local_lock_irq(event_lock);
  26802. mem_cgroup_charge_statistics(to, page, nr_pages);
  26803. memcg_check_events(to, page);
  26804. mem_cgroup_charge_statistics(from, page, -nr_pages);
  26805. memcg_check_events(from, page);
  26806. - local_irq_enable();
  26807. + local_unlock_irq(event_lock);
  26808. out_unlock:
  26809. unlock_page(page);
  26810. out:
  26811. @@ -5495,10 +5501,10 @@ void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  26812. VM_BUG_ON_PAGE(!PageTransHuge(page), page);
  26813. }
  26814. - local_irq_disable();
  26815. + local_lock_irq(event_lock);
  26816. mem_cgroup_charge_statistics(memcg, page, nr_pages);
  26817. memcg_check_events(memcg, page);
  26818. - local_irq_enable();
  26819. + local_unlock_irq(event_lock);
  26820. if (do_swap_account && PageSwapCache(page)) {
  26821. swp_entry_t entry = { .val = page_private(page) };
  26822. @@ -5554,14 +5560,14 @@ static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
  26823. memcg_oom_recover(memcg);
  26824. }
  26825. - local_irq_save(flags);
  26826. + local_lock_irqsave(event_lock, flags);
  26827. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  26828. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  26829. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  26830. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  26831. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  26832. memcg_check_events(memcg, dummy_page);
  26833. - local_irq_restore(flags);
  26834. + local_unlock_irqrestore(event_lock, flags);
  26835. if (!mem_cgroup_is_root(memcg))
  26836. css_put_many(&memcg->css, nr_pages);
  26837. @@ -5753,6 +5759,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  26838. {
  26839. struct mem_cgroup *memcg, *swap_memcg;
  26840. unsigned short oldid;
  26841. + unsigned long flags;
  26842. VM_BUG_ON_PAGE(PageLRU(page), page);
  26843. VM_BUG_ON_PAGE(page_count(page), page);
  26844. @@ -5793,12 +5800,16 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  26845. * important here to have the interrupts disabled because it is the
  26846. * only synchronisation we have for udpating the per-CPU variables.
  26847. */
  26848. + local_lock_irqsave(event_lock, flags);
  26849. +#ifndef CONFIG_PREEMPT_RT_BASE
  26850. VM_BUG_ON(!irqs_disabled());
  26851. +#endif
  26852. mem_cgroup_charge_statistics(memcg, page, -1);
  26853. memcg_check_events(memcg, page);
  26854. if (!mem_cgroup_is_root(memcg))
  26855. css_put(&memcg->css);
  26856. + local_unlock_irqrestore(event_lock, flags);
  26857. }
  26858. /**
  26859. diff --git a/mm/mmu_context.c b/mm/mmu_context.c
  26860. index f802c2d216a7..b1b6f238e42d 100644
  26861. --- a/mm/mmu_context.c
  26862. +++ b/mm/mmu_context.c
  26863. @@ -23,6 +23,7 @@ void use_mm(struct mm_struct *mm)
  26864. struct task_struct *tsk = current;
  26865. task_lock(tsk);
  26866. + preempt_disable_rt();
  26867. active_mm = tsk->active_mm;
  26868. if (active_mm != mm) {
  26869. atomic_inc(&mm->mm_count);
  26870. @@ -30,6 +31,7 @@ void use_mm(struct mm_struct *mm)
  26871. }
  26872. tsk->mm = mm;
  26873. switch_mm(active_mm, mm, tsk);
  26874. + preempt_enable_rt();
  26875. task_unlock(tsk);
  26876. #ifdef finish_arch_post_lock_switch
  26877. finish_arch_post_lock_switch();
  26878. diff --git a/mm/page_alloc.c b/mm/page_alloc.c
  26879. index 2bcdfbf8c36d..a500c9e740dd 100644
  26880. --- a/mm/page_alloc.c
  26881. +++ b/mm/page_alloc.c
  26882. @@ -60,6 +60,7 @@
  26883. #include <linux/page_ext.h>
  26884. #include <linux/hugetlb.h>
  26885. #include <linux/sched/rt.h>
  26886. +#include <linux/locallock.h>
  26887. #include <linux/page_owner.h>
  26888. #include <linux/kthread.h>
  26889. @@ -264,6 +265,18 @@ EXPORT_SYMBOL(nr_node_ids);
  26890. EXPORT_SYMBOL(nr_online_nodes);
  26891. #endif
  26892. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  26893. +
  26894. +#ifdef CONFIG_PREEMPT_RT_BASE
  26895. +# define cpu_lock_irqsave(cpu, flags) \
  26896. + local_lock_irqsave_on(pa_lock, flags, cpu)
  26897. +# define cpu_unlock_irqrestore(cpu, flags) \
  26898. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  26899. +#else
  26900. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  26901. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  26902. +#endif
  26903. +
  26904. int page_group_by_mobility_disabled __read_mostly;
  26905. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  26906. @@ -786,7 +799,7 @@ static inline int free_pages_check(struct page *page)
  26907. }
  26908. /*
  26909. - * Frees a number of pages from the PCP lists
  26910. + * Frees a number of pages which have been collected from the pcp lists.
  26911. * Assumes all pages on list are in same zone, and of same order.
  26912. * count is the number of pages to free.
  26913. *
  26914. @@ -797,18 +810,53 @@ static inline int free_pages_check(struct page *page)
  26915. * pinned" detection logic.
  26916. */
  26917. static void free_pcppages_bulk(struct zone *zone, int count,
  26918. - struct per_cpu_pages *pcp)
  26919. + struct list_head *list)
  26920. {
  26921. - int migratetype = 0;
  26922. - int batch_free = 0;
  26923. int to_free = count;
  26924. unsigned long nr_scanned;
  26925. + unsigned long flags;
  26926. +
  26927. + spin_lock_irqsave(&zone->lock, flags);
  26928. - spin_lock(&zone->lock);
  26929. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  26930. if (nr_scanned)
  26931. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  26932. + while (!list_empty(list)) {
  26933. + struct page *page = list_first_entry(list, struct page, lru);
  26934. + int mt; /* migratetype of the to-be-freed page */
  26935. +
  26936. + /* must delete as __free_one_page list manipulates */
  26937. + list_del(&page->lru);
  26938. +
  26939. + mt = get_pcppage_migratetype(page);
  26940. + /* MIGRATE_ISOLATE page should not go to pcplists */
  26941. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26942. + /* Pageblock could have been isolated meanwhile */
  26943. + if (unlikely(has_isolate_pageblock(zone)))
  26944. + mt = get_pageblock_migratetype(page);
  26945. +
  26946. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26947. + trace_mm_page_pcpu_drain(page, 0, mt);
  26948. + to_free--;
  26949. + }
  26950. + WARN_ON(to_free != 0);
  26951. + spin_unlock_irqrestore(&zone->lock, flags);
  26952. +}
  26953. +
  26954. +/*
  26955. + * Moves a number of pages from the PCP lists to free list which
  26956. + * is freed outside of the locked region.
  26957. + *
  26958. + * Assumes all pages on list are in same zone, and of same order.
  26959. + * count is the number of pages to free.
  26960. + */
  26961. +static void isolate_pcp_pages(int to_free, struct per_cpu_pages *src,
  26962. + struct list_head *dst)
  26963. +{
  26964. + int migratetype = 0;
  26965. + int batch_free = 0;
  26966. +
  26967. while (to_free) {
  26968. struct page *page;
  26969. struct list_head *list;
  26970. @@ -824,7 +872,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  26971. batch_free++;
  26972. if (++migratetype == MIGRATE_PCPTYPES)
  26973. migratetype = 0;
  26974. - list = &pcp->lists[migratetype];
  26975. + list = &src->lists[migratetype];
  26976. } while (list_empty(list));
  26977. /* This is the only non-empty list. Free them all. */
  26978. @@ -832,24 +880,12 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  26979. batch_free = to_free;
  26980. do {
  26981. - int mt; /* migratetype of the to-be-freed page */
  26982. -
  26983. - page = list_entry(list->prev, struct page, lru);
  26984. - /* must delete as __free_one_page list manipulates */
  26985. + page = list_last_entry(list, struct page, lru);
  26986. list_del(&page->lru);
  26987. - mt = get_pcppage_migratetype(page);
  26988. - /* MIGRATE_ISOLATE page should not go to pcplists */
  26989. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  26990. - /* Pageblock could have been isolated meanwhile */
  26991. - if (unlikely(has_isolate_pageblock(zone)))
  26992. - mt = get_pageblock_migratetype(page);
  26993. -
  26994. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  26995. - trace_mm_page_pcpu_drain(page, 0, mt);
  26996. + list_add(&page->lru, dst);
  26997. } while (--to_free && --batch_free && !list_empty(list));
  26998. }
  26999. - spin_unlock(&zone->lock);
  27000. }
  27001. static void free_one_page(struct zone *zone,
  27002. @@ -858,7 +894,9 @@ static void free_one_page(struct zone *zone,
  27003. int migratetype)
  27004. {
  27005. unsigned long nr_scanned;
  27006. - spin_lock(&zone->lock);
  27007. + unsigned long flags;
  27008. +
  27009. + spin_lock_irqsave(&zone->lock, flags);
  27010. nr_scanned = zone_page_state(zone, NR_PAGES_SCANNED);
  27011. if (nr_scanned)
  27012. __mod_zone_page_state(zone, NR_PAGES_SCANNED, -nr_scanned);
  27013. @@ -868,7 +906,7 @@ static void free_one_page(struct zone *zone,
  27014. migratetype = get_pfnblock_migratetype(page, pfn);
  27015. }
  27016. __free_one_page(page, pfn, zone, order, migratetype);
  27017. - spin_unlock(&zone->lock);
  27018. + spin_unlock_irqrestore(&zone->lock, flags);
  27019. }
  27020. static int free_tail_pages_check(struct page *head_page, struct page *page)
  27021. @@ -1019,10 +1057,10 @@ static void __free_pages_ok(struct page *page, unsigned int order)
  27022. return;
  27023. migratetype = get_pfnblock_migratetype(page, pfn);
  27024. - local_irq_save(flags);
  27025. + local_lock_irqsave(pa_lock, flags);
  27026. __count_vm_events(PGFREE, 1 << order);
  27027. free_one_page(page_zone(page), page, pfn, order, migratetype);
  27028. - local_irq_restore(flags);
  27029. + local_unlock_irqrestore(pa_lock, flags);
  27030. }
  27031. static void __init __free_pages_boot_core(struct page *page,
  27032. @@ -1879,16 +1917,18 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
  27033. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  27034. {
  27035. unsigned long flags;
  27036. + LIST_HEAD(dst);
  27037. int to_drain, batch;
  27038. - local_irq_save(flags);
  27039. + local_lock_irqsave(pa_lock, flags);
  27040. batch = READ_ONCE(pcp->batch);
  27041. to_drain = min(pcp->count, batch);
  27042. if (to_drain > 0) {
  27043. - free_pcppages_bulk(zone, to_drain, pcp);
  27044. + isolate_pcp_pages(to_drain, pcp, &dst);
  27045. pcp->count -= to_drain;
  27046. }
  27047. - local_irq_restore(flags);
  27048. + local_unlock_irqrestore(pa_lock, flags);
  27049. + free_pcppages_bulk(zone, to_drain, &dst);
  27050. }
  27051. #endif
  27052. @@ -1904,16 +1944,21 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
  27053. unsigned long flags;
  27054. struct per_cpu_pageset *pset;
  27055. struct per_cpu_pages *pcp;
  27056. + LIST_HEAD(dst);
  27057. + int count;
  27058. - local_irq_save(flags);
  27059. + cpu_lock_irqsave(cpu, flags);
  27060. pset = per_cpu_ptr(zone->pageset, cpu);
  27061. pcp = &pset->pcp;
  27062. - if (pcp->count) {
  27063. - free_pcppages_bulk(zone, pcp->count, pcp);
  27064. + count = pcp->count;
  27065. + if (count) {
  27066. + isolate_pcp_pages(count, pcp, &dst);
  27067. pcp->count = 0;
  27068. }
  27069. - local_irq_restore(flags);
  27070. + cpu_unlock_irqrestore(cpu, flags);
  27071. + if (count)
  27072. + free_pcppages_bulk(zone, count, &dst);
  27073. }
  27074. /*
  27075. @@ -1999,8 +2044,17 @@ void drain_all_pages(struct zone *zone)
  27076. else
  27077. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  27078. }
  27079. +#ifndef CONFIG_PREEMPT_RT_BASE
  27080. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  27081. zone, 1);
  27082. +#else
  27083. + for_each_cpu(cpu, &cpus_with_pcps) {
  27084. + if (zone)
  27085. + drain_pages_zone(cpu, zone);
  27086. + else
  27087. + drain_pages(cpu);
  27088. + }
  27089. +#endif
  27090. }
  27091. #ifdef CONFIG_HIBERNATION
  27092. @@ -2056,7 +2110,7 @@ void free_hot_cold_page(struct page *page, bool cold)
  27093. migratetype = get_pfnblock_migratetype(page, pfn);
  27094. set_pcppage_migratetype(page, migratetype);
  27095. - local_irq_save(flags);
  27096. + local_lock_irqsave(pa_lock, flags);
  27097. __count_vm_event(PGFREE);
  27098. /*
  27099. @@ -2082,12 +2136,17 @@ void free_hot_cold_page(struct page *page, bool cold)
  27100. pcp->count++;
  27101. if (pcp->count >= pcp->high) {
  27102. unsigned long batch = READ_ONCE(pcp->batch);
  27103. - free_pcppages_bulk(zone, batch, pcp);
  27104. + LIST_HEAD(dst);
  27105. +
  27106. + isolate_pcp_pages(batch, pcp, &dst);
  27107. pcp->count -= batch;
  27108. + local_unlock_irqrestore(pa_lock, flags);
  27109. + free_pcppages_bulk(zone, batch, &dst);
  27110. + return;
  27111. }
  27112. out:
  27113. - local_irq_restore(flags);
  27114. + local_unlock_irqrestore(pa_lock, flags);
  27115. }
  27116. /*
  27117. @@ -2222,7 +2281,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27118. struct per_cpu_pages *pcp;
  27119. struct list_head *list;
  27120. - local_irq_save(flags);
  27121. + local_lock_irqsave(pa_lock, flags);
  27122. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  27123. list = &pcp->lists[migratetype];
  27124. if (list_empty(list)) {
  27125. @@ -2254,7 +2313,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27126. */
  27127. WARN_ON_ONCE(order > 1);
  27128. }
  27129. - spin_lock_irqsave(&zone->lock, flags);
  27130. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  27131. page = NULL;
  27132. if (alloc_flags & ALLOC_HARDER) {
  27133. @@ -2264,11 +2323,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27134. }
  27135. if (!page)
  27136. page = __rmqueue(zone, order, migratetype, gfp_flags);
  27137. - spin_unlock(&zone->lock);
  27138. - if (!page)
  27139. + if (!page) {
  27140. + spin_unlock(&zone->lock);
  27141. goto failed;
  27142. + }
  27143. __mod_zone_freepage_state(zone, -(1 << order),
  27144. get_pcppage_migratetype(page));
  27145. + spin_unlock(&zone->lock);
  27146. }
  27147. __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
  27148. @@ -2278,13 +2339,13 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  27149. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  27150. zone_statistics(preferred_zone, zone, gfp_flags);
  27151. - local_irq_restore(flags);
  27152. + local_unlock_irqrestore(pa_lock, flags);
  27153. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  27154. return page;
  27155. failed:
  27156. - local_irq_restore(flags);
  27157. + local_unlock_irqrestore(pa_lock, flags);
  27158. return NULL;
  27159. }
  27160. @@ -5950,6 +6011,7 @@ static int page_alloc_cpu_notify(struct notifier_block *self,
  27161. void __init page_alloc_init(void)
  27162. {
  27163. hotcpu_notifier(page_alloc_cpu_notify, 0);
  27164. + local_irq_lock_init(pa_lock);
  27165. }
  27166. /*
  27167. @@ -6844,7 +6906,7 @@ void zone_pcp_reset(struct zone *zone)
  27168. struct per_cpu_pageset *pset;
  27169. /* avoid races with drain_pages() */
  27170. - local_irq_save(flags);
  27171. + local_lock_irqsave(pa_lock, flags);
  27172. if (zone->pageset != &boot_pageset) {
  27173. for_each_online_cpu(cpu) {
  27174. pset = per_cpu_ptr(zone->pageset, cpu);
  27175. @@ -6853,7 +6915,7 @@ void zone_pcp_reset(struct zone *zone)
  27176. free_percpu(zone->pageset);
  27177. zone->pageset = &boot_pageset;
  27178. }
  27179. - local_irq_restore(flags);
  27180. + local_unlock_irqrestore(pa_lock, flags);
  27181. }
  27182. #ifdef CONFIG_MEMORY_HOTREMOVE
  27183. diff --git a/mm/slab.h b/mm/slab.h
  27184. index 7b6087197997..afdc57941179 100644
  27185. --- a/mm/slab.h
  27186. +++ b/mm/slab.h
  27187. @@ -324,7 +324,11 @@ static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
  27188. * The slab lists for all objects.
  27189. */
  27190. struct kmem_cache_node {
  27191. +#ifdef CONFIG_SLUB
  27192. + raw_spinlock_t list_lock;
  27193. +#else
  27194. spinlock_t list_lock;
  27195. +#endif
  27196. #ifdef CONFIG_SLAB
  27197. struct list_head slabs_partial; /* partial list first, better asm code */
  27198. diff --git a/mm/slub.c b/mm/slub.c
  27199. index 65d5f92d51d2..feb4a445a546 100644
  27200. --- a/mm/slub.c
  27201. +++ b/mm/slub.c
  27202. @@ -1075,7 +1075,7 @@ static noinline struct kmem_cache_node *free_debug_processing(
  27203. void *object = head;
  27204. int cnt = 0;
  27205. - spin_lock_irqsave(&n->list_lock, *flags);
  27206. + raw_spin_lock_irqsave(&n->list_lock, *flags);
  27207. slab_lock(page);
  27208. if (!check_slab(s, page))
  27209. @@ -1136,7 +1136,7 @@ out:
  27210. fail:
  27211. slab_unlock(page);
  27212. - spin_unlock_irqrestore(&n->list_lock, *flags);
  27213. + raw_spin_unlock_irqrestore(&n->list_lock, *flags);
  27214. slab_fix(s, "Object at 0x%p not freed", object);
  27215. return NULL;
  27216. }
  27217. @@ -1263,6 +1263,12 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
  27218. #endif /* CONFIG_SLUB_DEBUG */
  27219. +struct slub_free_list {
  27220. + raw_spinlock_t lock;
  27221. + struct list_head list;
  27222. +};
  27223. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  27224. +
  27225. /*
  27226. * Hooks for other subsystems that check memory allocations. In a typical
  27227. * production configuration these hooks all should produce no code at all.
  27228. @@ -1399,10 +1405,17 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  27229. gfp_t alloc_gfp;
  27230. void *start, *p;
  27231. int idx, order;
  27232. + bool enableirqs = false;
  27233. flags &= gfp_allowed_mask;
  27234. if (gfpflags_allow_blocking(flags))
  27235. + enableirqs = true;
  27236. +#ifdef CONFIG_PREEMPT_RT_FULL
  27237. + if (system_state == SYSTEM_RUNNING)
  27238. + enableirqs = true;
  27239. +#endif
  27240. + if (enableirqs)
  27241. local_irq_enable();
  27242. flags |= s->allocflags;
  27243. @@ -1473,7 +1486,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  27244. page->frozen = 1;
  27245. out:
  27246. - if (gfpflags_allow_blocking(flags))
  27247. + if (enableirqs)
  27248. local_irq_disable();
  27249. if (!page)
  27250. return NULL;
  27251. @@ -1529,6 +1542,16 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
  27252. __free_kmem_pages(page, order);
  27253. }
  27254. +static void free_delayed(struct list_head *h)
  27255. +{
  27256. + while(!list_empty(h)) {
  27257. + struct page *page = list_first_entry(h, struct page, lru);
  27258. +
  27259. + list_del(&page->lru);
  27260. + __free_slab(page->slab_cache, page);
  27261. + }
  27262. +}
  27263. +
  27264. #define need_reserve_slab_rcu \
  27265. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  27266. @@ -1560,6 +1583,12 @@ static void free_slab(struct kmem_cache *s, struct page *page)
  27267. }
  27268. call_rcu(head, rcu_free_slab);
  27269. + } else if (irqs_disabled()) {
  27270. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  27271. +
  27272. + raw_spin_lock(&f->lock);
  27273. + list_add(&page->lru, &f->list);
  27274. + raw_spin_unlock(&f->lock);
  27275. } else
  27276. __free_slab(s, page);
  27277. }
  27278. @@ -1673,7 +1702,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  27279. if (!n || !n->nr_partial)
  27280. return NULL;
  27281. - spin_lock(&n->list_lock);
  27282. + raw_spin_lock(&n->list_lock);
  27283. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  27284. void *t;
  27285. @@ -1698,7 +1727,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  27286. break;
  27287. }
  27288. - spin_unlock(&n->list_lock);
  27289. + raw_spin_unlock(&n->list_lock);
  27290. return object;
  27291. }
  27292. @@ -1944,7 +1973,7 @@ redo:
  27293. * that acquire_slab() will see a slab page that
  27294. * is frozen
  27295. */
  27296. - spin_lock(&n->list_lock);
  27297. + raw_spin_lock(&n->list_lock);
  27298. }
  27299. } else {
  27300. m = M_FULL;
  27301. @@ -1955,7 +1984,7 @@ redo:
  27302. * slabs from diagnostic functions will not see
  27303. * any frozen slabs.
  27304. */
  27305. - spin_lock(&n->list_lock);
  27306. + raw_spin_lock(&n->list_lock);
  27307. }
  27308. }
  27309. @@ -1990,7 +2019,7 @@ redo:
  27310. goto redo;
  27311. if (lock)
  27312. - spin_unlock(&n->list_lock);
  27313. + raw_spin_unlock(&n->list_lock);
  27314. if (m == M_FREE) {
  27315. stat(s, DEACTIVATE_EMPTY);
  27316. @@ -2022,10 +2051,10 @@ static void unfreeze_partials(struct kmem_cache *s,
  27317. n2 = get_node(s, page_to_nid(page));
  27318. if (n != n2) {
  27319. if (n)
  27320. - spin_unlock(&n->list_lock);
  27321. + raw_spin_unlock(&n->list_lock);
  27322. n = n2;
  27323. - spin_lock(&n->list_lock);
  27324. + raw_spin_lock(&n->list_lock);
  27325. }
  27326. do {
  27327. @@ -2054,7 +2083,7 @@ static void unfreeze_partials(struct kmem_cache *s,
  27328. }
  27329. if (n)
  27330. - spin_unlock(&n->list_lock);
  27331. + raw_spin_unlock(&n->list_lock);
  27332. while (discard_page) {
  27333. page = discard_page;
  27334. @@ -2093,14 +2122,21 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  27335. pobjects = oldpage->pobjects;
  27336. pages = oldpage->pages;
  27337. if (drain && pobjects > s->cpu_partial) {
  27338. + struct slub_free_list *f;
  27339. unsigned long flags;
  27340. + LIST_HEAD(tofree);
  27341. /*
  27342. * partial array is full. Move the existing
  27343. * set to the per node partial list.
  27344. */
  27345. local_irq_save(flags);
  27346. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  27347. + f = this_cpu_ptr(&slub_free_list);
  27348. + raw_spin_lock(&f->lock);
  27349. + list_splice_init(&f->list, &tofree);
  27350. + raw_spin_unlock(&f->lock);
  27351. local_irq_restore(flags);
  27352. + free_delayed(&tofree);
  27353. oldpage = NULL;
  27354. pobjects = 0;
  27355. pages = 0;
  27356. @@ -2172,7 +2208,22 @@ static bool has_cpu_slab(int cpu, void *info)
  27357. static void flush_all(struct kmem_cache *s)
  27358. {
  27359. + LIST_HEAD(tofree);
  27360. + int cpu;
  27361. +
  27362. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  27363. + for_each_online_cpu(cpu) {
  27364. + struct slub_free_list *f;
  27365. +
  27366. + if (!has_cpu_slab(cpu, s))
  27367. + continue;
  27368. +
  27369. + f = &per_cpu(slub_free_list, cpu);
  27370. + raw_spin_lock_irq(&f->lock);
  27371. + list_splice_init(&f->list, &tofree);
  27372. + raw_spin_unlock_irq(&f->lock);
  27373. + free_delayed(&tofree);
  27374. + }
  27375. }
  27376. /*
  27377. @@ -2208,10 +2259,10 @@ static unsigned long count_partial(struct kmem_cache_node *n,
  27378. unsigned long x = 0;
  27379. struct page *page;
  27380. - spin_lock_irqsave(&n->list_lock, flags);
  27381. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27382. list_for_each_entry(page, &n->partial, lru)
  27383. x += get_count(page);
  27384. - spin_unlock_irqrestore(&n->list_lock, flags);
  27385. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27386. return x;
  27387. }
  27388. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  27389. @@ -2349,8 +2400,10 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  27390. * already disabled (which is the case for bulk allocation).
  27391. */
  27392. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  27393. - unsigned long addr, struct kmem_cache_cpu *c)
  27394. + unsigned long addr, struct kmem_cache_cpu *c,
  27395. + struct list_head *to_free)
  27396. {
  27397. + struct slub_free_list *f;
  27398. void *freelist;
  27399. struct page *page;
  27400. @@ -2410,6 +2463,13 @@ load_freelist:
  27401. VM_BUG_ON(!c->page->frozen);
  27402. c->freelist = get_freepointer(s, freelist);
  27403. c->tid = next_tid(c->tid);
  27404. +
  27405. +out:
  27406. + f = this_cpu_ptr(&slub_free_list);
  27407. + raw_spin_lock(&f->lock);
  27408. + list_splice_init(&f->list, to_free);
  27409. + raw_spin_unlock(&f->lock);
  27410. +
  27411. return freelist;
  27412. new_slab:
  27413. @@ -2441,7 +2501,7 @@ new_slab:
  27414. deactivate_slab(s, page, get_freepointer(s, freelist));
  27415. c->page = NULL;
  27416. c->freelist = NULL;
  27417. - return freelist;
  27418. + goto out;
  27419. }
  27420. /*
  27421. @@ -2453,6 +2513,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  27422. {
  27423. void *p;
  27424. unsigned long flags;
  27425. + LIST_HEAD(tofree);
  27426. local_irq_save(flags);
  27427. #ifdef CONFIG_PREEMPT
  27428. @@ -2464,8 +2525,9 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  27429. c = this_cpu_ptr(s->cpu_slab);
  27430. #endif
  27431. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  27432. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  27433. local_irq_restore(flags);
  27434. + free_delayed(&tofree);
  27435. return p;
  27436. }
  27437. @@ -2652,7 +2714,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  27438. do {
  27439. if (unlikely(n)) {
  27440. - spin_unlock_irqrestore(&n->list_lock, flags);
  27441. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27442. n = NULL;
  27443. }
  27444. prior = page->freelist;
  27445. @@ -2684,7 +2746,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  27446. * Otherwise the list_lock will synchronize with
  27447. * other processors updating the list of slabs.
  27448. */
  27449. - spin_lock_irqsave(&n->list_lock, flags);
  27450. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27451. }
  27452. }
  27453. @@ -2726,7 +2788,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  27454. add_partial(n, page, DEACTIVATE_TO_TAIL);
  27455. stat(s, FREE_ADD_PARTIAL);
  27456. }
  27457. - spin_unlock_irqrestore(&n->list_lock, flags);
  27458. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27459. return;
  27460. slab_empty:
  27461. @@ -2741,7 +2803,7 @@ slab_empty:
  27462. remove_full(s, n, page);
  27463. }
  27464. - spin_unlock_irqrestore(&n->list_lock, flags);
  27465. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27466. stat(s, FREE_SLAB);
  27467. discard_slab(s, page);
  27468. }
  27469. @@ -2913,6 +2975,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  27470. void **p)
  27471. {
  27472. struct kmem_cache_cpu *c;
  27473. + LIST_HEAD(to_free);
  27474. int i;
  27475. /* memcg and kmem_cache debug support */
  27476. @@ -2936,7 +2999,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  27477. * of re-populating per CPU c->freelist
  27478. */
  27479. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  27480. - _RET_IP_, c);
  27481. + _RET_IP_, c, &to_free);
  27482. if (unlikely(!p[i]))
  27483. goto error;
  27484. @@ -2948,6 +3011,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  27485. }
  27486. c->tid = next_tid(c->tid);
  27487. local_irq_enable();
  27488. + free_delayed(&to_free);
  27489. /* Clear memory outside IRQ disabled fastpath loop */
  27490. if (unlikely(flags & __GFP_ZERO)) {
  27491. @@ -3095,7 +3159,7 @@ static void
  27492. init_kmem_cache_node(struct kmem_cache_node *n)
  27493. {
  27494. n->nr_partial = 0;
  27495. - spin_lock_init(&n->list_lock);
  27496. + raw_spin_lock_init(&n->list_lock);
  27497. INIT_LIST_HEAD(&n->partial);
  27498. #ifdef CONFIG_SLUB_DEBUG
  27499. atomic_long_set(&n->nr_slabs, 0);
  27500. @@ -3677,7 +3741,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  27501. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  27502. INIT_LIST_HEAD(promote + i);
  27503. - spin_lock_irqsave(&n->list_lock, flags);
  27504. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27505. /*
  27506. * Build lists of slabs to discard or promote.
  27507. @@ -3708,7 +3772,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  27508. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  27509. list_splice(promote + i, &n->partial);
  27510. - spin_unlock_irqrestore(&n->list_lock, flags);
  27511. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27512. /* Release empty slabs */
  27513. list_for_each_entry_safe(page, t, &discard, lru)
  27514. @@ -3884,6 +3948,12 @@ void __init kmem_cache_init(void)
  27515. {
  27516. static __initdata struct kmem_cache boot_kmem_cache,
  27517. boot_kmem_cache_node;
  27518. + int cpu;
  27519. +
  27520. + for_each_possible_cpu(cpu) {
  27521. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  27522. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  27523. + }
  27524. if (debug_guardpage_minorder())
  27525. slub_max_order = 0;
  27526. @@ -4127,7 +4197,7 @@ static int validate_slab_node(struct kmem_cache *s,
  27527. struct page *page;
  27528. unsigned long flags;
  27529. - spin_lock_irqsave(&n->list_lock, flags);
  27530. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27531. list_for_each_entry(page, &n->partial, lru) {
  27532. validate_slab_slab(s, page, map);
  27533. @@ -4149,7 +4219,7 @@ static int validate_slab_node(struct kmem_cache *s,
  27534. s->name, count, atomic_long_read(&n->nr_slabs));
  27535. out:
  27536. - spin_unlock_irqrestore(&n->list_lock, flags);
  27537. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27538. return count;
  27539. }
  27540. @@ -4337,12 +4407,12 @@ static int list_locations(struct kmem_cache *s, char *buf,
  27541. if (!atomic_long_read(&n->nr_slabs))
  27542. continue;
  27543. - spin_lock_irqsave(&n->list_lock, flags);
  27544. + raw_spin_lock_irqsave(&n->list_lock, flags);
  27545. list_for_each_entry(page, &n->partial, lru)
  27546. process_slab(&t, s, page, alloc, map);
  27547. list_for_each_entry(page, &n->full, lru)
  27548. process_slab(&t, s, page, alloc, map);
  27549. - spin_unlock_irqrestore(&n->list_lock, flags);
  27550. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  27551. }
  27552. for (i = 0; i < t.count; i++) {
  27553. diff --git a/mm/swap.c b/mm/swap.c
  27554. index 39395fb549c0..ad16649221d7 100644
  27555. --- a/mm/swap.c
  27556. +++ b/mm/swap.c
  27557. @@ -31,6 +31,7 @@
  27558. #include <linux/memcontrol.h>
  27559. #include <linux/gfp.h>
  27560. #include <linux/uio.h>
  27561. +#include <linux/locallock.h>
  27562. #include <linux/hugetlb.h>
  27563. #include <linux/page_idle.h>
  27564. @@ -46,6 +47,9 @@ static DEFINE_PER_CPU(struct pagevec, lru_add_pvec);
  27565. static DEFINE_PER_CPU(struct pagevec, lru_rotate_pvecs);
  27566. static DEFINE_PER_CPU(struct pagevec, lru_deactivate_file_pvecs);
  27567. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  27568. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  27569. +
  27570. /*
  27571. * This path almost never happens for VM activity - pages are normally
  27572. * freed via pagevecs. But it gets used by networking.
  27573. @@ -481,11 +485,11 @@ void rotate_reclaimable_page(struct page *page)
  27574. unsigned long flags;
  27575. page_cache_get(page);
  27576. - local_irq_save(flags);
  27577. + local_lock_irqsave(rotate_lock, flags);
  27578. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  27579. if (!pagevec_add(pvec, page))
  27580. pagevec_move_tail(pvec);
  27581. - local_irq_restore(flags);
  27582. + local_unlock_irqrestore(rotate_lock, flags);
  27583. }
  27584. }
  27585. @@ -536,12 +540,13 @@ static bool need_activate_page_drain(int cpu)
  27586. void activate_page(struct page *page)
  27587. {
  27588. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  27589. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  27590. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27591. + activate_page_pvecs);
  27592. page_cache_get(page);
  27593. if (!pagevec_add(pvec, page))
  27594. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  27595. - put_cpu_var(activate_page_pvecs);
  27596. + put_locked_var(swapvec_lock, activate_page_pvecs);
  27597. }
  27598. }
  27599. @@ -567,7 +572,7 @@ void activate_page(struct page *page)
  27600. static void __lru_cache_activate_page(struct page *page)
  27601. {
  27602. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27603. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27604. int i;
  27605. /*
  27606. @@ -589,7 +594,7 @@ static void __lru_cache_activate_page(struct page *page)
  27607. }
  27608. }
  27609. - put_cpu_var(lru_add_pvec);
  27610. + put_locked_var(swapvec_lock, lru_add_pvec);
  27611. }
  27612. /*
  27613. @@ -630,13 +635,13 @@ EXPORT_SYMBOL(mark_page_accessed);
  27614. static void __lru_cache_add(struct page *page)
  27615. {
  27616. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  27617. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  27618. page_cache_get(page);
  27619. if (!pagevec_space(pvec))
  27620. __pagevec_lru_add(pvec);
  27621. pagevec_add(pvec, page);
  27622. - put_cpu_var(lru_add_pvec);
  27623. + put_locked_var(swapvec_lock, lru_add_pvec);
  27624. }
  27625. /**
  27626. @@ -816,9 +821,15 @@ void lru_add_drain_cpu(int cpu)
  27627. unsigned long flags;
  27628. /* No harm done if a racing interrupt already did this */
  27629. - local_irq_save(flags);
  27630. +#ifdef CONFIG_PREEMPT_RT_BASE
  27631. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  27632. + pagevec_move_tail(pvec);
  27633. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  27634. +#else
  27635. + local_lock_irqsave(rotate_lock, flags);
  27636. pagevec_move_tail(pvec);
  27637. - local_irq_restore(flags);
  27638. + local_unlock_irqrestore(rotate_lock, flags);
  27639. +#endif
  27640. }
  27641. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  27642. @@ -846,26 +857,47 @@ void deactivate_file_page(struct page *page)
  27643. return;
  27644. if (likely(get_page_unless_zero(page))) {
  27645. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  27646. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  27647. + lru_deactivate_file_pvecs);
  27648. if (!pagevec_add(pvec, page))
  27649. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  27650. - put_cpu_var(lru_deactivate_file_pvecs);
  27651. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  27652. }
  27653. }
  27654. void lru_add_drain(void)
  27655. {
  27656. - lru_add_drain_cpu(get_cpu());
  27657. - put_cpu();
  27658. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  27659. + local_unlock_cpu(swapvec_lock);
  27660. }
  27661. +
  27662. +#ifdef CONFIG_PREEMPT_RT_BASE
  27663. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27664. +{
  27665. + local_lock_on(swapvec_lock, cpu);
  27666. + lru_add_drain_cpu(cpu);
  27667. + local_unlock_on(swapvec_lock, cpu);
  27668. +}
  27669. +
  27670. +#else
  27671. +
  27672. static void lru_add_drain_per_cpu(struct work_struct *dummy)
  27673. {
  27674. lru_add_drain();
  27675. }
  27676. static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  27677. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  27678. +{
  27679. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27680. +
  27681. + INIT_WORK(work, lru_add_drain_per_cpu);
  27682. + schedule_work_on(cpu, work);
  27683. + cpumask_set_cpu(cpu, has_work);
  27684. +}
  27685. +#endif
  27686. void lru_add_drain_all(void)
  27687. {
  27688. @@ -878,20 +910,17 @@ void lru_add_drain_all(void)
  27689. cpumask_clear(&has_work);
  27690. for_each_online_cpu(cpu) {
  27691. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  27692. -
  27693. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  27694. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  27695. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  27696. - need_activate_page_drain(cpu)) {
  27697. - INIT_WORK(work, lru_add_drain_per_cpu);
  27698. - schedule_work_on(cpu, work);
  27699. - cpumask_set_cpu(cpu, &has_work);
  27700. - }
  27701. + need_activate_page_drain(cpu))
  27702. + remote_lru_add_drain(cpu, &has_work);
  27703. }
  27704. +#ifndef CONFIG_PREEMPT_RT_BASE
  27705. for_each_cpu(cpu, &has_work)
  27706. flush_work(&per_cpu(lru_add_drain_work, cpu));
  27707. +#endif
  27708. put_online_cpus();
  27709. mutex_unlock(&lock);
  27710. diff --git a/mm/truncate.c b/mm/truncate.c
  27711. index 76e35ad97102..5f196420020c 100644
  27712. --- a/mm/truncate.c
  27713. +++ b/mm/truncate.c
  27714. @@ -56,8 +56,11 @@ static void clear_exceptional_entry(struct address_space *mapping,
  27715. * protected by mapping->tree_lock.
  27716. */
  27717. if (!workingset_node_shadows(node) &&
  27718. - !list_empty(&node->private_list))
  27719. - list_lru_del(&workingset_shadow_nodes, &node->private_list);
  27720. + !list_empty(&node->private_list)) {
  27721. + local_lock(workingset_shadow_lock);
  27722. + list_lru_del(&__workingset_shadow_nodes, &node->private_list);
  27723. + local_unlock(workingset_shadow_lock);
  27724. + }
  27725. __radix_tree_delete_node(&mapping->page_tree, node);
  27726. unlock:
  27727. spin_unlock_irq(&mapping->tree_lock);
  27728. diff --git a/mm/vmalloc.c b/mm/vmalloc.c
  27729. index 8e3c9c5a3042..68740314ad54 100644
  27730. --- a/mm/vmalloc.c
  27731. +++ b/mm/vmalloc.c
  27732. @@ -821,7 +821,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  27733. struct vmap_block *vb;
  27734. struct vmap_area *va;
  27735. unsigned long vb_idx;
  27736. - int node, err;
  27737. + int node, err, cpu;
  27738. void *vaddr;
  27739. node = numa_node_id();
  27740. @@ -864,11 +864,12 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  27741. BUG_ON(err);
  27742. radix_tree_preload_end();
  27743. - vbq = &get_cpu_var(vmap_block_queue);
  27744. + cpu = get_cpu_light();
  27745. + vbq = this_cpu_ptr(&vmap_block_queue);
  27746. spin_lock(&vbq->lock);
  27747. list_add_tail_rcu(&vb->free_list, &vbq->free);
  27748. spin_unlock(&vbq->lock);
  27749. - put_cpu_var(vmap_block_queue);
  27750. + put_cpu_light();
  27751. return vaddr;
  27752. }
  27753. @@ -937,6 +938,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  27754. struct vmap_block *vb;
  27755. void *vaddr = NULL;
  27756. unsigned int order;
  27757. + int cpu;
  27758. BUG_ON(offset_in_page(size));
  27759. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  27760. @@ -951,7 +953,8 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  27761. order = get_order(size);
  27762. rcu_read_lock();
  27763. - vbq = &get_cpu_var(vmap_block_queue);
  27764. + cpu = get_cpu_light();
  27765. + vbq = this_cpu_ptr(&vmap_block_queue);
  27766. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  27767. unsigned long pages_off;
  27768. @@ -974,7 +977,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  27769. break;
  27770. }
  27771. - put_cpu_var(vmap_block_queue);
  27772. + put_cpu_light();
  27773. rcu_read_unlock();
  27774. /* Allocate new block if nothing was found */
  27775. diff --git a/mm/vmstat.c b/mm/vmstat.c
  27776. index c54fd2924f25..64416fd7c209 100644
  27777. --- a/mm/vmstat.c
  27778. +++ b/mm/vmstat.c
  27779. @@ -226,6 +226,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  27780. long x;
  27781. long t;
  27782. + preempt_disable_rt();
  27783. x = delta + __this_cpu_read(*p);
  27784. t = __this_cpu_read(pcp->stat_threshold);
  27785. @@ -235,6 +236,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  27786. x = 0;
  27787. }
  27788. __this_cpu_write(*p, x);
  27789. + preempt_enable_rt();
  27790. }
  27791. EXPORT_SYMBOL(__mod_zone_page_state);
  27792. @@ -267,6 +269,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  27793. s8 __percpu *p = pcp->vm_stat_diff + item;
  27794. s8 v, t;
  27795. + preempt_disable_rt();
  27796. v = __this_cpu_inc_return(*p);
  27797. t = __this_cpu_read(pcp->stat_threshold);
  27798. if (unlikely(v > t)) {
  27799. @@ -275,6 +278,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  27800. zone_page_state_add(v + overstep, zone, item);
  27801. __this_cpu_write(*p, -overstep);
  27802. }
  27803. + preempt_enable_rt();
  27804. }
  27805. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  27806. @@ -289,6 +293,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  27807. s8 __percpu *p = pcp->vm_stat_diff + item;
  27808. s8 v, t;
  27809. + preempt_disable_rt();
  27810. v = __this_cpu_dec_return(*p);
  27811. t = __this_cpu_read(pcp->stat_threshold);
  27812. if (unlikely(v < - t)) {
  27813. @@ -297,6 +302,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  27814. zone_page_state_add(v - overstep, zone, item);
  27815. __this_cpu_write(*p, overstep);
  27816. }
  27817. + preempt_enable_rt();
  27818. }
  27819. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  27820. diff --git a/mm/workingset.c b/mm/workingset.c
  27821. index aa017133744b..263d0194734a 100644
  27822. --- a/mm/workingset.c
  27823. +++ b/mm/workingset.c
  27824. @@ -264,7 +264,8 @@ void workingset_activation(struct page *page)
  27825. * point where they would still be useful.
  27826. */
  27827. -struct list_lru workingset_shadow_nodes;
  27828. +struct list_lru __workingset_shadow_nodes;
  27829. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  27830. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  27831. struct shrink_control *sc)
  27832. @@ -274,9 +275,9 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  27833. unsigned long pages;
  27834. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27835. - local_irq_disable();
  27836. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  27837. - local_irq_enable();
  27838. + local_lock_irq(workingset_shadow_lock);
  27839. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  27840. + local_unlock_irq(workingset_shadow_lock);
  27841. pages = node_present_pages(sc->nid);
  27842. /*
  27843. @@ -363,9 +364,9 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
  27844. spin_unlock(&mapping->tree_lock);
  27845. ret = LRU_REMOVED_RETRY;
  27846. out:
  27847. - local_irq_enable();
  27848. + local_unlock_irq(workingset_shadow_lock);
  27849. cond_resched();
  27850. - local_irq_disable();
  27851. + local_lock_irq(workingset_shadow_lock);
  27852. spin_lock(lru_lock);
  27853. return ret;
  27854. }
  27855. @@ -376,10 +377,10 @@ static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  27856. unsigned long ret;
  27857. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  27858. - local_irq_disable();
  27859. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  27860. + local_lock_irq(workingset_shadow_lock);
  27861. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  27862. shadow_lru_isolate, NULL);
  27863. - local_irq_enable();
  27864. + local_unlock_irq(workingset_shadow_lock);
  27865. return ret;
  27866. }
  27867. @@ -400,7 +401,7 @@ static int __init workingset_init(void)
  27868. {
  27869. int ret;
  27870. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  27871. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  27872. if (ret)
  27873. goto err;
  27874. ret = register_shrinker(&workingset_shadow_shrinker);
  27875. @@ -408,7 +409,7 @@ static int __init workingset_init(void)
  27876. goto err_list_lru;
  27877. return 0;
  27878. err_list_lru:
  27879. - list_lru_destroy(&workingset_shadow_nodes);
  27880. + list_lru_destroy(&__workingset_shadow_nodes);
  27881. err:
  27882. return ret;
  27883. }
  27884. diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
  27885. index c1ea19478119..b552fd607df8 100644
  27886. --- a/mm/zsmalloc.c
  27887. +++ b/mm/zsmalloc.c
  27888. @@ -1289,7 +1289,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  27889. class = pool->size_class[class_idx];
  27890. off = obj_idx_to_offset(page, obj_idx, class->size);
  27891. - area = &get_cpu_var(zs_map_area);
  27892. + area = per_cpu_ptr(&zs_map_area, get_cpu_light());
  27893. area->vm_mm = mm;
  27894. if (off + class->size <= PAGE_SIZE) {
  27895. /* this object is contained entirely within a page */
  27896. @@ -1342,7 +1342,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  27897. __zs_unmap_object(area, pages, off, class->size);
  27898. }
  27899. - put_cpu_var(zs_map_area);
  27900. + put_cpu_light();
  27901. unpin_tag(handle);
  27902. }
  27903. EXPORT_SYMBOL_GPL(zs_unmap_object);
  27904. diff --git a/net/core/dev.c b/net/core/dev.c
  27905. index 9efbdb3ff78a..0e17592adbff 100644
  27906. --- a/net/core/dev.c
  27907. +++ b/net/core/dev.c
  27908. @@ -186,6 +186,7 @@ static unsigned int napi_gen_id;
  27909. static DEFINE_HASHTABLE(napi_hash, 8);
  27910. static seqcount_t devnet_rename_seq;
  27911. +static DEFINE_MUTEX(devnet_rename_mutex);
  27912. static inline void dev_base_seq_inc(struct net *net)
  27913. {
  27914. @@ -207,14 +208,14 @@ static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  27915. static inline void rps_lock(struct softnet_data *sd)
  27916. {
  27917. #ifdef CONFIG_RPS
  27918. - spin_lock(&sd->input_pkt_queue.lock);
  27919. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  27920. #endif
  27921. }
  27922. static inline void rps_unlock(struct softnet_data *sd)
  27923. {
  27924. #ifdef CONFIG_RPS
  27925. - spin_unlock(&sd->input_pkt_queue.lock);
  27926. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  27927. #endif
  27928. }
  27929. @@ -884,7 +885,8 @@ retry:
  27930. strcpy(name, dev->name);
  27931. rcu_read_unlock();
  27932. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  27933. - cond_resched();
  27934. + mutex_lock(&devnet_rename_mutex);
  27935. + mutex_unlock(&devnet_rename_mutex);
  27936. goto retry;
  27937. }
  27938. @@ -1153,20 +1155,17 @@ int dev_change_name(struct net_device *dev, const char *newname)
  27939. if (dev->flags & IFF_UP)
  27940. return -EBUSY;
  27941. - write_seqcount_begin(&devnet_rename_seq);
  27942. + mutex_lock(&devnet_rename_mutex);
  27943. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27944. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  27945. - write_seqcount_end(&devnet_rename_seq);
  27946. - return 0;
  27947. - }
  27948. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  27949. + goto outunlock;
  27950. memcpy(oldname, dev->name, IFNAMSIZ);
  27951. err = dev_get_valid_name(net, dev, newname);
  27952. - if (err < 0) {
  27953. - write_seqcount_end(&devnet_rename_seq);
  27954. - return err;
  27955. - }
  27956. + if (err < 0)
  27957. + goto outunlock;
  27958. if (oldname[0] && !strchr(oldname, '%'))
  27959. netdev_info(dev, "renamed from %s\n", oldname);
  27960. @@ -1179,11 +1178,12 @@ rollback:
  27961. if (ret) {
  27962. memcpy(dev->name, oldname, IFNAMSIZ);
  27963. dev->name_assign_type = old_assign_type;
  27964. - write_seqcount_end(&devnet_rename_seq);
  27965. - return ret;
  27966. + err = ret;
  27967. + goto outunlock;
  27968. }
  27969. - write_seqcount_end(&devnet_rename_seq);
  27970. + __raw_write_seqcount_end(&devnet_rename_seq);
  27971. + mutex_unlock(&devnet_rename_mutex);
  27972. netdev_adjacent_rename_links(dev, oldname);
  27973. @@ -1204,7 +1204,8 @@ rollback:
  27974. /* err >= 0 after dev_alloc_name() or stores the first errno */
  27975. if (err >= 0) {
  27976. err = ret;
  27977. - write_seqcount_begin(&devnet_rename_seq);
  27978. + mutex_lock(&devnet_rename_mutex);
  27979. + __raw_write_seqcount_begin(&devnet_rename_seq);
  27980. memcpy(dev->name, oldname, IFNAMSIZ);
  27981. memcpy(oldname, newname, IFNAMSIZ);
  27982. dev->name_assign_type = old_assign_type;
  27983. @@ -1217,6 +1218,11 @@ rollback:
  27984. }
  27985. return err;
  27986. +
  27987. +outunlock:
  27988. + __raw_write_seqcount_end(&devnet_rename_seq);
  27989. + mutex_unlock(&devnet_rename_mutex);
  27990. + return err;
  27991. }
  27992. /**
  27993. @@ -2246,6 +2252,7 @@ static inline void __netif_reschedule(struct Qdisc *q)
  27994. sd->output_queue_tailp = &q->next_sched;
  27995. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  27996. local_irq_restore(flags);
  27997. + preempt_check_resched_rt();
  27998. }
  27999. void __netif_schedule(struct Qdisc *q)
  28000. @@ -2327,6 +2334,7 @@ void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  28001. __this_cpu_write(softnet_data.completion_queue, skb);
  28002. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  28003. local_irq_restore(flags);
  28004. + preempt_check_resched_rt();
  28005. }
  28006. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  28007. @@ -2883,7 +2891,11 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  28008. * This permits __QDISC___STATE_RUNNING owner to get the lock more
  28009. * often and dequeue packets faster.
  28010. */
  28011. +#ifdef CONFIG_PREEMPT_RT_FULL
  28012. + contended = true;
  28013. +#else
  28014. contended = qdisc_is_running(q);
  28015. +#endif
  28016. if (unlikely(contended))
  28017. spin_lock(&q->busylock);
  28018. @@ -2943,9 +2955,44 @@ static void skb_update_prio(struct sk_buff *skb)
  28019. #define skb_update_prio(skb)
  28020. #endif
  28021. +#ifdef CONFIG_PREEMPT_RT_FULL
  28022. +
  28023. +static inline int xmit_rec_read(void)
  28024. +{
  28025. + return current->xmit_recursion;
  28026. +}
  28027. +
  28028. +static inline void xmit_rec_inc(void)
  28029. +{
  28030. + current->xmit_recursion++;
  28031. +}
  28032. +
  28033. +static inline void xmit_rec_dec(void)
  28034. +{
  28035. + current->xmit_recursion--;
  28036. +}
  28037. +
  28038. +#else
  28039. +
  28040. DEFINE_PER_CPU(int, xmit_recursion);
  28041. EXPORT_SYMBOL(xmit_recursion);
  28042. +static inline int xmit_rec_read(void)
  28043. +{
  28044. + return __this_cpu_read(xmit_recursion);
  28045. +}
  28046. +
  28047. +static inline void xmit_rec_inc(void)
  28048. +{
  28049. + __this_cpu_inc(xmit_recursion);
  28050. +}
  28051. +
  28052. +static inline void xmit_rec_dec(void)
  28053. +{
  28054. + __this_cpu_dec(xmit_recursion);
  28055. +}
  28056. +#endif
  28057. +
  28058. #define RECURSION_LIMIT 10
  28059. /**
  28060. @@ -3138,7 +3185,7 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  28061. if (txq->xmit_lock_owner != cpu) {
  28062. - if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT)
  28063. + if (xmit_rec_read() > RECURSION_LIMIT)
  28064. goto recursion_alert;
  28065. skb = validate_xmit_skb(skb, dev);
  28066. @@ -3148,9 +3195,9 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  28067. HARD_TX_LOCK(dev, txq, cpu);
  28068. if (!netif_xmit_stopped(txq)) {
  28069. - __this_cpu_inc(xmit_recursion);
  28070. + xmit_rec_inc();
  28071. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  28072. - __this_cpu_dec(xmit_recursion);
  28073. + xmit_rec_dec();
  28074. if (dev_xmit_complete(rc)) {
  28075. HARD_TX_UNLOCK(dev, txq);
  28076. goto out;
  28077. @@ -3524,6 +3571,7 @@ drop:
  28078. rps_unlock(sd);
  28079. local_irq_restore(flags);
  28080. + preempt_check_resched_rt();
  28081. atomic_long_inc(&skb->dev->rx_dropped);
  28082. kfree_skb(skb);
  28083. @@ -3542,7 +3590,7 @@ static int netif_rx_internal(struct sk_buff *skb)
  28084. struct rps_dev_flow voidflow, *rflow = &voidflow;
  28085. int cpu;
  28086. - preempt_disable();
  28087. + migrate_disable();
  28088. rcu_read_lock();
  28089. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  28090. @@ -3552,13 +3600,13 @@ static int netif_rx_internal(struct sk_buff *skb)
  28091. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  28092. rcu_read_unlock();
  28093. - preempt_enable();
  28094. + migrate_enable();
  28095. } else
  28096. #endif
  28097. {
  28098. unsigned int qtail;
  28099. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  28100. - put_cpu();
  28101. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  28102. + put_cpu_light();
  28103. }
  28104. return ret;
  28105. }
  28106. @@ -3592,16 +3640,44 @@ int netif_rx_ni(struct sk_buff *skb)
  28107. trace_netif_rx_ni_entry(skb);
  28108. - preempt_disable();
  28109. + local_bh_disable();
  28110. err = netif_rx_internal(skb);
  28111. - if (local_softirq_pending())
  28112. - do_softirq();
  28113. - preempt_enable();
  28114. + local_bh_enable();
  28115. return err;
  28116. }
  28117. EXPORT_SYMBOL(netif_rx_ni);
  28118. +#ifdef CONFIG_PREEMPT_RT_FULL
  28119. +/*
  28120. + * RT runs ksoftirqd as a real time thread and the root_lock is a
  28121. + * "sleeping spinlock". If the trylock fails then we can go into an
  28122. + * infinite loop when ksoftirqd preempted the task which actually
  28123. + * holds the lock, because we requeue q and raise NET_TX softirq
  28124. + * causing ksoftirqd to loop forever.
  28125. + *
  28126. + * It's safe to use spin_lock on RT here as softirqs run in thread
  28127. + * context and cannot deadlock against the thread which is holding
  28128. + * root_lock.
  28129. + *
  28130. + * On !RT the trylock might fail, but there we bail out from the
  28131. + * softirq loop after 10 attempts which we can't do on RT. And the
  28132. + * task holding root_lock cannot be preempted, so the only downside of
  28133. + * that trylock is that we need 10 loops to decide that we should have
  28134. + * given up in the first one :)
  28135. + */
  28136. +static inline int take_root_lock(spinlock_t *lock)
  28137. +{
  28138. + spin_lock(lock);
  28139. + return 1;
  28140. +}
  28141. +#else
  28142. +static inline int take_root_lock(spinlock_t *lock)
  28143. +{
  28144. + return spin_trylock(lock);
  28145. +}
  28146. +#endif
  28147. +
  28148. static void net_tx_action(struct softirq_action *h)
  28149. {
  28150. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  28151. @@ -3643,7 +3719,7 @@ static void net_tx_action(struct softirq_action *h)
  28152. head = head->next_sched;
  28153. root_lock = qdisc_lock(q);
  28154. - if (spin_trylock(root_lock)) {
  28155. + if (take_root_lock(root_lock)) {
  28156. smp_mb__before_atomic();
  28157. clear_bit(__QDISC_STATE_SCHED,
  28158. &q->state);
  28159. @@ -4049,7 +4125,7 @@ static void flush_backlog(void *arg)
  28160. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  28161. if (skb->dev == dev) {
  28162. __skb_unlink(skb, &sd->input_pkt_queue);
  28163. - kfree_skb(skb);
  28164. + __skb_queue_tail(&sd->tofree_queue, skb);
  28165. input_queue_head_incr(sd);
  28166. }
  28167. }
  28168. @@ -4058,10 +4134,13 @@ static void flush_backlog(void *arg)
  28169. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  28170. if (skb->dev == dev) {
  28171. __skb_unlink(skb, &sd->process_queue);
  28172. - kfree_skb(skb);
  28173. + __skb_queue_tail(&sd->tofree_queue, skb);
  28174. input_queue_head_incr(sd);
  28175. }
  28176. }
  28177. +
  28178. + if (!skb_queue_empty(&sd->tofree_queue))
  28179. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  28180. }
  28181. static int napi_gro_complete(struct sk_buff *skb)
  28182. @@ -4515,6 +4594,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  28183. sd->rps_ipi_list = NULL;
  28184. local_irq_enable();
  28185. + preempt_check_resched_rt();
  28186. /* Send pending IPI's to kick RPS processing on remote cpus. */
  28187. while (remsd) {
  28188. @@ -4528,6 +4608,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  28189. } else
  28190. #endif
  28191. local_irq_enable();
  28192. + preempt_check_resched_rt();
  28193. }
  28194. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  28195. @@ -4609,6 +4690,7 @@ void __napi_schedule(struct napi_struct *n)
  28196. local_irq_save(flags);
  28197. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  28198. local_irq_restore(flags);
  28199. + preempt_check_resched_rt();
  28200. }
  28201. EXPORT_SYMBOL(__napi_schedule);
  28202. @@ -4885,7 +4967,7 @@ static void net_rx_action(struct softirq_action *h)
  28203. list_splice_tail(&repoll, &list);
  28204. list_splice(&list, &sd->poll_list);
  28205. if (!list_empty(&sd->poll_list))
  28206. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  28207. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  28208. net_rps_action_and_irq_enable(sd);
  28209. }
  28210. @@ -7218,7 +7300,7 @@ EXPORT_SYMBOL(free_netdev);
  28211. void synchronize_net(void)
  28212. {
  28213. might_sleep();
  28214. - if (rtnl_is_locked())
  28215. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  28216. synchronize_rcu_expedited();
  28217. else
  28218. synchronize_rcu();
  28219. @@ -7459,16 +7541,20 @@ static int dev_cpu_callback(struct notifier_block *nfb,
  28220. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  28221. local_irq_enable();
  28222. + preempt_check_resched_rt();
  28223. /* Process offline CPU's input_pkt_queue */
  28224. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  28225. netif_rx_ni(skb);
  28226. input_queue_head_incr(oldsd);
  28227. }
  28228. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  28229. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  28230. netif_rx_ni(skb);
  28231. input_queue_head_incr(oldsd);
  28232. }
  28233. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  28234. + kfree_skb(skb);
  28235. + }
  28236. return NOTIFY_OK;
  28237. }
  28238. @@ -7770,8 +7856,9 @@ static int __init net_dev_init(void)
  28239. for_each_possible_cpu(i) {
  28240. struct softnet_data *sd = &per_cpu(softnet_data, i);
  28241. - skb_queue_head_init(&sd->input_pkt_queue);
  28242. - skb_queue_head_init(&sd->process_queue);
  28243. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  28244. + skb_queue_head_init_raw(&sd->process_queue);
  28245. + skb_queue_head_init_raw(&sd->tofree_queue);
  28246. INIT_LIST_HEAD(&sd->poll_list);
  28247. sd->output_queue_tailp = &sd->output_queue;
  28248. #ifdef CONFIG_RPS
  28249. diff --git a/net/core/skbuff.c b/net/core/skbuff.c
  28250. index 4968b5ddea69..c8d778f405dc 100644
  28251. --- a/net/core/skbuff.c
  28252. +++ b/net/core/skbuff.c
  28253. @@ -63,6 +63,7 @@
  28254. #include <linux/errqueue.h>
  28255. #include <linux/prefetch.h>
  28256. #include <linux/if_vlan.h>
  28257. +#include <linux/locallock.h>
  28258. #include <net/protocol.h>
  28259. #include <net/dst.h>
  28260. @@ -351,6 +352,8 @@ EXPORT_SYMBOL(build_skb);
  28261. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  28262. static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache);
  28263. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  28264. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  28265. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  28266. {
  28267. @@ -358,10 +361,10 @@ static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  28268. unsigned long flags;
  28269. void *data;
  28270. - local_irq_save(flags);
  28271. + local_lock_irqsave(netdev_alloc_lock, flags);
  28272. nc = this_cpu_ptr(&netdev_alloc_cache);
  28273. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  28274. - local_irq_restore(flags);
  28275. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  28276. return data;
  28277. }
  28278. @@ -380,9 +383,13 @@ EXPORT_SYMBOL(netdev_alloc_frag);
  28279. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  28280. {
  28281. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  28282. + struct page_frag_cache *nc;
  28283. + void *data;
  28284. - return __alloc_page_frag(nc, fragsz, gfp_mask);
  28285. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28286. + data = __alloc_page_frag(nc, fragsz, gfp_mask);
  28287. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28288. + return data;
  28289. }
  28290. void *napi_alloc_frag(unsigned int fragsz)
  28291. @@ -429,13 +436,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  28292. if (sk_memalloc_socks())
  28293. gfp_mask |= __GFP_MEMALLOC;
  28294. - local_irq_save(flags);
  28295. + local_lock_irqsave(netdev_alloc_lock, flags);
  28296. nc = this_cpu_ptr(&netdev_alloc_cache);
  28297. data = __alloc_page_frag(nc, len, gfp_mask);
  28298. pfmemalloc = nc->pfmemalloc;
  28299. - local_irq_restore(flags);
  28300. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  28301. if (unlikely(!data))
  28302. return NULL;
  28303. @@ -476,9 +483,10 @@ EXPORT_SYMBOL(__netdev_alloc_skb);
  28304. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  28305. gfp_t gfp_mask)
  28306. {
  28307. - struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  28308. + struct page_frag_cache *nc;
  28309. struct sk_buff *skb;
  28310. void *data;
  28311. + bool pfmemalloc;
  28312. len += NET_SKB_PAD + NET_IP_ALIGN;
  28313. @@ -496,7 +504,11 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  28314. if (sk_memalloc_socks())
  28315. gfp_mask |= __GFP_MEMALLOC;
  28316. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28317. data = __alloc_page_frag(nc, len, gfp_mask);
  28318. + pfmemalloc = nc->pfmemalloc;
  28319. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  28320. +
  28321. if (unlikely(!data))
  28322. return NULL;
  28323. @@ -507,7 +519,7 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  28324. }
  28325. /* use OR instead of assignment to avoid clearing of bits in mask */
  28326. - if (nc->pfmemalloc)
  28327. + if (pfmemalloc)
  28328. skb->pfmemalloc = 1;
  28329. skb->head_frag = 1;
  28330. diff --git a/net/core/sock.c b/net/core/sock.c
  28331. index 0d91f7dca751..9c3234299fc3 100644
  28332. --- a/net/core/sock.c
  28333. +++ b/net/core/sock.c
  28334. @@ -2435,12 +2435,11 @@ void lock_sock_nested(struct sock *sk, int subclass)
  28335. if (sk->sk_lock.owned)
  28336. __lock_sock(sk);
  28337. sk->sk_lock.owned = 1;
  28338. - spin_unlock(&sk->sk_lock.slock);
  28339. + spin_unlock_bh(&sk->sk_lock.slock);
  28340. /*
  28341. * The sk_lock has mutex_lock() semantics here:
  28342. */
  28343. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  28344. - local_bh_enable();
  28345. }
  28346. EXPORT_SYMBOL(lock_sock_nested);
  28347. diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
  28348. index 36e26977c908..74314d95d0f8 100644
  28349. --- a/net/ipv4/icmp.c
  28350. +++ b/net/ipv4/icmp.c
  28351. @@ -69,6 +69,7 @@
  28352. #include <linux/jiffies.h>
  28353. #include <linux/kernel.h>
  28354. #include <linux/fcntl.h>
  28355. +#include <linux/sysrq.h>
  28356. #include <linux/socket.h>
  28357. #include <linux/in.h>
  28358. #include <linux/inet.h>
  28359. @@ -891,6 +892,30 @@ static bool icmp_redirect(struct sk_buff *skb)
  28360. }
  28361. /*
  28362. + * 32bit and 64bit have different timestamp length, so we check for
  28363. + * the cookie at offset 20 and verify it is repeated at offset 50
  28364. + */
  28365. +#define CO_POS0 20
  28366. +#define CO_POS1 50
  28367. +#define CO_SIZE sizeof(int)
  28368. +#define ICMP_SYSRQ_SIZE 57
  28369. +
  28370. +/*
  28371. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  28372. + * pattern and if it matches send the next byte as a trigger to sysrq.
  28373. + */
  28374. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  28375. +{
  28376. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  28377. + char *p = skb->data;
  28378. +
  28379. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  28380. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  28381. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  28382. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  28383. +}
  28384. +
  28385. +/*
  28386. * Handle ICMP_ECHO ("ping") requests.
  28387. *
  28388. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  28389. @@ -917,6 +942,11 @@ static bool icmp_echo(struct sk_buff *skb)
  28390. icmp_param.data_len = skb->len;
  28391. icmp_param.head_len = sizeof(struct icmphdr);
  28392. icmp_reply(&icmp_param, skb);
  28393. +
  28394. + if (skb->len == ICMP_SYSRQ_SIZE &&
  28395. + net->ipv4.sysctl_icmp_echo_sysrq) {
  28396. + icmp_check_sysrq(net, skb);
  28397. + }
  28398. }
  28399. /* should there be an ICMP stat for ignored echos? */
  28400. return true;
  28401. diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
  28402. index a0bd7a55193e..1866f910263f 100644
  28403. --- a/net/ipv4/sysctl_net_ipv4.c
  28404. +++ b/net/ipv4/sysctl_net_ipv4.c
  28405. @@ -818,6 +818,13 @@ static struct ctl_table ipv4_net_table[] = {
  28406. .proc_handler = proc_dointvec
  28407. },
  28408. {
  28409. + .procname = "icmp_echo_sysrq",
  28410. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  28411. + .maxlen = sizeof(int),
  28412. + .mode = 0644,
  28413. + .proc_handler = proc_dointvec
  28414. + },
  28415. + {
  28416. .procname = "icmp_ignore_bogus_error_responses",
  28417. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  28418. .maxlen = sizeof(int),
  28419. diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
  28420. index a3bb8f7f5fc5..3be977394a80 100644
  28421. --- a/net/mac80211/rx.c
  28422. +++ b/net/mac80211/rx.c
  28423. @@ -3574,7 +3574,7 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct sk_buff *skb,
  28424. struct ieee80211_supported_band *sband;
  28425. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  28426. - WARN_ON_ONCE(softirq_count() == 0);
  28427. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  28428. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  28429. goto drop;
  28430. diff --git a/net/netfilter/core.c b/net/netfilter/core.c
  28431. index f39276d1c2d7..10880c89d62f 100644
  28432. --- a/net/netfilter/core.c
  28433. +++ b/net/netfilter/core.c
  28434. @@ -22,11 +22,17 @@
  28435. #include <linux/proc_fs.h>
  28436. #include <linux/mutex.h>
  28437. #include <linux/slab.h>
  28438. +#include <linux/locallock.h>
  28439. #include <net/net_namespace.h>
  28440. #include <net/sock.h>
  28441. #include "nf_internals.h"
  28442. +#ifdef CONFIG_PREEMPT_RT_BASE
  28443. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  28444. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  28445. +#endif
  28446. +
  28447. static DEFINE_MUTEX(afinfo_mutex);
  28448. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  28449. diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
  28450. index a86f26d05bc2..77276e3ff2a2 100644
  28451. --- a/net/packet/af_packet.c
  28452. +++ b/net/packet/af_packet.c
  28453. @@ -63,6 +63,7 @@
  28454. #include <linux/if_packet.h>
  28455. #include <linux/wireless.h>
  28456. #include <linux/kernel.h>
  28457. +#include <linux/delay.h>
  28458. #include <linux/kmod.h>
  28459. #include <linux/slab.h>
  28460. #include <linux/vmalloc.h>
  28461. @@ -695,7 +696,7 @@ static void prb_retire_rx_blk_timer_expired(unsigned long data)
  28462. if (BLOCK_NUM_PKTS(pbd)) {
  28463. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28464. /* Waiting for skb_copy_bits to finish... */
  28465. - cpu_relax();
  28466. + cpu_chill();
  28467. }
  28468. }
  28469. @@ -957,7 +958,7 @@ static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  28470. if (!(status & TP_STATUS_BLK_TMO)) {
  28471. while (atomic_read(&pkc->blk_fill_in_prog)) {
  28472. /* Waiting for skb_copy_bits to finish... */
  28473. - cpu_relax();
  28474. + cpu_chill();
  28475. }
  28476. }
  28477. prb_close_block(pkc, pbd, po, status);
  28478. diff --git a/net/rds/ib_rdma.c b/net/rds/ib_rdma.c
  28479. index a2340748ec86..19123a97b354 100644
  28480. --- a/net/rds/ib_rdma.c
  28481. +++ b/net/rds/ib_rdma.c
  28482. @@ -34,6 +34,7 @@
  28483. #include <linux/slab.h>
  28484. #include <linux/rculist.h>
  28485. #include <linux/llist.h>
  28486. +#include <linux/delay.h>
  28487. #include "rds.h"
  28488. #include "ib.h"
  28489. @@ -313,7 +314,7 @@ static inline void wait_clean_list_grace(void)
  28490. for_each_online_cpu(cpu) {
  28491. flag = &per_cpu(clean_list_grace, cpu);
  28492. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  28493. - cpu_relax();
  28494. + cpu_chill();
  28495. }
  28496. }
  28497. diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
  28498. index aa4725038f94..00b81cab28f3 100644
  28499. --- a/net/sched/sch_generic.c
  28500. +++ b/net/sched/sch_generic.c
  28501. @@ -893,7 +893,7 @@ void dev_deactivate_many(struct list_head *head)
  28502. /* Wait for outstanding qdisc_run calls. */
  28503. list_for_each_entry(dev, head, close_list)
  28504. while (some_qdisc_is_busy(dev))
  28505. - yield();
  28506. + msleep(1);
  28507. }
  28508. void dev_deactivate(struct net_device *dev)
  28509. diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
  28510. index a6cbb2104667..5b69bb580617 100644
  28511. --- a/net/sunrpc/svc_xprt.c
  28512. +++ b/net/sunrpc/svc_xprt.c
  28513. @@ -340,7 +340,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  28514. goto out;
  28515. }
  28516. - cpu = get_cpu();
  28517. + cpu = get_cpu_light();
  28518. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  28519. atomic_long_inc(&pool->sp_stats.packets);
  28520. @@ -376,7 +376,7 @@ redo_search:
  28521. atomic_long_inc(&pool->sp_stats.threads_woken);
  28522. wake_up_process(rqstp->rq_task);
  28523. - put_cpu();
  28524. + put_cpu_light();
  28525. goto out;
  28526. }
  28527. rcu_read_unlock();
  28528. @@ -397,7 +397,7 @@ redo_search:
  28529. goto redo_search;
  28530. }
  28531. rqstp = NULL;
  28532. - put_cpu();
  28533. + put_cpu_light();
  28534. out:
  28535. trace_svc_xprt_do_enqueue(xprt, rqstp);
  28536. }
  28537. diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h
  28538. index 6fdc97ef6023..523e0420d7f0 100755
  28539. --- a/scripts/mkcompile_h
  28540. +++ b/scripts/mkcompile_h
  28541. @@ -4,7 +4,8 @@ TARGET=$1
  28542. ARCH=$2
  28543. SMP=$3
  28544. PREEMPT=$4
  28545. -CC=$5
  28546. +RT=$5
  28547. +CC=$6
  28548. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  28549. @@ -57,6 +58,7 @@ UTS_VERSION="#$VERSION"
  28550. CONFIG_FLAGS=""
  28551. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  28552. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  28553. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  28554. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  28555. # Truncate to maximum length
  28556. diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
  28557. index 4ba64fd49759..34e50186885d 100644
  28558. --- a/sound/core/pcm_native.c
  28559. +++ b/sound/core/pcm_native.c
  28560. @@ -135,7 +135,7 @@ EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
  28561. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  28562. {
  28563. if (!substream->pcm->nonatomic)
  28564. - local_irq_disable();
  28565. + local_irq_disable_nort();
  28566. snd_pcm_stream_lock(substream);
  28567. }
  28568. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  28569. @@ -150,7 +150,7 @@ void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
  28570. {
  28571. snd_pcm_stream_unlock(substream);
  28572. if (!substream->pcm->nonatomic)
  28573. - local_irq_enable();
  28574. + local_irq_enable_nort();
  28575. }
  28576. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  28577. @@ -158,7 +158,7 @@ unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
  28578. {
  28579. unsigned long flags = 0;
  28580. if (!substream->pcm->nonatomic)
  28581. - local_irq_save(flags);
  28582. + local_irq_save_nort(flags);
  28583. snd_pcm_stream_lock(substream);
  28584. return flags;
  28585. }
  28586. @@ -176,7 +176,7 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  28587. {
  28588. snd_pcm_stream_unlock(substream);
  28589. if (!substream->pcm->nonatomic)
  28590. - local_irq_restore(flags);
  28591. + local_irq_restore_nort(flags);
  28592. }
  28593. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  28594. diff --git a/virt/kvm/async_pf.c b/virt/kvm/async_pf.c
  28595. index 4f70d12e392d..9378d0919ed8 100644
  28596. --- a/virt/kvm/async_pf.c
  28597. +++ b/virt/kvm/async_pf.c
  28598. @@ -98,8 +98,8 @@ static void async_pf_execute(struct work_struct *work)
  28599. * This memory barrier pairs with prepare_to_wait's set_current_state()
  28600. */
  28601. smp_mb();
  28602. - if (waitqueue_active(&vcpu->wq))
  28603. - wake_up_interruptible(&vcpu->wq);
  28604. + if (swait_active(&vcpu->wq))
  28605. + swake_up(&vcpu->wq);
  28606. mmput(mm);
  28607. kvm_put_kvm(vcpu->kvm);
  28608. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
  28609. index 336ed267c407..7748ca386e60 100644
  28610. --- a/virt/kvm/kvm_main.c
  28611. +++ b/virt/kvm/kvm_main.c
  28612. @@ -228,8 +228,7 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  28613. vcpu->kvm = kvm;
  28614. vcpu->vcpu_id = id;
  28615. vcpu->pid = NULL;
  28616. - vcpu->halt_poll_ns = 0;
  28617. - init_waitqueue_head(&vcpu->wq);
  28618. + init_swait_queue_head(&vcpu->wq);
  28619. kvm_async_pf_vcpu_init(vcpu);
  28620. vcpu->pre_pcpu = -1;
  28621. @@ -2005,7 +2004,7 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
  28622. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  28623. {
  28624. ktime_t start, cur;
  28625. - DEFINE_WAIT(wait);
  28626. + DECLARE_SWAITQUEUE(wait);
  28627. bool waited = false;
  28628. u64 block_ns;
  28629. @@ -2030,7 +2029,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  28630. kvm_arch_vcpu_blocking(vcpu);
  28631. for (;;) {
  28632. - prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28633. + prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  28634. if (kvm_vcpu_check_block(vcpu) < 0)
  28635. break;
  28636. @@ -2039,7 +2038,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  28637. schedule();
  28638. }
  28639. - finish_wait(&vcpu->wq, &wait);
  28640. + finish_swait(&vcpu->wq, &wait);
  28641. cur = ktime_get();
  28642. kvm_arch_vcpu_unblocking(vcpu);
  28643. @@ -2071,11 +2070,11 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  28644. {
  28645. int me;
  28646. int cpu = vcpu->cpu;
  28647. - wait_queue_head_t *wqp;
  28648. + struct swait_queue_head *wqp;
  28649. wqp = kvm_arch_vcpu_wq(vcpu);
  28650. - if (waitqueue_active(wqp)) {
  28651. - wake_up_interruptible(wqp);
  28652. + if (swait_active(wqp)) {
  28653. + swake_up(wqp);
  28654. ++vcpu->stat.halt_wakeup;
  28655. }
  28656. @@ -2176,7 +2175,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  28657. continue;
  28658. if (vcpu == me)
  28659. continue;
  28660. - if (waitqueue_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28661. + if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  28662. continue;
  28663. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  28664. continue;