patch-realtime 763 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908
  1. diff --git a/Documentation/hwlat_detector.txt b/Documentation/hwlat_detector.txt
  2. new file mode 100644
  3. index 000000000000..cb61516483d3
  4. --- /dev/null
  5. +++ b/Documentation/hwlat_detector.txt
  6. @@ -0,0 +1,64 @@
  7. +Introduction:
  8. +-------------
  9. +
  10. +The module hwlat_detector is a special purpose kernel module that is used to
  11. +detect large system latencies induced by the behavior of certain underlying
  12. +hardware or firmware, independent of Linux itself. The code was developed
  13. +originally to detect SMIs (System Management Interrupts) on x86 systems,
  14. +however there is nothing x86 specific about this patchset. It was
  15. +originally written for use by the "RT" patch since the Real Time
  16. +kernel is highly latency sensitive.
  17. +
  18. +SMIs are usually not serviced by the Linux kernel, which typically does not
  19. +even know that they are occuring. SMIs are instead are set up by BIOS code
  20. +and are serviced by BIOS code, usually for "critical" events such as
  21. +management of thermal sensors and fans. Sometimes though, SMIs are used for
  22. +other tasks and those tasks can spend an inordinate amount of time in the
  23. +handler (sometimes measured in milliseconds). Obviously this is a problem if
  24. +you are trying to keep event service latencies down in the microsecond range.
  25. +
  26. +The hardware latency detector works by hogging all of the cpus for configurable
  27. +amounts of time (by calling stop_machine()), polling the CPU Time Stamp Counter
  28. +for some period, then looking for gaps in the TSC data. Any gap indicates a
  29. +time when the polling was interrupted and since the machine is stopped and
  30. +interrupts turned off the only thing that could do that would be an SMI.
  31. +
  32. +Note that the SMI detector should *NEVER* be used in a production environment.
  33. +It is intended to be run manually to determine if the hardware platform has a
  34. +problem with long system firmware service routines.
  35. +
  36. +Usage:
  37. +------
  38. +
  39. +Loading the module hwlat_detector passing the parameter "enabled=1" (or by
  40. +setting the "enable" entry in "hwlat_detector" debugfs toggled on) is the only
  41. +step required to start the hwlat_detector. It is possible to redefine the
  42. +threshold in microseconds (us) above which latency spikes will be taken
  43. +into account (parameter "threshold=").
  44. +
  45. +Example:
  46. +
  47. + # modprobe hwlat_detector enabled=1 threshold=100
  48. +
  49. +After the module is loaded, it creates a directory named "hwlat_detector" under
  50. +the debugfs mountpoint, "/debug/hwlat_detector" for this text. It is necessary
  51. +to have debugfs mounted, which might be on /sys/debug on your system.
  52. +
  53. +The /debug/hwlat_detector interface contains the following files:
  54. +
  55. +count - number of latency spikes observed since last reset
  56. +enable - a global enable/disable toggle (0/1), resets count
  57. +max - maximum hardware latency actually observed (usecs)
  58. +sample - a pipe from which to read current raw sample data
  59. + in the format <timestamp> <latency observed usecs>
  60. + (can be opened O_NONBLOCK for a single sample)
  61. +threshold - minimum latency value to be considered (usecs)
  62. +width - time period to sample with CPUs held (usecs)
  63. + must be less than the total window size (enforced)
  64. +window - total period of sampling, width being inside (usecs)
  65. +
  66. +By default we will set width to 500,000 and window to 1,000,000, meaning that
  67. +we will sample every 1,000,000 usecs (1s) for 500,000 usecs (0.5s). If we
  68. +observe any latencies that exceed the threshold (initially 100 usecs),
  69. +then we write to a global sample ring buffer of 8K samples, which is
  70. +consumed by reading from the "sample" (pipe) debugfs file interface.
  71. diff --git a/Documentation/sysrq.txt b/Documentation/sysrq.txt
  72. index 3a3b30ac2a75..9e0745cafbd8 100644
  73. --- a/Documentation/sysrq.txt
  74. +++ b/Documentation/sysrq.txt
  75. @@ -59,10 +59,17 @@ On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>,
  76. On other - If you know of the key combos for other architectures, please
  77. let me know so I can add them to this section.
  78. -On all - write a character to /proc/sysrq-trigger. e.g.:
  79. -
  80. +On all - write a character to /proc/sysrq-trigger, e.g.:
  81. echo t > /proc/sysrq-trigger
  82. +On all - Enable network SysRq by writing a cookie to icmp_echo_sysrq, e.g.
  83. + echo 0x01020304 >/proc/sys/net/ipv4/icmp_echo_sysrq
  84. + Send an ICMP echo request with this pattern plus the particular
  85. + SysRq command key. Example:
  86. + # ping -c1 -s57 -p0102030468
  87. + will trigger the SysRq-H (help) command.
  88. +
  89. +
  90. * What are the 'command' keys?
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. 'b' - Will immediately reboot the system without syncing or unmounting
  93. diff --git a/Documentation/trace/histograms.txt b/Documentation/trace/histograms.txt
  94. new file mode 100644
  95. index 000000000000..6f2aeabf7faa
  96. --- /dev/null
  97. +++ b/Documentation/trace/histograms.txt
  98. @@ -0,0 +1,186 @@
  99. + Using the Linux Kernel Latency Histograms
  100. +
  101. +
  102. +This document gives a short explanation how to enable, configure and use
  103. +latency histograms. Latency histograms are primarily relevant in the
  104. +context of real-time enabled kernels (CONFIG_PREEMPT/CONFIG_PREEMPT_RT)
  105. +and are used in the quality management of the Linux real-time
  106. +capabilities.
  107. +
  108. +
  109. +* Purpose of latency histograms
  110. +
  111. +A latency histogram continuously accumulates the frequencies of latency
  112. +data. There are two types of histograms
  113. +- potential sources of latencies
  114. +- effective latencies
  115. +
  116. +
  117. +* Potential sources of latencies
  118. +
  119. +Potential sources of latencies are code segments where interrupts,
  120. +preemption or both are disabled (aka critical sections). To create
  121. +histograms of potential sources of latency, the kernel stores the time
  122. +stamp at the start of a critical section, determines the time elapsed
  123. +when the end of the section is reached, and increments the frequency
  124. +counter of that latency value - irrespective of whether any concurrently
  125. +running process is affected by latency or not.
  126. +- Configuration items (in the Kernel hacking/Tracers submenu)
  127. + CONFIG_INTERRUPT_OFF_LATENCY
  128. + CONFIG_PREEMPT_OFF_LATENCY
  129. +
  130. +
  131. +* Effective latencies
  132. +
  133. +Effective latencies are actually occuring during wakeup of a process. To
  134. +determine effective latencies, the kernel stores the time stamp when a
  135. +process is scheduled to be woken up, and determines the duration of the
  136. +wakeup time shortly before control is passed over to this process. Note
  137. +that the apparent latency in user space may be somewhat longer, since the
  138. +process may be interrupted after control is passed over to it but before
  139. +the execution in user space takes place. Simply measuring the interval
  140. +between enqueuing and wakeup may also not appropriate in cases when a
  141. +process is scheduled as a result of a timer expiration. The timer may have
  142. +missed its deadline, e.g. due to disabled interrupts, but this latency
  143. +would not be registered. Therefore, the offsets of missed timers are
  144. +recorded in a separate histogram. If both wakeup latency and missed timer
  145. +offsets are configured and enabled, a third histogram may be enabled that
  146. +records the overall latency as a sum of the timer latency, if any, and the
  147. +wakeup latency. This histogram is called "timerandwakeup".
  148. +- Configuration items (in the Kernel hacking/Tracers submenu)
  149. + CONFIG_WAKEUP_LATENCY
  150. + CONFIG_MISSED_TIMER_OFSETS
  151. +
  152. +
  153. +* Usage
  154. +
  155. +The interface to the administration of the latency histograms is located
  156. +in the debugfs file system. To mount it, either enter
  157. +
  158. +mount -t sysfs nodev /sys
  159. +mount -t debugfs nodev /sys/kernel/debug
  160. +
  161. +from shell command line level, or add
  162. +
  163. +nodev /sys sysfs defaults 0 0
  164. +nodev /sys/kernel/debug debugfs defaults 0 0
  165. +
  166. +to the file /etc/fstab. All latency histogram related files are then
  167. +available in the directory /sys/kernel/debug/tracing/latency_hist. A
  168. +particular histogram type is enabled by writing non-zero to the related
  169. +variable in the /sys/kernel/debug/tracing/latency_hist/enable directory.
  170. +Select "preemptirqsoff" for the histograms of potential sources of
  171. +latencies and "wakeup" for histograms of effective latencies etc. The
  172. +histogram data - one per CPU - are available in the files
  173. +
  174. +/sys/kernel/debug/tracing/latency_hist/preemptoff/CPUx
  175. +/sys/kernel/debug/tracing/latency_hist/irqsoff/CPUx
  176. +/sys/kernel/debug/tracing/latency_hist/preemptirqsoff/CPUx
  177. +/sys/kernel/debug/tracing/latency_hist/wakeup/CPUx
  178. +/sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio/CPUx
  179. +/sys/kernel/debug/tracing/latency_hist/missed_timer_offsets/CPUx
  180. +/sys/kernel/debug/tracing/latency_hist/timerandwakeup/CPUx
  181. +
  182. +The histograms are reset by writing non-zero to the file "reset" in a
  183. +particular latency directory. To reset all latency data, use
  184. +
  185. +#!/bin/sh
  186. +
  187. +TRACINGDIR=/sys/kernel/debug/tracing
  188. +HISTDIR=$TRACINGDIR/latency_hist
  189. +
  190. +if test -d $HISTDIR
  191. +then
  192. + cd $HISTDIR
  193. + for i in `find . | grep /reset$`
  194. + do
  195. + echo 1 >$i
  196. + done
  197. +fi
  198. +
  199. +
  200. +* Data format
  201. +
  202. +Latency data are stored with a resolution of one microsecond. The
  203. +maximum latency is 10,240 microseconds. The data are only valid, if the
  204. +overflow register is empty. Every output line contains the latency in
  205. +microseconds in the first row and the number of samples in the second
  206. +row. To display only lines with a positive latency count, use, for
  207. +example,
  208. +
  209. +grep -v " 0$" /sys/kernel/debug/tracing/latency_hist/preemptoff/CPU0
  210. +
  211. +#Minimum latency: 0 microseconds.
  212. +#Average latency: 0 microseconds.
  213. +#Maximum latency: 25 microseconds.
  214. +#Total samples: 3104770694
  215. +#There are 0 samples greater or equal than 10240 microseconds
  216. +#usecs samples
  217. + 0 2984486876
  218. + 1 49843506
  219. + 2 58219047
  220. + 3 5348126
  221. + 4 2187960
  222. + 5 3388262
  223. + 6 959289
  224. + 7 208294
  225. + 8 40420
  226. + 9 4485
  227. + 10 14918
  228. + 11 18340
  229. + 12 25052
  230. + 13 19455
  231. + 14 5602
  232. + 15 969
  233. + 16 47
  234. + 17 18
  235. + 18 14
  236. + 19 1
  237. + 20 3
  238. + 21 2
  239. + 22 5
  240. + 23 2
  241. + 25 1
  242. +
  243. +
  244. +* Wakeup latency of a selected process
  245. +
  246. +To only collect wakeup latency data of a particular process, write the
  247. +PID of the requested process to
  248. +
  249. +/sys/kernel/debug/tracing/latency_hist/wakeup/pid
  250. +
  251. +PIDs are not considered, if this variable is set to 0.
  252. +
  253. +
  254. +* Details of the process with the highest wakeup latency so far
  255. +
  256. +Selected data of the process that suffered from the highest wakeup
  257. +latency that occurred in a particular CPU are available in the file
  258. +
  259. +/sys/kernel/debug/tracing/latency_hist/wakeup/max_latency-CPUx.
  260. +
  261. +In addition, other relevant system data at the time when the
  262. +latency occurred are given.
  263. +
  264. +The format of the data is (all in one line):
  265. +<PID> <Priority> <Latency> (<Timeroffset>) <Command> \
  266. +<- <PID> <Priority> <Command> <Timestamp>
  267. +
  268. +The value of <Timeroffset> is only relevant in the combined timer
  269. +and wakeup latency recording. In the wakeup recording, it is
  270. +always 0, in the missed_timer_offsets recording, it is the same
  271. +as <Latency>.
  272. +
  273. +When retrospectively searching for the origin of a latency and
  274. +tracing was not enabled, it may be helpful to know the name and
  275. +some basic data of the task that (finally) was switching to the
  276. +late real-tlme task. In addition to the victim's data, also the
  277. +data of the possible culprit are therefore displayed after the
  278. +"<-" symbol.
  279. +
  280. +Finally, the timestamp of the time when the latency occurred
  281. +in <seconds>.<microseconds> after the most recent system boot
  282. +is provided.
  283. +
  284. +These data are also reset when the wakeup histogram is reset.
  285. diff --git a/Makefile b/Makefile
  286. index b249529204cd..5d699d055995 100644
  287. --- a/Makefile
  288. +++ b/Makefile
  289. @@ -398,12 +398,12 @@ KBUILD_CPPFLAGS := -D__KERNEL__
  290. KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
  291. -fno-strict-aliasing -fno-common \
  292. -Werror-implicit-function-declaration \
  293. - -Wno-format-security \
  294. + -Wno-format-security -fno-PIE \
  295. -std=gnu89
  296. KBUILD_AFLAGS_KERNEL :=
  297. KBUILD_CFLAGS_KERNEL :=
  298. -KBUILD_AFLAGS := -D__ASSEMBLY__
  299. +KBUILD_AFLAGS := -D__ASSEMBLY__ -fno-PIE
  300. KBUILD_AFLAGS_MODULE := -DMODULE
  301. KBUILD_CFLAGS_MODULE := -DMODULE
  302. KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds
  303. diff --git a/arch/Kconfig b/arch/Kconfig
  304. index fd6e9712af81..085134ee13e9 100644
  305. --- a/arch/Kconfig
  306. +++ b/arch/Kconfig
  307. @@ -9,6 +9,7 @@ config OPROFILE
  308. tristate "OProfile system profiling"
  309. depends on PROFILING
  310. depends on HAVE_OPROFILE
  311. + depends on !PREEMPT_RT_FULL
  312. select RING_BUFFER
  313. select RING_BUFFER_ALLOW_SWAP
  314. help
  315. @@ -52,6 +53,7 @@ config KPROBES
  316. config JUMP_LABEL
  317. bool "Optimize very unlikely/likely branches"
  318. depends on HAVE_ARCH_JUMP_LABEL
  319. + depends on (!INTERRUPT_OFF_HIST && !PREEMPT_OFF_HIST && !WAKEUP_LATENCY_HIST && !MISSED_TIMER_OFFSETS_HIST)
  320. help
  321. This option enables a transparent branch optimization that
  322. makes certain almost-always-true or almost-always-false branch
  323. diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
  324. index a9c4e48bb7ec..6eefe4f32302 100644
  325. --- a/arch/arm/Kconfig
  326. +++ b/arch/arm/Kconfig
  327. @@ -36,7 +36,7 @@ config ARM
  328. select HAVE_ARCH_AUDITSYSCALL if (AEABI && !OABI_COMPAT)
  329. select HAVE_ARCH_BITREVERSE if (CPU_32v7M || CPU_32v7) && !CPU_32v6
  330. select HAVE_ARCH_HARDENED_USERCOPY
  331. - select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU
  332. + select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL && !CPU_ENDIAN_BE32 && MMU && !PREEMPT_RT_BASE
  333. select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
  334. select HAVE_ARCH_MMAP_RND_BITS if MMU
  335. select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT)
  336. @@ -75,6 +75,7 @@ config ARM
  337. select HAVE_PERF_EVENTS
  338. select HAVE_PERF_REGS
  339. select HAVE_PERF_USER_STACK_DUMP
  340. + select HAVE_PREEMPT_LAZY
  341. select HAVE_RCU_TABLE_FREE if (SMP && ARM_LPAE)
  342. select HAVE_REGS_AND_STACK_ACCESS_API
  343. select HAVE_SYSCALL_TRACEPOINTS
  344. diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h
  345. index 12ebfcc1d539..c962084605bc 100644
  346. --- a/arch/arm/include/asm/switch_to.h
  347. +++ b/arch/arm/include/asm/switch_to.h
  348. @@ -3,6 +3,13 @@
  349. #include <linux/thread_info.h>
  350. +#if defined CONFIG_PREEMPT_RT_FULL && defined CONFIG_HIGHMEM
  351. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p);
  352. +#else
  353. +static inline void
  354. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  355. +#endif
  356. +
  357. /*
  358. * For v7 SMP cores running a preemptible kernel we may be pre-empted
  359. * during a TLB maintenance operation, so execute an inner-shareable dsb
  360. @@ -25,6 +32,7 @@ extern struct task_struct *__switch_to(struct task_struct *, struct thread_info
  361. #define switch_to(prev,next,last) \
  362. do { \
  363. __complete_pending_tlbi(); \
  364. + switch_kmaps(prev, next); \
  365. last = __switch_to(prev,task_thread_info(prev), task_thread_info(next)); \
  366. } while (0)
  367. diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
  368. index 776757d1604a..1f36a4eccc72 100644
  369. --- a/arch/arm/include/asm/thread_info.h
  370. +++ b/arch/arm/include/asm/thread_info.h
  371. @@ -49,6 +49,7 @@ struct cpu_context_save {
  372. struct thread_info {
  373. unsigned long flags; /* low level flags */
  374. int preempt_count; /* 0 => preemptable, <0 => bug */
  375. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  376. mm_segment_t addr_limit; /* address limit */
  377. struct task_struct *task; /* main task structure */
  378. __u32 cpu; /* cpu */
  379. @@ -142,7 +143,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  380. #define TIF_SYSCALL_TRACE 4 /* syscall trace active */
  381. #define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
  382. #define TIF_SYSCALL_TRACEPOINT 6 /* syscall tracepoint instrumentation */
  383. -#define TIF_SECCOMP 7 /* seccomp syscall filtering active */
  384. +#define TIF_SECCOMP 8 /* seccomp syscall filtering active */
  385. +#define TIF_NEED_RESCHED_LAZY 7
  386. #define TIF_NOHZ 12 /* in adaptive nohz mode */
  387. #define TIF_USING_IWMMXT 17
  388. @@ -152,6 +154,7 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  389. #define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
  390. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  391. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  392. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  393. #define _TIF_UPROBE (1 << TIF_UPROBE)
  394. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  395. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  396. @@ -167,7 +170,8 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
  397. * Change these and you break ASM code in entry-common.S
  398. */
  399. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  400. - _TIF_NOTIFY_RESUME | _TIF_UPROBE)
  401. + _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  402. + _TIF_NEED_RESCHED_LAZY)
  403. #endif /* __KERNEL__ */
  404. #endif /* __ASM_ARM_THREAD_INFO_H */
  405. diff --git a/arch/arm/kernel/asm-offsets.c b/arch/arm/kernel/asm-offsets.c
  406. index 608008229c7d..3866da3f7bb7 100644
  407. --- a/arch/arm/kernel/asm-offsets.c
  408. +++ b/arch/arm/kernel/asm-offsets.c
  409. @@ -65,6 +65,7 @@ int main(void)
  410. BLANK();
  411. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  412. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  413. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  414. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  415. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  416. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  417. diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
  418. index 9f157e7c51e7..468e224d76aa 100644
  419. --- a/arch/arm/kernel/entry-armv.S
  420. +++ b/arch/arm/kernel/entry-armv.S
  421. @@ -220,11 +220,18 @@ ENDPROC(__dabt_svc)
  422. #ifdef CONFIG_PREEMPT
  423. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  424. - ldr r0, [tsk, #TI_FLAGS] @ get flags
  425. teq r8, #0 @ if preempt count != 0
  426. + bne 1f @ return from exeption
  427. + ldr r0, [tsk, #TI_FLAGS] @ get flags
  428. + tst r0, #_TIF_NEED_RESCHED @ if NEED_RESCHED is set
  429. + blne svc_preempt @ preempt!
  430. +
  431. + ldr r8, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  432. + teq r8, #0 @ if preempt lazy count != 0
  433. movne r0, #0 @ force flags to 0
  434. - tst r0, #_TIF_NEED_RESCHED
  435. + tst r0, #_TIF_NEED_RESCHED_LAZY
  436. blne svc_preempt
  437. +1:
  438. #endif
  439. svc_exit r5, irq = 1 @ return from exception
  440. @@ -239,8 +246,14 @@ ENDPROC(__irq_svc)
  441. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  442. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  443. tst r0, #_TIF_NEED_RESCHED
  444. + bne 1b
  445. + tst r0, #_TIF_NEED_RESCHED_LAZY
  446. reteq r8 @ go again
  447. - b 1b
  448. + ldr r0, [tsk, #TI_PREEMPT_LAZY] @ get preempt lazy count
  449. + teq r0, #0 @ if preempt lazy count != 0
  450. + beq 1b
  451. + ret r8 @ go again
  452. +
  453. #endif
  454. __und_fault:
  455. diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S
  456. index 10c3283d6c19..8872937862cc 100644
  457. --- a/arch/arm/kernel/entry-common.S
  458. +++ b/arch/arm/kernel/entry-common.S
  459. @@ -36,7 +36,9 @@
  460. UNWIND(.cantunwind )
  461. disable_irq_notrace @ disable interrupts
  462. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  463. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  464. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  465. + bne fast_work_pending
  466. + tst r1, #_TIF_SECCOMP
  467. bne fast_work_pending
  468. /* perform architecture specific actions before user return */
  469. @@ -62,8 +64,11 @@ ENDPROC(ret_fast_syscall)
  470. str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
  471. disable_irq_notrace @ disable interrupts
  472. ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing
  473. - tst r1, #_TIF_SYSCALL_WORK | _TIF_WORK_MASK
  474. + tst r1, #((_TIF_SYSCALL_WORK | _TIF_WORK_MASK) & ~_TIF_SECCOMP)
  475. + bne do_slower_path
  476. + tst r1, #_TIF_SECCOMP
  477. beq no_work_pending
  478. +do_slower_path:
  479. UNWIND(.fnend )
  480. ENDPROC(ret_fast_syscall)
  481. diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c
  482. index 612eb530f33f..cd3006dc1fd3 100644
  483. --- a/arch/arm/kernel/process.c
  484. +++ b/arch/arm/kernel/process.c
  485. @@ -323,6 +323,30 @@ unsigned long arch_randomize_brk(struct mm_struct *mm)
  486. }
  487. #ifdef CONFIG_MMU
  488. +/*
  489. + * CONFIG_SPLIT_PTLOCK_CPUS results in a page->ptl lock. If the lock is not
  490. + * initialized by pgtable_page_ctor() then a coredump of the vector page will
  491. + * fail.
  492. + */
  493. +static int __init vectors_user_mapping_init_page(void)
  494. +{
  495. + struct page *page;
  496. + unsigned long addr = 0xffff0000;
  497. + pgd_t *pgd;
  498. + pud_t *pud;
  499. + pmd_t *pmd;
  500. +
  501. + pgd = pgd_offset_k(addr);
  502. + pud = pud_offset(pgd, addr);
  503. + pmd = pmd_offset(pud, addr);
  504. + page = pmd_page(*(pmd));
  505. +
  506. + pgtable_page_ctor(page);
  507. +
  508. + return 0;
  509. +}
  510. +late_initcall(vectors_user_mapping_init_page);
  511. +
  512. #ifdef CONFIG_KUSER_HELPERS
  513. /*
  514. * The vectors page is always readable from user space for the
  515. diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c
  516. index 7b8f2141427b..96541e00b74a 100644
  517. --- a/arch/arm/kernel/signal.c
  518. +++ b/arch/arm/kernel/signal.c
  519. @@ -572,7 +572,8 @@ do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  520. */
  521. trace_hardirqs_off();
  522. do {
  523. - if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  524. + if (likely(thread_flags & (_TIF_NEED_RESCHED |
  525. + _TIF_NEED_RESCHED_LAZY))) {
  526. schedule();
  527. } else {
  528. if (unlikely(!user_mode(regs)))
  529. diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
  530. index 861521606c6d..e5ca865d321b 100644
  531. --- a/arch/arm/kernel/smp.c
  532. +++ b/arch/arm/kernel/smp.c
  533. @@ -234,8 +234,6 @@ int __cpu_disable(void)
  534. flush_cache_louis();
  535. local_flush_tlb_all();
  536. - clear_tasks_mm_cpumask(cpu);
  537. -
  538. return 0;
  539. }
  540. @@ -251,6 +249,9 @@ void __cpu_die(unsigned int cpu)
  541. pr_err("CPU%u: cpu didn't die\n", cpu);
  542. return;
  543. }
  544. +
  545. + clear_tasks_mm_cpumask(cpu);
  546. +
  547. pr_notice("CPU%u: shutdown\n", cpu);
  548. /*
  549. diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
  550. index 0bee233fef9a..314cfb232a63 100644
  551. --- a/arch/arm/kernel/unwind.c
  552. +++ b/arch/arm/kernel/unwind.c
  553. @@ -93,7 +93,7 @@ extern const struct unwind_idx __start_unwind_idx[];
  554. static const struct unwind_idx *__origin_unwind_idx;
  555. extern const struct unwind_idx __stop_unwind_idx[];
  556. -static DEFINE_SPINLOCK(unwind_lock);
  557. +static DEFINE_RAW_SPINLOCK(unwind_lock);
  558. static LIST_HEAD(unwind_tables);
  559. /* Convert a prel31 symbol to an absolute address */
  560. @@ -201,7 +201,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  561. /* module unwind tables */
  562. struct unwind_table *table;
  563. - spin_lock_irqsave(&unwind_lock, flags);
  564. + raw_spin_lock_irqsave(&unwind_lock, flags);
  565. list_for_each_entry(table, &unwind_tables, list) {
  566. if (addr >= table->begin_addr &&
  567. addr < table->end_addr) {
  568. @@ -213,7 +213,7 @@ static const struct unwind_idx *unwind_find_idx(unsigned long addr)
  569. break;
  570. }
  571. }
  572. - spin_unlock_irqrestore(&unwind_lock, flags);
  573. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  574. }
  575. pr_debug("%s: idx = %p\n", __func__, idx);
  576. @@ -529,9 +529,9 @@ struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
  577. tab->begin_addr = text_addr;
  578. tab->end_addr = text_addr + text_size;
  579. - spin_lock_irqsave(&unwind_lock, flags);
  580. + raw_spin_lock_irqsave(&unwind_lock, flags);
  581. list_add_tail(&tab->list, &unwind_tables);
  582. - spin_unlock_irqrestore(&unwind_lock, flags);
  583. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  584. return tab;
  585. }
  586. @@ -543,9 +543,9 @@ void unwind_table_del(struct unwind_table *tab)
  587. if (!tab)
  588. return;
  589. - spin_lock_irqsave(&unwind_lock, flags);
  590. + raw_spin_lock_irqsave(&unwind_lock, flags);
  591. list_del(&tab->list);
  592. - spin_unlock_irqrestore(&unwind_lock, flags);
  593. + raw_spin_unlock_irqrestore(&unwind_lock, flags);
  594. kfree(tab);
  595. }
  596. diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c
  597. index c94b90d43772..244dde72018a 100644
  598. --- a/arch/arm/kvm/arm.c
  599. +++ b/arch/arm/kvm/arm.c
  600. @@ -584,7 +584,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  601. * involves poking the GIC, which must be done in a
  602. * non-preemptible context.
  603. */
  604. - preempt_disable();
  605. + migrate_disable();
  606. kvm_pmu_flush_hwstate(vcpu);
  607. kvm_timer_flush_hwstate(vcpu);
  608. kvm_vgic_flush_hwstate(vcpu);
  609. @@ -605,7 +605,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  610. kvm_pmu_sync_hwstate(vcpu);
  611. kvm_timer_sync_hwstate(vcpu);
  612. kvm_vgic_sync_hwstate(vcpu);
  613. - preempt_enable();
  614. + migrate_enable();
  615. continue;
  616. }
  617. @@ -661,7 +661,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  618. kvm_vgic_sync_hwstate(vcpu);
  619. - preempt_enable();
  620. + migrate_enable();
  621. ret = handle_exit(vcpu, run, ret);
  622. }
  623. diff --git a/arch/arm/mach-exynos/platsmp.c b/arch/arm/mach-exynos/platsmp.c
  624. index 98ffe1e62ad5..df9769ddece5 100644
  625. --- a/arch/arm/mach-exynos/platsmp.c
  626. +++ b/arch/arm/mach-exynos/platsmp.c
  627. @@ -229,7 +229,7 @@ static void __iomem *scu_base_addr(void)
  628. return (void __iomem *)(S5P_VA_SCU);
  629. }
  630. -static DEFINE_SPINLOCK(boot_lock);
  631. +static DEFINE_RAW_SPINLOCK(boot_lock);
  632. static void exynos_secondary_init(unsigned int cpu)
  633. {
  634. @@ -242,8 +242,8 @@ static void exynos_secondary_init(unsigned int cpu)
  635. /*
  636. * Synchronise with the boot thread.
  637. */
  638. - spin_lock(&boot_lock);
  639. - spin_unlock(&boot_lock);
  640. + raw_spin_lock(&boot_lock);
  641. + raw_spin_unlock(&boot_lock);
  642. }
  643. int exynos_set_boot_addr(u32 core_id, unsigned long boot_addr)
  644. @@ -307,7 +307,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  645. * Set synchronisation state between this boot processor
  646. * and the secondary one
  647. */
  648. - spin_lock(&boot_lock);
  649. + raw_spin_lock(&boot_lock);
  650. /*
  651. * The secondary processor is waiting to be released from
  652. @@ -334,7 +334,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  653. if (timeout == 0) {
  654. printk(KERN_ERR "cpu1 power enable failed");
  655. - spin_unlock(&boot_lock);
  656. + raw_spin_unlock(&boot_lock);
  657. return -ETIMEDOUT;
  658. }
  659. }
  660. @@ -380,7 +380,7 @@ static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  661. * calibrations, then wait for it to finish
  662. */
  663. fail:
  664. - spin_unlock(&boot_lock);
  665. + raw_spin_unlock(&boot_lock);
  666. return pen_release != -1 ? ret : 0;
  667. }
  668. diff --git a/arch/arm/mach-hisi/platmcpm.c b/arch/arm/mach-hisi/platmcpm.c
  669. index 4b653a8cb75c..b03d5a922cb1 100644
  670. --- a/arch/arm/mach-hisi/platmcpm.c
  671. +++ b/arch/arm/mach-hisi/platmcpm.c
  672. @@ -61,7 +61,7 @@
  673. static void __iomem *sysctrl, *fabric;
  674. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  675. -static DEFINE_SPINLOCK(boot_lock);
  676. +static DEFINE_RAW_SPINLOCK(boot_lock);
  677. static u32 fabric_phys_addr;
  678. /*
  679. * [0]: bootwrapper physical address
  680. @@ -113,7 +113,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  681. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  682. return -EINVAL;
  683. - spin_lock_irq(&boot_lock);
  684. + raw_spin_lock_irq(&boot_lock);
  685. if (hip04_cpu_table[cluster][cpu])
  686. goto out;
  687. @@ -147,7 +147,7 @@ static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  688. out:
  689. hip04_cpu_table[cluster][cpu]++;
  690. - spin_unlock_irq(&boot_lock);
  691. + raw_spin_unlock_irq(&boot_lock);
  692. return 0;
  693. }
  694. @@ -162,11 +162,11 @@ static void hip04_cpu_die(unsigned int l_cpu)
  695. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  696. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  697. - spin_lock(&boot_lock);
  698. + raw_spin_lock(&boot_lock);
  699. hip04_cpu_table[cluster][cpu]--;
  700. if (hip04_cpu_table[cluster][cpu] == 1) {
  701. /* A power_up request went ahead of us. */
  702. - spin_unlock(&boot_lock);
  703. + raw_spin_unlock(&boot_lock);
  704. return;
  705. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  706. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  707. @@ -174,7 +174,7 @@ static void hip04_cpu_die(unsigned int l_cpu)
  708. }
  709. last_man = hip04_cluster_is_down(cluster);
  710. - spin_unlock(&boot_lock);
  711. + raw_spin_unlock(&boot_lock);
  712. if (last_man) {
  713. /* Since it's Cortex A15, disable L2 prefetching. */
  714. asm volatile(
  715. @@ -203,7 +203,7 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  716. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  717. count = TIMEOUT_MSEC / POLL_MSEC;
  718. - spin_lock_irq(&boot_lock);
  719. + raw_spin_lock_irq(&boot_lock);
  720. for (tries = 0; tries < count; tries++) {
  721. if (hip04_cpu_table[cluster][cpu])
  722. goto err;
  723. @@ -211,10 +211,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  724. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  725. if (data & CORE_WFI_STATUS(cpu))
  726. break;
  727. - spin_unlock_irq(&boot_lock);
  728. + raw_spin_unlock_irq(&boot_lock);
  729. /* Wait for clean L2 when the whole cluster is down. */
  730. msleep(POLL_MSEC);
  731. - spin_lock_irq(&boot_lock);
  732. + raw_spin_lock_irq(&boot_lock);
  733. }
  734. if (tries >= count)
  735. goto err;
  736. @@ -231,10 +231,10 @@ static int hip04_cpu_kill(unsigned int l_cpu)
  737. goto err;
  738. if (hip04_cluster_is_down(cluster))
  739. hip04_set_snoop_filter(cluster, 0);
  740. - spin_unlock_irq(&boot_lock);
  741. + raw_spin_unlock_irq(&boot_lock);
  742. return 1;
  743. err:
  744. - spin_unlock_irq(&boot_lock);
  745. + raw_spin_unlock_irq(&boot_lock);
  746. return 0;
  747. }
  748. #endif
  749. diff --git a/arch/arm/mach-omap2/omap-smp.c b/arch/arm/mach-omap2/omap-smp.c
  750. index b4de3da6dffa..b52893319d75 100644
  751. --- a/arch/arm/mach-omap2/omap-smp.c
  752. +++ b/arch/arm/mach-omap2/omap-smp.c
  753. @@ -64,7 +64,7 @@ static const struct omap_smp_config omap5_cfg __initconst = {
  754. .startup_addr = omap5_secondary_startup,
  755. };
  756. -static DEFINE_SPINLOCK(boot_lock);
  757. +static DEFINE_RAW_SPINLOCK(boot_lock);
  758. void __iomem *omap4_get_scu_base(void)
  759. {
  760. @@ -131,8 +131,8 @@ static void omap4_secondary_init(unsigned int cpu)
  761. /*
  762. * Synchronise with the boot thread.
  763. */
  764. - spin_lock(&boot_lock);
  765. - spin_unlock(&boot_lock);
  766. + raw_spin_lock(&boot_lock);
  767. + raw_spin_unlock(&boot_lock);
  768. }
  769. static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  770. @@ -146,7 +146,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  771. * Set synchronisation state between this boot processor
  772. * and the secondary one
  773. */
  774. - spin_lock(&boot_lock);
  775. + raw_spin_lock(&boot_lock);
  776. /*
  777. * Update the AuxCoreBoot0 with boot state for secondary core.
  778. @@ -223,7 +223,7 @@ static int omap4_boot_secondary(unsigned int cpu, struct task_struct *idle)
  779. * Now the secondary core is starting up let it run its
  780. * calibrations, then wait for it to finish
  781. */
  782. - spin_unlock(&boot_lock);
  783. + raw_spin_unlock(&boot_lock);
  784. return 0;
  785. }
  786. diff --git a/arch/arm/mach-prima2/platsmp.c b/arch/arm/mach-prima2/platsmp.c
  787. index 0875b99add18..18b6d98d2581 100644
  788. --- a/arch/arm/mach-prima2/platsmp.c
  789. +++ b/arch/arm/mach-prima2/platsmp.c
  790. @@ -22,7 +22,7 @@
  791. static void __iomem *clk_base;
  792. -static DEFINE_SPINLOCK(boot_lock);
  793. +static DEFINE_RAW_SPINLOCK(boot_lock);
  794. static void sirfsoc_secondary_init(unsigned int cpu)
  795. {
  796. @@ -36,8 +36,8 @@ static void sirfsoc_secondary_init(unsigned int cpu)
  797. /*
  798. * Synchronise with the boot thread.
  799. */
  800. - spin_lock(&boot_lock);
  801. - spin_unlock(&boot_lock);
  802. + raw_spin_lock(&boot_lock);
  803. + raw_spin_unlock(&boot_lock);
  804. }
  805. static const struct of_device_id clk_ids[] = {
  806. @@ -75,7 +75,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  807. /* make sure write buffer is drained */
  808. mb();
  809. - spin_lock(&boot_lock);
  810. + raw_spin_lock(&boot_lock);
  811. /*
  812. * The secondary processor is waiting to be released from
  813. @@ -107,7 +107,7 @@ static int sirfsoc_boot_secondary(unsigned int cpu, struct task_struct *idle)
  814. * now the secondary core is starting up let it run its
  815. * calibrations, then wait for it to finish
  816. */
  817. - spin_unlock(&boot_lock);
  818. + raw_spin_unlock(&boot_lock);
  819. return pen_release != -1 ? -ENOSYS : 0;
  820. }
  821. diff --git a/arch/arm/mach-qcom/platsmp.c b/arch/arm/mach-qcom/platsmp.c
  822. index 5494c9e0c909..e8ce157d3548 100644
  823. --- a/arch/arm/mach-qcom/platsmp.c
  824. +++ b/arch/arm/mach-qcom/platsmp.c
  825. @@ -46,7 +46,7 @@
  826. extern void secondary_startup_arm(void);
  827. -static DEFINE_SPINLOCK(boot_lock);
  828. +static DEFINE_RAW_SPINLOCK(boot_lock);
  829. #ifdef CONFIG_HOTPLUG_CPU
  830. static void qcom_cpu_die(unsigned int cpu)
  831. @@ -60,8 +60,8 @@ static void qcom_secondary_init(unsigned int cpu)
  832. /*
  833. * Synchronise with the boot thread.
  834. */
  835. - spin_lock(&boot_lock);
  836. - spin_unlock(&boot_lock);
  837. + raw_spin_lock(&boot_lock);
  838. + raw_spin_unlock(&boot_lock);
  839. }
  840. static int scss_release_secondary(unsigned int cpu)
  841. @@ -284,7 +284,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  842. * set synchronisation state between this boot processor
  843. * and the secondary one
  844. */
  845. - spin_lock(&boot_lock);
  846. + raw_spin_lock(&boot_lock);
  847. /*
  848. * Send the secondary CPU a soft interrupt, thereby causing
  849. @@ -297,7 +297,7 @@ static int qcom_boot_secondary(unsigned int cpu, int (*func)(unsigned int))
  850. * now the secondary core is starting up let it run its
  851. * calibrations, then wait for it to finish
  852. */
  853. - spin_unlock(&boot_lock);
  854. + raw_spin_unlock(&boot_lock);
  855. return ret;
  856. }
  857. diff --git a/arch/arm/mach-spear/platsmp.c b/arch/arm/mach-spear/platsmp.c
  858. index 8d1e2d551786..7fa56cc78118 100644
  859. --- a/arch/arm/mach-spear/platsmp.c
  860. +++ b/arch/arm/mach-spear/platsmp.c
  861. @@ -32,7 +32,7 @@ static void write_pen_release(int val)
  862. sync_cache_w(&pen_release);
  863. }
  864. -static DEFINE_SPINLOCK(boot_lock);
  865. +static DEFINE_RAW_SPINLOCK(boot_lock);
  866. static void __iomem *scu_base = IOMEM(VA_SCU_BASE);
  867. @@ -47,8 +47,8 @@ static void spear13xx_secondary_init(unsigned int cpu)
  868. /*
  869. * Synchronise with the boot thread.
  870. */
  871. - spin_lock(&boot_lock);
  872. - spin_unlock(&boot_lock);
  873. + raw_spin_lock(&boot_lock);
  874. + raw_spin_unlock(&boot_lock);
  875. }
  876. static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  877. @@ -59,7 +59,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  878. * set synchronisation state between this boot processor
  879. * and the secondary one
  880. */
  881. - spin_lock(&boot_lock);
  882. + raw_spin_lock(&boot_lock);
  883. /*
  884. * The secondary processor is waiting to be released from
  885. @@ -84,7 +84,7 @@ static int spear13xx_boot_secondary(unsigned int cpu, struct task_struct *idle)
  886. * now the secondary core is starting up let it run its
  887. * calibrations, then wait for it to finish
  888. */
  889. - spin_unlock(&boot_lock);
  890. + raw_spin_unlock(&boot_lock);
  891. return pen_release != -1 ? -ENOSYS : 0;
  892. }
  893. diff --git a/arch/arm/mach-sti/platsmp.c b/arch/arm/mach-sti/platsmp.c
  894. index ea5a2277ee46..b988e081ac79 100644
  895. --- a/arch/arm/mach-sti/platsmp.c
  896. +++ b/arch/arm/mach-sti/platsmp.c
  897. @@ -35,7 +35,7 @@ static void write_pen_release(int val)
  898. sync_cache_w(&pen_release);
  899. }
  900. -static DEFINE_SPINLOCK(boot_lock);
  901. +static DEFINE_RAW_SPINLOCK(boot_lock);
  902. static void sti_secondary_init(unsigned int cpu)
  903. {
  904. @@ -48,8 +48,8 @@ static void sti_secondary_init(unsigned int cpu)
  905. /*
  906. * Synchronise with the boot thread.
  907. */
  908. - spin_lock(&boot_lock);
  909. - spin_unlock(&boot_lock);
  910. + raw_spin_lock(&boot_lock);
  911. + raw_spin_unlock(&boot_lock);
  912. }
  913. static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  914. @@ -60,7 +60,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  915. * set synchronisation state between this boot processor
  916. * and the secondary one
  917. */
  918. - spin_lock(&boot_lock);
  919. + raw_spin_lock(&boot_lock);
  920. /*
  921. * The secondary processor is waiting to be released from
  922. @@ -91,7 +91,7 @@ static int sti_boot_secondary(unsigned int cpu, struct task_struct *idle)
  923. * now the secondary core is starting up let it run its
  924. * calibrations, then wait for it to finish
  925. */
  926. - spin_unlock(&boot_lock);
  927. + raw_spin_unlock(&boot_lock);
  928. return pen_release != -1 ? -ENOSYS : 0;
  929. }
  930. diff --git a/arch/arm/mm/fault.c b/arch/arm/mm/fault.c
  931. index 3a2e678b8d30..3ed1e9ba6a01 100644
  932. --- a/arch/arm/mm/fault.c
  933. +++ b/arch/arm/mm/fault.c
  934. @@ -430,6 +430,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  935. if (addr < TASK_SIZE)
  936. return do_page_fault(addr, fsr, regs);
  937. + if (interrupts_enabled(regs))
  938. + local_irq_enable();
  939. +
  940. if (user_mode(regs))
  941. goto bad_area;
  942. @@ -497,6 +500,9 @@ do_translation_fault(unsigned long addr, unsigned int fsr,
  943. static int
  944. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  945. {
  946. + if (interrupts_enabled(regs))
  947. + local_irq_enable();
  948. +
  949. do_bad_area(addr, fsr, regs);
  950. return 0;
  951. }
  952. diff --git a/arch/arm/mm/highmem.c b/arch/arm/mm/highmem.c
  953. index d02f8187b1cc..542692dbd40a 100644
  954. --- a/arch/arm/mm/highmem.c
  955. +++ b/arch/arm/mm/highmem.c
  956. @@ -34,6 +34,11 @@ static inline pte_t get_fixmap_pte(unsigned long vaddr)
  957. return *ptep;
  958. }
  959. +static unsigned int fixmap_idx(int type)
  960. +{
  961. + return FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  962. +}
  963. +
  964. void *kmap(struct page *page)
  965. {
  966. might_sleep();
  967. @@ -54,12 +59,13 @@ EXPORT_SYMBOL(kunmap);
  968. void *kmap_atomic(struct page *page)
  969. {
  970. + pte_t pte = mk_pte(page, kmap_prot);
  971. unsigned int idx;
  972. unsigned long vaddr;
  973. void *kmap;
  974. int type;
  975. - preempt_disable();
  976. + preempt_disable_nort();
  977. pagefault_disable();
  978. if (!PageHighMem(page))
  979. return page_address(page);
  980. @@ -79,7 +85,7 @@ void *kmap_atomic(struct page *page)
  981. type = kmap_atomic_idx_push();
  982. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  983. + idx = fixmap_idx(type);
  984. vaddr = __fix_to_virt(idx);
  985. #ifdef CONFIG_DEBUG_HIGHMEM
  986. /*
  987. @@ -93,7 +99,10 @@ void *kmap_atomic(struct page *page)
  988. * in place, so the contained TLB flush ensures the TLB is updated
  989. * with the new mapping.
  990. */
  991. - set_fixmap_pte(idx, mk_pte(page, kmap_prot));
  992. +#ifdef CONFIG_PREEMPT_RT_FULL
  993. + current->kmap_pte[type] = pte;
  994. +#endif
  995. + set_fixmap_pte(idx, pte);
  996. return (void *)vaddr;
  997. }
  998. @@ -106,44 +115,75 @@ void __kunmap_atomic(void *kvaddr)
  999. if (kvaddr >= (void *)FIXADDR_START) {
  1000. type = kmap_atomic_idx();
  1001. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1002. + idx = fixmap_idx(type);
  1003. if (cache_is_vivt())
  1004. __cpuc_flush_dcache_area((void *)vaddr, PAGE_SIZE);
  1005. +#ifdef CONFIG_PREEMPT_RT_FULL
  1006. + current->kmap_pte[type] = __pte(0);
  1007. +#endif
  1008. #ifdef CONFIG_DEBUG_HIGHMEM
  1009. BUG_ON(vaddr != __fix_to_virt(idx));
  1010. - set_fixmap_pte(idx, __pte(0));
  1011. #else
  1012. (void) idx; /* to kill a warning */
  1013. #endif
  1014. + set_fixmap_pte(idx, __pte(0));
  1015. kmap_atomic_idx_pop();
  1016. } else if (vaddr >= PKMAP_ADDR(0) && vaddr < PKMAP_ADDR(LAST_PKMAP)) {
  1017. /* this address was obtained through kmap_high_get() */
  1018. kunmap_high(pte_page(pkmap_page_table[PKMAP_NR(vaddr)]));
  1019. }
  1020. pagefault_enable();
  1021. - preempt_enable();
  1022. + preempt_enable_nort();
  1023. }
  1024. EXPORT_SYMBOL(__kunmap_atomic);
  1025. void *kmap_atomic_pfn(unsigned long pfn)
  1026. {
  1027. + pte_t pte = pfn_pte(pfn, kmap_prot);
  1028. unsigned long vaddr;
  1029. int idx, type;
  1030. struct page *page = pfn_to_page(pfn);
  1031. - preempt_disable();
  1032. + preempt_disable_nort();
  1033. pagefault_disable();
  1034. if (!PageHighMem(page))
  1035. return page_address(page);
  1036. type = kmap_atomic_idx_push();
  1037. - idx = FIX_KMAP_BEGIN + type + KM_TYPE_NR * smp_processor_id();
  1038. + idx = fixmap_idx(type);
  1039. vaddr = __fix_to_virt(idx);
  1040. #ifdef CONFIG_DEBUG_HIGHMEM
  1041. BUG_ON(!pte_none(get_fixmap_pte(vaddr)));
  1042. #endif
  1043. - set_fixmap_pte(idx, pfn_pte(pfn, kmap_prot));
  1044. +#ifdef CONFIG_PREEMPT_RT_FULL
  1045. + current->kmap_pte[type] = pte;
  1046. +#endif
  1047. + set_fixmap_pte(idx, pte);
  1048. return (void *)vaddr;
  1049. }
  1050. +#if defined CONFIG_PREEMPT_RT_FULL
  1051. +void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  1052. +{
  1053. + int i;
  1054. +
  1055. + /*
  1056. + * Clear @prev's kmap_atomic mappings
  1057. + */
  1058. + for (i = 0; i < prev_p->kmap_idx; i++) {
  1059. + int idx = fixmap_idx(i);
  1060. +
  1061. + set_fixmap_pte(idx, __pte(0));
  1062. + }
  1063. + /*
  1064. + * Restore @next_p's kmap_atomic mappings
  1065. + */
  1066. + for (i = 0; i < next_p->kmap_idx; i++) {
  1067. + int idx = fixmap_idx(i);
  1068. +
  1069. + if (!pte_none(next_p->kmap_pte[i]))
  1070. + set_fixmap_pte(idx, next_p->kmap_pte[i]);
  1071. + }
  1072. +}
  1073. +#endif
  1074. diff --git a/arch/arm/plat-versatile/platsmp.c b/arch/arm/plat-versatile/platsmp.c
  1075. index c2366510187a..6b60f582b738 100644
  1076. --- a/arch/arm/plat-versatile/platsmp.c
  1077. +++ b/arch/arm/plat-versatile/platsmp.c
  1078. @@ -32,7 +32,7 @@ static void write_pen_release(int val)
  1079. sync_cache_w(&pen_release);
  1080. }
  1081. -static DEFINE_SPINLOCK(boot_lock);
  1082. +static DEFINE_RAW_SPINLOCK(boot_lock);
  1083. void versatile_secondary_init(unsigned int cpu)
  1084. {
  1085. @@ -45,8 +45,8 @@ void versatile_secondary_init(unsigned int cpu)
  1086. /*
  1087. * Synchronise with the boot thread.
  1088. */
  1089. - spin_lock(&boot_lock);
  1090. - spin_unlock(&boot_lock);
  1091. + raw_spin_lock(&boot_lock);
  1092. + raw_spin_unlock(&boot_lock);
  1093. }
  1094. int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1095. @@ -57,7 +57,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1096. * Set synchronisation state between this boot processor
  1097. * and the secondary one
  1098. */
  1099. - spin_lock(&boot_lock);
  1100. + raw_spin_lock(&boot_lock);
  1101. /*
  1102. * This is really belt and braces; we hold unintended secondary
  1103. @@ -87,7 +87,7 @@ int versatile_boot_secondary(unsigned int cpu, struct task_struct *idle)
  1104. * now the secondary core is starting up let it run its
  1105. * calibrations, then wait for it to finish
  1106. */
  1107. - spin_unlock(&boot_lock);
  1108. + raw_spin_unlock(&boot_lock);
  1109. return pen_release != -1 ? -ENOSYS : 0;
  1110. }
  1111. diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
  1112. index bc3f00f586f1..0f3df6d5154a 100644
  1113. --- a/arch/arm64/Kconfig
  1114. +++ b/arch/arm64/Kconfig
  1115. @@ -90,6 +90,7 @@ config ARM64
  1116. select HAVE_PERF_EVENTS
  1117. select HAVE_PERF_REGS
  1118. select HAVE_PERF_USER_STACK_DUMP
  1119. + select HAVE_PREEMPT_LAZY
  1120. select HAVE_REGS_AND_STACK_ACCESS_API
  1121. select HAVE_RCU_TABLE_FREE
  1122. select HAVE_SYSCALL_TRACEPOINTS
  1123. @@ -689,7 +690,7 @@ config XEN_DOM0
  1124. config XEN
  1125. bool "Xen guest support on ARM64"
  1126. - depends on ARM64 && OF
  1127. + depends on ARM64 && OF && !PREEMPT_RT_FULL
  1128. select SWIOTLB_XEN
  1129. select PARAVIRT
  1130. help
  1131. diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h
  1132. index abd64bd1f6d9..9170788ffa37 100644
  1133. --- a/arch/arm64/include/asm/thread_info.h
  1134. +++ b/arch/arm64/include/asm/thread_info.h
  1135. @@ -49,6 +49,7 @@ struct thread_info {
  1136. mm_segment_t addr_limit; /* address limit */
  1137. struct task_struct *task; /* main task structure */
  1138. int preempt_count; /* 0 => preemptable, <0 => bug */
  1139. + int preempt_lazy_count; /* 0 => preemptable, <0 => bug */
  1140. int cpu; /* cpu */
  1141. };
  1142. @@ -109,6 +110,7 @@ static inline struct thread_info *current_thread_info(void)
  1143. #define TIF_NEED_RESCHED 1
  1144. #define TIF_NOTIFY_RESUME 2 /* callback before returning to user */
  1145. #define TIF_FOREIGN_FPSTATE 3 /* CPU's FP state is not current's */
  1146. +#define TIF_NEED_RESCHED_LAZY 4
  1147. #define TIF_NOHZ 7
  1148. #define TIF_SYSCALL_TRACE 8
  1149. #define TIF_SYSCALL_AUDIT 9
  1150. @@ -124,6 +126,7 @@ static inline struct thread_info *current_thread_info(void)
  1151. #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
  1152. #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
  1153. #define _TIF_FOREIGN_FPSTATE (1 << TIF_FOREIGN_FPSTATE)
  1154. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  1155. #define _TIF_NOHZ (1 << TIF_NOHZ)
  1156. #define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
  1157. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  1158. @@ -132,7 +135,8 @@ static inline struct thread_info *current_thread_info(void)
  1159. #define _TIF_32BIT (1 << TIF_32BIT)
  1160. #define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
  1161. - _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE)
  1162. + _TIF_NOTIFY_RESUME | _TIF_FOREIGN_FPSTATE | \
  1163. + _TIF_NEED_RESCHED_LAZY)
  1164. #define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1165. _TIF_SYSCALL_TRACEPOINT | _TIF_SECCOMP | \
  1166. diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c
  1167. index 05070b72fc28..acfeddb1283a 100644
  1168. --- a/arch/arm64/kernel/asm-offsets.c
  1169. +++ b/arch/arm64/kernel/asm-offsets.c
  1170. @@ -37,6 +37,7 @@ int main(void)
  1171. BLANK();
  1172. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1173. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1174. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1175. DEFINE(TI_ADDR_LIMIT, offsetof(struct thread_info, addr_limit));
  1176. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1177. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1178. diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S
  1179. index 441420ca7d08..404792bdca99 100644
  1180. --- a/arch/arm64/kernel/entry.S
  1181. +++ b/arch/arm64/kernel/entry.S
  1182. @@ -434,11 +434,16 @@ ENDPROC(el1_sync)
  1183. #ifdef CONFIG_PREEMPT
  1184. ldr w24, [tsk, #TI_PREEMPT] // get preempt count
  1185. - cbnz w24, 1f // preempt count != 0
  1186. + cbnz w24, 2f // preempt count != 0
  1187. ldr x0, [tsk, #TI_FLAGS] // get flags
  1188. - tbz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1189. - bl el1_preempt
  1190. + tbnz x0, #TIF_NEED_RESCHED, 1f // needs rescheduling?
  1191. +
  1192. + ldr w24, [tsk, #TI_PREEMPT_LAZY] // get preempt lazy count
  1193. + cbnz w24, 2f // preempt lazy count != 0
  1194. + tbz x0, #TIF_NEED_RESCHED_LAZY, 2f // needs rescheduling?
  1195. 1:
  1196. + bl el1_preempt
  1197. +2:
  1198. #endif
  1199. #ifdef CONFIG_TRACE_IRQFLAGS
  1200. bl trace_hardirqs_on
  1201. @@ -452,6 +457,7 @@ ENDPROC(el1_irq)
  1202. 1: bl preempt_schedule_irq // irq en/disable is done inside
  1203. ldr x0, [tsk, #TI_FLAGS] // get new tasks TI_FLAGS
  1204. tbnz x0, #TIF_NEED_RESCHED, 1b // needs rescheduling?
  1205. + tbnz x0, #TIF_NEED_RESCHED_LAZY, 1b // needs rescheduling?
  1206. ret x24
  1207. #endif
  1208. @@ -708,6 +714,7 @@ ENDPROC(cpu_switch_to)
  1209. */
  1210. work_pending:
  1211. tbnz x1, #TIF_NEED_RESCHED, work_resched
  1212. + tbnz x1, #TIF_NEED_RESCHED_LAZY, work_resched
  1213. /* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
  1214. mov x0, sp // 'regs'
  1215. enable_irq // enable interrupts for do_notify_resume()
  1216. diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
  1217. index 212ff92920d2..71ad38d3d76b 100644
  1218. --- a/arch/mips/Kconfig
  1219. +++ b/arch/mips/Kconfig
  1220. @@ -2480,7 +2480,7 @@ config MIPS_ASID_BITS_VARIABLE
  1221. #
  1222. config HIGHMEM
  1223. bool "High Memory Support"
  1224. - depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA
  1225. + depends on 32BIT && CPU_SUPPORTS_HIGHMEM && SYS_SUPPORTS_HIGHMEM && !CPU_MIPS32_3_5_EVA && !PREEMPT_RT_FULL
  1226. config CPU_SUPPORTS_HIGHMEM
  1227. bool
  1228. diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
  1229. index 792cb1768c8f..ddf5a0fdb25a 100644
  1230. --- a/arch/powerpc/Kconfig
  1231. +++ b/arch/powerpc/Kconfig
  1232. @@ -57,10 +57,11 @@ config LOCKDEP_SUPPORT
  1233. config RWSEM_GENERIC_SPINLOCK
  1234. bool
  1235. + default y if PREEMPT_RT_FULL
  1236. config RWSEM_XCHGADD_ALGORITHM
  1237. bool
  1238. - default y
  1239. + default y if !PREEMPT_RT_FULL
  1240. config GENERIC_LOCKBREAK
  1241. bool
  1242. @@ -140,6 +141,7 @@ config PPC
  1243. select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
  1244. select GENERIC_STRNCPY_FROM_USER
  1245. select GENERIC_STRNLEN_USER
  1246. + select HAVE_PREEMPT_LAZY
  1247. select HAVE_MOD_ARCH_SPECIFIC
  1248. select MODULES_USE_ELF_RELA
  1249. select CLONE_BACKWARDS
  1250. @@ -326,7 +328,7 @@ menu "Kernel options"
  1251. config HIGHMEM
  1252. bool "High memory support"
  1253. - depends on PPC32
  1254. + depends on PPC32 && !PREEMPT_RT_FULL
  1255. source kernel/Kconfig.hz
  1256. source kernel/Kconfig.preempt
  1257. diff --git a/arch/powerpc/include/asm/thread_info.h b/arch/powerpc/include/asm/thread_info.h
  1258. index 87e4b2d8dcd4..981e501a4359 100644
  1259. --- a/arch/powerpc/include/asm/thread_info.h
  1260. +++ b/arch/powerpc/include/asm/thread_info.h
  1261. @@ -43,6 +43,8 @@ struct thread_info {
  1262. int cpu; /* cpu we're on */
  1263. int preempt_count; /* 0 => preemptable,
  1264. <0 => BUG */
  1265. + int preempt_lazy_count; /* 0 => preemptable,
  1266. + <0 => BUG */
  1267. unsigned long local_flags; /* private flags for thread */
  1268. #ifdef CONFIG_LIVEPATCH
  1269. unsigned long *livepatch_sp;
  1270. @@ -88,8 +90,7 @@ static inline struct thread_info *current_thread_info(void)
  1271. #define TIF_SYSCALL_TRACE 0 /* syscall trace active */
  1272. #define TIF_SIGPENDING 1 /* signal pending */
  1273. #define TIF_NEED_RESCHED 2 /* rescheduling necessary */
  1274. -#define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling
  1275. - TIF_NEED_RESCHED */
  1276. +#define TIF_NEED_RESCHED_LAZY 3 /* lazy rescheduling necessary */
  1277. #define TIF_32BIT 4 /* 32 bit binary */
  1278. #define TIF_RESTORE_TM 5 /* need to restore TM FP/VEC/VSX */
  1279. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  1280. @@ -107,6 +108,8 @@ static inline struct thread_info *current_thread_info(void)
  1281. #if defined(CONFIG_PPC64)
  1282. #define TIF_ELF2ABI 18 /* function descriptors must die! */
  1283. #endif
  1284. +#define TIF_POLLING_NRFLAG 19 /* true if poll_idle() is polling
  1285. + TIF_NEED_RESCHED */
  1286. /* as above, but as bit values */
  1287. #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
  1288. @@ -125,14 +128,16 @@ static inline struct thread_info *current_thread_info(void)
  1289. #define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
  1290. #define _TIF_EMULATE_STACK_STORE (1<<TIF_EMULATE_STACK_STORE)
  1291. #define _TIF_NOHZ (1<<TIF_NOHZ)
  1292. +#define _TIF_NEED_RESCHED_LAZY (1<<TIF_NEED_RESCHED_LAZY)
  1293. #define _TIF_SYSCALL_DOTRACE (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  1294. _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \
  1295. _TIF_NOHZ)
  1296. #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
  1297. _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1298. - _TIF_RESTORE_TM)
  1299. + _TIF_RESTORE_TM | _TIF_NEED_RESCHED_LAZY)
  1300. #define _TIF_PERSYSCALL_MASK (_TIF_RESTOREALL|_TIF_NOERROR)
  1301. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  1302. /* Bits in local_flags */
  1303. /* Don't move TLF_NAPPING without adjusting the code in entry_32.S */
  1304. diff --git a/arch/powerpc/kernel/asm-offsets.c b/arch/powerpc/kernel/asm-offsets.c
  1305. index b89d14c0352c..81ae8f4c88f6 100644
  1306. --- a/arch/powerpc/kernel/asm-offsets.c
  1307. +++ b/arch/powerpc/kernel/asm-offsets.c
  1308. @@ -156,6 +156,7 @@ int main(void)
  1309. DEFINE(TI_FLAGS, offsetof(struct thread_info, flags));
  1310. DEFINE(TI_LOCAL_FLAGS, offsetof(struct thread_info, local_flags));
  1311. DEFINE(TI_PREEMPT, offsetof(struct thread_info, preempt_count));
  1312. + DEFINE(TI_PREEMPT_LAZY, offsetof(struct thread_info, preempt_lazy_count));
  1313. DEFINE(TI_TASK, offsetof(struct thread_info, task));
  1314. DEFINE(TI_CPU, offsetof(struct thread_info, cpu));
  1315. diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
  1316. index 9899032230b4..f95b93f46c47 100644
  1317. --- a/arch/powerpc/kernel/entry_32.S
  1318. +++ b/arch/powerpc/kernel/entry_32.S
  1319. @@ -835,7 +835,14 @@ user_exc_return: /* r10 contains MSR_KERNEL here */
  1320. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1321. bne restore
  1322. andi. r8,r8,_TIF_NEED_RESCHED
  1323. + bne+ 1f
  1324. + lwz r0,TI_PREEMPT_LAZY(r9)
  1325. + cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  1326. + bne restore
  1327. + lwz r0,TI_FLAGS(r9)
  1328. + andi. r0,r0,_TIF_NEED_RESCHED_LAZY
  1329. beq+ restore
  1330. +1:
  1331. lwz r3,_MSR(r1)
  1332. andi. r0,r3,MSR_EE /* interrupts off? */
  1333. beq restore /* don't schedule if so */
  1334. @@ -846,11 +853,11 @@ user_exc_return: /* r10 contains MSR_KERNEL here */
  1335. */
  1336. bl trace_hardirqs_off
  1337. #endif
  1338. -1: bl preempt_schedule_irq
  1339. +2: bl preempt_schedule_irq
  1340. CURRENT_THREAD_INFO(r9, r1)
  1341. lwz r3,TI_FLAGS(r9)
  1342. - andi. r0,r3,_TIF_NEED_RESCHED
  1343. - bne- 1b
  1344. + andi. r0,r3,_TIF_NEED_RESCHED_MASK
  1345. + bne- 2b
  1346. #ifdef CONFIG_TRACE_IRQFLAGS
  1347. /* And now, to properly rebalance the above, we tell lockdep they
  1348. * are being turned back on, which will happen when we return
  1349. @@ -1171,7 +1178,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  1350. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1351. do_work: /* r10 contains MSR_KERNEL here */
  1352. - andi. r0,r9,_TIF_NEED_RESCHED
  1353. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1354. beq do_user_signal
  1355. do_resched: /* r10 contains MSR_KERNEL here */
  1356. @@ -1192,7 +1199,7 @@ do_resched: /* r10 contains MSR_KERNEL here */
  1357. MTMSRD(r10) /* disable interrupts */
  1358. CURRENT_THREAD_INFO(r9, r1)
  1359. lwz r9,TI_FLAGS(r9)
  1360. - andi. r0,r9,_TIF_NEED_RESCHED
  1361. + andi. r0,r9,_TIF_NEED_RESCHED_MASK
  1362. bne- do_resched
  1363. andi. r0,r9,_TIF_USER_WORK_MASK
  1364. beq restore_user
  1365. diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
  1366. index 5afd03e5e8b8..f5d4c2a033ef 100644
  1367. --- a/arch/powerpc/kernel/entry_64.S
  1368. +++ b/arch/powerpc/kernel/entry_64.S
  1369. @@ -657,7 +657,7 @@ _GLOBAL(ret_from_except_lite)
  1370. bl restore_math
  1371. b restore
  1372. #endif
  1373. -1: andi. r0,r4,_TIF_NEED_RESCHED
  1374. +1: andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1375. beq 2f
  1376. bl restore_interrupts
  1377. SCHEDULE_USER
  1378. @@ -719,10 +719,18 @@ _GLOBAL(ret_from_except_lite)
  1379. #ifdef CONFIG_PREEMPT
  1380. /* Check if we need to preempt */
  1381. - andi. r0,r4,_TIF_NEED_RESCHED
  1382. - beq+ restore
  1383. - /* Check that preempt_count() == 0 and interrupts are enabled */
  1384. lwz r8,TI_PREEMPT(r9)
  1385. + cmpwi 0,r8,0 /* if non-zero, just restore regs and return */
  1386. + bne restore
  1387. + andi. r0,r4,_TIF_NEED_RESCHED
  1388. + bne+ check_count
  1389. +
  1390. + andi. r0,r4,_TIF_NEED_RESCHED_LAZY
  1391. + beq+ restore
  1392. + lwz r8,TI_PREEMPT_LAZY(r9)
  1393. +
  1394. + /* Check that preempt_count() == 0 and interrupts are enabled */
  1395. +check_count:
  1396. cmpwi cr1,r8,0
  1397. ld r0,SOFTE(r1)
  1398. cmpdi r0,0
  1399. @@ -739,7 +747,7 @@ _GLOBAL(ret_from_except_lite)
  1400. /* Re-test flags and eventually loop */
  1401. CURRENT_THREAD_INFO(r9, r1)
  1402. ld r4,TI_FLAGS(r9)
  1403. - andi. r0,r4,_TIF_NEED_RESCHED
  1404. + andi. r0,r4,_TIF_NEED_RESCHED_MASK
  1405. bne 1b
  1406. /*
  1407. diff --git a/arch/powerpc/kernel/irq.c b/arch/powerpc/kernel/irq.c
  1408. index 08887cf2b20e..f1770ea2d094 100644
  1409. --- a/arch/powerpc/kernel/irq.c
  1410. +++ b/arch/powerpc/kernel/irq.c
  1411. @@ -633,6 +633,7 @@ void irq_ctx_init(void)
  1412. }
  1413. }
  1414. +#ifndef CONFIG_PREEMPT_RT_FULL
  1415. void do_softirq_own_stack(void)
  1416. {
  1417. struct thread_info *curtp, *irqtp;
  1418. @@ -650,6 +651,7 @@ void do_softirq_own_stack(void)
  1419. if (irqtp->flags)
  1420. set_bits(irqtp->flags, &curtp->flags);
  1421. }
  1422. +#endif
  1423. irq_hw_number_t virq_to_hw(unsigned int virq)
  1424. {
  1425. diff --git a/arch/powerpc/kernel/misc_32.S b/arch/powerpc/kernel/misc_32.S
  1426. index d9c912b6e632..7b2e997a5083 100644
  1427. --- a/arch/powerpc/kernel/misc_32.S
  1428. +++ b/arch/powerpc/kernel/misc_32.S
  1429. @@ -40,6 +40,7 @@
  1430. * We store the saved ksp_limit in the unused part
  1431. * of the STACK_FRAME_OVERHEAD
  1432. */
  1433. +#ifndef CONFIG_PREEMPT_RT_FULL
  1434. _GLOBAL(call_do_softirq)
  1435. mflr r0
  1436. stw r0,4(r1)
  1437. @@ -56,6 +57,7 @@ _GLOBAL(call_do_softirq)
  1438. stw r10,THREAD+KSP_LIMIT(r2)
  1439. mtlr r0
  1440. blr
  1441. +#endif
  1442. /*
  1443. * void call_do_irq(struct pt_regs *regs, struct thread_info *irqtp);
  1444. diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
  1445. index cb195157b318..c919a2bfd0ca 100644
  1446. --- a/arch/powerpc/kernel/misc_64.S
  1447. +++ b/arch/powerpc/kernel/misc_64.S
  1448. @@ -30,6 +30,7 @@
  1449. .text
  1450. +#ifndef CONFIG_PREEMPT_RT_FULL
  1451. _GLOBAL(call_do_softirq)
  1452. mflr r0
  1453. std r0,16(r1)
  1454. @@ -40,6 +41,7 @@ _GLOBAL(call_do_softirq)
  1455. ld r0,16(r1)
  1456. mtlr r0
  1457. blr
  1458. +#endif
  1459. _GLOBAL(call_do_irq)
  1460. mflr r0
  1461. diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig
  1462. index c2024ac9d4e8..2303788da7e1 100644
  1463. --- a/arch/powerpc/kvm/Kconfig
  1464. +++ b/arch/powerpc/kvm/Kconfig
  1465. @@ -172,6 +172,7 @@ config KVM_E500MC
  1466. config KVM_MPIC
  1467. bool "KVM in-kernel MPIC emulation"
  1468. depends on KVM && E500
  1469. + depends on !PREEMPT_RT_FULL
  1470. select HAVE_KVM_IRQCHIP
  1471. select HAVE_KVM_IRQFD
  1472. select HAVE_KVM_IRQ_ROUTING
  1473. diff --git a/arch/powerpc/platforms/ps3/device-init.c b/arch/powerpc/platforms/ps3/device-init.c
  1474. index 57caaf11a83f..030c9bfe52e3 100644
  1475. --- a/arch/powerpc/platforms/ps3/device-init.c
  1476. +++ b/arch/powerpc/platforms/ps3/device-init.c
  1477. @@ -752,7 +752,7 @@ static int ps3_notification_read_write(struct ps3_notification_device *dev,
  1478. }
  1479. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  1480. - res = wait_event_interruptible(dev->done.wait,
  1481. + res = swait_event_interruptible(dev->done.wait,
  1482. dev->done.done || kthread_should_stop());
  1483. if (kthread_should_stop())
  1484. res = -EINTR;
  1485. diff --git a/arch/sh/kernel/irq.c b/arch/sh/kernel/irq.c
  1486. index 6c0378c0b8b5..abd58b4dff97 100644
  1487. --- a/arch/sh/kernel/irq.c
  1488. +++ b/arch/sh/kernel/irq.c
  1489. @@ -147,6 +147,7 @@ void irq_ctx_exit(int cpu)
  1490. hardirq_ctx[cpu] = NULL;
  1491. }
  1492. +#ifndef CONFIG_PREEMPT_RT_FULL
  1493. void do_softirq_own_stack(void)
  1494. {
  1495. struct thread_info *curctx;
  1496. @@ -174,6 +175,7 @@ void do_softirq_own_stack(void)
  1497. "r5", "r6", "r7", "r8", "r9", "r15", "t", "pr"
  1498. );
  1499. }
  1500. +#endif
  1501. #else
  1502. static inline void handle_one_irq(unsigned int irq)
  1503. {
  1504. diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
  1505. index 59b09600dd32..1b073eb3dc2a 100644
  1506. --- a/arch/sparc/Kconfig
  1507. +++ b/arch/sparc/Kconfig
  1508. @@ -187,12 +187,10 @@ config NR_CPUS
  1509. source kernel/Kconfig.hz
  1510. config RWSEM_GENERIC_SPINLOCK
  1511. - bool
  1512. - default y if SPARC32
  1513. + def_bool PREEMPT_RT_FULL
  1514. config RWSEM_XCHGADD_ALGORITHM
  1515. - bool
  1516. - default y if SPARC64
  1517. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1518. config GENERIC_HWEIGHT
  1519. bool
  1520. diff --git a/arch/sparc/kernel/irq_64.c b/arch/sparc/kernel/irq_64.c
  1521. index 34a7930b76ef..773740521008 100644
  1522. --- a/arch/sparc/kernel/irq_64.c
  1523. +++ b/arch/sparc/kernel/irq_64.c
  1524. @@ -854,6 +854,7 @@ void __irq_entry handler_irq(int pil, struct pt_regs *regs)
  1525. set_irq_regs(old_regs);
  1526. }
  1527. +#ifndef CONFIG_PREEMPT_RT_FULL
  1528. void do_softirq_own_stack(void)
  1529. {
  1530. void *orig_sp, *sp = softirq_stack[smp_processor_id()];
  1531. @@ -868,6 +869,7 @@ void do_softirq_own_stack(void)
  1532. __asm__ __volatile__("mov %0, %%sp"
  1533. : : "r" (orig_sp));
  1534. }
  1535. +#endif
  1536. #ifdef CONFIG_HOTPLUG_CPU
  1537. void fixup_irqs(void)
  1538. diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
  1539. index 2a1f0ce7c59a..bd4ab87efb31 100644
  1540. --- a/arch/x86/Kconfig
  1541. +++ b/arch/x86/Kconfig
  1542. @@ -17,6 +17,7 @@ config X86_64
  1543. ### Arch settings
  1544. config X86
  1545. def_bool y
  1546. + select HAVE_PREEMPT_LAZY
  1547. select ACPI_LEGACY_TABLES_LOOKUP if ACPI
  1548. select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI
  1549. select ANON_INODES
  1550. @@ -231,8 +232,11 @@ config ARCH_MAY_HAVE_PC_FDC
  1551. def_bool y
  1552. depends on ISA_DMA_API
  1553. +config RWSEM_GENERIC_SPINLOCK
  1554. + def_bool PREEMPT_RT_FULL
  1555. +
  1556. config RWSEM_XCHGADD_ALGORITHM
  1557. - def_bool y
  1558. + def_bool !RWSEM_GENERIC_SPINLOCK && !PREEMPT_RT_FULL
  1559. config GENERIC_CALIBRATE_DELAY
  1560. def_bool y
  1561. @@ -885,7 +889,7 @@ config IOMMU_HELPER
  1562. config MAXSMP
  1563. bool "Enable Maximum number of SMP Processors and NUMA Nodes"
  1564. depends on X86_64 && SMP && DEBUG_KERNEL
  1565. - select CPUMASK_OFFSTACK
  1566. + select CPUMASK_OFFSTACK if !PREEMPT_RT_FULL
  1567. ---help---
  1568. Enable maximum number of CPUS and NUMA Nodes for this architecture.
  1569. If unsure, say N.
  1570. diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c
  1571. index 0ab5ee1c26af..fff8f6f1f90c 100644
  1572. --- a/arch/x86/crypto/aesni-intel_glue.c
  1573. +++ b/arch/x86/crypto/aesni-intel_glue.c
  1574. @@ -372,14 +372,14 @@ static int ecb_encrypt(struct blkcipher_desc *desc,
  1575. err = blkcipher_walk_virt(desc, &walk);
  1576. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1577. - kernel_fpu_begin();
  1578. while ((nbytes = walk.nbytes)) {
  1579. + kernel_fpu_begin();
  1580. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1581. - nbytes & AES_BLOCK_MASK);
  1582. + nbytes & AES_BLOCK_MASK);
  1583. + kernel_fpu_end();
  1584. nbytes &= AES_BLOCK_SIZE - 1;
  1585. err = blkcipher_walk_done(desc, &walk, nbytes);
  1586. }
  1587. - kernel_fpu_end();
  1588. return err;
  1589. }
  1590. @@ -396,14 +396,14 @@ static int ecb_decrypt(struct blkcipher_desc *desc,
  1591. err = blkcipher_walk_virt(desc, &walk);
  1592. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1593. - kernel_fpu_begin();
  1594. while ((nbytes = walk.nbytes)) {
  1595. + kernel_fpu_begin();
  1596. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1597. nbytes & AES_BLOCK_MASK);
  1598. + kernel_fpu_end();
  1599. nbytes &= AES_BLOCK_SIZE - 1;
  1600. err = blkcipher_walk_done(desc, &walk, nbytes);
  1601. }
  1602. - kernel_fpu_end();
  1603. return err;
  1604. }
  1605. @@ -420,14 +420,14 @@ static int cbc_encrypt(struct blkcipher_desc *desc,
  1606. err = blkcipher_walk_virt(desc, &walk);
  1607. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1608. - kernel_fpu_begin();
  1609. while ((nbytes = walk.nbytes)) {
  1610. + kernel_fpu_begin();
  1611. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1612. nbytes & AES_BLOCK_MASK, walk.iv);
  1613. + kernel_fpu_end();
  1614. nbytes &= AES_BLOCK_SIZE - 1;
  1615. err = blkcipher_walk_done(desc, &walk, nbytes);
  1616. }
  1617. - kernel_fpu_end();
  1618. return err;
  1619. }
  1620. @@ -444,14 +444,14 @@ static int cbc_decrypt(struct blkcipher_desc *desc,
  1621. err = blkcipher_walk_virt(desc, &walk);
  1622. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1623. - kernel_fpu_begin();
  1624. while ((nbytes = walk.nbytes)) {
  1625. + kernel_fpu_begin();
  1626. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1627. nbytes & AES_BLOCK_MASK, walk.iv);
  1628. + kernel_fpu_end();
  1629. nbytes &= AES_BLOCK_SIZE - 1;
  1630. err = blkcipher_walk_done(desc, &walk, nbytes);
  1631. }
  1632. - kernel_fpu_end();
  1633. return err;
  1634. }
  1635. @@ -503,18 +503,20 @@ static int ctr_crypt(struct blkcipher_desc *desc,
  1636. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  1637. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1638. - kernel_fpu_begin();
  1639. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  1640. + kernel_fpu_begin();
  1641. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  1642. nbytes & AES_BLOCK_MASK, walk.iv);
  1643. + kernel_fpu_end();
  1644. nbytes &= AES_BLOCK_SIZE - 1;
  1645. err = blkcipher_walk_done(desc, &walk, nbytes);
  1646. }
  1647. if (walk.nbytes) {
  1648. + kernel_fpu_begin();
  1649. ctr_crypt_final(ctx, &walk);
  1650. + kernel_fpu_end();
  1651. err = blkcipher_walk_done(desc, &walk, 0);
  1652. }
  1653. - kernel_fpu_end();
  1654. return err;
  1655. }
  1656. diff --git a/arch/x86/crypto/cast5_avx_glue.c b/arch/x86/crypto/cast5_avx_glue.c
  1657. index 8648158f3916..d7699130ee36 100644
  1658. --- a/arch/x86/crypto/cast5_avx_glue.c
  1659. +++ b/arch/x86/crypto/cast5_avx_glue.c
  1660. @@ -59,7 +59,7 @@ static inline void cast5_fpu_end(bool fpu_enabled)
  1661. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  1662. bool enc)
  1663. {
  1664. - bool fpu_enabled = false;
  1665. + bool fpu_enabled;
  1666. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  1667. const unsigned int bsize = CAST5_BLOCK_SIZE;
  1668. unsigned int nbytes;
  1669. @@ -75,7 +75,7 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  1670. u8 *wsrc = walk->src.virt.addr;
  1671. u8 *wdst = walk->dst.virt.addr;
  1672. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1673. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1674. /* Process multi-block batch */
  1675. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  1676. @@ -103,10 +103,9 @@ static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  1677. } while (nbytes >= bsize);
  1678. done:
  1679. + cast5_fpu_end(fpu_enabled);
  1680. err = blkcipher_walk_done(desc, walk, nbytes);
  1681. }
  1682. -
  1683. - cast5_fpu_end(fpu_enabled);
  1684. return err;
  1685. }
  1686. @@ -227,7 +226,7 @@ static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
  1687. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1688. struct scatterlist *src, unsigned int nbytes)
  1689. {
  1690. - bool fpu_enabled = false;
  1691. + bool fpu_enabled;
  1692. struct blkcipher_walk walk;
  1693. int err;
  1694. @@ -236,12 +235,11 @@ static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1695. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1696. while ((nbytes = walk.nbytes)) {
  1697. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1698. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1699. nbytes = __cbc_decrypt(desc, &walk);
  1700. + cast5_fpu_end(fpu_enabled);
  1701. err = blkcipher_walk_done(desc, &walk, nbytes);
  1702. }
  1703. -
  1704. - cast5_fpu_end(fpu_enabled);
  1705. return err;
  1706. }
  1707. @@ -311,7 +309,7 @@ static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  1708. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1709. struct scatterlist *src, unsigned int nbytes)
  1710. {
  1711. - bool fpu_enabled = false;
  1712. + bool fpu_enabled;
  1713. struct blkcipher_walk walk;
  1714. int err;
  1715. @@ -320,13 +318,12 @@ static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  1716. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  1717. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  1718. - fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  1719. + fpu_enabled = cast5_fpu_begin(false, nbytes);
  1720. nbytes = __ctr_crypt(desc, &walk);
  1721. + cast5_fpu_end(fpu_enabled);
  1722. err = blkcipher_walk_done(desc, &walk, nbytes);
  1723. }
  1724. - cast5_fpu_end(fpu_enabled);
  1725. -
  1726. if (walk.nbytes) {
  1727. ctr_crypt_final(desc, &walk);
  1728. err = blkcipher_walk_done(desc, &walk, 0);
  1729. diff --git a/arch/x86/crypto/glue_helper.c b/arch/x86/crypto/glue_helper.c
  1730. index 6a85598931b5..3a506ce7ed93 100644
  1731. --- a/arch/x86/crypto/glue_helper.c
  1732. +++ b/arch/x86/crypto/glue_helper.c
  1733. @@ -39,7 +39,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  1734. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  1735. const unsigned int bsize = 128 / 8;
  1736. unsigned int nbytes, i, func_bytes;
  1737. - bool fpu_enabled = false;
  1738. + bool fpu_enabled;
  1739. int err;
  1740. err = blkcipher_walk_virt(desc, walk);
  1741. @@ -49,7 +49,7 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  1742. u8 *wdst = walk->dst.virt.addr;
  1743. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1744. - desc, fpu_enabled, nbytes);
  1745. + desc, false, nbytes);
  1746. for (i = 0; i < gctx->num_funcs; i++) {
  1747. func_bytes = bsize * gctx->funcs[i].num_blocks;
  1748. @@ -71,10 +71,10 @@ static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  1749. }
  1750. done:
  1751. + glue_fpu_end(fpu_enabled);
  1752. err = blkcipher_walk_done(desc, walk, nbytes);
  1753. }
  1754. - glue_fpu_end(fpu_enabled);
  1755. return err;
  1756. }
  1757. @@ -194,7 +194,7 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  1758. struct scatterlist *src, unsigned int nbytes)
  1759. {
  1760. const unsigned int bsize = 128 / 8;
  1761. - bool fpu_enabled = false;
  1762. + bool fpu_enabled;
  1763. struct blkcipher_walk walk;
  1764. int err;
  1765. @@ -203,12 +203,12 @@ int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  1766. while ((nbytes = walk.nbytes)) {
  1767. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1768. - desc, fpu_enabled, nbytes);
  1769. + desc, false, nbytes);
  1770. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  1771. + glue_fpu_end(fpu_enabled);
  1772. err = blkcipher_walk_done(desc, &walk, nbytes);
  1773. }
  1774. - glue_fpu_end(fpu_enabled);
  1775. return err;
  1776. }
  1777. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  1778. @@ -277,7 +277,7 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  1779. struct scatterlist *src, unsigned int nbytes)
  1780. {
  1781. const unsigned int bsize = 128 / 8;
  1782. - bool fpu_enabled = false;
  1783. + bool fpu_enabled;
  1784. struct blkcipher_walk walk;
  1785. int err;
  1786. @@ -286,13 +286,12 @@ int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  1787. while ((nbytes = walk.nbytes) >= bsize) {
  1788. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1789. - desc, fpu_enabled, nbytes);
  1790. + desc, false, nbytes);
  1791. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  1792. + glue_fpu_end(fpu_enabled);
  1793. err = blkcipher_walk_done(desc, &walk, nbytes);
  1794. }
  1795. - glue_fpu_end(fpu_enabled);
  1796. -
  1797. if (walk.nbytes) {
  1798. glue_ctr_crypt_final_128bit(
  1799. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  1800. @@ -347,7 +346,7 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  1801. void *tweak_ctx, void *crypt_ctx)
  1802. {
  1803. const unsigned int bsize = 128 / 8;
  1804. - bool fpu_enabled = false;
  1805. + bool fpu_enabled;
  1806. struct blkcipher_walk walk;
  1807. int err;
  1808. @@ -360,21 +359,21 @@ int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  1809. /* set minimum length to bsize, for tweak_fn */
  1810. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1811. - desc, fpu_enabled,
  1812. + desc, false,
  1813. nbytes < bsize ? bsize : nbytes);
  1814. -
  1815. /* calculate first value of T */
  1816. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  1817. + glue_fpu_end(fpu_enabled);
  1818. while (nbytes) {
  1819. + fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  1820. + desc, false, nbytes);
  1821. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  1822. + glue_fpu_end(fpu_enabled);
  1823. err = blkcipher_walk_done(desc, &walk, nbytes);
  1824. nbytes = walk.nbytes;
  1825. }
  1826. -
  1827. - glue_fpu_end(fpu_enabled);
  1828. -
  1829. return err;
  1830. }
  1831. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  1832. diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
  1833. index 1433f6b4607d..f963fde8e4fa 100644
  1834. --- a/arch/x86/entry/common.c
  1835. +++ b/arch/x86/entry/common.c
  1836. @@ -136,7 +136,7 @@ static long syscall_trace_enter(struct pt_regs *regs)
  1837. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  1838. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  1839. - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  1840. + _TIF_NEED_RESCHED_MASK | _TIF_USER_RETURN_NOTIFY)
  1841. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  1842. {
  1843. @@ -152,9 +152,16 @@ static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  1844. /* We have work to do. */
  1845. local_irq_enable();
  1846. - if (cached_flags & _TIF_NEED_RESCHED)
  1847. + if (cached_flags & _TIF_NEED_RESCHED_MASK)
  1848. schedule();
  1849. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  1850. + if (unlikely(current->forced_info.si_signo)) {
  1851. + struct task_struct *t = current;
  1852. + force_sig_info(t->forced_info.si_signo, &t->forced_info, t);
  1853. + t->forced_info.si_signo = 0;
  1854. + }
  1855. +#endif
  1856. if (cached_flags & _TIF_UPROBE)
  1857. uprobe_notify_resume(regs);
  1858. diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
  1859. index 0b56666e6039..1d8ee026c9c5 100644
  1860. --- a/arch/x86/entry/entry_32.S
  1861. +++ b/arch/x86/entry/entry_32.S
  1862. @@ -271,8 +271,25 @@ END(ret_from_exception)
  1863. ENTRY(resume_kernel)
  1864. DISABLE_INTERRUPTS(CLBR_ANY)
  1865. need_resched:
  1866. + # preempt count == 0 + NEED_RS set?
  1867. cmpl $0, PER_CPU_VAR(__preempt_count)
  1868. +#ifndef CONFIG_PREEMPT_LAZY
  1869. jnz restore_all
  1870. +#else
  1871. + jz test_int_off
  1872. +
  1873. + # atleast preempt count == 0 ?
  1874. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1875. + jne restore_all
  1876. +
  1877. + GET_THREAD_INFO(%ebp)
  1878. + cmpl $0,TI_preempt_lazy_count(%ebp) # non-zero preempt_lazy_count ?
  1879. + jnz restore_all
  1880. +
  1881. + testl $_TIF_NEED_RESCHED_LAZY, TI_flags(%ebp)
  1882. + jz restore_all
  1883. +test_int_off:
  1884. +#endif
  1885. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  1886. jz restore_all
  1887. call preempt_schedule_irq
  1888. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  1889. index 02fff3ebfb87..81ec3d016df0 100644
  1890. --- a/arch/x86/entry/entry_64.S
  1891. +++ b/arch/x86/entry/entry_64.S
  1892. @@ -512,7 +512,23 @@ GLOBAL(retint_user)
  1893. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  1894. jnc 1f
  1895. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  1896. +#ifndef CONFIG_PREEMPT_LAZY
  1897. jnz 1f
  1898. +#else
  1899. + jz do_preempt_schedule_irq
  1900. +
  1901. + # atleast preempt count == 0 ?
  1902. + cmpl $_PREEMPT_ENABLED,PER_CPU_VAR(__preempt_count)
  1903. + jnz 1f
  1904. +
  1905. + GET_THREAD_INFO(%rcx)
  1906. + cmpl $0, TI_preempt_lazy_count(%rcx)
  1907. + jnz 1f
  1908. +
  1909. + bt $TIF_NEED_RESCHED_LAZY,TI_flags(%rcx)
  1910. + jnc 1f
  1911. +do_preempt_schedule_irq:
  1912. +#endif
  1913. call preempt_schedule_irq
  1914. jmp 0b
  1915. 1:
  1916. @@ -817,6 +833,7 @@ END(native_load_gs_index)
  1917. jmp 2b
  1918. .previous
  1919. +#ifndef CONFIG_PREEMPT_RT_FULL
  1920. /* Call softirq on interrupt stack. Interrupts are off. */
  1921. ENTRY(do_softirq_own_stack)
  1922. pushq %rbp
  1923. @@ -829,6 +846,7 @@ ENTRY(do_softirq_own_stack)
  1924. decl PER_CPU_VAR(irq_count)
  1925. ret
  1926. END(do_softirq_own_stack)
  1927. +#endif
  1928. #ifdef CONFIG_XEN
  1929. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  1930. diff --git a/arch/x86/include/asm/preempt.h b/arch/x86/include/asm/preempt.h
  1931. index 17f218645701..11bd1b7ee6eb 100644
  1932. --- a/arch/x86/include/asm/preempt.h
  1933. +++ b/arch/x86/include/asm/preempt.h
  1934. @@ -79,17 +79,46 @@ static __always_inline void __preempt_count_sub(int val)
  1935. * a decrement which hits zero means we have no preempt_count and should
  1936. * reschedule.
  1937. */
  1938. -static __always_inline bool __preempt_count_dec_and_test(void)
  1939. +static __always_inline bool ____preempt_count_dec_and_test(void)
  1940. {
  1941. GEN_UNARY_RMWcc("decl", __preempt_count, __percpu_arg(0), e);
  1942. }
  1943. +static __always_inline bool __preempt_count_dec_and_test(void)
  1944. +{
  1945. + if (____preempt_count_dec_and_test())
  1946. + return true;
  1947. +#ifdef CONFIG_PREEMPT_LAZY
  1948. + if (current_thread_info()->preempt_lazy_count)
  1949. + return false;
  1950. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1951. +#else
  1952. + return false;
  1953. +#endif
  1954. +}
  1955. +
  1956. /*
  1957. * Returns true when we need to resched and can (barring IRQ state).
  1958. */
  1959. static __always_inline bool should_resched(int preempt_offset)
  1960. {
  1961. +#ifdef CONFIG_PREEMPT_LAZY
  1962. + u32 tmp;
  1963. +
  1964. + tmp = raw_cpu_read_4(__preempt_count);
  1965. + if (tmp == preempt_offset)
  1966. + return true;
  1967. +
  1968. + /* preempt count == 0 ? */
  1969. + tmp &= ~PREEMPT_NEED_RESCHED;
  1970. + if (tmp)
  1971. + return false;
  1972. + if (current_thread_info()->preempt_lazy_count)
  1973. + return false;
  1974. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  1975. +#else
  1976. return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
  1977. +#endif
  1978. }
  1979. #ifdef CONFIG_PREEMPT
  1980. diff --git a/arch/x86/include/asm/signal.h b/arch/x86/include/asm/signal.h
  1981. index dd1e7d6387ab..d59bedb28bab 100644
  1982. --- a/arch/x86/include/asm/signal.h
  1983. +++ b/arch/x86/include/asm/signal.h
  1984. @@ -23,6 +23,19 @@ typedef struct {
  1985. unsigned long sig[_NSIG_WORDS];
  1986. } sigset_t;
  1987. +/*
  1988. + * Because some traps use the IST stack, we must keep preemption
  1989. + * disabled while calling do_trap(), but do_trap() may call
  1990. + * force_sig_info() which will grab the signal spin_locks for the
  1991. + * task, which in PREEMPT_RT_FULL are mutexes. By defining
  1992. + * ARCH_RT_DELAYS_SIGNAL_SEND the force_sig_info() will set
  1993. + * TIF_NOTIFY_RESUME and set up the signal to be sent on exit of the
  1994. + * trap.
  1995. + */
  1996. +#if defined(CONFIG_PREEMPT_RT_FULL)
  1997. +#define ARCH_RT_DELAYS_SIGNAL_SEND
  1998. +#endif
  1999. +
  2000. #ifndef CONFIG_COMPAT
  2001. typedef sigset_t compat_sigset_t;
  2002. #endif
  2003. diff --git a/arch/x86/include/asm/stackprotector.h b/arch/x86/include/asm/stackprotector.h
  2004. index 58505f01962f..02fa39652cd6 100644
  2005. --- a/arch/x86/include/asm/stackprotector.h
  2006. +++ b/arch/x86/include/asm/stackprotector.h
  2007. @@ -59,7 +59,7 @@
  2008. */
  2009. static __always_inline void boot_init_stack_canary(void)
  2010. {
  2011. - u64 canary;
  2012. + u64 uninitialized_var(canary);
  2013. u64 tsc;
  2014. #ifdef CONFIG_X86_64
  2015. @@ -70,8 +70,15 @@ static __always_inline void boot_init_stack_canary(void)
  2016. * of randomness. The TSC only matters for very early init,
  2017. * there it already has some randomness on most systems. Later
  2018. * on during the bootup the random pool has true entropy too.
  2019. + *
  2020. + * For preempt-rt we need to weaken the randomness a bit, as
  2021. + * we can't call into the random generator from atomic context
  2022. + * due to locking constraints. We just leave canary
  2023. + * uninitialized and use the TSC based randomness on top of it.
  2024. */
  2025. +#ifndef CONFIG_PREEMPT_RT_FULL
  2026. get_random_bytes(&canary, sizeof(canary));
  2027. +#endif
  2028. tsc = rdtsc();
  2029. canary += tsc + (tsc << 32UL);
  2030. diff --git a/arch/x86/include/asm/thread_info.h b/arch/x86/include/asm/thread_info.h
  2031. index 8b7c8d8e0852..631059ef61da 100644
  2032. --- a/arch/x86/include/asm/thread_info.h
  2033. +++ b/arch/x86/include/asm/thread_info.h
  2034. @@ -57,6 +57,8 @@ struct thread_info {
  2035. __u32 flags; /* low level flags */
  2036. __u32 status; /* thread synchronous flags */
  2037. __u32 cpu; /* current CPU */
  2038. + int preempt_lazy_count; /* 0 => lazy preemptable
  2039. + <0 => BUG */
  2040. };
  2041. #define INIT_THREAD_INFO(tsk) \
  2042. @@ -73,6 +75,10 @@ struct thread_info {
  2043. #include <asm/asm-offsets.h>
  2044. +#define GET_THREAD_INFO(reg) \
  2045. + _ASM_MOV PER_CPU_VAR(cpu_current_top_of_stack),reg ; \
  2046. + _ASM_SUB $(THREAD_SIZE),reg ;
  2047. +
  2048. #endif
  2049. /*
  2050. @@ -91,6 +97,7 @@ struct thread_info {
  2051. #define TIF_SYSCALL_EMU 6 /* syscall emulation active */
  2052. #define TIF_SYSCALL_AUDIT 7 /* syscall auditing active */
  2053. #define TIF_SECCOMP 8 /* secure computing */
  2054. +#define TIF_NEED_RESCHED_LAZY 9 /* lazy rescheduling necessary */
  2055. #define TIF_USER_RETURN_NOTIFY 11 /* notify kernel of userspace return */
  2056. #define TIF_UPROBE 12 /* breakpointed or singlestepping */
  2057. #define TIF_NOTSC 16 /* TSC is not accessible in userland */
  2058. @@ -115,6 +122,7 @@ struct thread_info {
  2059. #define _TIF_SYSCALL_EMU (1 << TIF_SYSCALL_EMU)
  2060. #define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
  2061. #define _TIF_SECCOMP (1 << TIF_SECCOMP)
  2062. +#define _TIF_NEED_RESCHED_LAZY (1 << TIF_NEED_RESCHED_LAZY)
  2063. #define _TIF_USER_RETURN_NOTIFY (1 << TIF_USER_RETURN_NOTIFY)
  2064. #define _TIF_UPROBE (1 << TIF_UPROBE)
  2065. #define _TIF_NOTSC (1 << TIF_NOTSC)
  2066. @@ -151,6 +159,8 @@ struct thread_info {
  2067. #define _TIF_WORK_CTXSW_PREV (_TIF_WORK_CTXSW|_TIF_USER_RETURN_NOTIFY)
  2068. #define _TIF_WORK_CTXSW_NEXT (_TIF_WORK_CTXSW)
  2069. +#define _TIF_NEED_RESCHED_MASK (_TIF_NEED_RESCHED | _TIF_NEED_RESCHED_LAZY)
  2070. +
  2071. #define STACK_WARN (THREAD_SIZE/8)
  2072. /*
  2073. diff --git a/arch/x86/include/asm/uv/uv_bau.h b/arch/x86/include/asm/uv/uv_bau.h
  2074. index cc44d926c17e..df278aa0f638 100644
  2075. --- a/arch/x86/include/asm/uv/uv_bau.h
  2076. +++ b/arch/x86/include/asm/uv/uv_bau.h
  2077. @@ -615,9 +615,9 @@ struct bau_control {
  2078. cycles_t send_message;
  2079. cycles_t period_end;
  2080. cycles_t period_time;
  2081. - spinlock_t uvhub_lock;
  2082. - spinlock_t queue_lock;
  2083. - spinlock_t disable_lock;
  2084. + raw_spinlock_t uvhub_lock;
  2085. + raw_spinlock_t queue_lock;
  2086. + raw_spinlock_t disable_lock;
  2087. /* tunables */
  2088. int max_concurr;
  2089. int max_concurr_const;
  2090. @@ -776,15 +776,15 @@ static inline int atom_asr(short i, struct atomic_short *v)
  2091. * to be lowered below the current 'v'. atomic_add_unless can only stop
  2092. * on equal.
  2093. */
  2094. -static inline int atomic_inc_unless_ge(spinlock_t *lock, atomic_t *v, int u)
  2095. +static inline int atomic_inc_unless_ge(raw_spinlock_t *lock, atomic_t *v, int u)
  2096. {
  2097. - spin_lock(lock);
  2098. + raw_spin_lock(lock);
  2099. if (atomic_read(v) >= u) {
  2100. - spin_unlock(lock);
  2101. + raw_spin_unlock(lock);
  2102. return 0;
  2103. }
  2104. atomic_inc(v);
  2105. - spin_unlock(lock);
  2106. + raw_spin_unlock(lock);
  2107. return 1;
  2108. }
  2109. diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c
  2110. index fbd19444403f..e78f477a4ae3 100644
  2111. --- a/arch/x86/kernel/acpi/boot.c
  2112. +++ b/arch/x86/kernel/acpi/boot.c
  2113. @@ -87,7 +87,9 @@ static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;
  2114. * ->ioapic_mutex
  2115. * ->ioapic_lock
  2116. */
  2117. +#ifdef CONFIG_X86_IO_APIC
  2118. static DEFINE_MUTEX(acpi_ioapic_lock);
  2119. +#endif
  2120. /* --------------------------------------------------------------------------
  2121. Boot-time Configuration
  2122. diff --git a/arch/x86/kernel/apic/io_apic.c b/arch/x86/kernel/apic/io_apic.c
  2123. index 48e6d84f173e..0b5a8b994f65 100644
  2124. --- a/arch/x86/kernel/apic/io_apic.c
  2125. +++ b/arch/x86/kernel/apic/io_apic.c
  2126. @@ -1712,7 +1712,8 @@ static bool io_apic_level_ack_pending(struct mp_chip_data *data)
  2127. static inline bool ioapic_irqd_mask(struct irq_data *data)
  2128. {
  2129. /* If we are moving the irq we need to mask it */
  2130. - if (unlikely(irqd_is_setaffinity_pending(data))) {
  2131. + if (unlikely(irqd_is_setaffinity_pending(data) &&
  2132. + !irqd_irq_inprogress(data))) {
  2133. mask_ioapic_irq(data);
  2134. return true;
  2135. }
  2136. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  2137. index 2bd5c6ff7ee7..a2c317f5839b 100644
  2138. --- a/arch/x86/kernel/asm-offsets.c
  2139. +++ b/arch/x86/kernel/asm-offsets.c
  2140. @@ -31,6 +31,7 @@ void common(void) {
  2141. BLANK();
  2142. OFFSET(TI_flags, thread_info, flags);
  2143. OFFSET(TI_status, thread_info, status);
  2144. + OFFSET(TI_preempt_lazy_count, thread_info, preempt_lazy_count);
  2145. BLANK();
  2146. OFFSET(TASK_addr_limit, task_struct, thread.addr_limit);
  2147. @@ -88,4 +89,5 @@ void common(void) {
  2148. BLANK();
  2149. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  2150. + DEFINE(_PREEMPT_ENABLED, PREEMPT_ENABLED);
  2151. }
  2152. diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
  2153. index 79d8ec849468..accbf0e806d0 100644
  2154. --- a/arch/x86/kernel/cpu/mcheck/mce.c
  2155. +++ b/arch/x86/kernel/cpu/mcheck/mce.c
  2156. @@ -41,6 +41,8 @@
  2157. #include <linux/debugfs.h>
  2158. #include <linux/irq_work.h>
  2159. #include <linux/export.h>
  2160. +#include <linux/jiffies.h>
  2161. +#include <linux/swork.h>
  2162. #include <asm/processor.h>
  2163. #include <asm/traps.h>
  2164. @@ -1291,7 +1293,7 @@ void mce_log_therm_throt_event(__u64 status)
  2165. static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
  2166. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  2167. -static DEFINE_PER_CPU(struct timer_list, mce_timer);
  2168. +static DEFINE_PER_CPU(struct hrtimer, mce_timer);
  2169. static unsigned long mce_adjust_timer_default(unsigned long interval)
  2170. {
  2171. @@ -1300,32 +1302,18 @@ static unsigned long mce_adjust_timer_default(unsigned long interval)
  2172. static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
  2173. -static void __restart_timer(struct timer_list *t, unsigned long interval)
  2174. +static enum hrtimer_restart __restart_timer(struct hrtimer *timer, unsigned long interval)
  2175. {
  2176. - unsigned long when = jiffies + interval;
  2177. - unsigned long flags;
  2178. -
  2179. - local_irq_save(flags);
  2180. -
  2181. - if (timer_pending(t)) {
  2182. - if (time_before(when, t->expires))
  2183. - mod_timer(t, when);
  2184. - } else {
  2185. - t->expires = round_jiffies(when);
  2186. - add_timer_on(t, smp_processor_id());
  2187. - }
  2188. -
  2189. - local_irq_restore(flags);
  2190. + if (!interval)
  2191. + return HRTIMER_NORESTART;
  2192. + hrtimer_forward_now(timer, ns_to_ktime(jiffies_to_nsecs(interval)));
  2193. + return HRTIMER_RESTART;
  2194. }
  2195. -static void mce_timer_fn(unsigned long data)
  2196. +static enum hrtimer_restart mce_timer_fn(struct hrtimer *timer)
  2197. {
  2198. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2199. - int cpu = smp_processor_id();
  2200. unsigned long iv;
  2201. - WARN_ON(cpu != data);
  2202. -
  2203. iv = __this_cpu_read(mce_next_interval);
  2204. if (mce_available(this_cpu_ptr(&cpu_info))) {
  2205. @@ -1348,7 +1336,7 @@ static void mce_timer_fn(unsigned long data)
  2206. done:
  2207. __this_cpu_write(mce_next_interval, iv);
  2208. - __restart_timer(t, iv);
  2209. + return __restart_timer(timer, iv);
  2210. }
  2211. /*
  2212. @@ -1356,7 +1344,7 @@ static void mce_timer_fn(unsigned long data)
  2213. */
  2214. void mce_timer_kick(unsigned long interval)
  2215. {
  2216. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2217. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2218. unsigned long iv = __this_cpu_read(mce_next_interval);
  2219. __restart_timer(t, interval);
  2220. @@ -1371,7 +1359,7 @@ static void mce_timer_delete_all(void)
  2221. int cpu;
  2222. for_each_online_cpu(cpu)
  2223. - del_timer_sync(&per_cpu(mce_timer, cpu));
  2224. + hrtimer_cancel(&per_cpu(mce_timer, cpu));
  2225. }
  2226. static void mce_do_trigger(struct work_struct *work)
  2227. @@ -1381,6 +1369,56 @@ static void mce_do_trigger(struct work_struct *work)
  2228. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2229. +static void __mce_notify_work(struct swork_event *event)
  2230. +{
  2231. + /* Not more than two messages every minute */
  2232. + static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2233. +
  2234. + /* wake processes polling /dev/mcelog */
  2235. + wake_up_interruptible(&mce_chrdev_wait);
  2236. +
  2237. + /*
  2238. + * There is no risk of missing notifications because
  2239. + * work_pending is always cleared before the function is
  2240. + * executed.
  2241. + */
  2242. + if (mce_helper[0] && !work_pending(&mce_trigger_work))
  2243. + schedule_work(&mce_trigger_work);
  2244. +
  2245. + if (__ratelimit(&ratelimit))
  2246. + pr_info(HW_ERR "Machine check events logged\n");
  2247. +}
  2248. +
  2249. +#ifdef CONFIG_PREEMPT_RT_FULL
  2250. +static bool notify_work_ready __read_mostly;
  2251. +static struct swork_event notify_work;
  2252. +
  2253. +static int mce_notify_work_init(void)
  2254. +{
  2255. + int err;
  2256. +
  2257. + err = swork_get();
  2258. + if (err)
  2259. + return err;
  2260. +
  2261. + INIT_SWORK(&notify_work, __mce_notify_work);
  2262. + notify_work_ready = true;
  2263. + return 0;
  2264. +}
  2265. +
  2266. +static void mce_notify_work(void)
  2267. +{
  2268. + if (notify_work_ready)
  2269. + swork_queue(&notify_work);
  2270. +}
  2271. +#else
  2272. +static void mce_notify_work(void)
  2273. +{
  2274. + __mce_notify_work(NULL);
  2275. +}
  2276. +static inline int mce_notify_work_init(void) { return 0; }
  2277. +#endif
  2278. +
  2279. /*
  2280. * Notify the user(s) about new machine check events.
  2281. * Can be called from interrupt context, but not from machine check/NMI
  2282. @@ -1388,19 +1426,8 @@ static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  2283. */
  2284. int mce_notify_irq(void)
  2285. {
  2286. - /* Not more than two messages every minute */
  2287. - static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  2288. -
  2289. if (test_and_clear_bit(0, &mce_need_notify)) {
  2290. - /* wake processes polling /dev/mcelog */
  2291. - wake_up_interruptible(&mce_chrdev_wait);
  2292. -
  2293. - if (mce_helper[0])
  2294. - schedule_work(&mce_trigger_work);
  2295. -
  2296. - if (__ratelimit(&ratelimit))
  2297. - pr_info(HW_ERR "Machine check events logged\n");
  2298. -
  2299. + mce_notify_work();
  2300. return 1;
  2301. }
  2302. return 0;
  2303. @@ -1717,7 +1744,7 @@ static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
  2304. }
  2305. }
  2306. -static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2307. +static void mce_start_timer(unsigned int cpu, struct hrtimer *t)
  2308. {
  2309. unsigned long iv = check_interval * HZ;
  2310. @@ -1726,16 +1753,17 @@ static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  2311. per_cpu(mce_next_interval, cpu) = iv;
  2312. - t->expires = round_jiffies(jiffies + iv);
  2313. - add_timer_on(t, cpu);
  2314. + hrtimer_start_range_ns(t, ns_to_ktime(jiffies_to_usecs(iv) * 1000ULL),
  2315. + 0, HRTIMER_MODE_REL_PINNED);
  2316. }
  2317. static void __mcheck_cpu_init_timer(void)
  2318. {
  2319. - struct timer_list *t = this_cpu_ptr(&mce_timer);
  2320. + struct hrtimer *t = this_cpu_ptr(&mce_timer);
  2321. unsigned int cpu = smp_processor_id();
  2322. - setup_pinned_timer(t, mce_timer_fn, cpu);
  2323. + hrtimer_init(t, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2324. + t->function = mce_timer_fn;
  2325. mce_start_timer(cpu, t);
  2326. }
  2327. @@ -2459,6 +2487,8 @@ static void mce_disable_cpu(void *h)
  2328. if (!mce_available(raw_cpu_ptr(&cpu_info)))
  2329. return;
  2330. + hrtimer_cancel(this_cpu_ptr(&mce_timer));
  2331. +
  2332. if (!(action & CPU_TASKS_FROZEN))
  2333. cmci_clear();
  2334. @@ -2481,6 +2511,7 @@ static void mce_reenable_cpu(void *h)
  2335. if (b->init)
  2336. wrmsrl(msr_ops.ctl(i), b->ctl);
  2337. }
  2338. + __mcheck_cpu_init_timer();
  2339. }
  2340. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  2341. @@ -2488,7 +2519,6 @@ static int
  2342. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2343. {
  2344. unsigned int cpu = (unsigned long)hcpu;
  2345. - struct timer_list *t = &per_cpu(mce_timer, cpu);
  2346. switch (action & ~CPU_TASKS_FROZEN) {
  2347. case CPU_ONLINE:
  2348. @@ -2508,11 +2538,9 @@ mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  2349. break;
  2350. case CPU_DOWN_PREPARE:
  2351. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  2352. - del_timer_sync(t);
  2353. break;
  2354. case CPU_DOWN_FAILED:
  2355. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  2356. - mce_start_timer(cpu, t);
  2357. break;
  2358. }
  2359. @@ -2551,6 +2579,10 @@ static __init int mcheck_init_device(void)
  2360. goto err_out;
  2361. }
  2362. + err = mce_notify_work_init();
  2363. + if (err)
  2364. + goto err_out;
  2365. +
  2366. if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
  2367. err = -ENOMEM;
  2368. goto err_out;
  2369. diff --git a/arch/x86/kernel/dumpstack_32.c b/arch/x86/kernel/dumpstack_32.c
  2370. index 09675712eba8..eea7557b355d 100644
  2371. --- a/arch/x86/kernel/dumpstack_32.c
  2372. +++ b/arch/x86/kernel/dumpstack_32.c
  2373. @@ -42,7 +42,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2374. unsigned long *stack, unsigned long bp,
  2375. const struct stacktrace_ops *ops, void *data)
  2376. {
  2377. - const unsigned cpu = get_cpu();
  2378. + const unsigned cpu = get_cpu_light();
  2379. int graph = 0;
  2380. u32 *prev_esp;
  2381. @@ -84,7 +84,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2382. break;
  2383. touch_nmi_watchdog();
  2384. }
  2385. - put_cpu();
  2386. + put_cpu_light();
  2387. }
  2388. EXPORT_SYMBOL(dump_trace);
  2389. diff --git a/arch/x86/kernel/dumpstack_64.c b/arch/x86/kernel/dumpstack_64.c
  2390. index 9ee4520ce83c..2cd610b68868 100644
  2391. --- a/arch/x86/kernel/dumpstack_64.c
  2392. +++ b/arch/x86/kernel/dumpstack_64.c
  2393. @@ -152,7 +152,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2394. unsigned long *stack, unsigned long bp,
  2395. const struct stacktrace_ops *ops, void *data)
  2396. {
  2397. - const unsigned cpu = get_cpu();
  2398. + const unsigned cpu = get_cpu_light();
  2399. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  2400. unsigned long dummy;
  2401. unsigned used = 0;
  2402. @@ -239,7 +239,7 @@ void dump_trace(struct task_struct *task, struct pt_regs *regs,
  2403. * This handles the process stack:
  2404. */
  2405. bp = ops->walk_stack(task, stack, bp, ops, data, NULL, &graph);
  2406. - put_cpu();
  2407. + put_cpu_light();
  2408. }
  2409. EXPORT_SYMBOL(dump_trace);
  2410. @@ -253,7 +253,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2411. int cpu;
  2412. int i;
  2413. - preempt_disable();
  2414. + migrate_disable();
  2415. cpu = smp_processor_id();
  2416. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  2417. @@ -299,7 +299,7 @@ show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  2418. stack++;
  2419. touch_nmi_watchdog();
  2420. }
  2421. - preempt_enable();
  2422. + migrate_enable();
  2423. pr_cont("\n");
  2424. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  2425. diff --git a/arch/x86/kernel/irq_32.c b/arch/x86/kernel/irq_32.c
  2426. index 1f38d9a4d9de..053bf3b2ef39 100644
  2427. --- a/arch/x86/kernel/irq_32.c
  2428. +++ b/arch/x86/kernel/irq_32.c
  2429. @@ -127,6 +127,7 @@ void irq_ctx_init(int cpu)
  2430. cpu, per_cpu(hardirq_stack, cpu), per_cpu(softirq_stack, cpu));
  2431. }
  2432. +#ifndef CONFIG_PREEMPT_RT_FULL
  2433. void do_softirq_own_stack(void)
  2434. {
  2435. struct irq_stack *irqstk;
  2436. @@ -143,6 +144,7 @@ void do_softirq_own_stack(void)
  2437. call_on_stack(__do_softirq, isp);
  2438. }
  2439. +#endif
  2440. bool handle_irq(struct irq_desc *desc, struct pt_regs *regs)
  2441. {
  2442. diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c
  2443. index d86be29c38c7..b0e29d1a0571 100644
  2444. --- a/arch/x86/kernel/process_32.c
  2445. +++ b/arch/x86/kernel/process_32.c
  2446. @@ -35,6 +35,7 @@
  2447. #include <linux/uaccess.h>
  2448. #include <linux/io.h>
  2449. #include <linux/kdebug.h>
  2450. +#include <linux/highmem.h>
  2451. #include <asm/pgtable.h>
  2452. #include <asm/ldt.h>
  2453. @@ -210,6 +211,35 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  2454. }
  2455. EXPORT_SYMBOL_GPL(start_thread);
  2456. +#ifdef CONFIG_PREEMPT_RT_FULL
  2457. +static void switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p)
  2458. +{
  2459. + int i;
  2460. +
  2461. + /*
  2462. + * Clear @prev's kmap_atomic mappings
  2463. + */
  2464. + for (i = 0; i < prev_p->kmap_idx; i++) {
  2465. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2466. + pte_t *ptep = kmap_pte - idx;
  2467. +
  2468. + kpte_clear_flush(ptep, __fix_to_virt(FIX_KMAP_BEGIN + idx));
  2469. + }
  2470. + /*
  2471. + * Restore @next_p's kmap_atomic mappings
  2472. + */
  2473. + for (i = 0; i < next_p->kmap_idx; i++) {
  2474. + int idx = i + KM_TYPE_NR * smp_processor_id();
  2475. +
  2476. + if (!pte_none(next_p->kmap_pte[i]))
  2477. + set_pte(kmap_pte - idx, next_p->kmap_pte[i]);
  2478. + }
  2479. +}
  2480. +#else
  2481. +static inline void
  2482. +switch_kmaps(struct task_struct *prev_p, struct task_struct *next_p) { }
  2483. +#endif
  2484. +
  2485. /*
  2486. * switch_to(x,y) should switch tasks from x to y.
  2487. @@ -286,6 +316,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  2488. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  2489. __switch_to_xtra(prev_p, next_p, tss);
  2490. + switch_kmaps(prev_p, next_p);
  2491. +
  2492. /*
  2493. * Leave lazy mode, flushing any hypercalls made here.
  2494. * This must be done before restoring TLS segments so
  2495. diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
  2496. index b62c85229711..d907b281a9d6 100644
  2497. --- a/arch/x86/kvm/lapic.c
  2498. +++ b/arch/x86/kvm/lapic.c
  2499. @@ -1938,6 +1938,7 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu)
  2500. hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
  2501. HRTIMER_MODE_ABS_PINNED);
  2502. apic->lapic_timer.timer.function = apic_timer_fn;
  2503. + apic->lapic_timer.timer.irqsafe = 1;
  2504. /*
  2505. * APIC is created enabled. This will prevent kvm_lapic_set_base from
  2506. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
  2507. index 699f8726539a..24f30c86510c 100644
  2508. --- a/arch/x86/kvm/x86.c
  2509. +++ b/arch/x86/kvm/x86.c
  2510. @@ -5865,6 +5865,13 @@ int kvm_arch_init(void *opaque)
  2511. goto out;
  2512. }
  2513. +#ifdef CONFIG_PREEMPT_RT_FULL
  2514. + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  2515. + printk(KERN_ERR "RT requires X86_FEATURE_CONSTANT_TSC\n");
  2516. + return -EOPNOTSUPP;
  2517. + }
  2518. +#endif
  2519. +
  2520. r = kvm_mmu_module_init();
  2521. if (r)
  2522. goto out_free_percpu;
  2523. diff --git a/arch/x86/mm/highmem_32.c b/arch/x86/mm/highmem_32.c
  2524. index 6d18b70ed5a9..f752724c22e8 100644
  2525. --- a/arch/x86/mm/highmem_32.c
  2526. +++ b/arch/x86/mm/highmem_32.c
  2527. @@ -32,10 +32,11 @@ EXPORT_SYMBOL(kunmap);
  2528. */
  2529. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  2530. {
  2531. + pte_t pte = mk_pte(page, prot);
  2532. unsigned long vaddr;
  2533. int idx, type;
  2534. - preempt_disable();
  2535. + preempt_disable_nort();
  2536. pagefault_disable();
  2537. if (!PageHighMem(page))
  2538. @@ -45,7 +46,10 @@ void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  2539. idx = type + KM_TYPE_NR*smp_processor_id();
  2540. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2541. BUG_ON(!pte_none(*(kmap_pte-idx)));
  2542. - set_pte(kmap_pte-idx, mk_pte(page, prot));
  2543. +#ifdef CONFIG_PREEMPT_RT_FULL
  2544. + current->kmap_pte[type] = pte;
  2545. +#endif
  2546. + set_pte(kmap_pte-idx, pte);
  2547. arch_flush_lazy_mmu_mode();
  2548. return (void *)vaddr;
  2549. @@ -88,6 +92,9 @@ void __kunmap_atomic(void *kvaddr)
  2550. * is a bad idea also, in case the page changes cacheability
  2551. * attributes or becomes a protected page in a hypervisor.
  2552. */
  2553. +#ifdef CONFIG_PREEMPT_RT_FULL
  2554. + current->kmap_pte[type] = __pte(0);
  2555. +#endif
  2556. kpte_clear_flush(kmap_pte-idx, vaddr);
  2557. kmap_atomic_idx_pop();
  2558. arch_flush_lazy_mmu_mode();
  2559. @@ -100,7 +107,7 @@ void __kunmap_atomic(void *kvaddr)
  2560. #endif
  2561. pagefault_enable();
  2562. - preempt_enable();
  2563. + preempt_enable_nort();
  2564. }
  2565. EXPORT_SYMBOL(__kunmap_atomic);
  2566. diff --git a/arch/x86/mm/iomap_32.c b/arch/x86/mm/iomap_32.c
  2567. index ada98b39b8ad..585f6829653b 100644
  2568. --- a/arch/x86/mm/iomap_32.c
  2569. +++ b/arch/x86/mm/iomap_32.c
  2570. @@ -56,6 +56,7 @@ EXPORT_SYMBOL_GPL(iomap_free);
  2571. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  2572. {
  2573. + pte_t pte = pfn_pte(pfn, prot);
  2574. unsigned long vaddr;
  2575. int idx, type;
  2576. @@ -65,7 +66,12 @@ void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  2577. type = kmap_atomic_idx_push();
  2578. idx = type + KM_TYPE_NR * smp_processor_id();
  2579. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  2580. - set_pte(kmap_pte - idx, pfn_pte(pfn, prot));
  2581. + WARN_ON(!pte_none(*(kmap_pte - idx)));
  2582. +
  2583. +#ifdef CONFIG_PREEMPT_RT_FULL
  2584. + current->kmap_pte[type] = pte;
  2585. +#endif
  2586. + set_pte(kmap_pte - idx, pte);
  2587. arch_flush_lazy_mmu_mode();
  2588. return (void *)vaddr;
  2589. @@ -113,6 +119,9 @@ iounmap_atomic(void __iomem *kvaddr)
  2590. * is a bad idea also, in case the page changes cacheability
  2591. * attributes or becomes a protected page in a hypervisor.
  2592. */
  2593. +#ifdef CONFIG_PREEMPT_RT_FULL
  2594. + current->kmap_pte[type] = __pte(0);
  2595. +#endif
  2596. kpte_clear_flush(kmap_pte-idx, vaddr);
  2597. kmap_atomic_idx_pop();
  2598. }
  2599. diff --git a/arch/x86/platform/uv/tlb_uv.c b/arch/x86/platform/uv/tlb_uv.c
  2600. index fdb4d42b4ce5..8ab90fbecff0 100644
  2601. --- a/arch/x86/platform/uv/tlb_uv.c
  2602. +++ b/arch/x86/platform/uv/tlb_uv.c
  2603. @@ -729,9 +729,9 @@ static void destination_plugged(struct bau_desc *bau_desc,
  2604. quiesce_local_uvhub(hmaster);
  2605. - spin_lock(&hmaster->queue_lock);
  2606. + raw_spin_lock(&hmaster->queue_lock);
  2607. reset_with_ipi(&bau_desc->distribution, bcp);
  2608. - spin_unlock(&hmaster->queue_lock);
  2609. + raw_spin_unlock(&hmaster->queue_lock);
  2610. end_uvhub_quiesce(hmaster);
  2611. @@ -751,9 +751,9 @@ static void destination_timeout(struct bau_desc *bau_desc,
  2612. quiesce_local_uvhub(hmaster);
  2613. - spin_lock(&hmaster->queue_lock);
  2614. + raw_spin_lock(&hmaster->queue_lock);
  2615. reset_with_ipi(&bau_desc->distribution, bcp);
  2616. - spin_unlock(&hmaster->queue_lock);
  2617. + raw_spin_unlock(&hmaster->queue_lock);
  2618. end_uvhub_quiesce(hmaster);
  2619. @@ -774,7 +774,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  2620. cycles_t tm1;
  2621. hmaster = bcp->uvhub_master;
  2622. - spin_lock(&hmaster->disable_lock);
  2623. + raw_spin_lock(&hmaster->disable_lock);
  2624. if (!bcp->baudisabled) {
  2625. stat->s_bau_disabled++;
  2626. tm1 = get_cycles();
  2627. @@ -787,7 +787,7 @@ static void disable_for_period(struct bau_control *bcp, struct ptc_stats *stat)
  2628. }
  2629. }
  2630. }
  2631. - spin_unlock(&hmaster->disable_lock);
  2632. + raw_spin_unlock(&hmaster->disable_lock);
  2633. }
  2634. static void count_max_concurr(int stat, struct bau_control *bcp,
  2635. @@ -850,7 +850,7 @@ static void record_send_stats(cycles_t time1, cycles_t time2,
  2636. */
  2637. static void uv1_throttle(struct bau_control *hmaster, struct ptc_stats *stat)
  2638. {
  2639. - spinlock_t *lock = &hmaster->uvhub_lock;
  2640. + raw_spinlock_t *lock = &hmaster->uvhub_lock;
  2641. atomic_t *v;
  2642. v = &hmaster->active_descriptor_count;
  2643. @@ -983,7 +983,7 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  2644. struct bau_control *hmaster;
  2645. hmaster = bcp->uvhub_master;
  2646. - spin_lock(&hmaster->disable_lock);
  2647. + raw_spin_lock(&hmaster->disable_lock);
  2648. if (bcp->baudisabled && (get_cycles() >= bcp->set_bau_on_time)) {
  2649. stat->s_bau_reenabled++;
  2650. for_each_present_cpu(tcpu) {
  2651. @@ -995,10 +995,10 @@ static int check_enable(struct bau_control *bcp, struct ptc_stats *stat)
  2652. tbcp->period_giveups = 0;
  2653. }
  2654. }
  2655. - spin_unlock(&hmaster->disable_lock);
  2656. + raw_spin_unlock(&hmaster->disable_lock);
  2657. return 0;
  2658. }
  2659. - spin_unlock(&hmaster->disable_lock);
  2660. + raw_spin_unlock(&hmaster->disable_lock);
  2661. return -1;
  2662. }
  2663. @@ -1916,9 +1916,9 @@ static void __init init_per_cpu_tunables(void)
  2664. bcp->cong_reps = congested_reps;
  2665. bcp->disabled_period = sec_2_cycles(disabled_period);
  2666. bcp->giveup_limit = giveup_limit;
  2667. - spin_lock_init(&bcp->queue_lock);
  2668. - spin_lock_init(&bcp->uvhub_lock);
  2669. - spin_lock_init(&bcp->disable_lock);
  2670. + raw_spin_lock_init(&bcp->queue_lock);
  2671. + raw_spin_lock_init(&bcp->uvhub_lock);
  2672. + raw_spin_lock_init(&bcp->disable_lock);
  2673. }
  2674. }
  2675. diff --git a/arch/x86/platform/uv/uv_time.c b/arch/x86/platform/uv/uv_time.c
  2676. index b333fc45f9ec..8b85916e6986 100644
  2677. --- a/arch/x86/platform/uv/uv_time.c
  2678. +++ b/arch/x86/platform/uv/uv_time.c
  2679. @@ -57,7 +57,7 @@ static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  2680. /* There is one of these allocated per node */
  2681. struct uv_rtc_timer_head {
  2682. - spinlock_t lock;
  2683. + raw_spinlock_t lock;
  2684. /* next cpu waiting for timer, local node relative: */
  2685. int next_cpu;
  2686. /* number of cpus on this node: */
  2687. @@ -177,7 +177,7 @@ static __init int uv_rtc_allocate_timers(void)
  2688. uv_rtc_deallocate_timers();
  2689. return -ENOMEM;
  2690. }
  2691. - spin_lock_init(&head->lock);
  2692. + raw_spin_lock_init(&head->lock);
  2693. head->ncpus = uv_blade_nr_possible_cpus(bid);
  2694. head->next_cpu = -1;
  2695. blade_info[bid] = head;
  2696. @@ -231,7 +231,7 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  2697. unsigned long flags;
  2698. int next_cpu;
  2699. - spin_lock_irqsave(&head->lock, flags);
  2700. + raw_spin_lock_irqsave(&head->lock, flags);
  2701. next_cpu = head->next_cpu;
  2702. *t = expires;
  2703. @@ -243,12 +243,12 @@ static int uv_rtc_set_timer(int cpu, u64 expires)
  2704. if (uv_setup_intr(cpu, expires)) {
  2705. *t = ULLONG_MAX;
  2706. uv_rtc_find_next_timer(head, pnode);
  2707. - spin_unlock_irqrestore(&head->lock, flags);
  2708. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2709. return -ETIME;
  2710. }
  2711. }
  2712. - spin_unlock_irqrestore(&head->lock, flags);
  2713. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2714. return 0;
  2715. }
  2716. @@ -267,7 +267,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  2717. unsigned long flags;
  2718. int rc = 0;
  2719. - spin_lock_irqsave(&head->lock, flags);
  2720. + raw_spin_lock_irqsave(&head->lock, flags);
  2721. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  2722. rc = 1;
  2723. @@ -279,7 +279,7 @@ static int uv_rtc_unset_timer(int cpu, int force)
  2724. uv_rtc_find_next_timer(head, pnode);
  2725. }
  2726. - spin_unlock_irqrestore(&head->lock, flags);
  2727. + raw_spin_unlock_irqrestore(&head->lock, flags);
  2728. return rc;
  2729. }
  2730. @@ -299,13 +299,18 @@ static int uv_rtc_unset_timer(int cpu, int force)
  2731. static cycle_t uv_read_rtc(struct clocksource *cs)
  2732. {
  2733. unsigned long offset;
  2734. + cycle_t cycles;
  2735. + preempt_disable();
  2736. if (uv_get_min_hub_revision_id() == 1)
  2737. offset = 0;
  2738. else
  2739. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  2740. - return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  2741. + cycles = (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  2742. + preempt_enable();
  2743. +
  2744. + return cycles;
  2745. }
  2746. /*
  2747. diff --git a/block/blk-core.c b/block/blk-core.c
  2748. index 36c7ac328d8c..caa5fc1be2a2 100644
  2749. --- a/block/blk-core.c
  2750. +++ b/block/blk-core.c
  2751. @@ -125,6 +125,9 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
  2752. INIT_LIST_HEAD(&rq->queuelist);
  2753. INIT_LIST_HEAD(&rq->timeout_list);
  2754. +#ifdef CONFIG_PREEMPT_RT_FULL
  2755. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  2756. +#endif
  2757. rq->cpu = -1;
  2758. rq->q = q;
  2759. rq->__sector = (sector_t) -1;
  2760. @@ -233,7 +236,7 @@ EXPORT_SYMBOL(blk_start_queue_async);
  2761. **/
  2762. void blk_start_queue(struct request_queue *q)
  2763. {
  2764. - WARN_ON(!irqs_disabled());
  2765. + WARN_ON_NONRT(!irqs_disabled());
  2766. queue_flag_clear(QUEUE_FLAG_STOPPED, q);
  2767. __blk_run_queue(q);
  2768. @@ -659,7 +662,7 @@ int blk_queue_enter(struct request_queue *q, bool nowait)
  2769. if (nowait)
  2770. return -EBUSY;
  2771. - ret = wait_event_interruptible(q->mq_freeze_wq,
  2772. + ret = swait_event_interruptible(q->mq_freeze_wq,
  2773. !atomic_read(&q->mq_freeze_depth) ||
  2774. blk_queue_dying(q));
  2775. if (blk_queue_dying(q))
  2776. @@ -679,7 +682,7 @@ static void blk_queue_usage_counter_release(struct percpu_ref *ref)
  2777. struct request_queue *q =
  2778. container_of(ref, struct request_queue, q_usage_counter);
  2779. - wake_up_all(&q->mq_freeze_wq);
  2780. + swake_up_all(&q->mq_freeze_wq);
  2781. }
  2782. static void blk_rq_timed_out_timer(unsigned long data)
  2783. @@ -748,7 +751,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
  2784. q->bypass_depth = 1;
  2785. __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
  2786. - init_waitqueue_head(&q->mq_freeze_wq);
  2787. + init_swait_queue_head(&q->mq_freeze_wq);
  2788. /*
  2789. * Init percpu_ref in atomic mode so that it's faster to shutdown.
  2790. @@ -3171,7 +3174,7 @@ static void queue_unplugged(struct request_queue *q, unsigned int depth,
  2791. blk_run_queue_async(q);
  2792. else
  2793. __blk_run_queue(q);
  2794. - spin_unlock(q->queue_lock);
  2795. + spin_unlock_irq(q->queue_lock);
  2796. }
  2797. static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
  2798. @@ -3219,7 +3222,6 @@ EXPORT_SYMBOL(blk_check_plugged);
  2799. void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2800. {
  2801. struct request_queue *q;
  2802. - unsigned long flags;
  2803. struct request *rq;
  2804. LIST_HEAD(list);
  2805. unsigned int depth;
  2806. @@ -3239,11 +3241,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2807. q = NULL;
  2808. depth = 0;
  2809. - /*
  2810. - * Save and disable interrupts here, to avoid doing it for every
  2811. - * queue lock we have to take.
  2812. - */
  2813. - local_irq_save(flags);
  2814. while (!list_empty(&list)) {
  2815. rq = list_entry_rq(list.next);
  2816. list_del_init(&rq->queuelist);
  2817. @@ -3256,7 +3253,7 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2818. queue_unplugged(q, depth, from_schedule);
  2819. q = rq->q;
  2820. depth = 0;
  2821. - spin_lock(q->queue_lock);
  2822. + spin_lock_irq(q->queue_lock);
  2823. }
  2824. /*
  2825. @@ -3283,8 +3280,6 @@ void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
  2826. */
  2827. if (q)
  2828. queue_unplugged(q, depth, from_schedule);
  2829. -
  2830. - local_irq_restore(flags);
  2831. }
  2832. void blk_finish_plug(struct blk_plug *plug)
  2833. diff --git a/block/blk-ioc.c b/block/blk-ioc.c
  2834. index 381cb50a673c..dc8785233d94 100644
  2835. --- a/block/blk-ioc.c
  2836. +++ b/block/blk-ioc.c
  2837. @@ -7,6 +7,7 @@
  2838. #include <linux/bio.h>
  2839. #include <linux/blkdev.h>
  2840. #include <linux/slab.h>
  2841. +#include <linux/delay.h>
  2842. #include "blk.h"
  2843. @@ -109,7 +110,7 @@ static void ioc_release_fn(struct work_struct *work)
  2844. spin_unlock(q->queue_lock);
  2845. } else {
  2846. spin_unlock_irqrestore(&ioc->lock, flags);
  2847. - cpu_relax();
  2848. + cpu_chill();
  2849. spin_lock_irqsave_nested(&ioc->lock, flags, 1);
  2850. }
  2851. }
  2852. @@ -187,7 +188,7 @@ void put_io_context_active(struct io_context *ioc)
  2853. spin_unlock(icq->q->queue_lock);
  2854. } else {
  2855. spin_unlock_irqrestore(&ioc->lock, flags);
  2856. - cpu_relax();
  2857. + cpu_chill();
  2858. goto retry;
  2859. }
  2860. }
  2861. diff --git a/block/blk-mq-cpu.c b/block/blk-mq-cpu.c
  2862. index bb3ed488f7b5..628c6c13c482 100644
  2863. --- a/block/blk-mq-cpu.c
  2864. +++ b/block/blk-mq-cpu.c
  2865. @@ -16,7 +16,7 @@
  2866. #include "blk-mq.h"
  2867. static LIST_HEAD(blk_mq_cpu_notify_list);
  2868. -static DEFINE_RAW_SPINLOCK(blk_mq_cpu_notify_lock);
  2869. +static DEFINE_SPINLOCK(blk_mq_cpu_notify_lock);
  2870. static int blk_mq_main_cpu_notify(struct notifier_block *self,
  2871. unsigned long action, void *hcpu)
  2872. @@ -25,7 +25,10 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  2873. struct blk_mq_cpu_notifier *notify;
  2874. int ret = NOTIFY_OK;
  2875. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  2876. + if (action != CPU_POST_DEAD)
  2877. + return NOTIFY_OK;
  2878. +
  2879. + spin_lock(&blk_mq_cpu_notify_lock);
  2880. list_for_each_entry(notify, &blk_mq_cpu_notify_list, list) {
  2881. ret = notify->notify(notify->data, action, cpu);
  2882. @@ -33,7 +36,7 @@ static int blk_mq_main_cpu_notify(struct notifier_block *self,
  2883. break;
  2884. }
  2885. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  2886. + spin_unlock(&blk_mq_cpu_notify_lock);
  2887. return ret;
  2888. }
  2889. @@ -41,16 +44,16 @@ void blk_mq_register_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  2890. {
  2891. BUG_ON(!notifier->notify);
  2892. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  2893. + spin_lock(&blk_mq_cpu_notify_lock);
  2894. list_add_tail(&notifier->list, &blk_mq_cpu_notify_list);
  2895. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  2896. + spin_unlock(&blk_mq_cpu_notify_lock);
  2897. }
  2898. void blk_mq_unregister_cpu_notifier(struct blk_mq_cpu_notifier *notifier)
  2899. {
  2900. - raw_spin_lock(&blk_mq_cpu_notify_lock);
  2901. + spin_lock(&blk_mq_cpu_notify_lock);
  2902. list_del(&notifier->list);
  2903. - raw_spin_unlock(&blk_mq_cpu_notify_lock);
  2904. + spin_unlock(&blk_mq_cpu_notify_lock);
  2905. }
  2906. void blk_mq_init_cpu_notifier(struct blk_mq_cpu_notifier *notifier,
  2907. diff --git a/block/blk-mq.c b/block/blk-mq.c
  2908. index c207fa9870eb..ac71b0455e9f 100644
  2909. --- a/block/blk-mq.c
  2910. +++ b/block/blk-mq.c
  2911. @@ -92,7 +92,7 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
  2912. static void blk_mq_freeze_queue_wait(struct request_queue *q)
  2913. {
  2914. - wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  2915. + swait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
  2916. }
  2917. /*
  2918. @@ -130,7 +130,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)
  2919. WARN_ON_ONCE(freeze_depth < 0);
  2920. if (!freeze_depth) {
  2921. percpu_ref_reinit(&q->q_usage_counter);
  2922. - wake_up_all(&q->mq_freeze_wq);
  2923. + swake_up_all(&q->mq_freeze_wq);
  2924. }
  2925. }
  2926. EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
  2927. @@ -149,7 +149,7 @@ void blk_mq_wake_waiters(struct request_queue *q)
  2928. * dying, we need to ensure that processes currently waiting on
  2929. * the queue are notified as well.
  2930. */
  2931. - wake_up_all(&q->mq_freeze_wq);
  2932. + swake_up_all(&q->mq_freeze_wq);
  2933. }
  2934. bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
  2935. @@ -197,6 +197,9 @@ static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
  2936. rq->resid_len = 0;
  2937. rq->sense = NULL;
  2938. +#ifdef CONFIG_PREEMPT_RT_FULL
  2939. + INIT_WORK(&rq->work, __blk_mq_complete_request_remote_work);
  2940. +#endif
  2941. INIT_LIST_HEAD(&rq->timeout_list);
  2942. rq->timeout = 0;
  2943. @@ -379,6 +382,17 @@ void blk_mq_end_request(struct request *rq, int error)
  2944. }
  2945. EXPORT_SYMBOL(blk_mq_end_request);
  2946. +#ifdef CONFIG_PREEMPT_RT_FULL
  2947. +
  2948. +void __blk_mq_complete_request_remote_work(struct work_struct *work)
  2949. +{
  2950. + struct request *rq = container_of(work, struct request, work);
  2951. +
  2952. + rq->q->softirq_done_fn(rq);
  2953. +}
  2954. +
  2955. +#else
  2956. +
  2957. static void __blk_mq_complete_request_remote(void *data)
  2958. {
  2959. struct request *rq = data;
  2960. @@ -386,6 +400,8 @@ static void __blk_mq_complete_request_remote(void *data)
  2961. rq->q->softirq_done_fn(rq);
  2962. }
  2963. +#endif
  2964. +
  2965. static void blk_mq_ipi_complete_request(struct request *rq)
  2966. {
  2967. struct blk_mq_ctx *ctx = rq->mq_ctx;
  2968. @@ -397,19 +413,23 @@ static void blk_mq_ipi_complete_request(struct request *rq)
  2969. return;
  2970. }
  2971. - cpu = get_cpu();
  2972. + cpu = get_cpu_light();
  2973. if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
  2974. shared = cpus_share_cache(cpu, ctx->cpu);
  2975. if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
  2976. +#ifdef CONFIG_PREEMPT_RT_FULL
  2977. + schedule_work_on(ctx->cpu, &rq->work);
  2978. +#else
  2979. rq->csd.func = __blk_mq_complete_request_remote;
  2980. rq->csd.info = rq;
  2981. rq->csd.flags = 0;
  2982. smp_call_function_single_async(ctx->cpu, &rq->csd);
  2983. +#endif
  2984. } else {
  2985. rq->q->softirq_done_fn(rq);
  2986. }
  2987. - put_cpu();
  2988. + put_cpu_light();
  2989. }
  2990. static void __blk_mq_complete_request(struct request *rq)
  2991. @@ -938,14 +958,14 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
  2992. return;
  2993. if (!async) {
  2994. - int cpu = get_cpu();
  2995. + int cpu = get_cpu_light();
  2996. if (cpumask_test_cpu(cpu, hctx->cpumask)) {
  2997. __blk_mq_run_hw_queue(hctx);
  2998. - put_cpu();
  2999. + put_cpu_light();
  3000. return;
  3001. }
  3002. - put_cpu();
  3003. + put_cpu_light();
  3004. }
  3005. kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
  3006. @@ -1667,7 +1687,7 @@ static int blk_mq_hctx_notify(void *data, unsigned long action,
  3007. {
  3008. struct blk_mq_hw_ctx *hctx = data;
  3009. - if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  3010. + if (action == CPU_POST_DEAD)
  3011. return blk_mq_hctx_cpu_offline(hctx, cpu);
  3012. /*
  3013. diff --git a/block/blk-mq.h b/block/blk-mq.h
  3014. index 9087b11037b7..0401d76e827c 100644
  3015. --- a/block/blk-mq.h
  3016. +++ b/block/blk-mq.h
  3017. @@ -86,12 +86,12 @@ static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  3018. */
  3019. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  3020. {
  3021. - return __blk_mq_get_ctx(q, get_cpu());
  3022. + return __blk_mq_get_ctx(q, get_cpu_light());
  3023. }
  3024. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  3025. {
  3026. - put_cpu();
  3027. + put_cpu_light();
  3028. }
  3029. struct blk_mq_alloc_data {
  3030. diff --git a/block/blk-softirq.c b/block/blk-softirq.c
  3031. index 53b1737e978d..81c3c0a62edf 100644
  3032. --- a/block/blk-softirq.c
  3033. +++ b/block/blk-softirq.c
  3034. @@ -51,6 +51,7 @@ static void trigger_softirq(void *data)
  3035. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3036. local_irq_restore(flags);
  3037. + preempt_check_resched_rt();
  3038. }
  3039. /*
  3040. @@ -93,6 +94,7 @@ static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
  3041. this_cpu_ptr(&blk_cpu_done));
  3042. raise_softirq_irqoff(BLOCK_SOFTIRQ);
  3043. local_irq_enable();
  3044. + preempt_check_resched_rt();
  3045. }
  3046. return NOTIFY_OK;
  3047. @@ -150,6 +152,7 @@ void __blk_complete_request(struct request *req)
  3048. goto do_local;
  3049. local_irq_restore(flags);
  3050. + preempt_check_resched_rt();
  3051. }
  3052. /**
  3053. diff --git a/block/bounce.c b/block/bounce.c
  3054. index 1cb5dd3a5da1..2f1ec8a67cbe 100644
  3055. --- a/block/bounce.c
  3056. +++ b/block/bounce.c
  3057. @@ -55,11 +55,11 @@ static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  3058. unsigned long flags;
  3059. unsigned char *vto;
  3060. - local_irq_save(flags);
  3061. + local_irq_save_nort(flags);
  3062. vto = kmap_atomic(to->bv_page);
  3063. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  3064. kunmap_atomic(vto);
  3065. - local_irq_restore(flags);
  3066. + local_irq_restore_nort(flags);
  3067. }
  3068. #else /* CONFIG_HIGHMEM */
  3069. diff --git a/crypto/algapi.c b/crypto/algapi.c
  3070. index df939b54b09f..efe5e06adcf7 100644
  3071. --- a/crypto/algapi.c
  3072. +++ b/crypto/algapi.c
  3073. @@ -718,13 +718,13 @@ EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  3074. int crypto_register_notifier(struct notifier_block *nb)
  3075. {
  3076. - return blocking_notifier_chain_register(&crypto_chain, nb);
  3077. + return srcu_notifier_chain_register(&crypto_chain, nb);
  3078. }
  3079. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  3080. int crypto_unregister_notifier(struct notifier_block *nb)
  3081. {
  3082. - return blocking_notifier_chain_unregister(&crypto_chain, nb);
  3083. + return srcu_notifier_chain_unregister(&crypto_chain, nb);
  3084. }
  3085. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  3086. diff --git a/crypto/api.c b/crypto/api.c
  3087. index bbc147cb5dec..bc1a848f02ec 100644
  3088. --- a/crypto/api.c
  3089. +++ b/crypto/api.c
  3090. @@ -31,7 +31,7 @@ EXPORT_SYMBOL_GPL(crypto_alg_list);
  3091. DECLARE_RWSEM(crypto_alg_sem);
  3092. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  3093. -BLOCKING_NOTIFIER_HEAD(crypto_chain);
  3094. +SRCU_NOTIFIER_HEAD(crypto_chain);
  3095. EXPORT_SYMBOL_GPL(crypto_chain);
  3096. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  3097. @@ -236,10 +236,10 @@ int crypto_probing_notify(unsigned long val, void *v)
  3098. {
  3099. int ok;
  3100. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3101. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3102. if (ok == NOTIFY_DONE) {
  3103. request_module("cryptomgr");
  3104. - ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  3105. + ok = srcu_notifier_call_chain(&crypto_chain, val, v);
  3106. }
  3107. return ok;
  3108. diff --git a/crypto/internal.h b/crypto/internal.h
  3109. index 7eefcdb00227..0ecc7f5a2f40 100644
  3110. --- a/crypto/internal.h
  3111. +++ b/crypto/internal.h
  3112. @@ -47,7 +47,7 @@ struct crypto_larval {
  3113. extern struct list_head crypto_alg_list;
  3114. extern struct rw_semaphore crypto_alg_sem;
  3115. -extern struct blocking_notifier_head crypto_chain;
  3116. +extern struct srcu_notifier_head crypto_chain;
  3117. #ifdef CONFIG_PROC_FS
  3118. void __init crypto_init_proc(void);
  3119. @@ -146,7 +146,7 @@ static inline int crypto_is_moribund(struct crypto_alg *alg)
  3120. static inline void crypto_notify(unsigned long val, void *v)
  3121. {
  3122. - blocking_notifier_call_chain(&crypto_chain, val, v);
  3123. + srcu_notifier_call_chain(&crypto_chain, val, v);
  3124. }
  3125. #endif /* _CRYPTO_INTERNAL_H */
  3126. diff --git a/drivers/acpi/acpica/acglobal.h b/drivers/acpi/acpica/acglobal.h
  3127. index fded776236e2..bda523219d50 100644
  3128. --- a/drivers/acpi/acpica/acglobal.h
  3129. +++ b/drivers/acpi/acpica/acglobal.h
  3130. @@ -116,7 +116,7 @@ ACPI_GLOBAL(u8, acpi_gbl_global_lock_pending);
  3131. * interrupt level
  3132. */
  3133. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_gpe_lock); /* For GPE data structs and registers */
  3134. -ACPI_GLOBAL(acpi_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3135. +ACPI_GLOBAL(acpi_raw_spinlock, acpi_gbl_hardware_lock); /* For ACPI H/W except GPE registers */
  3136. ACPI_GLOBAL(acpi_spinlock, acpi_gbl_reference_count_lock);
  3137. /* Mutex for _OSI support */
  3138. diff --git a/drivers/acpi/acpica/hwregs.c b/drivers/acpi/acpica/hwregs.c
  3139. index 3b7fb99362b6..696bf8e62afb 100644
  3140. --- a/drivers/acpi/acpica/hwregs.c
  3141. +++ b/drivers/acpi/acpica/hwregs.c
  3142. @@ -363,14 +363,14 @@ acpi_status acpi_hw_clear_acpi_status(void)
  3143. ACPI_BITMASK_ALL_FIXED_STATUS,
  3144. ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
  3145. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3146. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3147. /* Clear the fixed events in PM1 A/B */
  3148. status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
  3149. ACPI_BITMASK_ALL_FIXED_STATUS);
  3150. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3151. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3152. if (ACPI_FAILURE(status)) {
  3153. goto exit;
  3154. diff --git a/drivers/acpi/acpica/hwxface.c b/drivers/acpi/acpica/hwxface.c
  3155. index 98c26ff39409..6e236f2ea791 100644
  3156. --- a/drivers/acpi/acpica/hwxface.c
  3157. +++ b/drivers/acpi/acpica/hwxface.c
  3158. @@ -373,7 +373,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3159. return_ACPI_STATUS(AE_BAD_PARAMETER);
  3160. }
  3161. - lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
  3162. + raw_spin_lock_irqsave(acpi_gbl_hardware_lock, lock_flags);
  3163. /*
  3164. * At this point, we know that the parent register is one of the
  3165. @@ -434,7 +434,7 @@ acpi_status acpi_write_bit_register(u32 register_id, u32 value)
  3166. unlock_and_exit:
  3167. - acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
  3168. + raw_spin_unlock_irqrestore(acpi_gbl_hardware_lock, lock_flags);
  3169. return_ACPI_STATUS(status);
  3170. }
  3171. diff --git a/drivers/acpi/acpica/utmutex.c b/drivers/acpi/acpica/utmutex.c
  3172. index 15073375bd00..357e7ca5a587 100644
  3173. --- a/drivers/acpi/acpica/utmutex.c
  3174. +++ b/drivers/acpi/acpica/utmutex.c
  3175. @@ -88,7 +88,7 @@ acpi_status acpi_ut_mutex_initialize(void)
  3176. return_ACPI_STATUS (status);
  3177. }
  3178. - status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  3179. + status = acpi_os_create_raw_lock (&acpi_gbl_hardware_lock);
  3180. if (ACPI_FAILURE (status)) {
  3181. return_ACPI_STATUS (status);
  3182. }
  3183. @@ -145,7 +145,7 @@ void acpi_ut_mutex_terminate(void)
  3184. /* Delete the spinlocks */
  3185. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  3186. - acpi_os_delete_lock(acpi_gbl_hardware_lock);
  3187. + acpi_os_delete_raw_lock(acpi_gbl_hardware_lock);
  3188. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  3189. /* Delete the reader/writer lock */
  3190. diff --git a/drivers/ata/libata-sff.c b/drivers/ata/libata-sff.c
  3191. index 051b6158d1b7..7ad293bef6ed 100644
  3192. --- a/drivers/ata/libata-sff.c
  3193. +++ b/drivers/ata/libata-sff.c
  3194. @@ -678,9 +678,9 @@ unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  3195. unsigned long flags;
  3196. unsigned int consumed;
  3197. - local_irq_save(flags);
  3198. + local_irq_save_nort(flags);
  3199. consumed = ata_sff_data_xfer32(dev, buf, buflen, rw);
  3200. - local_irq_restore(flags);
  3201. + local_irq_restore_nort(flags);
  3202. return consumed;
  3203. }
  3204. @@ -719,7 +719,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  3205. unsigned long flags;
  3206. /* FIXME: use a bounce buffer */
  3207. - local_irq_save(flags);
  3208. + local_irq_save_nort(flags);
  3209. buf = kmap_atomic(page);
  3210. /* do the actual data transfer */
  3211. @@ -727,7 +727,7 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
  3212. do_write);
  3213. kunmap_atomic(buf);
  3214. - local_irq_restore(flags);
  3215. + local_irq_restore_nort(flags);
  3216. } else {
  3217. buf = page_address(page);
  3218. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  3219. @@ -864,7 +864,7 @@ static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3220. unsigned long flags;
  3221. /* FIXME: use bounce buffer */
  3222. - local_irq_save(flags);
  3223. + local_irq_save_nort(flags);
  3224. buf = kmap_atomic(page);
  3225. /* do the actual data transfer */
  3226. @@ -872,7 +872,7 @@ static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  3227. count, rw);
  3228. kunmap_atomic(buf);
  3229. - local_irq_restore(flags);
  3230. + local_irq_restore_nort(flags);
  3231. } else {
  3232. buf = page_address(page);
  3233. consumed = ap->ops->sff_data_xfer(dev, buf + offset,
  3234. diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
  3235. index 4b5cd3a7b2b6..fa8329ad79fd 100644
  3236. --- a/drivers/block/zram/zcomp.c
  3237. +++ b/drivers/block/zram/zcomp.c
  3238. @@ -118,12 +118,19 @@ ssize_t zcomp_available_show(const char *comp, char *buf)
  3239. struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
  3240. {
  3241. - return *get_cpu_ptr(comp->stream);
  3242. + struct zcomp_strm *zstrm;
  3243. +
  3244. + zstrm = *this_cpu_ptr(comp->stream);
  3245. + spin_lock(&zstrm->zcomp_lock);
  3246. + return zstrm;
  3247. }
  3248. void zcomp_stream_put(struct zcomp *comp)
  3249. {
  3250. - put_cpu_ptr(comp->stream);
  3251. + struct zcomp_strm *zstrm;
  3252. +
  3253. + zstrm = *this_cpu_ptr(comp->stream);
  3254. + spin_unlock(&zstrm->zcomp_lock);
  3255. }
  3256. int zcomp_compress(struct zcomp_strm *zstrm,
  3257. @@ -174,6 +181,7 @@ static int __zcomp_cpu_notifier(struct zcomp *comp,
  3258. pr_err("Can't allocate a compression stream\n");
  3259. return NOTIFY_BAD;
  3260. }
  3261. + spin_lock_init(&zstrm->zcomp_lock);
  3262. *per_cpu_ptr(comp->stream, cpu) = zstrm;
  3263. break;
  3264. case CPU_DEAD:
  3265. diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
  3266. index 478cac2ed465..f7a6efdc3285 100644
  3267. --- a/drivers/block/zram/zcomp.h
  3268. +++ b/drivers/block/zram/zcomp.h
  3269. @@ -14,6 +14,7 @@ struct zcomp_strm {
  3270. /* compression/decompression buffer */
  3271. void *buffer;
  3272. struct crypto_comp *tfm;
  3273. + spinlock_t zcomp_lock;
  3274. };
  3275. /* dynamic per-device compression frontend */
  3276. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
  3277. index 04365b17ee67..b4a0577a4dbc 100644
  3278. --- a/drivers/block/zram/zram_drv.c
  3279. +++ b/drivers/block/zram/zram_drv.c
  3280. @@ -519,6 +519,8 @@ static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
  3281. goto out_error;
  3282. }
  3283. + zram_meta_init_table_locks(meta, disksize);
  3284. +
  3285. return meta;
  3286. out_error:
  3287. @@ -566,28 +568,28 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  3288. struct zram_meta *meta = zram->meta;
  3289. unsigned long handle;
  3290. unsigned int size;
  3291. + struct zcomp_strm *zstrm;
  3292. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3293. + zram_lock_table(&meta->table[index]);
  3294. handle = meta->table[index].handle;
  3295. size = zram_get_obj_size(meta, index);
  3296. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  3297. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3298. + zram_unlock_table(&meta->table[index]);
  3299. clear_page(mem);
  3300. return 0;
  3301. }
  3302. + zstrm = zcomp_stream_get(zram->comp);
  3303. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
  3304. if (size == PAGE_SIZE) {
  3305. copy_page(mem, cmem);
  3306. } else {
  3307. - struct zcomp_strm *zstrm = zcomp_stream_get(zram->comp);
  3308. -
  3309. ret = zcomp_decompress(zstrm, cmem, size, mem);
  3310. - zcomp_stream_put(zram->comp);
  3311. }
  3312. zs_unmap_object(meta->mem_pool, handle);
  3313. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3314. + zcomp_stream_put(zram->comp);
  3315. + zram_unlock_table(&meta->table[index]);
  3316. /* Should NEVER happen. Return bio error if it does. */
  3317. if (unlikely(ret)) {
  3318. @@ -607,14 +609,14 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  3319. struct zram_meta *meta = zram->meta;
  3320. page = bvec->bv_page;
  3321. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3322. + zram_lock_table(&meta->table[index]);
  3323. if (unlikely(!meta->table[index].handle) ||
  3324. zram_test_flag(meta, index, ZRAM_ZERO)) {
  3325. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3326. + zram_unlock_table(&meta->table[index]);
  3327. handle_zero_page(bvec);
  3328. return 0;
  3329. }
  3330. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3331. + zram_unlock_table(&meta->table[index]);
  3332. if (is_partial_io(bvec))
  3333. /* Use a temporary buffer to decompress the page */
  3334. @@ -691,10 +693,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  3335. if (user_mem)
  3336. kunmap_atomic(user_mem);
  3337. /* Free memory associated with this sector now. */
  3338. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3339. + zram_lock_table(&meta->table[index]);
  3340. zram_free_page(zram, index);
  3341. zram_set_flag(meta, index, ZRAM_ZERO);
  3342. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3343. + zram_unlock_table(&meta->table[index]);
  3344. atomic64_inc(&zram->stats.zero_pages);
  3345. ret = 0;
  3346. @@ -785,12 +787,12 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  3347. * Free memory associated with this sector
  3348. * before overwriting unused sectors.
  3349. */
  3350. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3351. + zram_lock_table(&meta->table[index]);
  3352. zram_free_page(zram, index);
  3353. meta->table[index].handle = handle;
  3354. zram_set_obj_size(meta, index, clen);
  3355. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3356. + zram_unlock_table(&meta->table[index]);
  3357. /* Update stats */
  3358. atomic64_add(clen, &zram->stats.compr_data_size);
  3359. @@ -833,9 +835,9 @@ static void zram_bio_discard(struct zram *zram, u32 index,
  3360. }
  3361. while (n >= PAGE_SIZE) {
  3362. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3363. + zram_lock_table(&meta->table[index]);
  3364. zram_free_page(zram, index);
  3365. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3366. + zram_unlock_table(&meta->table[index]);
  3367. atomic64_inc(&zram->stats.notify_free);
  3368. index++;
  3369. n -= PAGE_SIZE;
  3370. @@ -964,9 +966,9 @@ static void zram_slot_free_notify(struct block_device *bdev,
  3371. zram = bdev->bd_disk->private_data;
  3372. meta = zram->meta;
  3373. - bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  3374. + zram_lock_table(&meta->table[index]);
  3375. zram_free_page(zram, index);
  3376. - bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  3377. + zram_unlock_table(&meta->table[index]);
  3378. atomic64_inc(&zram->stats.notify_free);
  3379. }
  3380. diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
  3381. index 74fcf10da374..fd4020c99b9e 100644
  3382. --- a/drivers/block/zram/zram_drv.h
  3383. +++ b/drivers/block/zram/zram_drv.h
  3384. @@ -73,6 +73,9 @@ enum zram_pageflags {
  3385. struct zram_table_entry {
  3386. unsigned long handle;
  3387. unsigned long value;
  3388. +#ifdef CONFIG_PREEMPT_RT_BASE
  3389. + spinlock_t lock;
  3390. +#endif
  3391. };
  3392. struct zram_stats {
  3393. @@ -120,4 +123,42 @@ struct zram {
  3394. */
  3395. bool claim; /* Protected by bdev->bd_mutex */
  3396. };
  3397. +
  3398. +#ifndef CONFIG_PREEMPT_RT_BASE
  3399. +static inline void zram_lock_table(struct zram_table_entry *table)
  3400. +{
  3401. + bit_spin_lock(ZRAM_ACCESS, &table->value);
  3402. +}
  3403. +
  3404. +static inline void zram_unlock_table(struct zram_table_entry *table)
  3405. +{
  3406. + bit_spin_unlock(ZRAM_ACCESS, &table->value);
  3407. +}
  3408. +
  3409. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize) { }
  3410. +#else /* CONFIG_PREEMPT_RT_BASE */
  3411. +static inline void zram_lock_table(struct zram_table_entry *table)
  3412. +{
  3413. + spin_lock(&table->lock);
  3414. + __set_bit(ZRAM_ACCESS, &table->value);
  3415. +}
  3416. +
  3417. +static inline void zram_unlock_table(struct zram_table_entry *table)
  3418. +{
  3419. + __clear_bit(ZRAM_ACCESS, &table->value);
  3420. + spin_unlock(&table->lock);
  3421. +}
  3422. +
  3423. +static inline void zram_meta_init_table_locks(struct zram_meta *meta, u64 disksize)
  3424. +{
  3425. + size_t num_pages = disksize >> PAGE_SHIFT;
  3426. + size_t index;
  3427. +
  3428. + for (index = 0; index < num_pages; index++) {
  3429. + spinlock_t *lock = &meta->table[index].lock;
  3430. + spin_lock_init(lock);
  3431. + }
  3432. +}
  3433. +#endif /* CONFIG_PREEMPT_RT_BASE */
  3434. +
  3435. #endif
  3436. diff --git a/drivers/char/random.c b/drivers/char/random.c
  3437. index 3efb3bf0ab83..c894d2e266f3 100644
  3438. --- a/drivers/char/random.c
  3439. +++ b/drivers/char/random.c
  3440. @@ -1028,8 +1028,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  3441. } sample;
  3442. long delta, delta2, delta3;
  3443. - preempt_disable();
  3444. -
  3445. sample.jiffies = jiffies;
  3446. sample.cycles = random_get_entropy();
  3447. sample.num = num;
  3448. @@ -1070,7 +1068,6 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
  3449. */
  3450. credit_entropy_bits(r, min_t(int, fls(delta>>1), 11));
  3451. }
  3452. - preempt_enable();
  3453. }
  3454. void add_input_randomness(unsigned int type, unsigned int code,
  3455. @@ -1123,28 +1120,27 @@ static __u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
  3456. return *(ptr + f->reg_idx++);
  3457. }
  3458. -void add_interrupt_randomness(int irq, int irq_flags)
  3459. +void add_interrupt_randomness(int irq, int irq_flags, __u64 ip)
  3460. {
  3461. struct entropy_store *r;
  3462. struct fast_pool *fast_pool = this_cpu_ptr(&irq_randomness);
  3463. - struct pt_regs *regs = get_irq_regs();
  3464. unsigned long now = jiffies;
  3465. cycles_t cycles = random_get_entropy();
  3466. __u32 c_high, j_high;
  3467. - __u64 ip;
  3468. unsigned long seed;
  3469. int credit = 0;
  3470. if (cycles == 0)
  3471. - cycles = get_reg(fast_pool, regs);
  3472. + cycles = get_reg(fast_pool, NULL);
  3473. c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
  3474. j_high = (sizeof(now) > 4) ? now >> 32 : 0;
  3475. fast_pool->pool[0] ^= cycles ^ j_high ^ irq;
  3476. fast_pool->pool[1] ^= now ^ c_high;
  3477. - ip = regs ? instruction_pointer(regs) : _RET_IP_;
  3478. + if (!ip)
  3479. + ip = _RET_IP_;
  3480. fast_pool->pool[2] ^= ip;
  3481. fast_pool->pool[3] ^= (sizeof(ip) > 4) ? ip >> 32 :
  3482. - get_reg(fast_pool, regs);
  3483. + get_reg(fast_pool, NULL);
  3484. fast_mix(fast_pool);
  3485. add_interrupt_bench(cycles);
  3486. diff --git a/drivers/clocksource/tcb_clksrc.c b/drivers/clocksource/tcb_clksrc.c
  3487. index 4da2af9694a2..5b6f57f500b8 100644
  3488. --- a/drivers/clocksource/tcb_clksrc.c
  3489. +++ b/drivers/clocksource/tcb_clksrc.c
  3490. @@ -23,8 +23,7 @@
  3491. * this 32 bit free-running counter. the second channel is not used.
  3492. *
  3493. * - The third channel may be used to provide a 16-bit clockevent
  3494. - * source, used in either periodic or oneshot mode. This runs
  3495. - * at 32 KiHZ, and can handle delays of up to two seconds.
  3496. + * source, used in either periodic or oneshot mode.
  3497. *
  3498. * A boot clocksource and clockevent source are also currently needed,
  3499. * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
  3500. @@ -74,6 +73,8 @@ static struct clocksource clksrc = {
  3501. struct tc_clkevt_device {
  3502. struct clock_event_device clkevt;
  3503. struct clk *clk;
  3504. + bool clk_enabled;
  3505. + u32 freq;
  3506. void __iomem *regs;
  3507. };
  3508. @@ -82,15 +83,26 @@ static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
  3509. return container_of(clkevt, struct tc_clkevt_device, clkevt);
  3510. }
  3511. -/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
  3512. - * because using one of the divided clocks would usually mean the
  3513. - * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
  3514. - *
  3515. - * A divided clock could be good for high resolution timers, since
  3516. - * 30.5 usec resolution can seem "low".
  3517. - */
  3518. static u32 timer_clock;
  3519. +static void tc_clk_disable(struct clock_event_device *d)
  3520. +{
  3521. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3522. +
  3523. + clk_disable(tcd->clk);
  3524. + tcd->clk_enabled = false;
  3525. +}
  3526. +
  3527. +static void tc_clk_enable(struct clock_event_device *d)
  3528. +{
  3529. + struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3530. +
  3531. + if (tcd->clk_enabled)
  3532. + return;
  3533. + clk_enable(tcd->clk);
  3534. + tcd->clk_enabled = true;
  3535. +}
  3536. +
  3537. static int tc_shutdown(struct clock_event_device *d)
  3538. {
  3539. struct tc_clkevt_device *tcd = to_tc_clkevt(d);
  3540. @@ -98,8 +110,14 @@ static int tc_shutdown(struct clock_event_device *d)
  3541. __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
  3542. __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
  3543. + return 0;
  3544. +}
  3545. +
  3546. +static int tc_shutdown_clk_off(struct clock_event_device *d)
  3547. +{
  3548. + tc_shutdown(d);
  3549. if (!clockevent_state_detached(d))
  3550. - clk_disable(tcd->clk);
  3551. + tc_clk_disable(d);
  3552. return 0;
  3553. }
  3554. @@ -112,9 +130,9 @@ static int tc_set_oneshot(struct clock_event_device *d)
  3555. if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
  3556. tc_shutdown(d);
  3557. - clk_enable(tcd->clk);
  3558. + tc_clk_enable(d);
  3559. - /* slow clock, count up to RC, then irq and stop */
  3560. + /* count up to RC, then irq and stop */
  3561. __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
  3562. ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
  3563. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  3564. @@ -134,12 +152,12 @@ static int tc_set_periodic(struct clock_event_device *d)
  3565. /* By not making the gentime core emulate periodic mode on top
  3566. * of oneshot, we get lower overhead and improved accuracy.
  3567. */
  3568. - clk_enable(tcd->clk);
  3569. + tc_clk_enable(d);
  3570. - /* slow clock, count up to RC, then irq and restart */
  3571. + /* count up to RC, then irq and restart */
  3572. __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
  3573. regs + ATMEL_TC_REG(2, CMR));
  3574. - __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  3575. + __raw_writel((tcd->freq + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
  3576. /* Enable clock and interrupts on RC compare */
  3577. __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
  3578. @@ -166,9 +184,13 @@ static struct tc_clkevt_device clkevt = {
  3579. .features = CLOCK_EVT_FEAT_PERIODIC |
  3580. CLOCK_EVT_FEAT_ONESHOT,
  3581. /* Should be lower than at91rm9200's system timer */
  3582. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  3583. .rating = 125,
  3584. +#else
  3585. + .rating = 200,
  3586. +#endif
  3587. .set_next_event = tc_next_event,
  3588. - .set_state_shutdown = tc_shutdown,
  3589. + .set_state_shutdown = tc_shutdown_clk_off,
  3590. .set_state_periodic = tc_set_periodic,
  3591. .set_state_oneshot = tc_set_oneshot,
  3592. },
  3593. @@ -188,8 +210,9 @@ static irqreturn_t ch2_irq(int irq, void *handle)
  3594. return IRQ_NONE;
  3595. }
  3596. -static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  3597. +static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
  3598. {
  3599. + unsigned divisor = atmel_tc_divisors[divisor_idx];
  3600. int ret;
  3601. struct clk *t2_clk = tc->clk[2];
  3602. int irq = tc->irq[2];
  3603. @@ -210,7 +233,11 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  3604. clkevt.regs = tc->regs;
  3605. clkevt.clk = t2_clk;
  3606. - timer_clock = clk32k_divisor_idx;
  3607. + timer_clock = divisor_idx;
  3608. + if (!divisor)
  3609. + clkevt.freq = 32768;
  3610. + else
  3611. + clkevt.freq = clk_get_rate(t2_clk) / divisor;
  3612. clkevt.clkevt.cpumask = cpumask_of(0);
  3613. @@ -221,7 +248,7 @@ static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
  3614. return ret;
  3615. }
  3616. - clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
  3617. + clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
  3618. return ret;
  3619. }
  3620. @@ -358,7 +385,11 @@ static int __init tcb_clksrc_init(void)
  3621. goto err_disable_t1;
  3622. /* channel 2: periodic and oneshot timer support */
  3623. +#ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  3624. ret = setup_clkevents(tc, clk32k_divisor_idx);
  3625. +#else
  3626. + ret = setup_clkevents(tc, best_divisor_idx);
  3627. +#endif
  3628. if (ret)
  3629. goto err_unregister_clksrc;
  3630. diff --git a/drivers/clocksource/timer-atmel-pit.c b/drivers/clocksource/timer-atmel-pit.c
  3631. index 7f0f5b26d8c5..1553f19e73e7 100644
  3632. --- a/drivers/clocksource/timer-atmel-pit.c
  3633. +++ b/drivers/clocksource/timer-atmel-pit.c
  3634. @@ -46,6 +46,7 @@ struct pit_data {
  3635. u32 cycle;
  3636. u32 cnt;
  3637. unsigned int irq;
  3638. + bool irq_requested;
  3639. struct clk *mck;
  3640. };
  3641. @@ -96,15 +97,29 @@ static int pit_clkevt_shutdown(struct clock_event_device *dev)
  3642. /* disable irq, leaving the clocksource active */
  3643. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  3644. + if (data->irq_requested) {
  3645. + free_irq(data->irq, data);
  3646. + data->irq_requested = false;
  3647. + }
  3648. return 0;
  3649. }
  3650. +static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id);
  3651. /*
  3652. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  3653. */
  3654. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  3655. {
  3656. struct pit_data *data = clkevt_to_pit_data(dev);
  3657. + int ret;
  3658. +
  3659. + ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  3660. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3661. + "at91_tick", data);
  3662. + if (ret)
  3663. + panic(pr_fmt("Unable to setup IRQ\n"));
  3664. +
  3665. + data->irq_requested = true;
  3666. /* update clocksource counter */
  3667. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  3668. @@ -211,15 +226,6 @@ static int __init at91sam926x_pit_common_init(struct pit_data *data)
  3669. return ret;
  3670. }
  3671. - /* Set up irq handler */
  3672. - ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  3673. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3674. - "at91_tick", data);
  3675. - if (ret) {
  3676. - pr_err("Unable to setup IRQ\n");
  3677. - return ret;
  3678. - }
  3679. -
  3680. /* Set up and register clockevents */
  3681. data->clkevt.name = "pit";
  3682. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  3683. diff --git a/drivers/clocksource/timer-atmel-st.c b/drivers/clocksource/timer-atmel-st.c
  3684. index e90ab5b63a90..9e124087c55f 100644
  3685. --- a/drivers/clocksource/timer-atmel-st.c
  3686. +++ b/drivers/clocksource/timer-atmel-st.c
  3687. @@ -115,18 +115,29 @@ static void clkdev32k_disable_and_flush_irq(void)
  3688. last_crtr = read_CRTR();
  3689. }
  3690. +static int atmel_st_irq;
  3691. +
  3692. static int clkevt32k_shutdown(struct clock_event_device *evt)
  3693. {
  3694. clkdev32k_disable_and_flush_irq();
  3695. irqmask = 0;
  3696. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  3697. + free_irq(atmel_st_irq, regmap_st);
  3698. return 0;
  3699. }
  3700. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  3701. {
  3702. + int ret;
  3703. +
  3704. clkdev32k_disable_and_flush_irq();
  3705. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  3706. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3707. + "at91_tick", regmap_st);
  3708. + if (ret)
  3709. + panic(pr_fmt("Unable to setup IRQ\n"));
  3710. +
  3711. /*
  3712. * ALM for oneshot irqs, set by next_event()
  3713. * before 32 seconds have passed.
  3714. @@ -139,8 +150,16 @@ static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  3715. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  3716. {
  3717. + int ret;
  3718. +
  3719. clkdev32k_disable_and_flush_irq();
  3720. + ret = request_irq(atmel_st_irq, at91rm9200_timer_interrupt,
  3721. + IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3722. + "at91_tick", regmap_st);
  3723. + if (ret)
  3724. + panic(pr_fmt("Unable to setup IRQ\n"));
  3725. +
  3726. /* PIT for periodic irqs; fixed rate of 1/HZ */
  3727. irqmask = AT91_ST_PITS;
  3728. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  3729. @@ -198,7 +217,7 @@ static int __init atmel_st_timer_init(struct device_node *node)
  3730. {
  3731. struct clk *sclk;
  3732. unsigned int sclk_rate, val;
  3733. - int irq, ret;
  3734. + int ret;
  3735. regmap_st = syscon_node_to_regmap(node);
  3736. if (IS_ERR(regmap_st)) {
  3737. @@ -212,21 +231,12 @@ static int __init atmel_st_timer_init(struct device_node *node)
  3738. regmap_read(regmap_st, AT91_ST_SR, &val);
  3739. /* Get the interrupts property */
  3740. - irq = irq_of_parse_and_map(node, 0);
  3741. - if (!irq) {
  3742. + atmel_st_irq = irq_of_parse_and_map(node, 0);
  3743. + if (!atmel_st_irq) {
  3744. pr_err("Unable to get IRQ from DT\n");
  3745. return -EINVAL;
  3746. }
  3747. - /* Make IRQs happen for the system timer */
  3748. - ret = request_irq(irq, at91rm9200_timer_interrupt,
  3749. - IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  3750. - "at91_tick", regmap_st);
  3751. - if (ret) {
  3752. - pr_err("Unable to setup IRQ\n");
  3753. - return ret;
  3754. - }
  3755. -
  3756. sclk = of_clk_get(node, 0);
  3757. if (IS_ERR(sclk)) {
  3758. pr_err("Unable to get slow clock\n");
  3759. diff --git a/drivers/connector/cn_proc.c b/drivers/connector/cn_proc.c
  3760. index a782ce87715c..19d265948526 100644
  3761. --- a/drivers/connector/cn_proc.c
  3762. +++ b/drivers/connector/cn_proc.c
  3763. @@ -32,6 +32,7 @@
  3764. #include <linux/pid_namespace.h>
  3765. #include <linux/cn_proc.h>
  3766. +#include <linux/locallock.h>
  3767. /*
  3768. * Size of a cn_msg followed by a proc_event structure. Since the
  3769. @@ -54,10 +55,11 @@ static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
  3770. /* proc_event_counts is used as the sequence number of the netlink message */
  3771. static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  3772. +static DEFINE_LOCAL_IRQ_LOCK(send_msg_lock);
  3773. static inline void send_msg(struct cn_msg *msg)
  3774. {
  3775. - preempt_disable();
  3776. + local_lock(send_msg_lock);
  3777. msg->seq = __this_cpu_inc_return(proc_event_counts) - 1;
  3778. ((struct proc_event *)msg->data)->cpu = smp_processor_id();
  3779. @@ -70,7 +72,7 @@ static inline void send_msg(struct cn_msg *msg)
  3780. */
  3781. cn_netlink_send(msg, 0, CN_IDX_PROC, GFP_NOWAIT);
  3782. - preempt_enable();
  3783. + local_unlock(send_msg_lock);
  3784. }
  3785. void proc_fork_connector(struct task_struct *task)
  3786. diff --git a/drivers/cpufreq/Kconfig.x86 b/drivers/cpufreq/Kconfig.x86
  3787. index adbd1de1cea5..1fac5074f2cf 100644
  3788. --- a/drivers/cpufreq/Kconfig.x86
  3789. +++ b/drivers/cpufreq/Kconfig.x86
  3790. @@ -124,7 +124,7 @@ config X86_POWERNOW_K7_ACPI
  3791. config X86_POWERNOW_K8
  3792. tristate "AMD Opteron/Athlon64 PowerNow!"
  3793. - depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ
  3794. + depends on ACPI && ACPI_PROCESSOR && X86_ACPI_CPUFREQ && !PREEMPT_RT_BASE
  3795. help
  3796. This adds the CPUFreq driver for K8/early Opteron/Athlon64 processors.
  3797. Support for K10 and newer processors is now in acpi-cpufreq.
  3798. diff --git a/drivers/gpu/drm/i915/i915_gem_execbuffer.c b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  3799. index b35e5b6475b2..ce60807fb1d4 100644
  3800. --- a/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  3801. +++ b/drivers/gpu/drm/i915/i915_gem_execbuffer.c
  3802. @@ -1302,7 +1302,9 @@ i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  3803. if (ret)
  3804. return ret;
  3805. +#ifndef CONFIG_PREEMPT_RT_BASE
  3806. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  3807. +#endif
  3808. i915_gem_execbuffer_move_to_active(vmas, params->request);
  3809. diff --git a/drivers/gpu/drm/i915/i915_gem_shrinker.c b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  3810. index 6f10b421487b..dd3a9a6ace11 100644
  3811. --- a/drivers/gpu/drm/i915/i915_gem_shrinker.c
  3812. +++ b/drivers/gpu/drm/i915/i915_gem_shrinker.c
  3813. @@ -40,7 +40,7 @@ static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3814. if (!mutex_is_locked(mutex))
  3815. return false;
  3816. -#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
  3817. +#if (defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)) && !defined(CONFIG_PREEMPT_RT_BASE)
  3818. return mutex->owner == task;
  3819. #else
  3820. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3821. diff --git a/drivers/gpu/drm/i915/i915_irq.c b/drivers/gpu/drm/i915/i915_irq.c
  3822. index 1c2aec392412..1d85c0c791f1 100644
  3823. --- a/drivers/gpu/drm/i915/i915_irq.c
  3824. +++ b/drivers/gpu/drm/i915/i915_irq.c
  3825. @@ -812,6 +812,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  3826. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  3827. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  3828. + preempt_disable_rt();
  3829. /* Get optional system timestamp before query. */
  3830. if (stime)
  3831. @@ -863,6 +864,7 @@ static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  3832. *etime = ktime_get();
  3833. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  3834. + preempt_enable_rt();
  3835. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  3836. diff --git a/drivers/gpu/drm/i915/intel_display.c b/drivers/gpu/drm/i915/intel_display.c
  3837. index e9a64fba6333..2aac27b13d86 100644
  3838. --- a/drivers/gpu/drm/i915/intel_display.c
  3839. +++ b/drivers/gpu/drm/i915/intel_display.c
  3840. @@ -11647,7 +11647,7 @@ void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  3841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3842. struct intel_flip_work *work;
  3843. - WARN_ON(!in_interrupt());
  3844. + WARN_ON_NONRT(!in_interrupt());
  3845. if (crtc == NULL)
  3846. return;
  3847. diff --git a/drivers/gpu/drm/i915/intel_sprite.c b/drivers/gpu/drm/i915/intel_sprite.c
  3848. index 4178849631ad..0eb939c92544 100644
  3849. --- a/drivers/gpu/drm/i915/intel_sprite.c
  3850. +++ b/drivers/gpu/drm/i915/intel_sprite.c
  3851. @@ -38,6 +38,7 @@
  3852. #include "intel_drv.h"
  3853. #include <drm/i915_drm.h>
  3854. #include "i915_drv.h"
  3855. +#include <linux/locallock.h>
  3856. static bool
  3857. format_is_yuv(uint32_t format)
  3858. @@ -64,6 +65,8 @@ int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
  3859. 1000 * adjusted_mode->crtc_htotal);
  3860. }
  3861. +static DEFINE_LOCAL_IRQ_LOCK(pipe_update_lock);
  3862. +
  3863. /**
  3864. * intel_pipe_update_start() - start update of a set of display registers
  3865. * @crtc: the crtc of which the registers are going to be updated
  3866. @@ -94,7 +97,7 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  3867. min = vblank_start - intel_usecs_to_scanlines(adjusted_mode, 100);
  3868. max = vblank_start - 1;
  3869. - local_irq_disable();
  3870. + local_lock_irq(pipe_update_lock);
  3871. if (min <= 0 || max <= 0)
  3872. return;
  3873. @@ -124,11 +127,11 @@ void intel_pipe_update_start(struct intel_crtc *crtc)
  3874. break;
  3875. }
  3876. - local_irq_enable();
  3877. + local_unlock_irq(pipe_update_lock);
  3878. timeout = schedule_timeout(timeout);
  3879. - local_irq_disable();
  3880. + local_lock_irq(pipe_update_lock);
  3881. }
  3882. finish_wait(wq, &wait);
  3883. @@ -180,7 +183,7 @@ void intel_pipe_update_end(struct intel_crtc *crtc, struct intel_flip_work *work
  3884. crtc->base.state->event = NULL;
  3885. }
  3886. - local_irq_enable();
  3887. + local_unlock_irq(pipe_update_lock);
  3888. if (crtc->debug.start_vbl_count &&
  3889. crtc->debug.start_vbl_count != end_vbl_count) {
  3890. diff --git a/drivers/gpu/drm/msm/msm_gem_shrinker.c b/drivers/gpu/drm/msm/msm_gem_shrinker.c
  3891. index 283d2841ba58..d01f6ed1977e 100644
  3892. --- a/drivers/gpu/drm/msm/msm_gem_shrinker.c
  3893. +++ b/drivers/gpu/drm/msm/msm_gem_shrinker.c
  3894. @@ -23,7 +23,7 @@ static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3895. if (!mutex_is_locked(mutex))
  3896. return false;
  3897. -#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3898. +#if (defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)) && !defined(CONFIG_PREEMPT_RT_BASE)
  3899. return mutex->owner == task;
  3900. #else
  3901. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3902. diff --git a/drivers/gpu/drm/radeon/radeon_display.c b/drivers/gpu/drm/radeon/radeon_display.c
  3903. index c3206fb8f4cf..6e2423186e2a 100644
  3904. --- a/drivers/gpu/drm/radeon/radeon_display.c
  3905. +++ b/drivers/gpu/drm/radeon/radeon_display.c
  3906. @@ -1869,6 +1869,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  3907. struct radeon_device *rdev = dev->dev_private;
  3908. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  3909. + preempt_disable_rt();
  3910. /* Get optional system timestamp before query. */
  3911. if (stime)
  3912. @@ -1961,6 +1962,7 @@ int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  3913. *etime = ktime_get();
  3914. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  3915. + preempt_enable_rt();
  3916. /* Decode into vertical and horizontal scanout position. */
  3917. *vpos = position & 0x1fff;
  3918. diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
  3919. index e82f7e1c217c..b57d917b6ab7 100644
  3920. --- a/drivers/hv/vmbus_drv.c
  3921. +++ b/drivers/hv/vmbus_drv.c
  3922. @@ -761,6 +761,8 @@ static void vmbus_isr(void)
  3923. void *page_addr;
  3924. struct hv_message *msg;
  3925. union hv_synic_event_flags *event;
  3926. + struct pt_regs *regs = get_irq_regs();
  3927. + u64 ip = regs ? instruction_pointer(regs) : 0;
  3928. bool handled = false;
  3929. page_addr = hv_context.synic_event_page[cpu];
  3930. @@ -808,7 +810,7 @@ static void vmbus_isr(void)
  3931. tasklet_schedule(hv_context.msg_dpc[cpu]);
  3932. }
  3933. - add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  3934. + add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0, ip);
  3935. }
  3936. diff --git a/drivers/ide/alim15x3.c b/drivers/ide/alim15x3.c
  3937. index 36f76e28a0bf..394f142f90c7 100644
  3938. --- a/drivers/ide/alim15x3.c
  3939. +++ b/drivers/ide/alim15x3.c
  3940. @@ -234,7 +234,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  3941. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  3942. - local_irq_save(flags);
  3943. + local_irq_save_nort(flags);
  3944. if (m5229_revision < 0xC2) {
  3945. /*
  3946. @@ -325,7 +325,7 @@ static int init_chipset_ali15x3(struct pci_dev *dev)
  3947. }
  3948. pci_dev_put(north);
  3949. pci_dev_put(isa_dev);
  3950. - local_irq_restore(flags);
  3951. + local_irq_restore_nort(flags);
  3952. return 0;
  3953. }
  3954. diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
  3955. index 0ceae5cbd89a..c212e85d7f3e 100644
  3956. --- a/drivers/ide/hpt366.c
  3957. +++ b/drivers/ide/hpt366.c
  3958. @@ -1236,7 +1236,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  3959. dma_old = inb(base + 2);
  3960. - local_irq_save(flags);
  3961. + local_irq_save_nort(flags);
  3962. dma_new = dma_old;
  3963. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  3964. @@ -1247,7 +1247,7 @@ static int init_dma_hpt366(ide_hwif_t *hwif,
  3965. if (dma_new != dma_old)
  3966. outb(dma_new, base + 2);
  3967. - local_irq_restore(flags);
  3968. + local_irq_restore_nort(flags);
  3969. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  3970. hwif->name, base, base + 7);
  3971. diff --git a/drivers/ide/ide-io-std.c b/drivers/ide/ide-io-std.c
  3972. index 19763977568c..4169433faab5 100644
  3973. --- a/drivers/ide/ide-io-std.c
  3974. +++ b/drivers/ide/ide-io-std.c
  3975. @@ -175,7 +175,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  3976. unsigned long uninitialized_var(flags);
  3977. if ((io_32bit & 2) && !mmio) {
  3978. - local_irq_save(flags);
  3979. + local_irq_save_nort(flags);
  3980. ata_vlb_sync(io_ports->nsect_addr);
  3981. }
  3982. @@ -186,7 +186,7 @@ void ide_input_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  3983. insl(data_addr, buf, words);
  3984. if ((io_32bit & 2) && !mmio)
  3985. - local_irq_restore(flags);
  3986. + local_irq_restore_nort(flags);
  3987. if (((len + 1) & 3) < 2)
  3988. return;
  3989. @@ -219,7 +219,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  3990. unsigned long uninitialized_var(flags);
  3991. if ((io_32bit & 2) && !mmio) {
  3992. - local_irq_save(flags);
  3993. + local_irq_save_nort(flags);
  3994. ata_vlb_sync(io_ports->nsect_addr);
  3995. }
  3996. @@ -230,7 +230,7 @@ void ide_output_data(ide_drive_t *drive, struct ide_cmd *cmd, void *buf,
  3997. outsl(data_addr, buf, words);
  3998. if ((io_32bit & 2) && !mmio)
  3999. - local_irq_restore(flags);
  4000. + local_irq_restore_nort(flags);
  4001. if (((len + 1) & 3) < 2)
  4002. return;
  4003. diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
  4004. index 669ea1e45795..e12e43e62245 100644
  4005. --- a/drivers/ide/ide-io.c
  4006. +++ b/drivers/ide/ide-io.c
  4007. @@ -659,7 +659,7 @@ void ide_timer_expiry (unsigned long data)
  4008. /* disable_irq_nosync ?? */
  4009. disable_irq(hwif->irq);
  4010. /* local CPU only, as if we were handling an interrupt */
  4011. - local_irq_disable();
  4012. + local_irq_disable_nort();
  4013. if (hwif->polling) {
  4014. startstop = handler(drive);
  4015. } else if (drive_is_ready(drive)) {
  4016. diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
  4017. index 376f2dc410c5..f014dd1b73dc 100644
  4018. --- a/drivers/ide/ide-iops.c
  4019. +++ b/drivers/ide/ide-iops.c
  4020. @@ -129,12 +129,12 @@ int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
  4021. if ((stat & ATA_BUSY) == 0)
  4022. break;
  4023. - local_irq_restore(flags);
  4024. + local_irq_restore_nort(flags);
  4025. *rstat = stat;
  4026. return -EBUSY;
  4027. }
  4028. }
  4029. - local_irq_restore(flags);
  4030. + local_irq_restore_nort(flags);
  4031. }
  4032. /*
  4033. * Allow status to settle, then read it again.
  4034. diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
  4035. index 0b63facd1d87..4ceba37afc0c 100644
  4036. --- a/drivers/ide/ide-probe.c
  4037. +++ b/drivers/ide/ide-probe.c
  4038. @@ -196,10 +196,10 @@ static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  4039. int bswap = 1;
  4040. /* local CPU only; some systems need this */
  4041. - local_irq_save(flags);
  4042. + local_irq_save_nort(flags);
  4043. /* read 512 bytes of id info */
  4044. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  4045. - local_irq_restore(flags);
  4046. + local_irq_restore_nort(flags);
  4047. drive->dev_flags |= IDE_DFLAG_ID_READ;
  4048. #ifdef DEBUG
  4049. diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
  4050. index a716693417a3..be0568c722d6 100644
  4051. --- a/drivers/ide/ide-taskfile.c
  4052. +++ b/drivers/ide/ide-taskfile.c
  4053. @@ -250,7 +250,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  4054. page_is_high = PageHighMem(page);
  4055. if (page_is_high)
  4056. - local_irq_save(flags);
  4057. + local_irq_save_nort(flags);
  4058. buf = kmap_atomic(page) + offset;
  4059. @@ -271,7 +271,7 @@ void ide_pio_bytes(ide_drive_t *drive, struct ide_cmd *cmd,
  4060. kunmap_atomic(buf);
  4061. if (page_is_high)
  4062. - local_irq_restore(flags);
  4063. + local_irq_restore_nort(flags);
  4064. len -= nr_bytes;
  4065. }
  4066. @@ -414,7 +414,7 @@ static ide_startstop_t pre_task_out_intr(ide_drive_t *drive,
  4067. }
  4068. if ((drive->dev_flags & IDE_DFLAG_UNMASK) == 0)
  4069. - local_irq_disable();
  4070. + local_irq_disable_nort();
  4071. ide_set_handler(drive, &task_pio_intr, WAIT_WORSTCASE);
  4072. diff --git a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  4073. index d3394b6add24..506bfba6ec9f 100644
  4074. --- a/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  4075. +++ b/drivers/infiniband/ulp/ipoib/ipoib_multicast.c
  4076. @@ -897,7 +897,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  4077. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  4078. - local_irq_save(flags);
  4079. + local_irq_save_nort(flags);
  4080. netif_addr_lock(dev);
  4081. spin_lock(&priv->lock);
  4082. @@ -979,7 +979,7 @@ void ipoib_mcast_restart_task(struct work_struct *work)
  4083. spin_unlock(&priv->lock);
  4084. netif_addr_unlock(dev);
  4085. - local_irq_restore(flags);
  4086. + local_irq_restore_nort(flags);
  4087. /*
  4088. * make sure the in-flight joins have finished before we attempt
  4089. diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c
  4090. index 4a2a9e370be7..e970d9afd179 100644
  4091. --- a/drivers/input/gameport/gameport.c
  4092. +++ b/drivers/input/gameport/gameport.c
  4093. @@ -91,13 +91,13 @@ static int gameport_measure_speed(struct gameport *gameport)
  4094. tx = ~0;
  4095. for (i = 0; i < 50; i++) {
  4096. - local_irq_save(flags);
  4097. + local_irq_save_nort(flags);
  4098. t1 = ktime_get_ns();
  4099. for (t = 0; t < 50; t++)
  4100. gameport_read(gameport);
  4101. t2 = ktime_get_ns();
  4102. t3 = ktime_get_ns();
  4103. - local_irq_restore(flags);
  4104. + local_irq_restore_nort(flags);
  4105. udelay(i * 10);
  4106. t = (t2 - t1) - (t3 - t2);
  4107. if (t < tx)
  4108. @@ -124,12 +124,12 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  4109. tx = 1 << 30;
  4110. for(i = 0; i < 50; i++) {
  4111. - local_irq_save(flags);
  4112. + local_irq_save_nort(flags);
  4113. GET_TIME(t1);
  4114. for (t = 0; t < 50; t++) gameport_read(gameport);
  4115. GET_TIME(t2);
  4116. GET_TIME(t3);
  4117. - local_irq_restore(flags);
  4118. + local_irq_restore_nort(flags);
  4119. udelay(i * 10);
  4120. if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
  4121. }
  4122. @@ -148,11 +148,11 @@ static int old_gameport_measure_speed(struct gameport *gameport)
  4123. tx = 1 << 30;
  4124. for(i = 0; i < 50; i++) {
  4125. - local_irq_save(flags);
  4126. + local_irq_save_nort(flags);
  4127. t1 = rdtsc();
  4128. for (t = 0; t < 50; t++) gameport_read(gameport);
  4129. t2 = rdtsc();
  4130. - local_irq_restore(flags);
  4131. + local_irq_restore_nort(flags);
  4132. udelay(i * 10);
  4133. if (t2 - t1 < tx) tx = t2 - t1;
  4134. }
  4135. diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
  4136. index 96de97a46079..a6ec875d941b 100644
  4137. --- a/drivers/iommu/amd_iommu.c
  4138. +++ b/drivers/iommu/amd_iommu.c
  4139. @@ -1832,10 +1832,10 @@ static int __attach_device(struct iommu_dev_data *dev_data,
  4140. int ret;
  4141. /*
  4142. - * Must be called with IRQs disabled. Warn here to detect early
  4143. - * when its not.
  4144. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  4145. + * detect early when its not.
  4146. */
  4147. - WARN_ON(!irqs_disabled());
  4148. + WARN_ON_NONRT(!irqs_disabled());
  4149. /* lock domain */
  4150. spin_lock(&domain->lock);
  4151. @@ -2003,10 +2003,10 @@ static void __detach_device(struct iommu_dev_data *dev_data)
  4152. struct protection_domain *domain;
  4153. /*
  4154. - * Must be called with IRQs disabled. Warn here to detect early
  4155. - * when its not.
  4156. + * Must be called with IRQs disabled on a non RT kernel. Warn here to
  4157. + * detect early when its not.
  4158. */
  4159. - WARN_ON(!irqs_disabled());
  4160. + WARN_ON_NONRT(!irqs_disabled());
  4161. if (WARN_ON(!dev_data->domain))
  4162. return;
  4163. diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
  4164. index ebb5bf3ddbd9..598f5df45f6b 100644
  4165. --- a/drivers/iommu/intel-iommu.c
  4166. +++ b/drivers/iommu/intel-iommu.c
  4167. @@ -479,7 +479,7 @@ struct deferred_flush_data {
  4168. struct deferred_flush_table *tables;
  4169. };
  4170. -DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush);
  4171. +static DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush);
  4172. /* bitmap for indexing intel_iommus */
  4173. static int g_num_of_iommus;
  4174. @@ -3626,10 +3626,8 @@ static void add_unmap(struct dmar_domain *dom, unsigned long iova_pfn,
  4175. struct intel_iommu *iommu;
  4176. struct deferred_flush_entry *entry;
  4177. struct deferred_flush_data *flush_data;
  4178. - unsigned int cpuid;
  4179. - cpuid = get_cpu();
  4180. - flush_data = per_cpu_ptr(&deferred_flush, cpuid);
  4181. + flush_data = raw_cpu_ptr(&deferred_flush);
  4182. /* Flush all CPUs' entries to avoid deferring too much. If
  4183. * this becomes a bottleneck, can just flush us, and rely on
  4184. @@ -3662,8 +3660,6 @@ static void add_unmap(struct dmar_domain *dom, unsigned long iova_pfn,
  4185. }
  4186. flush_data->size++;
  4187. spin_unlock_irqrestore(&flush_data->lock, flags);
  4188. -
  4189. - put_cpu();
  4190. }
  4191. static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)
  4192. diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c
  4193. index e23001bfcfee..359d5d169ec0 100644
  4194. --- a/drivers/iommu/iova.c
  4195. +++ b/drivers/iommu/iova.c
  4196. @@ -22,6 +22,7 @@
  4197. #include <linux/slab.h>
  4198. #include <linux/smp.h>
  4199. #include <linux/bitops.h>
  4200. +#include <linux/cpu.h>
  4201. static bool iova_rcache_insert(struct iova_domain *iovad,
  4202. unsigned long pfn,
  4203. @@ -420,10 +421,8 @@ alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
  4204. /* Try replenishing IOVAs by flushing rcache. */
  4205. flushed_rcache = true;
  4206. - preempt_disable();
  4207. for_each_online_cpu(cpu)
  4208. free_cpu_cached_iovas(cpu, iovad);
  4209. - preempt_enable();
  4210. goto retry;
  4211. }
  4212. @@ -751,7 +750,7 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
  4213. bool can_insert = false;
  4214. unsigned long flags;
  4215. - cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
  4216. + cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  4217. spin_lock_irqsave(&cpu_rcache->lock, flags);
  4218. if (!iova_magazine_full(cpu_rcache->loaded)) {
  4219. @@ -781,7 +780,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
  4220. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  4221. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  4222. - put_cpu_ptr(rcache->cpu_rcaches);
  4223. if (mag_to_free) {
  4224. iova_magazine_free_pfns(mag_to_free, iovad);
  4225. @@ -815,7 +813,7 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  4226. bool has_pfn = false;
  4227. unsigned long flags;
  4228. - cpu_rcache = get_cpu_ptr(rcache->cpu_rcaches);
  4229. + cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  4230. spin_lock_irqsave(&cpu_rcache->lock, flags);
  4231. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  4232. @@ -837,7 +835,6 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  4233. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  4234. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  4235. - put_cpu_ptr(rcache->cpu_rcaches);
  4236. return iova_pfn;
  4237. }
  4238. diff --git a/drivers/leds/trigger/Kconfig b/drivers/leds/trigger/Kconfig
  4239. index 3f9ddb9fafa7..09da5b6b44a1 100644
  4240. --- a/drivers/leds/trigger/Kconfig
  4241. +++ b/drivers/leds/trigger/Kconfig
  4242. @@ -69,7 +69,7 @@ config LEDS_TRIGGER_BACKLIGHT
  4243. config LEDS_TRIGGER_CPU
  4244. bool "LED CPU Trigger"
  4245. - depends on LEDS_TRIGGERS
  4246. + depends on LEDS_TRIGGERS && !PREEMPT_RT_BASE
  4247. help
  4248. This allows LEDs to be controlled by active CPUs. This shows
  4249. the active CPUs across an array of LEDs so you can see which
  4250. diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
  4251. index 4d200883c505..98b64ed5cb81 100644
  4252. --- a/drivers/md/bcache/Kconfig
  4253. +++ b/drivers/md/bcache/Kconfig
  4254. @@ -1,6 +1,7 @@
  4255. config BCACHE
  4256. tristate "Block device as cache"
  4257. + depends on !PREEMPT_RT_FULL
  4258. ---help---
  4259. Allows a block device to be used as cache for other devices; uses
  4260. a btree for indexing and the layout is optimized for SSDs.
  4261. diff --git a/drivers/md/dm-rq.c b/drivers/md/dm-rq.c
  4262. index 5da86c8b6545..2aa092f2977e 100644
  4263. --- a/drivers/md/dm-rq.c
  4264. +++ b/drivers/md/dm-rq.c
  4265. @@ -811,7 +811,7 @@ static void dm_old_request_fn(struct request_queue *q)
  4266. /* Establish tio->ti before queuing work (map_tio_request) */
  4267. tio->ti = ti;
  4268. queue_kthread_work(&md->kworker, &tio->work);
  4269. - BUG_ON(!irqs_disabled());
  4270. + BUG_ON_NONRT(!irqs_disabled());
  4271. }
  4272. }
  4273. diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
  4274. index ee7fc3701700..ae59c9e13911 100644
  4275. --- a/drivers/md/raid5.c
  4276. +++ b/drivers/md/raid5.c
  4277. @@ -1928,8 +1928,9 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  4278. struct raid5_percpu *percpu;
  4279. unsigned long cpu;
  4280. - cpu = get_cpu();
  4281. + cpu = get_cpu_light();
  4282. percpu = per_cpu_ptr(conf->percpu, cpu);
  4283. + spin_lock(&percpu->lock);
  4284. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  4285. ops_run_biofill(sh);
  4286. overlap_clear++;
  4287. @@ -1985,7 +1986,8 @@ static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
  4288. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  4289. wake_up(&sh->raid_conf->wait_for_overlap);
  4290. }
  4291. - put_cpu();
  4292. + spin_unlock(&percpu->lock);
  4293. + put_cpu_light();
  4294. }
  4295. static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
  4296. @@ -6438,6 +6440,7 @@ static int raid5_alloc_percpu(struct r5conf *conf)
  4297. __func__, cpu);
  4298. break;
  4299. }
  4300. + spin_lock_init(&per_cpu_ptr(conf->percpu, cpu)->lock);
  4301. }
  4302. put_online_cpus();
  4303. diff --git a/drivers/md/raid5.h b/drivers/md/raid5.h
  4304. index 517d4b68a1be..efe91887ecd7 100644
  4305. --- a/drivers/md/raid5.h
  4306. +++ b/drivers/md/raid5.h
  4307. @@ -504,6 +504,7 @@ struct r5conf {
  4308. int recovery_disabled;
  4309. /* per cpu variables */
  4310. struct raid5_percpu {
  4311. + spinlock_t lock; /* Protection for -RT */
  4312. struct page *spare_page; /* Used when checking P/Q in raid6 */
  4313. struct flex_array *scribble; /* space for constructing buffer
  4314. * lists and performing address
  4315. diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
  4316. index d00252828966..9faab404faac 100644
  4317. --- a/drivers/misc/Kconfig
  4318. +++ b/drivers/misc/Kconfig
  4319. @@ -54,6 +54,7 @@ config AD525X_DPOT_SPI
  4320. config ATMEL_TCLIB
  4321. bool "Atmel AT32/AT91 Timer/Counter Library"
  4322. depends on (AVR32 || ARCH_AT91)
  4323. + default y if PREEMPT_RT_FULL
  4324. help
  4325. Select this if you want a library to allocate the Timer/Counter
  4326. blocks found on many Atmel processors. This facilitates using
  4327. @@ -69,8 +70,7 @@ config ATMEL_TCB_CLKSRC
  4328. are combined to make a single 32-bit timer.
  4329. When GENERIC_CLOCKEVENTS is defined, the third timer channel
  4330. - may be used as a clock event device supporting oneshot mode
  4331. - (delays of up to two seconds) based on the 32 KiHz clock.
  4332. + may be used as a clock event device supporting oneshot mode.
  4333. config ATMEL_TCB_CLKSRC_BLOCK
  4334. int
  4335. @@ -84,6 +84,15 @@ config ATMEL_TCB_CLKSRC_BLOCK
  4336. TC can be used for other purposes, such as PWM generation and
  4337. interval timing.
  4338. +config ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
  4339. + bool "TC Block use 32 KiHz clock"
  4340. + depends on ATMEL_TCB_CLKSRC
  4341. + default y if !PREEMPT_RT_FULL
  4342. + help
  4343. + Select this to use 32 KiHz base clock rate as TC block clock
  4344. + source for clock events.
  4345. +
  4346. +
  4347. config DUMMY_IRQ
  4348. tristate "Dummy IRQ handler"
  4349. default n
  4350. @@ -114,6 +123,35 @@ config IBM_ASM
  4351. for information on the specific driver level and support statement
  4352. for your IBM server.
  4353. +config HWLAT_DETECTOR
  4354. + tristate "Testing module to detect hardware-induced latencies"
  4355. + depends on DEBUG_FS
  4356. + depends on RING_BUFFER
  4357. + default m
  4358. + ---help---
  4359. + A simple hardware latency detector. Use this module to detect
  4360. + large latencies introduced by the behavior of the underlying
  4361. + system firmware external to Linux. We do this using periodic
  4362. + use of stop_machine to grab all available CPUs and measure
  4363. + for unexplainable gaps in the CPU timestamp counter(s). By
  4364. + default, the module is not enabled until the "enable" file
  4365. + within the "hwlat_detector" debugfs directory is toggled.
  4366. +
  4367. + This module is often used to detect SMI (System Management
  4368. + Interrupts) on x86 systems, though is not x86 specific. To
  4369. + this end, we default to using a sample window of 1 second,
  4370. + during which we will sample for 0.5 seconds. If an SMI or
  4371. + similar event occurs during that time, it is recorded
  4372. + into an 8K samples global ring buffer until retreived.
  4373. +
  4374. + WARNING: This software should never be enabled (it can be built
  4375. + but should not be turned on after it is loaded) in a production
  4376. + environment where high latencies are a concern since the
  4377. + sampling mechanism actually introduces latencies for
  4378. + regular tasks while the CPU(s) are being held.
  4379. +
  4380. + If unsure, say N
  4381. +
  4382. config PHANTOM
  4383. tristate "Sensable PHANToM (PCI)"
  4384. depends on PCI
  4385. diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
  4386. index fb32516ddfe2..8643df9af3c4 100644
  4387. --- a/drivers/misc/Makefile
  4388. +++ b/drivers/misc/Makefile
  4389. @@ -38,6 +38,7 @@ obj-$(CONFIG_C2PORT) += c2port/
  4390. obj-$(CONFIG_HMC6352) += hmc6352.o
  4391. obj-y += eeprom/
  4392. obj-y += cb710/
  4393. +obj-$(CONFIG_HWLAT_DETECTOR) += hwlat_detector.o
  4394. obj-$(CONFIG_SPEAR13XX_PCIE_GADGET) += spear13xx_pcie_gadget.o
  4395. obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
  4396. obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
  4397. diff --git a/drivers/misc/hwlat_detector.c b/drivers/misc/hwlat_detector.c
  4398. new file mode 100644
  4399. index 000000000000..52f5ad5fd9c0
  4400. --- /dev/null
  4401. +++ b/drivers/misc/hwlat_detector.c
  4402. @@ -0,0 +1,1240 @@
  4403. +/*
  4404. + * hwlat_detector.c - A simple Hardware Latency detector.
  4405. + *
  4406. + * Use this module to detect large system latencies induced by the behavior of
  4407. + * certain underlying system hardware or firmware, independent of Linux itself.
  4408. + * The code was developed originally to detect the presence of SMIs on Intel
  4409. + * and AMD systems, although there is no dependency upon x86 herein.
  4410. + *
  4411. + * The classical example usage of this module is in detecting the presence of
  4412. + * SMIs or System Management Interrupts on Intel and AMD systems. An SMI is a
  4413. + * somewhat special form of hardware interrupt spawned from earlier CPU debug
  4414. + * modes in which the (BIOS/EFI/etc.) firmware arranges for the South Bridge
  4415. + * LPC (or other device) to generate a special interrupt under certain
  4416. + * circumstances, for example, upon expiration of a special SMI timer device,
  4417. + * due to certain external thermal readings, on certain I/O address accesses,
  4418. + * and other situations. An SMI hits a special CPU pin, triggers a special
  4419. + * SMI mode (complete with special memory map), and the OS is unaware.
  4420. + *
  4421. + * Although certain hardware-inducing latencies are necessary (for example,
  4422. + * a modern system often requires an SMI handler for correct thermal control
  4423. + * and remote management) they can wreak havoc upon any OS-level performance
  4424. + * guarantees toward low-latency, especially when the OS is not even made
  4425. + * aware of the presence of these interrupts. For this reason, we need a
  4426. + * somewhat brute force mechanism to detect these interrupts. In this case,
  4427. + * we do it by hogging all of the CPU(s) for configurable timer intervals,
  4428. + * sampling the built-in CPU timer, looking for discontiguous readings.
  4429. + *
  4430. + * WARNING: This implementation necessarily introduces latencies. Therefore,
  4431. + * you should NEVER use this module in a production environment
  4432. + * requiring any kind of low-latency performance guarantee(s).
  4433. + *
  4434. + * Copyright (C) 2008-2009 Jon Masters, Red Hat, Inc. <jcm@redhat.com>
  4435. + *
  4436. + * Includes useful feedback from Clark Williams <clark@redhat.com>
  4437. + *
  4438. + * This file is licensed under the terms of the GNU General Public
  4439. + * License version 2. This program is licensed "as is" without any
  4440. + * warranty of any kind, whether express or implied.
  4441. + */
  4442. +
  4443. +#include <linux/module.h>
  4444. +#include <linux/init.h>
  4445. +#include <linux/ring_buffer.h>
  4446. +#include <linux/time.h>
  4447. +#include <linux/hrtimer.h>
  4448. +#include <linux/kthread.h>
  4449. +#include <linux/debugfs.h>
  4450. +#include <linux/seq_file.h>
  4451. +#include <linux/uaccess.h>
  4452. +#include <linux/version.h>
  4453. +#include <linux/delay.h>
  4454. +#include <linux/slab.h>
  4455. +#include <linux/trace_clock.h>
  4456. +
  4457. +#define BUF_SIZE_DEFAULT 262144UL /* 8K*(sizeof(entry)) */
  4458. +#define BUF_FLAGS (RB_FL_OVERWRITE) /* no block on full */
  4459. +#define U64STR_SIZE 22 /* 20 digits max */
  4460. +
  4461. +#define VERSION "1.0.0"
  4462. +#define BANNER "hwlat_detector: "
  4463. +#define DRVNAME "hwlat_detector"
  4464. +#define DEFAULT_SAMPLE_WINDOW 1000000 /* 1s */
  4465. +#define DEFAULT_SAMPLE_WIDTH 500000 /* 0.5s */
  4466. +#define DEFAULT_LAT_THRESHOLD 10 /* 10us */
  4467. +
  4468. +/* Module metadata */
  4469. +
  4470. +MODULE_LICENSE("GPL");
  4471. +MODULE_AUTHOR("Jon Masters <jcm@redhat.com>");
  4472. +MODULE_DESCRIPTION("A simple hardware latency detector");
  4473. +MODULE_VERSION(VERSION);
  4474. +
  4475. +/* Module parameters */
  4476. +
  4477. +static int debug;
  4478. +static int enabled;
  4479. +static int threshold;
  4480. +
  4481. +module_param(debug, int, 0); /* enable debug */
  4482. +module_param(enabled, int, 0); /* enable detector */
  4483. +module_param(threshold, int, 0); /* latency threshold */
  4484. +
  4485. +/* Buffering and sampling */
  4486. +
  4487. +static struct ring_buffer *ring_buffer; /* sample buffer */
  4488. +static DEFINE_MUTEX(ring_buffer_mutex); /* lock changes */
  4489. +static unsigned long buf_size = BUF_SIZE_DEFAULT;
  4490. +static struct task_struct *kthread; /* sampling thread */
  4491. +
  4492. +/* DebugFS filesystem entries */
  4493. +
  4494. +static struct dentry *debug_dir; /* debugfs directory */
  4495. +static struct dentry *debug_max; /* maximum TSC delta */
  4496. +static struct dentry *debug_count; /* total detect count */
  4497. +static struct dentry *debug_sample_width; /* sample width us */
  4498. +static struct dentry *debug_sample_window; /* sample window us */
  4499. +static struct dentry *debug_sample; /* raw samples us */
  4500. +static struct dentry *debug_threshold; /* threshold us */
  4501. +static struct dentry *debug_enable; /* enable/disable */
  4502. +
  4503. +/* Individual samples and global state */
  4504. +
  4505. +struct sample; /* latency sample */
  4506. +struct data; /* Global state */
  4507. +
  4508. +/* Sampling functions */
  4509. +static int __buffer_add_sample(struct sample *sample);
  4510. +static struct sample *buffer_get_sample(struct sample *sample);
  4511. +
  4512. +/* Threading and state */
  4513. +static int kthread_fn(void *unused);
  4514. +static int start_kthread(void);
  4515. +static int stop_kthread(void);
  4516. +static void __reset_stats(void);
  4517. +static int init_stats(void);
  4518. +
  4519. +/* Debugfs interface */
  4520. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  4521. + size_t cnt, loff_t *ppos, const u64 *entry);
  4522. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  4523. + size_t cnt, loff_t *ppos, u64 *entry);
  4524. +static int debug_sample_fopen(struct inode *inode, struct file *filp);
  4525. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  4526. + size_t cnt, loff_t *ppos);
  4527. +static int debug_sample_release(struct inode *inode, struct file *filp);
  4528. +static int debug_enable_fopen(struct inode *inode, struct file *filp);
  4529. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  4530. + size_t cnt, loff_t *ppos);
  4531. +static ssize_t debug_enable_fwrite(struct file *file,
  4532. + const char __user *user_buffer,
  4533. + size_t user_size, loff_t *offset);
  4534. +
  4535. +/* Initialization functions */
  4536. +static int init_debugfs(void);
  4537. +static void free_debugfs(void);
  4538. +static int detector_init(void);
  4539. +static void detector_exit(void);
  4540. +
  4541. +/* Individual latency samples are stored here when detected and packed into
  4542. + * the ring_buffer circular buffer, where they are overwritten when
  4543. + * more than buf_size/sizeof(sample) samples are received. */
  4544. +struct sample {
  4545. + u64 seqnum; /* unique sequence */
  4546. + u64 duration; /* ktime delta */
  4547. + u64 outer_duration; /* ktime delta (outer loop) */
  4548. + struct timespec timestamp; /* wall time */
  4549. + unsigned long lost;
  4550. +};
  4551. +
  4552. +/* keep the global state somewhere. */
  4553. +static struct data {
  4554. +
  4555. + struct mutex lock; /* protect changes */
  4556. +
  4557. + u64 count; /* total since reset */
  4558. + u64 max_sample; /* max hardware latency */
  4559. + u64 threshold; /* sample threshold level */
  4560. +
  4561. + u64 sample_window; /* total sampling window (on+off) */
  4562. + u64 sample_width; /* active sampling portion of window */
  4563. +
  4564. + atomic_t sample_open; /* whether the sample file is open */
  4565. +
  4566. + wait_queue_head_t wq; /* waitqeue for new sample values */
  4567. +
  4568. +} data;
  4569. +
  4570. +/**
  4571. + * __buffer_add_sample - add a new latency sample recording to the ring buffer
  4572. + * @sample: The new latency sample value
  4573. + *
  4574. + * This receives a new latency sample and records it in a global ring buffer.
  4575. + * No additional locking is used in this case.
  4576. + */
  4577. +static int __buffer_add_sample(struct sample *sample)
  4578. +{
  4579. + return ring_buffer_write(ring_buffer,
  4580. + sizeof(struct sample), sample);
  4581. +}
  4582. +
  4583. +/**
  4584. + * buffer_get_sample - remove a hardware latency sample from the ring buffer
  4585. + * @sample: Pre-allocated storage for the sample
  4586. + *
  4587. + * This retrieves a hardware latency sample from the global circular buffer
  4588. + */
  4589. +static struct sample *buffer_get_sample(struct sample *sample)
  4590. +{
  4591. + struct ring_buffer_event *e = NULL;
  4592. + struct sample *s = NULL;
  4593. + unsigned int cpu = 0;
  4594. +
  4595. + if (!sample)
  4596. + return NULL;
  4597. +
  4598. + mutex_lock(&ring_buffer_mutex);
  4599. + for_each_online_cpu(cpu) {
  4600. + e = ring_buffer_consume(ring_buffer, cpu, NULL, &sample->lost);
  4601. + if (e)
  4602. + break;
  4603. + }
  4604. +
  4605. + if (e) {
  4606. + s = ring_buffer_event_data(e);
  4607. + memcpy(sample, s, sizeof(struct sample));
  4608. + } else
  4609. + sample = NULL;
  4610. + mutex_unlock(&ring_buffer_mutex);
  4611. +
  4612. + return sample;
  4613. +}
  4614. +
  4615. +#ifndef CONFIG_TRACING
  4616. +#define time_type ktime_t
  4617. +#define time_get() ktime_get()
  4618. +#define time_to_us(x) ktime_to_us(x)
  4619. +#define time_sub(a, b) ktime_sub(a, b)
  4620. +#define init_time(a, b) (a).tv64 = b
  4621. +#define time_u64(a) ((a).tv64)
  4622. +#else
  4623. +#define time_type u64
  4624. +#define time_get() trace_clock_local()
  4625. +#define time_to_us(x) div_u64(x, 1000)
  4626. +#define time_sub(a, b) ((a) - (b))
  4627. +#define init_time(a, b) (a = b)
  4628. +#define time_u64(a) a
  4629. +#endif
  4630. +/**
  4631. + * get_sample - sample the CPU TSC and look for likely hardware latencies
  4632. + *
  4633. + * Used to repeatedly capture the CPU TSC (or similar), looking for potential
  4634. + * hardware-induced latency. Called with interrupts disabled and with
  4635. + * data.lock held.
  4636. + */
  4637. +static int get_sample(void)
  4638. +{
  4639. + time_type start, t1, t2, last_t2;
  4640. + s64 diff, total = 0;
  4641. + u64 sample = 0;
  4642. + u64 outer_sample = 0;
  4643. + int ret = -1;
  4644. +
  4645. + init_time(last_t2, 0);
  4646. + start = time_get(); /* start timestamp */
  4647. +
  4648. + do {
  4649. +
  4650. + t1 = time_get(); /* we'll look for a discontinuity */
  4651. + t2 = time_get();
  4652. +
  4653. + if (time_u64(last_t2)) {
  4654. + /* Check the delta from outer loop (t2 to next t1) */
  4655. + diff = time_to_us(time_sub(t1, last_t2));
  4656. + /* This shouldn't happen */
  4657. + if (diff < 0) {
  4658. + pr_err(BANNER "time running backwards\n");
  4659. + goto out;
  4660. + }
  4661. + if (diff > outer_sample)
  4662. + outer_sample = diff;
  4663. + }
  4664. + last_t2 = t2;
  4665. +
  4666. + total = time_to_us(time_sub(t2, start)); /* sample width */
  4667. +
  4668. + /* This checks the inner loop (t1 to t2) */
  4669. + diff = time_to_us(time_sub(t2, t1)); /* current diff */
  4670. +
  4671. + /* This shouldn't happen */
  4672. + if (diff < 0) {
  4673. + pr_err(BANNER "time running backwards\n");
  4674. + goto out;
  4675. + }
  4676. +
  4677. + if (diff > sample)
  4678. + sample = diff; /* only want highest value */
  4679. +
  4680. + } while (total <= data.sample_width);
  4681. +
  4682. + ret = 0;
  4683. +
  4684. + /* If we exceed the threshold value, we have found a hardware latency */
  4685. + if (sample > data.threshold || outer_sample > data.threshold) {
  4686. + struct sample s;
  4687. +
  4688. + ret = 1;
  4689. +
  4690. + data.count++;
  4691. + s.seqnum = data.count;
  4692. + s.duration = sample;
  4693. + s.outer_duration = outer_sample;
  4694. + s.timestamp = CURRENT_TIME;
  4695. + __buffer_add_sample(&s);
  4696. +
  4697. + /* Keep a running maximum ever recorded hardware latency */
  4698. + if (sample > data.max_sample)
  4699. + data.max_sample = sample;
  4700. + }
  4701. +
  4702. +out:
  4703. + return ret;
  4704. +}
  4705. +
  4706. +/*
  4707. + * kthread_fn - The CPU time sampling/hardware latency detection kernel thread
  4708. + * @unused: A required part of the kthread API.
  4709. + *
  4710. + * Used to periodically sample the CPU TSC via a call to get_sample. We
  4711. + * disable interrupts, which does (intentionally) introduce latency since we
  4712. + * need to ensure nothing else might be running (and thus pre-empting).
  4713. + * Obviously this should never be used in production environments.
  4714. + *
  4715. + * Currently this runs on which ever CPU it was scheduled on, but most
  4716. + * real-worald hardware latency situations occur across several CPUs,
  4717. + * but we might later generalize this if we find there are any actualy
  4718. + * systems with alternate SMI delivery or other hardware latencies.
  4719. + */
  4720. +static int kthread_fn(void *unused)
  4721. +{
  4722. + int ret;
  4723. + u64 interval;
  4724. +
  4725. + while (!kthread_should_stop()) {
  4726. +
  4727. + mutex_lock(&data.lock);
  4728. +
  4729. + local_irq_disable();
  4730. + ret = get_sample();
  4731. + local_irq_enable();
  4732. +
  4733. + if (ret > 0)
  4734. + wake_up(&data.wq); /* wake up reader(s) */
  4735. +
  4736. + interval = data.sample_window - data.sample_width;
  4737. + do_div(interval, USEC_PER_MSEC); /* modifies interval value */
  4738. +
  4739. + mutex_unlock(&data.lock);
  4740. +
  4741. + if (msleep_interruptible(interval))
  4742. + break;
  4743. + }
  4744. +
  4745. + return 0;
  4746. +}
  4747. +
  4748. +/**
  4749. + * start_kthread - Kick off the hardware latency sampling/detector kthread
  4750. + *
  4751. + * This starts a kernel thread that will sit and sample the CPU timestamp
  4752. + * counter (TSC or similar) and look for potential hardware latencies.
  4753. + */
  4754. +static int start_kthread(void)
  4755. +{
  4756. + kthread = kthread_run(kthread_fn, NULL,
  4757. + DRVNAME);
  4758. + if (IS_ERR(kthread)) {
  4759. + pr_err(BANNER "could not start sampling thread\n");
  4760. + enabled = 0;
  4761. + return -ENOMEM;
  4762. + }
  4763. +
  4764. + return 0;
  4765. +}
  4766. +
  4767. +/**
  4768. + * stop_kthread - Inform the hardware latency samping/detector kthread to stop
  4769. + *
  4770. + * This kicks the running hardware latency sampling/detector kernel thread and
  4771. + * tells it to stop sampling now. Use this on unload and at system shutdown.
  4772. + */
  4773. +static int stop_kthread(void)
  4774. +{
  4775. + int ret;
  4776. +
  4777. + ret = kthread_stop(kthread);
  4778. +
  4779. + return ret;
  4780. +}
  4781. +
  4782. +/**
  4783. + * __reset_stats - Reset statistics for the hardware latency detector
  4784. + *
  4785. + * We use data to store various statistics and global state. We call this
  4786. + * function in order to reset those when "enable" is toggled on or off, and
  4787. + * also at initialization. Should be called with data.lock held.
  4788. + */
  4789. +static void __reset_stats(void)
  4790. +{
  4791. + data.count = 0;
  4792. + data.max_sample = 0;
  4793. + ring_buffer_reset(ring_buffer); /* flush out old sample entries */
  4794. +}
  4795. +
  4796. +/**
  4797. + * init_stats - Setup global state statistics for the hardware latency detector
  4798. + *
  4799. + * We use data to store various statistics and global state. We also use
  4800. + * a global ring buffer (ring_buffer) to keep raw samples of detected hardware
  4801. + * induced system latencies. This function initializes these structures and
  4802. + * allocates the global ring buffer also.
  4803. + */
  4804. +static int init_stats(void)
  4805. +{
  4806. + int ret = -ENOMEM;
  4807. +
  4808. + mutex_init(&data.lock);
  4809. + init_waitqueue_head(&data.wq);
  4810. + atomic_set(&data.sample_open, 0);
  4811. +
  4812. + ring_buffer = ring_buffer_alloc(buf_size, BUF_FLAGS);
  4813. +
  4814. + if (WARN(!ring_buffer, KERN_ERR BANNER
  4815. + "failed to allocate ring buffer!\n"))
  4816. + goto out;
  4817. +
  4818. + __reset_stats();
  4819. + data.threshold = threshold ?: DEFAULT_LAT_THRESHOLD; /* threshold us */
  4820. + data.sample_window = DEFAULT_SAMPLE_WINDOW; /* window us */
  4821. + data.sample_width = DEFAULT_SAMPLE_WIDTH; /* width us */
  4822. +
  4823. + ret = 0;
  4824. +
  4825. +out:
  4826. + return ret;
  4827. +
  4828. +}
  4829. +
  4830. +/*
  4831. + * simple_data_read - Wrapper read function for global state debugfs entries
  4832. + * @filp: The active open file structure for the debugfs "file"
  4833. + * @ubuf: The userspace provided buffer to read value into
  4834. + * @cnt: The maximum number of bytes to read
  4835. + * @ppos: The current "file" position
  4836. + * @entry: The entry to read from
  4837. + *
  4838. + * This function provides a generic read implementation for the global state
  4839. + * "data" structure debugfs filesystem entries. It would be nice to use
  4840. + * simple_attr_read directly, but we need to make sure that the data.lock
  4841. + * is held during the actual read.
  4842. + */
  4843. +static ssize_t simple_data_read(struct file *filp, char __user *ubuf,
  4844. + size_t cnt, loff_t *ppos, const u64 *entry)
  4845. +{
  4846. + char buf[U64STR_SIZE];
  4847. + u64 val = 0;
  4848. + int len = 0;
  4849. +
  4850. + memset(buf, 0, sizeof(buf));
  4851. +
  4852. + if (!entry)
  4853. + return -EFAULT;
  4854. +
  4855. + mutex_lock(&data.lock);
  4856. + val = *entry;
  4857. + mutex_unlock(&data.lock);
  4858. +
  4859. + len = snprintf(buf, sizeof(buf), "%llu\n", (unsigned long long)val);
  4860. +
  4861. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  4862. +
  4863. +}
  4864. +
  4865. +/*
  4866. + * simple_data_write - Wrapper write function for global state debugfs entries
  4867. + * @filp: The active open file structure for the debugfs "file"
  4868. + * @ubuf: The userspace provided buffer to write value from
  4869. + * @cnt: The maximum number of bytes to write
  4870. + * @ppos: The current "file" position
  4871. + * @entry: The entry to write to
  4872. + *
  4873. + * This function provides a generic write implementation for the global state
  4874. + * "data" structure debugfs filesystem entries. It would be nice to use
  4875. + * simple_attr_write directly, but we need to make sure that the data.lock
  4876. + * is held during the actual write.
  4877. + */
  4878. +static ssize_t simple_data_write(struct file *filp, const char __user *ubuf,
  4879. + size_t cnt, loff_t *ppos, u64 *entry)
  4880. +{
  4881. + char buf[U64STR_SIZE];
  4882. + int csize = min(cnt, sizeof(buf));
  4883. + u64 val = 0;
  4884. + int err = 0;
  4885. +
  4886. + memset(buf, '\0', sizeof(buf));
  4887. + if (copy_from_user(buf, ubuf, csize))
  4888. + return -EFAULT;
  4889. +
  4890. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  4891. + err = kstrtoull(buf, 10, &val);
  4892. + if (err)
  4893. + return -EINVAL;
  4894. +
  4895. + mutex_lock(&data.lock);
  4896. + *entry = val;
  4897. + mutex_unlock(&data.lock);
  4898. +
  4899. + return csize;
  4900. +}
  4901. +
  4902. +/**
  4903. + * debug_count_fopen - Open function for "count" debugfs entry
  4904. + * @inode: The in-kernel inode representation of the debugfs "file"
  4905. + * @filp: The active open file structure for the debugfs "file"
  4906. + *
  4907. + * This function provides an open implementation for the "count" debugfs
  4908. + * interface to the hardware latency detector.
  4909. + */
  4910. +static int debug_count_fopen(struct inode *inode, struct file *filp)
  4911. +{
  4912. + return 0;
  4913. +}
  4914. +
  4915. +/**
  4916. + * debug_count_fread - Read function for "count" debugfs entry
  4917. + * @filp: The active open file structure for the debugfs "file"
  4918. + * @ubuf: The userspace provided buffer to read value into
  4919. + * @cnt: The maximum number of bytes to read
  4920. + * @ppos: The current "file" position
  4921. + *
  4922. + * This function provides a read implementation for the "count" debugfs
  4923. + * interface to the hardware latency detector. Can be used to read the
  4924. + * number of latency readings exceeding the configured threshold since
  4925. + * the detector was last reset (e.g. by writing a zero into "count").
  4926. + */
  4927. +static ssize_t debug_count_fread(struct file *filp, char __user *ubuf,
  4928. + size_t cnt, loff_t *ppos)
  4929. +{
  4930. + return simple_data_read(filp, ubuf, cnt, ppos, &data.count);
  4931. +}
  4932. +
  4933. +/**
  4934. + * debug_count_fwrite - Write function for "count" debugfs entry
  4935. + * @filp: The active open file structure for the debugfs "file"
  4936. + * @ubuf: The user buffer that contains the value to write
  4937. + * @cnt: The maximum number of bytes to write to "file"
  4938. + * @ppos: The current position in the debugfs "file"
  4939. + *
  4940. + * This function provides a write implementation for the "count" debugfs
  4941. + * interface to the hardware latency detector. Can be used to write a
  4942. + * desired value, especially to zero the total count.
  4943. + */
  4944. +static ssize_t debug_count_fwrite(struct file *filp,
  4945. + const char __user *ubuf,
  4946. + size_t cnt,
  4947. + loff_t *ppos)
  4948. +{
  4949. + return simple_data_write(filp, ubuf, cnt, ppos, &data.count);
  4950. +}
  4951. +
  4952. +/**
  4953. + * debug_enable_fopen - Dummy open function for "enable" debugfs interface
  4954. + * @inode: The in-kernel inode representation of the debugfs "file"
  4955. + * @filp: The active open file structure for the debugfs "file"
  4956. + *
  4957. + * This function provides an open implementation for the "enable" debugfs
  4958. + * interface to the hardware latency detector.
  4959. + */
  4960. +static int debug_enable_fopen(struct inode *inode, struct file *filp)
  4961. +{
  4962. + return 0;
  4963. +}
  4964. +
  4965. +/**
  4966. + * debug_enable_fread - Read function for "enable" debugfs interface
  4967. + * @filp: The active open file structure for the debugfs "file"
  4968. + * @ubuf: The userspace provided buffer to read value into
  4969. + * @cnt: The maximum number of bytes to read
  4970. + * @ppos: The current "file" position
  4971. + *
  4972. + * This function provides a read implementation for the "enable" debugfs
  4973. + * interface to the hardware latency detector. Can be used to determine
  4974. + * whether the detector is currently enabled ("0\n" or "1\n" returned).
  4975. + */
  4976. +static ssize_t debug_enable_fread(struct file *filp, char __user *ubuf,
  4977. + size_t cnt, loff_t *ppos)
  4978. +{
  4979. + char buf[4];
  4980. +
  4981. + if ((cnt < sizeof(buf)) || (*ppos))
  4982. + return 0;
  4983. +
  4984. + buf[0] = enabled ? '1' : '0';
  4985. + buf[1] = '\n';
  4986. + buf[2] = '\0';
  4987. + if (copy_to_user(ubuf, buf, strlen(buf)))
  4988. + return -EFAULT;
  4989. + return *ppos = strlen(buf);
  4990. +}
  4991. +
  4992. +/**
  4993. + * debug_enable_fwrite - Write function for "enable" debugfs interface
  4994. + * @filp: The active open file structure for the debugfs "file"
  4995. + * @ubuf: The user buffer that contains the value to write
  4996. + * @cnt: The maximum number of bytes to write to "file"
  4997. + * @ppos: The current position in the debugfs "file"
  4998. + *
  4999. + * This function provides a write implementation for the "enable" debugfs
  5000. + * interface to the hardware latency detector. Can be used to enable or
  5001. + * disable the detector, which will have the side-effect of possibly
  5002. + * also resetting the global stats and kicking off the measuring
  5003. + * kthread (on an enable) or the converse (upon a disable).
  5004. + */
  5005. +static ssize_t debug_enable_fwrite(struct file *filp,
  5006. + const char __user *ubuf,
  5007. + size_t cnt,
  5008. + loff_t *ppos)
  5009. +{
  5010. + char buf[4];
  5011. + int csize = min(cnt, sizeof(buf));
  5012. + long val = 0;
  5013. + int err = 0;
  5014. +
  5015. + memset(buf, '\0', sizeof(buf));
  5016. + if (copy_from_user(buf, ubuf, csize))
  5017. + return -EFAULT;
  5018. +
  5019. + buf[sizeof(buf)-1] = '\0'; /* just in case */
  5020. + err = kstrtoul(buf, 10, &val);
  5021. + if (err)
  5022. + return -EINVAL;
  5023. +
  5024. + if (val) {
  5025. + if (enabled)
  5026. + goto unlock;
  5027. + enabled = 1;
  5028. + __reset_stats();
  5029. + if (start_kthread())
  5030. + return -EFAULT;
  5031. + } else {
  5032. + if (!enabled)
  5033. + goto unlock;
  5034. + enabled = 0;
  5035. + err = stop_kthread();
  5036. + if (err) {
  5037. + pr_err(BANNER "cannot stop kthread\n");
  5038. + return -EFAULT;
  5039. + }
  5040. + wake_up(&data.wq); /* reader(s) should return */
  5041. + }
  5042. +unlock:
  5043. + return csize;
  5044. +}
  5045. +
  5046. +/**
  5047. + * debug_max_fopen - Open function for "max" debugfs entry
  5048. + * @inode: The in-kernel inode representation of the debugfs "file"
  5049. + * @filp: The active open file structure for the debugfs "file"
  5050. + *
  5051. + * This function provides an open implementation for the "max" debugfs
  5052. + * interface to the hardware latency detector.
  5053. + */
  5054. +static int debug_max_fopen(struct inode *inode, struct file *filp)
  5055. +{
  5056. + return 0;
  5057. +}
  5058. +
  5059. +/**
  5060. + * debug_max_fread - Read function for "max" debugfs entry
  5061. + * @filp: The active open file structure for the debugfs "file"
  5062. + * @ubuf: The userspace provided buffer to read value into
  5063. + * @cnt: The maximum number of bytes to read
  5064. + * @ppos: The current "file" position
  5065. + *
  5066. + * This function provides a read implementation for the "max" debugfs
  5067. + * interface to the hardware latency detector. Can be used to determine
  5068. + * the maximum latency value observed since it was last reset.
  5069. + */
  5070. +static ssize_t debug_max_fread(struct file *filp, char __user *ubuf,
  5071. + size_t cnt, loff_t *ppos)
  5072. +{
  5073. + return simple_data_read(filp, ubuf, cnt, ppos, &data.max_sample);
  5074. +}
  5075. +
  5076. +/**
  5077. + * debug_max_fwrite - Write function for "max" debugfs entry
  5078. + * @filp: The active open file structure for the debugfs "file"
  5079. + * @ubuf: The user buffer that contains the value to write
  5080. + * @cnt: The maximum number of bytes to write to "file"
  5081. + * @ppos: The current position in the debugfs "file"
  5082. + *
  5083. + * This function provides a write implementation for the "max" debugfs
  5084. + * interface to the hardware latency detector. Can be used to reset the
  5085. + * maximum or set it to some other desired value - if, then, subsequent
  5086. + * measurements exceed this value, the maximum will be updated.
  5087. + */
  5088. +static ssize_t debug_max_fwrite(struct file *filp,
  5089. + const char __user *ubuf,
  5090. + size_t cnt,
  5091. + loff_t *ppos)
  5092. +{
  5093. + return simple_data_write(filp, ubuf, cnt, ppos, &data.max_sample);
  5094. +}
  5095. +
  5096. +
  5097. +/**
  5098. + * debug_sample_fopen - An open function for "sample" debugfs interface
  5099. + * @inode: The in-kernel inode representation of this debugfs "file"
  5100. + * @filp: The active open file structure for the debugfs "file"
  5101. + *
  5102. + * This function handles opening the "sample" file within the hardware
  5103. + * latency detector debugfs directory interface. This file is used to read
  5104. + * raw samples from the global ring_buffer and allows the user to see a
  5105. + * running latency history. Can be opened blocking or non-blocking,
  5106. + * affecting whether it behaves as a buffer read pipe, or does not.
  5107. + * Implements simple locking to prevent multiple simultaneous use.
  5108. + */
  5109. +static int debug_sample_fopen(struct inode *inode, struct file *filp)
  5110. +{
  5111. + if (!atomic_add_unless(&data.sample_open, 1, 1))
  5112. + return -EBUSY;
  5113. + else
  5114. + return 0;
  5115. +}
  5116. +
  5117. +/**
  5118. + * debug_sample_fread - A read function for "sample" debugfs interface
  5119. + * @filp: The active open file structure for the debugfs "file"
  5120. + * @ubuf: The user buffer that will contain the samples read
  5121. + * @cnt: The maximum bytes to read from the debugfs "file"
  5122. + * @ppos: The current position in the debugfs "file"
  5123. + *
  5124. + * This function handles reading from the "sample" file within the hardware
  5125. + * latency detector debugfs directory interface. This file is used to read
  5126. + * raw samples from the global ring_buffer and allows the user to see a
  5127. + * running latency history. By default this will block pending a new
  5128. + * value written into the sample buffer, unless there are already a
  5129. + * number of value(s) waiting in the buffer, or the sample file was
  5130. + * previously opened in a non-blocking mode of operation.
  5131. + */
  5132. +static ssize_t debug_sample_fread(struct file *filp, char __user *ubuf,
  5133. + size_t cnt, loff_t *ppos)
  5134. +{
  5135. + int len = 0;
  5136. + char buf[64];
  5137. + struct sample *sample = NULL;
  5138. +
  5139. + if (!enabled)
  5140. + return 0;
  5141. +
  5142. + sample = kzalloc(sizeof(struct sample), GFP_KERNEL);
  5143. + if (!sample)
  5144. + return -ENOMEM;
  5145. +
  5146. + while (!buffer_get_sample(sample)) {
  5147. +
  5148. + DEFINE_WAIT(wait);
  5149. +
  5150. + if (filp->f_flags & O_NONBLOCK) {
  5151. + len = -EAGAIN;
  5152. + goto out;
  5153. + }
  5154. +
  5155. + prepare_to_wait(&data.wq, &wait, TASK_INTERRUPTIBLE);
  5156. + schedule();
  5157. + finish_wait(&data.wq, &wait);
  5158. +
  5159. + if (signal_pending(current)) {
  5160. + len = -EINTR;
  5161. + goto out;
  5162. + }
  5163. +
  5164. + if (!enabled) { /* enable was toggled */
  5165. + len = 0;
  5166. + goto out;
  5167. + }
  5168. + }
  5169. +
  5170. + len = snprintf(buf, sizeof(buf), "%010lu.%010lu\t%llu\t%llu\n",
  5171. + sample->timestamp.tv_sec,
  5172. + sample->timestamp.tv_nsec,
  5173. + sample->duration,
  5174. + sample->outer_duration);
  5175. +
  5176. +
  5177. + /* handling partial reads is more trouble than it's worth */
  5178. + if (len > cnt)
  5179. + goto out;
  5180. +
  5181. + if (copy_to_user(ubuf, buf, len))
  5182. + len = -EFAULT;
  5183. +
  5184. +out:
  5185. + kfree(sample);
  5186. + return len;
  5187. +}
  5188. +
  5189. +/**
  5190. + * debug_sample_release - Release function for "sample" debugfs interface
  5191. + * @inode: The in-kernel inode represenation of the debugfs "file"
  5192. + * @filp: The active open file structure for the debugfs "file"
  5193. + *
  5194. + * This function completes the close of the debugfs interface "sample" file.
  5195. + * Frees the sample_open "lock" so that other users may open the interface.
  5196. + */
  5197. +static int debug_sample_release(struct inode *inode, struct file *filp)
  5198. +{
  5199. + atomic_dec(&data.sample_open);
  5200. +
  5201. + return 0;
  5202. +}
  5203. +
  5204. +/**
  5205. + * debug_threshold_fopen - Open function for "threshold" debugfs entry
  5206. + * @inode: The in-kernel inode representation of the debugfs "file"
  5207. + * @filp: The active open file structure for the debugfs "file"
  5208. + *
  5209. + * This function provides an open implementation for the "threshold" debugfs
  5210. + * interface to the hardware latency detector.
  5211. + */
  5212. +static int debug_threshold_fopen(struct inode *inode, struct file *filp)
  5213. +{
  5214. + return 0;
  5215. +}
  5216. +
  5217. +/**
  5218. + * debug_threshold_fread - Read function for "threshold" debugfs entry
  5219. + * @filp: The active open file structure for the debugfs "file"
  5220. + * @ubuf: The userspace provided buffer to read value into
  5221. + * @cnt: The maximum number of bytes to read
  5222. + * @ppos: The current "file" position
  5223. + *
  5224. + * This function provides a read implementation for the "threshold" debugfs
  5225. + * interface to the hardware latency detector. It can be used to determine
  5226. + * the current threshold level at which a latency will be recorded in the
  5227. + * global ring buffer, typically on the order of 10us.
  5228. + */
  5229. +static ssize_t debug_threshold_fread(struct file *filp, char __user *ubuf,
  5230. + size_t cnt, loff_t *ppos)
  5231. +{
  5232. + return simple_data_read(filp, ubuf, cnt, ppos, &data.threshold);
  5233. +}
  5234. +
  5235. +/**
  5236. + * debug_threshold_fwrite - Write function for "threshold" debugfs entry
  5237. + * @filp: The active open file structure for the debugfs "file"
  5238. + * @ubuf: The user buffer that contains the value to write
  5239. + * @cnt: The maximum number of bytes to write to "file"
  5240. + * @ppos: The current position in the debugfs "file"
  5241. + *
  5242. + * This function provides a write implementation for the "threshold" debugfs
  5243. + * interface to the hardware latency detector. It can be used to configure
  5244. + * the threshold level at which any subsequently detected latencies will
  5245. + * be recorded into the global ring buffer.
  5246. + */
  5247. +static ssize_t debug_threshold_fwrite(struct file *filp,
  5248. + const char __user *ubuf,
  5249. + size_t cnt,
  5250. + loff_t *ppos)
  5251. +{
  5252. + int ret;
  5253. +
  5254. + ret = simple_data_write(filp, ubuf, cnt, ppos, &data.threshold);
  5255. +
  5256. + if (enabled)
  5257. + wake_up_process(kthread);
  5258. +
  5259. + return ret;
  5260. +}
  5261. +
  5262. +/**
  5263. + * debug_width_fopen - Open function for "width" debugfs entry
  5264. + * @inode: The in-kernel inode representation of the debugfs "file"
  5265. + * @filp: The active open file structure for the debugfs "file"
  5266. + *
  5267. + * This function provides an open implementation for the "width" debugfs
  5268. + * interface to the hardware latency detector.
  5269. + */
  5270. +static int debug_width_fopen(struct inode *inode, struct file *filp)
  5271. +{
  5272. + return 0;
  5273. +}
  5274. +
  5275. +/**
  5276. + * debug_width_fread - Read function for "width" debugfs entry
  5277. + * @filp: The active open file structure for the debugfs "file"
  5278. + * @ubuf: The userspace provided buffer to read value into
  5279. + * @cnt: The maximum number of bytes to read
  5280. + * @ppos: The current "file" position
  5281. + *
  5282. + * This function provides a read implementation for the "width" debugfs
  5283. + * interface to the hardware latency detector. It can be used to determine
  5284. + * for how many us of the total window us we will actively sample for any
  5285. + * hardware-induced latecy periods. Obviously, it is not possible to
  5286. + * sample constantly and have the system respond to a sample reader, or,
  5287. + * worse, without having the system appear to have gone out to lunch.
  5288. + */
  5289. +static ssize_t debug_width_fread(struct file *filp, char __user *ubuf,
  5290. + size_t cnt, loff_t *ppos)
  5291. +{
  5292. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_width);
  5293. +}
  5294. +
  5295. +/**
  5296. + * debug_width_fwrite - Write function for "width" debugfs entry
  5297. + * @filp: The active open file structure for the debugfs "file"
  5298. + * @ubuf: The user buffer that contains the value to write
  5299. + * @cnt: The maximum number of bytes to write to "file"
  5300. + * @ppos: The current position in the debugfs "file"
  5301. + *
  5302. + * This function provides a write implementation for the "width" debugfs
  5303. + * interface to the hardware latency detector. It can be used to configure
  5304. + * for how many us of the total window us we will actively sample for any
  5305. + * hardware-induced latency periods. Obviously, it is not possible to
  5306. + * sample constantly and have the system respond to a sample reader, or,
  5307. + * worse, without having the system appear to have gone out to lunch. It
  5308. + * is enforced that width is less that the total window size.
  5309. + */
  5310. +static ssize_t debug_width_fwrite(struct file *filp,
  5311. + const char __user *ubuf,
  5312. + size_t cnt,
  5313. + loff_t *ppos)
  5314. +{
  5315. + char buf[U64STR_SIZE];
  5316. + int csize = min(cnt, sizeof(buf));
  5317. + u64 val = 0;
  5318. + int err = 0;
  5319. +
  5320. + memset(buf, '\0', sizeof(buf));
  5321. + if (copy_from_user(buf, ubuf, csize))
  5322. + return -EFAULT;
  5323. +
  5324. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  5325. + err = kstrtoull(buf, 10, &val);
  5326. + if (err)
  5327. + return -EINVAL;
  5328. +
  5329. + mutex_lock(&data.lock);
  5330. + if (val < data.sample_window)
  5331. + data.sample_width = val;
  5332. + else {
  5333. + mutex_unlock(&data.lock);
  5334. + return -EINVAL;
  5335. + }
  5336. + mutex_unlock(&data.lock);
  5337. +
  5338. + if (enabled)
  5339. + wake_up_process(kthread);
  5340. +
  5341. + return csize;
  5342. +}
  5343. +
  5344. +/**
  5345. + * debug_window_fopen - Open function for "window" debugfs entry
  5346. + * @inode: The in-kernel inode representation of the debugfs "file"
  5347. + * @filp: The active open file structure for the debugfs "file"
  5348. + *
  5349. + * This function provides an open implementation for the "window" debugfs
  5350. + * interface to the hardware latency detector. The window is the total time
  5351. + * in us that will be considered one sample period. Conceptually, windows
  5352. + * occur back-to-back and contain a sample width period during which
  5353. + * actual sampling occurs.
  5354. + */
  5355. +static int debug_window_fopen(struct inode *inode, struct file *filp)
  5356. +{
  5357. + return 0;
  5358. +}
  5359. +
  5360. +/**
  5361. + * debug_window_fread - Read function for "window" debugfs entry
  5362. + * @filp: The active open file structure for the debugfs "file"
  5363. + * @ubuf: The userspace provided buffer to read value into
  5364. + * @cnt: The maximum number of bytes to read
  5365. + * @ppos: The current "file" position
  5366. + *
  5367. + * This function provides a read implementation for the "window" debugfs
  5368. + * interface to the hardware latency detector. The window is the total time
  5369. + * in us that will be considered one sample period. Conceptually, windows
  5370. + * occur back-to-back and contain a sample width period during which
  5371. + * actual sampling occurs. Can be used to read the total window size.
  5372. + */
  5373. +static ssize_t debug_window_fread(struct file *filp, char __user *ubuf,
  5374. + size_t cnt, loff_t *ppos)
  5375. +{
  5376. + return simple_data_read(filp, ubuf, cnt, ppos, &data.sample_window);
  5377. +}
  5378. +
  5379. +/**
  5380. + * debug_window_fwrite - Write function for "window" debugfs entry
  5381. + * @filp: The active open file structure for the debugfs "file"
  5382. + * @ubuf: The user buffer that contains the value to write
  5383. + * @cnt: The maximum number of bytes to write to "file"
  5384. + * @ppos: The current position in the debugfs "file"
  5385. + *
  5386. + * This function provides a write implementation for the "window" debufds
  5387. + * interface to the hardware latency detetector. The window is the total time
  5388. + * in us that will be considered one sample period. Conceptually, windows
  5389. + * occur back-to-back and contain a sample width period during which
  5390. + * actual sampling occurs. Can be used to write a new total window size. It
  5391. + * is enfoced that any value written must be greater than the sample width
  5392. + * size, or an error results.
  5393. + */
  5394. +static ssize_t debug_window_fwrite(struct file *filp,
  5395. + const char __user *ubuf,
  5396. + size_t cnt,
  5397. + loff_t *ppos)
  5398. +{
  5399. + char buf[U64STR_SIZE];
  5400. + int csize = min(cnt, sizeof(buf));
  5401. + u64 val = 0;
  5402. + int err = 0;
  5403. +
  5404. + memset(buf, '\0', sizeof(buf));
  5405. + if (copy_from_user(buf, ubuf, csize))
  5406. + return -EFAULT;
  5407. +
  5408. + buf[U64STR_SIZE-1] = '\0'; /* just in case */
  5409. + err = kstrtoull(buf, 10, &val);
  5410. + if (err)
  5411. + return -EINVAL;
  5412. +
  5413. + mutex_lock(&data.lock);
  5414. + if (data.sample_width < val)
  5415. + data.sample_window = val;
  5416. + else {
  5417. + mutex_unlock(&data.lock);
  5418. + return -EINVAL;
  5419. + }
  5420. + mutex_unlock(&data.lock);
  5421. +
  5422. + return csize;
  5423. +}
  5424. +
  5425. +/*
  5426. + * Function pointers for the "count" debugfs file operations
  5427. + */
  5428. +static const struct file_operations count_fops = {
  5429. + .open = debug_count_fopen,
  5430. + .read = debug_count_fread,
  5431. + .write = debug_count_fwrite,
  5432. + .owner = THIS_MODULE,
  5433. +};
  5434. +
  5435. +/*
  5436. + * Function pointers for the "enable" debugfs file operations
  5437. + */
  5438. +static const struct file_operations enable_fops = {
  5439. + .open = debug_enable_fopen,
  5440. + .read = debug_enable_fread,
  5441. + .write = debug_enable_fwrite,
  5442. + .owner = THIS_MODULE,
  5443. +};
  5444. +
  5445. +/*
  5446. + * Function pointers for the "max" debugfs file operations
  5447. + */
  5448. +static const struct file_operations max_fops = {
  5449. + .open = debug_max_fopen,
  5450. + .read = debug_max_fread,
  5451. + .write = debug_max_fwrite,
  5452. + .owner = THIS_MODULE,
  5453. +};
  5454. +
  5455. +/*
  5456. + * Function pointers for the "sample" debugfs file operations
  5457. + */
  5458. +static const struct file_operations sample_fops = {
  5459. + .open = debug_sample_fopen,
  5460. + .read = debug_sample_fread,
  5461. + .release = debug_sample_release,
  5462. + .owner = THIS_MODULE,
  5463. +};
  5464. +
  5465. +/*
  5466. + * Function pointers for the "threshold" debugfs file operations
  5467. + */
  5468. +static const struct file_operations threshold_fops = {
  5469. + .open = debug_threshold_fopen,
  5470. + .read = debug_threshold_fread,
  5471. + .write = debug_threshold_fwrite,
  5472. + .owner = THIS_MODULE,
  5473. +};
  5474. +
  5475. +/*
  5476. + * Function pointers for the "width" debugfs file operations
  5477. + */
  5478. +static const struct file_operations width_fops = {
  5479. + .open = debug_width_fopen,
  5480. + .read = debug_width_fread,
  5481. + .write = debug_width_fwrite,
  5482. + .owner = THIS_MODULE,
  5483. +};
  5484. +
  5485. +/*
  5486. + * Function pointers for the "window" debugfs file operations
  5487. + */
  5488. +static const struct file_operations window_fops = {
  5489. + .open = debug_window_fopen,
  5490. + .read = debug_window_fread,
  5491. + .write = debug_window_fwrite,
  5492. + .owner = THIS_MODULE,
  5493. +};
  5494. +
  5495. +/**
  5496. + * init_debugfs - A function to initialize the debugfs interface files
  5497. + *
  5498. + * This function creates entries in debugfs for "hwlat_detector", including
  5499. + * files to read values from the detector, current samples, and the
  5500. + * maximum sample that has been captured since the hardware latency
  5501. + * dectector was started.
  5502. + */
  5503. +static int init_debugfs(void)
  5504. +{
  5505. + int ret = -ENOMEM;
  5506. +
  5507. + debug_dir = debugfs_create_dir(DRVNAME, NULL);
  5508. + if (!debug_dir)
  5509. + goto err_debug_dir;
  5510. +
  5511. + debug_sample = debugfs_create_file("sample", 0444,
  5512. + debug_dir, NULL,
  5513. + &sample_fops);
  5514. + if (!debug_sample)
  5515. + goto err_sample;
  5516. +
  5517. + debug_count = debugfs_create_file("count", 0444,
  5518. + debug_dir, NULL,
  5519. + &count_fops);
  5520. + if (!debug_count)
  5521. + goto err_count;
  5522. +
  5523. + debug_max = debugfs_create_file("max", 0444,
  5524. + debug_dir, NULL,
  5525. + &max_fops);
  5526. + if (!debug_max)
  5527. + goto err_max;
  5528. +
  5529. + debug_sample_window = debugfs_create_file("window", 0644,
  5530. + debug_dir, NULL,
  5531. + &window_fops);
  5532. + if (!debug_sample_window)
  5533. + goto err_window;
  5534. +
  5535. + debug_sample_width = debugfs_create_file("width", 0644,
  5536. + debug_dir, NULL,
  5537. + &width_fops);
  5538. + if (!debug_sample_width)
  5539. + goto err_width;
  5540. +
  5541. + debug_threshold = debugfs_create_file("threshold", 0644,
  5542. + debug_dir, NULL,
  5543. + &threshold_fops);
  5544. + if (!debug_threshold)
  5545. + goto err_threshold;
  5546. +
  5547. + debug_enable = debugfs_create_file("enable", 0644,
  5548. + debug_dir, &enabled,
  5549. + &enable_fops);
  5550. + if (!debug_enable)
  5551. + goto err_enable;
  5552. +
  5553. + else {
  5554. + ret = 0;
  5555. + goto out;
  5556. + }
  5557. +
  5558. +err_enable:
  5559. + debugfs_remove(debug_threshold);
  5560. +err_threshold:
  5561. + debugfs_remove(debug_sample_width);
  5562. +err_width:
  5563. + debugfs_remove(debug_sample_window);
  5564. +err_window:
  5565. + debugfs_remove(debug_max);
  5566. +err_max:
  5567. + debugfs_remove(debug_count);
  5568. +err_count:
  5569. + debugfs_remove(debug_sample);
  5570. +err_sample:
  5571. + debugfs_remove(debug_dir);
  5572. +err_debug_dir:
  5573. +out:
  5574. + return ret;
  5575. +}
  5576. +
  5577. +/**
  5578. + * free_debugfs - A function to cleanup the debugfs file interface
  5579. + */
  5580. +static void free_debugfs(void)
  5581. +{
  5582. + /* could also use a debugfs_remove_recursive */
  5583. + debugfs_remove(debug_enable);
  5584. + debugfs_remove(debug_threshold);
  5585. + debugfs_remove(debug_sample_width);
  5586. + debugfs_remove(debug_sample_window);
  5587. + debugfs_remove(debug_max);
  5588. + debugfs_remove(debug_count);
  5589. + debugfs_remove(debug_sample);
  5590. + debugfs_remove(debug_dir);
  5591. +}
  5592. +
  5593. +/**
  5594. + * detector_init - Standard module initialization code
  5595. + */
  5596. +static int detector_init(void)
  5597. +{
  5598. + int ret = -ENOMEM;
  5599. +
  5600. + pr_info(BANNER "version %s\n", VERSION);
  5601. +
  5602. + ret = init_stats();
  5603. + if (ret)
  5604. + goto out;
  5605. +
  5606. + ret = init_debugfs();
  5607. + if (ret)
  5608. + goto err_stats;
  5609. +
  5610. + if (enabled)
  5611. + ret = start_kthread();
  5612. +
  5613. + goto out;
  5614. +
  5615. +err_stats:
  5616. + ring_buffer_free(ring_buffer);
  5617. +out:
  5618. + return ret;
  5619. +
  5620. +}
  5621. +
  5622. +/**
  5623. + * detector_exit - Standard module cleanup code
  5624. + */
  5625. +static void detector_exit(void)
  5626. +{
  5627. + int err;
  5628. +
  5629. + if (enabled) {
  5630. + enabled = 0;
  5631. + err = stop_kthread();
  5632. + if (err)
  5633. + pr_err(BANNER "cannot stop kthread\n");
  5634. + }
  5635. +
  5636. + free_debugfs();
  5637. + ring_buffer_free(ring_buffer); /* free up the ring buffer */
  5638. +
  5639. +}
  5640. +
  5641. +module_init(detector_init);
  5642. +module_exit(detector_exit);
  5643. diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
  5644. index df990bb8c873..1a162709a85e 100644
  5645. --- a/drivers/mmc/host/mmci.c
  5646. +++ b/drivers/mmc/host/mmci.c
  5647. @@ -1147,15 +1147,12 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  5648. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  5649. struct variant_data *variant = host->variant;
  5650. void __iomem *base = host->base;
  5651. - unsigned long flags;
  5652. u32 status;
  5653. status = readl(base + MMCISTATUS);
  5654. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  5655. - local_irq_save(flags);
  5656. -
  5657. do {
  5658. unsigned int remain, len;
  5659. char *buffer;
  5660. @@ -1195,8 +1192,6 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  5661. sg_miter_stop(sg_miter);
  5662. - local_irq_restore(flags);
  5663. -
  5664. /*
  5665. * If we have less than the fifo 'half-full' threshold to transfer,
  5666. * trigger a PIO interrupt as soon as any data is available.
  5667. diff --git a/drivers/net/ethernet/3com/3c59x.c b/drivers/net/ethernet/3com/3c59x.c
  5668. index 25c55ab05c7d..5a1d117a8744 100644
  5669. --- a/drivers/net/ethernet/3com/3c59x.c
  5670. +++ b/drivers/net/ethernet/3com/3c59x.c
  5671. @@ -842,9 +842,9 @@ static void poll_vortex(struct net_device *dev)
  5672. {
  5673. struct vortex_private *vp = netdev_priv(dev);
  5674. unsigned long flags;
  5675. - local_irq_save(flags);
  5676. + local_irq_save_nort(flags);
  5677. (vp->full_bus_master_rx ? boomerang_interrupt:vortex_interrupt)(dev->irq,dev);
  5678. - local_irq_restore(flags);
  5679. + local_irq_restore_nort(flags);
  5680. }
  5681. #endif
  5682. @@ -1910,12 +1910,12 @@ static void vortex_tx_timeout(struct net_device *dev)
  5683. * Block interrupts because vortex_interrupt does a bare spin_lock()
  5684. */
  5685. unsigned long flags;
  5686. - local_irq_save(flags);
  5687. + local_irq_save_nort(flags);
  5688. if (vp->full_bus_master_tx)
  5689. boomerang_interrupt(dev->irq, dev);
  5690. else
  5691. vortex_interrupt(dev->irq, dev);
  5692. - local_irq_restore(flags);
  5693. + local_irq_restore_nort(flags);
  5694. }
  5695. }
  5696. diff --git a/drivers/net/ethernet/realtek/8139too.c b/drivers/net/ethernet/realtek/8139too.c
  5697. index da4c2d8a4173..1420dfb56bac 100644
  5698. --- a/drivers/net/ethernet/realtek/8139too.c
  5699. +++ b/drivers/net/ethernet/realtek/8139too.c
  5700. @@ -2233,7 +2233,7 @@ static void rtl8139_poll_controller(struct net_device *dev)
  5701. struct rtl8139_private *tp = netdev_priv(dev);
  5702. const int irq = tp->pci_dev->irq;
  5703. - disable_irq(irq);
  5704. + disable_irq_nosync(irq);
  5705. rtl8139_interrupt(irq, dev);
  5706. enable_irq(irq);
  5707. }
  5708. diff --git a/drivers/net/wireless/intersil/orinoco/orinoco_usb.c b/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
  5709. index 56f109bc8394..02afc796bc71 100644
  5710. --- a/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
  5711. +++ b/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
  5712. @@ -697,7 +697,7 @@ static void ezusb_req_ctx_wait(struct ezusb_priv *upriv,
  5713. while (!ctx->done.done && msecs--)
  5714. udelay(1000);
  5715. } else {
  5716. - wait_event_interruptible(ctx->done.wait,
  5717. + swait_event_interruptible(ctx->done.wait,
  5718. ctx->done.done);
  5719. }
  5720. break;
  5721. diff --git a/drivers/pci/access.c b/drivers/pci/access.c
  5722. index d11cdbb8fba3..223bbb9acb03 100644
  5723. --- a/drivers/pci/access.c
  5724. +++ b/drivers/pci/access.c
  5725. @@ -672,7 +672,7 @@ void pci_cfg_access_unlock(struct pci_dev *dev)
  5726. WARN_ON(!dev->block_cfg_access);
  5727. dev->block_cfg_access = 0;
  5728. - wake_up_all(&pci_cfg_wait);
  5729. + wake_up_all_locked(&pci_cfg_wait);
  5730. raw_spin_unlock_irqrestore(&pci_lock, flags);
  5731. }
  5732. EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
  5733. diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
  5734. index 9bd41a35a78a..8e2d436c2e3f 100644
  5735. --- a/drivers/scsi/fcoe/fcoe.c
  5736. +++ b/drivers/scsi/fcoe/fcoe.c
  5737. @@ -1455,11 +1455,11 @@ static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
  5738. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  5739. {
  5740. struct fcoe_percpu_s *fps;
  5741. - int rc;
  5742. + int rc, cpu = get_cpu_light();
  5743. - fps = &get_cpu_var(fcoe_percpu);
  5744. + fps = &per_cpu(fcoe_percpu, cpu);
  5745. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  5746. - put_cpu_var(fcoe_percpu);
  5747. + put_cpu_light();
  5748. return rc;
  5749. }
  5750. @@ -1646,11 +1646,11 @@ static inline int fcoe_filter_frames(struct fc_lport *lport,
  5751. return 0;
  5752. }
  5753. - stats = per_cpu_ptr(lport->stats, get_cpu());
  5754. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  5755. stats->InvalidCRCCount++;
  5756. if (stats->InvalidCRCCount < 5)
  5757. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  5758. - put_cpu();
  5759. + put_cpu_light();
  5760. return -EINVAL;
  5761. }
  5762. @@ -1693,7 +1693,7 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  5763. */
  5764. hp = (struct fcoe_hdr *) skb_network_header(skb);
  5765. - stats = per_cpu_ptr(lport->stats, get_cpu());
  5766. + stats = per_cpu_ptr(lport->stats, get_cpu_light());
  5767. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  5768. if (stats->ErrorFrames < 5)
  5769. printk(KERN_WARNING "fcoe: FCoE version "
  5770. @@ -1725,13 +1725,13 @@ static void fcoe_recv_frame(struct sk_buff *skb)
  5771. goto drop;
  5772. if (!fcoe_filter_frames(lport, fp)) {
  5773. - put_cpu();
  5774. + put_cpu_light();
  5775. fc_exch_recv(lport, fp);
  5776. return;
  5777. }
  5778. drop:
  5779. stats->ErrorFrames++;
  5780. - put_cpu();
  5781. + put_cpu_light();
  5782. kfree_skb(skb);
  5783. }
  5784. diff --git a/drivers/scsi/fcoe/fcoe_ctlr.c b/drivers/scsi/fcoe/fcoe_ctlr.c
  5785. index dcf36537a767..1a1f2e46452c 100644
  5786. --- a/drivers/scsi/fcoe/fcoe_ctlr.c
  5787. +++ b/drivers/scsi/fcoe/fcoe_ctlr.c
  5788. @@ -834,7 +834,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  5789. INIT_LIST_HEAD(&del_list);
  5790. - stats = per_cpu_ptr(fip->lp->stats, get_cpu());
  5791. + stats = per_cpu_ptr(fip->lp->stats, get_cpu_light());
  5792. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  5793. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  5794. @@ -870,7 +870,7 @@ static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  5795. sel_time = fcf->time;
  5796. }
  5797. }
  5798. - put_cpu();
  5799. + put_cpu_light();
  5800. list_for_each_entry_safe(fcf, next, &del_list, list) {
  5801. /* Removes fcf from current list */
  5802. diff --git a/drivers/scsi/libfc/fc_exch.c b/drivers/scsi/libfc/fc_exch.c
  5803. index e72673b0a8fb..da598a6caa22 100644
  5804. --- a/drivers/scsi/libfc/fc_exch.c
  5805. +++ b/drivers/scsi/libfc/fc_exch.c
  5806. @@ -814,10 +814,10 @@ static struct fc_exch *fc_exch_em_alloc(struct fc_lport *lport,
  5807. }
  5808. memset(ep, 0, sizeof(*ep));
  5809. - cpu = get_cpu();
  5810. + cpu = get_cpu_light();
  5811. pool = per_cpu_ptr(mp->pool, cpu);
  5812. spin_lock_bh(&pool->lock);
  5813. - put_cpu();
  5814. + put_cpu_light();
  5815. /* peek cache of free slot */
  5816. if (pool->left != FC_XID_UNKNOWN) {
  5817. diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c
  5818. index 763f012fdeca..d0f61b595470 100644
  5819. --- a/drivers/scsi/libsas/sas_ata.c
  5820. +++ b/drivers/scsi/libsas/sas_ata.c
  5821. @@ -190,7 +190,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  5822. /* TODO: audit callers to ensure they are ready for qc_issue to
  5823. * unconditionally re-enable interrupts
  5824. */
  5825. - local_irq_save(flags);
  5826. + local_irq_save_nort(flags);
  5827. spin_unlock(ap->lock);
  5828. /* If the device fell off, no sense in issuing commands */
  5829. @@ -252,7 +252,7 @@ static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  5830. out:
  5831. spin_lock(ap->lock);
  5832. - local_irq_restore(flags);
  5833. + local_irq_restore_nort(flags);
  5834. return ret;
  5835. }
  5836. diff --git a/drivers/scsi/qla2xxx/qla_inline.h b/drivers/scsi/qla2xxx/qla_inline.h
  5837. index edc48f3b8230..ee5c6f9dfb6f 100644
  5838. --- a/drivers/scsi/qla2xxx/qla_inline.h
  5839. +++ b/drivers/scsi/qla2xxx/qla_inline.h
  5840. @@ -59,12 +59,12 @@ qla2x00_poll(struct rsp_que *rsp)
  5841. {
  5842. unsigned long flags;
  5843. struct qla_hw_data *ha = rsp->hw;
  5844. - local_irq_save(flags);
  5845. + local_irq_save_nort(flags);
  5846. if (IS_P3P_TYPE(ha))
  5847. qla82xx_poll(0, rsp);
  5848. else
  5849. ha->isp_ops->intr_handler(0, rsp);
  5850. - local_irq_restore(flags);
  5851. + local_irq_restore_nort(flags);
  5852. }
  5853. static inline uint8_t *
  5854. diff --git a/drivers/scsi/qla2xxx/qla_isr.c b/drivers/scsi/qla2xxx/qla_isr.c
  5855. index 987f1c729e9c..18391e07d70f 100644
  5856. --- a/drivers/scsi/qla2xxx/qla_isr.c
  5857. +++ b/drivers/scsi/qla2xxx/qla_isr.c
  5858. @@ -3125,7 +3125,11 @@ qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
  5859. * kref_put().
  5860. */
  5861. kref_get(&qentry->irq_notify.kref);
  5862. +#ifdef CONFIG_PREEMPT_RT_BASE
  5863. + swork_queue(&qentry->irq_notify.swork);
  5864. +#else
  5865. schedule_work(&qentry->irq_notify.work);
  5866. +#endif
  5867. }
  5868. /*
  5869. diff --git a/drivers/thermal/x86_pkg_temp_thermal.c b/drivers/thermal/x86_pkg_temp_thermal.c
  5870. index 97f0a2bd93ed..a4f45aaa9ad4 100644
  5871. --- a/drivers/thermal/x86_pkg_temp_thermal.c
  5872. +++ b/drivers/thermal/x86_pkg_temp_thermal.c
  5873. @@ -29,6 +29,7 @@
  5874. #include <linux/pm.h>
  5875. #include <linux/thermal.h>
  5876. #include <linux/debugfs.h>
  5877. +#include <linux/swork.h>
  5878. #include <asm/cpu_device_id.h>
  5879. #include <asm/mce.h>
  5880. @@ -352,7 +353,7 @@ static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  5881. }
  5882. }
  5883. -static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  5884. +static void platform_thermal_notify_work(struct swork_event *event)
  5885. {
  5886. unsigned long flags;
  5887. int cpu = smp_processor_id();
  5888. @@ -369,7 +370,7 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  5889. pkg_work_scheduled[phy_id]) {
  5890. disable_pkg_thres_interrupt();
  5891. spin_unlock_irqrestore(&pkg_work_lock, flags);
  5892. - return -EINVAL;
  5893. + return;
  5894. }
  5895. pkg_work_scheduled[phy_id] = 1;
  5896. spin_unlock_irqrestore(&pkg_work_lock, flags);
  5897. @@ -378,9 +379,48 @@ static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  5898. schedule_delayed_work_on(cpu,
  5899. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  5900. msecs_to_jiffies(notify_delay_ms));
  5901. +}
  5902. +
  5903. +#ifdef CONFIG_PREEMPT_RT_FULL
  5904. +static struct swork_event notify_work;
  5905. +
  5906. +static int thermal_notify_work_init(void)
  5907. +{
  5908. + int err;
  5909. +
  5910. + err = swork_get();
  5911. + if (err)
  5912. + return err;
  5913. +
  5914. + INIT_SWORK(&notify_work, platform_thermal_notify_work);
  5915. return 0;
  5916. }
  5917. +static void thermal_notify_work_cleanup(void)
  5918. +{
  5919. + swork_put();
  5920. +}
  5921. +
  5922. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  5923. +{
  5924. + swork_queue(&notify_work);
  5925. + return 0;
  5926. +}
  5927. +
  5928. +#else /* !CONFIG_PREEMPT_RT_FULL */
  5929. +
  5930. +static int thermal_notify_work_init(void) { return 0; }
  5931. +
  5932. +static void thermal_notify_work_cleanup(void) { }
  5933. +
  5934. +static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  5935. +{
  5936. + platform_thermal_notify_work(NULL);
  5937. +
  5938. + return 0;
  5939. +}
  5940. +#endif /* CONFIG_PREEMPT_RT_FULL */
  5941. +
  5942. static int find_siblings_cpu(int cpu)
  5943. {
  5944. int i;
  5945. @@ -584,6 +624,9 @@ static int __init pkg_temp_thermal_init(void)
  5946. if (!x86_match_cpu(pkg_temp_thermal_ids))
  5947. return -ENODEV;
  5948. + if (!thermal_notify_work_init())
  5949. + return -ENODEV;
  5950. +
  5951. spin_lock_init(&pkg_work_lock);
  5952. platform_thermal_package_notify =
  5953. pkg_temp_thermal_platform_thermal_notify;
  5954. @@ -608,7 +651,7 @@ static int __init pkg_temp_thermal_init(void)
  5955. kfree(pkg_work_scheduled);
  5956. platform_thermal_package_notify = NULL;
  5957. platform_thermal_package_rate_control = NULL;
  5958. -
  5959. + thermal_notify_work_cleanup();
  5960. return -ENODEV;
  5961. }
  5962. @@ -633,6 +676,7 @@ static void __exit pkg_temp_thermal_exit(void)
  5963. mutex_unlock(&phy_dev_list_mutex);
  5964. platform_thermal_package_notify = NULL;
  5965. platform_thermal_package_rate_control = NULL;
  5966. + thermal_notify_work_cleanup();
  5967. for_each_online_cpu(i)
  5968. cancel_delayed_work_sync(
  5969. &per_cpu(pkg_temp_thermal_threshold_work, i));
  5970. diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
  5971. index dcf43f66404f..a9ae57122841 100644
  5972. --- a/drivers/tty/serial/8250/8250_core.c
  5973. +++ b/drivers/tty/serial/8250/8250_core.c
  5974. @@ -58,7 +58,16 @@ static struct uart_driver serial8250_reg;
  5975. static unsigned int skip_txen_test; /* force skip of txen test at init time */
  5976. -#define PASS_LIMIT 512
  5977. +/*
  5978. + * On -rt we can have a more delays, and legitimately
  5979. + * so - so don't drop work spuriously and spam the
  5980. + * syslog:
  5981. + */
  5982. +#ifdef CONFIG_PREEMPT_RT_FULL
  5983. +# define PASS_LIMIT 1000000
  5984. +#else
  5985. +# define PASS_LIMIT 512
  5986. +#endif
  5987. #include <asm/serial.h>
  5988. /*
  5989. diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
  5990. index 858a54633664..fc44fb59aef6 100644
  5991. --- a/drivers/tty/serial/8250/8250_port.c
  5992. +++ b/drivers/tty/serial/8250/8250_port.c
  5993. @@ -35,6 +35,7 @@
  5994. #include <linux/nmi.h>
  5995. #include <linux/mutex.h>
  5996. #include <linux/slab.h>
  5997. +#include <linux/kdb.h>
  5998. #include <linux/uaccess.h>
  5999. #include <linux/pm_runtime.h>
  6000. #include <linux/timer.h>
  6001. @@ -3109,9 +3110,9 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
  6002. serial8250_rpm_get(up);
  6003. - if (port->sysrq)
  6004. + if (port->sysrq || oops_in_progress)
  6005. locked = 0;
  6006. - else if (oops_in_progress)
  6007. + else if (in_kdb_printk())
  6008. locked = spin_trylock_irqsave(&port->lock, flags);
  6009. else
  6010. spin_lock_irqsave(&port->lock, flags);
  6011. diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c
  6012. index 8a9e213387a7..dd1f9a426b74 100644
  6013. --- a/drivers/tty/serial/amba-pl011.c
  6014. +++ b/drivers/tty/serial/amba-pl011.c
  6015. @@ -2167,13 +2167,19 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  6016. clk_enable(uap->clk);
  6017. - local_irq_save(flags);
  6018. + /*
  6019. + * local_irq_save(flags);
  6020. + *
  6021. + * This local_irq_save() is nonsense. If we come in via sysrq
  6022. + * handling then interrupts are already disabled. Aside of
  6023. + * that the port.sysrq check is racy on SMP regardless.
  6024. + */
  6025. if (uap->port.sysrq)
  6026. locked = 0;
  6027. else if (oops_in_progress)
  6028. - locked = spin_trylock(&uap->port.lock);
  6029. + locked = spin_trylock_irqsave(&uap->port.lock, flags);
  6030. else
  6031. - spin_lock(&uap->port.lock);
  6032. + spin_lock_irqsave(&uap->port.lock, flags);
  6033. /*
  6034. * First save the CR then disable the interrupts
  6035. @@ -2197,8 +2203,7 @@ pl011_console_write(struct console *co, const char *s, unsigned int count)
  6036. pl011_write(old_cr, uap, REG_CR);
  6037. if (locked)
  6038. - spin_unlock(&uap->port.lock);
  6039. - local_irq_restore(flags);
  6040. + spin_unlock_irqrestore(&uap->port.lock, flags);
  6041. clk_disable(uap->clk);
  6042. }
  6043. diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c
  6044. index a2a529994ba5..0ee7c4c518df 100644
  6045. --- a/drivers/tty/serial/omap-serial.c
  6046. +++ b/drivers/tty/serial/omap-serial.c
  6047. @@ -1257,13 +1257,10 @@ serial_omap_console_write(struct console *co, const char *s,
  6048. pm_runtime_get_sync(up->dev);
  6049. - local_irq_save(flags);
  6050. - if (up->port.sysrq)
  6051. - locked = 0;
  6052. - else if (oops_in_progress)
  6053. - locked = spin_trylock(&up->port.lock);
  6054. + if (up->port.sysrq || oops_in_progress)
  6055. + locked = spin_trylock_irqsave(&up->port.lock, flags);
  6056. else
  6057. - spin_lock(&up->port.lock);
  6058. + spin_lock_irqsave(&up->port.lock, flags);
  6059. /*
  6060. * First save the IER then disable the interrupts
  6061. @@ -1292,8 +1289,7 @@ serial_omap_console_write(struct console *co, const char *s,
  6062. pm_runtime_mark_last_busy(up->dev);
  6063. pm_runtime_put_autosuspend(up->dev);
  6064. if (locked)
  6065. - spin_unlock(&up->port.lock);
  6066. - local_irq_restore(flags);
  6067. + spin_unlock_irqrestore(&up->port.lock, flags);
  6068. }
  6069. static int __init
  6070. diff --git a/drivers/tty/serial/sc16is7xx.c b/drivers/tty/serial/sc16is7xx.c
  6071. index f36e6df2fa90..e086ea4d2997 100644
  6072. --- a/drivers/tty/serial/sc16is7xx.c
  6073. +++ b/drivers/tty/serial/sc16is7xx.c
  6074. @@ -1240,7 +1240,7 @@ static int sc16is7xx_probe(struct device *dev,
  6075. /* Setup interrupt */
  6076. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  6077. - IRQF_ONESHOT | flags, dev_name(dev), s);
  6078. + flags, dev_name(dev), s);
  6079. if (!ret)
  6080. return 0;
  6081. diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
  6082. index d2e3f655c26f..fdd027a9bbd7 100644
  6083. --- a/drivers/usb/core/hcd.c
  6084. +++ b/drivers/usb/core/hcd.c
  6085. @@ -1760,9 +1760,9 @@ static void __usb_hcd_giveback_urb(struct urb *urb)
  6086. * and no one may trigger the above deadlock situation when
  6087. * running complete() in tasklet.
  6088. */
  6089. - local_irq_save(flags);
  6090. + local_irq_save_nort(flags);
  6091. urb->complete(urb);
  6092. - local_irq_restore(flags);
  6093. + local_irq_restore_nort(flags);
  6094. usb_anchor_resume_wakeups(anchor);
  6095. atomic_dec(&urb->use_count);
  6096. diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
  6097. index 5c8429f23a89..fa835fb1a186 100644
  6098. --- a/drivers/usb/gadget/function/f_fs.c
  6099. +++ b/drivers/usb/gadget/function/f_fs.c
  6100. @@ -1509,7 +1509,7 @@ static void ffs_data_put(struct ffs_data *ffs)
  6101. pr_info("%s(): freeing\n", __func__);
  6102. ffs_data_clear(ffs);
  6103. BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
  6104. - waitqueue_active(&ffs->ep0req_completion.wait));
  6105. + swait_active(&ffs->ep0req_completion.wait));
  6106. kfree(ffs->dev_name);
  6107. kfree(ffs);
  6108. }
  6109. diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
  6110. index 16104b5ebdcb..5c506c2b88ad 100644
  6111. --- a/drivers/usb/gadget/legacy/inode.c
  6112. +++ b/drivers/usb/gadget/legacy/inode.c
  6113. @@ -346,7 +346,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  6114. spin_unlock_irq (&epdata->dev->lock);
  6115. if (likely (value == 0)) {
  6116. - value = wait_event_interruptible (done.wait, done.done);
  6117. + value = swait_event_interruptible (done.wait, done.done);
  6118. if (value != 0) {
  6119. spin_lock_irq (&epdata->dev->lock);
  6120. if (likely (epdata->ep != NULL)) {
  6121. @@ -355,7 +355,7 @@ ep_io (struct ep_data *epdata, void *buf, unsigned len)
  6122. usb_ep_dequeue (epdata->ep, epdata->req);
  6123. spin_unlock_irq (&epdata->dev->lock);
  6124. - wait_event (done.wait, done.done);
  6125. + swait_event (done.wait, done.done);
  6126. if (epdata->status == -ECONNRESET)
  6127. epdata->status = -EINTR;
  6128. } else {
  6129. diff --git a/fs/aio.c b/fs/aio.c
  6130. index 4fe81d1c60f9..e68c06a4a017 100644
  6131. --- a/fs/aio.c
  6132. +++ b/fs/aio.c
  6133. @@ -40,6 +40,7 @@
  6134. #include <linux/ramfs.h>
  6135. #include <linux/percpu-refcount.h>
  6136. #include <linux/mount.h>
  6137. +#include <linux/swork.h>
  6138. #include <asm/kmap_types.h>
  6139. #include <asm/uaccess.h>
  6140. @@ -115,7 +116,7 @@ struct kioctx {
  6141. struct page **ring_pages;
  6142. long nr_pages;
  6143. - struct work_struct free_work;
  6144. + struct swork_event free_work;
  6145. /*
  6146. * signals when all in-flight requests are done
  6147. @@ -258,6 +259,7 @@ static int __init aio_setup(void)
  6148. .mount = aio_mount,
  6149. .kill_sb = kill_anon_super,
  6150. };
  6151. + BUG_ON(swork_get());
  6152. aio_mnt = kern_mount(&aio_fs);
  6153. if (IS_ERR(aio_mnt))
  6154. panic("Failed to create aio fs mount.");
  6155. @@ -578,9 +580,9 @@ static int kiocb_cancel(struct aio_kiocb *kiocb)
  6156. return cancel(&kiocb->common);
  6157. }
  6158. -static void free_ioctx(struct work_struct *work)
  6159. +static void free_ioctx(struct swork_event *sev)
  6160. {
  6161. - struct kioctx *ctx = container_of(work, struct kioctx, free_work);
  6162. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  6163. pr_debug("freeing %p\n", ctx);
  6164. @@ -599,8 +601,8 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  6165. if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count))
  6166. complete(&ctx->rq_wait->comp);
  6167. - INIT_WORK(&ctx->free_work, free_ioctx);
  6168. - schedule_work(&ctx->free_work);
  6169. + INIT_SWORK(&ctx->free_work, free_ioctx);
  6170. + swork_queue(&ctx->free_work);
  6171. }
  6172. /*
  6173. @@ -608,9 +610,9 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
  6174. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  6175. * now it's safe to cancel any that need to be.
  6176. */
  6177. -static void free_ioctx_users(struct percpu_ref *ref)
  6178. +static void free_ioctx_users_work(struct swork_event *sev)
  6179. {
  6180. - struct kioctx *ctx = container_of(ref, struct kioctx, users);
  6181. + struct kioctx *ctx = container_of(sev, struct kioctx, free_work);
  6182. struct aio_kiocb *req;
  6183. spin_lock_irq(&ctx->ctx_lock);
  6184. @@ -629,6 +631,14 @@ static void free_ioctx_users(struct percpu_ref *ref)
  6185. percpu_ref_put(&ctx->reqs);
  6186. }
  6187. +static void free_ioctx_users(struct percpu_ref *ref)
  6188. +{
  6189. + struct kioctx *ctx = container_of(ref, struct kioctx, users);
  6190. +
  6191. + INIT_SWORK(&ctx->free_work, free_ioctx_users_work);
  6192. + swork_queue(&ctx->free_work);
  6193. +}
  6194. +
  6195. static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
  6196. {
  6197. unsigned i, new_nr;
  6198. diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
  6199. index a439548de785..7c392647d03b 100644
  6200. --- a/fs/autofs4/autofs_i.h
  6201. +++ b/fs/autofs4/autofs_i.h
  6202. @@ -30,6 +30,7 @@
  6203. #include <linux/sched.h>
  6204. #include <linux/mount.h>
  6205. #include <linux/namei.h>
  6206. +#include <linux/delay.h>
  6207. #include <asm/current.h>
  6208. #include <linux/uaccess.h>
  6209. diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
  6210. index d8e6d421c27f..2e689ab1306b 100644
  6211. --- a/fs/autofs4/expire.c
  6212. +++ b/fs/autofs4/expire.c
  6213. @@ -148,7 +148,7 @@ static struct dentry *get_next_positive_dentry(struct dentry *prev,
  6214. parent = p->d_parent;
  6215. if (!spin_trylock(&parent->d_lock)) {
  6216. spin_unlock(&p->d_lock);
  6217. - cpu_relax();
  6218. + cpu_chill();
  6219. goto relock;
  6220. }
  6221. spin_unlock(&p->d_lock);
  6222. diff --git a/fs/buffer.c b/fs/buffer.c
  6223. index 9c8eb9b6db6a..d15d77f72cf7 100644
  6224. --- a/fs/buffer.c
  6225. +++ b/fs/buffer.c
  6226. @@ -301,8 +301,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6227. * decide that the page is now completely done.
  6228. */
  6229. first = page_buffers(page);
  6230. - local_irq_save(flags);
  6231. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  6232. + flags = bh_uptodate_lock_irqsave(first);
  6233. clear_buffer_async_read(bh);
  6234. unlock_buffer(bh);
  6235. tmp = bh;
  6236. @@ -315,8 +314,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6237. }
  6238. tmp = tmp->b_this_page;
  6239. } while (tmp != bh);
  6240. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6241. - local_irq_restore(flags);
  6242. + bh_uptodate_unlock_irqrestore(first, flags);
  6243. /*
  6244. * If none of the buffers had errors and they are all
  6245. @@ -328,9 +326,7 @@ static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6246. return;
  6247. still_busy:
  6248. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6249. - local_irq_restore(flags);
  6250. - return;
  6251. + bh_uptodate_unlock_irqrestore(first, flags);
  6252. }
  6253. /*
  6254. @@ -358,8 +354,7 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  6255. }
  6256. first = page_buffers(page);
  6257. - local_irq_save(flags);
  6258. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  6259. + flags = bh_uptodate_lock_irqsave(first);
  6260. clear_buffer_async_write(bh);
  6261. unlock_buffer(bh);
  6262. @@ -371,15 +366,12 @@ void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  6263. }
  6264. tmp = tmp->b_this_page;
  6265. }
  6266. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6267. - local_irq_restore(flags);
  6268. + bh_uptodate_unlock_irqrestore(first, flags);
  6269. end_page_writeback(page);
  6270. return;
  6271. still_busy:
  6272. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6273. - local_irq_restore(flags);
  6274. - return;
  6275. + bh_uptodate_unlock_irqrestore(first, flags);
  6276. }
  6277. EXPORT_SYMBOL(end_buffer_async_write);
  6278. @@ -3384,6 +3376,7 @@ struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
  6279. struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
  6280. if (ret) {
  6281. INIT_LIST_HEAD(&ret->b_assoc_buffers);
  6282. + buffer_head_init_locks(ret);
  6283. preempt_disable();
  6284. __this_cpu_inc(bh_accounting.nr);
  6285. recalc_bh_state();
  6286. diff --git a/fs/cifs/readdir.c b/fs/cifs/readdir.c
  6287. index 8f6a2a5863b9..4217828d0b68 100644
  6288. --- a/fs/cifs/readdir.c
  6289. +++ b/fs/cifs/readdir.c
  6290. @@ -80,7 +80,7 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name,
  6291. struct inode *inode;
  6292. struct super_block *sb = parent->d_sb;
  6293. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  6294. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6295. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6296. cifs_dbg(FYI, "%s: for %s\n", __func__, name->name);
  6297. diff --git a/fs/dcache.c b/fs/dcache.c
  6298. index 5c7cc953ac81..a9bb31f1c1af 100644
  6299. --- a/fs/dcache.c
  6300. +++ b/fs/dcache.c
  6301. @@ -19,6 +19,7 @@
  6302. #include <linux/mm.h>
  6303. #include <linux/fs.h>
  6304. #include <linux/fsnotify.h>
  6305. +#include <linux/delay.h>
  6306. #include <linux/slab.h>
  6307. #include <linux/init.h>
  6308. #include <linux/hash.h>
  6309. @@ -750,6 +751,8 @@ static inline bool fast_dput(struct dentry *dentry)
  6310. */
  6311. void dput(struct dentry *dentry)
  6312. {
  6313. + struct dentry *parent;
  6314. +
  6315. if (unlikely(!dentry))
  6316. return;
  6317. @@ -788,9 +791,18 @@ void dput(struct dentry *dentry)
  6318. return;
  6319. kill_it:
  6320. - dentry = dentry_kill(dentry);
  6321. - if (dentry) {
  6322. - cond_resched();
  6323. + parent = dentry_kill(dentry);
  6324. + if (parent) {
  6325. + int r;
  6326. +
  6327. + if (parent == dentry) {
  6328. + /* the task with the highest priority won't schedule */
  6329. + r = cond_resched();
  6330. + if (!r)
  6331. + cpu_chill();
  6332. + } else {
  6333. + dentry = parent;
  6334. + }
  6335. goto repeat;
  6336. }
  6337. }
  6338. @@ -2321,7 +2333,7 @@ void d_delete(struct dentry * dentry)
  6339. if (dentry->d_lockref.count == 1) {
  6340. if (!spin_trylock(&inode->i_lock)) {
  6341. spin_unlock(&dentry->d_lock);
  6342. - cpu_relax();
  6343. + cpu_chill();
  6344. goto again;
  6345. }
  6346. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  6347. @@ -2381,21 +2393,24 @@ static inline void end_dir_add(struct inode *dir, unsigned n)
  6348. static void d_wait_lookup(struct dentry *dentry)
  6349. {
  6350. - if (d_in_lookup(dentry)) {
  6351. - DECLARE_WAITQUEUE(wait, current);
  6352. - add_wait_queue(dentry->d_wait, &wait);
  6353. - do {
  6354. - set_current_state(TASK_UNINTERRUPTIBLE);
  6355. - spin_unlock(&dentry->d_lock);
  6356. - schedule();
  6357. - spin_lock(&dentry->d_lock);
  6358. - } while (d_in_lookup(dentry));
  6359. - }
  6360. + struct swait_queue __wait;
  6361. +
  6362. + if (!d_in_lookup(dentry))
  6363. + return;
  6364. +
  6365. + INIT_LIST_HEAD(&__wait.task_list);
  6366. + do {
  6367. + prepare_to_swait(dentry->d_wait, &__wait, TASK_UNINTERRUPTIBLE);
  6368. + spin_unlock(&dentry->d_lock);
  6369. + schedule();
  6370. + spin_lock(&dentry->d_lock);
  6371. + } while (d_in_lookup(dentry));
  6372. + finish_swait(dentry->d_wait, &__wait);
  6373. }
  6374. struct dentry *d_alloc_parallel(struct dentry *parent,
  6375. const struct qstr *name,
  6376. - wait_queue_head_t *wq)
  6377. + struct swait_queue_head *wq)
  6378. {
  6379. unsigned int hash = name->hash;
  6380. struct hlist_bl_head *b = in_lookup_hash(parent, hash);
  6381. @@ -2504,7 +2519,7 @@ void __d_lookup_done(struct dentry *dentry)
  6382. hlist_bl_lock(b);
  6383. dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
  6384. __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
  6385. - wake_up_all(dentry->d_wait);
  6386. + swake_up_all(dentry->d_wait);
  6387. dentry->d_wait = NULL;
  6388. hlist_bl_unlock(b);
  6389. INIT_HLIST_NODE(&dentry->d_u.d_alias);
  6390. @@ -3601,6 +3616,11 @@ EXPORT_SYMBOL(d_genocide);
  6391. void __init vfs_caches_init_early(void)
  6392. {
  6393. + int i;
  6394. +
  6395. + for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
  6396. + INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
  6397. +
  6398. dcache_init_early();
  6399. inode_init_early();
  6400. }
  6401. diff --git a/fs/eventpoll.c b/fs/eventpoll.c
  6402. index 10db91218933..42af0a06f657 100644
  6403. --- a/fs/eventpoll.c
  6404. +++ b/fs/eventpoll.c
  6405. @@ -510,12 +510,12 @@ static int ep_poll_wakeup_proc(void *priv, void *cookie, int call_nests)
  6406. */
  6407. static void ep_poll_safewake(wait_queue_head_t *wq)
  6408. {
  6409. - int this_cpu = get_cpu();
  6410. + int this_cpu = get_cpu_light();
  6411. ep_call_nested(&poll_safewake_ncalls, EP_MAX_NESTS,
  6412. ep_poll_wakeup_proc, NULL, wq, (void *) (long) this_cpu);
  6413. - put_cpu();
  6414. + put_cpu_light();
  6415. }
  6416. static void ep_remove_wait_queue(struct eppoll_entry *pwq)
  6417. diff --git a/fs/exec.c b/fs/exec.c
  6418. index 6fcfb3f7b137..751370a71ec5 100644
  6419. --- a/fs/exec.c
  6420. +++ b/fs/exec.c
  6421. @@ -1012,12 +1012,14 @@ static int exec_mmap(struct mm_struct *mm)
  6422. }
  6423. }
  6424. task_lock(tsk);
  6425. + preempt_disable_rt();
  6426. active_mm = tsk->active_mm;
  6427. tsk->mm = mm;
  6428. tsk->active_mm = mm;
  6429. activate_mm(active_mm, mm);
  6430. tsk->mm->vmacache_seqnum = 0;
  6431. vmacache_flush(tsk);
  6432. + preempt_enable_rt();
  6433. task_unlock(tsk);
  6434. if (old_mm) {
  6435. up_read(&old_mm->mmap_sem);
  6436. diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
  6437. index 4ff9251e9d3a..8fe489ec2ef1 100644
  6438. --- a/fs/fuse/dir.c
  6439. +++ b/fs/fuse/dir.c
  6440. @@ -1174,7 +1174,7 @@ static int fuse_direntplus_link(struct file *file,
  6441. struct inode *dir = d_inode(parent);
  6442. struct fuse_conn *fc;
  6443. struct inode *inode;
  6444. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6445. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6446. if (!o->nodeid) {
  6447. /*
  6448. diff --git a/fs/jbd2/checkpoint.c b/fs/jbd2/checkpoint.c
  6449. index 684996c8a3a4..6e18a06aaabe 100644
  6450. --- a/fs/jbd2/checkpoint.c
  6451. +++ b/fs/jbd2/checkpoint.c
  6452. @@ -116,6 +116,8 @@ void __jbd2_log_wait_for_space(journal_t *journal)
  6453. nblocks = jbd2_space_needed(journal);
  6454. while (jbd2_log_space_left(journal) < nblocks) {
  6455. write_unlock(&journal->j_state_lock);
  6456. + if (current->plug)
  6457. + io_schedule();
  6458. mutex_lock(&journal->j_checkpoint_mutex);
  6459. /*
  6460. diff --git a/fs/namei.c b/fs/namei.c
  6461. index adb04146df09..a89dfaf9f209 100644
  6462. --- a/fs/namei.c
  6463. +++ b/fs/namei.c
  6464. @@ -1629,7 +1629,7 @@ static struct dentry *lookup_slow(const struct qstr *name,
  6465. {
  6466. struct dentry *dentry = ERR_PTR(-ENOENT), *old;
  6467. struct inode *inode = dir->d_inode;
  6468. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6469. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6470. inode_lock_shared(inode);
  6471. /* Don't go there if it's already dead */
  6472. @@ -3086,7 +3086,7 @@ static int lookup_open(struct nameidata *nd, struct path *path,
  6473. struct dentry *dentry;
  6474. int error, create_error = 0;
  6475. umode_t mode = op->mode;
  6476. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6477. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6478. if (unlikely(IS_DEADDIR(dir_inode)))
  6479. return -ENOENT;
  6480. diff --git a/fs/namespace.c b/fs/namespace.c
  6481. index 7bb2cda3bfef..cf79b18e7b58 100644
  6482. --- a/fs/namespace.c
  6483. +++ b/fs/namespace.c
  6484. @@ -14,6 +14,7 @@
  6485. #include <linux/mnt_namespace.h>
  6486. #include <linux/user_namespace.h>
  6487. #include <linux/namei.h>
  6488. +#include <linux/delay.h>
  6489. #include <linux/security.h>
  6490. #include <linux/idr.h>
  6491. #include <linux/init.h> /* init_rootfs */
  6492. @@ -353,8 +354,11 @@ int __mnt_want_write(struct vfsmount *m)
  6493. * incremented count after it has set MNT_WRITE_HOLD.
  6494. */
  6495. smp_mb();
  6496. - while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  6497. - cpu_relax();
  6498. + while (ACCESS_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  6499. + preempt_enable();
  6500. + cpu_chill();
  6501. + preempt_disable();
  6502. + }
  6503. /*
  6504. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  6505. * be set to match its requirements. So we must not load that until
  6506. diff --git a/fs/nfs/delegation.c b/fs/nfs/delegation.c
  6507. index b9c65421ed81..03ffe8af8785 100644
  6508. --- a/fs/nfs/delegation.c
  6509. +++ b/fs/nfs/delegation.c
  6510. @@ -150,11 +150,11 @@ static int nfs_delegation_claim_opens(struct inode *inode,
  6511. sp = state->owner;
  6512. /* Block nfs4_proc_unlck */
  6513. mutex_lock(&sp->so_delegreturn_mutex);
  6514. - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  6515. + seq = read_seqbegin(&sp->so_reclaim_seqlock);
  6516. err = nfs4_open_delegation_recall(ctx, state, stateid, type);
  6517. if (!err)
  6518. err = nfs_delegation_claim_locks(ctx, state, stateid);
  6519. - if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  6520. + if (!err && read_seqretry(&sp->so_reclaim_seqlock, seq))
  6521. err = -EAGAIN;
  6522. mutex_unlock(&sp->so_delegreturn_mutex);
  6523. put_nfs_open_context(ctx);
  6524. diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
  6525. index 6bc5a68e39f1..ce6488e07a13 100644
  6526. --- a/fs/nfs/dir.c
  6527. +++ b/fs/nfs/dir.c
  6528. @@ -485,7 +485,7 @@ static
  6529. void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
  6530. {
  6531. struct qstr filename = QSTR_INIT(entry->name, entry->len);
  6532. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6533. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6534. struct dentry *dentry;
  6535. struct dentry *alias;
  6536. struct inode *dir = d_inode(parent);
  6537. @@ -1490,7 +1490,7 @@ int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
  6538. struct file *file, unsigned open_flags,
  6539. umode_t mode, int *opened)
  6540. {
  6541. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6542. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6543. struct nfs_open_context *ctx;
  6544. struct dentry *res;
  6545. struct iattr attr = { .ia_valid = ATTR_OPEN };
  6546. @@ -1805,7 +1805,11 @@ int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  6547. trace_nfs_rmdir_enter(dir, dentry);
  6548. if (d_really_is_positive(dentry)) {
  6549. +#ifdef CONFIG_PREEMPT_RT_BASE
  6550. + down(&NFS_I(d_inode(dentry))->rmdir_sem);
  6551. +#else
  6552. down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  6553. +#endif
  6554. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  6555. /* Ensure the VFS deletes this inode */
  6556. switch (error) {
  6557. @@ -1815,7 +1819,11 @@ int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  6558. case -ENOENT:
  6559. nfs_dentry_handle_enoent(dentry);
  6560. }
  6561. +#ifdef CONFIG_PREEMPT_RT_BASE
  6562. + up(&NFS_I(d_inode(dentry))->rmdir_sem);
  6563. +#else
  6564. up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
  6565. +#endif
  6566. } else
  6567. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  6568. trace_nfs_rmdir_exit(dir, dentry, error);
  6569. diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
  6570. index bf4ec5ecc97e..36cd5fc9192c 100644
  6571. --- a/fs/nfs/inode.c
  6572. +++ b/fs/nfs/inode.c
  6573. @@ -1957,7 +1957,11 @@ static void init_once(void *foo)
  6574. nfsi->nrequests = 0;
  6575. nfsi->commit_info.ncommit = 0;
  6576. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  6577. +#ifdef CONFIG_PREEMPT_RT_BASE
  6578. + sema_init(&nfsi->rmdir_sem, 1);
  6579. +#else
  6580. init_rwsem(&nfsi->rmdir_sem);
  6581. +#endif
  6582. nfs4_init_once(nfsi);
  6583. }
  6584. diff --git a/fs/nfs/nfs4_fs.h b/fs/nfs/nfs4_fs.h
  6585. index 9bf64eacba5b..041da5cb80f5 100644
  6586. --- a/fs/nfs/nfs4_fs.h
  6587. +++ b/fs/nfs/nfs4_fs.h
  6588. @@ -107,7 +107,7 @@ struct nfs4_state_owner {
  6589. unsigned long so_flags;
  6590. struct list_head so_states;
  6591. struct nfs_seqid_counter so_seqid;
  6592. - seqcount_t so_reclaim_seqcount;
  6593. + seqlock_t so_reclaim_seqlock;
  6594. struct mutex so_delegreturn_mutex;
  6595. };
  6596. diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
  6597. index a9dec32ba9ba..49b64dfb307c 100644
  6598. --- a/fs/nfs/nfs4proc.c
  6599. +++ b/fs/nfs/nfs4proc.c
  6600. @@ -2525,7 +2525,7 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  6601. unsigned int seq;
  6602. int ret;
  6603. - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  6604. + seq = raw_seqcount_begin(&sp->so_reclaim_seqlock.seqcount);
  6605. ret = _nfs4_proc_open(opendata);
  6606. if (ret != 0)
  6607. @@ -2561,7 +2561,7 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  6608. ctx->state = state;
  6609. if (d_inode(dentry) == state->inode) {
  6610. nfs_inode_attach_open_context(ctx);
  6611. - if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  6612. + if (read_seqretry(&sp->so_reclaim_seqlock, seq))
  6613. nfs4_schedule_stateid_recovery(server, state);
  6614. }
  6615. out:
  6616. diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c
  6617. index 8353f33f0466..657e13ed4b5d 100644
  6618. --- a/fs/nfs/nfs4state.c
  6619. +++ b/fs/nfs/nfs4state.c
  6620. @@ -488,7 +488,7 @@ nfs4_alloc_state_owner(struct nfs_server *server,
  6621. nfs4_init_seqid_counter(&sp->so_seqid);
  6622. atomic_set(&sp->so_count, 1);
  6623. INIT_LIST_HEAD(&sp->so_lru);
  6624. - seqcount_init(&sp->so_reclaim_seqcount);
  6625. + seqlock_init(&sp->so_reclaim_seqlock);
  6626. mutex_init(&sp->so_delegreturn_mutex);
  6627. return sp;
  6628. }
  6629. @@ -1459,8 +1459,12 @@ static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs
  6630. * recovering after a network partition or a reboot from a
  6631. * server that doesn't support a grace period.
  6632. */
  6633. +#ifdef CONFIG_PREEMPT_RT_FULL
  6634. + write_seqlock(&sp->so_reclaim_seqlock);
  6635. +#else
  6636. + write_seqcount_begin(&sp->so_reclaim_seqlock.seqcount);
  6637. +#endif
  6638. spin_lock(&sp->so_lock);
  6639. - raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
  6640. restart:
  6641. list_for_each_entry(state, &sp->so_states, open_states) {
  6642. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  6643. @@ -1528,14 +1532,20 @@ static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs
  6644. spin_lock(&sp->so_lock);
  6645. goto restart;
  6646. }
  6647. - raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  6648. spin_unlock(&sp->so_lock);
  6649. +#ifdef CONFIG_PREEMPT_RT_FULL
  6650. + write_sequnlock(&sp->so_reclaim_seqlock);
  6651. +#else
  6652. + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount);
  6653. +#endif
  6654. return 0;
  6655. out_err:
  6656. nfs4_put_open_state(state);
  6657. - spin_lock(&sp->so_lock);
  6658. - raw_write_seqcount_end(&sp->so_reclaim_seqcount);
  6659. - spin_unlock(&sp->so_lock);
  6660. +#ifdef CONFIG_PREEMPT_RT_FULL
  6661. + write_sequnlock(&sp->so_reclaim_seqlock);
  6662. +#else
  6663. + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount);
  6664. +#endif
  6665. return status;
  6666. }
  6667. diff --git a/fs/nfs/unlink.c b/fs/nfs/unlink.c
  6668. index 191aa577dd1f..58990c8f52e0 100644
  6669. --- a/fs/nfs/unlink.c
  6670. +++ b/fs/nfs/unlink.c
  6671. @@ -12,7 +12,7 @@
  6672. #include <linux/sunrpc/clnt.h>
  6673. #include <linux/nfs_fs.h>
  6674. #include <linux/sched.h>
  6675. -#include <linux/wait.h>
  6676. +#include <linux/swait.h>
  6677. #include <linux/namei.h>
  6678. #include <linux/fsnotify.h>
  6679. @@ -51,6 +51,29 @@ static void nfs_async_unlink_done(struct rpc_task *task, void *calldata)
  6680. rpc_restart_call_prepare(task);
  6681. }
  6682. +#ifdef CONFIG_PREEMPT_RT_BASE
  6683. +static void nfs_down_anon(struct semaphore *sema)
  6684. +{
  6685. + down(sema);
  6686. +}
  6687. +
  6688. +static void nfs_up_anon(struct semaphore *sema)
  6689. +{
  6690. + up(sema);
  6691. +}
  6692. +
  6693. +#else
  6694. +static void nfs_down_anon(struct rw_semaphore *rwsem)
  6695. +{
  6696. + down_read_non_owner(rwsem);
  6697. +}
  6698. +
  6699. +static void nfs_up_anon(struct rw_semaphore *rwsem)
  6700. +{
  6701. + up_read_non_owner(rwsem);
  6702. +}
  6703. +#endif
  6704. +
  6705. /**
  6706. * nfs_async_unlink_release - Release the sillydelete data.
  6707. * @task: rpc_task of the sillydelete
  6708. @@ -64,7 +87,7 @@ static void nfs_async_unlink_release(void *calldata)
  6709. struct dentry *dentry = data->dentry;
  6710. struct super_block *sb = dentry->d_sb;
  6711. - up_read_non_owner(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
  6712. + nfs_up_anon(&NFS_I(d_inode(dentry->d_parent))->rmdir_sem);
  6713. d_lookup_done(dentry);
  6714. nfs_free_unlinkdata(data);
  6715. dput(dentry);
  6716. @@ -117,10 +140,10 @@ static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  6717. struct inode *dir = d_inode(dentry->d_parent);
  6718. struct dentry *alias;
  6719. - down_read_non_owner(&NFS_I(dir)->rmdir_sem);
  6720. + nfs_down_anon(&NFS_I(dir)->rmdir_sem);
  6721. alias = d_alloc_parallel(dentry->d_parent, &data->args.name, &data->wq);
  6722. if (IS_ERR(alias)) {
  6723. - up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  6724. + nfs_up_anon(&NFS_I(dir)->rmdir_sem);
  6725. return 0;
  6726. }
  6727. if (!d_in_lookup(alias)) {
  6728. @@ -142,7 +165,7 @@ static int nfs_call_unlink(struct dentry *dentry, struct nfs_unlinkdata *data)
  6729. ret = 0;
  6730. spin_unlock(&alias->d_lock);
  6731. dput(alias);
  6732. - up_read_non_owner(&NFS_I(dir)->rmdir_sem);
  6733. + nfs_up_anon(&NFS_I(dir)->rmdir_sem);
  6734. /*
  6735. * If we'd displaced old cached devname, free it. At that
  6736. * point dentry is definitely not a root, so we won't need
  6737. @@ -182,7 +205,7 @@ nfs_async_unlink(struct dentry *dentry, const struct qstr *name)
  6738. goto out_free_name;
  6739. }
  6740. data->res.dir_attr = &data->dir_attr;
  6741. - init_waitqueue_head(&data->wq);
  6742. + init_swait_queue_head(&data->wq);
  6743. status = -EBUSY;
  6744. spin_lock(&dentry->d_lock);
  6745. diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c
  6746. index fe251f187ff8..e89da4fb14c2 100644
  6747. --- a/fs/ntfs/aops.c
  6748. +++ b/fs/ntfs/aops.c
  6749. @@ -92,13 +92,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6750. ofs = 0;
  6751. if (file_ofs < init_size)
  6752. ofs = init_size - file_ofs;
  6753. - local_irq_save(flags);
  6754. + local_irq_save_nort(flags);
  6755. kaddr = kmap_atomic(page);
  6756. memset(kaddr + bh_offset(bh) + ofs, 0,
  6757. bh->b_size - ofs);
  6758. flush_dcache_page(page);
  6759. kunmap_atomic(kaddr);
  6760. - local_irq_restore(flags);
  6761. + local_irq_restore_nort(flags);
  6762. }
  6763. } else {
  6764. clear_buffer_uptodate(bh);
  6765. @@ -107,8 +107,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6766. "0x%llx.", (unsigned long long)bh->b_blocknr);
  6767. }
  6768. first = page_buffers(page);
  6769. - local_irq_save(flags);
  6770. - bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  6771. + flags = bh_uptodate_lock_irqsave(first);
  6772. clear_buffer_async_read(bh);
  6773. unlock_buffer(bh);
  6774. tmp = bh;
  6775. @@ -123,8 +122,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6776. }
  6777. tmp = tmp->b_this_page;
  6778. } while (tmp != bh);
  6779. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6780. - local_irq_restore(flags);
  6781. + bh_uptodate_unlock_irqrestore(first, flags);
  6782. /*
  6783. * If none of the buffers had errors then we can set the page uptodate,
  6784. * but we first have to perform the post read mst fixups, if the
  6785. @@ -145,13 +143,13 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6786. recs = PAGE_SIZE / rec_size;
  6787. /* Should have been verified before we got here... */
  6788. BUG_ON(!recs);
  6789. - local_irq_save(flags);
  6790. + local_irq_save_nort(flags);
  6791. kaddr = kmap_atomic(page);
  6792. for (i = 0; i < recs; i++)
  6793. post_read_mst_fixup((NTFS_RECORD*)(kaddr +
  6794. i * rec_size), rec_size);
  6795. kunmap_atomic(kaddr);
  6796. - local_irq_restore(flags);
  6797. + local_irq_restore_nort(flags);
  6798. flush_dcache_page(page);
  6799. if (likely(page_uptodate && !PageError(page)))
  6800. SetPageUptodate(page);
  6801. @@ -159,9 +157,7 @@ static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  6802. unlock_page(page);
  6803. return;
  6804. still_busy:
  6805. - bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  6806. - local_irq_restore(flags);
  6807. - return;
  6808. + bh_uptodate_unlock_irqrestore(first, flags);
  6809. }
  6810. /**
  6811. diff --git a/fs/proc/base.c b/fs/proc/base.c
  6812. index ac0df4dde823..ad1a4723ffdd 100644
  6813. --- a/fs/proc/base.c
  6814. +++ b/fs/proc/base.c
  6815. @@ -1819,7 +1819,7 @@ bool proc_fill_cache(struct file *file, struct dir_context *ctx,
  6816. child = d_hash_and_lookup(dir, &qname);
  6817. if (!child) {
  6818. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6819. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6820. child = d_alloc_parallel(dir, &qname, &wq);
  6821. if (IS_ERR(child))
  6822. goto end_instantiate;
  6823. diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
  6824. index 1b93650dda2f..c553bf3ea541 100644
  6825. --- a/fs/proc/proc_sysctl.c
  6826. +++ b/fs/proc/proc_sysctl.c
  6827. @@ -627,7 +627,7 @@ static bool proc_sys_fill_cache(struct file *file,
  6828. child = d_lookup(dir, &qname);
  6829. if (!child) {
  6830. - DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  6831. + DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(wq);
  6832. child = d_alloc_parallel(dir, &qname, &wq);
  6833. if (IS_ERR(child))
  6834. return false;
  6835. diff --git a/fs/timerfd.c b/fs/timerfd.c
  6836. index 9ae4abb4110b..8644b67c48fd 100644
  6837. --- a/fs/timerfd.c
  6838. +++ b/fs/timerfd.c
  6839. @@ -460,7 +460,10 @@ static int do_timerfd_settime(int ufd, int flags,
  6840. break;
  6841. }
  6842. spin_unlock_irq(&ctx->wqh.lock);
  6843. - cpu_relax();
  6844. + if (isalarm(ctx))
  6845. + hrtimer_wait_for_timer(&ctx->t.alarm.timer);
  6846. + else
  6847. + hrtimer_wait_for_timer(&ctx->t.tmr);
  6848. }
  6849. /*
  6850. diff --git a/include/acpi/platform/aclinux.h b/include/acpi/platform/aclinux.h
  6851. index 93b61b1f2beb..58270adb46ce 100644
  6852. --- a/include/acpi/platform/aclinux.h
  6853. +++ b/include/acpi/platform/aclinux.h
  6854. @@ -131,6 +131,7 @@
  6855. #define acpi_cache_t struct kmem_cache
  6856. #define acpi_spinlock spinlock_t *
  6857. +#define acpi_raw_spinlock raw_spinlock_t *
  6858. #define acpi_cpu_flags unsigned long
  6859. /* Use native linux version of acpi_os_allocate_zeroed */
  6860. @@ -149,6 +150,20 @@
  6861. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_get_thread_id
  6862. #define ACPI_USE_ALTERNATE_PROTOTYPE_acpi_os_create_lock
  6863. +#define acpi_os_create_raw_lock(__handle) \
  6864. +({ \
  6865. + raw_spinlock_t *lock = ACPI_ALLOCATE(sizeof(*lock)); \
  6866. + \
  6867. + if (lock) { \
  6868. + *(__handle) = lock; \
  6869. + raw_spin_lock_init(*(__handle)); \
  6870. + } \
  6871. + lock ? AE_OK : AE_NO_MEMORY; \
  6872. + })
  6873. +
  6874. +#define acpi_os_delete_raw_lock(__handle) kfree(__handle)
  6875. +
  6876. +
  6877. /*
  6878. * OSL interfaces used by debugger/disassembler
  6879. */
  6880. diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
  6881. index 6f96247226a4..fa53a21263c2 100644
  6882. --- a/include/asm-generic/bug.h
  6883. +++ b/include/asm-generic/bug.h
  6884. @@ -215,6 +215,20 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
  6885. # define WARN_ON_SMP(x) ({0;})
  6886. #endif
  6887. +#ifdef CONFIG_PREEMPT_RT_BASE
  6888. +# define BUG_ON_RT(c) BUG_ON(c)
  6889. +# define BUG_ON_NONRT(c) do { } while (0)
  6890. +# define WARN_ON_RT(condition) WARN_ON(condition)
  6891. +# define WARN_ON_NONRT(condition) do { } while (0)
  6892. +# define WARN_ON_ONCE_NONRT(condition) do { } while (0)
  6893. +#else
  6894. +# define BUG_ON_RT(c) do { } while (0)
  6895. +# define BUG_ON_NONRT(c) BUG_ON(c)
  6896. +# define WARN_ON_RT(condition) do { } while (0)
  6897. +# define WARN_ON_NONRT(condition) WARN_ON(condition)
  6898. +# define WARN_ON_ONCE_NONRT(condition) WARN_ON_ONCE(condition)
  6899. +#endif
  6900. +
  6901. #endif /* __ASSEMBLY__ */
  6902. #endif
  6903. diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
  6904. index e43bbffb5b7a..c23892264109 100644
  6905. --- a/include/linux/blk-mq.h
  6906. +++ b/include/linux/blk-mq.h
  6907. @@ -222,6 +222,7 @@ static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
  6908. struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *, const int ctx_index);
  6909. struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *, unsigned int, int);
  6910. +void __blk_mq_complete_request_remote_work(struct work_struct *work);
  6911. int blk_mq_request_started(struct request *rq);
  6912. void blk_mq_start_request(struct request *rq);
  6913. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
  6914. index e79055c8b577..8583c1af14ad 100644
  6915. --- a/include/linux/blkdev.h
  6916. +++ b/include/linux/blkdev.h
  6917. @@ -89,6 +89,7 @@ struct request {
  6918. struct list_head queuelist;
  6919. union {
  6920. struct call_single_data csd;
  6921. + struct work_struct work;
  6922. u64 fifo_time;
  6923. };
  6924. @@ -467,7 +468,7 @@ struct request_queue {
  6925. struct throtl_data *td;
  6926. #endif
  6927. struct rcu_head rcu_head;
  6928. - wait_queue_head_t mq_freeze_wq;
  6929. + struct swait_queue_head mq_freeze_wq;
  6930. struct percpu_ref q_usage_counter;
  6931. struct list_head all_q_node;
  6932. diff --git a/include/linux/bottom_half.h b/include/linux/bottom_half.h
  6933. index 8fdcb783197d..d07dbeec7bc1 100644
  6934. --- a/include/linux/bottom_half.h
  6935. +++ b/include/linux/bottom_half.h
  6936. @@ -3,6 +3,39 @@
  6937. #include <linux/preempt.h>
  6938. +#ifdef CONFIG_PREEMPT_RT_FULL
  6939. +
  6940. +extern void __local_bh_disable(void);
  6941. +extern void _local_bh_enable(void);
  6942. +extern void __local_bh_enable(void);
  6943. +
  6944. +static inline void local_bh_disable(void)
  6945. +{
  6946. + __local_bh_disable();
  6947. +}
  6948. +
  6949. +static inline void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  6950. +{
  6951. + __local_bh_disable();
  6952. +}
  6953. +
  6954. +static inline void local_bh_enable(void)
  6955. +{
  6956. + __local_bh_enable();
  6957. +}
  6958. +
  6959. +static inline void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  6960. +{
  6961. + __local_bh_enable();
  6962. +}
  6963. +
  6964. +static inline void local_bh_enable_ip(unsigned long ip)
  6965. +{
  6966. + __local_bh_enable();
  6967. +}
  6968. +
  6969. +#else
  6970. +
  6971. #ifdef CONFIG_TRACE_IRQFLAGS
  6972. extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
  6973. #else
  6974. @@ -30,5 +63,6 @@ static inline void local_bh_enable(void)
  6975. {
  6976. __local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  6977. }
  6978. +#endif
  6979. #endif /* _LINUX_BH_H */
  6980. diff --git a/include/linux/buffer_head.h b/include/linux/buffer_head.h
  6981. index ebbacd14d450..be5e87f6360a 100644
  6982. --- a/include/linux/buffer_head.h
  6983. +++ b/include/linux/buffer_head.h
  6984. @@ -75,8 +75,50 @@ struct buffer_head {
  6985. struct address_space *b_assoc_map; /* mapping this buffer is
  6986. associated with */
  6987. atomic_t b_count; /* users using this buffer_head */
  6988. +#ifdef CONFIG_PREEMPT_RT_BASE
  6989. + spinlock_t b_uptodate_lock;
  6990. +#if IS_ENABLED(CONFIG_JBD2)
  6991. + spinlock_t b_state_lock;
  6992. + spinlock_t b_journal_head_lock;
  6993. +#endif
  6994. +#endif
  6995. };
  6996. +static inline unsigned long bh_uptodate_lock_irqsave(struct buffer_head *bh)
  6997. +{
  6998. + unsigned long flags;
  6999. +
  7000. +#ifndef CONFIG_PREEMPT_RT_BASE
  7001. + local_irq_save(flags);
  7002. + bit_spin_lock(BH_Uptodate_Lock, &bh->b_state);
  7003. +#else
  7004. + spin_lock_irqsave(&bh->b_uptodate_lock, flags);
  7005. +#endif
  7006. + return flags;
  7007. +}
  7008. +
  7009. +static inline void
  7010. +bh_uptodate_unlock_irqrestore(struct buffer_head *bh, unsigned long flags)
  7011. +{
  7012. +#ifndef CONFIG_PREEMPT_RT_BASE
  7013. + bit_spin_unlock(BH_Uptodate_Lock, &bh->b_state);
  7014. + local_irq_restore(flags);
  7015. +#else
  7016. + spin_unlock_irqrestore(&bh->b_uptodate_lock, flags);
  7017. +#endif
  7018. +}
  7019. +
  7020. +static inline void buffer_head_init_locks(struct buffer_head *bh)
  7021. +{
  7022. +#ifdef CONFIG_PREEMPT_RT_BASE
  7023. + spin_lock_init(&bh->b_uptodate_lock);
  7024. +#if IS_ENABLED(CONFIG_JBD2)
  7025. + spin_lock_init(&bh->b_state_lock);
  7026. + spin_lock_init(&bh->b_journal_head_lock);
  7027. +#endif
  7028. +#endif
  7029. +}
  7030. +
  7031. /*
  7032. * macro tricks to expand the set_buffer_foo(), clear_buffer_foo()
  7033. * and buffer_foo() functions.
  7034. diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
  7035. index 5b17de62c962..56027cc01a56 100644
  7036. --- a/include/linux/cgroup-defs.h
  7037. +++ b/include/linux/cgroup-defs.h
  7038. @@ -16,6 +16,7 @@
  7039. #include <linux/percpu-refcount.h>
  7040. #include <linux/percpu-rwsem.h>
  7041. #include <linux/workqueue.h>
  7042. +#include <linux/swork.h>
  7043. #ifdef CONFIG_CGROUPS
  7044. @@ -137,6 +138,7 @@ struct cgroup_subsys_state {
  7045. /* percpu_ref killing and RCU release */
  7046. struct rcu_head rcu_head;
  7047. struct work_struct destroy_work;
  7048. + struct swork_event destroy_swork;
  7049. };
  7050. /*
  7051. diff --git a/include/linux/completion.h b/include/linux/completion.h
  7052. index 5d5aaae3af43..3bca1590e29f 100644
  7053. --- a/include/linux/completion.h
  7054. +++ b/include/linux/completion.h
  7055. @@ -7,8 +7,7 @@
  7056. * Atomic wait-for-completion handler data structures.
  7057. * See kernel/sched/completion.c for details.
  7058. */
  7059. -
  7060. -#include <linux/wait.h>
  7061. +#include <linux/swait.h>
  7062. /*
  7063. * struct completion - structure used to maintain state for a "completion"
  7064. @@ -24,11 +23,11 @@
  7065. */
  7066. struct completion {
  7067. unsigned int done;
  7068. - wait_queue_head_t wait;
  7069. + struct swait_queue_head wait;
  7070. };
  7071. #define COMPLETION_INITIALIZER(work) \
  7072. - { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  7073. + { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
  7074. #define COMPLETION_INITIALIZER_ONSTACK(work) \
  7075. ({ init_completion(&work); work; })
  7076. @@ -73,7 +72,7 @@ struct completion {
  7077. static inline void init_completion(struct completion *x)
  7078. {
  7079. x->done = 0;
  7080. - init_waitqueue_head(&x->wait);
  7081. + init_swait_queue_head(&x->wait);
  7082. }
  7083. /**
  7084. diff --git a/include/linux/cpu.h b/include/linux/cpu.h
  7085. index 797d9c8e9a1b..6eabd9e8a98b 100644
  7086. --- a/include/linux/cpu.h
  7087. +++ b/include/linux/cpu.h
  7088. @@ -201,6 +201,8 @@ extern void get_online_cpus(void);
  7089. extern void put_online_cpus(void);
  7090. extern void cpu_hotplug_disable(void);
  7091. extern void cpu_hotplug_enable(void);
  7092. +extern void pin_current_cpu(void);
  7093. +extern void unpin_current_cpu(void);
  7094. #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri)
  7095. #define __hotcpu_notifier(fn, pri) __cpu_notifier(fn, pri)
  7096. #define register_hotcpu_notifier(nb) register_cpu_notifier(nb)
  7097. @@ -218,6 +220,8 @@ static inline void cpu_hotplug_done(void) {}
  7098. #define put_online_cpus() do { } while (0)
  7099. #define cpu_hotplug_disable() do { } while (0)
  7100. #define cpu_hotplug_enable() do { } while (0)
  7101. +static inline void pin_current_cpu(void) { }
  7102. +static inline void unpin_current_cpu(void) { }
  7103. #define hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  7104. #define __hotcpu_notifier(fn, pri) do { (void)(fn); } while (0)
  7105. /* These aren't inline functions due to a GCC bug. */
  7106. diff --git a/include/linux/dcache.h b/include/linux/dcache.h
  7107. index 5ff3e9a4fe5f..ed0431599fd7 100644
  7108. --- a/include/linux/dcache.h
  7109. +++ b/include/linux/dcache.h
  7110. @@ -11,6 +11,7 @@
  7111. #include <linux/rcupdate.h>
  7112. #include <linux/lockref.h>
  7113. #include <linux/stringhash.h>
  7114. +#include <linux/wait.h>
  7115. struct path;
  7116. struct vfsmount;
  7117. @@ -100,7 +101,7 @@ struct dentry {
  7118. union {
  7119. struct list_head d_lru; /* LRU list */
  7120. - wait_queue_head_t *d_wait; /* in-lookup ones only */
  7121. + struct swait_queue_head *d_wait; /* in-lookup ones only */
  7122. };
  7123. struct list_head d_child; /* child of parent list */
  7124. struct list_head d_subdirs; /* our children */
  7125. @@ -230,7 +231,7 @@ extern void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op
  7126. extern struct dentry * d_alloc(struct dentry *, const struct qstr *);
  7127. extern struct dentry * d_alloc_pseudo(struct super_block *, const struct qstr *);
  7128. extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *,
  7129. - wait_queue_head_t *);
  7130. + struct swait_queue_head *);
  7131. extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
  7132. extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
  7133. extern struct dentry * d_exact_alias(struct dentry *, struct inode *);
  7134. diff --git a/include/linux/delay.h b/include/linux/delay.h
  7135. index a6ecb34cf547..37caab306336 100644
  7136. --- a/include/linux/delay.h
  7137. +++ b/include/linux/delay.h
  7138. @@ -52,4 +52,10 @@ static inline void ssleep(unsigned int seconds)
  7139. msleep(seconds * 1000);
  7140. }
  7141. +#ifdef CONFIG_PREEMPT_RT_FULL
  7142. +extern void cpu_chill(void);
  7143. +#else
  7144. +# define cpu_chill() cpu_relax()
  7145. +#endif
  7146. +
  7147. #endif /* defined(_LINUX_DELAY_H) */
  7148. diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
  7149. index 7d565afe35d2..8e31b4d245d2 100644
  7150. --- a/include/linux/ftrace.h
  7151. +++ b/include/linux/ftrace.h
  7152. @@ -714,6 +714,7 @@ static inline void __ftrace_enabled_restore(int enabled)
  7153. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  7154. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  7155. +#ifdef CONFIG_USING_GET_LOCK_PARENT_IP
  7156. static inline unsigned long get_lock_parent_ip(void)
  7157. {
  7158. unsigned long addr = CALLER_ADDR0;
  7159. @@ -725,6 +726,7 @@ static inline unsigned long get_lock_parent_ip(void)
  7160. return addr;
  7161. return CALLER_ADDR2;
  7162. }
  7163. +#endif
  7164. #ifdef CONFIG_IRQSOFF_TRACER
  7165. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  7166. diff --git a/include/linux/highmem.h b/include/linux/highmem.h
  7167. index bb3f3297062a..a117a33ef72c 100644
  7168. --- a/include/linux/highmem.h
  7169. +++ b/include/linux/highmem.h
  7170. @@ -7,6 +7,7 @@
  7171. #include <linux/mm.h>
  7172. #include <linux/uaccess.h>
  7173. #include <linux/hardirq.h>
  7174. +#include <linux/sched.h>
  7175. #include <asm/cacheflush.h>
  7176. @@ -65,7 +66,7 @@ static inline void kunmap(struct page *page)
  7177. static inline void *kmap_atomic(struct page *page)
  7178. {
  7179. - preempt_disable();
  7180. + preempt_disable_nort();
  7181. pagefault_disable();
  7182. return page_address(page);
  7183. }
  7184. @@ -74,7 +75,7 @@ static inline void *kmap_atomic(struct page *page)
  7185. static inline void __kunmap_atomic(void *addr)
  7186. {
  7187. pagefault_enable();
  7188. - preempt_enable();
  7189. + preempt_enable_nort();
  7190. }
  7191. #define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))
  7192. @@ -86,32 +87,51 @@ static inline void __kunmap_atomic(void *addr)
  7193. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  7194. +#ifndef CONFIG_PREEMPT_RT_FULL
  7195. DECLARE_PER_CPU(int, __kmap_atomic_idx);
  7196. +#endif
  7197. static inline int kmap_atomic_idx_push(void)
  7198. {
  7199. +#ifndef CONFIG_PREEMPT_RT_FULL
  7200. int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;
  7201. -#ifdef CONFIG_DEBUG_HIGHMEM
  7202. +# ifdef CONFIG_DEBUG_HIGHMEM
  7203. WARN_ON_ONCE(in_irq() && !irqs_disabled());
  7204. BUG_ON(idx >= KM_TYPE_NR);
  7205. -#endif
  7206. +# endif
  7207. return idx;
  7208. +#else
  7209. + current->kmap_idx++;
  7210. + BUG_ON(current->kmap_idx > KM_TYPE_NR);
  7211. + return current->kmap_idx - 1;
  7212. +#endif
  7213. }
  7214. static inline int kmap_atomic_idx(void)
  7215. {
  7216. +#ifndef CONFIG_PREEMPT_RT_FULL
  7217. return __this_cpu_read(__kmap_atomic_idx) - 1;
  7218. +#else
  7219. + return current->kmap_idx - 1;
  7220. +#endif
  7221. }
  7222. static inline void kmap_atomic_idx_pop(void)
  7223. {
  7224. -#ifdef CONFIG_DEBUG_HIGHMEM
  7225. +#ifndef CONFIG_PREEMPT_RT_FULL
  7226. +# ifdef CONFIG_DEBUG_HIGHMEM
  7227. int idx = __this_cpu_dec_return(__kmap_atomic_idx);
  7228. BUG_ON(idx < 0);
  7229. -#else
  7230. +# else
  7231. __this_cpu_dec(__kmap_atomic_idx);
  7232. +# endif
  7233. +#else
  7234. + current->kmap_idx--;
  7235. +# ifdef CONFIG_DEBUG_HIGHMEM
  7236. + BUG_ON(current->kmap_idx < 0);
  7237. +# endif
  7238. #endif
  7239. }
  7240. diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
  7241. index 5e00f80b1535..65d0671f20b4 100644
  7242. --- a/include/linux/hrtimer.h
  7243. +++ b/include/linux/hrtimer.h
  7244. @@ -87,6 +87,9 @@ enum hrtimer_restart {
  7245. * @function: timer expiry callback function
  7246. * @base: pointer to the timer base (per cpu and per clock)
  7247. * @state: state information (See bit values above)
  7248. + * @cb_entry: list entry to defer timers from hardirq context
  7249. + * @irqsafe: timer can run in hardirq context
  7250. + * @praecox: timer expiry time if expired at the time of programming
  7251. * @is_rel: Set if the timer was armed relative
  7252. * @start_pid: timer statistics field to store the pid of the task which
  7253. * started the timer
  7254. @@ -103,6 +106,11 @@ struct hrtimer {
  7255. enum hrtimer_restart (*function)(struct hrtimer *);
  7256. struct hrtimer_clock_base *base;
  7257. u8 state;
  7258. + struct list_head cb_entry;
  7259. + int irqsafe;
  7260. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  7261. + ktime_t praecox;
  7262. +#endif
  7263. u8 is_rel;
  7264. #ifdef CONFIG_TIMER_STATS
  7265. int start_pid;
  7266. @@ -123,11 +131,7 @@ struct hrtimer_sleeper {
  7267. struct task_struct *task;
  7268. };
  7269. -#ifdef CONFIG_64BIT
  7270. # define HRTIMER_CLOCK_BASE_ALIGN 64
  7271. -#else
  7272. -# define HRTIMER_CLOCK_BASE_ALIGN 32
  7273. -#endif
  7274. /**
  7275. * struct hrtimer_clock_base - the timer base for a specific clock
  7276. @@ -136,6 +140,7 @@ struct hrtimer_sleeper {
  7277. * timer to a base on another cpu.
  7278. * @clockid: clock id for per_cpu support
  7279. * @active: red black tree root node for the active timers
  7280. + * @expired: list head for deferred timers.
  7281. * @get_time: function to retrieve the current time of the clock
  7282. * @offset: offset of this clock to the monotonic base
  7283. */
  7284. @@ -144,6 +149,7 @@ struct hrtimer_clock_base {
  7285. int index;
  7286. clockid_t clockid;
  7287. struct timerqueue_head active;
  7288. + struct list_head expired;
  7289. ktime_t (*get_time)(void);
  7290. ktime_t offset;
  7291. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  7292. @@ -187,6 +193,7 @@ struct hrtimer_cpu_base {
  7293. raw_spinlock_t lock;
  7294. seqcount_t seq;
  7295. struct hrtimer *running;
  7296. + struct hrtimer *running_soft;
  7297. unsigned int cpu;
  7298. unsigned int active_bases;
  7299. unsigned int clock_was_set_seq;
  7300. @@ -203,6 +210,9 @@ struct hrtimer_cpu_base {
  7301. unsigned int nr_hangs;
  7302. unsigned int max_hang_time;
  7303. #endif
  7304. +#ifdef CONFIG_PREEMPT_RT_BASE
  7305. + wait_queue_head_t wait;
  7306. +#endif
  7307. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  7308. } ____cacheline_aligned;
  7309. @@ -412,6 +422,13 @@ static inline void hrtimer_restart(struct hrtimer *timer)
  7310. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  7311. }
  7312. +/* Softirq preemption could deadlock timer removal */
  7313. +#ifdef CONFIG_PREEMPT_RT_BASE
  7314. + extern void hrtimer_wait_for_timer(const struct hrtimer *timer);
  7315. +#else
  7316. +# define hrtimer_wait_for_timer(timer) do { cpu_relax(); } while (0)
  7317. +#endif
  7318. +
  7319. /* Query timers: */
  7320. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  7321. @@ -436,7 +453,7 @@ static inline int hrtimer_is_queued(struct hrtimer *timer)
  7322. * Helper function to check, whether the timer is running the callback
  7323. * function
  7324. */
  7325. -static inline int hrtimer_callback_running(struct hrtimer *timer)
  7326. +static inline int hrtimer_callback_running(const struct hrtimer *timer)
  7327. {
  7328. return timer->base->cpu_base->running == timer;
  7329. }
  7330. diff --git a/include/linux/idr.h b/include/linux/idr.h
  7331. index 083d61e92706..5899796f50cb 100644
  7332. --- a/include/linux/idr.h
  7333. +++ b/include/linux/idr.h
  7334. @@ -95,10 +95,14 @@ bool idr_is_empty(struct idr *idp);
  7335. * Each idr_preload() should be matched with an invocation of this
  7336. * function. See idr_preload() for details.
  7337. */
  7338. +#ifdef CONFIG_PREEMPT_RT_FULL
  7339. +void idr_preload_end(void);
  7340. +#else
  7341. static inline void idr_preload_end(void)
  7342. {
  7343. preempt_enable();
  7344. }
  7345. +#endif
  7346. /**
  7347. * idr_find - return pointer for given id
  7348. diff --git a/include/linux/init_task.h b/include/linux/init_task.h
  7349. index f8834f820ec2..a688d5e19578 100644
  7350. --- a/include/linux/init_task.h
  7351. +++ b/include/linux/init_task.h
  7352. @@ -148,6 +148,12 @@ extern struct task_group root_task_group;
  7353. # define INIT_PERF_EVENTS(tsk)
  7354. #endif
  7355. +#ifdef CONFIG_PREEMPT_RT_BASE
  7356. +# define INIT_TIMER_LIST .posix_timer_list = NULL,
  7357. +#else
  7358. +# define INIT_TIMER_LIST
  7359. +#endif
  7360. +
  7361. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
  7362. # define INIT_VTIME(tsk) \
  7363. .vtime_seqcount = SEQCNT_ZERO(tsk.vtime_seqcount), \
  7364. @@ -239,6 +245,7 @@ extern struct task_group root_task_group;
  7365. .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
  7366. .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
  7367. .timer_slack_ns = 50000, /* 50 usec default slack */ \
  7368. + INIT_TIMER_LIST \
  7369. .pids = { \
  7370. [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
  7371. [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
  7372. diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
  7373. index b6683f0ffc9f..c0a351daf736 100644
  7374. --- a/include/linux/interrupt.h
  7375. +++ b/include/linux/interrupt.h
  7376. @@ -14,6 +14,7 @@
  7377. #include <linux/hrtimer.h>
  7378. #include <linux/kref.h>
  7379. #include <linux/workqueue.h>
  7380. +#include <linux/swork.h>
  7381. #include <linux/atomic.h>
  7382. #include <asm/ptrace.h>
  7383. @@ -61,6 +62,7 @@
  7384. * interrupt handler after suspending interrupts. For system
  7385. * wakeup devices users need to implement wakeup detection in
  7386. * their interrupt handlers.
  7387. + * IRQF_NO_SOFTIRQ_CALL - Do not process softirqs in the irq thread context (RT)
  7388. */
  7389. #define IRQF_SHARED 0x00000080
  7390. #define IRQF_PROBE_SHARED 0x00000100
  7391. @@ -74,6 +76,7 @@
  7392. #define IRQF_NO_THREAD 0x00010000
  7393. #define IRQF_EARLY_RESUME 0x00020000
  7394. #define IRQF_COND_SUSPEND 0x00040000
  7395. +#define IRQF_NO_SOFTIRQ_CALL 0x00080000
  7396. #define IRQF_TIMER (__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
  7397. @@ -196,7 +199,7 @@ extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
  7398. #ifdef CONFIG_LOCKDEP
  7399. # define local_irq_enable_in_hardirq() do { } while (0)
  7400. #else
  7401. -# define local_irq_enable_in_hardirq() local_irq_enable()
  7402. +# define local_irq_enable_in_hardirq() local_irq_enable_nort()
  7403. #endif
  7404. extern void disable_irq_nosync(unsigned int irq);
  7405. @@ -216,6 +219,7 @@ extern void resume_device_irqs(void);
  7406. * struct irq_affinity_notify - context for notification of IRQ affinity changes
  7407. * @irq: Interrupt to which notification applies
  7408. * @kref: Reference count, for internal use
  7409. + * @swork: Swork item, for internal use
  7410. * @work: Work item, for internal use
  7411. * @notify: Function to be called on change. This will be
  7412. * called in process context.
  7413. @@ -227,7 +231,11 @@ extern void resume_device_irqs(void);
  7414. struct irq_affinity_notify {
  7415. unsigned int irq;
  7416. struct kref kref;
  7417. +#ifdef CONFIG_PREEMPT_RT_BASE
  7418. + struct swork_event swork;
  7419. +#else
  7420. struct work_struct work;
  7421. +#endif
  7422. void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
  7423. void (*release)(struct kref *ref);
  7424. };
  7425. @@ -398,9 +406,13 @@ extern int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  7426. bool state);
  7427. #ifdef CONFIG_IRQ_FORCED_THREADING
  7428. +# ifndef CONFIG_PREEMPT_RT_BASE
  7429. extern bool force_irqthreads;
  7430. +# else
  7431. +# define force_irqthreads (true)
  7432. +# endif
  7433. #else
  7434. -#define force_irqthreads (0)
  7435. +#define force_irqthreads (false)
  7436. #endif
  7437. #ifndef __ARCH_SET_SOFTIRQ_PENDING
  7438. @@ -457,9 +469,10 @@ struct softirq_action
  7439. void (*action)(struct softirq_action *);
  7440. };
  7441. +#ifndef CONFIG_PREEMPT_RT_FULL
  7442. asmlinkage void do_softirq(void);
  7443. asmlinkage void __do_softirq(void);
  7444. -
  7445. +static inline void thread_do_softirq(void) { do_softirq(); }
  7446. #ifdef __ARCH_HAS_DO_SOFTIRQ
  7447. void do_softirq_own_stack(void);
  7448. #else
  7449. @@ -468,13 +481,25 @@ static inline void do_softirq_own_stack(void)
  7450. __do_softirq();
  7451. }
  7452. #endif
  7453. +#else
  7454. +extern void thread_do_softirq(void);
  7455. +#endif
  7456. extern void open_softirq(int nr, void (*action)(struct softirq_action *));
  7457. extern void softirq_init(void);
  7458. extern void __raise_softirq_irqoff(unsigned int nr);
  7459. +#ifdef CONFIG_PREEMPT_RT_FULL
  7460. +extern void __raise_softirq_irqoff_ksoft(unsigned int nr);
  7461. +#else
  7462. +static inline void __raise_softirq_irqoff_ksoft(unsigned int nr)
  7463. +{
  7464. + __raise_softirq_irqoff(nr);
  7465. +}
  7466. +#endif
  7467. extern void raise_softirq_irqoff(unsigned int nr);
  7468. extern void raise_softirq(unsigned int nr);
  7469. +extern void softirq_check_pending_idle(void);
  7470. DECLARE_PER_CPU(struct task_struct *, ksoftirqd);
  7471. @@ -496,8 +521,9 @@ static inline struct task_struct *this_cpu_ksoftirqd(void)
  7472. to be executed on some cpu at least once after this.
  7473. * If the tasklet is already scheduled, but its execution is still not
  7474. started, it will be executed only once.
  7475. - * If this tasklet is already running on another CPU (or schedule is called
  7476. - from tasklet itself), it is rescheduled for later.
  7477. + * If this tasklet is already running on another CPU, it is rescheduled
  7478. + for later.
  7479. + * Schedule must not be called from the tasklet itself (a lockup occurs)
  7480. * Tasklet is strictly serialized wrt itself, but not
  7481. wrt another tasklets. If client needs some intertask synchronization,
  7482. he makes it with spinlocks.
  7483. @@ -522,27 +548,36 @@ struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }
  7484. enum
  7485. {
  7486. TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
  7487. - TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
  7488. + TASKLET_STATE_RUN, /* Tasklet is running (SMP only) */
  7489. + TASKLET_STATE_PENDING /* Tasklet is pending */
  7490. };
  7491. -#ifdef CONFIG_SMP
  7492. +#define TASKLET_STATEF_SCHED (1 << TASKLET_STATE_SCHED)
  7493. +#define TASKLET_STATEF_RUN (1 << TASKLET_STATE_RUN)
  7494. +#define TASKLET_STATEF_PENDING (1 << TASKLET_STATE_PENDING)
  7495. +
  7496. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  7497. static inline int tasklet_trylock(struct tasklet_struct *t)
  7498. {
  7499. return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
  7500. }
  7501. +static inline int tasklet_tryunlock(struct tasklet_struct *t)
  7502. +{
  7503. + return cmpxchg(&t->state, TASKLET_STATEF_RUN, 0) == TASKLET_STATEF_RUN;
  7504. +}
  7505. +
  7506. static inline void tasklet_unlock(struct tasklet_struct *t)
  7507. {
  7508. smp_mb__before_atomic();
  7509. clear_bit(TASKLET_STATE_RUN, &(t)->state);
  7510. }
  7511. -static inline void tasklet_unlock_wait(struct tasklet_struct *t)
  7512. -{
  7513. - while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
  7514. -}
  7515. +extern void tasklet_unlock_wait(struct tasklet_struct *t);
  7516. +
  7517. #else
  7518. #define tasklet_trylock(t) 1
  7519. +#define tasklet_tryunlock(t) 1
  7520. #define tasklet_unlock_wait(t) do { } while (0)
  7521. #define tasklet_unlock(t) do { } while (0)
  7522. #endif
  7523. @@ -591,12 +626,7 @@ static inline void tasklet_disable(struct tasklet_struct *t)
  7524. smp_mb();
  7525. }
  7526. -static inline void tasklet_enable(struct tasklet_struct *t)
  7527. -{
  7528. - smp_mb__before_atomic();
  7529. - atomic_dec(&t->count);
  7530. -}
  7531. -
  7532. +extern void tasklet_enable(struct tasklet_struct *t);
  7533. extern void tasklet_kill(struct tasklet_struct *t);
  7534. extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
  7535. extern void tasklet_init(struct tasklet_struct *t,
  7536. @@ -627,6 +657,12 @@ void tasklet_hrtimer_cancel(struct tasklet_hrtimer *ttimer)
  7537. tasklet_kill(&ttimer->tasklet);
  7538. }
  7539. +#ifdef CONFIG_PREEMPT_RT_FULL
  7540. +extern void softirq_early_init(void);
  7541. +#else
  7542. +static inline void softirq_early_init(void) { }
  7543. +#endif
  7544. +
  7545. /*
  7546. * Autoprobing for irqs:
  7547. *
  7548. diff --git a/include/linux/irq.h b/include/linux/irq.h
  7549. index 0ac26c892fe2..ede85f106aef 100644
  7550. --- a/include/linux/irq.h
  7551. +++ b/include/linux/irq.h
  7552. @@ -72,6 +72,7 @@ enum irqchip_irq_state;
  7553. * IRQ_IS_POLLED - Always polled by another interrupt. Exclude
  7554. * it from the spurious interrupt detection
  7555. * mechanism and from core side polling.
  7556. + * IRQ_NO_SOFTIRQ_CALL - No softirq processing in the irq thread context (RT)
  7557. * IRQ_DISABLE_UNLAZY - Disable lazy irq disable
  7558. */
  7559. enum {
  7560. @@ -99,13 +100,14 @@ enum {
  7561. IRQ_PER_CPU_DEVID = (1 << 17),
  7562. IRQ_IS_POLLED = (1 << 18),
  7563. IRQ_DISABLE_UNLAZY = (1 << 19),
  7564. + IRQ_NO_SOFTIRQ_CALL = (1 << 20),
  7565. };
  7566. #define IRQF_MODIFY_MASK \
  7567. (IRQ_TYPE_SENSE_MASK | IRQ_NOPROBE | IRQ_NOREQUEST | \
  7568. IRQ_NOAUTOEN | IRQ_MOVE_PCNTXT | IRQ_LEVEL | IRQ_NO_BALANCING | \
  7569. IRQ_PER_CPU | IRQ_NESTED_THREAD | IRQ_NOTHREAD | IRQ_PER_CPU_DEVID | \
  7570. - IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY)
  7571. + IRQ_IS_POLLED | IRQ_DISABLE_UNLAZY | IRQ_NO_SOFTIRQ_CALL)
  7572. #define IRQ_NO_BALANCING_MASK (IRQ_PER_CPU | IRQ_NO_BALANCING)
  7573. diff --git a/include/linux/irq_work.h b/include/linux/irq_work.h
  7574. index 47b9ebd4a74f..2543aab05daa 100644
  7575. --- a/include/linux/irq_work.h
  7576. +++ b/include/linux/irq_work.h
  7577. @@ -16,6 +16,7 @@
  7578. #define IRQ_WORK_BUSY 2UL
  7579. #define IRQ_WORK_FLAGS 3UL
  7580. #define IRQ_WORK_LAZY 4UL /* Doesn't want IPI, wait for tick */
  7581. +#define IRQ_WORK_HARD_IRQ 8UL /* Run hard IRQ context, even on RT */
  7582. struct irq_work {
  7583. unsigned long flags;
  7584. @@ -51,4 +52,10 @@ static inline bool irq_work_needs_cpu(void) { return false; }
  7585. static inline void irq_work_run(void) { }
  7586. #endif
  7587. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  7588. +void irq_work_tick_soft(void);
  7589. +#else
  7590. +static inline void irq_work_tick_soft(void) { }
  7591. +#endif
  7592. +
  7593. #endif /* _LINUX_IRQ_WORK_H */
  7594. diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
  7595. index b51beebf9804..219d9824f762 100644
  7596. --- a/include/linux/irqdesc.h
  7597. +++ b/include/linux/irqdesc.h
  7598. @@ -64,6 +64,7 @@ struct irq_desc {
  7599. unsigned int irqs_unhandled;
  7600. atomic_t threads_handled;
  7601. int threads_handled_last;
  7602. + u64 random_ip;
  7603. raw_spinlock_t lock;
  7604. struct cpumask *percpu_enabled;
  7605. const struct cpumask *percpu_affinity;
  7606. diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
  7607. index 5dd1272d1ab2..9b77034f7c5e 100644
  7608. --- a/include/linux/irqflags.h
  7609. +++ b/include/linux/irqflags.h
  7610. @@ -25,8 +25,6 @@
  7611. # define trace_softirqs_enabled(p) ((p)->softirqs_enabled)
  7612. # define trace_hardirq_enter() do { current->hardirq_context++; } while (0)
  7613. # define trace_hardirq_exit() do { current->hardirq_context--; } while (0)
  7614. -# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  7615. -# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  7616. # define INIT_TRACE_IRQFLAGS .softirqs_enabled = 1,
  7617. #else
  7618. # define trace_hardirqs_on() do { } while (0)
  7619. @@ -39,9 +37,15 @@
  7620. # define trace_softirqs_enabled(p) 0
  7621. # define trace_hardirq_enter() do { } while (0)
  7622. # define trace_hardirq_exit() do { } while (0)
  7623. +# define INIT_TRACE_IRQFLAGS
  7624. +#endif
  7625. +
  7626. +#if defined(CONFIG_TRACE_IRQFLAGS) && !defined(CONFIG_PREEMPT_RT_FULL)
  7627. +# define lockdep_softirq_enter() do { current->softirq_context++; } while (0)
  7628. +# define lockdep_softirq_exit() do { current->softirq_context--; } while (0)
  7629. +#else
  7630. # define lockdep_softirq_enter() do { } while (0)
  7631. # define lockdep_softirq_exit() do { } while (0)
  7632. -# define INIT_TRACE_IRQFLAGS
  7633. #endif
  7634. #if defined(CONFIG_IRQSOFF_TRACER) || \
  7635. @@ -148,4 +152,23 @@
  7636. #define irqs_disabled_flags(flags) raw_irqs_disabled_flags(flags)
  7637. +/*
  7638. + * local_irq* variants depending on RT/!RT
  7639. + */
  7640. +#ifdef CONFIG_PREEMPT_RT_FULL
  7641. +# define local_irq_disable_nort() do { } while (0)
  7642. +# define local_irq_enable_nort() do { } while (0)
  7643. +# define local_irq_save_nort(flags) local_save_flags(flags)
  7644. +# define local_irq_restore_nort(flags) (void)(flags)
  7645. +# define local_irq_disable_rt() local_irq_disable()
  7646. +# define local_irq_enable_rt() local_irq_enable()
  7647. +#else
  7648. +# define local_irq_disable_nort() local_irq_disable()
  7649. +# define local_irq_enable_nort() local_irq_enable()
  7650. +# define local_irq_save_nort(flags) local_irq_save(flags)
  7651. +# define local_irq_restore_nort(flags) local_irq_restore(flags)
  7652. +# define local_irq_disable_rt() do { } while (0)
  7653. +# define local_irq_enable_rt() do { } while (0)
  7654. +#endif
  7655. +
  7656. #endif
  7657. diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
  7658. index dfaa1f4dcb0c..d57dd06544a1 100644
  7659. --- a/include/linux/jbd2.h
  7660. +++ b/include/linux/jbd2.h
  7661. @@ -347,32 +347,56 @@ static inline struct journal_head *bh2jh(struct buffer_head *bh)
  7662. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  7663. {
  7664. +#ifndef CONFIG_PREEMPT_RT_BASE
  7665. bit_spin_lock(BH_State, &bh->b_state);
  7666. +#else
  7667. + spin_lock(&bh->b_state_lock);
  7668. +#endif
  7669. }
  7670. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  7671. {
  7672. +#ifndef CONFIG_PREEMPT_RT_BASE
  7673. return bit_spin_trylock(BH_State, &bh->b_state);
  7674. +#else
  7675. + return spin_trylock(&bh->b_state_lock);
  7676. +#endif
  7677. }
  7678. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  7679. {
  7680. +#ifndef CONFIG_PREEMPT_RT_BASE
  7681. return bit_spin_is_locked(BH_State, &bh->b_state);
  7682. +#else
  7683. + return spin_is_locked(&bh->b_state_lock);
  7684. +#endif
  7685. }
  7686. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  7687. {
  7688. +#ifndef CONFIG_PREEMPT_RT_BASE
  7689. bit_spin_unlock(BH_State, &bh->b_state);
  7690. +#else
  7691. + spin_unlock(&bh->b_state_lock);
  7692. +#endif
  7693. }
  7694. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  7695. {
  7696. +#ifndef CONFIG_PREEMPT_RT_BASE
  7697. bit_spin_lock(BH_JournalHead, &bh->b_state);
  7698. +#else
  7699. + spin_lock(&bh->b_journal_head_lock);
  7700. +#endif
  7701. }
  7702. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  7703. {
  7704. +#ifndef CONFIG_PREEMPT_RT_BASE
  7705. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  7706. +#else
  7707. + spin_unlock(&bh->b_journal_head_lock);
  7708. +#endif
  7709. }
  7710. #define J_ASSERT(assert) BUG_ON(!(assert))
  7711. diff --git a/include/linux/kdb.h b/include/linux/kdb.h
  7712. index 410decacff8f..0861bebfc188 100644
  7713. --- a/include/linux/kdb.h
  7714. +++ b/include/linux/kdb.h
  7715. @@ -167,6 +167,7 @@ extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
  7716. extern __printf(1, 2) int kdb_printf(const char *, ...);
  7717. typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
  7718. +#define in_kdb_printk() (kdb_trap_printk)
  7719. extern void kdb_init(int level);
  7720. /* Access to kdb specific polling devices */
  7721. @@ -201,6 +202,7 @@ extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
  7722. extern int kdb_unregister(char *);
  7723. #else /* ! CONFIG_KGDB_KDB */
  7724. static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
  7725. +#define in_kdb_printk() (0)
  7726. static inline void kdb_init(int level) {}
  7727. static inline int kdb_register(char *cmd, kdb_func_t func, char *usage,
  7728. char *help, short minlen) { return 0; }
  7729. diff --git a/include/linux/kernel.h b/include/linux/kernel.h
  7730. index d96a6118d26a..37de2ce2d290 100644
  7731. --- a/include/linux/kernel.h
  7732. +++ b/include/linux/kernel.h
  7733. @@ -194,6 +194,9 @@ extern int _cond_resched(void);
  7734. */
  7735. # define might_sleep() \
  7736. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  7737. +
  7738. +# define might_sleep_no_state_check() \
  7739. + do { ___might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  7740. # define sched_annotate_sleep() (current->task_state_change = 0)
  7741. #else
  7742. static inline void ___might_sleep(const char *file, int line,
  7743. @@ -201,6 +204,7 @@ extern int _cond_resched(void);
  7744. static inline void __might_sleep(const char *file, int line,
  7745. int preempt_offset) { }
  7746. # define might_sleep() do { might_resched(); } while (0)
  7747. +# define might_sleep_no_state_check() do { might_resched(); } while (0)
  7748. # define sched_annotate_sleep() do { } while (0)
  7749. #endif
  7750. @@ -491,6 +495,7 @@ extern enum system_states {
  7751. SYSTEM_HALT,
  7752. SYSTEM_POWER_OFF,
  7753. SYSTEM_RESTART,
  7754. + SYSTEM_SUSPEND,
  7755. } system_state;
  7756. #define TAINT_PROPRIETARY_MODULE 0
  7757. diff --git a/include/linux/lglock.h b/include/linux/lglock.h
  7758. index c92ebd100d9b..6f035f635d0e 100644
  7759. --- a/include/linux/lglock.h
  7760. +++ b/include/linux/lglock.h
  7761. @@ -34,13 +34,30 @@
  7762. #endif
  7763. struct lglock {
  7764. +#ifdef CONFIG_PREEMPT_RT_FULL
  7765. + struct rt_mutex __percpu *lock;
  7766. +#else
  7767. arch_spinlock_t __percpu *lock;
  7768. +#endif
  7769. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  7770. struct lock_class_key lock_key;
  7771. struct lockdep_map lock_dep_map;
  7772. #endif
  7773. };
  7774. +#ifdef CONFIG_PREEMPT_RT_FULL
  7775. +# define DEFINE_LGLOCK(name) \
  7776. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  7777. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  7778. + struct lglock name = { .lock = &name ## _lock }
  7779. +
  7780. +# define DEFINE_STATIC_LGLOCK(name) \
  7781. + static DEFINE_PER_CPU(struct rt_mutex, name ## _lock) \
  7782. + = __RT_MUTEX_INITIALIZER( name ## _lock); \
  7783. + static struct lglock name = { .lock = &name ## _lock }
  7784. +
  7785. +#else
  7786. +
  7787. #define DEFINE_LGLOCK(name) \
  7788. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  7789. = __ARCH_SPIN_LOCK_UNLOCKED; \
  7790. @@ -50,6 +67,7 @@ struct lglock {
  7791. static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
  7792. = __ARCH_SPIN_LOCK_UNLOCKED; \
  7793. static struct lglock name = { .lock = &name ## _lock }
  7794. +#endif
  7795. void lg_lock_init(struct lglock *lg, char *name);
  7796. @@ -64,6 +82,12 @@ void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
  7797. void lg_global_lock(struct lglock *lg);
  7798. void lg_global_unlock(struct lglock *lg);
  7799. +#ifndef CONFIG_PREEMPT_RT_FULL
  7800. +#define lg_global_trylock_relax(name) lg_global_lock(name)
  7801. +#else
  7802. +void lg_global_trylock_relax(struct lglock *lg);
  7803. +#endif
  7804. +
  7805. #else
  7806. /* When !CONFIG_SMP, map lglock to spinlock */
  7807. #define lglock spinlock
  7808. diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h
  7809. index cb483305e1f5..4e5062316bb6 100644
  7810. --- a/include/linux/list_bl.h
  7811. +++ b/include/linux/list_bl.h
  7812. @@ -2,6 +2,7 @@
  7813. #define _LINUX_LIST_BL_H
  7814. #include <linux/list.h>
  7815. +#include <linux/spinlock.h>
  7816. #include <linux/bit_spinlock.h>
  7817. /*
  7818. @@ -32,13 +33,24 @@
  7819. struct hlist_bl_head {
  7820. struct hlist_bl_node *first;
  7821. +#ifdef CONFIG_PREEMPT_RT_BASE
  7822. + raw_spinlock_t lock;
  7823. +#endif
  7824. };
  7825. struct hlist_bl_node {
  7826. struct hlist_bl_node *next, **pprev;
  7827. };
  7828. -#define INIT_HLIST_BL_HEAD(ptr) \
  7829. - ((ptr)->first = NULL)
  7830. +
  7831. +#ifdef CONFIG_PREEMPT_RT_BASE
  7832. +#define INIT_HLIST_BL_HEAD(h) \
  7833. +do { \
  7834. + (h)->first = NULL; \
  7835. + raw_spin_lock_init(&(h)->lock); \
  7836. +} while (0)
  7837. +#else
  7838. +#define INIT_HLIST_BL_HEAD(h) (h)->first = NULL
  7839. +#endif
  7840. static inline void INIT_HLIST_BL_NODE(struct hlist_bl_node *h)
  7841. {
  7842. @@ -118,12 +130,26 @@ static inline void hlist_bl_del_init(struct hlist_bl_node *n)
  7843. static inline void hlist_bl_lock(struct hlist_bl_head *b)
  7844. {
  7845. +#ifndef CONFIG_PREEMPT_RT_BASE
  7846. bit_spin_lock(0, (unsigned long *)b);
  7847. +#else
  7848. + raw_spin_lock(&b->lock);
  7849. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  7850. + __set_bit(0, (unsigned long *)b);
  7851. +#endif
  7852. +#endif
  7853. }
  7854. static inline void hlist_bl_unlock(struct hlist_bl_head *b)
  7855. {
  7856. +#ifndef CONFIG_PREEMPT_RT_BASE
  7857. __bit_spin_unlock(0, (unsigned long *)b);
  7858. +#else
  7859. +#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  7860. + __clear_bit(0, (unsigned long *)b);
  7861. +#endif
  7862. + raw_spin_unlock(&b->lock);
  7863. +#endif
  7864. }
  7865. static inline bool hlist_bl_is_locked(struct hlist_bl_head *b)
  7866. diff --git a/include/linux/locallock.h b/include/linux/locallock.h
  7867. new file mode 100644
  7868. index 000000000000..845c77f1a5ca
  7869. --- /dev/null
  7870. +++ b/include/linux/locallock.h
  7871. @@ -0,0 +1,278 @@
  7872. +#ifndef _LINUX_LOCALLOCK_H
  7873. +#define _LINUX_LOCALLOCK_H
  7874. +
  7875. +#include <linux/percpu.h>
  7876. +#include <linux/spinlock.h>
  7877. +
  7878. +#ifdef CONFIG_PREEMPT_RT_BASE
  7879. +
  7880. +#ifdef CONFIG_DEBUG_SPINLOCK
  7881. +# define LL_WARN(cond) WARN_ON(cond)
  7882. +#else
  7883. +# define LL_WARN(cond) do { } while (0)
  7884. +#endif
  7885. +
  7886. +/*
  7887. + * per cpu lock based substitute for local_irq_*()
  7888. + */
  7889. +struct local_irq_lock {
  7890. + spinlock_t lock;
  7891. + struct task_struct *owner;
  7892. + int nestcnt;
  7893. + unsigned long flags;
  7894. +};
  7895. +
  7896. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) \
  7897. + DEFINE_PER_CPU(struct local_irq_lock, lvar) = { \
  7898. + .lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  7899. +
  7900. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) \
  7901. + DECLARE_PER_CPU(struct local_irq_lock, lvar)
  7902. +
  7903. +#define local_irq_lock_init(lvar) \
  7904. + do { \
  7905. + int __cpu; \
  7906. + for_each_possible_cpu(__cpu) \
  7907. + spin_lock_init(&per_cpu(lvar, __cpu).lock); \
  7908. + } while (0)
  7909. +
  7910. +/*
  7911. + * spin_lock|trylock|unlock_local flavour that does not migrate disable
  7912. + * used for __local_lock|trylock|unlock where get_local_var/put_local_var
  7913. + * already takes care of the migrate_disable/enable
  7914. + * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
  7915. + */
  7916. +#ifdef CONFIG_PREEMPT_RT_FULL
  7917. +# define spin_lock_local(lock) rt_spin_lock__no_mg(lock)
  7918. +# define spin_trylock_local(lock) rt_spin_trylock__no_mg(lock)
  7919. +# define spin_unlock_local(lock) rt_spin_unlock__no_mg(lock)
  7920. +#else
  7921. +# define spin_lock_local(lock) spin_lock(lock)
  7922. +# define spin_trylock_local(lock) spin_trylock(lock)
  7923. +# define spin_unlock_local(lock) spin_unlock(lock)
  7924. +#endif
  7925. +
  7926. +static inline void __local_lock(struct local_irq_lock *lv)
  7927. +{
  7928. + if (lv->owner != current) {
  7929. + spin_lock_local(&lv->lock);
  7930. + LL_WARN(lv->owner);
  7931. + LL_WARN(lv->nestcnt);
  7932. + lv->owner = current;
  7933. + }
  7934. + lv->nestcnt++;
  7935. +}
  7936. +
  7937. +#define local_lock(lvar) \
  7938. + do { __local_lock(&get_local_var(lvar)); } while (0)
  7939. +
  7940. +#define local_lock_on(lvar, cpu) \
  7941. + do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  7942. +
  7943. +static inline int __local_trylock(struct local_irq_lock *lv)
  7944. +{
  7945. + if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  7946. + LL_WARN(lv->owner);
  7947. + LL_WARN(lv->nestcnt);
  7948. + lv->owner = current;
  7949. + lv->nestcnt = 1;
  7950. + return 1;
  7951. + }
  7952. + return 0;
  7953. +}
  7954. +
  7955. +#define local_trylock(lvar) \
  7956. + ({ \
  7957. + int __locked; \
  7958. + __locked = __local_trylock(&get_local_var(lvar)); \
  7959. + if (!__locked) \
  7960. + put_local_var(lvar); \
  7961. + __locked; \
  7962. + })
  7963. +
  7964. +static inline void __local_unlock(struct local_irq_lock *lv)
  7965. +{
  7966. + LL_WARN(lv->nestcnt == 0);
  7967. + LL_WARN(lv->owner != current);
  7968. + if (--lv->nestcnt)
  7969. + return;
  7970. +
  7971. + lv->owner = NULL;
  7972. + spin_unlock_local(&lv->lock);
  7973. +}
  7974. +
  7975. +#define local_unlock(lvar) \
  7976. + do { \
  7977. + __local_unlock(this_cpu_ptr(&lvar)); \
  7978. + put_local_var(lvar); \
  7979. + } while (0)
  7980. +
  7981. +#define local_unlock_on(lvar, cpu) \
  7982. + do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  7983. +
  7984. +static inline void __local_lock_irq(struct local_irq_lock *lv)
  7985. +{
  7986. + spin_lock_irqsave(&lv->lock, lv->flags);
  7987. + LL_WARN(lv->owner);
  7988. + LL_WARN(lv->nestcnt);
  7989. + lv->owner = current;
  7990. + lv->nestcnt = 1;
  7991. +}
  7992. +
  7993. +#define local_lock_irq(lvar) \
  7994. + do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  7995. +
  7996. +#define local_lock_irq_on(lvar, cpu) \
  7997. + do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  7998. +
  7999. +static inline void __local_unlock_irq(struct local_irq_lock *lv)
  8000. +{
  8001. + LL_WARN(!lv->nestcnt);
  8002. + LL_WARN(lv->owner != current);
  8003. + lv->owner = NULL;
  8004. + lv->nestcnt = 0;
  8005. + spin_unlock_irq(&lv->lock);
  8006. +}
  8007. +
  8008. +#define local_unlock_irq(lvar) \
  8009. + do { \
  8010. + __local_unlock_irq(this_cpu_ptr(&lvar)); \
  8011. + put_local_var(lvar); \
  8012. + } while (0)
  8013. +
  8014. +#define local_unlock_irq_on(lvar, cpu) \
  8015. + do { \
  8016. + __local_unlock_irq(&per_cpu(lvar, cpu)); \
  8017. + } while (0)
  8018. +
  8019. +static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  8020. +{
  8021. + if (lv->owner != current) {
  8022. + __local_lock_irq(lv);
  8023. + return 0;
  8024. + } else {
  8025. + lv->nestcnt++;
  8026. + return 1;
  8027. + }
  8028. +}
  8029. +
  8030. +#define local_lock_irqsave(lvar, _flags) \
  8031. + do { \
  8032. + if (__local_lock_irqsave(&get_local_var(lvar))) \
  8033. + put_local_var(lvar); \
  8034. + _flags = __this_cpu_read(lvar.flags); \
  8035. + } while (0)
  8036. +
  8037. +#define local_lock_irqsave_on(lvar, _flags, cpu) \
  8038. + do { \
  8039. + __local_lock_irqsave(&per_cpu(lvar, cpu)); \
  8040. + _flags = per_cpu(lvar, cpu).flags; \
  8041. + } while (0)
  8042. +
  8043. +static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  8044. + unsigned long flags)
  8045. +{
  8046. + LL_WARN(!lv->nestcnt);
  8047. + LL_WARN(lv->owner != current);
  8048. + if (--lv->nestcnt)
  8049. + return 0;
  8050. +
  8051. + lv->owner = NULL;
  8052. + spin_unlock_irqrestore(&lv->lock, lv->flags);
  8053. + return 1;
  8054. +}
  8055. +
  8056. +#define local_unlock_irqrestore(lvar, flags) \
  8057. + do { \
  8058. + if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  8059. + put_local_var(lvar); \
  8060. + } while (0)
  8061. +
  8062. +#define local_unlock_irqrestore_on(lvar, flags, cpu) \
  8063. + do { \
  8064. + __local_unlock_irqrestore(&per_cpu(lvar, cpu), flags); \
  8065. + } while (0)
  8066. +
  8067. +#define local_spin_trylock_irq(lvar, lock) \
  8068. + ({ \
  8069. + int __locked; \
  8070. + local_lock_irq(lvar); \
  8071. + __locked = spin_trylock(lock); \
  8072. + if (!__locked) \
  8073. + local_unlock_irq(lvar); \
  8074. + __locked; \
  8075. + })
  8076. +
  8077. +#define local_spin_lock_irq(lvar, lock) \
  8078. + do { \
  8079. + local_lock_irq(lvar); \
  8080. + spin_lock(lock); \
  8081. + } while (0)
  8082. +
  8083. +#define local_spin_unlock_irq(lvar, lock) \
  8084. + do { \
  8085. + spin_unlock(lock); \
  8086. + local_unlock_irq(lvar); \
  8087. + } while (0)
  8088. +
  8089. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  8090. + do { \
  8091. + local_lock_irqsave(lvar, flags); \
  8092. + spin_lock(lock); \
  8093. + } while (0)
  8094. +
  8095. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  8096. + do { \
  8097. + spin_unlock(lock); \
  8098. + local_unlock_irqrestore(lvar, flags); \
  8099. + } while (0)
  8100. +
  8101. +#define get_locked_var(lvar, var) \
  8102. + (*({ \
  8103. + local_lock(lvar); \
  8104. + this_cpu_ptr(&var); \
  8105. + }))
  8106. +
  8107. +#define put_locked_var(lvar, var) local_unlock(lvar);
  8108. +
  8109. +#define local_lock_cpu(lvar) \
  8110. + ({ \
  8111. + local_lock(lvar); \
  8112. + smp_processor_id(); \
  8113. + })
  8114. +
  8115. +#define local_unlock_cpu(lvar) local_unlock(lvar)
  8116. +
  8117. +#else /* PREEMPT_RT_BASE */
  8118. +
  8119. +#define DEFINE_LOCAL_IRQ_LOCK(lvar) __typeof__(const int) lvar
  8120. +#define DECLARE_LOCAL_IRQ_LOCK(lvar) extern __typeof__(const int) lvar
  8121. +
  8122. +static inline void local_irq_lock_init(int lvar) { }
  8123. +
  8124. +#define local_lock(lvar) preempt_disable()
  8125. +#define local_unlock(lvar) preempt_enable()
  8126. +#define local_lock_irq(lvar) local_irq_disable()
  8127. +#define local_lock_irq_on(lvar, cpu) local_irq_disable()
  8128. +#define local_unlock_irq(lvar) local_irq_enable()
  8129. +#define local_unlock_irq_on(lvar, cpu) local_irq_enable()
  8130. +#define local_lock_irqsave(lvar, flags) local_irq_save(flags)
  8131. +#define local_unlock_irqrestore(lvar, flags) local_irq_restore(flags)
  8132. +
  8133. +#define local_spin_trylock_irq(lvar, lock) spin_trylock_irq(lock)
  8134. +#define local_spin_lock_irq(lvar, lock) spin_lock_irq(lock)
  8135. +#define local_spin_unlock_irq(lvar, lock) spin_unlock_irq(lock)
  8136. +#define local_spin_lock_irqsave(lvar, lock, flags) \
  8137. + spin_lock_irqsave(lock, flags)
  8138. +#define local_spin_unlock_irqrestore(lvar, lock, flags) \
  8139. + spin_unlock_irqrestore(lock, flags)
  8140. +
  8141. +#define get_locked_var(lvar, var) get_cpu_var(var)
  8142. +#define put_locked_var(lvar, var) put_cpu_var(var)
  8143. +
  8144. +#define local_lock_cpu(lvar) get_cpu()
  8145. +#define local_unlock_cpu(lvar) put_cpu()
  8146. +
  8147. +#endif
  8148. +
  8149. +#endif
  8150. diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
  8151. index 903200f4ec41..df670d441fc9 100644
  8152. --- a/include/linux/mm_types.h
  8153. +++ b/include/linux/mm_types.h
  8154. @@ -11,6 +11,7 @@
  8155. #include <linux/completion.h>
  8156. #include <linux/cpumask.h>
  8157. #include <linux/uprobes.h>
  8158. +#include <linux/rcupdate.h>
  8159. #include <linux/page-flags-layout.h>
  8160. #include <linux/workqueue.h>
  8161. #include <asm/page.h>
  8162. @@ -508,6 +509,9 @@ struct mm_struct {
  8163. bool tlb_flush_pending;
  8164. #endif
  8165. struct uprobes_state uprobes_state;
  8166. +#ifdef CONFIG_PREEMPT_RT_BASE
  8167. + struct rcu_head delayed_drop;
  8168. +#endif
  8169. #ifdef CONFIG_X86_INTEL_MPX
  8170. /* address of the bounds directory */
  8171. void __user *bd_addr;
  8172. diff --git a/include/linux/mutex.h b/include/linux/mutex.h
  8173. index 2cb7531e7d7a..b3fdfc820216 100644
  8174. --- a/include/linux/mutex.h
  8175. +++ b/include/linux/mutex.h
  8176. @@ -19,6 +19,17 @@
  8177. #include <asm/processor.h>
  8178. #include <linux/osq_lock.h>
  8179. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8180. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  8181. + , .dep_map = { .name = #lockname }
  8182. +#else
  8183. +# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  8184. +#endif
  8185. +
  8186. +#ifdef CONFIG_PREEMPT_RT_FULL
  8187. +# include <linux/mutex_rt.h>
  8188. +#else
  8189. +
  8190. /*
  8191. * Simple, straightforward mutexes with strict semantics:
  8192. *
  8193. @@ -99,13 +110,6 @@ do { \
  8194. static inline void mutex_destroy(struct mutex *lock) {}
  8195. #endif
  8196. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8197. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  8198. - , .dep_map = { .name = #lockname }
  8199. -#else
  8200. -# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  8201. -#endif
  8202. -
  8203. #define __MUTEX_INITIALIZER(lockname) \
  8204. { .count = ATOMIC_INIT(1) \
  8205. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  8206. @@ -173,6 +177,8 @@ extern int __must_check mutex_lock_killable(struct mutex *lock);
  8207. extern int mutex_trylock(struct mutex *lock);
  8208. extern void mutex_unlock(struct mutex *lock);
  8209. +#endif /* !PREEMPT_RT_FULL */
  8210. +
  8211. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  8212. #endif /* __LINUX_MUTEX_H */
  8213. diff --git a/include/linux/mutex_rt.h b/include/linux/mutex_rt.h
  8214. new file mode 100644
  8215. index 000000000000..c38a44b14da5
  8216. --- /dev/null
  8217. +++ b/include/linux/mutex_rt.h
  8218. @@ -0,0 +1,84 @@
  8219. +#ifndef __LINUX_MUTEX_RT_H
  8220. +#define __LINUX_MUTEX_RT_H
  8221. +
  8222. +#ifndef __LINUX_MUTEX_H
  8223. +#error "Please include mutex.h"
  8224. +#endif
  8225. +
  8226. +#include <linux/rtmutex.h>
  8227. +
  8228. +/* FIXME: Just for __lockfunc */
  8229. +#include <linux/spinlock.h>
  8230. +
  8231. +struct mutex {
  8232. + struct rt_mutex lock;
  8233. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8234. + struct lockdep_map dep_map;
  8235. +#endif
  8236. +};
  8237. +
  8238. +#define __MUTEX_INITIALIZER(mutexname) \
  8239. + { \
  8240. + .lock = __RT_MUTEX_INITIALIZER(mutexname.lock) \
  8241. + __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
  8242. + }
  8243. +
  8244. +#define DEFINE_MUTEX(mutexname) \
  8245. + struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  8246. +
  8247. +extern void __mutex_do_init(struct mutex *lock, const char *name, struct lock_class_key *key);
  8248. +extern void __lockfunc _mutex_lock(struct mutex *lock);
  8249. +extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
  8250. +extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
  8251. +extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
  8252. +extern void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  8253. +extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
  8254. +extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
  8255. +extern int __lockfunc _mutex_trylock(struct mutex *lock);
  8256. +extern void __lockfunc _mutex_unlock(struct mutex *lock);
  8257. +
  8258. +#define mutex_is_locked(l) rt_mutex_is_locked(&(l)->lock)
  8259. +#define mutex_lock(l) _mutex_lock(l)
  8260. +#define mutex_lock_interruptible(l) _mutex_lock_interruptible(l)
  8261. +#define mutex_lock_killable(l) _mutex_lock_killable(l)
  8262. +#define mutex_trylock(l) _mutex_trylock(l)
  8263. +#define mutex_unlock(l) _mutex_unlock(l)
  8264. +#define mutex_destroy(l) rt_mutex_destroy(&(l)->lock)
  8265. +
  8266. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  8267. +# define mutex_lock_nested(l, s) _mutex_lock_nested(l, s)
  8268. +# define mutex_lock_interruptible_nested(l, s) \
  8269. + _mutex_lock_interruptible_nested(l, s)
  8270. +# define mutex_lock_killable_nested(l, s) \
  8271. + _mutex_lock_killable_nested(l, s)
  8272. +
  8273. +# define mutex_lock_nest_lock(lock, nest_lock) \
  8274. +do { \
  8275. + typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  8276. + _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  8277. +} while (0)
  8278. +
  8279. +#else
  8280. +# define mutex_lock_nested(l, s) _mutex_lock(l)
  8281. +# define mutex_lock_interruptible_nested(l, s) \
  8282. + _mutex_lock_interruptible(l)
  8283. +# define mutex_lock_killable_nested(l, s) \
  8284. + _mutex_lock_killable(l)
  8285. +# define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  8286. +#endif
  8287. +
  8288. +# define mutex_init(mutex) \
  8289. +do { \
  8290. + static struct lock_class_key __key; \
  8291. + \
  8292. + rt_mutex_init(&(mutex)->lock); \
  8293. + __mutex_do_init((mutex), #mutex, &__key); \
  8294. +} while (0)
  8295. +
  8296. +# define __mutex_init(mutex, name, key) \
  8297. +do { \
  8298. + rt_mutex_init(&(mutex)->lock); \
  8299. + __mutex_do_init((mutex), name, key); \
  8300. +} while (0)
  8301. +
  8302. +#endif
  8303. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  8304. index e8d79d4ebcfe..2ae8fa187016 100644
  8305. --- a/include/linux/netdevice.h
  8306. +++ b/include/linux/netdevice.h
  8307. @@ -2409,14 +2409,53 @@ void netdev_freemem(struct net_device *dev);
  8308. void synchronize_net(void);
  8309. int init_dummy_netdev(struct net_device *dev);
  8310. -DECLARE_PER_CPU(int, xmit_recursion);
  8311. #define XMIT_RECURSION_LIMIT 10
  8312. +#ifdef CONFIG_PREEMPT_RT_FULL
  8313. +static inline int dev_recursion_level(void)
  8314. +{
  8315. + return current->xmit_recursion;
  8316. +}
  8317. +
  8318. +static inline int xmit_rec_read(void)
  8319. +{
  8320. + return current->xmit_recursion;
  8321. +}
  8322. +
  8323. +static inline void xmit_rec_inc(void)
  8324. +{
  8325. + current->xmit_recursion++;
  8326. +}
  8327. +
  8328. +static inline void xmit_rec_dec(void)
  8329. +{
  8330. + current->xmit_recursion--;
  8331. +}
  8332. +
  8333. +#else
  8334. +
  8335. +DECLARE_PER_CPU(int, xmit_recursion);
  8336. static inline int dev_recursion_level(void)
  8337. {
  8338. return this_cpu_read(xmit_recursion);
  8339. }
  8340. +static inline int xmit_rec_read(void)
  8341. +{
  8342. + return __this_cpu_read(xmit_recursion);
  8343. +}
  8344. +
  8345. +static inline void xmit_rec_inc(void)
  8346. +{
  8347. + __this_cpu_inc(xmit_recursion);
  8348. +}
  8349. +
  8350. +static inline void xmit_rec_dec(void)
  8351. +{
  8352. + __this_cpu_dec(xmit_recursion);
  8353. +}
  8354. +#endif
  8355. +
  8356. struct net_device *dev_get_by_index(struct net *net, int ifindex);
  8357. struct net_device *__dev_get_by_index(struct net *net, int ifindex);
  8358. struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);
  8359. @@ -2794,6 +2833,7 @@ struct softnet_data {
  8360. unsigned int dropped;
  8361. struct sk_buff_head input_pkt_queue;
  8362. struct napi_struct backlog;
  8363. + struct sk_buff_head tofree_queue;
  8364. };
  8365. diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
  8366. index 2ad1a2b289b5..b4d10155af54 100644
  8367. --- a/include/linux/netfilter/x_tables.h
  8368. +++ b/include/linux/netfilter/x_tables.h
  8369. @@ -4,6 +4,7 @@
  8370. #include <linux/netdevice.h>
  8371. #include <linux/static_key.h>
  8372. +#include <linux/locallock.h>
  8373. #include <uapi/linux/netfilter/x_tables.h>
  8374. /* Test a struct->invflags and a boolean for inequality */
  8375. @@ -300,6 +301,8 @@ void xt_free_table_info(struct xt_table_info *info);
  8376. */
  8377. DECLARE_PER_CPU(seqcount_t, xt_recseq);
  8378. +DECLARE_LOCAL_IRQ_LOCK(xt_write_lock);
  8379. +
  8380. /* xt_tee_enabled - true if x_tables needs to handle reentrancy
  8381. *
  8382. * Enabled if current ip(6)tables ruleset has at least one -j TEE rule.
  8383. @@ -320,6 +323,9 @@ static inline unsigned int xt_write_recseq_begin(void)
  8384. {
  8385. unsigned int addend;
  8386. + /* RT protection */
  8387. + local_lock(xt_write_lock);
  8388. +
  8389. /*
  8390. * Low order bit of sequence is set if we already
  8391. * called xt_write_recseq_begin().
  8392. @@ -350,6 +356,7 @@ static inline void xt_write_recseq_end(unsigned int addend)
  8393. /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
  8394. smp_wmb();
  8395. __this_cpu_add(xt_recseq.sequence, addend);
  8396. + local_unlock(xt_write_lock);
  8397. }
  8398. /*
  8399. diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
  8400. index 810124b33327..d54ca43d571f 100644
  8401. --- a/include/linux/nfs_fs.h
  8402. +++ b/include/linux/nfs_fs.h
  8403. @@ -165,7 +165,11 @@ struct nfs_inode {
  8404. /* Readers: in-flight sillydelete RPC calls */
  8405. /* Writers: rmdir */
  8406. +#ifdef CONFIG_PREEMPT_RT_BASE
  8407. + struct semaphore rmdir_sem;
  8408. +#else
  8409. struct rw_semaphore rmdir_sem;
  8410. +#endif
  8411. #if IS_ENABLED(CONFIG_NFS_V4)
  8412. struct nfs4_cached_acl *nfs4_acl;
  8413. diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
  8414. index 7cc0deee5bde..a20f49ee69ee 100644
  8415. --- a/include/linux/nfs_xdr.h
  8416. +++ b/include/linux/nfs_xdr.h
  8417. @@ -1484,7 +1484,7 @@ struct nfs_unlinkdata {
  8418. struct nfs_removeargs args;
  8419. struct nfs_removeres res;
  8420. struct dentry *dentry;
  8421. - wait_queue_head_t wq;
  8422. + struct swait_queue_head wq;
  8423. struct rpc_cred *cred;
  8424. struct nfs_fattr dir_attr;
  8425. long timeout;
  8426. diff --git a/include/linux/notifier.h b/include/linux/notifier.h
  8427. index 4149868de4e6..babe5b9bcb91 100644
  8428. --- a/include/linux/notifier.h
  8429. +++ b/include/linux/notifier.h
  8430. @@ -6,7 +6,7 @@
  8431. *
  8432. * Alan Cox <Alan.Cox@linux.org>
  8433. */
  8434. -
  8435. +
  8436. #ifndef _LINUX_NOTIFIER_H
  8437. #define _LINUX_NOTIFIER_H
  8438. #include <linux/errno.h>
  8439. @@ -42,9 +42,7 @@
  8440. * in srcu_notifier_call_chain(): no cache bounces and no memory barriers.
  8441. * As compensation, srcu_notifier_chain_unregister() is rather expensive.
  8442. * SRCU notifier chains should be used when the chain will be called very
  8443. - * often but notifier_blocks will seldom be removed. Also, SRCU notifier
  8444. - * chains are slightly more difficult to use because they require special
  8445. - * runtime initialization.
  8446. + * often but notifier_blocks will seldom be removed.
  8447. */
  8448. struct notifier_block;
  8449. @@ -90,7 +88,7 @@ struct srcu_notifier_head {
  8450. (name)->head = NULL; \
  8451. } while (0)
  8452. -/* srcu_notifier_heads must be initialized and cleaned up dynamically */
  8453. +/* srcu_notifier_heads must be cleaned up dynamically */
  8454. extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  8455. #define srcu_cleanup_notifier_head(name) \
  8456. cleanup_srcu_struct(&(name)->srcu);
  8457. @@ -103,7 +101,13 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  8458. .head = NULL }
  8459. #define RAW_NOTIFIER_INIT(name) { \
  8460. .head = NULL }
  8461. -/* srcu_notifier_heads cannot be initialized statically */
  8462. +
  8463. +#define SRCU_NOTIFIER_INIT(name, pcpu) \
  8464. + { \
  8465. + .mutex = __MUTEX_INITIALIZER(name.mutex), \
  8466. + .head = NULL, \
  8467. + .srcu = __SRCU_STRUCT_INIT(name.srcu, pcpu), \
  8468. + }
  8469. #define ATOMIC_NOTIFIER_HEAD(name) \
  8470. struct atomic_notifier_head name = \
  8471. @@ -115,6 +119,18 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
  8472. struct raw_notifier_head name = \
  8473. RAW_NOTIFIER_INIT(name)
  8474. +#define _SRCU_NOTIFIER_HEAD(name, mod) \
  8475. + static DEFINE_PER_CPU(struct srcu_struct_array, \
  8476. + name##_head_srcu_array); \
  8477. + mod struct srcu_notifier_head name = \
  8478. + SRCU_NOTIFIER_INIT(name, name##_head_srcu_array)
  8479. +
  8480. +#define SRCU_NOTIFIER_HEAD(name) \
  8481. + _SRCU_NOTIFIER_HEAD(name, )
  8482. +
  8483. +#define SRCU_NOTIFIER_HEAD_STATIC(name) \
  8484. + _SRCU_NOTIFIER_HEAD(name, static)
  8485. +
  8486. #ifdef __KERNEL__
  8487. extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  8488. @@ -184,12 +200,12 @@ static inline int notifier_to_errno(int ret)
  8489. /*
  8490. * Declared notifiers so far. I can imagine quite a few more chains
  8491. - * over time (eg laptop power reset chains, reboot chain (to clean
  8492. + * over time (eg laptop power reset chains, reboot chain (to clean
  8493. * device units up), device [un]mount chain, module load/unload chain,
  8494. - * low memory chain, screenblank chain (for plug in modular screenblankers)
  8495. + * low memory chain, screenblank chain (for plug in modular screenblankers)
  8496. * VC switch chains (for loadable kernel svgalib VC switch helpers) etc...
  8497. */
  8498. -
  8499. +
  8500. /* CPU notfiers are defined in include/linux/cpu.h. */
  8501. /* netdevice notifiers are defined in include/linux/netdevice.h */
  8502. diff --git a/include/linux/percpu.h b/include/linux/percpu.h
  8503. index 56939d3f6e53..1c7e33fc83e4 100644
  8504. --- a/include/linux/percpu.h
  8505. +++ b/include/linux/percpu.h
  8506. @@ -18,6 +18,35 @@
  8507. #define PERCPU_MODULE_RESERVE 0
  8508. #endif
  8509. +#ifdef CONFIG_PREEMPT_RT_FULL
  8510. +
  8511. +#define get_local_var(var) (*({ \
  8512. + migrate_disable(); \
  8513. + this_cpu_ptr(&var); }))
  8514. +
  8515. +#define put_local_var(var) do { \
  8516. + (void)&(var); \
  8517. + migrate_enable(); \
  8518. +} while (0)
  8519. +
  8520. +# define get_local_ptr(var) ({ \
  8521. + migrate_disable(); \
  8522. + this_cpu_ptr(var); })
  8523. +
  8524. +# define put_local_ptr(var) do { \
  8525. + (void)(var); \
  8526. + migrate_enable(); \
  8527. +} while (0)
  8528. +
  8529. +#else
  8530. +
  8531. +#define get_local_var(var) get_cpu_var(var)
  8532. +#define put_local_var(var) put_cpu_var(var)
  8533. +#define get_local_ptr(var) get_cpu_ptr(var)
  8534. +#define put_local_ptr(var) put_cpu_ptr(var)
  8535. +
  8536. +#endif
  8537. +
  8538. /* minimum unit size, also is the maximum supported allocation size */
  8539. #define PCPU_MIN_UNIT_SIZE PFN_ALIGN(32 << 10)
  8540. diff --git a/include/linux/pid.h b/include/linux/pid.h
  8541. index 23705a53abba..2cc64b779f03 100644
  8542. --- a/include/linux/pid.h
  8543. +++ b/include/linux/pid.h
  8544. @@ -2,6 +2,7 @@
  8545. #define _LINUX_PID_H
  8546. #include <linux/rcupdate.h>
  8547. +#include <linux/atomic.h>
  8548. enum pid_type
  8549. {
  8550. diff --git a/include/linux/preempt.h b/include/linux/preempt.h
  8551. index 75e4e30677f1..1cfb1cb72354 100644
  8552. --- a/include/linux/preempt.h
  8553. +++ b/include/linux/preempt.h
  8554. @@ -50,7 +50,11 @@
  8555. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  8556. #define NMI_OFFSET (1UL << NMI_SHIFT)
  8557. -#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  8558. +#ifndef CONFIG_PREEMPT_RT_FULL
  8559. +# define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  8560. +#else
  8561. +# define SOFTIRQ_DISABLE_OFFSET (0)
  8562. +#endif
  8563. /* We use the MSB mostly because its available */
  8564. #define PREEMPT_NEED_RESCHED 0x80000000
  8565. @@ -59,9 +63,15 @@
  8566. #include <asm/preempt.h>
  8567. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  8568. -#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  8569. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  8570. | NMI_MASK))
  8571. +#ifndef CONFIG_PREEMPT_RT_FULL
  8572. +# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  8573. +# define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  8574. +#else
  8575. +# define softirq_count() (0UL)
  8576. +extern int in_serving_softirq(void);
  8577. +#endif
  8578. /*
  8579. * Are we doing bottom half or hardware interrupt processing?
  8580. @@ -72,7 +82,6 @@
  8581. #define in_irq() (hardirq_count())
  8582. #define in_softirq() (softirq_count())
  8583. #define in_interrupt() (irq_count())
  8584. -#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  8585. /*
  8586. * Are we in NMI context?
  8587. @@ -91,7 +100,11 @@
  8588. /*
  8589. * The preempt_count offset after spin_lock()
  8590. */
  8591. +#if !defined(CONFIG_PREEMPT_RT_FULL)
  8592. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  8593. +#else
  8594. +#define PREEMPT_LOCK_OFFSET 0
  8595. +#endif
  8596. /*
  8597. * The preempt_count offset needed for things like:
  8598. @@ -140,6 +153,20 @@ extern void preempt_count_sub(int val);
  8599. #define preempt_count_inc() preempt_count_add(1)
  8600. #define preempt_count_dec() preempt_count_sub(1)
  8601. +#ifdef CONFIG_PREEMPT_LAZY
  8602. +#define add_preempt_lazy_count(val) do { preempt_lazy_count() += (val); } while (0)
  8603. +#define sub_preempt_lazy_count(val) do { preempt_lazy_count() -= (val); } while (0)
  8604. +#define inc_preempt_lazy_count() add_preempt_lazy_count(1)
  8605. +#define dec_preempt_lazy_count() sub_preempt_lazy_count(1)
  8606. +#define preempt_lazy_count() (current_thread_info()->preempt_lazy_count)
  8607. +#else
  8608. +#define add_preempt_lazy_count(val) do { } while (0)
  8609. +#define sub_preempt_lazy_count(val) do { } while (0)
  8610. +#define inc_preempt_lazy_count() do { } while (0)
  8611. +#define dec_preempt_lazy_count() do { } while (0)
  8612. +#define preempt_lazy_count() (0)
  8613. +#endif
  8614. +
  8615. #ifdef CONFIG_PREEMPT_COUNT
  8616. #define preempt_disable() \
  8617. @@ -148,13 +175,25 @@ do { \
  8618. barrier(); \
  8619. } while (0)
  8620. +#define preempt_lazy_disable() \
  8621. +do { \
  8622. + inc_preempt_lazy_count(); \
  8623. + barrier(); \
  8624. +} while (0)
  8625. +
  8626. #define sched_preempt_enable_no_resched() \
  8627. do { \
  8628. barrier(); \
  8629. preempt_count_dec(); \
  8630. } while (0)
  8631. -#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  8632. +#ifdef CONFIG_PREEMPT_RT_BASE
  8633. +# define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  8634. +# define preempt_check_resched_rt() preempt_check_resched()
  8635. +#else
  8636. +# define preempt_enable_no_resched() preempt_enable()
  8637. +# define preempt_check_resched_rt() barrier();
  8638. +#endif
  8639. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  8640. @@ -179,6 +218,13 @@ do { \
  8641. __preempt_schedule(); \
  8642. } while (0)
  8643. +#define preempt_lazy_enable() \
  8644. +do { \
  8645. + dec_preempt_lazy_count(); \
  8646. + barrier(); \
  8647. + preempt_check_resched(); \
  8648. +} while (0)
  8649. +
  8650. #else /* !CONFIG_PREEMPT */
  8651. #define preempt_enable() \
  8652. do { \
  8653. @@ -224,6 +270,7 @@ do { \
  8654. #define preempt_disable_notrace() barrier()
  8655. #define preempt_enable_no_resched_notrace() barrier()
  8656. #define preempt_enable_notrace() barrier()
  8657. +#define preempt_check_resched_rt() barrier()
  8658. #define preemptible() 0
  8659. #endif /* CONFIG_PREEMPT_COUNT */
  8660. @@ -244,10 +291,31 @@ do { \
  8661. } while (0)
  8662. #define preempt_fold_need_resched() \
  8663. do { \
  8664. - if (tif_need_resched()) \
  8665. + if (tif_need_resched_now()) \
  8666. set_preempt_need_resched(); \
  8667. } while (0)
  8668. +#ifdef CONFIG_PREEMPT_RT_FULL
  8669. +# define preempt_disable_rt() preempt_disable()
  8670. +# define preempt_enable_rt() preempt_enable()
  8671. +# define preempt_disable_nort() barrier()
  8672. +# define preempt_enable_nort() barrier()
  8673. +# ifdef CONFIG_SMP
  8674. + extern void migrate_disable(void);
  8675. + extern void migrate_enable(void);
  8676. +# else /* CONFIG_SMP */
  8677. +# define migrate_disable() barrier()
  8678. +# define migrate_enable() barrier()
  8679. +# endif /* CONFIG_SMP */
  8680. +#else
  8681. +# define preempt_disable_rt() barrier()
  8682. +# define preempt_enable_rt() barrier()
  8683. +# define preempt_disable_nort() preempt_disable()
  8684. +# define preempt_enable_nort() preempt_enable()
  8685. +# define migrate_disable() preempt_disable()
  8686. +# define migrate_enable() preempt_enable()
  8687. +#endif
  8688. +
  8689. #ifdef CONFIG_PREEMPT_NOTIFIERS
  8690. struct preempt_notifier;
  8691. diff --git a/include/linux/printk.h b/include/linux/printk.h
  8692. index 696a56be7d3e..310aa321ef0c 100644
  8693. --- a/include/linux/printk.h
  8694. +++ b/include/linux/printk.h
  8695. @@ -125,9 +125,11 @@ struct va_format {
  8696. #ifdef CONFIG_EARLY_PRINTK
  8697. extern asmlinkage __printf(1, 2)
  8698. void early_printk(const char *fmt, ...);
  8699. +extern void printk_kill(void);
  8700. #else
  8701. static inline __printf(1, 2) __cold
  8702. void early_printk(const char *s, ...) { }
  8703. +static inline void printk_kill(void) { }
  8704. #endif
  8705. #ifdef CONFIG_PRINTK_NMI
  8706. diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h
  8707. index 52b97db93830..fd9ea1c68db6 100644
  8708. --- a/include/linux/radix-tree.h
  8709. +++ b/include/linux/radix-tree.h
  8710. @@ -289,9 +289,19 @@ unsigned int radix_tree_gang_lookup(struct radix_tree_root *root,
  8711. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  8712. void ***results, unsigned long *indices,
  8713. unsigned long first_index, unsigned int max_items);
  8714. +#ifdef CONFIG_PREEMPT_RT_FULL
  8715. +static inline int radix_tree_preload(gfp_t gm) { return 0; }
  8716. +static inline int radix_tree_maybe_preload(gfp_t gfp_mask) { return 0; }
  8717. +static inline int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
  8718. +{
  8719. + return 0;
  8720. +};
  8721. +
  8722. +#else
  8723. int radix_tree_preload(gfp_t gfp_mask);
  8724. int radix_tree_maybe_preload(gfp_t gfp_mask);
  8725. int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
  8726. +#endif
  8727. void radix_tree_init(void);
  8728. void *radix_tree_tag_set(struct radix_tree_root *root,
  8729. unsigned long index, unsigned int tag);
  8730. @@ -316,7 +326,7 @@ unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  8731. static inline void radix_tree_preload_end(void)
  8732. {
  8733. - preempt_enable();
  8734. + preempt_enable_nort();
  8735. }
  8736. /**
  8737. diff --git a/include/linux/random.h b/include/linux/random.h
  8738. index 3d6e9815cd85..f6e8860b6494 100644
  8739. --- a/include/linux/random.h
  8740. +++ b/include/linux/random.h
  8741. @@ -20,7 +20,7 @@ struct random_ready_callback {
  8742. extern void add_device_randomness(const void *, unsigned int);
  8743. extern void add_input_randomness(unsigned int type, unsigned int code,
  8744. unsigned int value);
  8745. -extern void add_interrupt_randomness(int irq, int irq_flags);
  8746. +extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip);
  8747. extern void get_random_bytes(void *buf, int nbytes);
  8748. extern int add_random_ready_callback(struct random_ready_callback *rdy);
  8749. diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
  8750. index e585018498d5..25c64474fc27 100644
  8751. --- a/include/linux/rbtree.h
  8752. +++ b/include/linux/rbtree.h
  8753. @@ -31,7 +31,7 @@
  8754. #include <linux/kernel.h>
  8755. #include <linux/stddef.h>
  8756. -#include <linux/rcupdate.h>
  8757. +#include <linux/rcu_assign_pointer.h>
  8758. struct rb_node {
  8759. unsigned long __rb_parent_color;
  8760. diff --git a/include/linux/rbtree_augmented.h b/include/linux/rbtree_augmented.h
  8761. index d076183e49be..36bfb4dd57ae 100644
  8762. --- a/include/linux/rbtree_augmented.h
  8763. +++ b/include/linux/rbtree_augmented.h
  8764. @@ -26,6 +26,7 @@
  8765. #include <linux/compiler.h>
  8766. #include <linux/rbtree.h>
  8767. +#include <linux/rcupdate.h>
  8768. /*
  8769. * Please note - only struct rb_augment_callbacks and the prototypes for
  8770. diff --git a/include/linux/rcu_assign_pointer.h b/include/linux/rcu_assign_pointer.h
  8771. new file mode 100644
  8772. index 000000000000..7066962a4379
  8773. --- /dev/null
  8774. +++ b/include/linux/rcu_assign_pointer.h
  8775. @@ -0,0 +1,54 @@
  8776. +#ifndef __LINUX_RCU_ASSIGN_POINTER_H__
  8777. +#define __LINUX_RCU_ASSIGN_POINTER_H__
  8778. +#include <linux/compiler.h>
  8779. +#include <asm/barrier.h>
  8780. +
  8781. +/**
  8782. + * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  8783. + * @v: The value to statically initialize with.
  8784. + */
  8785. +#define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  8786. +
  8787. +/**
  8788. + * rcu_assign_pointer() - assign to RCU-protected pointer
  8789. + * @p: pointer to assign to
  8790. + * @v: value to assign (publish)
  8791. + *
  8792. + * Assigns the specified value to the specified RCU-protected
  8793. + * pointer, ensuring that any concurrent RCU readers will see
  8794. + * any prior initialization.
  8795. + *
  8796. + * Inserts memory barriers on architectures that require them
  8797. + * (which is most of them), and also prevents the compiler from
  8798. + * reordering the code that initializes the structure after the pointer
  8799. + * assignment. More importantly, this call documents which pointers
  8800. + * will be dereferenced by RCU read-side code.
  8801. + *
  8802. + * In some special cases, you may use RCU_INIT_POINTER() instead
  8803. + * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  8804. + * to the fact that it does not constrain either the CPU or the compiler.
  8805. + * That said, using RCU_INIT_POINTER() when you should have used
  8806. + * rcu_assign_pointer() is a very bad thing that results in
  8807. + * impossible-to-diagnose memory corruption. So please be careful.
  8808. + * See the RCU_INIT_POINTER() comment header for details.
  8809. + *
  8810. + * Note that rcu_assign_pointer() evaluates each of its arguments only
  8811. + * once, appearances notwithstanding. One of the "extra" evaluations
  8812. + * is in typeof() and the other visible only to sparse (__CHECKER__),
  8813. + * neither of which actually execute the argument. As with most cpp
  8814. + * macros, this execute-arguments-only-once property is important, so
  8815. + * please be careful when making changes to rcu_assign_pointer() and the
  8816. + * other macros that it invokes.
  8817. + */
  8818. +#define rcu_assign_pointer(p, v) \
  8819. +({ \
  8820. + uintptr_t _r_a_p__v = (uintptr_t)(v); \
  8821. + \
  8822. + if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  8823. + WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  8824. + else \
  8825. + smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  8826. + _r_a_p__v; \
  8827. +})
  8828. +
  8829. +#endif
  8830. diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
  8831. index 1aa62e1a761b..2a614acb433e 100644
  8832. --- a/include/linux/rcupdate.h
  8833. +++ b/include/linux/rcupdate.h
  8834. @@ -46,6 +46,7 @@
  8835. #include <linux/compiler.h>
  8836. #include <linux/ktime.h>
  8837. #include <linux/irqflags.h>
  8838. +#include <linux/rcu_assign_pointer.h>
  8839. #include <asm/barrier.h>
  8840. @@ -178,6 +179,9 @@ void call_rcu(struct rcu_head *head,
  8841. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  8842. +#ifdef CONFIG_PREEMPT_RT_FULL
  8843. +#define call_rcu_bh call_rcu
  8844. +#else
  8845. /**
  8846. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  8847. * @head: structure to be used for queueing the RCU updates.
  8848. @@ -201,6 +205,7 @@ void call_rcu(struct rcu_head *head,
  8849. */
  8850. void call_rcu_bh(struct rcu_head *head,
  8851. rcu_callback_t func);
  8852. +#endif
  8853. /**
  8854. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  8855. @@ -301,6 +306,11 @@ void synchronize_rcu(void);
  8856. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  8857. */
  8858. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  8859. +#ifndef CONFIG_PREEMPT_RT_FULL
  8860. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  8861. +#else
  8862. +static inline int sched_rcu_preempt_depth(void) { return 0; }
  8863. +#endif
  8864. #else /* #ifdef CONFIG_PREEMPT_RCU */
  8865. @@ -326,6 +336,8 @@ static inline int rcu_preempt_depth(void)
  8866. return 0;
  8867. }
  8868. +#define sched_rcu_preempt_depth() rcu_preempt_depth()
  8869. +
  8870. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  8871. /* Internal to kernel */
  8872. @@ -500,7 +512,14 @@ extern struct lockdep_map rcu_callback_map;
  8873. int debug_lockdep_rcu_enabled(void);
  8874. int rcu_read_lock_held(void);
  8875. +#ifdef CONFIG_PREEMPT_RT_FULL
  8876. +static inline int rcu_read_lock_bh_held(void)
  8877. +{
  8878. + return rcu_read_lock_held();
  8879. +}
  8880. +#else
  8881. int rcu_read_lock_bh_held(void);
  8882. +#endif
  8883. /**
  8884. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  8885. @@ -621,54 +640,6 @@ static inline void rcu_preempt_sleep_check(void)
  8886. })
  8887. /**
  8888. - * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  8889. - * @v: The value to statically initialize with.
  8890. - */
  8891. -#define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  8892. -
  8893. -/**
  8894. - * rcu_assign_pointer() - assign to RCU-protected pointer
  8895. - * @p: pointer to assign to
  8896. - * @v: value to assign (publish)
  8897. - *
  8898. - * Assigns the specified value to the specified RCU-protected
  8899. - * pointer, ensuring that any concurrent RCU readers will see
  8900. - * any prior initialization.
  8901. - *
  8902. - * Inserts memory barriers on architectures that require them
  8903. - * (which is most of them), and also prevents the compiler from
  8904. - * reordering the code that initializes the structure after the pointer
  8905. - * assignment. More importantly, this call documents which pointers
  8906. - * will be dereferenced by RCU read-side code.
  8907. - *
  8908. - * In some special cases, you may use RCU_INIT_POINTER() instead
  8909. - * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  8910. - * to the fact that it does not constrain either the CPU or the compiler.
  8911. - * That said, using RCU_INIT_POINTER() when you should have used
  8912. - * rcu_assign_pointer() is a very bad thing that results in
  8913. - * impossible-to-diagnose memory corruption. So please be careful.
  8914. - * See the RCU_INIT_POINTER() comment header for details.
  8915. - *
  8916. - * Note that rcu_assign_pointer() evaluates each of its arguments only
  8917. - * once, appearances notwithstanding. One of the "extra" evaluations
  8918. - * is in typeof() and the other visible only to sparse (__CHECKER__),
  8919. - * neither of which actually execute the argument. As with most cpp
  8920. - * macros, this execute-arguments-only-once property is important, so
  8921. - * please be careful when making changes to rcu_assign_pointer() and the
  8922. - * other macros that it invokes.
  8923. - */
  8924. -#define rcu_assign_pointer(p, v) \
  8925. -({ \
  8926. - uintptr_t _r_a_p__v = (uintptr_t)(v); \
  8927. - \
  8928. - if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  8929. - WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  8930. - else \
  8931. - smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  8932. - _r_a_p__v; \
  8933. -})
  8934. -
  8935. -/**
  8936. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  8937. * @p: The pointer to read
  8938. *
  8939. @@ -946,10 +917,14 @@ static inline void rcu_read_unlock(void)
  8940. static inline void rcu_read_lock_bh(void)
  8941. {
  8942. local_bh_disable();
  8943. +#ifdef CONFIG_PREEMPT_RT_FULL
  8944. + rcu_read_lock();
  8945. +#else
  8946. __acquire(RCU_BH);
  8947. rcu_lock_acquire(&rcu_bh_lock_map);
  8948. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  8949. "rcu_read_lock_bh() used illegally while idle");
  8950. +#endif
  8951. }
  8952. /*
  8953. @@ -959,10 +934,14 @@ static inline void rcu_read_lock_bh(void)
  8954. */
  8955. static inline void rcu_read_unlock_bh(void)
  8956. {
  8957. +#ifdef CONFIG_PREEMPT_RT_FULL
  8958. + rcu_read_unlock();
  8959. +#else
  8960. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  8961. "rcu_read_unlock_bh() used illegally while idle");
  8962. rcu_lock_release(&rcu_bh_lock_map);
  8963. __release(RCU_BH);
  8964. +#endif
  8965. local_bh_enable();
  8966. }
  8967. diff --git a/include/linux/rcutree.h b/include/linux/rcutree.h
  8968. index 63a4e4cf40a5..08ab12df2863 100644
  8969. --- a/include/linux/rcutree.h
  8970. +++ b/include/linux/rcutree.h
  8971. @@ -44,7 +44,11 @@ static inline void rcu_virt_note_context_switch(int cpu)
  8972. rcu_note_context_switch();
  8973. }
  8974. +#ifdef CONFIG_PREEMPT_RT_FULL
  8975. +# define synchronize_rcu_bh synchronize_rcu
  8976. +#else
  8977. void synchronize_rcu_bh(void);
  8978. +#endif
  8979. void synchronize_sched_expedited(void);
  8980. void synchronize_rcu_expedited(void);
  8981. @@ -72,7 +76,11 @@ static inline void synchronize_rcu_bh_expedited(void)
  8982. }
  8983. void rcu_barrier(void);
  8984. +#ifdef CONFIG_PREEMPT_RT_FULL
  8985. +# define rcu_barrier_bh rcu_barrier
  8986. +#else
  8987. void rcu_barrier_bh(void);
  8988. +#endif
  8989. void rcu_barrier_sched(void);
  8990. unsigned long get_state_synchronize_rcu(void);
  8991. void cond_synchronize_rcu(unsigned long oldstate);
  8992. @@ -82,17 +90,14 @@ void cond_synchronize_sched(unsigned long oldstate);
  8993. extern unsigned long rcutorture_testseq;
  8994. extern unsigned long rcutorture_vernum;
  8995. unsigned long rcu_batches_started(void);
  8996. -unsigned long rcu_batches_started_bh(void);
  8997. unsigned long rcu_batches_started_sched(void);
  8998. unsigned long rcu_batches_completed(void);
  8999. -unsigned long rcu_batches_completed_bh(void);
  9000. unsigned long rcu_batches_completed_sched(void);
  9001. unsigned long rcu_exp_batches_completed(void);
  9002. unsigned long rcu_exp_batches_completed_sched(void);
  9003. void show_rcu_gp_kthreads(void);
  9004. void rcu_force_quiescent_state(void);
  9005. -void rcu_bh_force_quiescent_state(void);
  9006. void rcu_sched_force_quiescent_state(void);
  9007. void rcu_idle_enter(void);
  9008. @@ -109,6 +114,16 @@ extern int rcu_scheduler_active __read_mostly;
  9009. bool rcu_is_watching(void);
  9010. +#ifndef CONFIG_PREEMPT_RT_FULL
  9011. +void rcu_bh_force_quiescent_state(void);
  9012. +unsigned long rcu_batches_started_bh(void);
  9013. +unsigned long rcu_batches_completed_bh(void);
  9014. +#else
  9015. +# define rcu_bh_force_quiescent_state rcu_force_quiescent_state
  9016. +# define rcu_batches_completed_bh rcu_batches_completed
  9017. +# define rcu_batches_started_bh rcu_batches_completed
  9018. +#endif
  9019. +
  9020. void rcu_all_qs(void);
  9021. /* RCUtree hotplug events */
  9022. diff --git a/include/linux/rtmutex.h b/include/linux/rtmutex.h
  9023. index 1abba5ce2a2f..30211c627511 100644
  9024. --- a/include/linux/rtmutex.h
  9025. +++ b/include/linux/rtmutex.h
  9026. @@ -13,11 +13,15 @@
  9027. #define __LINUX_RT_MUTEX_H
  9028. #include <linux/linkage.h>
  9029. +#include <linux/spinlock_types_raw.h>
  9030. #include <linux/rbtree.h>
  9031. -#include <linux/spinlock_types.h>
  9032. extern int max_lock_depth; /* for sysctl */
  9033. +#ifdef CONFIG_DEBUG_MUTEXES
  9034. +#include <linux/debug_locks.h>
  9035. +#endif
  9036. +
  9037. /**
  9038. * The rt_mutex structure
  9039. *
  9040. @@ -31,8 +35,8 @@ struct rt_mutex {
  9041. struct rb_root waiters;
  9042. struct rb_node *waiters_leftmost;
  9043. struct task_struct *owner;
  9044. -#ifdef CONFIG_DEBUG_RT_MUTEXES
  9045. int save_state;
  9046. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  9047. const char *name, *file;
  9048. int line;
  9049. void *magic;
  9050. @@ -55,22 +59,33 @@ struct hrtimer_sleeper;
  9051. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  9052. #endif
  9053. +# define rt_mutex_init(mutex) \
  9054. + do { \
  9055. + raw_spin_lock_init(&(mutex)->wait_lock); \
  9056. + __rt_mutex_init(mutex, #mutex); \
  9057. + } while (0)
  9058. +
  9059. #ifdef CONFIG_DEBUG_RT_MUTEXES
  9060. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  9061. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  9062. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__)
  9063. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  9064. #else
  9065. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  9066. -# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL)
  9067. # define rt_mutex_debug_task_free(t) do { } while (0)
  9068. #endif
  9069. -#define __RT_MUTEX_INITIALIZER(mutexname) \
  9070. - { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  9071. +#define __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  9072. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  9073. , .waiters = RB_ROOT \
  9074. , .owner = NULL \
  9075. - __DEBUG_RT_MUTEX_INITIALIZER(mutexname)}
  9076. + __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
  9077. +
  9078. +#define __RT_MUTEX_INITIALIZER(mutexname) \
  9079. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) }
  9080. +
  9081. +#define __RT_MUTEX_INITIALIZER_SAVE_STATE(mutexname) \
  9082. + { __RT_MUTEX_INITIALIZER_PLAIN(mutexname) \
  9083. + , .save_state = 1 }
  9084. #define DEFINE_RT_MUTEX(mutexname) \
  9085. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  9086. @@ -91,6 +106,7 @@ extern void rt_mutex_destroy(struct rt_mutex *lock);
  9087. extern void rt_mutex_lock(struct rt_mutex *lock);
  9088. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  9089. +extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  9090. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  9091. struct hrtimer_sleeper *timeout);
  9092. diff --git a/include/linux/rwlock_rt.h b/include/linux/rwlock_rt.h
  9093. new file mode 100644
  9094. index 000000000000..49ed2d45d3be
  9095. --- /dev/null
  9096. +++ b/include/linux/rwlock_rt.h
  9097. @@ -0,0 +1,99 @@
  9098. +#ifndef __LINUX_RWLOCK_RT_H
  9099. +#define __LINUX_RWLOCK_RT_H
  9100. +
  9101. +#ifndef __LINUX_SPINLOCK_H
  9102. +#error Do not include directly. Use spinlock.h
  9103. +#endif
  9104. +
  9105. +#define rwlock_init(rwl) \
  9106. +do { \
  9107. + static struct lock_class_key __key; \
  9108. + \
  9109. + rt_mutex_init(&(rwl)->lock); \
  9110. + __rt_rwlock_init(rwl, #rwl, &__key); \
  9111. +} while (0)
  9112. +
  9113. +extern void __lockfunc rt_write_lock(rwlock_t *rwlock);
  9114. +extern void __lockfunc rt_read_lock(rwlock_t *rwlock);
  9115. +extern int __lockfunc rt_write_trylock(rwlock_t *rwlock);
  9116. +extern int __lockfunc rt_write_trylock_irqsave(rwlock_t *trylock, unsigned long *flags);
  9117. +extern int __lockfunc rt_read_trylock(rwlock_t *rwlock);
  9118. +extern void __lockfunc rt_write_unlock(rwlock_t *rwlock);
  9119. +extern void __lockfunc rt_read_unlock(rwlock_t *rwlock);
  9120. +extern unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock);
  9121. +extern unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock);
  9122. +extern void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key);
  9123. +
  9124. +#define read_trylock(lock) __cond_lock(lock, rt_read_trylock(lock))
  9125. +#define write_trylock(lock) __cond_lock(lock, rt_write_trylock(lock))
  9126. +
  9127. +#define write_trylock_irqsave(lock, flags) \
  9128. + __cond_lock(lock, rt_write_trylock_irqsave(lock, &flags))
  9129. +
  9130. +#define read_lock_irqsave(lock, flags) \
  9131. + do { \
  9132. + typecheck(unsigned long, flags); \
  9133. + flags = rt_read_lock_irqsave(lock); \
  9134. + } while (0)
  9135. +
  9136. +#define write_lock_irqsave(lock, flags) \
  9137. + do { \
  9138. + typecheck(unsigned long, flags); \
  9139. + flags = rt_write_lock_irqsave(lock); \
  9140. + } while (0)
  9141. +
  9142. +#define read_lock(lock) rt_read_lock(lock)
  9143. +
  9144. +#define read_lock_bh(lock) \
  9145. + do { \
  9146. + local_bh_disable(); \
  9147. + rt_read_lock(lock); \
  9148. + } while (0)
  9149. +
  9150. +#define read_lock_irq(lock) read_lock(lock)
  9151. +
  9152. +#define write_lock(lock) rt_write_lock(lock)
  9153. +
  9154. +#define write_lock_bh(lock) \
  9155. + do { \
  9156. + local_bh_disable(); \
  9157. + rt_write_lock(lock); \
  9158. + } while (0)
  9159. +
  9160. +#define write_lock_irq(lock) write_lock(lock)
  9161. +
  9162. +#define read_unlock(lock) rt_read_unlock(lock)
  9163. +
  9164. +#define read_unlock_bh(lock) \
  9165. + do { \
  9166. + rt_read_unlock(lock); \
  9167. + local_bh_enable(); \
  9168. + } while (0)
  9169. +
  9170. +#define read_unlock_irq(lock) read_unlock(lock)
  9171. +
  9172. +#define write_unlock(lock) rt_write_unlock(lock)
  9173. +
  9174. +#define write_unlock_bh(lock) \
  9175. + do { \
  9176. + rt_write_unlock(lock); \
  9177. + local_bh_enable(); \
  9178. + } while (0)
  9179. +
  9180. +#define write_unlock_irq(lock) write_unlock(lock)
  9181. +
  9182. +#define read_unlock_irqrestore(lock, flags) \
  9183. + do { \
  9184. + typecheck(unsigned long, flags); \
  9185. + (void) flags; \
  9186. + rt_read_unlock(lock); \
  9187. + } while (0)
  9188. +
  9189. +#define write_unlock_irqrestore(lock, flags) \
  9190. + do { \
  9191. + typecheck(unsigned long, flags); \
  9192. + (void) flags; \
  9193. + rt_write_unlock(lock); \
  9194. + } while (0)
  9195. +
  9196. +#endif
  9197. diff --git a/include/linux/rwlock_types.h b/include/linux/rwlock_types.h
  9198. index cc0072e93e36..5317cd957292 100644
  9199. --- a/include/linux/rwlock_types.h
  9200. +++ b/include/linux/rwlock_types.h
  9201. @@ -1,6 +1,10 @@
  9202. #ifndef __LINUX_RWLOCK_TYPES_H
  9203. #define __LINUX_RWLOCK_TYPES_H
  9204. +#if !defined(__LINUX_SPINLOCK_TYPES_H)
  9205. +# error "Do not include directly, include spinlock_types.h"
  9206. +#endif
  9207. +
  9208. /*
  9209. * include/linux/rwlock_types.h - generic rwlock type definitions
  9210. * and initializers
  9211. diff --git a/include/linux/rwlock_types_rt.h b/include/linux/rwlock_types_rt.h
  9212. new file mode 100644
  9213. index 000000000000..51b28d775fe1
  9214. --- /dev/null
  9215. +++ b/include/linux/rwlock_types_rt.h
  9216. @@ -0,0 +1,33 @@
  9217. +#ifndef __LINUX_RWLOCK_TYPES_RT_H
  9218. +#define __LINUX_RWLOCK_TYPES_RT_H
  9219. +
  9220. +#ifndef __LINUX_SPINLOCK_TYPES_H
  9221. +#error "Do not include directly. Include spinlock_types.h instead"
  9222. +#endif
  9223. +
  9224. +/*
  9225. + * rwlocks - rtmutex which allows single reader recursion
  9226. + */
  9227. +typedef struct {
  9228. + struct rt_mutex lock;
  9229. + int read_depth;
  9230. + unsigned int break_lock;
  9231. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9232. + struct lockdep_map dep_map;
  9233. +#endif
  9234. +} rwlock_t;
  9235. +
  9236. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9237. +# define RW_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  9238. +#else
  9239. +# define RW_DEP_MAP_INIT(lockname)
  9240. +#endif
  9241. +
  9242. +#define __RW_LOCK_UNLOCKED(name) \
  9243. + { .lock = __RT_MUTEX_INITIALIZER_SAVE_STATE(name.lock), \
  9244. + RW_DEP_MAP_INIT(name) }
  9245. +
  9246. +#define DEFINE_RWLOCK(name) \
  9247. + rwlock_t name = __RW_LOCK_UNLOCKED(name)
  9248. +
  9249. +#endif
  9250. diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
  9251. index dd1d14250340..8e1f44ff1f2f 100644
  9252. --- a/include/linux/rwsem.h
  9253. +++ b/include/linux/rwsem.h
  9254. @@ -19,6 +19,10 @@
  9255. #include <linux/osq_lock.h>
  9256. #endif
  9257. +#ifdef CONFIG_PREEMPT_RT_FULL
  9258. +#include <linux/rwsem_rt.h>
  9259. +#else /* PREEMPT_RT_FULL */
  9260. +
  9261. struct rw_semaphore;
  9262. #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
  9263. @@ -184,4 +188,6 @@ extern void up_read_non_owner(struct rw_semaphore *sem);
  9264. # define up_read_non_owner(sem) up_read(sem)
  9265. #endif
  9266. +#endif /* !PREEMPT_RT_FULL */
  9267. +
  9268. #endif /* _LINUX_RWSEM_H */
  9269. diff --git a/include/linux/rwsem_rt.h b/include/linux/rwsem_rt.h
  9270. new file mode 100644
  9271. index 000000000000..e26bd95a57c3
  9272. --- /dev/null
  9273. +++ b/include/linux/rwsem_rt.h
  9274. @@ -0,0 +1,167 @@
  9275. +#ifndef _LINUX_RWSEM_RT_H
  9276. +#define _LINUX_RWSEM_RT_H
  9277. +
  9278. +#ifndef _LINUX_RWSEM_H
  9279. +#error "Include rwsem.h"
  9280. +#endif
  9281. +
  9282. +/*
  9283. + * RW-semaphores are a spinlock plus a reader-depth count.
  9284. + *
  9285. + * Note that the semantics are different from the usual
  9286. + * Linux rw-sems, in PREEMPT_RT mode we do not allow
  9287. + * multiple readers to hold the lock at once, we only allow
  9288. + * a read-lock owner to read-lock recursively. This is
  9289. + * better for latency, makes the implementation inherently
  9290. + * fair and makes it simpler as well.
  9291. + */
  9292. +
  9293. +#include <linux/rtmutex.h>
  9294. +
  9295. +struct rw_semaphore {
  9296. + struct rt_mutex lock;
  9297. + int read_depth;
  9298. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9299. + struct lockdep_map dep_map;
  9300. +#endif
  9301. +};
  9302. +
  9303. +#define __RWSEM_INITIALIZER(name) \
  9304. + { .lock = __RT_MUTEX_INITIALIZER(name.lock), \
  9305. + RW_DEP_MAP_INIT(name) }
  9306. +
  9307. +#define DECLARE_RWSEM(lockname) \
  9308. + struct rw_semaphore lockname = __RWSEM_INITIALIZER(lockname)
  9309. +
  9310. +extern void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  9311. + struct lock_class_key *key);
  9312. +
  9313. +#define __rt_init_rwsem(sem, name, key) \
  9314. + do { \
  9315. + rt_mutex_init(&(sem)->lock); \
  9316. + __rt_rwsem_init((sem), (name), (key));\
  9317. + } while (0)
  9318. +
  9319. +#define __init_rwsem(sem, name, key) __rt_init_rwsem(sem, name, key)
  9320. +
  9321. +# define rt_init_rwsem(sem) \
  9322. +do { \
  9323. + static struct lock_class_key __key; \
  9324. + \
  9325. + __rt_init_rwsem((sem), #sem, &__key); \
  9326. +} while (0)
  9327. +
  9328. +extern void rt_down_write(struct rw_semaphore *rwsem);
  9329. +extern int rt_down_write_killable(struct rw_semaphore *rwsem);
  9330. +extern void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass);
  9331. +extern void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass);
  9332. +extern int rt_down_write_killable_nested(struct rw_semaphore *rwsem,
  9333. + int subclass);
  9334. +extern void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  9335. + struct lockdep_map *nest);
  9336. +extern void rt__down_read(struct rw_semaphore *rwsem);
  9337. +extern void rt_down_read(struct rw_semaphore *rwsem);
  9338. +extern int rt_down_write_trylock(struct rw_semaphore *rwsem);
  9339. +extern int rt__down_read_trylock(struct rw_semaphore *rwsem);
  9340. +extern int rt_down_read_trylock(struct rw_semaphore *rwsem);
  9341. +extern void __rt_up_read(struct rw_semaphore *rwsem);
  9342. +extern void rt_up_read(struct rw_semaphore *rwsem);
  9343. +extern void rt_up_write(struct rw_semaphore *rwsem);
  9344. +extern void rt_downgrade_write(struct rw_semaphore *rwsem);
  9345. +
  9346. +#define init_rwsem(sem) rt_init_rwsem(sem)
  9347. +#define rwsem_is_locked(s) rt_mutex_is_locked(&(s)->lock)
  9348. +
  9349. +static inline int rwsem_is_contended(struct rw_semaphore *sem)
  9350. +{
  9351. + /* rt_mutex_has_waiters() */
  9352. + return !RB_EMPTY_ROOT(&sem->lock.waiters);
  9353. +}
  9354. +
  9355. +static inline void __down_read(struct rw_semaphore *sem)
  9356. +{
  9357. + rt__down_read(sem);
  9358. +}
  9359. +
  9360. +static inline void down_read(struct rw_semaphore *sem)
  9361. +{
  9362. + rt_down_read(sem);
  9363. +}
  9364. +
  9365. +static inline int __down_read_trylock(struct rw_semaphore *sem)
  9366. +{
  9367. + return rt__down_read_trylock(sem);
  9368. +}
  9369. +
  9370. +static inline int down_read_trylock(struct rw_semaphore *sem)
  9371. +{
  9372. + return rt_down_read_trylock(sem);
  9373. +}
  9374. +
  9375. +static inline void down_write(struct rw_semaphore *sem)
  9376. +{
  9377. + rt_down_write(sem);
  9378. +}
  9379. +
  9380. +static inline int down_write_killable(struct rw_semaphore *sem)
  9381. +{
  9382. + return rt_down_write_killable(sem);
  9383. +}
  9384. +
  9385. +static inline int down_write_trylock(struct rw_semaphore *sem)
  9386. +{
  9387. + return rt_down_write_trylock(sem);
  9388. +}
  9389. +
  9390. +static inline void __up_read(struct rw_semaphore *sem)
  9391. +{
  9392. + __rt_up_read(sem);
  9393. +}
  9394. +
  9395. +static inline void up_read(struct rw_semaphore *sem)
  9396. +{
  9397. + rt_up_read(sem);
  9398. +}
  9399. +
  9400. +static inline void up_write(struct rw_semaphore *sem)
  9401. +{
  9402. + rt_up_write(sem);
  9403. +}
  9404. +
  9405. +static inline void downgrade_write(struct rw_semaphore *sem)
  9406. +{
  9407. + rt_downgrade_write(sem);
  9408. +}
  9409. +
  9410. +static inline void down_read_nested(struct rw_semaphore *sem, int subclass)
  9411. +{
  9412. + return rt_down_read_nested(sem, subclass);
  9413. +}
  9414. +
  9415. +static inline void down_write_nested(struct rw_semaphore *sem, int subclass)
  9416. +{
  9417. + rt_down_write_nested(sem, subclass);
  9418. +}
  9419. +
  9420. +static inline int down_write_killable_nested(struct rw_semaphore *sem,
  9421. + int subclass)
  9422. +{
  9423. + return rt_down_write_killable_nested(sem, subclass);
  9424. +}
  9425. +
  9426. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  9427. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  9428. + struct rw_semaphore *nest_lock)
  9429. +{
  9430. + rt_down_write_nested_lock(sem, &nest_lock->dep_map);
  9431. +}
  9432. +
  9433. +#else
  9434. +
  9435. +static inline void down_write_nest_lock(struct rw_semaphore *sem,
  9436. + struct rw_semaphore *nest_lock)
  9437. +{
  9438. + rt_down_write_nested_lock(sem, NULL);
  9439. +}
  9440. +#endif
  9441. +#endif
  9442. diff --git a/include/linux/sched.h b/include/linux/sched.h
  9443. index 62c68e513e39..c873ce0183ab 100644
  9444. --- a/include/linux/sched.h
  9445. +++ b/include/linux/sched.h
  9446. @@ -26,6 +26,7 @@ struct sched_param {
  9447. #include <linux/nodemask.h>
  9448. #include <linux/mm_types.h>
  9449. #include <linux/preempt.h>
  9450. +#include <asm/kmap_types.h>
  9451. #include <asm/page.h>
  9452. #include <asm/ptrace.h>
  9453. @@ -243,10 +244,7 @@ extern char ___assert_task_state[1 - 2*!!(
  9454. TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
  9455. __TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
  9456. -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
  9457. #define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
  9458. -#define task_is_stopped_or_traced(task) \
  9459. - ((task->state & (__TASK_STOPPED | __TASK_TRACED)) != 0)
  9460. #define task_contributes_to_load(task) \
  9461. ((task->state & TASK_UNINTERRUPTIBLE) != 0 && \
  9462. (task->flags & PF_FROZEN) == 0 && \
  9463. @@ -312,6 +310,11 @@ extern char ___assert_task_state[1 - 2*!!(
  9464. #endif
  9465. +#define __set_current_state_no_track(state_value) \
  9466. + do { current->state = (state_value); } while (0)
  9467. +#define set_current_state_no_track(state_value) \
  9468. + set_mb(current->state, (state_value))
  9469. +
  9470. /* Task command name length */
  9471. #define TASK_COMM_LEN 16
  9472. @@ -1009,8 +1012,18 @@ struct wake_q_head {
  9473. struct wake_q_head name = { WAKE_Q_TAIL, &name.first }
  9474. extern void wake_q_add(struct wake_q_head *head,
  9475. - struct task_struct *task);
  9476. -extern void wake_up_q(struct wake_q_head *head);
  9477. + struct task_struct *task);
  9478. +extern void __wake_up_q(struct wake_q_head *head, bool sleeper);
  9479. +
  9480. +static inline void wake_up_q(struct wake_q_head *head)
  9481. +{
  9482. + __wake_up_q(head, false);
  9483. +}
  9484. +
  9485. +static inline void wake_up_q_sleeper(struct wake_q_head *head)
  9486. +{
  9487. + __wake_up_q(head, true);
  9488. +}
  9489. /*
  9490. * sched-domains (multiprocessor balancing) declarations:
  9491. @@ -1459,6 +1472,7 @@ struct tlbflush_unmap_batch {
  9492. struct task_struct {
  9493. volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
  9494. + volatile long saved_state; /* saved state for "spinlock sleepers" */
  9495. void *stack;
  9496. atomic_t usage;
  9497. unsigned int flags; /* per process flags, defined below */
  9498. @@ -1495,6 +1509,12 @@ struct task_struct {
  9499. #endif
  9500. unsigned int policy;
  9501. +#ifdef CONFIG_PREEMPT_RT_FULL
  9502. + int migrate_disable;
  9503. +# ifdef CONFIG_SCHED_DEBUG
  9504. + int migrate_disable_atomic;
  9505. +# endif
  9506. +#endif
  9507. int nr_cpus_allowed;
  9508. cpumask_t cpus_allowed;
  9509. @@ -1629,6 +1649,9 @@ struct task_struct {
  9510. struct task_cputime cputime_expires;
  9511. struct list_head cpu_timers[3];
  9512. +#ifdef CONFIG_PREEMPT_RT_BASE
  9513. + struct task_struct *posix_timer_list;
  9514. +#endif
  9515. /* process credentials */
  9516. const struct cred __rcu *real_cred; /* objective and real subjective task
  9517. @@ -1659,10 +1682,15 @@ struct task_struct {
  9518. /* signal handlers */
  9519. struct signal_struct *signal;
  9520. struct sighand_struct *sighand;
  9521. + struct sigqueue *sigqueue_cache;
  9522. sigset_t blocked, real_blocked;
  9523. sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
  9524. struct sigpending pending;
  9525. +#ifdef CONFIG_PREEMPT_RT_FULL
  9526. + /* TODO: move me into ->restart_block ? */
  9527. + struct siginfo forced_info;
  9528. +#endif
  9529. unsigned long sas_ss_sp;
  9530. size_t sas_ss_size;
  9531. @@ -1891,6 +1919,12 @@ struct task_struct {
  9532. /* bitmask and counter of trace recursion */
  9533. unsigned long trace_recursion;
  9534. #endif /* CONFIG_TRACING */
  9535. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  9536. + u64 preempt_timestamp_hist;
  9537. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  9538. + long timer_offset;
  9539. +#endif
  9540. +#endif
  9541. #ifdef CONFIG_KCOV
  9542. /* Coverage collection mode enabled for this task (0 if disabled). */
  9543. enum kcov_mode kcov_mode;
  9544. @@ -1916,9 +1950,23 @@ struct task_struct {
  9545. unsigned int sequential_io;
  9546. unsigned int sequential_io_avg;
  9547. #endif
  9548. +#ifdef CONFIG_PREEMPT_RT_BASE
  9549. + struct rcu_head put_rcu;
  9550. + int softirq_nestcnt;
  9551. + unsigned int softirqs_raised;
  9552. +#endif
  9553. +#ifdef CONFIG_PREEMPT_RT_FULL
  9554. +# if defined CONFIG_HIGHMEM || defined CONFIG_X86_32
  9555. + int kmap_idx;
  9556. + pte_t kmap_pte[KM_TYPE_NR];
  9557. +# endif
  9558. +#endif
  9559. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  9560. unsigned long task_state_change;
  9561. #endif
  9562. +#ifdef CONFIG_PREEMPT_RT_FULL
  9563. + int xmit_recursion;
  9564. +#endif
  9565. int pagefault_disabled;
  9566. #ifdef CONFIG_MMU
  9567. struct task_struct *oom_reaper_list;
  9568. @@ -1939,14 +1987,6 @@ extern int arch_task_struct_size __read_mostly;
  9569. # define arch_task_struct_size (sizeof(struct task_struct))
  9570. #endif
  9571. -/* Future-safe accessor for struct task_struct's cpus_allowed. */
  9572. -#define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
  9573. -
  9574. -static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  9575. -{
  9576. - return p->nr_cpus_allowed;
  9577. -}
  9578. -
  9579. #define TNF_MIGRATED 0x01
  9580. #define TNF_NO_GROUP 0x02
  9581. #define TNF_SHARED 0x04
  9582. @@ -2162,6 +2202,15 @@ extern struct pid *cad_pid;
  9583. extern void free_task(struct task_struct *tsk);
  9584. #define get_task_struct(tsk) do { atomic_inc(&(tsk)->usage); } while(0)
  9585. +#ifdef CONFIG_PREEMPT_RT_BASE
  9586. +extern void __put_task_struct_cb(struct rcu_head *rhp);
  9587. +
  9588. +static inline void put_task_struct(struct task_struct *t)
  9589. +{
  9590. + if (atomic_dec_and_test(&t->usage))
  9591. + call_rcu(&t->put_rcu, __put_task_struct_cb);
  9592. +}
  9593. +#else
  9594. extern void __put_task_struct(struct task_struct *t);
  9595. static inline void put_task_struct(struct task_struct *t)
  9596. @@ -2169,6 +2218,7 @@ static inline void put_task_struct(struct task_struct *t)
  9597. if (atomic_dec_and_test(&t->usage))
  9598. __put_task_struct(t);
  9599. }
  9600. +#endif
  9601. struct task_struct *task_rcu_dereference(struct task_struct **ptask);
  9602. struct task_struct *try_get_task_struct(struct task_struct **ptask);
  9603. @@ -2210,6 +2260,7 @@ extern void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut,
  9604. /*
  9605. * Per process flags
  9606. */
  9607. +#define PF_IN_SOFTIRQ 0x00000001 /* Task is serving softirq */
  9608. #define PF_EXITING 0x00000004 /* getting shut down */
  9609. #define PF_EXITPIDONE 0x00000008 /* pi exit done on shut down */
  9610. #define PF_VCPU 0x00000010 /* I'm a virtual CPU */
  9611. @@ -2378,6 +2429,10 @@ extern void do_set_cpus_allowed(struct task_struct *p,
  9612. extern int set_cpus_allowed_ptr(struct task_struct *p,
  9613. const struct cpumask *new_mask);
  9614. +int migrate_me(void);
  9615. +void tell_sched_cpu_down_begin(int cpu);
  9616. +void tell_sched_cpu_down_done(int cpu);
  9617. +
  9618. #else
  9619. static inline void do_set_cpus_allowed(struct task_struct *p,
  9620. const struct cpumask *new_mask)
  9621. @@ -2390,6 +2445,9 @@ static inline int set_cpus_allowed_ptr(struct task_struct *p,
  9622. return -EINVAL;
  9623. return 0;
  9624. }
  9625. +static inline int migrate_me(void) { return 0; }
  9626. +static inline void tell_sched_cpu_down_begin(int cpu) { }
  9627. +static inline void tell_sched_cpu_down_done(int cpu) { }
  9628. #endif
  9629. #ifdef CONFIG_NO_HZ_COMMON
  9630. @@ -2624,6 +2682,7 @@ extern void xtime_update(unsigned long ticks);
  9631. extern int wake_up_state(struct task_struct *tsk, unsigned int state);
  9632. extern int wake_up_process(struct task_struct *tsk);
  9633. +extern int wake_up_lock_sleeper(struct task_struct * tsk);
  9634. extern void wake_up_new_task(struct task_struct *tsk);
  9635. #ifdef CONFIG_SMP
  9636. extern void kick_process(struct task_struct *tsk);
  9637. @@ -2832,6 +2891,17 @@ static inline void mmdrop(struct mm_struct *mm)
  9638. __mmdrop(mm);
  9639. }
  9640. +#ifdef CONFIG_PREEMPT_RT_BASE
  9641. +extern void __mmdrop_delayed(struct rcu_head *rhp);
  9642. +static inline void mmdrop_delayed(struct mm_struct *mm)
  9643. +{
  9644. + if (atomic_dec_and_test(&mm->mm_count))
  9645. + call_rcu(&mm->delayed_drop, __mmdrop_delayed);
  9646. +}
  9647. +#else
  9648. +# define mmdrop_delayed(mm) mmdrop(mm)
  9649. +#endif
  9650. +
  9651. static inline bool mmget_not_zero(struct mm_struct *mm)
  9652. {
  9653. return atomic_inc_not_zero(&mm->mm_users);
  9654. @@ -3168,6 +3238,43 @@ static inline int test_tsk_need_resched(struct task_struct *tsk)
  9655. return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED));
  9656. }
  9657. +#ifdef CONFIG_PREEMPT_LAZY
  9658. +static inline void set_tsk_need_resched_lazy(struct task_struct *tsk)
  9659. +{
  9660. + set_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  9661. +}
  9662. +
  9663. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk)
  9664. +{
  9665. + clear_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY);
  9666. +}
  9667. +
  9668. +static inline int test_tsk_need_resched_lazy(struct task_struct *tsk)
  9669. +{
  9670. + return unlikely(test_tsk_thread_flag(tsk,TIF_NEED_RESCHED_LAZY));
  9671. +}
  9672. +
  9673. +static inline int need_resched_lazy(void)
  9674. +{
  9675. + return test_thread_flag(TIF_NEED_RESCHED_LAZY);
  9676. +}
  9677. +
  9678. +static inline int need_resched_now(void)
  9679. +{
  9680. + return test_thread_flag(TIF_NEED_RESCHED);
  9681. +}
  9682. +
  9683. +#else
  9684. +static inline void clear_tsk_need_resched_lazy(struct task_struct *tsk) { }
  9685. +static inline int need_resched_lazy(void) { return 0; }
  9686. +
  9687. +static inline int need_resched_now(void)
  9688. +{
  9689. + return test_thread_flag(TIF_NEED_RESCHED);
  9690. +}
  9691. +
  9692. +#endif
  9693. +
  9694. static inline int restart_syscall(void)
  9695. {
  9696. set_tsk_thread_flag(current, TIF_SIGPENDING);
  9697. @@ -3199,6 +3306,51 @@ static inline int signal_pending_state(long state, struct task_struct *p)
  9698. return (state & TASK_INTERRUPTIBLE) || __fatal_signal_pending(p);
  9699. }
  9700. +static inline bool __task_is_stopped_or_traced(struct task_struct *task)
  9701. +{
  9702. + if (task->state & (__TASK_STOPPED | __TASK_TRACED))
  9703. + return true;
  9704. +#ifdef CONFIG_PREEMPT_RT_FULL
  9705. + if (task->saved_state & (__TASK_STOPPED | __TASK_TRACED))
  9706. + return true;
  9707. +#endif
  9708. + return false;
  9709. +}
  9710. +
  9711. +static inline bool task_is_stopped_or_traced(struct task_struct *task)
  9712. +{
  9713. + bool traced_stopped;
  9714. +
  9715. +#ifdef CONFIG_PREEMPT_RT_FULL
  9716. + unsigned long flags;
  9717. +
  9718. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  9719. + traced_stopped = __task_is_stopped_or_traced(task);
  9720. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  9721. +#else
  9722. + traced_stopped = __task_is_stopped_or_traced(task);
  9723. +#endif
  9724. + return traced_stopped;
  9725. +}
  9726. +
  9727. +static inline bool task_is_traced(struct task_struct *task)
  9728. +{
  9729. + bool traced = false;
  9730. +
  9731. + if (task->state & __TASK_TRACED)
  9732. + return true;
  9733. +#ifdef CONFIG_PREEMPT_RT_FULL
  9734. + /* in case the task is sleeping on tasklist_lock */
  9735. + raw_spin_lock_irq(&task->pi_lock);
  9736. + if (task->state & __TASK_TRACED)
  9737. + traced = true;
  9738. + else if (task->saved_state & __TASK_TRACED)
  9739. + traced = true;
  9740. + raw_spin_unlock_irq(&task->pi_lock);
  9741. +#endif
  9742. + return traced;
  9743. +}
  9744. +
  9745. /*
  9746. * cond_resched() and cond_resched_lock(): latency reduction via
  9747. * explicit rescheduling in places that are safe. The return
  9748. @@ -3220,12 +3372,16 @@ extern int __cond_resched_lock(spinlock_t *lock);
  9749. __cond_resched_lock(lock); \
  9750. })
  9751. +#ifndef CONFIG_PREEMPT_RT_FULL
  9752. extern int __cond_resched_softirq(void);
  9753. #define cond_resched_softirq() ({ \
  9754. ___might_sleep(__FILE__, __LINE__, SOFTIRQ_DISABLE_OFFSET); \
  9755. __cond_resched_softirq(); \
  9756. })
  9757. +#else
  9758. +# define cond_resched_softirq() cond_resched()
  9759. +#endif
  9760. static inline void cond_resched_rcu(void)
  9761. {
  9762. @@ -3387,6 +3543,31 @@ static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
  9763. #endif /* CONFIG_SMP */
  9764. +static inline int __migrate_disabled(struct task_struct *p)
  9765. +{
  9766. +#ifdef CONFIG_PREEMPT_RT_FULL
  9767. + return p->migrate_disable;
  9768. +#else
  9769. + return 0;
  9770. +#endif
  9771. +}
  9772. +
  9773. +/* Future-safe accessor for struct task_struct's cpus_allowed. */
  9774. +static inline const struct cpumask *tsk_cpus_allowed(struct task_struct *p)
  9775. +{
  9776. + if (__migrate_disabled(p))
  9777. + return cpumask_of(task_cpu(p));
  9778. +
  9779. + return &p->cpus_allowed;
  9780. +}
  9781. +
  9782. +static inline int tsk_nr_cpus_allowed(struct task_struct *p)
  9783. +{
  9784. + if (__migrate_disabled(p))
  9785. + return 1;
  9786. + return p->nr_cpus_allowed;
  9787. +}
  9788. +
  9789. extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
  9790. extern long sched_getaffinity(pid_t pid, struct cpumask *mask);
  9791. diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
  9792. index ead97654c4e9..3d7223ffdd3b 100644
  9793. --- a/include/linux/seqlock.h
  9794. +++ b/include/linux/seqlock.h
  9795. @@ -220,20 +220,30 @@ static inline int read_seqcount_retry(const seqcount_t *s, unsigned start)
  9796. return __read_seqcount_retry(s, start);
  9797. }
  9798. -
  9799. -
  9800. -static inline void raw_write_seqcount_begin(seqcount_t *s)
  9801. +static inline void __raw_write_seqcount_begin(seqcount_t *s)
  9802. {
  9803. s->sequence++;
  9804. smp_wmb();
  9805. }
  9806. -static inline void raw_write_seqcount_end(seqcount_t *s)
  9807. +static inline void raw_write_seqcount_begin(seqcount_t *s)
  9808. +{
  9809. + preempt_disable_rt();
  9810. + __raw_write_seqcount_begin(s);
  9811. +}
  9812. +
  9813. +static inline void __raw_write_seqcount_end(seqcount_t *s)
  9814. {
  9815. smp_wmb();
  9816. s->sequence++;
  9817. }
  9818. +static inline void raw_write_seqcount_end(seqcount_t *s)
  9819. +{
  9820. + __raw_write_seqcount_end(s);
  9821. + preempt_enable_rt();
  9822. +}
  9823. +
  9824. /**
  9825. * raw_write_seqcount_barrier - do a seq write barrier
  9826. * @s: pointer to seqcount_t
  9827. @@ -428,10 +438,32 @@ typedef struct {
  9828. /*
  9829. * Read side functions for starting and finalizing a read side section.
  9830. */
  9831. +#ifndef CONFIG_PREEMPT_RT_FULL
  9832. static inline unsigned read_seqbegin(const seqlock_t *sl)
  9833. {
  9834. return read_seqcount_begin(&sl->seqcount);
  9835. }
  9836. +#else
  9837. +/*
  9838. + * Starvation safe read side for RT
  9839. + */
  9840. +static inline unsigned read_seqbegin(seqlock_t *sl)
  9841. +{
  9842. + unsigned ret;
  9843. +
  9844. +repeat:
  9845. + ret = ACCESS_ONCE(sl->seqcount.sequence);
  9846. + if (unlikely(ret & 1)) {
  9847. + /*
  9848. + * Take the lock and let the writer proceed (i.e. evtl
  9849. + * boost it), otherwise we could loop here forever.
  9850. + */
  9851. + spin_unlock_wait(&sl->lock);
  9852. + goto repeat;
  9853. + }
  9854. + return ret;
  9855. +}
  9856. +#endif
  9857. static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  9858. {
  9859. @@ -446,36 +478,45 @@ static inline unsigned read_seqretry(const seqlock_t *sl, unsigned start)
  9860. static inline void write_seqlock(seqlock_t *sl)
  9861. {
  9862. spin_lock(&sl->lock);
  9863. - write_seqcount_begin(&sl->seqcount);
  9864. + __raw_write_seqcount_begin(&sl->seqcount);
  9865. +}
  9866. +
  9867. +static inline int try_write_seqlock(seqlock_t *sl)
  9868. +{
  9869. + if (spin_trylock(&sl->lock)) {
  9870. + __raw_write_seqcount_begin(&sl->seqcount);
  9871. + return 1;
  9872. + }
  9873. + return 0;
  9874. }
  9875. static inline void write_sequnlock(seqlock_t *sl)
  9876. {
  9877. - write_seqcount_end(&sl->seqcount);
  9878. + __raw_write_seqcount_end(&sl->seqcount);
  9879. spin_unlock(&sl->lock);
  9880. }
  9881. static inline void write_seqlock_bh(seqlock_t *sl)
  9882. {
  9883. spin_lock_bh(&sl->lock);
  9884. - write_seqcount_begin(&sl->seqcount);
  9885. + __raw_write_seqcount_begin(&sl->seqcount);
  9886. }
  9887. static inline void write_sequnlock_bh(seqlock_t *sl)
  9888. {
  9889. - write_seqcount_end(&sl->seqcount);
  9890. + __raw_write_seqcount_end(&sl->seqcount);
  9891. spin_unlock_bh(&sl->lock);
  9892. }
  9893. static inline void write_seqlock_irq(seqlock_t *sl)
  9894. {
  9895. spin_lock_irq(&sl->lock);
  9896. - write_seqcount_begin(&sl->seqcount);
  9897. + __raw_write_seqcount_begin(&sl->seqcount);
  9898. }
  9899. static inline void write_sequnlock_irq(seqlock_t *sl)
  9900. {
  9901. - write_seqcount_end(&sl->seqcount);
  9902. + __raw_write_seqcount_end(&sl->seqcount);
  9903. spin_unlock_irq(&sl->lock);
  9904. }
  9905. @@ -484,7 +525,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  9906. unsigned long flags;
  9907. spin_lock_irqsave(&sl->lock, flags);
  9908. - write_seqcount_begin(&sl->seqcount);
  9909. + __raw_write_seqcount_begin(&sl->seqcount);
  9910. return flags;
  9911. }
  9912. @@ -494,7 +535,7 @@ static inline unsigned long __write_seqlock_irqsave(seqlock_t *sl)
  9913. static inline void
  9914. write_sequnlock_irqrestore(seqlock_t *sl, unsigned long flags)
  9915. {
  9916. - write_seqcount_end(&sl->seqcount);
  9917. + __raw_write_seqcount_end(&sl->seqcount);
  9918. spin_unlock_irqrestore(&sl->lock, flags);
  9919. }
  9920. diff --git a/include/linux/signal.h b/include/linux/signal.h
  9921. index b63f63eaa39c..295540fdfc72 100644
  9922. --- a/include/linux/signal.h
  9923. +++ b/include/linux/signal.h
  9924. @@ -233,6 +233,7 @@ static inline void init_sigpending(struct sigpending *sig)
  9925. }
  9926. extern void flush_sigqueue(struct sigpending *queue);
  9927. +extern void flush_task_sigqueue(struct task_struct *tsk);
  9928. /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
  9929. static inline int valid_signal(unsigned long sig)
  9930. diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
  9931. index 0f665cb26b50..59c38d1635c8 100644
  9932. --- a/include/linux/skbuff.h
  9933. +++ b/include/linux/skbuff.h
  9934. @@ -284,6 +284,7 @@ struct sk_buff_head {
  9935. __u32 qlen;
  9936. spinlock_t lock;
  9937. + raw_spinlock_t raw_lock;
  9938. };
  9939. struct sk_buff;
  9940. @@ -1565,6 +1566,12 @@ static inline void skb_queue_head_init(struct sk_buff_head *list)
  9941. __skb_queue_head_init(list);
  9942. }
  9943. +static inline void skb_queue_head_init_raw(struct sk_buff_head *list)
  9944. +{
  9945. + raw_spin_lock_init(&list->raw_lock);
  9946. + __skb_queue_head_init(list);
  9947. +}
  9948. +
  9949. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  9950. struct lock_class_key *class)
  9951. {
  9952. diff --git a/include/linux/smp.h b/include/linux/smp.h
  9953. index eccae4690f41..64ec52d951c3 100644
  9954. --- a/include/linux/smp.h
  9955. +++ b/include/linux/smp.h
  9956. @@ -185,6 +185,9 @@ static inline void smp_init(void) { }
  9957. #define get_cpu() ({ preempt_disable(); smp_processor_id(); })
  9958. #define put_cpu() preempt_enable()
  9959. +#define get_cpu_light() ({ migrate_disable(); smp_processor_id(); })
  9960. +#define put_cpu_light() migrate_enable()
  9961. +
  9962. /*
  9963. * Callback to arch code if there's nosmp or maxcpus=0 on the
  9964. * boot command line:
  9965. diff --git a/include/linux/spinlock.h b/include/linux/spinlock.h
  9966. index 47dd0cebd204..02928fa5499d 100644
  9967. --- a/include/linux/spinlock.h
  9968. +++ b/include/linux/spinlock.h
  9969. @@ -271,7 +271,11 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  9970. #define raw_spin_can_lock(lock) (!raw_spin_is_locked(lock))
  9971. /* Include rwlock functions */
  9972. -#include <linux/rwlock.h>
  9973. +#ifdef CONFIG_PREEMPT_RT_FULL
  9974. +# include <linux/rwlock_rt.h>
  9975. +#else
  9976. +# include <linux/rwlock.h>
  9977. +#endif
  9978. /*
  9979. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  9980. @@ -282,6 +286,10 @@ static inline void do_raw_spin_unlock(raw_spinlock_t *lock) __releases(lock)
  9981. # include <linux/spinlock_api_up.h>
  9982. #endif
  9983. +#ifdef CONFIG_PREEMPT_RT_FULL
  9984. +# include <linux/spinlock_rt.h>
  9985. +#else /* PREEMPT_RT_FULL */
  9986. +
  9987. /*
  9988. * Map the spin_lock functions to the raw variants for PREEMPT_RT=n
  9989. */
  9990. @@ -347,6 +355,12 @@ static __always_inline void spin_unlock(spinlock_t *lock)
  9991. raw_spin_unlock(&lock->rlock);
  9992. }
  9993. +static __always_inline int spin_unlock_no_deboost(spinlock_t *lock)
  9994. +{
  9995. + raw_spin_unlock(&lock->rlock);
  9996. + return 0;
  9997. +}
  9998. +
  9999. static __always_inline void spin_unlock_bh(spinlock_t *lock)
  10000. {
  10001. raw_spin_unlock_bh(&lock->rlock);
  10002. @@ -416,4 +430,6 @@ extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  10003. #define atomic_dec_and_lock(atomic, lock) \
  10004. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  10005. +#endif /* !PREEMPT_RT_FULL */
  10006. +
  10007. #endif /* __LINUX_SPINLOCK_H */
  10008. diff --git a/include/linux/spinlock_api_smp.h b/include/linux/spinlock_api_smp.h
  10009. index 5344268e6e62..043263f30e81 100644
  10010. --- a/include/linux/spinlock_api_smp.h
  10011. +++ b/include/linux/spinlock_api_smp.h
  10012. @@ -189,6 +189,8 @@ static inline int __raw_spin_trylock_bh(raw_spinlock_t *lock)
  10013. return 0;
  10014. }
  10015. -#include <linux/rwlock_api_smp.h>
  10016. +#ifndef CONFIG_PREEMPT_RT_FULL
  10017. +# include <linux/rwlock_api_smp.h>
  10018. +#endif
  10019. #endif /* __LINUX_SPINLOCK_API_SMP_H */
  10020. diff --git a/include/linux/spinlock_rt.h b/include/linux/spinlock_rt.h
  10021. new file mode 100644
  10022. index 000000000000..7eb87584e843
  10023. --- /dev/null
  10024. +++ b/include/linux/spinlock_rt.h
  10025. @@ -0,0 +1,165 @@
  10026. +#ifndef __LINUX_SPINLOCK_RT_H
  10027. +#define __LINUX_SPINLOCK_RT_H
  10028. +
  10029. +#ifndef __LINUX_SPINLOCK_H
  10030. +#error Do not include directly. Use spinlock.h
  10031. +#endif
  10032. +
  10033. +#include <linux/bug.h>
  10034. +
  10035. +extern void
  10036. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key);
  10037. +
  10038. +#define spin_lock_init(slock) \
  10039. +do { \
  10040. + static struct lock_class_key __key; \
  10041. + \
  10042. + rt_mutex_init(&(slock)->lock); \
  10043. + __rt_spin_lock_init(slock, #slock, &__key); \
  10044. +} while (0)
  10045. +
  10046. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock);
  10047. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock);
  10048. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock);
  10049. +
  10050. +extern void __lockfunc rt_spin_lock(spinlock_t *lock);
  10051. +extern unsigned long __lockfunc rt_spin_lock_trace_flags(spinlock_t *lock);
  10052. +extern void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass);
  10053. +extern void __lockfunc rt_spin_unlock(spinlock_t *lock);
  10054. +extern int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock);
  10055. +extern void __lockfunc rt_spin_unlock_wait(spinlock_t *lock);
  10056. +extern int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags);
  10057. +extern int __lockfunc rt_spin_trylock_bh(spinlock_t *lock);
  10058. +extern int __lockfunc rt_spin_trylock(spinlock_t *lock);
  10059. +extern int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock);
  10060. +
  10061. +/*
  10062. + * lockdep-less calls, for derived types like rwlock:
  10063. + * (for trylock they can use rt_mutex_trylock() directly.
  10064. + */
  10065. +extern void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock);
  10066. +extern void __lockfunc __rt_spin_lock(struct rt_mutex *lock);
  10067. +extern void __lockfunc __rt_spin_unlock(struct rt_mutex *lock);
  10068. +extern int __lockfunc __rt_spin_trylock(struct rt_mutex *lock);
  10069. +
  10070. +#define spin_lock(lock) rt_spin_lock(lock)
  10071. +
  10072. +#define spin_lock_bh(lock) \
  10073. + do { \
  10074. + local_bh_disable(); \
  10075. + rt_spin_lock(lock); \
  10076. + } while (0)
  10077. +
  10078. +#define spin_lock_irq(lock) spin_lock(lock)
  10079. +
  10080. +#define spin_do_trylock(lock) __cond_lock(lock, rt_spin_trylock(lock))
  10081. +
  10082. +#define spin_trylock(lock) \
  10083. +({ \
  10084. + int __locked; \
  10085. + __locked = spin_do_trylock(lock); \
  10086. + __locked; \
  10087. +})
  10088. +
  10089. +#ifdef CONFIG_LOCKDEP
  10090. +# define spin_lock_nested(lock, subclass) \
  10091. + do { \
  10092. + rt_spin_lock_nested(lock, subclass); \
  10093. + } while (0)
  10094. +
  10095. +#define spin_lock_bh_nested(lock, subclass) \
  10096. + do { \
  10097. + local_bh_disable(); \
  10098. + rt_spin_lock_nested(lock, subclass); \
  10099. + } while (0)
  10100. +
  10101. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  10102. + do { \
  10103. + typecheck(unsigned long, flags); \
  10104. + flags = 0; \
  10105. + rt_spin_lock_nested(lock, subclass); \
  10106. + } while (0)
  10107. +#else
  10108. +# define spin_lock_nested(lock, subclass) spin_lock(lock)
  10109. +# define spin_lock_bh_nested(lock, subclass) spin_lock_bh(lock)
  10110. +
  10111. +# define spin_lock_irqsave_nested(lock, flags, subclass) \
  10112. + do { \
  10113. + typecheck(unsigned long, flags); \
  10114. + flags = 0; \
  10115. + spin_lock(lock); \
  10116. + } while (0)
  10117. +#endif
  10118. +
  10119. +#define spin_lock_irqsave(lock, flags) \
  10120. + do { \
  10121. + typecheck(unsigned long, flags); \
  10122. + flags = 0; \
  10123. + spin_lock(lock); \
  10124. + } while (0)
  10125. +
  10126. +static inline unsigned long spin_lock_trace_flags(spinlock_t *lock)
  10127. +{
  10128. + unsigned long flags = 0;
  10129. +#ifdef CONFIG_TRACE_IRQFLAGS
  10130. + flags = rt_spin_lock_trace_flags(lock);
  10131. +#else
  10132. + spin_lock(lock); /* lock_local */
  10133. +#endif
  10134. + return flags;
  10135. +}
  10136. +
  10137. +/* FIXME: we need rt_spin_lock_nest_lock */
  10138. +#define spin_lock_nest_lock(lock, nest_lock) spin_lock_nested(lock, 0)
  10139. +
  10140. +#define spin_unlock(lock) rt_spin_unlock(lock)
  10141. +#define spin_unlock_no_deboost(lock) rt_spin_unlock_no_deboost(lock)
  10142. +
  10143. +#define spin_unlock_bh(lock) \
  10144. + do { \
  10145. + rt_spin_unlock(lock); \
  10146. + local_bh_enable(); \
  10147. + } while (0)
  10148. +
  10149. +#define spin_unlock_irq(lock) spin_unlock(lock)
  10150. +
  10151. +#define spin_unlock_irqrestore(lock, flags) \
  10152. + do { \
  10153. + typecheck(unsigned long, flags); \
  10154. + (void) flags; \
  10155. + spin_unlock(lock); \
  10156. + } while (0)
  10157. +
  10158. +#define spin_trylock_bh(lock) __cond_lock(lock, rt_spin_trylock_bh(lock))
  10159. +#define spin_trylock_irq(lock) spin_trylock(lock)
  10160. +
  10161. +#define spin_trylock_irqsave(lock, flags) \
  10162. + rt_spin_trylock_irqsave(lock, &(flags))
  10163. +
  10164. +#define spin_unlock_wait(lock) rt_spin_unlock_wait(lock)
  10165. +
  10166. +#ifdef CONFIG_GENERIC_LOCKBREAK
  10167. +# define spin_is_contended(lock) ((lock)->break_lock)
  10168. +#else
  10169. +# define spin_is_contended(lock) (((void)(lock), 0))
  10170. +#endif
  10171. +
  10172. +static inline int spin_can_lock(spinlock_t *lock)
  10173. +{
  10174. + return !rt_mutex_is_locked(&lock->lock);
  10175. +}
  10176. +
  10177. +static inline int spin_is_locked(spinlock_t *lock)
  10178. +{
  10179. + return rt_mutex_is_locked(&lock->lock);
  10180. +}
  10181. +
  10182. +static inline void assert_spin_locked(spinlock_t *lock)
  10183. +{
  10184. + BUG_ON(!spin_is_locked(lock));
  10185. +}
  10186. +
  10187. +#define atomic_dec_and_lock(atomic, lock) \
  10188. + atomic_dec_and_spin_lock(atomic, lock)
  10189. +
  10190. +#endif
  10191. diff --git a/include/linux/spinlock_types.h b/include/linux/spinlock_types.h
  10192. index 73548eb13a5d..10bac715ea96 100644
  10193. --- a/include/linux/spinlock_types.h
  10194. +++ b/include/linux/spinlock_types.h
  10195. @@ -9,80 +9,15 @@
  10196. * Released under the General Public License (GPL).
  10197. */
  10198. -#if defined(CONFIG_SMP)
  10199. -# include <asm/spinlock_types.h>
  10200. +#include <linux/spinlock_types_raw.h>
  10201. +
  10202. +#ifndef CONFIG_PREEMPT_RT_FULL
  10203. +# include <linux/spinlock_types_nort.h>
  10204. +# include <linux/rwlock_types.h>
  10205. #else
  10206. -# include <linux/spinlock_types_up.h>
  10207. +# include <linux/rtmutex.h>
  10208. +# include <linux/spinlock_types_rt.h>
  10209. +# include <linux/rwlock_types_rt.h>
  10210. #endif
  10211. -#include <linux/lockdep.h>
  10212. -
  10213. -typedef struct raw_spinlock {
  10214. - arch_spinlock_t raw_lock;
  10215. -#ifdef CONFIG_GENERIC_LOCKBREAK
  10216. - unsigned int break_lock;
  10217. -#endif
  10218. -#ifdef CONFIG_DEBUG_SPINLOCK
  10219. - unsigned int magic, owner_cpu;
  10220. - void *owner;
  10221. -#endif
  10222. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10223. - struct lockdep_map dep_map;
  10224. -#endif
  10225. -} raw_spinlock_t;
  10226. -
  10227. -#define SPINLOCK_MAGIC 0xdead4ead
  10228. -
  10229. -#define SPINLOCK_OWNER_INIT ((void *)-1L)
  10230. -
  10231. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10232. -# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  10233. -#else
  10234. -# define SPIN_DEP_MAP_INIT(lockname)
  10235. -#endif
  10236. -
  10237. -#ifdef CONFIG_DEBUG_SPINLOCK
  10238. -# define SPIN_DEBUG_INIT(lockname) \
  10239. - .magic = SPINLOCK_MAGIC, \
  10240. - .owner_cpu = -1, \
  10241. - .owner = SPINLOCK_OWNER_INIT,
  10242. -#else
  10243. -# define SPIN_DEBUG_INIT(lockname)
  10244. -#endif
  10245. -
  10246. -#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  10247. - { \
  10248. - .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  10249. - SPIN_DEBUG_INIT(lockname) \
  10250. - SPIN_DEP_MAP_INIT(lockname) }
  10251. -
  10252. -#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  10253. - (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  10254. -
  10255. -#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  10256. -
  10257. -typedef struct spinlock {
  10258. - union {
  10259. - struct raw_spinlock rlock;
  10260. -
  10261. -#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10262. -# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  10263. - struct {
  10264. - u8 __padding[LOCK_PADSIZE];
  10265. - struct lockdep_map dep_map;
  10266. - };
  10267. -#endif
  10268. - };
  10269. -} spinlock_t;
  10270. -
  10271. -#define __SPIN_LOCK_INITIALIZER(lockname) \
  10272. - { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  10273. -
  10274. -#define __SPIN_LOCK_UNLOCKED(lockname) \
  10275. - (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  10276. -
  10277. -#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  10278. -
  10279. -#include <linux/rwlock_types.h>
  10280. -
  10281. #endif /* __LINUX_SPINLOCK_TYPES_H */
  10282. diff --git a/include/linux/spinlock_types_nort.h b/include/linux/spinlock_types_nort.h
  10283. new file mode 100644
  10284. index 000000000000..f1dac1fb1d6a
  10285. --- /dev/null
  10286. +++ b/include/linux/spinlock_types_nort.h
  10287. @@ -0,0 +1,33 @@
  10288. +#ifndef __LINUX_SPINLOCK_TYPES_NORT_H
  10289. +#define __LINUX_SPINLOCK_TYPES_NORT_H
  10290. +
  10291. +#ifndef __LINUX_SPINLOCK_TYPES_H
  10292. +#error "Do not include directly. Include spinlock_types.h instead"
  10293. +#endif
  10294. +
  10295. +/*
  10296. + * The non RT version maps spinlocks to raw_spinlocks
  10297. + */
  10298. +typedef struct spinlock {
  10299. + union {
  10300. + struct raw_spinlock rlock;
  10301. +
  10302. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10303. +# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
  10304. + struct {
  10305. + u8 __padding[LOCK_PADSIZE];
  10306. + struct lockdep_map dep_map;
  10307. + };
  10308. +#endif
  10309. + };
  10310. +} spinlock_t;
  10311. +
  10312. +#define __SPIN_LOCK_INITIALIZER(lockname) \
  10313. + { { .rlock = __RAW_SPIN_LOCK_INITIALIZER(lockname) } }
  10314. +
  10315. +#define __SPIN_LOCK_UNLOCKED(lockname) \
  10316. + (spinlock_t ) __SPIN_LOCK_INITIALIZER(lockname)
  10317. +
  10318. +#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
  10319. +
  10320. +#endif
  10321. diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h
  10322. new file mode 100644
  10323. index 000000000000..edffc4d53fc9
  10324. --- /dev/null
  10325. +++ b/include/linux/spinlock_types_raw.h
  10326. @@ -0,0 +1,56 @@
  10327. +#ifndef __LINUX_SPINLOCK_TYPES_RAW_H
  10328. +#define __LINUX_SPINLOCK_TYPES_RAW_H
  10329. +
  10330. +#if defined(CONFIG_SMP)
  10331. +# include <asm/spinlock_types.h>
  10332. +#else
  10333. +# include <linux/spinlock_types_up.h>
  10334. +#endif
  10335. +
  10336. +#include <linux/lockdep.h>
  10337. +
  10338. +typedef struct raw_spinlock {
  10339. + arch_spinlock_t raw_lock;
  10340. +#ifdef CONFIG_GENERIC_LOCKBREAK
  10341. + unsigned int break_lock;
  10342. +#endif
  10343. +#ifdef CONFIG_DEBUG_SPINLOCK
  10344. + unsigned int magic, owner_cpu;
  10345. + void *owner;
  10346. +#endif
  10347. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10348. + struct lockdep_map dep_map;
  10349. +#endif
  10350. +} raw_spinlock_t;
  10351. +
  10352. +#define SPINLOCK_MAGIC 0xdead4ead
  10353. +
  10354. +#define SPINLOCK_OWNER_INIT ((void *)-1L)
  10355. +
  10356. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10357. +# define SPIN_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }
  10358. +#else
  10359. +# define SPIN_DEP_MAP_INIT(lockname)
  10360. +#endif
  10361. +
  10362. +#ifdef CONFIG_DEBUG_SPINLOCK
  10363. +# define SPIN_DEBUG_INIT(lockname) \
  10364. + .magic = SPINLOCK_MAGIC, \
  10365. + .owner_cpu = -1, \
  10366. + .owner = SPINLOCK_OWNER_INIT,
  10367. +#else
  10368. +# define SPIN_DEBUG_INIT(lockname)
  10369. +#endif
  10370. +
  10371. +#define __RAW_SPIN_LOCK_INITIALIZER(lockname) \
  10372. + { \
  10373. + .raw_lock = __ARCH_SPIN_LOCK_UNLOCKED, \
  10374. + SPIN_DEBUG_INIT(lockname) \
  10375. + SPIN_DEP_MAP_INIT(lockname) }
  10376. +
  10377. +#define __RAW_SPIN_LOCK_UNLOCKED(lockname) \
  10378. + (raw_spinlock_t) __RAW_SPIN_LOCK_INITIALIZER(lockname)
  10379. +
  10380. +#define DEFINE_RAW_SPINLOCK(x) raw_spinlock_t x = __RAW_SPIN_LOCK_UNLOCKED(x)
  10381. +
  10382. +#endif
  10383. diff --git a/include/linux/spinlock_types_rt.h b/include/linux/spinlock_types_rt.h
  10384. new file mode 100644
  10385. index 000000000000..3e3d8c5f7a9a
  10386. --- /dev/null
  10387. +++ b/include/linux/spinlock_types_rt.h
  10388. @@ -0,0 +1,48 @@
  10389. +#ifndef __LINUX_SPINLOCK_TYPES_RT_H
  10390. +#define __LINUX_SPINLOCK_TYPES_RT_H
  10391. +
  10392. +#ifndef __LINUX_SPINLOCK_TYPES_H
  10393. +#error "Do not include directly. Include spinlock_types.h instead"
  10394. +#endif
  10395. +
  10396. +#include <linux/cache.h>
  10397. +
  10398. +/*
  10399. + * PREEMPT_RT: spinlocks - an RT mutex plus lock-break field:
  10400. + */
  10401. +typedef struct spinlock {
  10402. + struct rt_mutex lock;
  10403. + unsigned int break_lock;
  10404. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  10405. + struct lockdep_map dep_map;
  10406. +#endif
  10407. +} spinlock_t;
  10408. +
  10409. +#ifdef CONFIG_DEBUG_RT_MUTEXES
  10410. +# define __RT_SPIN_INITIALIZER(name) \
  10411. + { \
  10412. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  10413. + .save_state = 1, \
  10414. + .file = __FILE__, \
  10415. + .line = __LINE__ , \
  10416. + }
  10417. +#else
  10418. +# define __RT_SPIN_INITIALIZER(name) \
  10419. + { \
  10420. + .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
  10421. + .save_state = 1, \
  10422. + }
  10423. +#endif
  10424. +
  10425. +/*
  10426. +.wait_list = PLIST_HEAD_INIT_RAW((name).lock.wait_list, (name).lock.wait_lock)
  10427. +*/
  10428. +
  10429. +#define __SPIN_LOCK_UNLOCKED(name) \
  10430. + { .lock = __RT_SPIN_INITIALIZER(name.lock), \
  10431. + SPIN_DEP_MAP_INIT(name) }
  10432. +
  10433. +#define DEFINE_SPINLOCK(name) \
  10434. + spinlock_t name = __SPIN_LOCK_UNLOCKED(name)
  10435. +
  10436. +#endif
  10437. diff --git a/include/linux/srcu.h b/include/linux/srcu.h
  10438. index dc8eb63c6568..e793d3a257da 100644
  10439. --- a/include/linux/srcu.h
  10440. +++ b/include/linux/srcu.h
  10441. @@ -84,10 +84,10 @@ int init_srcu_struct(struct srcu_struct *sp);
  10442. void process_srcu(struct work_struct *work);
  10443. -#define __SRCU_STRUCT_INIT(name) \
  10444. +#define __SRCU_STRUCT_INIT(name, pcpu_name) \
  10445. { \
  10446. .completed = -300, \
  10447. - .per_cpu_ref = &name##_srcu_array, \
  10448. + .per_cpu_ref = &pcpu_name, \
  10449. .queue_lock = __SPIN_LOCK_UNLOCKED(name.queue_lock), \
  10450. .running = false, \
  10451. .batch_queue = RCU_BATCH_INIT(name.batch_queue), \
  10452. @@ -119,7 +119,7 @@ void process_srcu(struct work_struct *work);
  10453. */
  10454. #define __DEFINE_SRCU(name, is_static) \
  10455. static DEFINE_PER_CPU(struct srcu_struct_array, name##_srcu_array);\
  10456. - is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name)
  10457. + is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_array)
  10458. #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
  10459. #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
  10460. diff --git a/include/linux/suspend.h b/include/linux/suspend.h
  10461. index 7693e39b14fe..b36eedeb28d1 100644
  10462. --- a/include/linux/suspend.h
  10463. +++ b/include/linux/suspend.h
  10464. @@ -193,6 +193,12 @@ struct platform_freeze_ops {
  10465. void (*end)(void);
  10466. };
  10467. +#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
  10468. +extern bool pm_in_action;
  10469. +#else
  10470. +# define pm_in_action false
  10471. +#endif
  10472. +
  10473. #ifdef CONFIG_SUSPEND
  10474. /**
  10475. * suspend_set_ops - set platform dependent suspend operations
  10476. diff --git a/include/linux/swait.h b/include/linux/swait.h
  10477. index c1f9c62a8a50..83f004a72320 100644
  10478. --- a/include/linux/swait.h
  10479. +++ b/include/linux/swait.h
  10480. @@ -87,6 +87,7 @@ static inline int swait_active(struct swait_queue_head *q)
  10481. extern void swake_up(struct swait_queue_head *q);
  10482. extern void swake_up_all(struct swait_queue_head *q);
  10483. extern void swake_up_locked(struct swait_queue_head *q);
  10484. +extern void swake_up_all_locked(struct swait_queue_head *q);
  10485. extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
  10486. extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state);
  10487. diff --git a/include/linux/swap.h b/include/linux/swap.h
  10488. index e1d761463243..4ae9a4434ad3 100644
  10489. --- a/include/linux/swap.h
  10490. +++ b/include/linux/swap.h
  10491. @@ -11,6 +11,7 @@
  10492. #include <linux/fs.h>
  10493. #include <linux/atomic.h>
  10494. #include <linux/page-flags.h>
  10495. +#include <linux/locallock.h>
  10496. #include <asm/page.h>
  10497. struct notifier_block;
  10498. @@ -243,7 +244,8 @@ struct swap_info_struct {
  10499. void *workingset_eviction(struct address_space *mapping, struct page *page);
  10500. bool workingset_refault(void *shadow);
  10501. void workingset_activation(struct page *page);
  10502. -extern struct list_lru workingset_shadow_nodes;
  10503. +extern struct list_lru __workingset_shadow_nodes;
  10504. +DECLARE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  10505. static inline unsigned int workingset_node_pages(struct radix_tree_node *node)
  10506. {
  10507. @@ -288,6 +290,7 @@ extern unsigned long nr_free_pagecache_pages(void);
  10508. /* linux/mm/swap.c */
  10509. +DECLARE_LOCAL_IRQ_LOCK(swapvec_lock);
  10510. extern void lru_cache_add(struct page *);
  10511. extern void lru_cache_add_anon(struct page *page);
  10512. extern void lru_cache_add_file(struct page *page);
  10513. diff --git a/include/linux/swork.h b/include/linux/swork.h
  10514. new file mode 100644
  10515. index 000000000000..f175fa9a6016
  10516. --- /dev/null
  10517. +++ b/include/linux/swork.h
  10518. @@ -0,0 +1,24 @@
  10519. +#ifndef _LINUX_SWORK_H
  10520. +#define _LINUX_SWORK_H
  10521. +
  10522. +#include <linux/list.h>
  10523. +
  10524. +struct swork_event {
  10525. + struct list_head item;
  10526. + unsigned long flags;
  10527. + void (*func)(struct swork_event *);
  10528. +};
  10529. +
  10530. +static inline void INIT_SWORK(struct swork_event *event,
  10531. + void (*func)(struct swork_event *))
  10532. +{
  10533. + event->flags = 0;
  10534. + event->func = func;
  10535. +}
  10536. +
  10537. +bool swork_queue(struct swork_event *sev);
  10538. +
  10539. +int swork_get(void);
  10540. +void swork_put(void);
  10541. +
  10542. +#endif /* _LINUX_SWORK_H */
  10543. diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
  10544. index 2b5b10eed74f..8bf15b1858f5 100644
  10545. --- a/include/linux/thread_info.h
  10546. +++ b/include/linux/thread_info.h
  10547. @@ -103,7 +103,17 @@ static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  10548. #define test_thread_flag(flag) \
  10549. test_ti_thread_flag(current_thread_info(), flag)
  10550. -#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  10551. +#ifdef CONFIG_PREEMPT_LAZY
  10552. +#define tif_need_resched() (test_thread_flag(TIF_NEED_RESCHED) || \
  10553. + test_thread_flag(TIF_NEED_RESCHED_LAZY))
  10554. +#define tif_need_resched_now() (test_thread_flag(TIF_NEED_RESCHED))
  10555. +#define tif_need_resched_lazy() test_thread_flag(TIF_NEED_RESCHED_LAZY))
  10556. +
  10557. +#else
  10558. +#define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  10559. +#define tif_need_resched_now() test_thread_flag(TIF_NEED_RESCHED)
  10560. +#define tif_need_resched_lazy() 0
  10561. +#endif
  10562. #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
  10563. static inline int arch_within_stack_frames(const void * const stack,
  10564. diff --git a/include/linux/timer.h b/include/linux/timer.h
  10565. index 51d601f192d4..83cea629efe1 100644
  10566. --- a/include/linux/timer.h
  10567. +++ b/include/linux/timer.h
  10568. @@ -241,7 +241,7 @@ extern void add_timer(struct timer_list *timer);
  10569. extern int try_to_del_timer_sync(struct timer_list *timer);
  10570. -#ifdef CONFIG_SMP
  10571. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  10572. extern int del_timer_sync(struct timer_list *timer);
  10573. #else
  10574. # define del_timer_sync(t) del_timer(t)
  10575. diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
  10576. index be007610ceb0..15154b13a53b 100644
  10577. --- a/include/linux/trace_events.h
  10578. +++ b/include/linux/trace_events.h
  10579. @@ -56,6 +56,9 @@ struct trace_entry {
  10580. unsigned char flags;
  10581. unsigned char preempt_count;
  10582. int pid;
  10583. + unsigned short migrate_disable;
  10584. + unsigned short padding;
  10585. + unsigned char preempt_lazy_count;
  10586. };
  10587. #define TRACE_EVENT_TYPE_MAX \
  10588. diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
  10589. index f30c187ed785..83bf0f798426 100644
  10590. --- a/include/linux/uaccess.h
  10591. +++ b/include/linux/uaccess.h
  10592. @@ -24,6 +24,7 @@ static __always_inline void pagefault_disabled_dec(void)
  10593. */
  10594. static inline void pagefault_disable(void)
  10595. {
  10596. + migrate_disable();
  10597. pagefault_disabled_inc();
  10598. /*
  10599. * make sure to have issued the store before a pagefault
  10600. @@ -40,6 +41,7 @@ static inline void pagefault_enable(void)
  10601. */
  10602. barrier();
  10603. pagefault_disabled_dec();
  10604. + migrate_enable();
  10605. }
  10606. /*
  10607. diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
  10608. index 4a29c75b146e..0a294e950df8 100644
  10609. --- a/include/linux/uprobes.h
  10610. +++ b/include/linux/uprobes.h
  10611. @@ -27,6 +27,7 @@
  10612. #include <linux/errno.h>
  10613. #include <linux/rbtree.h>
  10614. #include <linux/types.h>
  10615. +#include <linux/wait.h>
  10616. struct vm_area_struct;
  10617. struct mm_struct;
  10618. diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h
  10619. index 613771909b6e..e28c5a43229d 100644
  10620. --- a/include/linux/vmstat.h
  10621. +++ b/include/linux/vmstat.h
  10622. @@ -33,7 +33,9 @@ DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
  10623. */
  10624. static inline void __count_vm_event(enum vm_event_item item)
  10625. {
  10626. + preempt_disable_rt();
  10627. raw_cpu_inc(vm_event_states.event[item]);
  10628. + preempt_enable_rt();
  10629. }
  10630. static inline void count_vm_event(enum vm_event_item item)
  10631. @@ -43,7 +45,9 @@ static inline void count_vm_event(enum vm_event_item item)
  10632. static inline void __count_vm_events(enum vm_event_item item, long delta)
  10633. {
  10634. + preempt_disable_rt();
  10635. raw_cpu_add(vm_event_states.event[item], delta);
  10636. + preempt_enable_rt();
  10637. }
  10638. static inline void count_vm_events(enum vm_event_item item, long delta)
  10639. diff --git a/include/linux/wait.h b/include/linux/wait.h
  10640. index c3ff74d764fa..60222150a409 100644
  10641. --- a/include/linux/wait.h
  10642. +++ b/include/linux/wait.h
  10643. @@ -8,6 +8,7 @@
  10644. #include <linux/spinlock.h>
  10645. #include <asm/current.h>
  10646. #include <uapi/linux/wait.h>
  10647. +#include <linux/atomic.h>
  10648. typedef struct __wait_queue wait_queue_t;
  10649. typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
  10650. diff --git a/include/net/dst.h b/include/net/dst.h
  10651. index 6835d224d47b..55a5a9698f14 100644
  10652. --- a/include/net/dst.h
  10653. +++ b/include/net/dst.h
  10654. @@ -446,7 +446,7 @@ static inline void dst_confirm(struct dst_entry *dst)
  10655. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  10656. struct sk_buff *skb)
  10657. {
  10658. - const struct hh_cache *hh;
  10659. + struct hh_cache *hh;
  10660. if (dst->pending_confirm) {
  10661. unsigned long now = jiffies;
  10662. diff --git a/include/net/gen_stats.h b/include/net/gen_stats.h
  10663. index 231e121cc7d9..d125222b979d 100644
  10664. --- a/include/net/gen_stats.h
  10665. +++ b/include/net/gen_stats.h
  10666. @@ -5,6 +5,7 @@
  10667. #include <linux/socket.h>
  10668. #include <linux/rtnetlink.h>
  10669. #include <linux/pkt_sched.h>
  10670. +#include <net/net_seq_lock.h>
  10671. struct gnet_stats_basic_cpu {
  10672. struct gnet_stats_basic_packed bstats;
  10673. @@ -33,11 +34,11 @@ int gnet_stats_start_copy_compat(struct sk_buff *skb, int type,
  10674. spinlock_t *lock, struct gnet_dump *d,
  10675. int padattr);
  10676. -int gnet_stats_copy_basic(const seqcount_t *running,
  10677. +int gnet_stats_copy_basic(net_seqlock_t *running,
  10678. struct gnet_dump *d,
  10679. struct gnet_stats_basic_cpu __percpu *cpu,
  10680. struct gnet_stats_basic_packed *b);
  10681. -void __gnet_stats_copy_basic(const seqcount_t *running,
  10682. +void __gnet_stats_copy_basic(net_seqlock_t *running,
  10683. struct gnet_stats_basic_packed *bstats,
  10684. struct gnet_stats_basic_cpu __percpu *cpu,
  10685. struct gnet_stats_basic_packed *b);
  10686. @@ -55,14 +56,14 @@ int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
  10687. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  10688. struct gnet_stats_rate_est64 *rate_est,
  10689. spinlock_t *stats_lock,
  10690. - seqcount_t *running, struct nlattr *opt);
  10691. + net_seqlock_t *running, struct nlattr *opt);
  10692. void gen_kill_estimator(struct gnet_stats_basic_packed *bstats,
  10693. struct gnet_stats_rate_est64 *rate_est);
  10694. int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  10695. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  10696. struct gnet_stats_rate_est64 *rate_est,
  10697. spinlock_t *stats_lock,
  10698. - seqcount_t *running, struct nlattr *opt);
  10699. + net_seqlock_t *running, struct nlattr *opt);
  10700. bool gen_estimator_active(const struct gnet_stats_basic_packed *bstats,
  10701. const struct gnet_stats_rate_est64 *rate_est);
  10702. #endif
  10703. diff --git a/include/net/neighbour.h b/include/net/neighbour.h
  10704. index 8b683841e574..bf656008f6e7 100644
  10705. --- a/include/net/neighbour.h
  10706. +++ b/include/net/neighbour.h
  10707. @@ -446,7 +446,7 @@ static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  10708. }
  10709. #endif
  10710. -static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  10711. +static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  10712. {
  10713. unsigned int seq;
  10714. int hh_len;
  10715. @@ -501,7 +501,7 @@ struct neighbour_cb {
  10716. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  10717. -static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  10718. +static inline void neigh_ha_snapshot(char *dst, struct neighbour *n,
  10719. const struct net_device *dev)
  10720. {
  10721. unsigned int seq;
  10722. diff --git a/include/net/net_seq_lock.h b/include/net/net_seq_lock.h
  10723. new file mode 100644
  10724. index 000000000000..a7034298a82a
  10725. --- /dev/null
  10726. +++ b/include/net/net_seq_lock.h
  10727. @@ -0,0 +1,15 @@
  10728. +#ifndef __NET_NET_SEQ_LOCK_H__
  10729. +#define __NET_NET_SEQ_LOCK_H__
  10730. +
  10731. +#ifdef CONFIG_PREEMPT_RT_BASE
  10732. +# define net_seqlock_t seqlock_t
  10733. +# define net_seq_begin(__r) read_seqbegin(__r)
  10734. +# define net_seq_retry(__r, __s) read_seqretry(__r, __s)
  10735. +
  10736. +#else
  10737. +# define net_seqlock_t seqcount_t
  10738. +# define net_seq_begin(__r) read_seqcount_begin(__r)
  10739. +# define net_seq_retry(__r, __s) read_seqcount_retry(__r, __s)
  10740. +#endif
  10741. +
  10742. +#endif
  10743. diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
  10744. index d061ffeb1e71..12ef433dc3b8 100644
  10745. --- a/include/net/netns/ipv4.h
  10746. +++ b/include/net/netns/ipv4.h
  10747. @@ -70,6 +70,7 @@ struct netns_ipv4 {
  10748. int sysctl_icmp_echo_ignore_all;
  10749. int sysctl_icmp_echo_ignore_broadcasts;
  10750. + int sysctl_icmp_echo_sysrq;
  10751. int sysctl_icmp_ignore_bogus_error_responses;
  10752. int sysctl_icmp_ratelimit;
  10753. int sysctl_icmp_ratemask;
  10754. diff --git a/include/net/sch_generic.h b/include/net/sch_generic.h
  10755. index 909aff2db2b3..c47219d6e4bc 100644
  10756. --- a/include/net/sch_generic.h
  10757. +++ b/include/net/sch_generic.h
  10758. @@ -10,6 +10,7 @@
  10759. #include <linux/dynamic_queue_limits.h>
  10760. #include <net/gen_stats.h>
  10761. #include <net/rtnetlink.h>
  10762. +#include <net/net_seq_lock.h>
  10763. struct Qdisc_ops;
  10764. struct qdisc_walker;
  10765. @@ -78,7 +79,7 @@ struct Qdisc {
  10766. struct sk_buff *gso_skb ____cacheline_aligned_in_smp;
  10767. struct sk_buff_head q;
  10768. struct gnet_stats_basic_packed bstats;
  10769. - seqcount_t running;
  10770. + net_seqlock_t running;
  10771. struct gnet_stats_queue qstats;
  10772. unsigned long state;
  10773. struct Qdisc *next_sched;
  10774. @@ -90,13 +91,22 @@ struct Qdisc {
  10775. spinlock_t busylock ____cacheline_aligned_in_smp;
  10776. };
  10777. -static inline bool qdisc_is_running(const struct Qdisc *qdisc)
  10778. +static inline bool qdisc_is_running(struct Qdisc *qdisc)
  10779. {
  10780. +#ifdef CONFIG_PREEMPT_RT_BASE
  10781. + return spin_is_locked(&qdisc->running.lock) ? true : false;
  10782. +#else
  10783. return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
  10784. +#endif
  10785. }
  10786. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  10787. {
  10788. +#ifdef CONFIG_PREEMPT_RT_BASE
  10789. + if (try_write_seqlock(&qdisc->running))
  10790. + return true;
  10791. + return false;
  10792. +#else
  10793. if (qdisc_is_running(qdisc))
  10794. return false;
  10795. /* Variant of write_seqcount_begin() telling lockdep a trylock
  10796. @@ -105,11 +115,16 @@ static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  10797. raw_write_seqcount_begin(&qdisc->running);
  10798. seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
  10799. return true;
  10800. +#endif
  10801. }
  10802. static inline void qdisc_run_end(struct Qdisc *qdisc)
  10803. {
  10804. +#ifdef CONFIG_PREEMPT_RT_BASE
  10805. + write_sequnlock(&qdisc->running);
  10806. +#else
  10807. write_seqcount_end(&qdisc->running);
  10808. +#endif
  10809. }
  10810. static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
  10811. @@ -300,7 +315,7 @@ static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
  10812. return qdisc_lock(root);
  10813. }
  10814. -static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  10815. +static inline net_seqlock_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  10816. {
  10817. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  10818. diff --git a/include/trace/events/hist.h b/include/trace/events/hist.h
  10819. new file mode 100644
  10820. index 000000000000..f7710de1b1f3
  10821. --- /dev/null
  10822. +++ b/include/trace/events/hist.h
  10823. @@ -0,0 +1,73 @@
  10824. +#undef TRACE_SYSTEM
  10825. +#define TRACE_SYSTEM hist
  10826. +
  10827. +#if !defined(_TRACE_HIST_H) || defined(TRACE_HEADER_MULTI_READ)
  10828. +#define _TRACE_HIST_H
  10829. +
  10830. +#include "latency_hist.h"
  10831. +#include <linux/tracepoint.h>
  10832. +
  10833. +#if !defined(CONFIG_PREEMPT_OFF_HIST) && !defined(CONFIG_INTERRUPT_OFF_HIST)
  10834. +#define trace_preemptirqsoff_hist(a, b)
  10835. +#define trace_preemptirqsoff_hist_rcuidle(a, b)
  10836. +#else
  10837. +TRACE_EVENT(preemptirqsoff_hist,
  10838. +
  10839. + TP_PROTO(int reason, int starthist),
  10840. +
  10841. + TP_ARGS(reason, starthist),
  10842. +
  10843. + TP_STRUCT__entry(
  10844. + __field(int, reason)
  10845. + __field(int, starthist)
  10846. + ),
  10847. +
  10848. + TP_fast_assign(
  10849. + __entry->reason = reason;
  10850. + __entry->starthist = starthist;
  10851. + ),
  10852. +
  10853. + TP_printk("reason=%s starthist=%s", getaction(__entry->reason),
  10854. + __entry->starthist ? "start" : "stop")
  10855. +);
  10856. +#endif
  10857. +
  10858. +#ifndef CONFIG_MISSED_TIMER_OFFSETS_HIST
  10859. +#define trace_hrtimer_interrupt(a, b, c, d)
  10860. +#else
  10861. +TRACE_EVENT(hrtimer_interrupt,
  10862. +
  10863. + TP_PROTO(int cpu, long long offset, struct task_struct *curr,
  10864. + struct task_struct *task),
  10865. +
  10866. + TP_ARGS(cpu, offset, curr, task),
  10867. +
  10868. + TP_STRUCT__entry(
  10869. + __field(int, cpu)
  10870. + __field(long long, offset)
  10871. + __array(char, ccomm, TASK_COMM_LEN)
  10872. + __field(int, cprio)
  10873. + __array(char, tcomm, TASK_COMM_LEN)
  10874. + __field(int, tprio)
  10875. + ),
  10876. +
  10877. + TP_fast_assign(
  10878. + __entry->cpu = cpu;
  10879. + __entry->offset = offset;
  10880. + memcpy(__entry->ccomm, curr->comm, TASK_COMM_LEN);
  10881. + __entry->cprio = curr->prio;
  10882. + memcpy(__entry->tcomm, task != NULL ? task->comm : "<none>",
  10883. + task != NULL ? TASK_COMM_LEN : 7);
  10884. + __entry->tprio = task != NULL ? task->prio : -1;
  10885. + ),
  10886. +
  10887. + TP_printk("cpu=%d offset=%lld curr=%s[%d] thread=%s[%d]",
  10888. + __entry->cpu, __entry->offset, __entry->ccomm,
  10889. + __entry->cprio, __entry->tcomm, __entry->tprio)
  10890. +);
  10891. +#endif
  10892. +
  10893. +#endif /* _TRACE_HIST_H */
  10894. +
  10895. +/* This part must be outside protection */
  10896. +#include <trace/define_trace.h>
  10897. diff --git a/include/trace/events/latency_hist.h b/include/trace/events/latency_hist.h
  10898. new file mode 100644
  10899. index 000000000000..d3f2fbd560b1
  10900. --- /dev/null
  10901. +++ b/include/trace/events/latency_hist.h
  10902. @@ -0,0 +1,29 @@
  10903. +#ifndef _LATENCY_HIST_H
  10904. +#define _LATENCY_HIST_H
  10905. +
  10906. +enum hist_action {
  10907. + IRQS_ON,
  10908. + PREEMPT_ON,
  10909. + TRACE_STOP,
  10910. + IRQS_OFF,
  10911. + PREEMPT_OFF,
  10912. + TRACE_START,
  10913. +};
  10914. +
  10915. +static char *actions[] = {
  10916. + "IRQS_ON",
  10917. + "PREEMPT_ON",
  10918. + "TRACE_STOP",
  10919. + "IRQS_OFF",
  10920. + "PREEMPT_OFF",
  10921. + "TRACE_START",
  10922. +};
  10923. +
  10924. +static inline char *getaction(int action)
  10925. +{
  10926. + if (action >= 0 && action <= sizeof(actions)/sizeof(actions[0]))
  10927. + return actions[action];
  10928. + return "unknown";
  10929. +}
  10930. +
  10931. +#endif /* _LATENCY_HIST_H */
  10932. diff --git a/init/Kconfig b/init/Kconfig
  10933. index cac3f096050d..b6c9166d878a 100644
  10934. --- a/init/Kconfig
  10935. +++ b/init/Kconfig
  10936. @@ -496,7 +496,7 @@ config TINY_RCU
  10937. config RCU_EXPERT
  10938. bool "Make expert-level adjustments to RCU configuration"
  10939. - default n
  10940. + default y if PREEMPT_RT_FULL
  10941. help
  10942. This option needs to be enabled if you wish to make
  10943. expert-level adjustments to RCU configuration. By default,
  10944. @@ -613,7 +613,7 @@ config RCU_FANOUT_LEAF
  10945. config RCU_FAST_NO_HZ
  10946. bool "Accelerate last non-dyntick-idle CPU's grace periods"
  10947. - depends on NO_HZ_COMMON && SMP && RCU_EXPERT
  10948. + depends on NO_HZ_COMMON && SMP && RCU_EXPERT && !PREEMPT_RT_FULL
  10949. default n
  10950. help
  10951. This option permits CPUs to enter dynticks-idle state even if
  10952. @@ -640,7 +640,7 @@ config TREE_RCU_TRACE
  10953. config RCU_BOOST
  10954. bool "Enable RCU priority boosting"
  10955. depends on RT_MUTEXES && PREEMPT_RCU && RCU_EXPERT
  10956. - default n
  10957. + default y if PREEMPT_RT_FULL
  10958. help
  10959. This option boosts the priority of preempted RCU readers that
  10960. block the current preemptible RCU grace period for too long.
  10961. @@ -1054,6 +1054,7 @@ config CFS_BANDWIDTH
  10962. config RT_GROUP_SCHED
  10963. bool "Group scheduling for SCHED_RR/FIFO"
  10964. depends on CGROUP_SCHED
  10965. + depends on !PREEMPT_RT_FULL
  10966. default n
  10967. help
  10968. This feature lets you explicitly allocate real CPU bandwidth
  10969. @@ -1761,6 +1762,7 @@ choice
  10970. config SLAB
  10971. bool "SLAB"
  10972. + depends on !PREEMPT_RT_FULL
  10973. select HAVE_HARDENED_USERCOPY_ALLOCATOR
  10974. help
  10975. The regular slab allocator that is established and known to work
  10976. @@ -1781,6 +1783,7 @@ config SLUB
  10977. config SLOB
  10978. depends on EXPERT
  10979. bool "SLOB (Simple Allocator)"
  10980. + depends on !PREEMPT_RT_FULL
  10981. help
  10982. SLOB replaces the stock allocator with a drastically simpler
  10983. allocator. SLOB is generally more space efficient but
  10984. @@ -1799,7 +1802,7 @@ config SLAB_FREELIST_RANDOM
  10985. config SLUB_CPU_PARTIAL
  10986. default y
  10987. - depends on SLUB && SMP
  10988. + depends on SLUB && SMP && !PREEMPT_RT_FULL
  10989. bool "SLUB per cpu partial cache"
  10990. help
  10991. Per cpu partial caches accellerate objects allocation and freeing
  10992. diff --git a/init/Makefile b/init/Makefile
  10993. index 7bc47ee31c36..88cf473554e0 100644
  10994. --- a/init/Makefile
  10995. +++ b/init/Makefile
  10996. @@ -33,4 +33,4 @@ $(obj)/version.o: include/generated/compile.h
  10997. include/generated/compile.h: FORCE
  10998. @$($(quiet)chk_compile.h)
  10999. $(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
  11000. - "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CC) $(KBUILD_CFLAGS)"
  11001. + "$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" "$(CONFIG_PREEMPT_RT_FULL)" "$(CC) $(KBUILD_CFLAGS)"
  11002. diff --git a/init/main.c b/init/main.c
  11003. index a8a58e2794a5..e4c979e37a91 100644
  11004. --- a/init/main.c
  11005. +++ b/init/main.c
  11006. @@ -507,6 +507,7 @@ asmlinkage __visible void __init start_kernel(void)
  11007. setup_command_line(command_line);
  11008. setup_nr_cpu_ids();
  11009. setup_per_cpu_areas();
  11010. + softirq_early_init();
  11011. boot_cpu_state_init();
  11012. smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
  11013. diff --git a/ipc/msg.c b/ipc/msg.c
  11014. index c6521c205cb4..996d89023552 100644
  11015. --- a/ipc/msg.c
  11016. +++ b/ipc/msg.c
  11017. @@ -183,20 +183,14 @@ static void ss_wakeup(struct list_head *h, int kill)
  11018. }
  11019. }
  11020. -static void expunge_all(struct msg_queue *msq, int res)
  11021. +static void expunge_all(struct msg_queue *msq, int res,
  11022. + struct wake_q_head *wake_q)
  11023. {
  11024. struct msg_receiver *msr, *t;
  11025. list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
  11026. - msr->r_msg = NULL; /* initialize expunge ordering */
  11027. - wake_up_process(msr->r_tsk);
  11028. - /*
  11029. - * Ensure that the wakeup is visible before setting r_msg as
  11030. - * the receiving end depends on it: either spinning on a nil,
  11031. - * or dealing with -EAGAIN cases. See lockless receive part 1
  11032. - * and 2 in do_msgrcv().
  11033. - */
  11034. - smp_wmb(); /* barrier (B) */
  11035. +
  11036. + wake_q_add(wake_q, msr->r_tsk);
  11037. msr->r_msg = ERR_PTR(res);
  11038. }
  11039. }
  11040. @@ -213,11 +207,13 @@ static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  11041. {
  11042. struct msg_msg *msg, *t;
  11043. struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
  11044. + WAKE_Q(wake_q);
  11045. - expunge_all(msq, -EIDRM);
  11046. + expunge_all(msq, -EIDRM, &wake_q);
  11047. ss_wakeup(&msq->q_senders, 1);
  11048. msg_rmid(ns, msq);
  11049. ipc_unlock_object(&msq->q_perm);
  11050. + wake_up_q(&wake_q);
  11051. rcu_read_unlock();
  11052. list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
  11053. @@ -342,6 +338,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  11054. struct kern_ipc_perm *ipcp;
  11055. struct msqid64_ds uninitialized_var(msqid64);
  11056. struct msg_queue *msq;
  11057. + WAKE_Q(wake_q);
  11058. int err;
  11059. if (cmd == IPC_SET) {
  11060. @@ -389,7 +386,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  11061. /* sleeping receivers might be excluded by
  11062. * stricter permissions.
  11063. */
  11064. - expunge_all(msq, -EAGAIN);
  11065. + expunge_all(msq, -EAGAIN, &wake_q);
  11066. /* sleeping senders might be able to send
  11067. * due to a larger queue size.
  11068. */
  11069. @@ -402,6 +399,7 @@ static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
  11070. out_unlock0:
  11071. ipc_unlock_object(&msq->q_perm);
  11072. + wake_up_q(&wake_q);
  11073. out_unlock1:
  11074. rcu_read_unlock();
  11075. out_up:
  11076. @@ -566,7 +564,8 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
  11077. return 0;
  11078. }
  11079. -static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  11080. +static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  11081. + struct wake_q_head *wake_q)
  11082. {
  11083. struct msg_receiver *msr, *t;
  11084. @@ -577,27 +576,13 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
  11085. list_del(&msr->r_list);
  11086. if (msr->r_maxsize < msg->m_ts) {
  11087. - /* initialize pipelined send ordering */
  11088. - msr->r_msg = NULL;
  11089. - wake_up_process(msr->r_tsk);
  11090. - /* barrier (B) see barrier comment below */
  11091. - smp_wmb();
  11092. + wake_q_add(wake_q, msr->r_tsk);
  11093. msr->r_msg = ERR_PTR(-E2BIG);
  11094. } else {
  11095. - msr->r_msg = NULL;
  11096. msq->q_lrpid = task_pid_vnr(msr->r_tsk);
  11097. msq->q_rtime = get_seconds();
  11098. - wake_up_process(msr->r_tsk);
  11099. - /*
  11100. - * Ensure that the wakeup is visible before
  11101. - * setting r_msg, as the receiving can otherwise
  11102. - * exit - once r_msg is set, the receiver can
  11103. - * continue. See lockless receive part 1 and 2
  11104. - * in do_msgrcv(). Barrier (B).
  11105. - */
  11106. - smp_wmb();
  11107. + wake_q_add(wake_q, msr->r_tsk);
  11108. msr->r_msg = msg;
  11109. -
  11110. return 1;
  11111. }
  11112. }
  11113. @@ -613,6 +598,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  11114. struct msg_msg *msg;
  11115. int err;
  11116. struct ipc_namespace *ns;
  11117. + WAKE_Q(wake_q);
  11118. ns = current->nsproxy->ipc_ns;
  11119. @@ -698,7 +684,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  11120. msq->q_lspid = task_tgid_vnr(current);
  11121. msq->q_stime = get_seconds();
  11122. - if (!pipelined_send(msq, msg)) {
  11123. + if (!pipelined_send(msq, msg, &wake_q)) {
  11124. /* no one is waiting for this message, enqueue it */
  11125. list_add_tail(&msg->m_list, &msq->q_messages);
  11126. msq->q_cbytes += msgsz;
  11127. @@ -712,6 +698,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
  11128. out_unlock0:
  11129. ipc_unlock_object(&msq->q_perm);
  11130. + wake_up_q(&wake_q);
  11131. out_unlock1:
  11132. rcu_read_unlock();
  11133. if (msg != NULL)
  11134. @@ -932,57 +919,25 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgfl
  11135. rcu_read_lock();
  11136. /* Lockless receive, part 2:
  11137. - * Wait until pipelined_send or expunge_all are outside of
  11138. - * wake_up_process(). There is a race with exit(), see
  11139. - * ipc/mqueue.c for the details. The correct serialization
  11140. - * ensures that a receiver cannot continue without the wakeup
  11141. - * being visibible _before_ setting r_msg:
  11142. + * The work in pipelined_send() and expunge_all():
  11143. + * - Set pointer to message
  11144. + * - Queue the receiver task for later wakeup
  11145. + * - Wake up the process after the lock is dropped.
  11146. *
  11147. - * CPU 0 CPU 1
  11148. - * <loop receiver>
  11149. - * smp_rmb(); (A) <-- pair -. <waker thread>
  11150. - * <load ->r_msg> | msr->r_msg = NULL;
  11151. - * | wake_up_process();
  11152. - * <continue> `------> smp_wmb(); (B)
  11153. - * msr->r_msg = msg;
  11154. - *
  11155. - * Where (A) orders the message value read and where (B) orders
  11156. - * the write to the r_msg -- done in both pipelined_send and
  11157. - * expunge_all.
  11158. + * Should the process wake up before this wakeup (due to a
  11159. + * signal) it will either see the message and continue …
  11160. */
  11161. - for (;;) {
  11162. - /*
  11163. - * Pairs with writer barrier in pipelined_send
  11164. - * or expunge_all.
  11165. - */
  11166. - smp_rmb(); /* barrier (A) */
  11167. - msg = (struct msg_msg *)msr_d.r_msg;
  11168. - if (msg)
  11169. - break;
  11170. - /*
  11171. - * The cpu_relax() call is a compiler barrier
  11172. - * which forces everything in this loop to be
  11173. - * re-loaded.
  11174. - */
  11175. - cpu_relax();
  11176. - }
  11177. -
  11178. - /* Lockless receive, part 3:
  11179. - * If there is a message or an error then accept it without
  11180. - * locking.
  11181. - */
  11182. + msg = (struct msg_msg *)msr_d.r_msg;
  11183. if (msg != ERR_PTR(-EAGAIN))
  11184. goto out_unlock1;
  11185. - /* Lockless receive, part 3:
  11186. - * Acquire the queue spinlock.
  11187. - */
  11188. + /*
  11189. + * … or see -EAGAIN, acquire the lock to check the message
  11190. + * again.
  11191. + */
  11192. ipc_lock_object(&msq->q_perm);
  11193. - /* Lockless receive, part 4:
  11194. - * Repeat test after acquiring the spinlock.
  11195. - */
  11196. msg = (struct msg_msg *)msr_d.r_msg;
  11197. if (msg != ERR_PTR(-EAGAIN))
  11198. goto out_unlock0;
  11199. diff --git a/ipc/sem.c b/ipc/sem.c
  11200. index 5e318c5f749d..ec9203971539 100644
  11201. --- a/ipc/sem.c
  11202. +++ b/ipc/sem.c
  11203. @@ -712,6 +712,13 @@ static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
  11204. static void wake_up_sem_queue_prepare(struct list_head *pt,
  11205. struct sem_queue *q, int error)
  11206. {
  11207. +#ifdef CONFIG_PREEMPT_RT_BASE
  11208. + struct task_struct *p = q->sleeper;
  11209. + get_task_struct(p);
  11210. + q->status = error;
  11211. + wake_up_process(p);
  11212. + put_task_struct(p);
  11213. +#else
  11214. if (list_empty(pt)) {
  11215. /*
  11216. * Hold preempt off so that we don't get preempted and have the
  11217. @@ -723,6 +730,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  11218. q->pid = error;
  11219. list_add_tail(&q->list, pt);
  11220. +#endif
  11221. }
  11222. /**
  11223. @@ -736,6 +744,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt,
  11224. */
  11225. static void wake_up_sem_queue_do(struct list_head *pt)
  11226. {
  11227. +#ifndef CONFIG_PREEMPT_RT_BASE
  11228. struct sem_queue *q, *t;
  11229. int did_something;
  11230. @@ -748,6 +757,7 @@ static void wake_up_sem_queue_do(struct list_head *pt)
  11231. }
  11232. if (did_something)
  11233. preempt_enable();
  11234. +#endif
  11235. }
  11236. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  11237. diff --git a/kernel/Kconfig.locks b/kernel/Kconfig.locks
  11238. index ebdb0043203a..b9e6aa7e5aa6 100644
  11239. --- a/kernel/Kconfig.locks
  11240. +++ b/kernel/Kconfig.locks
  11241. @@ -225,11 +225,11 @@ config ARCH_SUPPORTS_ATOMIC_RMW
  11242. config MUTEX_SPIN_ON_OWNER
  11243. def_bool y
  11244. - depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW
  11245. + depends on SMP && !DEBUG_MUTEXES && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  11246. config RWSEM_SPIN_ON_OWNER
  11247. def_bool y
  11248. - depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW
  11249. + depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW && !PREEMPT_RT_FULL
  11250. config LOCK_SPIN_ON_OWNER
  11251. def_bool y
  11252. diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
  11253. index 3f9c97419f02..11dbe26a8279 100644
  11254. --- a/kernel/Kconfig.preempt
  11255. +++ b/kernel/Kconfig.preempt
  11256. @@ -1,3 +1,16 @@
  11257. +config PREEMPT
  11258. + bool
  11259. + select PREEMPT_COUNT
  11260. +
  11261. +config PREEMPT_RT_BASE
  11262. + bool
  11263. + select PREEMPT
  11264. +
  11265. +config HAVE_PREEMPT_LAZY
  11266. + bool
  11267. +
  11268. +config PREEMPT_LAZY
  11269. + def_bool y if HAVE_PREEMPT_LAZY && PREEMPT_RT_FULL
  11270. choice
  11271. prompt "Preemption Model"
  11272. @@ -33,9 +46,9 @@ config PREEMPT_VOLUNTARY
  11273. Select this if you are building a kernel for a desktop system.
  11274. -config PREEMPT
  11275. +config PREEMPT__LL
  11276. bool "Preemptible Kernel (Low-Latency Desktop)"
  11277. - select PREEMPT_COUNT
  11278. + select PREEMPT
  11279. select UNINLINE_SPIN_UNLOCK if !ARCH_INLINE_SPIN_UNLOCK
  11280. help
  11281. This option reduces the latency of the kernel by making
  11282. @@ -52,6 +65,22 @@ config PREEMPT
  11283. embedded system with latency requirements in the milliseconds
  11284. range.
  11285. +config PREEMPT_RTB
  11286. + bool "Preemptible Kernel (Basic RT)"
  11287. + select PREEMPT_RT_BASE
  11288. + help
  11289. + This option is basically the same as (Low-Latency Desktop) but
  11290. + enables changes which are preliminary for the full preemptible
  11291. + RT kernel.
  11292. +
  11293. +config PREEMPT_RT_FULL
  11294. + bool "Fully Preemptible Kernel (RT)"
  11295. + depends on IRQ_FORCED_THREADING
  11296. + select PREEMPT_RT_BASE
  11297. + select PREEMPT_RCU
  11298. + help
  11299. + All and everything
  11300. +
  11301. endchoice
  11302. config PREEMPT_COUNT
  11303. diff --git a/kernel/Makefile b/kernel/Makefile
  11304. index e2ec54e2b952..bff8214bf5f6 100644
  11305. --- a/kernel/Makefile
  11306. +++ b/kernel/Makefile
  11307. @@ -11,6 +11,13 @@ obj-y = fork.o exec_domain.o panic.o \
  11308. notifier.o ksysfs.o cred.o reboot.o \
  11309. async.o range.o smpboot.o
  11310. +# Tracing may do some dangerous __builtin_return_address() operations
  11311. +# We know they are dangerous, we don't need gcc telling us that.
  11312. +ifdef CONFIG_USING_GET_LOCK_PARENT_IP
  11313. +FRAME_CFLAGS := $(call cc-disable-warning,frame-address)
  11314. +KBUILD_CFLAGS += $(FRAME_CFLAGS)
  11315. +endif
  11316. +
  11317. obj-$(CONFIG_MULTIUSER) += groups.o
  11318. ifdef CONFIG_FUNCTION_TRACER
  11319. diff --git a/kernel/cgroup.c b/kernel/cgroup.c
  11320. index d6b729beba49..11d61b2ca938 100644
  11321. --- a/kernel/cgroup.c
  11322. +++ b/kernel/cgroup.c
  11323. @@ -5027,10 +5027,10 @@ static void css_free_rcu_fn(struct rcu_head *rcu_head)
  11324. queue_work(cgroup_destroy_wq, &css->destroy_work);
  11325. }
  11326. -static void css_release_work_fn(struct work_struct *work)
  11327. +static void css_release_work_fn(struct swork_event *sev)
  11328. {
  11329. struct cgroup_subsys_state *css =
  11330. - container_of(work, struct cgroup_subsys_state, destroy_work);
  11331. + container_of(sev, struct cgroup_subsys_state, destroy_swork);
  11332. struct cgroup_subsys *ss = css->ss;
  11333. struct cgroup *cgrp = css->cgroup;
  11334. @@ -5071,8 +5071,8 @@ static void css_release(struct percpu_ref *ref)
  11335. struct cgroup_subsys_state *css =
  11336. container_of(ref, struct cgroup_subsys_state, refcnt);
  11337. - INIT_WORK(&css->destroy_work, css_release_work_fn);
  11338. - queue_work(cgroup_destroy_wq, &css->destroy_work);
  11339. + INIT_SWORK(&css->destroy_swork, css_release_work_fn);
  11340. + swork_queue(&css->destroy_swork);
  11341. }
  11342. static void init_and_link_css(struct cgroup_subsys_state *css,
  11343. @@ -5716,6 +5716,7 @@ static int __init cgroup_wq_init(void)
  11344. */
  11345. cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
  11346. BUG_ON(!cgroup_destroy_wq);
  11347. + BUG_ON(swork_get());
  11348. /*
  11349. * Used to destroy pidlists and separate to serve as flush domain.
  11350. diff --git a/kernel/cpu.c b/kernel/cpu.c
  11351. index 341bf80f80bd..b575429a8a00 100644
  11352. --- a/kernel/cpu.c
  11353. +++ b/kernel/cpu.c
  11354. @@ -152,8 +152,8 @@ static struct {
  11355. #endif
  11356. } cpu_hotplug = {
  11357. .active_writer = NULL,
  11358. - .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  11359. .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
  11360. + .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
  11361. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  11362. .dep_map = {.name = "cpu_hotplug.lock" },
  11363. #endif
  11364. @@ -166,6 +166,289 @@ static struct {
  11365. #define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
  11366. #define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
  11367. +/**
  11368. + * hotplug_pcp - per cpu hotplug descriptor
  11369. + * @unplug: set when pin_current_cpu() needs to sync tasks
  11370. + * @sync_tsk: the task that waits for tasks to finish pinned sections
  11371. + * @refcount: counter of tasks in pinned sections
  11372. + * @grab_lock: set when the tasks entering pinned sections should wait
  11373. + * @synced: notifier for @sync_tsk to tell cpu_down it's finished
  11374. + * @mutex: the mutex to make tasks wait (used when @grab_lock is true)
  11375. + * @mutex_init: zero if the mutex hasn't been initialized yet.
  11376. + *
  11377. + * Although @unplug and @sync_tsk may point to the same task, the @unplug
  11378. + * is used as a flag and still exists after @sync_tsk has exited and
  11379. + * @sync_tsk set to NULL.
  11380. + */
  11381. +struct hotplug_pcp {
  11382. + struct task_struct *unplug;
  11383. + struct task_struct *sync_tsk;
  11384. + int refcount;
  11385. + int grab_lock;
  11386. + struct completion synced;
  11387. + struct completion unplug_wait;
  11388. +#ifdef CONFIG_PREEMPT_RT_FULL
  11389. + /*
  11390. + * Note, on PREEMPT_RT, the hotplug lock must save the state of
  11391. + * the task, otherwise the mutex will cause the task to fail
  11392. + * to sleep when required. (Because it's called from migrate_disable())
  11393. + *
  11394. + * The spinlock_t on PREEMPT_RT is a mutex that saves the task's
  11395. + * state.
  11396. + */
  11397. + spinlock_t lock;
  11398. +#else
  11399. + struct mutex mutex;
  11400. +#endif
  11401. + int mutex_init;
  11402. +};
  11403. +
  11404. +#ifdef CONFIG_PREEMPT_RT_FULL
  11405. +# define hotplug_lock(hp) rt_spin_lock__no_mg(&(hp)->lock)
  11406. +# define hotplug_unlock(hp) rt_spin_unlock__no_mg(&(hp)->lock)
  11407. +#else
  11408. +# define hotplug_lock(hp) mutex_lock(&(hp)->mutex)
  11409. +# define hotplug_unlock(hp) mutex_unlock(&(hp)->mutex)
  11410. +#endif
  11411. +
  11412. +static DEFINE_PER_CPU(struct hotplug_pcp, hotplug_pcp);
  11413. +
  11414. +/**
  11415. + * pin_current_cpu - Prevent the current cpu from being unplugged
  11416. + *
  11417. + * Lightweight version of get_online_cpus() to prevent cpu from being
  11418. + * unplugged when code runs in a migration disabled region.
  11419. + *
  11420. + * Must be called with preemption disabled (preempt_count = 1)!
  11421. + */
  11422. +void pin_current_cpu(void)
  11423. +{
  11424. + struct hotplug_pcp *hp;
  11425. + int force = 0;
  11426. +
  11427. +retry:
  11428. + hp = this_cpu_ptr(&hotplug_pcp);
  11429. +
  11430. + if (!hp->unplug || hp->refcount || force || preempt_count() > 1 ||
  11431. + hp->unplug == current) {
  11432. + hp->refcount++;
  11433. + return;
  11434. + }
  11435. + if (hp->grab_lock) {
  11436. + preempt_enable();
  11437. + hotplug_lock(hp);
  11438. + hotplug_unlock(hp);
  11439. + } else {
  11440. + preempt_enable();
  11441. + /*
  11442. + * Try to push this task off of this CPU.
  11443. + */
  11444. + if (!migrate_me()) {
  11445. + preempt_disable();
  11446. + hp = this_cpu_ptr(&hotplug_pcp);
  11447. + if (!hp->grab_lock) {
  11448. + /*
  11449. + * Just let it continue it's already pinned
  11450. + * or about to sleep.
  11451. + */
  11452. + force = 1;
  11453. + goto retry;
  11454. + }
  11455. + preempt_enable();
  11456. + }
  11457. + }
  11458. + preempt_disable();
  11459. + goto retry;
  11460. +}
  11461. +
  11462. +/**
  11463. + * unpin_current_cpu - Allow unplug of current cpu
  11464. + *
  11465. + * Must be called with preemption or interrupts disabled!
  11466. + */
  11467. +void unpin_current_cpu(void)
  11468. +{
  11469. + struct hotplug_pcp *hp = this_cpu_ptr(&hotplug_pcp);
  11470. +
  11471. + WARN_ON(hp->refcount <= 0);
  11472. +
  11473. + /* This is safe. sync_unplug_thread is pinned to this cpu */
  11474. + if (!--hp->refcount && hp->unplug && hp->unplug != current)
  11475. + wake_up_process(hp->unplug);
  11476. +}
  11477. +
  11478. +static void wait_for_pinned_cpus(struct hotplug_pcp *hp)
  11479. +{
  11480. + set_current_state(TASK_UNINTERRUPTIBLE);
  11481. + while (hp->refcount) {
  11482. + schedule_preempt_disabled();
  11483. + set_current_state(TASK_UNINTERRUPTIBLE);
  11484. + }
  11485. +}
  11486. +
  11487. +static int sync_unplug_thread(void *data)
  11488. +{
  11489. + struct hotplug_pcp *hp = data;
  11490. +
  11491. + wait_for_completion(&hp->unplug_wait);
  11492. + preempt_disable();
  11493. + hp->unplug = current;
  11494. + wait_for_pinned_cpus(hp);
  11495. +
  11496. + /*
  11497. + * This thread will synchronize the cpu_down() with threads
  11498. + * that have pinned the CPU. When the pinned CPU count reaches
  11499. + * zero, we inform the cpu_down code to continue to the next step.
  11500. + */
  11501. + set_current_state(TASK_UNINTERRUPTIBLE);
  11502. + preempt_enable();
  11503. + complete(&hp->synced);
  11504. +
  11505. + /*
  11506. + * If all succeeds, the next step will need tasks to wait till
  11507. + * the CPU is offline before continuing. To do this, the grab_lock
  11508. + * is set and tasks going into pin_current_cpu() will block on the
  11509. + * mutex. But we still need to wait for those that are already in
  11510. + * pinned CPU sections. If the cpu_down() failed, the kthread_should_stop()
  11511. + * will kick this thread out.
  11512. + */
  11513. + while (!hp->grab_lock && !kthread_should_stop()) {
  11514. + schedule();
  11515. + set_current_state(TASK_UNINTERRUPTIBLE);
  11516. + }
  11517. +
  11518. + /* Make sure grab_lock is seen before we see a stale completion */
  11519. + smp_mb();
  11520. +
  11521. + /*
  11522. + * Now just before cpu_down() enters stop machine, we need to make
  11523. + * sure all tasks that are in pinned CPU sections are out, and new
  11524. + * tasks will now grab the lock, keeping them from entering pinned
  11525. + * CPU sections.
  11526. + */
  11527. + if (!kthread_should_stop()) {
  11528. + preempt_disable();
  11529. + wait_for_pinned_cpus(hp);
  11530. + preempt_enable();
  11531. + complete(&hp->synced);
  11532. + }
  11533. +
  11534. + set_current_state(TASK_UNINTERRUPTIBLE);
  11535. + while (!kthread_should_stop()) {
  11536. + schedule();
  11537. + set_current_state(TASK_UNINTERRUPTIBLE);
  11538. + }
  11539. + set_current_state(TASK_RUNNING);
  11540. +
  11541. + /*
  11542. + * Force this thread off this CPU as it's going down and
  11543. + * we don't want any more work on this CPU.
  11544. + */
  11545. + current->flags &= ~PF_NO_SETAFFINITY;
  11546. + set_cpus_allowed_ptr(current, cpu_present_mask);
  11547. + migrate_me();
  11548. + return 0;
  11549. +}
  11550. +
  11551. +static void __cpu_unplug_sync(struct hotplug_pcp *hp)
  11552. +{
  11553. + wake_up_process(hp->sync_tsk);
  11554. + wait_for_completion(&hp->synced);
  11555. +}
  11556. +
  11557. +static void __cpu_unplug_wait(unsigned int cpu)
  11558. +{
  11559. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  11560. +
  11561. + complete(&hp->unplug_wait);
  11562. + wait_for_completion(&hp->synced);
  11563. +}
  11564. +
  11565. +/*
  11566. + * Start the sync_unplug_thread on the target cpu and wait for it to
  11567. + * complete.
  11568. + */
  11569. +static int cpu_unplug_begin(unsigned int cpu)
  11570. +{
  11571. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  11572. + int err;
  11573. +
  11574. + /* Protected by cpu_hotplug.lock */
  11575. + if (!hp->mutex_init) {
  11576. +#ifdef CONFIG_PREEMPT_RT_FULL
  11577. + spin_lock_init(&hp->lock);
  11578. +#else
  11579. + mutex_init(&hp->mutex);
  11580. +#endif
  11581. + hp->mutex_init = 1;
  11582. + }
  11583. +
  11584. + /* Inform the scheduler to migrate tasks off this CPU */
  11585. + tell_sched_cpu_down_begin(cpu);
  11586. +
  11587. + init_completion(&hp->synced);
  11588. + init_completion(&hp->unplug_wait);
  11589. +
  11590. + hp->sync_tsk = kthread_create(sync_unplug_thread, hp, "sync_unplug/%d", cpu);
  11591. + if (IS_ERR(hp->sync_tsk)) {
  11592. + err = PTR_ERR(hp->sync_tsk);
  11593. + hp->sync_tsk = NULL;
  11594. + return err;
  11595. + }
  11596. + kthread_bind(hp->sync_tsk, cpu);
  11597. +
  11598. + /*
  11599. + * Wait for tasks to get out of the pinned sections,
  11600. + * it's still OK if new tasks enter. Some CPU notifiers will
  11601. + * wait for tasks that are going to enter these sections and
  11602. + * we must not have them block.
  11603. + */
  11604. + wake_up_process(hp->sync_tsk);
  11605. + return 0;
  11606. +}
  11607. +
  11608. +static void cpu_unplug_sync(unsigned int cpu)
  11609. +{
  11610. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  11611. +
  11612. + init_completion(&hp->synced);
  11613. + /* The completion needs to be initialzied before setting grab_lock */
  11614. + smp_wmb();
  11615. +
  11616. + /* Grab the mutex before setting grab_lock */
  11617. + hotplug_lock(hp);
  11618. + hp->grab_lock = 1;
  11619. +
  11620. + /*
  11621. + * The CPU notifiers have been completed.
  11622. + * Wait for tasks to get out of pinned CPU sections and have new
  11623. + * tasks block until the CPU is completely down.
  11624. + */
  11625. + __cpu_unplug_sync(hp);
  11626. +
  11627. + /* All done with the sync thread */
  11628. + kthread_stop(hp->sync_tsk);
  11629. + hp->sync_tsk = NULL;
  11630. +}
  11631. +
  11632. +static void cpu_unplug_done(unsigned int cpu)
  11633. +{
  11634. + struct hotplug_pcp *hp = &per_cpu(hotplug_pcp, cpu);
  11635. +
  11636. + hp->unplug = NULL;
  11637. + /* Let all tasks know cpu unplug is finished before cleaning up */
  11638. + smp_wmb();
  11639. +
  11640. + if (hp->sync_tsk)
  11641. + kthread_stop(hp->sync_tsk);
  11642. +
  11643. + if (hp->grab_lock) {
  11644. + hotplug_unlock(hp);
  11645. + /* protected by cpu_hotplug.lock */
  11646. + hp->grab_lock = 0;
  11647. + }
  11648. + tell_sched_cpu_down_done(cpu);
  11649. +}
  11650. void get_online_cpus(void)
  11651. {
  11652. @@ -710,10 +993,14 @@ static int takedown_cpu(unsigned int cpu)
  11653. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  11654. int err;
  11655. + __cpu_unplug_wait(cpu);
  11656. /* Park the smpboot threads */
  11657. kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
  11658. smpboot_park_threads(cpu);
  11659. + /* Notifiers are done. Don't let any more tasks pin this CPU. */
  11660. + cpu_unplug_sync(cpu);
  11661. +
  11662. /*
  11663. * Prevent irq alloc/free while the dying cpu reorganizes the
  11664. * interrupt affinities.
  11665. @@ -799,6 +1086,9 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  11666. struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
  11667. int prev_state, ret = 0;
  11668. bool hasdied = false;
  11669. + int mycpu;
  11670. + cpumask_var_t cpumask;
  11671. + cpumask_var_t cpumask_org;
  11672. if (num_online_cpus() == 1)
  11673. return -EBUSY;
  11674. @@ -806,7 +1096,34 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  11675. if (!cpu_present(cpu))
  11676. return -EINVAL;
  11677. + /* Move the downtaker off the unplug cpu */
  11678. + if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  11679. + return -ENOMEM;
  11680. + if (!alloc_cpumask_var(&cpumask_org, GFP_KERNEL)) {
  11681. + free_cpumask_var(cpumask);
  11682. + return -ENOMEM;
  11683. + }
  11684. +
  11685. + cpumask_copy(cpumask_org, tsk_cpus_allowed(current));
  11686. + cpumask_andnot(cpumask, cpu_online_mask, cpumask_of(cpu));
  11687. + set_cpus_allowed_ptr(current, cpumask);
  11688. + free_cpumask_var(cpumask);
  11689. + migrate_disable();
  11690. + mycpu = smp_processor_id();
  11691. + if (mycpu == cpu) {
  11692. + printk(KERN_ERR "Yuck! Still on unplug CPU\n!");
  11693. + migrate_enable();
  11694. + ret = -EBUSY;
  11695. + goto restore_cpus;
  11696. + }
  11697. +
  11698. + migrate_enable();
  11699. cpu_hotplug_begin();
  11700. + ret = cpu_unplug_begin(cpu);
  11701. + if (ret) {
  11702. + printk("cpu_unplug_begin(%d) failed\n", cpu);
  11703. + goto out_cancel;
  11704. + }
  11705. cpuhp_tasks_frozen = tasks_frozen;
  11706. @@ -845,10 +1162,15 @@ static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
  11707. hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
  11708. out:
  11709. + cpu_unplug_done(cpu);
  11710. +out_cancel:
  11711. cpu_hotplug_done();
  11712. /* This post dead nonsense must die */
  11713. if (!ret && hasdied)
  11714. cpu_notify_nofail(CPU_POST_DEAD, cpu);
  11715. +restore_cpus:
  11716. + set_cpus_allowed_ptr(current, cpumask_org);
  11717. + free_cpumask_var(cpumask_org);
  11718. return ret;
  11719. }
  11720. diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
  11721. index fc1ef736253c..83c666537a7a 100644
  11722. --- a/kernel/debug/kdb/kdb_io.c
  11723. +++ b/kernel/debug/kdb/kdb_io.c
  11724. @@ -554,7 +554,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  11725. int linecount;
  11726. int colcount;
  11727. int logging, saved_loglevel = 0;
  11728. - int saved_trap_printk;
  11729. int got_printf_lock = 0;
  11730. int retlen = 0;
  11731. int fnd, len;
  11732. @@ -565,8 +564,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  11733. unsigned long uninitialized_var(flags);
  11734. preempt_disable();
  11735. - saved_trap_printk = kdb_trap_printk;
  11736. - kdb_trap_printk = 0;
  11737. /* Serialize kdb_printf if multiple cpus try to write at once.
  11738. * But if any cpu goes recursive in kdb, just print the output,
  11739. @@ -855,7 +852,6 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  11740. } else {
  11741. __release(kdb_printf_lock);
  11742. }
  11743. - kdb_trap_printk = saved_trap_printk;
  11744. preempt_enable();
  11745. return retlen;
  11746. }
  11747. @@ -865,9 +861,11 @@ int kdb_printf(const char *fmt, ...)
  11748. va_list ap;
  11749. int r;
  11750. + kdb_trap_printk++;
  11751. va_start(ap, fmt);
  11752. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  11753. va_end(ap);
  11754. + kdb_trap_printk--;
  11755. return r;
  11756. }
  11757. diff --git a/kernel/events/core.c b/kernel/events/core.c
  11758. index fc9bb2225291..bc2db7e1ae04 100644
  11759. --- a/kernel/events/core.c
  11760. +++ b/kernel/events/core.c
  11761. @@ -1042,6 +1042,7 @@ static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  11762. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  11763. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  11764. timer->function = perf_mux_hrtimer_handler;
  11765. + timer->irqsafe = 1;
  11766. }
  11767. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  11768. @@ -8215,6 +8216,7 @@ static void perf_swevent_init_hrtimer(struct perf_event *event)
  11769. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  11770. hwc->hrtimer.function = perf_swevent_hrtimer;
  11771. + hwc->hrtimer.irqsafe = 1;
  11772. /*
  11773. * Since hrtimers have a fixed rate, we can do a static freq->period
  11774. diff --git a/kernel/exit.c b/kernel/exit.c
  11775. index 091a78be3b09..170b672bbb38 100644
  11776. --- a/kernel/exit.c
  11777. +++ b/kernel/exit.c
  11778. @@ -143,7 +143,7 @@ static void __exit_signal(struct task_struct *tsk)
  11779. * Do this under ->siglock, we can race with another thread
  11780. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  11781. */
  11782. - flush_sigqueue(&tsk->pending);
  11783. + flush_task_sigqueue(tsk);
  11784. tsk->sighand = NULL;
  11785. spin_unlock(&sighand->siglock);
  11786. diff --git a/kernel/fork.c b/kernel/fork.c
  11787. index beb31725f7e2..e398cb9e62fa 100644
  11788. --- a/kernel/fork.c
  11789. +++ b/kernel/fork.c
  11790. @@ -251,7 +251,9 @@ static inline void put_signal_struct(struct signal_struct *sig)
  11791. if (atomic_dec_and_test(&sig->sigcnt))
  11792. free_signal_struct(sig);
  11793. }
  11794. -
  11795. +#ifdef CONFIG_PREEMPT_RT_BASE
  11796. +static
  11797. +#endif
  11798. void __put_task_struct(struct task_struct *tsk)
  11799. {
  11800. WARN_ON(!tsk->exit_state);
  11801. @@ -268,7 +270,18 @@ void __put_task_struct(struct task_struct *tsk)
  11802. if (!profile_handoff_task(tsk))
  11803. free_task(tsk);
  11804. }
  11805. +#ifndef CONFIG_PREEMPT_RT_BASE
  11806. EXPORT_SYMBOL_GPL(__put_task_struct);
  11807. +#else
  11808. +void __put_task_struct_cb(struct rcu_head *rhp)
  11809. +{
  11810. + struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
  11811. +
  11812. + __put_task_struct(tsk);
  11813. +
  11814. +}
  11815. +EXPORT_SYMBOL_GPL(__put_task_struct_cb);
  11816. +#endif
  11817. void __init __weak arch_task_cache_init(void) { }
  11818. @@ -702,6 +715,19 @@ void __mmdrop(struct mm_struct *mm)
  11819. }
  11820. EXPORT_SYMBOL_GPL(__mmdrop);
  11821. +#ifdef CONFIG_PREEMPT_RT_BASE
  11822. +/*
  11823. + * RCU callback for delayed mm drop. Not strictly rcu, but we don't
  11824. + * want another facility to make this work.
  11825. + */
  11826. +void __mmdrop_delayed(struct rcu_head *rhp)
  11827. +{
  11828. + struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
  11829. +
  11830. + __mmdrop(mm);
  11831. +}
  11832. +#endif
  11833. +
  11834. static inline void __mmput(struct mm_struct *mm)
  11835. {
  11836. VM_BUG_ON(atomic_read(&mm->mm_users));
  11837. @@ -1274,6 +1300,9 @@ static void rt_mutex_init_task(struct task_struct *p)
  11838. */
  11839. static void posix_cpu_timers_init(struct task_struct *tsk)
  11840. {
  11841. +#ifdef CONFIG_PREEMPT_RT_BASE
  11842. + tsk->posix_timer_list = NULL;
  11843. +#endif
  11844. tsk->cputime_expires.prof_exp = 0;
  11845. tsk->cputime_expires.virt_exp = 0;
  11846. tsk->cputime_expires.sched_exp = 0;
  11847. @@ -1399,6 +1428,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
  11848. spin_lock_init(&p->alloc_lock);
  11849. init_sigpending(&p->pending);
  11850. + p->sigqueue_cache = NULL;
  11851. p->utime = p->stime = p->gtime = 0;
  11852. p->utimescaled = p->stimescaled = 0;
  11853. diff --git a/kernel/futex.c b/kernel/futex.c
  11854. index 46cb3a301bc1..6de82b959729 100644
  11855. --- a/kernel/futex.c
  11856. +++ b/kernel/futex.c
  11857. @@ -895,7 +895,9 @@ void exit_pi_state_list(struct task_struct *curr)
  11858. * task still owns the PI-state:
  11859. */
  11860. if (head->next != next) {
  11861. + raw_spin_unlock_irq(&curr->pi_lock);
  11862. spin_unlock(&hb->lock);
  11863. + raw_spin_lock_irq(&curr->pi_lock);
  11864. continue;
  11865. }
  11866. @@ -1290,6 +1292,7 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  11867. struct futex_pi_state *pi_state = this->pi_state;
  11868. u32 uninitialized_var(curval), newval;
  11869. WAKE_Q(wake_q);
  11870. + WAKE_Q(wake_sleeper_q);
  11871. bool deboost;
  11872. int ret = 0;
  11873. @@ -1356,7 +1359,8 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  11874. raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock);
  11875. - deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q);
  11876. + deboost = rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q,
  11877. + &wake_sleeper_q);
  11878. /*
  11879. * First unlock HB so the waiter does not spin on it once he got woken
  11880. @@ -1364,8 +1368,9 @@ static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this,
  11881. * deboost first (and lose our higher priority), then the task might get
  11882. * scheduled away before the wake up can take place.
  11883. */
  11884. - spin_unlock(&hb->lock);
  11885. + deboost |= spin_unlock_no_deboost(&hb->lock);
  11886. wake_up_q(&wake_q);
  11887. + wake_up_q_sleeper(&wake_sleeper_q);
  11888. if (deboost)
  11889. rt_mutex_adjust_prio(current);
  11890. @@ -1915,6 +1920,16 @@ static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
  11891. requeue_pi_wake_futex(this, &key2, hb2);
  11892. drop_count++;
  11893. continue;
  11894. + } else if (ret == -EAGAIN) {
  11895. + /*
  11896. + * Waiter was woken by timeout or
  11897. + * signal and has set pi_blocked_on to
  11898. + * PI_WAKEUP_INPROGRESS before we
  11899. + * tried to enqueue it on the rtmutex.
  11900. + */
  11901. + this->pi_state = NULL;
  11902. + put_pi_state(pi_state);
  11903. + continue;
  11904. } else if (ret) {
  11905. /*
  11906. * rt_mutex_start_proxy_lock() detected a
  11907. @@ -2805,7 +2820,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  11908. struct hrtimer_sleeper timeout, *to = NULL;
  11909. struct rt_mutex_waiter rt_waiter;
  11910. struct rt_mutex *pi_mutex = NULL;
  11911. - struct futex_hash_bucket *hb;
  11912. + struct futex_hash_bucket *hb, *hb2;
  11913. union futex_key key2 = FUTEX_KEY_INIT;
  11914. struct futex_q q = futex_q_init;
  11915. int res, ret;
  11916. @@ -2830,10 +2845,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  11917. * The waiter is allocated on our stack, manipulated by the requeue
  11918. * code while we sleep on uaddr.
  11919. */
  11920. - debug_rt_mutex_init_waiter(&rt_waiter);
  11921. - RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  11922. - RB_CLEAR_NODE(&rt_waiter.tree_entry);
  11923. - rt_waiter.task = NULL;
  11924. + rt_mutex_init_waiter(&rt_waiter, false);
  11925. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  11926. if (unlikely(ret != 0))
  11927. @@ -2864,20 +2876,55 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  11928. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  11929. futex_wait_queue_me(hb, &q, to);
  11930. - spin_lock(&hb->lock);
  11931. - ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  11932. - spin_unlock(&hb->lock);
  11933. - if (ret)
  11934. - goto out_put_keys;
  11935. + /*
  11936. + * On RT we must avoid races with requeue and trying to block
  11937. + * on two mutexes (hb->lock and uaddr2's rtmutex) by
  11938. + * serializing access to pi_blocked_on with pi_lock.
  11939. + */
  11940. + raw_spin_lock_irq(&current->pi_lock);
  11941. + if (current->pi_blocked_on) {
  11942. + /*
  11943. + * We have been requeued or are in the process of
  11944. + * being requeued.
  11945. + */
  11946. + raw_spin_unlock_irq(&current->pi_lock);
  11947. + } else {
  11948. + /*
  11949. + * Setting pi_blocked_on to PI_WAKEUP_INPROGRESS
  11950. + * prevents a concurrent requeue from moving us to the
  11951. + * uaddr2 rtmutex. After that we can safely acquire
  11952. + * (and possibly block on) hb->lock.
  11953. + */
  11954. + current->pi_blocked_on = PI_WAKEUP_INPROGRESS;
  11955. + raw_spin_unlock_irq(&current->pi_lock);
  11956. +
  11957. + spin_lock(&hb->lock);
  11958. +
  11959. + /*
  11960. + * Clean up pi_blocked_on. We might leak it otherwise
  11961. + * when we succeeded with the hb->lock in the fast
  11962. + * path.
  11963. + */
  11964. + raw_spin_lock_irq(&current->pi_lock);
  11965. + current->pi_blocked_on = NULL;
  11966. + raw_spin_unlock_irq(&current->pi_lock);
  11967. +
  11968. + ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  11969. + spin_unlock(&hb->lock);
  11970. + if (ret)
  11971. + goto out_put_keys;
  11972. + }
  11973. /*
  11974. - * In order for us to be here, we know our q.key == key2, and since
  11975. - * we took the hb->lock above, we also know that futex_requeue() has
  11976. - * completed and we no longer have to concern ourselves with a wakeup
  11977. - * race with the atomic proxy lock acquisition by the requeue code. The
  11978. - * futex_requeue dropped our key1 reference and incremented our key2
  11979. - * reference count.
  11980. + * In order to be here, we have either been requeued, are in
  11981. + * the process of being requeued, or requeue successfully
  11982. + * acquired uaddr2 on our behalf. If pi_blocked_on was
  11983. + * non-null above, we may be racing with a requeue. Do not
  11984. + * rely on q->lock_ptr to be hb2->lock until after blocking on
  11985. + * hb->lock or hb2->lock. The futex_requeue dropped our key1
  11986. + * reference and incremented our key2 reference count.
  11987. */
  11988. + hb2 = hash_futex(&key2);
  11989. /* Check if the requeue code acquired the second futex for us. */
  11990. if (!q.rt_waiter) {
  11991. @@ -2886,14 +2933,15 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  11992. * did a lock-steal - fix up the PI-state in that case.
  11993. */
  11994. if (q.pi_state && (q.pi_state->owner != current)) {
  11995. - spin_lock(q.lock_ptr);
  11996. + spin_lock(&hb2->lock);
  11997. + BUG_ON(&hb2->lock != q.lock_ptr);
  11998. ret = fixup_pi_state_owner(uaddr2, &q, current);
  11999. /*
  12000. * Drop the reference to the pi state which
  12001. * the requeue_pi() code acquired for us.
  12002. */
  12003. put_pi_state(q.pi_state);
  12004. - spin_unlock(q.lock_ptr);
  12005. + spin_unlock(&hb2->lock);
  12006. }
  12007. } else {
  12008. /*
  12009. @@ -2906,7 +2954,8 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  12010. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  12011. debug_rt_mutex_free_waiter(&rt_waiter);
  12012. - spin_lock(q.lock_ptr);
  12013. + spin_lock(&hb2->lock);
  12014. + BUG_ON(&hb2->lock != q.lock_ptr);
  12015. /*
  12016. * Fixup the pi_state owner and possibly acquire the lock if we
  12017. * haven't already.
  12018. diff --git a/kernel/irq/handle.c b/kernel/irq/handle.c
  12019. index d3f24905852c..f87aa8fdcc51 100644
  12020. --- a/kernel/irq/handle.c
  12021. +++ b/kernel/irq/handle.c
  12022. @@ -181,10 +181,16 @@ irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
  12023. {
  12024. irqreturn_t retval;
  12025. unsigned int flags = 0;
  12026. + struct pt_regs *regs = get_irq_regs();
  12027. + u64 ip = regs ? instruction_pointer(regs) : 0;
  12028. retval = __handle_irq_event_percpu(desc, &flags);
  12029. - add_interrupt_randomness(desc->irq_data.irq, flags);
  12030. +#ifdef CONFIG_PREEMPT_RT_FULL
  12031. + desc->random_ip = ip;
  12032. +#else
  12033. + add_interrupt_randomness(desc->irq_data.irq, flags, ip);
  12034. +#endif
  12035. if (!noirqdebug)
  12036. note_interrupt(desc, retval);
  12037. diff --git a/kernel/irq/manage.c b/kernel/irq/manage.c
  12038. index 9530fcd27704..fadf8f848299 100644
  12039. --- a/kernel/irq/manage.c
  12040. +++ b/kernel/irq/manage.c
  12041. @@ -22,6 +22,7 @@
  12042. #include "internals.h"
  12043. #ifdef CONFIG_IRQ_FORCED_THREADING
  12044. +# ifndef CONFIG_PREEMPT_RT_BASE
  12045. __read_mostly bool force_irqthreads;
  12046. static int __init setup_forced_irqthreads(char *arg)
  12047. @@ -30,6 +31,7 @@ static int __init setup_forced_irqthreads(char *arg)
  12048. return 0;
  12049. }
  12050. early_param("threadirqs", setup_forced_irqthreads);
  12051. +# endif
  12052. #endif
  12053. static void __synchronize_hardirq(struct irq_desc *desc)
  12054. @@ -233,7 +235,12 @@ int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
  12055. if (desc->affinity_notify) {
  12056. kref_get(&desc->affinity_notify->kref);
  12057. +
  12058. +#ifdef CONFIG_PREEMPT_RT_BASE
  12059. + swork_queue(&desc->affinity_notify->swork);
  12060. +#else
  12061. schedule_work(&desc->affinity_notify->work);
  12062. +#endif
  12063. }
  12064. irqd_set(data, IRQD_AFFINITY_SET);
  12065. @@ -271,10 +278,8 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  12066. }
  12067. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  12068. -static void irq_affinity_notify(struct work_struct *work)
  12069. +static void _irq_affinity_notify(struct irq_affinity_notify *notify)
  12070. {
  12071. - struct irq_affinity_notify *notify =
  12072. - container_of(work, struct irq_affinity_notify, work);
  12073. struct irq_desc *desc = irq_to_desc(notify->irq);
  12074. cpumask_var_t cpumask;
  12075. unsigned long flags;
  12076. @@ -296,6 +301,35 @@ static void irq_affinity_notify(struct work_struct *work)
  12077. kref_put(&notify->kref, notify->release);
  12078. }
  12079. +#ifdef CONFIG_PREEMPT_RT_BASE
  12080. +static void init_helper_thread(void)
  12081. +{
  12082. + static int init_sworker_once;
  12083. +
  12084. + if (init_sworker_once)
  12085. + return;
  12086. + if (WARN_ON(swork_get()))
  12087. + return;
  12088. + init_sworker_once = 1;
  12089. +}
  12090. +
  12091. +static void irq_affinity_notify(struct swork_event *swork)
  12092. +{
  12093. + struct irq_affinity_notify *notify =
  12094. + container_of(swork, struct irq_affinity_notify, swork);
  12095. + _irq_affinity_notify(notify);
  12096. +}
  12097. +
  12098. +#else
  12099. +
  12100. +static void irq_affinity_notify(struct work_struct *work)
  12101. +{
  12102. + struct irq_affinity_notify *notify =
  12103. + container_of(work, struct irq_affinity_notify, work);
  12104. + _irq_affinity_notify(notify);
  12105. +}
  12106. +#endif
  12107. +
  12108. /**
  12109. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  12110. * @irq: Interrupt for which to enable/disable notification
  12111. @@ -324,7 +358,12 @@ irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  12112. if (notify) {
  12113. notify->irq = irq;
  12114. kref_init(&notify->kref);
  12115. +#ifdef CONFIG_PREEMPT_RT_BASE
  12116. + INIT_SWORK(&notify->swork, irq_affinity_notify);
  12117. + init_helper_thread();
  12118. +#else
  12119. INIT_WORK(&notify->work, irq_affinity_notify);
  12120. +#endif
  12121. }
  12122. raw_spin_lock_irqsave(&desc->lock, flags);
  12123. @@ -879,7 +918,15 @@ irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  12124. local_bh_disable();
  12125. ret = action->thread_fn(action->irq, action->dev_id);
  12126. irq_finalize_oneshot(desc, action);
  12127. - local_bh_enable();
  12128. + /*
  12129. + * Interrupts which have real time requirements can be set up
  12130. + * to avoid softirq processing in the thread handler. This is
  12131. + * safe as these interrupts do not raise soft interrupts.
  12132. + */
  12133. + if (irq_settings_no_softirq_call(desc))
  12134. + _local_bh_enable();
  12135. + else
  12136. + local_bh_enable();
  12137. return ret;
  12138. }
  12139. @@ -976,6 +1023,12 @@ static int irq_thread(void *data)
  12140. if (action_ret == IRQ_WAKE_THREAD)
  12141. irq_wake_secondary(desc, action);
  12142. +#ifdef CONFIG_PREEMPT_RT_FULL
  12143. + migrate_disable();
  12144. + add_interrupt_randomness(action->irq, 0,
  12145. + desc->random_ip ^ (unsigned long) action);
  12146. + migrate_enable();
  12147. +#endif
  12148. wake_threads_waitq(desc);
  12149. }
  12150. @@ -1336,6 +1389,9 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  12151. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  12152. }
  12153. + if (new->flags & IRQF_NO_SOFTIRQ_CALL)
  12154. + irq_settings_set_no_softirq_call(desc);
  12155. +
  12156. /* Set default affinity mask once everything is setup */
  12157. setup_affinity(desc, mask);
  12158. @@ -2061,7 +2117,7 @@ EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
  12159. * This call sets the internal irqchip state of an interrupt,
  12160. * depending on the value of @which.
  12161. *
  12162. - * This function should be called with preemption disabled if the
  12163. + * This function should be called with migration disabled if the
  12164. * interrupt controller has per-cpu registers.
  12165. */
  12166. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  12167. diff --git a/kernel/irq/settings.h b/kernel/irq/settings.h
  12168. index 320579d89091..2df2d4445b1e 100644
  12169. --- a/kernel/irq/settings.h
  12170. +++ b/kernel/irq/settings.h
  12171. @@ -16,6 +16,7 @@ enum {
  12172. _IRQ_PER_CPU_DEVID = IRQ_PER_CPU_DEVID,
  12173. _IRQ_IS_POLLED = IRQ_IS_POLLED,
  12174. _IRQ_DISABLE_UNLAZY = IRQ_DISABLE_UNLAZY,
  12175. + _IRQ_NO_SOFTIRQ_CALL = IRQ_NO_SOFTIRQ_CALL,
  12176. _IRQF_MODIFY_MASK = IRQF_MODIFY_MASK,
  12177. };
  12178. @@ -30,6 +31,7 @@ enum {
  12179. #define IRQ_PER_CPU_DEVID GOT_YOU_MORON
  12180. #define IRQ_IS_POLLED GOT_YOU_MORON
  12181. #define IRQ_DISABLE_UNLAZY GOT_YOU_MORON
  12182. +#define IRQ_NO_SOFTIRQ_CALL GOT_YOU_MORON
  12183. #undef IRQF_MODIFY_MASK
  12184. #define IRQF_MODIFY_MASK GOT_YOU_MORON
  12185. @@ -40,6 +42,16 @@ irq_settings_clr_and_set(struct irq_desc *desc, u32 clr, u32 set)
  12186. desc->status_use_accessors |= (set & _IRQF_MODIFY_MASK);
  12187. }
  12188. +static inline bool irq_settings_no_softirq_call(struct irq_desc *desc)
  12189. +{
  12190. + return desc->status_use_accessors & _IRQ_NO_SOFTIRQ_CALL;
  12191. +}
  12192. +
  12193. +static inline void irq_settings_set_no_softirq_call(struct irq_desc *desc)
  12194. +{
  12195. + desc->status_use_accessors |= _IRQ_NO_SOFTIRQ_CALL;
  12196. +}
  12197. +
  12198. static inline bool irq_settings_is_per_cpu(struct irq_desc *desc)
  12199. {
  12200. return desc->status_use_accessors & _IRQ_PER_CPU;
  12201. diff --git a/kernel/irq/spurious.c b/kernel/irq/spurious.c
  12202. index 5707f97a3e6a..73f38dc7a7fb 100644
  12203. --- a/kernel/irq/spurious.c
  12204. +++ b/kernel/irq/spurious.c
  12205. @@ -442,6 +442,10 @@ MODULE_PARM_DESC(noirqdebug, "Disable irq lockup detection when true");
  12206. static int __init irqfixup_setup(char *str)
  12207. {
  12208. +#ifdef CONFIG_PREEMPT_RT_BASE
  12209. + pr_warn("irqfixup boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  12210. + return 1;
  12211. +#endif
  12212. irqfixup = 1;
  12213. printk(KERN_WARNING "Misrouted IRQ fixup support enabled.\n");
  12214. printk(KERN_WARNING "This may impact system performance.\n");
  12215. @@ -454,6 +458,10 @@ module_param(irqfixup, int, 0644);
  12216. static int __init irqpoll_setup(char *str)
  12217. {
  12218. +#ifdef CONFIG_PREEMPT_RT_BASE
  12219. + pr_warn("irqpoll boot option not supported w/ CONFIG_PREEMPT_RT_BASE\n");
  12220. + return 1;
  12221. +#endif
  12222. irqfixup = 2;
  12223. printk(KERN_WARNING "Misrouted IRQ fixup and polling support "
  12224. "enabled\n");
  12225. diff --git a/kernel/irq_work.c b/kernel/irq_work.c
  12226. index bcf107ce0854..2899ba0d23d1 100644
  12227. --- a/kernel/irq_work.c
  12228. +++ b/kernel/irq_work.c
  12229. @@ -17,6 +17,7 @@
  12230. #include <linux/cpu.h>
  12231. #include <linux/notifier.h>
  12232. #include <linux/smp.h>
  12233. +#include <linux/interrupt.h>
  12234. #include <asm/processor.h>
  12235. @@ -65,6 +66,8 @@ void __weak arch_irq_work_raise(void)
  12236. */
  12237. bool irq_work_queue_on(struct irq_work *work, int cpu)
  12238. {
  12239. + struct llist_head *list;
  12240. +
  12241. /* All work should have been flushed before going offline */
  12242. WARN_ON_ONCE(cpu_is_offline(cpu));
  12243. @@ -75,7 +78,12 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
  12244. if (!irq_work_claim(work))
  12245. return false;
  12246. - if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
  12247. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
  12248. + list = &per_cpu(lazy_list, cpu);
  12249. + else
  12250. + list = &per_cpu(raised_list, cpu);
  12251. +
  12252. + if (llist_add(&work->llnode, list))
  12253. arch_send_call_function_single_ipi(cpu);
  12254. return true;
  12255. @@ -86,6 +94,9 @@ EXPORT_SYMBOL_GPL(irq_work_queue_on);
  12256. /* Enqueue the irq work @work on the current CPU */
  12257. bool irq_work_queue(struct irq_work *work)
  12258. {
  12259. + struct llist_head *list;
  12260. + bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
  12261. +
  12262. /* Only queue if not already pending */
  12263. if (!irq_work_claim(work))
  12264. return false;
  12265. @@ -93,13 +104,15 @@ bool irq_work_queue(struct irq_work *work)
  12266. /* Queue the entry and raise the IPI if needed. */
  12267. preempt_disable();
  12268. - /* If the work is "lazy", handle it from next tick if any */
  12269. - if (work->flags & IRQ_WORK_LAZY) {
  12270. - if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
  12271. - tick_nohz_tick_stopped())
  12272. - arch_irq_work_raise();
  12273. - } else {
  12274. - if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
  12275. + lazy_work = work->flags & IRQ_WORK_LAZY;
  12276. +
  12277. + if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
  12278. + list = this_cpu_ptr(&lazy_list);
  12279. + else
  12280. + list = this_cpu_ptr(&raised_list);
  12281. +
  12282. + if (llist_add(&work->llnode, list)) {
  12283. + if (!lazy_work || tick_nohz_tick_stopped())
  12284. arch_irq_work_raise();
  12285. }
  12286. @@ -116,9 +129,8 @@ bool irq_work_needs_cpu(void)
  12287. raised = this_cpu_ptr(&raised_list);
  12288. lazy = this_cpu_ptr(&lazy_list);
  12289. - if (llist_empty(raised) || arch_irq_work_has_interrupt())
  12290. - if (llist_empty(lazy))
  12291. - return false;
  12292. + if (llist_empty(raised) && llist_empty(lazy))
  12293. + return false;
  12294. /* All work should have been flushed before going offline */
  12295. WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
  12296. @@ -132,7 +144,7 @@ static void irq_work_run_list(struct llist_head *list)
  12297. struct irq_work *work;
  12298. struct llist_node *llnode;
  12299. - BUG_ON(!irqs_disabled());
  12300. + BUG_ON_NONRT(!irqs_disabled());
  12301. if (llist_empty(list))
  12302. return;
  12303. @@ -169,7 +181,16 @@ static void irq_work_run_list(struct llist_head *list)
  12304. void irq_work_run(void)
  12305. {
  12306. irq_work_run_list(this_cpu_ptr(&raised_list));
  12307. - irq_work_run_list(this_cpu_ptr(&lazy_list));
  12308. + if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
  12309. + /*
  12310. + * NOTE: we raise softirq via IPI for safety,
  12311. + * and execute in irq_work_tick() to move the
  12312. + * overhead from hard to soft irq context.
  12313. + */
  12314. + if (!llist_empty(this_cpu_ptr(&lazy_list)))
  12315. + raise_softirq(TIMER_SOFTIRQ);
  12316. + } else
  12317. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  12318. }
  12319. EXPORT_SYMBOL_GPL(irq_work_run);
  12320. @@ -179,8 +200,17 @@ void irq_work_tick(void)
  12321. if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
  12322. irq_work_run_list(raised);
  12323. +
  12324. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  12325. + irq_work_run_list(this_cpu_ptr(&lazy_list));
  12326. +}
  12327. +
  12328. +#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_PREEMPT_RT_FULL)
  12329. +void irq_work_tick_soft(void)
  12330. +{
  12331. irq_work_run_list(this_cpu_ptr(&lazy_list));
  12332. }
  12333. +#endif
  12334. /*
  12335. * Synchronize against the irq_work @entry, ensures the entry is not
  12336. diff --git a/kernel/ksysfs.c b/kernel/ksysfs.c
  12337. index ee1bc1bb8feb..ddef07958840 100644
  12338. --- a/kernel/ksysfs.c
  12339. +++ b/kernel/ksysfs.c
  12340. @@ -136,6 +136,15 @@ KERNEL_ATTR_RO(vmcoreinfo);
  12341. #endif /* CONFIG_KEXEC_CORE */
  12342. +#if defined(CONFIG_PREEMPT_RT_FULL)
  12343. +static ssize_t realtime_show(struct kobject *kobj,
  12344. + struct kobj_attribute *attr, char *buf)
  12345. +{
  12346. + return sprintf(buf, "%d\n", 1);
  12347. +}
  12348. +KERNEL_ATTR_RO(realtime);
  12349. +#endif
  12350. +
  12351. /* whether file capabilities are enabled */
  12352. static ssize_t fscaps_show(struct kobject *kobj,
  12353. struct kobj_attribute *attr, char *buf)
  12354. @@ -225,6 +234,9 @@ static struct attribute * kernel_attrs[] = {
  12355. &rcu_expedited_attr.attr,
  12356. &rcu_normal_attr.attr,
  12357. #endif
  12358. +#ifdef CONFIG_PREEMPT_RT_FULL
  12359. + &realtime_attr.attr,
  12360. +#endif
  12361. NULL
  12362. };
  12363. diff --git a/kernel/locking/Makefile b/kernel/locking/Makefile
  12364. index 31322a4275cd..c6bba9299d8b 100644
  12365. --- a/kernel/locking/Makefile
  12366. +++ b/kernel/locking/Makefile
  12367. @@ -2,7 +2,7 @@
  12368. # and is generally not a function of system call inputs.
  12369. KCOV_INSTRUMENT := n
  12370. -obj-y += mutex.o semaphore.o rwsem.o percpu-rwsem.o
  12371. +obj-y += semaphore.o percpu-rwsem.o
  12372. ifdef CONFIG_FUNCTION_TRACER
  12373. CFLAGS_REMOVE_lockdep.o = $(CC_FLAGS_FTRACE)
  12374. @@ -11,7 +11,11 @@ CFLAGS_REMOVE_mutex-debug.o = $(CC_FLAGS_FTRACE)
  12375. CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
  12376. endif
  12377. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  12378. +obj-y += mutex.o
  12379. obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
  12380. +obj-y += rwsem.o
  12381. +endif
  12382. obj-$(CONFIG_LOCKDEP) += lockdep.o
  12383. ifeq ($(CONFIG_PROC_FS),y)
  12384. obj-$(CONFIG_LOCKDEP) += lockdep_proc.o
  12385. @@ -25,7 +29,10 @@ obj-$(CONFIG_RT_MUTEXES) += rtmutex.o
  12386. obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o
  12387. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock.o
  12388. obj-$(CONFIG_DEBUG_SPINLOCK) += spinlock_debug.o
  12389. +ifneq ($(CONFIG_PREEMPT_RT_FULL),y)
  12390. obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
  12391. obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem-xadd.o
  12392. +endif
  12393. +obj-$(CONFIG_PREEMPT_RT_FULL) += rt.o
  12394. obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o
  12395. obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o
  12396. diff --git a/kernel/locking/lglock.c b/kernel/locking/lglock.c
  12397. index 951cfcd10b4a..57e0ea72c28a 100644
  12398. --- a/kernel/locking/lglock.c
  12399. +++ b/kernel/locking/lglock.c
  12400. @@ -4,6 +4,15 @@
  12401. #include <linux/cpu.h>
  12402. #include <linux/string.h>
  12403. +#ifndef CONFIG_PREEMPT_RT_FULL
  12404. +# define lg_lock_ptr arch_spinlock_t
  12405. +# define lg_do_lock(l) arch_spin_lock(l)
  12406. +# define lg_do_unlock(l) arch_spin_unlock(l)
  12407. +#else
  12408. +# define lg_lock_ptr struct rt_mutex
  12409. +# define lg_do_lock(l) __rt_spin_lock__no_mg(l)
  12410. +# define lg_do_unlock(l) __rt_spin_unlock(l)
  12411. +#endif
  12412. /*
  12413. * Note there is no uninit, so lglocks cannot be defined in
  12414. * modules (but it's fine to use them from there)
  12415. @@ -12,51 +21,60 @@
  12416. void lg_lock_init(struct lglock *lg, char *name)
  12417. {
  12418. +#ifdef CONFIG_PREEMPT_RT_FULL
  12419. + int i;
  12420. +
  12421. + for_each_possible_cpu(i) {
  12422. + struct rt_mutex *lock = per_cpu_ptr(lg->lock, i);
  12423. +
  12424. + rt_mutex_init(lock);
  12425. + }
  12426. +#endif
  12427. LOCKDEP_INIT_MAP(&lg->lock_dep_map, name, &lg->lock_key, 0);
  12428. }
  12429. EXPORT_SYMBOL(lg_lock_init);
  12430. void lg_local_lock(struct lglock *lg)
  12431. {
  12432. - arch_spinlock_t *lock;
  12433. + lg_lock_ptr *lock;
  12434. - preempt_disable();
  12435. + migrate_disable();
  12436. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  12437. lock = this_cpu_ptr(lg->lock);
  12438. - arch_spin_lock(lock);
  12439. + lg_do_lock(lock);
  12440. }
  12441. EXPORT_SYMBOL(lg_local_lock);
  12442. void lg_local_unlock(struct lglock *lg)
  12443. {
  12444. - arch_spinlock_t *lock;
  12445. + lg_lock_ptr *lock;
  12446. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  12447. lock = this_cpu_ptr(lg->lock);
  12448. - arch_spin_unlock(lock);
  12449. - preempt_enable();
  12450. + lg_do_unlock(lock);
  12451. + migrate_enable();
  12452. }
  12453. EXPORT_SYMBOL(lg_local_unlock);
  12454. void lg_local_lock_cpu(struct lglock *lg, int cpu)
  12455. {
  12456. - arch_spinlock_t *lock;
  12457. + lg_lock_ptr *lock;
  12458. - preempt_disable();
  12459. + preempt_disable_nort();
  12460. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  12461. lock = per_cpu_ptr(lg->lock, cpu);
  12462. - arch_spin_lock(lock);
  12463. + lg_do_lock(lock);
  12464. }
  12465. EXPORT_SYMBOL(lg_local_lock_cpu);
  12466. void lg_local_unlock_cpu(struct lglock *lg, int cpu)
  12467. {
  12468. - arch_spinlock_t *lock;
  12469. + lg_lock_ptr *lock;
  12470. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  12471. lock = per_cpu_ptr(lg->lock, cpu);
  12472. - arch_spin_unlock(lock);
  12473. - preempt_enable();
  12474. + lg_do_unlock(lock);
  12475. + preempt_enable_nort();
  12476. }
  12477. EXPORT_SYMBOL(lg_local_unlock_cpu);
  12478. @@ -68,30 +86,30 @@ void lg_double_lock(struct lglock *lg, int cpu1, int cpu2)
  12479. if (cpu2 < cpu1)
  12480. swap(cpu1, cpu2);
  12481. - preempt_disable();
  12482. + preempt_disable_nort();
  12483. lock_acquire_shared(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  12484. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu1));
  12485. - arch_spin_lock(per_cpu_ptr(lg->lock, cpu2));
  12486. + lg_do_lock(per_cpu_ptr(lg->lock, cpu1));
  12487. + lg_do_lock(per_cpu_ptr(lg->lock, cpu2));
  12488. }
  12489. void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2)
  12490. {
  12491. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  12492. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu1));
  12493. - arch_spin_unlock(per_cpu_ptr(lg->lock, cpu2));
  12494. - preempt_enable();
  12495. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu1));
  12496. + lg_do_unlock(per_cpu_ptr(lg->lock, cpu2));
  12497. + preempt_enable_nort();
  12498. }
  12499. void lg_global_lock(struct lglock *lg)
  12500. {
  12501. int i;
  12502. - preempt_disable();
  12503. + preempt_disable_nort();
  12504. lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  12505. for_each_possible_cpu(i) {
  12506. - arch_spinlock_t *lock;
  12507. + lg_lock_ptr *lock;
  12508. lock = per_cpu_ptr(lg->lock, i);
  12509. - arch_spin_lock(lock);
  12510. + lg_do_lock(lock);
  12511. }
  12512. }
  12513. EXPORT_SYMBOL(lg_global_lock);
  12514. @@ -102,10 +120,35 @@ void lg_global_unlock(struct lglock *lg)
  12515. lock_release(&lg->lock_dep_map, 1, _RET_IP_);
  12516. for_each_possible_cpu(i) {
  12517. - arch_spinlock_t *lock;
  12518. + lg_lock_ptr *lock;
  12519. lock = per_cpu_ptr(lg->lock, i);
  12520. - arch_spin_unlock(lock);
  12521. + lg_do_unlock(lock);
  12522. }
  12523. - preempt_enable();
  12524. + preempt_enable_nort();
  12525. }
  12526. EXPORT_SYMBOL(lg_global_unlock);
  12527. +
  12528. +#ifdef CONFIG_PREEMPT_RT_FULL
  12529. +/*
  12530. + * HACK: If you use this, you get to keep the pieces.
  12531. + * Used in queue_stop_cpus_work() when stop machinery
  12532. + * is called from inactive CPU, so we can't schedule.
  12533. + */
  12534. +# define lg_do_trylock_relax(l) \
  12535. + do { \
  12536. + while (!__rt_spin_trylock(l)) \
  12537. + cpu_relax(); \
  12538. + } while (0)
  12539. +
  12540. +void lg_global_trylock_relax(struct lglock *lg)
  12541. +{
  12542. + int i;
  12543. +
  12544. + lock_acquire_exclusive(&lg->lock_dep_map, 0, 0, NULL, _RET_IP_);
  12545. + for_each_possible_cpu(i) {
  12546. + lg_lock_ptr *lock;
  12547. + lock = per_cpu_ptr(lg->lock, i);
  12548. + lg_do_trylock_relax(lock);
  12549. + }
  12550. +}
  12551. +#endif
  12552. diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
  12553. index 589d763a49b3..4b48c4bfb60c 100644
  12554. --- a/kernel/locking/lockdep.c
  12555. +++ b/kernel/locking/lockdep.c
  12556. @@ -3686,6 +3686,7 @@ static void check_flags(unsigned long flags)
  12557. }
  12558. }
  12559. +#ifndef CONFIG_PREEMPT_RT_FULL
  12560. /*
  12561. * We dont accurately track softirq state in e.g.
  12562. * hardirq contexts (such as on 4KSTACKS), so only
  12563. @@ -3700,6 +3701,7 @@ static void check_flags(unsigned long flags)
  12564. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  12565. }
  12566. }
  12567. +#endif
  12568. if (!debug_locks)
  12569. print_irqtrace_events(current);
  12570. diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
  12571. index f8c5af52a131..788068773e61 100644
  12572. --- a/kernel/locking/locktorture.c
  12573. +++ b/kernel/locking/locktorture.c
  12574. @@ -26,7 +26,6 @@
  12575. #include <linux/kthread.h>
  12576. #include <linux/sched/rt.h>
  12577. #include <linux/spinlock.h>
  12578. -#include <linux/rwlock.h>
  12579. #include <linux/mutex.h>
  12580. #include <linux/rwsem.h>
  12581. #include <linux/smp.h>
  12582. diff --git a/kernel/locking/rt.c b/kernel/locking/rt.c
  12583. new file mode 100644
  12584. index 000000000000..665754c00e1e
  12585. --- /dev/null
  12586. +++ b/kernel/locking/rt.c
  12587. @@ -0,0 +1,498 @@
  12588. +/*
  12589. + * kernel/rt.c
  12590. + *
  12591. + * Real-Time Preemption Support
  12592. + *
  12593. + * started by Ingo Molnar:
  12594. + *
  12595. + * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  12596. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  12597. + *
  12598. + * historic credit for proving that Linux spinlocks can be implemented via
  12599. + * RT-aware mutexes goes to many people: The Pmutex project (Dirk Grambow
  12600. + * and others) who prototyped it on 2.4 and did lots of comparative
  12601. + * research and analysis; TimeSys, for proving that you can implement a
  12602. + * fully preemptible kernel via the use of IRQ threading and mutexes;
  12603. + * Bill Huey for persuasively arguing on lkml that the mutex model is the
  12604. + * right one; and to MontaVista, who ported pmutexes to 2.6.
  12605. + *
  12606. + * This code is a from-scratch implementation and is not based on pmutexes,
  12607. + * but the idea of converting spinlocks to mutexes is used here too.
  12608. + *
  12609. + * lock debugging, locking tree, deadlock detection:
  12610. + *
  12611. + * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  12612. + * Released under the General Public License (GPL).
  12613. + *
  12614. + * Includes portions of the generic R/W semaphore implementation from:
  12615. + *
  12616. + * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  12617. + * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  12618. + * - Derived also from comments by Linus
  12619. + *
  12620. + * Pending ownership of locks and ownership stealing:
  12621. + *
  12622. + * Copyright (C) 2005, Kihon Technologies Inc., Steven Rostedt
  12623. + *
  12624. + * (also by Steven Rostedt)
  12625. + * - Converted single pi_lock to individual task locks.
  12626. + *
  12627. + * By Esben Nielsen:
  12628. + * Doing priority inheritance with help of the scheduler.
  12629. + *
  12630. + * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  12631. + * - major rework based on Esben Nielsens initial patch
  12632. + * - replaced thread_info references by task_struct refs
  12633. + * - removed task->pending_owner dependency
  12634. + * - BKL drop/reacquire for semaphore style locks to avoid deadlocks
  12635. + * in the scheduler return path as discussed with Steven Rostedt
  12636. + *
  12637. + * Copyright (C) 2006, Kihon Technologies Inc.
  12638. + * Steven Rostedt <rostedt@goodmis.org>
  12639. + * - debugged and patched Thomas Gleixner's rework.
  12640. + * - added back the cmpxchg to the rework.
  12641. + * - turned atomic require back on for SMP.
  12642. + */
  12643. +
  12644. +#include <linux/spinlock.h>
  12645. +#include <linux/rtmutex.h>
  12646. +#include <linux/sched.h>
  12647. +#include <linux/delay.h>
  12648. +#include <linux/module.h>
  12649. +#include <linux/kallsyms.h>
  12650. +#include <linux/syscalls.h>
  12651. +#include <linux/interrupt.h>
  12652. +#include <linux/plist.h>
  12653. +#include <linux/fs.h>
  12654. +#include <linux/futex.h>
  12655. +#include <linux/hrtimer.h>
  12656. +
  12657. +#include "rtmutex_common.h"
  12658. +
  12659. +/*
  12660. + * struct mutex functions
  12661. + */
  12662. +void __mutex_do_init(struct mutex *mutex, const char *name,
  12663. + struct lock_class_key *key)
  12664. +{
  12665. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12666. + /*
  12667. + * Make sure we are not reinitializing a held lock:
  12668. + */
  12669. + debug_check_no_locks_freed((void *)mutex, sizeof(*mutex));
  12670. + lockdep_init_map(&mutex->dep_map, name, key, 0);
  12671. +#endif
  12672. + mutex->lock.save_state = 0;
  12673. +}
  12674. +EXPORT_SYMBOL(__mutex_do_init);
  12675. +
  12676. +void __lockfunc _mutex_lock(struct mutex *lock)
  12677. +{
  12678. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12679. + rt_mutex_lock(&lock->lock);
  12680. +}
  12681. +EXPORT_SYMBOL(_mutex_lock);
  12682. +
  12683. +int __lockfunc _mutex_lock_interruptible(struct mutex *lock)
  12684. +{
  12685. + int ret;
  12686. +
  12687. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12688. + ret = rt_mutex_lock_interruptible(&lock->lock);
  12689. + if (ret)
  12690. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12691. + return ret;
  12692. +}
  12693. +EXPORT_SYMBOL(_mutex_lock_interruptible);
  12694. +
  12695. +int __lockfunc _mutex_lock_killable(struct mutex *lock)
  12696. +{
  12697. + int ret;
  12698. +
  12699. + mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  12700. + ret = rt_mutex_lock_killable(&lock->lock);
  12701. + if (ret)
  12702. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12703. + return ret;
  12704. +}
  12705. +EXPORT_SYMBOL(_mutex_lock_killable);
  12706. +
  12707. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12708. +void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass)
  12709. +{
  12710. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  12711. + rt_mutex_lock(&lock->lock);
  12712. +}
  12713. +EXPORT_SYMBOL(_mutex_lock_nested);
  12714. +
  12715. +void __lockfunc _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  12716. +{
  12717. + mutex_acquire_nest(&lock->dep_map, 0, 0, nest, _RET_IP_);
  12718. + rt_mutex_lock(&lock->lock);
  12719. +}
  12720. +EXPORT_SYMBOL(_mutex_lock_nest_lock);
  12721. +
  12722. +int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass)
  12723. +{
  12724. + int ret;
  12725. +
  12726. + mutex_acquire_nest(&lock->dep_map, subclass, 0, NULL, _RET_IP_);
  12727. + ret = rt_mutex_lock_interruptible(&lock->lock);
  12728. + if (ret)
  12729. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12730. + return ret;
  12731. +}
  12732. +EXPORT_SYMBOL(_mutex_lock_interruptible_nested);
  12733. +
  12734. +int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass)
  12735. +{
  12736. + int ret;
  12737. +
  12738. + mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  12739. + ret = rt_mutex_lock_killable(&lock->lock);
  12740. + if (ret)
  12741. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12742. + return ret;
  12743. +}
  12744. +EXPORT_SYMBOL(_mutex_lock_killable_nested);
  12745. +#endif
  12746. +
  12747. +int __lockfunc _mutex_trylock(struct mutex *lock)
  12748. +{
  12749. + int ret = rt_mutex_trylock(&lock->lock);
  12750. +
  12751. + if (ret)
  12752. + mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  12753. +
  12754. + return ret;
  12755. +}
  12756. +EXPORT_SYMBOL(_mutex_trylock);
  12757. +
  12758. +void __lockfunc _mutex_unlock(struct mutex *lock)
  12759. +{
  12760. + mutex_release(&lock->dep_map, 1, _RET_IP_);
  12761. + rt_mutex_unlock(&lock->lock);
  12762. +}
  12763. +EXPORT_SYMBOL(_mutex_unlock);
  12764. +
  12765. +/*
  12766. + * rwlock_t functions
  12767. + */
  12768. +int __lockfunc rt_write_trylock(rwlock_t *rwlock)
  12769. +{
  12770. + int ret;
  12771. +
  12772. + migrate_disable();
  12773. + ret = rt_mutex_trylock(&rwlock->lock);
  12774. + if (ret)
  12775. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  12776. + else
  12777. + migrate_enable();
  12778. +
  12779. + return ret;
  12780. +}
  12781. +EXPORT_SYMBOL(rt_write_trylock);
  12782. +
  12783. +int __lockfunc rt_write_trylock_irqsave(rwlock_t *rwlock, unsigned long *flags)
  12784. +{
  12785. + int ret;
  12786. +
  12787. + *flags = 0;
  12788. + ret = rt_write_trylock(rwlock);
  12789. + return ret;
  12790. +}
  12791. +EXPORT_SYMBOL(rt_write_trylock_irqsave);
  12792. +
  12793. +int __lockfunc rt_read_trylock(rwlock_t *rwlock)
  12794. +{
  12795. + struct rt_mutex *lock = &rwlock->lock;
  12796. + int ret = 1;
  12797. +
  12798. + /*
  12799. + * recursive read locks succeed when current owns the lock,
  12800. + * but not when read_depth == 0 which means that the lock is
  12801. + * write locked.
  12802. + */
  12803. + if (rt_mutex_owner(lock) != current) {
  12804. + migrate_disable();
  12805. + ret = rt_mutex_trylock(lock);
  12806. + if (ret)
  12807. + rwlock_acquire(&rwlock->dep_map, 0, 1, _RET_IP_);
  12808. + else
  12809. + migrate_enable();
  12810. +
  12811. + } else if (!rwlock->read_depth) {
  12812. + ret = 0;
  12813. + }
  12814. +
  12815. + if (ret)
  12816. + rwlock->read_depth++;
  12817. +
  12818. + return ret;
  12819. +}
  12820. +EXPORT_SYMBOL(rt_read_trylock);
  12821. +
  12822. +void __lockfunc rt_write_lock(rwlock_t *rwlock)
  12823. +{
  12824. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  12825. + __rt_spin_lock(&rwlock->lock);
  12826. +}
  12827. +EXPORT_SYMBOL(rt_write_lock);
  12828. +
  12829. +void __lockfunc rt_read_lock(rwlock_t *rwlock)
  12830. +{
  12831. + struct rt_mutex *lock = &rwlock->lock;
  12832. +
  12833. +
  12834. + /*
  12835. + * recursive read locks succeed when current owns the lock
  12836. + */
  12837. + if (rt_mutex_owner(lock) != current) {
  12838. + rwlock_acquire(&rwlock->dep_map, 0, 0, _RET_IP_);
  12839. + __rt_spin_lock(lock);
  12840. + }
  12841. + rwlock->read_depth++;
  12842. +}
  12843. +
  12844. +EXPORT_SYMBOL(rt_read_lock);
  12845. +
  12846. +void __lockfunc rt_write_unlock(rwlock_t *rwlock)
  12847. +{
  12848. + /* NOTE: we always pass in '1' for nested, for simplicity */
  12849. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  12850. + __rt_spin_unlock(&rwlock->lock);
  12851. + migrate_enable();
  12852. +}
  12853. +EXPORT_SYMBOL(rt_write_unlock);
  12854. +
  12855. +void __lockfunc rt_read_unlock(rwlock_t *rwlock)
  12856. +{
  12857. + /* Release the lock only when read_depth is down to 0 */
  12858. + if (--rwlock->read_depth == 0) {
  12859. + rwlock_release(&rwlock->dep_map, 1, _RET_IP_);
  12860. + __rt_spin_unlock(&rwlock->lock);
  12861. + migrate_enable();
  12862. + }
  12863. +}
  12864. +EXPORT_SYMBOL(rt_read_unlock);
  12865. +
  12866. +unsigned long __lockfunc rt_write_lock_irqsave(rwlock_t *rwlock)
  12867. +{
  12868. + rt_write_lock(rwlock);
  12869. +
  12870. + return 0;
  12871. +}
  12872. +EXPORT_SYMBOL(rt_write_lock_irqsave);
  12873. +
  12874. +unsigned long __lockfunc rt_read_lock_irqsave(rwlock_t *rwlock)
  12875. +{
  12876. + rt_read_lock(rwlock);
  12877. +
  12878. + return 0;
  12879. +}
  12880. +EXPORT_SYMBOL(rt_read_lock_irqsave);
  12881. +
  12882. +void __rt_rwlock_init(rwlock_t *rwlock, char *name, struct lock_class_key *key)
  12883. +{
  12884. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  12885. + /*
  12886. + * Make sure we are not reinitializing a held lock:
  12887. + */
  12888. + debug_check_no_locks_freed((void *)rwlock, sizeof(*rwlock));
  12889. + lockdep_init_map(&rwlock->dep_map, name, key, 0);
  12890. +#endif
  12891. + rwlock->lock.save_state = 1;
  12892. + rwlock->read_depth = 0;
  12893. +}
  12894. +EXPORT_SYMBOL(__rt_rwlock_init);
  12895. +
  12896. +/*
  12897. + * rw_semaphores
  12898. + */
  12899. +
  12900. +void rt_up_write(struct rw_semaphore *rwsem)
  12901. +{
  12902. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  12903. + rt_mutex_unlock(&rwsem->lock);
  12904. +}
  12905. +EXPORT_SYMBOL(rt_up_write);
  12906. +
  12907. +void __rt_up_read(struct rw_semaphore *rwsem)
  12908. +{
  12909. + if (--rwsem->read_depth == 0)
  12910. + rt_mutex_unlock(&rwsem->lock);
  12911. +}
  12912. +
  12913. +void rt_up_read(struct rw_semaphore *rwsem)
  12914. +{
  12915. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  12916. + __rt_up_read(rwsem);
  12917. +}
  12918. +EXPORT_SYMBOL(rt_up_read);
  12919. +
  12920. +/*
  12921. + * downgrade a write lock into a read lock
  12922. + * - just wake up any readers at the front of the queue
  12923. + */
  12924. +void rt_downgrade_write(struct rw_semaphore *rwsem)
  12925. +{
  12926. + BUG_ON(rt_mutex_owner(&rwsem->lock) != current);
  12927. + rwsem->read_depth = 1;
  12928. +}
  12929. +EXPORT_SYMBOL(rt_downgrade_write);
  12930. +
  12931. +int rt_down_write_trylock(struct rw_semaphore *rwsem)
  12932. +{
  12933. + int ret = rt_mutex_trylock(&rwsem->lock);
  12934. +
  12935. + if (ret)
  12936. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  12937. + return ret;
  12938. +}
  12939. +EXPORT_SYMBOL(rt_down_write_trylock);
  12940. +
  12941. +void rt_down_write(struct rw_semaphore *rwsem)
  12942. +{
  12943. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  12944. + rt_mutex_lock(&rwsem->lock);
  12945. +}
  12946. +EXPORT_SYMBOL(rt_down_write);
  12947. +
  12948. +int rt_down_write_killable(struct rw_semaphore *rwsem)
  12949. +{
  12950. + int ret;
  12951. +
  12952. + rwsem_acquire(&rwsem->dep_map, 0, 0, _RET_IP_);
  12953. + ret = rt_mutex_lock_killable(&rwsem->lock);
  12954. + if (ret)
  12955. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  12956. + return ret;
  12957. +}
  12958. +EXPORT_SYMBOL(rt_down_write_killable);
  12959. +
  12960. +int rt_down_write_killable_nested(struct rw_semaphore *rwsem, int subclass)
  12961. +{
  12962. + int ret;
  12963. +
  12964. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  12965. + ret = rt_mutex_lock_killable(&rwsem->lock);
  12966. + if (ret)
  12967. + rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
  12968. + return ret;
  12969. +}
  12970. +EXPORT_SYMBOL(rt_down_write_killable_nested);
  12971. +
  12972. +void rt_down_write_nested(struct rw_semaphore *rwsem, int subclass)
  12973. +{
  12974. + rwsem_acquire(&rwsem->dep_map, subclass, 0, _RET_IP_);
  12975. + rt_mutex_lock(&rwsem->lock);
  12976. +}
  12977. +EXPORT_SYMBOL(rt_down_write_nested);
  12978. +
  12979. +void rt_down_write_nested_lock(struct rw_semaphore *rwsem,
  12980. + struct lockdep_map *nest)
  12981. +{
  12982. + rwsem_acquire_nest(&rwsem->dep_map, 0, 0, nest, _RET_IP_);
  12983. + rt_mutex_lock(&rwsem->lock);
  12984. +}
  12985. +EXPORT_SYMBOL(rt_down_write_nested_lock);
  12986. +
  12987. +int rt__down_read_trylock(struct rw_semaphore *rwsem)
  12988. +{
  12989. + struct rt_mutex *lock = &rwsem->lock;
  12990. + int ret = 1;
  12991. +
  12992. + /*
  12993. + * recursive read locks succeed when current owns the rwsem,
  12994. + * but not when read_depth == 0 which means that the rwsem is
  12995. + * write locked.
  12996. + */
  12997. + if (rt_mutex_owner(lock) != current)
  12998. + ret = rt_mutex_trylock(&rwsem->lock);
  12999. + else if (!rwsem->read_depth)
  13000. + ret = 0;
  13001. +
  13002. + if (ret)
  13003. + rwsem->read_depth++;
  13004. + return ret;
  13005. +
  13006. +}
  13007. +
  13008. +int rt_down_read_trylock(struct rw_semaphore *rwsem)
  13009. +{
  13010. + int ret;
  13011. +
  13012. + ret = rt__down_read_trylock(rwsem);
  13013. + if (ret)
  13014. + rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
  13015. +
  13016. + return ret;
  13017. +}
  13018. +EXPORT_SYMBOL(rt_down_read_trylock);
  13019. +
  13020. +void rt__down_read(struct rw_semaphore *rwsem)
  13021. +{
  13022. + struct rt_mutex *lock = &rwsem->lock;
  13023. +
  13024. + if (rt_mutex_owner(lock) != current)
  13025. + rt_mutex_lock(&rwsem->lock);
  13026. + rwsem->read_depth++;
  13027. +}
  13028. +EXPORT_SYMBOL(rt__down_read);
  13029. +
  13030. +static void __rt_down_read(struct rw_semaphore *rwsem, int subclass)
  13031. +{
  13032. + rwsem_acquire_read(&rwsem->dep_map, subclass, 0, _RET_IP_);
  13033. + rt__down_read(rwsem);
  13034. +}
  13035. +
  13036. +void rt_down_read(struct rw_semaphore *rwsem)
  13037. +{
  13038. + __rt_down_read(rwsem, 0);
  13039. +}
  13040. +EXPORT_SYMBOL(rt_down_read);
  13041. +
  13042. +void rt_down_read_nested(struct rw_semaphore *rwsem, int subclass)
  13043. +{
  13044. + __rt_down_read(rwsem, subclass);
  13045. +}
  13046. +EXPORT_SYMBOL(rt_down_read_nested);
  13047. +
  13048. +void __rt_rwsem_init(struct rw_semaphore *rwsem, const char *name,
  13049. + struct lock_class_key *key)
  13050. +{
  13051. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13052. + /*
  13053. + * Make sure we are not reinitializing a held lock:
  13054. + */
  13055. + debug_check_no_locks_freed((void *)rwsem, sizeof(*rwsem));
  13056. + lockdep_init_map(&rwsem->dep_map, name, key, 0);
  13057. +#endif
  13058. + rwsem->read_depth = 0;
  13059. + rwsem->lock.save_state = 0;
  13060. +}
  13061. +EXPORT_SYMBOL(__rt_rwsem_init);
  13062. +
  13063. +/**
  13064. + * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  13065. + * @cnt: the atomic which we are to dec
  13066. + * @lock: the mutex to return holding if we dec to 0
  13067. + *
  13068. + * return true and hold lock if we dec to 0, return false otherwise
  13069. + */
  13070. +int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  13071. +{
  13072. + /* dec if we can't possibly hit 0 */
  13073. + if (atomic_add_unless(cnt, -1, 1))
  13074. + return 0;
  13075. + /* we might hit 0, so take the lock */
  13076. + mutex_lock(lock);
  13077. + if (!atomic_dec_and_test(cnt)) {
  13078. + /* when we actually did the dec, we didn't hit 0 */
  13079. + mutex_unlock(lock);
  13080. + return 0;
  13081. + }
  13082. + /* we hit 0, and we hold the lock */
  13083. + return 1;
  13084. +}
  13085. +EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  13086. diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
  13087. index 1ec0f48962b3..2576f7ccf8e2 100644
  13088. --- a/kernel/locking/rtmutex.c
  13089. +++ b/kernel/locking/rtmutex.c
  13090. @@ -7,6 +7,11 @@
  13091. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  13092. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  13093. * Copyright (C) 2006 Esben Nielsen
  13094. + * Adaptive Spinlocks:
  13095. + * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  13096. + * and Peter Morreale,
  13097. + * Adaptive Spinlocks simplification:
  13098. + * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  13099. *
  13100. * See Documentation/locking/rt-mutex-design.txt for details.
  13101. */
  13102. @@ -16,6 +21,7 @@
  13103. #include <linux/sched/rt.h>
  13104. #include <linux/sched/deadline.h>
  13105. #include <linux/timer.h>
  13106. +#include <linux/ww_mutex.h>
  13107. #include "rtmutex_common.h"
  13108. @@ -69,6 +75,12 @@ static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  13109. clear_rt_mutex_waiters(lock);
  13110. }
  13111. +static int rt_mutex_real_waiter(struct rt_mutex_waiter *waiter)
  13112. +{
  13113. + return waiter && waiter != PI_WAKEUP_INPROGRESS &&
  13114. + waiter != PI_REQUEUE_INPROGRESS;
  13115. +}
  13116. +
  13117. /*
  13118. * We can speed up the acquire/release, if there's no debugging state to be
  13119. * set up.
  13120. @@ -350,6 +362,14 @@ static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  13121. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  13122. }
  13123. +static void rt_mutex_wake_waiter(struct rt_mutex_waiter *waiter)
  13124. +{
  13125. + if (waiter->savestate)
  13126. + wake_up_lock_sleeper(waiter->task);
  13127. + else
  13128. + wake_up_process(waiter->task);
  13129. +}
  13130. +
  13131. /*
  13132. * Max number of times we'll walk the boosting chain:
  13133. */
  13134. @@ -357,7 +377,8 @@ int max_lock_depth = 1024;
  13135. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  13136. {
  13137. - return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  13138. + return rt_mutex_real_waiter(p->pi_blocked_on) ?
  13139. + p->pi_blocked_on->lock : NULL;
  13140. }
  13141. /*
  13142. @@ -493,7 +514,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  13143. * reached or the state of the chain has changed while we
  13144. * dropped the locks.
  13145. */
  13146. - if (!waiter)
  13147. + if (!rt_mutex_real_waiter(waiter))
  13148. goto out_unlock_pi;
  13149. /*
  13150. @@ -655,13 +676,16 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  13151. * follow here. This is the end of the chain we are walking.
  13152. */
  13153. if (!rt_mutex_owner(lock)) {
  13154. + struct rt_mutex_waiter *lock_top_waiter;
  13155. +
  13156. /*
  13157. * If the requeue [7] above changed the top waiter,
  13158. * then we need to wake the new top waiter up to try
  13159. * to get the lock.
  13160. */
  13161. - if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  13162. - wake_up_process(rt_mutex_top_waiter(lock)->task);
  13163. + lock_top_waiter = rt_mutex_top_waiter(lock);
  13164. + if (prerequeue_top_waiter != lock_top_waiter)
  13165. + rt_mutex_wake_waiter(lock_top_waiter);
  13166. raw_spin_unlock_irq(&lock->wait_lock);
  13167. return 0;
  13168. }
  13169. @@ -754,6 +778,25 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  13170. return ret;
  13171. }
  13172. +
  13173. +#define STEAL_NORMAL 0
  13174. +#define STEAL_LATERAL 1
  13175. +
  13176. +/*
  13177. + * Note that RT tasks are excluded from lateral-steals to prevent the
  13178. + * introduction of an unbounded latency
  13179. + */
  13180. +static inline int lock_is_stealable(struct task_struct *task,
  13181. + struct task_struct *pendowner, int mode)
  13182. +{
  13183. + if (mode == STEAL_NORMAL || rt_task(task)) {
  13184. + if (task->prio >= pendowner->prio)
  13185. + return 0;
  13186. + } else if (task->prio > pendowner->prio)
  13187. + return 0;
  13188. + return 1;
  13189. +}
  13190. +
  13191. /*
  13192. * Try to take an rt-mutex
  13193. *
  13194. @@ -764,8 +807,9 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  13195. * @waiter: The waiter that is queued to the lock's wait tree if the
  13196. * callsite called task_blocked_on_lock(), otherwise NULL
  13197. */
  13198. -static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13199. - struct rt_mutex_waiter *waiter)
  13200. +static int __try_to_take_rt_mutex(struct rt_mutex *lock,
  13201. + struct task_struct *task,
  13202. + struct rt_mutex_waiter *waiter, int mode)
  13203. {
  13204. /*
  13205. * Before testing whether we can acquire @lock, we set the
  13206. @@ -802,8 +846,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13207. * If waiter is not the highest priority waiter of
  13208. * @lock, give up.
  13209. */
  13210. - if (waiter != rt_mutex_top_waiter(lock))
  13211. + if (waiter != rt_mutex_top_waiter(lock)) {
  13212. + /* XXX lock_is_stealable() ? */
  13213. return 0;
  13214. + }
  13215. /*
  13216. * We can acquire the lock. Remove the waiter from the
  13217. @@ -821,14 +867,10 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13218. * not need to be dequeued.
  13219. */
  13220. if (rt_mutex_has_waiters(lock)) {
  13221. - /*
  13222. - * If @task->prio is greater than or equal to
  13223. - * the top waiter priority (kernel view),
  13224. - * @task lost.
  13225. - */
  13226. - if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  13227. - return 0;
  13228. + struct task_struct *pown = rt_mutex_top_waiter(lock)->task;
  13229. + if (task != pown && !lock_is_stealable(task, pown, mode))
  13230. + return 0;
  13231. /*
  13232. * The current top waiter stays enqueued. We
  13233. * don't have to change anything in the lock
  13234. @@ -877,6 +919,438 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13235. return 1;
  13236. }
  13237. +#ifdef CONFIG_PREEMPT_RT_FULL
  13238. +/*
  13239. + * preemptible spin_lock functions:
  13240. + */
  13241. +static inline void rt_spin_lock_fastlock(struct rt_mutex *lock,
  13242. + void (*slowfn)(struct rt_mutex *lock,
  13243. + bool mg_off),
  13244. + bool do_mig_dis)
  13245. +{
  13246. + might_sleep_no_state_check();
  13247. +
  13248. + if (do_mig_dis)
  13249. + migrate_disable();
  13250. +
  13251. + if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  13252. + rt_mutex_deadlock_account_lock(lock, current);
  13253. + else
  13254. + slowfn(lock, do_mig_dis);
  13255. +}
  13256. +
  13257. +static inline int rt_spin_lock_fastunlock(struct rt_mutex *lock,
  13258. + int (*slowfn)(struct rt_mutex *lock))
  13259. +{
  13260. + if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  13261. + rt_mutex_deadlock_account_unlock(current);
  13262. + return 0;
  13263. + }
  13264. + return slowfn(lock);
  13265. +}
  13266. +#ifdef CONFIG_SMP
  13267. +/*
  13268. + * Note that owner is a speculative pointer and dereferencing relies
  13269. + * on rcu_read_lock() and the check against the lock owner.
  13270. + */
  13271. +static int adaptive_wait(struct rt_mutex *lock,
  13272. + struct task_struct *owner)
  13273. +{
  13274. + int res = 0;
  13275. +
  13276. + rcu_read_lock();
  13277. + for (;;) {
  13278. + if (owner != rt_mutex_owner(lock))
  13279. + break;
  13280. + /*
  13281. + * Ensure that owner->on_cpu is dereferenced _after_
  13282. + * checking the above to be valid.
  13283. + */
  13284. + barrier();
  13285. + if (!owner->on_cpu) {
  13286. + res = 1;
  13287. + break;
  13288. + }
  13289. + cpu_relax();
  13290. + }
  13291. + rcu_read_unlock();
  13292. + return res;
  13293. +}
  13294. +#else
  13295. +static int adaptive_wait(struct rt_mutex *lock,
  13296. + struct task_struct *orig_owner)
  13297. +{
  13298. + return 1;
  13299. +}
  13300. +#endif
  13301. +
  13302. +static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  13303. + struct rt_mutex_waiter *waiter,
  13304. + struct task_struct *task,
  13305. + enum rtmutex_chainwalk chwalk);
  13306. +/*
  13307. + * Slow path lock function spin_lock style: this variant is very
  13308. + * careful not to miss any non-lock wakeups.
  13309. + *
  13310. + * We store the current state under p->pi_lock in p->saved_state and
  13311. + * the try_to_wake_up() code handles this accordingly.
  13312. + */
  13313. +static void noinline __sched rt_spin_lock_slowlock(struct rt_mutex *lock,
  13314. + bool mg_off)
  13315. +{
  13316. + struct task_struct *lock_owner, *self = current;
  13317. + struct rt_mutex_waiter waiter, *top_waiter;
  13318. + unsigned long flags;
  13319. + int ret;
  13320. +
  13321. + rt_mutex_init_waiter(&waiter, true);
  13322. +
  13323. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13324. +
  13325. + if (__try_to_take_rt_mutex(lock, self, NULL, STEAL_LATERAL)) {
  13326. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13327. + return;
  13328. + }
  13329. +
  13330. + BUG_ON(rt_mutex_owner(lock) == self);
  13331. +
  13332. + /*
  13333. + * We save whatever state the task is in and we'll restore it
  13334. + * after acquiring the lock taking real wakeups into account
  13335. + * as well. We are serialized via pi_lock against wakeups. See
  13336. + * try_to_wake_up().
  13337. + */
  13338. + raw_spin_lock(&self->pi_lock);
  13339. + self->saved_state = self->state;
  13340. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  13341. + raw_spin_unlock(&self->pi_lock);
  13342. +
  13343. + ret = task_blocks_on_rt_mutex(lock, &waiter, self, RT_MUTEX_MIN_CHAINWALK);
  13344. + BUG_ON(ret);
  13345. +
  13346. + for (;;) {
  13347. + /* Try to acquire the lock again. */
  13348. + if (__try_to_take_rt_mutex(lock, self, &waiter, STEAL_LATERAL))
  13349. + break;
  13350. +
  13351. + top_waiter = rt_mutex_top_waiter(lock);
  13352. + lock_owner = rt_mutex_owner(lock);
  13353. +
  13354. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13355. +
  13356. + debug_rt_mutex_print_deadlock(&waiter);
  13357. +
  13358. + if (top_waiter != &waiter || adaptive_wait(lock, lock_owner)) {
  13359. + if (mg_off)
  13360. + migrate_enable();
  13361. + schedule();
  13362. + if (mg_off)
  13363. + migrate_disable();
  13364. + }
  13365. +
  13366. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13367. +
  13368. + raw_spin_lock(&self->pi_lock);
  13369. + __set_current_state_no_track(TASK_UNINTERRUPTIBLE);
  13370. + raw_spin_unlock(&self->pi_lock);
  13371. + }
  13372. +
  13373. + /*
  13374. + * Restore the task state to current->saved_state. We set it
  13375. + * to the original state above and the try_to_wake_up() code
  13376. + * has possibly updated it when a real (non-rtmutex) wakeup
  13377. + * happened while we were blocked. Clear saved_state so
  13378. + * try_to_wakeup() does not get confused.
  13379. + */
  13380. + raw_spin_lock(&self->pi_lock);
  13381. + __set_current_state_no_track(self->saved_state);
  13382. + self->saved_state = TASK_RUNNING;
  13383. + raw_spin_unlock(&self->pi_lock);
  13384. +
  13385. + /*
  13386. + * try_to_take_rt_mutex() sets the waiter bit
  13387. + * unconditionally. We might have to fix that up:
  13388. + */
  13389. + fixup_rt_mutex_waiters(lock);
  13390. +
  13391. + BUG_ON(rt_mutex_has_waiters(lock) && &waiter == rt_mutex_top_waiter(lock));
  13392. + BUG_ON(!RB_EMPTY_NODE(&waiter.tree_entry));
  13393. +
  13394. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13395. +
  13396. + debug_rt_mutex_free_waiter(&waiter);
  13397. +}
  13398. +
  13399. +static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  13400. + struct wake_q_head *wake_sleeper_q,
  13401. + struct rt_mutex *lock);
  13402. +/*
  13403. + * Slow path to release a rt_mutex spin_lock style
  13404. + */
  13405. +static int noinline __sched rt_spin_lock_slowunlock(struct rt_mutex *lock)
  13406. +{
  13407. + unsigned long flags;
  13408. + WAKE_Q(wake_q);
  13409. + WAKE_Q(wake_sleeper_q);
  13410. +
  13411. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13412. +
  13413. + debug_rt_mutex_unlock(lock);
  13414. +
  13415. + rt_mutex_deadlock_account_unlock(current);
  13416. +
  13417. + if (!rt_mutex_has_waiters(lock)) {
  13418. + lock->owner = NULL;
  13419. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13420. + return 0;
  13421. + }
  13422. +
  13423. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  13424. +
  13425. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13426. + wake_up_q(&wake_q);
  13427. + wake_up_q_sleeper(&wake_sleeper_q);
  13428. +
  13429. + /* Undo pi boosting.when necessary */
  13430. + rt_mutex_adjust_prio(current);
  13431. + return 0;
  13432. +}
  13433. +
  13434. +static int noinline __sched rt_spin_lock_slowunlock_no_deboost(struct rt_mutex *lock)
  13435. +{
  13436. + unsigned long flags;
  13437. + WAKE_Q(wake_q);
  13438. + WAKE_Q(wake_sleeper_q);
  13439. +
  13440. + raw_spin_lock_irqsave(&lock->wait_lock, flags);
  13441. +
  13442. + debug_rt_mutex_unlock(lock);
  13443. +
  13444. + rt_mutex_deadlock_account_unlock(current);
  13445. +
  13446. + if (!rt_mutex_has_waiters(lock)) {
  13447. + lock->owner = NULL;
  13448. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13449. + return 0;
  13450. + }
  13451. +
  13452. + mark_wakeup_next_waiter(&wake_q, &wake_sleeper_q, lock);
  13453. +
  13454. + raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13455. + wake_up_q(&wake_q);
  13456. + wake_up_q_sleeper(&wake_sleeper_q);
  13457. + return 1;
  13458. +}
  13459. +
  13460. +void __lockfunc rt_spin_lock__no_mg(spinlock_t *lock)
  13461. +{
  13462. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, false);
  13463. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  13464. +}
  13465. +EXPORT_SYMBOL(rt_spin_lock__no_mg);
  13466. +
  13467. +void __lockfunc rt_spin_lock(spinlock_t *lock)
  13468. +{
  13469. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  13470. + spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  13471. +}
  13472. +EXPORT_SYMBOL(rt_spin_lock);
  13473. +
  13474. +void __lockfunc __rt_spin_lock(struct rt_mutex *lock)
  13475. +{
  13476. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, true);
  13477. +}
  13478. +EXPORT_SYMBOL(__rt_spin_lock);
  13479. +
  13480. +void __lockfunc __rt_spin_lock__no_mg(struct rt_mutex *lock)
  13481. +{
  13482. + rt_spin_lock_fastlock(lock, rt_spin_lock_slowlock, false);
  13483. +}
  13484. +EXPORT_SYMBOL(__rt_spin_lock__no_mg);
  13485. +
  13486. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13487. +void __lockfunc rt_spin_lock_nested(spinlock_t *lock, int subclass)
  13488. +{
  13489. + spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  13490. + rt_spin_lock_fastlock(&lock->lock, rt_spin_lock_slowlock, true);
  13491. +}
  13492. +EXPORT_SYMBOL(rt_spin_lock_nested);
  13493. +#endif
  13494. +
  13495. +void __lockfunc rt_spin_unlock__no_mg(spinlock_t *lock)
  13496. +{
  13497. + /* NOTE: we always pass in '1' for nested, for simplicity */
  13498. + spin_release(&lock->dep_map, 1, _RET_IP_);
  13499. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  13500. +}
  13501. +EXPORT_SYMBOL(rt_spin_unlock__no_mg);
  13502. +
  13503. +void __lockfunc rt_spin_unlock(spinlock_t *lock)
  13504. +{
  13505. + /* NOTE: we always pass in '1' for nested, for simplicity */
  13506. + spin_release(&lock->dep_map, 1, _RET_IP_);
  13507. + rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock);
  13508. + migrate_enable();
  13509. +}
  13510. +EXPORT_SYMBOL(rt_spin_unlock);
  13511. +
  13512. +int __lockfunc rt_spin_unlock_no_deboost(spinlock_t *lock)
  13513. +{
  13514. + int ret;
  13515. +
  13516. + /* NOTE: we always pass in '1' for nested, for simplicity */
  13517. + spin_release(&lock->dep_map, 1, _RET_IP_);
  13518. + ret = rt_spin_lock_fastunlock(&lock->lock, rt_spin_lock_slowunlock_no_deboost);
  13519. + migrate_enable();
  13520. + return ret;
  13521. +}
  13522. +
  13523. +void __lockfunc __rt_spin_unlock(struct rt_mutex *lock)
  13524. +{
  13525. + rt_spin_lock_fastunlock(lock, rt_spin_lock_slowunlock);
  13526. +}
  13527. +EXPORT_SYMBOL(__rt_spin_unlock);
  13528. +
  13529. +/*
  13530. + * Wait for the lock to get unlocked: instead of polling for an unlock
  13531. + * (like raw spinlocks do), we lock and unlock, to force the kernel to
  13532. + * schedule if there's contention:
  13533. + */
  13534. +void __lockfunc rt_spin_unlock_wait(spinlock_t *lock)
  13535. +{
  13536. + spin_lock(lock);
  13537. + spin_unlock(lock);
  13538. +}
  13539. +EXPORT_SYMBOL(rt_spin_unlock_wait);
  13540. +
  13541. +int __lockfunc __rt_spin_trylock(struct rt_mutex *lock)
  13542. +{
  13543. + return rt_mutex_trylock(lock);
  13544. +}
  13545. +
  13546. +int __lockfunc rt_spin_trylock__no_mg(spinlock_t *lock)
  13547. +{
  13548. + int ret;
  13549. +
  13550. + ret = rt_mutex_trylock(&lock->lock);
  13551. + if (ret)
  13552. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13553. + return ret;
  13554. +}
  13555. +EXPORT_SYMBOL(rt_spin_trylock__no_mg);
  13556. +
  13557. +int __lockfunc rt_spin_trylock(spinlock_t *lock)
  13558. +{
  13559. + int ret;
  13560. +
  13561. + migrate_disable();
  13562. + ret = rt_mutex_trylock(&lock->lock);
  13563. + if (ret)
  13564. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13565. + else
  13566. + migrate_enable();
  13567. + return ret;
  13568. +}
  13569. +EXPORT_SYMBOL(rt_spin_trylock);
  13570. +
  13571. +int __lockfunc rt_spin_trylock_bh(spinlock_t *lock)
  13572. +{
  13573. + int ret;
  13574. +
  13575. + local_bh_disable();
  13576. + ret = rt_mutex_trylock(&lock->lock);
  13577. + if (ret) {
  13578. + migrate_disable();
  13579. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13580. + } else
  13581. + local_bh_enable();
  13582. + return ret;
  13583. +}
  13584. +EXPORT_SYMBOL(rt_spin_trylock_bh);
  13585. +
  13586. +int __lockfunc rt_spin_trylock_irqsave(spinlock_t *lock, unsigned long *flags)
  13587. +{
  13588. + int ret;
  13589. +
  13590. + *flags = 0;
  13591. + ret = rt_mutex_trylock(&lock->lock);
  13592. + if (ret) {
  13593. + migrate_disable();
  13594. + spin_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  13595. + }
  13596. + return ret;
  13597. +}
  13598. +EXPORT_SYMBOL(rt_spin_trylock_irqsave);
  13599. +
  13600. +int atomic_dec_and_spin_lock(atomic_t *atomic, spinlock_t *lock)
  13601. +{
  13602. + /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  13603. + if (atomic_add_unless(atomic, -1, 1))
  13604. + return 0;
  13605. + rt_spin_lock(lock);
  13606. + if (atomic_dec_and_test(atomic))
  13607. + return 1;
  13608. + rt_spin_unlock(lock);
  13609. + return 0;
  13610. +}
  13611. +EXPORT_SYMBOL(atomic_dec_and_spin_lock);
  13612. +
  13613. + void
  13614. +__rt_spin_lock_init(spinlock_t *lock, char *name, struct lock_class_key *key)
  13615. +{
  13616. +#ifdef CONFIG_DEBUG_LOCK_ALLOC
  13617. + /*
  13618. + * Make sure we are not reinitializing a held lock:
  13619. + */
  13620. + debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  13621. + lockdep_init_map(&lock->dep_map, name, key, 0);
  13622. +#endif
  13623. +}
  13624. +EXPORT_SYMBOL(__rt_spin_lock_init);
  13625. +
  13626. +#endif /* PREEMPT_RT_FULL */
  13627. +
  13628. +#ifdef CONFIG_PREEMPT_RT_FULL
  13629. + static inline int __sched
  13630. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  13631. +{
  13632. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  13633. + struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  13634. +
  13635. + if (!hold_ctx)
  13636. + return 0;
  13637. +
  13638. + if (unlikely(ctx == hold_ctx))
  13639. + return -EALREADY;
  13640. +
  13641. + if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  13642. + (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  13643. +#ifdef CONFIG_DEBUG_MUTEXES
  13644. + DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  13645. + ctx->contending_lock = ww;
  13646. +#endif
  13647. + return -EDEADLK;
  13648. + }
  13649. +
  13650. + return 0;
  13651. +}
  13652. +#else
  13653. + static inline int __sched
  13654. +__mutex_lock_check_stamp(struct rt_mutex *lock, struct ww_acquire_ctx *ctx)
  13655. +{
  13656. + BUG();
  13657. + return 0;
  13658. +}
  13659. +
  13660. +#endif
  13661. +
  13662. +static inline int
  13663. +try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  13664. + struct rt_mutex_waiter *waiter)
  13665. +{
  13666. + return __try_to_take_rt_mutex(lock, task, waiter, STEAL_NORMAL);
  13667. +}
  13668. +
  13669. /*
  13670. * Task blocks on lock.
  13671. *
  13672. @@ -907,6 +1381,23 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  13673. return -EDEADLK;
  13674. raw_spin_lock(&task->pi_lock);
  13675. +
  13676. + /*
  13677. + * In the case of futex requeue PI, this will be a proxy
  13678. + * lock. The task will wake unaware that it is enqueueed on
  13679. + * this lock. Avoid blocking on two locks and corrupting
  13680. + * pi_blocked_on via the PI_WAKEUP_INPROGRESS
  13681. + * flag. futex_wait_requeue_pi() sets this when it wakes up
  13682. + * before requeue (due to a signal or timeout). Do not enqueue
  13683. + * the task if PI_WAKEUP_INPROGRESS is set.
  13684. + */
  13685. + if (task != current && task->pi_blocked_on == PI_WAKEUP_INPROGRESS) {
  13686. + raw_spin_unlock(&task->pi_lock);
  13687. + return -EAGAIN;
  13688. + }
  13689. +
  13690. + BUG_ON(rt_mutex_real_waiter(task->pi_blocked_on));
  13691. +
  13692. __rt_mutex_adjust_prio(task);
  13693. waiter->task = task;
  13694. waiter->lock = lock;
  13695. @@ -930,7 +1421,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  13696. rt_mutex_enqueue_pi(owner, waiter);
  13697. __rt_mutex_adjust_prio(owner);
  13698. - if (owner->pi_blocked_on)
  13699. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  13700. chain_walk = 1;
  13701. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  13702. chain_walk = 1;
  13703. @@ -972,6 +1463,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  13704. * Called with lock->wait_lock held and interrupts disabled.
  13705. */
  13706. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  13707. + struct wake_q_head *wake_sleeper_q,
  13708. struct rt_mutex *lock)
  13709. {
  13710. struct rt_mutex_waiter *waiter;
  13711. @@ -1000,7 +1492,10 @@ static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  13712. raw_spin_unlock(&current->pi_lock);
  13713. - wake_q_add(wake_q, waiter->task);
  13714. + if (waiter->savestate)
  13715. + wake_q_add(wake_sleeper_q, waiter->task);
  13716. + else
  13717. + wake_q_add(wake_q, waiter->task);
  13718. }
  13719. /*
  13720. @@ -1014,7 +1509,7 @@ static void remove_waiter(struct rt_mutex *lock,
  13721. {
  13722. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  13723. struct task_struct *owner = rt_mutex_owner(lock);
  13724. - struct rt_mutex *next_lock;
  13725. + struct rt_mutex *next_lock = NULL;
  13726. raw_spin_lock(&current->pi_lock);
  13727. rt_mutex_dequeue(lock, waiter);
  13728. @@ -1038,7 +1533,8 @@ static void remove_waiter(struct rt_mutex *lock,
  13729. __rt_mutex_adjust_prio(owner);
  13730. /* Store the lock on which owner is blocked or NULL */
  13731. - next_lock = task_blocked_on_lock(owner);
  13732. + if (rt_mutex_real_waiter(owner->pi_blocked_on))
  13733. + next_lock = task_blocked_on_lock(owner);
  13734. raw_spin_unlock(&owner->pi_lock);
  13735. @@ -1074,17 +1570,17 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  13736. raw_spin_lock_irqsave(&task->pi_lock, flags);
  13737. waiter = task->pi_blocked_on;
  13738. - if (!waiter || (waiter->prio == task->prio &&
  13739. + if (!rt_mutex_real_waiter(waiter) || (waiter->prio == task->prio &&
  13740. !dl_prio(task->prio))) {
  13741. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13742. return;
  13743. }
  13744. next_lock = waiter->lock;
  13745. - raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13746. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  13747. get_task_struct(task);
  13748. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  13749. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  13750. next_lock, NULL, task);
  13751. }
  13752. @@ -1102,7 +1598,8 @@ void rt_mutex_adjust_pi(struct task_struct *task)
  13753. static int __sched
  13754. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13755. struct hrtimer_sleeper *timeout,
  13756. - struct rt_mutex_waiter *waiter)
  13757. + struct rt_mutex_waiter *waiter,
  13758. + struct ww_acquire_ctx *ww_ctx)
  13759. {
  13760. int ret = 0;
  13761. @@ -1125,6 +1622,12 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13762. break;
  13763. }
  13764. + if (ww_ctx && ww_ctx->acquired > 0) {
  13765. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  13766. + if (ret)
  13767. + break;
  13768. + }
  13769. +
  13770. raw_spin_unlock_irq(&lock->wait_lock);
  13771. debug_rt_mutex_print_deadlock(waiter);
  13772. @@ -1159,21 +1662,96 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  13773. }
  13774. }
  13775. +static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  13776. + struct ww_acquire_ctx *ww_ctx)
  13777. +{
  13778. +#ifdef CONFIG_DEBUG_MUTEXES
  13779. + /*
  13780. + * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  13781. + * but released with a normal mutex_unlock in this call.
  13782. + *
  13783. + * This should never happen, always use ww_mutex_unlock.
  13784. + */
  13785. + DEBUG_LOCKS_WARN_ON(ww->ctx);
  13786. +
  13787. + /*
  13788. + * Not quite done after calling ww_acquire_done() ?
  13789. + */
  13790. + DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  13791. +
  13792. + if (ww_ctx->contending_lock) {
  13793. + /*
  13794. + * After -EDEADLK you tried to
  13795. + * acquire a different ww_mutex? Bad!
  13796. + */
  13797. + DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  13798. +
  13799. + /*
  13800. + * You called ww_mutex_lock after receiving -EDEADLK,
  13801. + * but 'forgot' to unlock everything else first?
  13802. + */
  13803. + DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  13804. + ww_ctx->contending_lock = NULL;
  13805. + }
  13806. +
  13807. + /*
  13808. + * Naughty, using a different class will lead to undefined behavior!
  13809. + */
  13810. + DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  13811. +#endif
  13812. + ww_ctx->acquired++;
  13813. +}
  13814. +
  13815. +#ifdef CONFIG_PREEMPT_RT_FULL
  13816. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  13817. + struct ww_acquire_ctx *ww_ctx)
  13818. +{
  13819. + struct ww_mutex *ww = container_of(lock, struct ww_mutex, base.lock);
  13820. + struct rt_mutex_waiter *waiter, *n;
  13821. +
  13822. + /*
  13823. + * This branch gets optimized out for the common case,
  13824. + * and is only important for ww_mutex_lock.
  13825. + */
  13826. + ww_mutex_lock_acquired(ww, ww_ctx);
  13827. + ww->ctx = ww_ctx;
  13828. +
  13829. + /*
  13830. + * Give any possible sleeping processes the chance to wake up,
  13831. + * so they can recheck if they have to back off.
  13832. + */
  13833. + rbtree_postorder_for_each_entry_safe(waiter, n, &lock->waiters,
  13834. + tree_entry) {
  13835. + /* XXX debug rt mutex waiter wakeup */
  13836. +
  13837. + BUG_ON(waiter->lock != lock);
  13838. + rt_mutex_wake_waiter(waiter);
  13839. + }
  13840. +}
  13841. +
  13842. +#else
  13843. +
  13844. +static void ww_mutex_account_lock(struct rt_mutex *lock,
  13845. + struct ww_acquire_ctx *ww_ctx)
  13846. +{
  13847. + BUG();
  13848. +}
  13849. +#endif
  13850. +
  13851. /*
  13852. * Slow path lock function:
  13853. */
  13854. static int __sched
  13855. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13856. struct hrtimer_sleeper *timeout,
  13857. - enum rtmutex_chainwalk chwalk)
  13858. + enum rtmutex_chainwalk chwalk,
  13859. + struct ww_acquire_ctx *ww_ctx)
  13860. {
  13861. struct rt_mutex_waiter waiter;
  13862. unsigned long flags;
  13863. int ret = 0;
  13864. - debug_rt_mutex_init_waiter(&waiter);
  13865. - RB_CLEAR_NODE(&waiter.pi_tree_entry);
  13866. - RB_CLEAR_NODE(&waiter.tree_entry);
  13867. + rt_mutex_init_waiter(&waiter, false);
  13868. /*
  13869. * Technically we could use raw_spin_[un]lock_irq() here, but this can
  13870. @@ -1187,6 +1765,8 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13871. /* Try to acquire the lock again: */
  13872. if (try_to_take_rt_mutex(lock, current, NULL)) {
  13873. + if (ww_ctx)
  13874. + ww_mutex_account_lock(lock, ww_ctx);
  13875. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13876. return 0;
  13877. }
  13878. @@ -1201,13 +1781,23 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
  13879. if (likely(!ret))
  13880. /* sleep on the mutex */
  13881. - ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  13882. + ret = __rt_mutex_slowlock(lock, state, timeout, &waiter,
  13883. + ww_ctx);
  13884. + else if (ww_ctx) {
  13885. + /* ww_mutex received EDEADLK, let it become EALREADY */
  13886. + ret = __mutex_lock_check_stamp(lock, ww_ctx);
  13887. + BUG_ON(!ret);
  13888. + }
  13889. if (unlikely(ret)) {
  13890. __set_current_state(TASK_RUNNING);
  13891. if (rt_mutex_has_waiters(lock))
  13892. remove_waiter(lock, &waiter);
  13893. - rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  13894. + /* ww_mutex want to report EDEADLK/EALREADY, let them */
  13895. + if (!ww_ctx)
  13896. + rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  13897. + } else if (ww_ctx) {
  13898. + ww_mutex_account_lock(lock, ww_ctx);
  13899. }
  13900. /*
  13901. @@ -1267,7 +1857,8 @@ static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  13902. * Return whether the current task needs to undo a potential priority boosting.
  13903. */
  13904. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  13905. - struct wake_q_head *wake_q)
  13906. + struct wake_q_head *wake_q,
  13907. + struct wake_q_head *wake_sleeper_q)
  13908. {
  13909. unsigned long flags;
  13910. @@ -1323,7 +1914,7 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  13911. *
  13912. * Queue the next waiter for wakeup once we release the wait_lock.
  13913. */
  13914. - mark_wakeup_next_waiter(wake_q, lock);
  13915. + mark_wakeup_next_waiter(wake_q, wake_sleeper_q, lock);
  13916. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  13917. @@ -1339,31 +1930,36 @@ static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  13918. */
  13919. static inline int
  13920. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  13921. + struct ww_acquire_ctx *ww_ctx,
  13922. int (*slowfn)(struct rt_mutex *lock, int state,
  13923. struct hrtimer_sleeper *timeout,
  13924. - enum rtmutex_chainwalk chwalk))
  13925. + enum rtmutex_chainwalk chwalk,
  13926. + struct ww_acquire_ctx *ww_ctx))
  13927. {
  13928. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  13929. rt_mutex_deadlock_account_lock(lock, current);
  13930. return 0;
  13931. } else
  13932. - return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  13933. + return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK,
  13934. + ww_ctx);
  13935. }
  13936. static inline int
  13937. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  13938. struct hrtimer_sleeper *timeout,
  13939. enum rtmutex_chainwalk chwalk,
  13940. + struct ww_acquire_ctx *ww_ctx,
  13941. int (*slowfn)(struct rt_mutex *lock, int state,
  13942. struct hrtimer_sleeper *timeout,
  13943. - enum rtmutex_chainwalk chwalk))
  13944. + enum rtmutex_chainwalk chwalk,
  13945. + struct ww_acquire_ctx *ww_ctx))
  13946. {
  13947. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  13948. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  13949. rt_mutex_deadlock_account_lock(lock, current);
  13950. return 0;
  13951. } else
  13952. - return slowfn(lock, state, timeout, chwalk);
  13953. + return slowfn(lock, state, timeout, chwalk, ww_ctx);
  13954. }
  13955. static inline int
  13956. @@ -1380,17 +1976,20 @@ rt_mutex_fasttrylock(struct rt_mutex *lock,
  13957. static inline void
  13958. rt_mutex_fastunlock(struct rt_mutex *lock,
  13959. bool (*slowfn)(struct rt_mutex *lock,
  13960. - struct wake_q_head *wqh))
  13961. + struct wake_q_head *wqh,
  13962. + struct wake_q_head *wq_sleeper))
  13963. {
  13964. WAKE_Q(wake_q);
  13965. + WAKE_Q(wake_sleeper_q);
  13966. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  13967. rt_mutex_deadlock_account_unlock(current);
  13968. } else {
  13969. - bool deboost = slowfn(lock, &wake_q);
  13970. + bool deboost = slowfn(lock, &wake_q, &wake_sleeper_q);
  13971. wake_up_q(&wake_q);
  13972. + wake_up_q_sleeper(&wake_sleeper_q);
  13973. /* Undo pi boosting if necessary: */
  13974. if (deboost)
  13975. @@ -1407,7 +2006,7 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
  13976. {
  13977. might_sleep();
  13978. - rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  13979. + rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, NULL, rt_mutex_slowlock);
  13980. }
  13981. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  13982. @@ -1424,7 +2023,7 @@ int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  13983. {
  13984. might_sleep();
  13985. - return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  13986. + return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, NULL, rt_mutex_slowlock);
  13987. }
  13988. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  13989. @@ -1437,11 +2036,30 @@ int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  13990. might_sleep();
  13991. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  13992. - RT_MUTEX_FULL_CHAINWALK,
  13993. + RT_MUTEX_FULL_CHAINWALK, NULL,
  13994. rt_mutex_slowlock);
  13995. }
  13996. /**
  13997. + * rt_mutex_lock_killable - lock a rt_mutex killable
  13998. + *
  13999. + * @lock: the rt_mutex to be locked
  14000. + * @detect_deadlock: deadlock detection on/off
  14001. + *
  14002. + * Returns:
  14003. + * 0 on success
  14004. + * -EINTR when interrupted by a signal
  14005. + * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  14006. + */
  14007. +int __sched rt_mutex_lock_killable(struct rt_mutex *lock)
  14008. +{
  14009. + might_sleep();
  14010. +
  14011. + return rt_mutex_fastlock(lock, TASK_KILLABLE, NULL, rt_mutex_slowlock);
  14012. +}
  14013. +EXPORT_SYMBOL_GPL(rt_mutex_lock_killable);
  14014. +
  14015. +/**
  14016. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  14017. * the timeout structure is provided
  14018. * by the caller
  14019. @@ -1461,6 +2079,7 @@ rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  14020. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  14021. RT_MUTEX_MIN_CHAINWALK,
  14022. + NULL,
  14023. rt_mutex_slowlock);
  14024. }
  14025. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  14026. @@ -1478,7 +2097,11 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  14027. */
  14028. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  14029. {
  14030. +#ifdef CONFIG_PREEMPT_RT_FULL
  14031. + if (WARN_ON_ONCE(in_irq() || in_nmi()))
  14032. +#else
  14033. if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
  14034. +#endif
  14035. return 0;
  14036. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  14037. @@ -1504,13 +2127,14 @@ EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  14038. * required or not.
  14039. */
  14040. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  14041. - struct wake_q_head *wqh)
  14042. + struct wake_q_head *wqh,
  14043. + struct wake_q_head *wq_sleeper)
  14044. {
  14045. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  14046. rt_mutex_deadlock_account_unlock(current);
  14047. return false;
  14048. }
  14049. - return rt_mutex_slowunlock(lock, wqh);
  14050. + return rt_mutex_slowunlock(lock, wqh, wq_sleeper);
  14051. }
  14052. /**
  14053. @@ -1543,13 +2167,12 @@ EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  14054. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  14055. {
  14056. lock->owner = NULL;
  14057. - raw_spin_lock_init(&lock->wait_lock);
  14058. lock->waiters = RB_ROOT;
  14059. lock->waiters_leftmost = NULL;
  14060. debug_rt_mutex_init(lock, name);
  14061. }
  14062. -EXPORT_SYMBOL_GPL(__rt_mutex_init);
  14063. +EXPORT_SYMBOL(__rt_mutex_init);
  14064. /**
  14065. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  14066. @@ -1564,7 +2187,7 @@ EXPORT_SYMBOL_GPL(__rt_mutex_init);
  14067. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  14068. struct task_struct *proxy_owner)
  14069. {
  14070. - __rt_mutex_init(lock, NULL);
  14071. + rt_mutex_init(lock);
  14072. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  14073. rt_mutex_set_owner(lock, proxy_owner);
  14074. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  14075. @@ -1612,6 +2235,35 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14076. return 1;
  14077. }
  14078. +#ifdef CONFIG_PREEMPT_RT_FULL
  14079. + /*
  14080. + * In PREEMPT_RT there's an added race.
  14081. + * If the task, that we are about to requeue, times out,
  14082. + * it can set the PI_WAKEUP_INPROGRESS. This tells the requeue
  14083. + * to skip this task. But right after the task sets
  14084. + * its pi_blocked_on to PI_WAKEUP_INPROGRESS it can then
  14085. + * block on the spin_lock(&hb->lock), which in RT is an rtmutex.
  14086. + * This will replace the PI_WAKEUP_INPROGRESS with the actual
  14087. + * lock that it blocks on. We *must not* place this task
  14088. + * on this proxy lock in that case.
  14089. + *
  14090. + * To prevent this race, we first take the task's pi_lock
  14091. + * and check if it has updated its pi_blocked_on. If it has,
  14092. + * we assume that it woke up and we return -EAGAIN.
  14093. + * Otherwise, we set the task's pi_blocked_on to
  14094. + * PI_REQUEUE_INPROGRESS, so that if the task is waking up
  14095. + * it will know that we are in the process of requeuing it.
  14096. + */
  14097. + raw_spin_lock(&task->pi_lock);
  14098. + if (task->pi_blocked_on) {
  14099. + raw_spin_unlock(&task->pi_lock);
  14100. + raw_spin_unlock_irq(&lock->wait_lock);
  14101. + return -EAGAIN;
  14102. + }
  14103. + task->pi_blocked_on = PI_REQUEUE_INPROGRESS;
  14104. + raw_spin_unlock(&task->pi_lock);
  14105. +#endif
  14106. +
  14107. /* We enforce deadlock detection for futexes */
  14108. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  14109. RT_MUTEX_FULL_CHAINWALK);
  14110. @@ -1626,7 +2278,7 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  14111. ret = 0;
  14112. }
  14113. - if (unlikely(ret))
  14114. + if (ret && rt_mutex_has_waiters(lock))
  14115. remove_waiter(lock, waiter);
  14116. raw_spin_unlock_irq(&lock->wait_lock);
  14117. @@ -1682,7 +2334,7 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  14118. set_current_state(TASK_INTERRUPTIBLE);
  14119. /* sleep on the mutex */
  14120. - ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  14121. + ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter, NULL);
  14122. if (unlikely(ret))
  14123. remove_waiter(lock, waiter);
  14124. @@ -1697,3 +2349,89 @@ int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  14125. return ret;
  14126. }
  14127. +
  14128. +static inline int
  14129. +ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  14130. +{
  14131. +#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  14132. + unsigned tmp;
  14133. +
  14134. + if (ctx->deadlock_inject_countdown-- == 0) {
  14135. + tmp = ctx->deadlock_inject_interval;
  14136. + if (tmp > UINT_MAX/4)
  14137. + tmp = UINT_MAX;
  14138. + else
  14139. + tmp = tmp*2 + tmp + tmp/2;
  14140. +
  14141. + ctx->deadlock_inject_interval = tmp;
  14142. + ctx->deadlock_inject_countdown = tmp;
  14143. + ctx->contending_lock = lock;
  14144. +
  14145. + ww_mutex_unlock(lock);
  14146. +
  14147. + return -EDEADLK;
  14148. + }
  14149. +#endif
  14150. +
  14151. + return 0;
  14152. +}
  14153. +
  14154. +#ifdef CONFIG_PREEMPT_RT_FULL
  14155. +int __sched
  14156. +__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  14157. +{
  14158. + int ret;
  14159. +
  14160. + might_sleep();
  14161. +
  14162. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  14163. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_INTERRUPTIBLE, NULL, 0, ww_ctx);
  14164. + if (ret)
  14165. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  14166. + else if (!ret && ww_ctx->acquired > 1)
  14167. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  14168. +
  14169. + return ret;
  14170. +}
  14171. +EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  14172. +
  14173. +int __sched
  14174. +__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ww_ctx)
  14175. +{
  14176. + int ret;
  14177. +
  14178. + might_sleep();
  14179. +
  14180. + mutex_acquire_nest(&lock->base.dep_map, 0, 0, &ww_ctx->dep_map, _RET_IP_);
  14181. + ret = rt_mutex_slowlock(&lock->base.lock, TASK_UNINTERRUPTIBLE, NULL, 0, ww_ctx);
  14182. + if (ret)
  14183. + mutex_release(&lock->base.dep_map, 1, _RET_IP_);
  14184. + else if (!ret && ww_ctx->acquired > 1)
  14185. + return ww_mutex_deadlock_injection(lock, ww_ctx);
  14186. +
  14187. + return ret;
  14188. +}
  14189. +EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  14190. +
  14191. +void __sched ww_mutex_unlock(struct ww_mutex *lock)
  14192. +{
  14193. + int nest = !!lock->ctx;
  14194. +
  14195. + /*
  14196. + * The unlocking fastpath is the 0->1 transition from 'locked'
  14197. + * into 'unlocked' state:
  14198. + */
  14199. + if (nest) {
  14200. +#ifdef CONFIG_DEBUG_MUTEXES
  14201. + DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  14202. +#endif
  14203. + if (lock->ctx->acquired > 0)
  14204. + lock->ctx->acquired--;
  14205. + lock->ctx = NULL;
  14206. + }
  14207. +
  14208. + mutex_release(&lock->base.dep_map, nest, _RET_IP_);
  14209. + rt_mutex_unlock(&lock->base.lock);
  14210. +}
  14211. +EXPORT_SYMBOL(ww_mutex_unlock);
  14212. +#endif
  14213. diff --git a/kernel/locking/rtmutex_common.h b/kernel/locking/rtmutex_common.h
  14214. index 4f5f83c7d2d3..289f062f26cd 100644
  14215. --- a/kernel/locking/rtmutex_common.h
  14216. +++ b/kernel/locking/rtmutex_common.h
  14217. @@ -27,6 +27,7 @@ struct rt_mutex_waiter {
  14218. struct rb_node pi_tree_entry;
  14219. struct task_struct *task;
  14220. struct rt_mutex *lock;
  14221. + bool savestate;
  14222. #ifdef CONFIG_DEBUG_RT_MUTEXES
  14223. unsigned long ip;
  14224. struct pid *deadlock_task_pid;
  14225. @@ -97,6 +98,9 @@ enum rtmutex_chainwalk {
  14226. /*
  14227. * PI-futex support (proxy locking functions, etc.):
  14228. */
  14229. +#define PI_WAKEUP_INPROGRESS ((struct rt_mutex_waiter *) 1)
  14230. +#define PI_REQUEUE_INPROGRESS ((struct rt_mutex_waiter *) 2)
  14231. +
  14232. extern struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock);
  14233. extern void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  14234. struct task_struct *proxy_owner);
  14235. @@ -110,7 +114,8 @@ extern int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  14236. struct rt_mutex_waiter *waiter);
  14237. extern int rt_mutex_timed_futex_lock(struct rt_mutex *l, struct hrtimer_sleeper *to);
  14238. extern bool rt_mutex_futex_unlock(struct rt_mutex *lock,
  14239. - struct wake_q_head *wqh);
  14240. + struct wake_q_head *wqh,
  14241. + struct wake_q_head *wq_sleeper);
  14242. extern void rt_mutex_adjust_prio(struct task_struct *task);
  14243. #ifdef CONFIG_DEBUG_RT_MUTEXES
  14244. @@ -119,4 +124,14 @@ extern void rt_mutex_adjust_prio(struct task_struct *task);
  14245. # include "rtmutex.h"
  14246. #endif
  14247. +static inline void
  14248. +rt_mutex_init_waiter(struct rt_mutex_waiter *waiter, bool savestate)
  14249. +{
  14250. + debug_rt_mutex_init_waiter(waiter);
  14251. + waiter->task = NULL;
  14252. + waiter->savestate = savestate;
  14253. + RB_CLEAR_NODE(&waiter->pi_tree_entry);
  14254. + RB_CLEAR_NODE(&waiter->tree_entry);
  14255. +}
  14256. +
  14257. #endif
  14258. diff --git a/kernel/locking/spinlock.c b/kernel/locking/spinlock.c
  14259. index db3ccb1dd614..909779647bd1 100644
  14260. --- a/kernel/locking/spinlock.c
  14261. +++ b/kernel/locking/spinlock.c
  14262. @@ -124,8 +124,11 @@ void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
  14263. * __[spin|read|write]_lock_bh()
  14264. */
  14265. BUILD_LOCK_OPS(spin, raw_spinlock);
  14266. +
  14267. +#ifndef CONFIG_PREEMPT_RT_FULL
  14268. BUILD_LOCK_OPS(read, rwlock);
  14269. BUILD_LOCK_OPS(write, rwlock);
  14270. +#endif
  14271. #endif
  14272. @@ -209,6 +212,8 @@ void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
  14273. EXPORT_SYMBOL(_raw_spin_unlock_bh);
  14274. #endif
  14275. +#ifndef CONFIG_PREEMPT_RT_FULL
  14276. +
  14277. #ifndef CONFIG_INLINE_READ_TRYLOCK
  14278. int __lockfunc _raw_read_trylock(rwlock_t *lock)
  14279. {
  14280. @@ -353,6 +358,8 @@ void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
  14281. EXPORT_SYMBOL(_raw_write_unlock_bh);
  14282. #endif
  14283. +#endif /* !PREEMPT_RT_FULL */
  14284. +
  14285. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  14286. void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
  14287. diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
  14288. index 0374a596cffa..94970338d518 100644
  14289. --- a/kernel/locking/spinlock_debug.c
  14290. +++ b/kernel/locking/spinlock_debug.c
  14291. @@ -31,6 +31,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  14292. EXPORT_SYMBOL(__raw_spin_lock_init);
  14293. +#ifndef CONFIG_PREEMPT_RT_FULL
  14294. void __rwlock_init(rwlock_t *lock, const char *name,
  14295. struct lock_class_key *key)
  14296. {
  14297. @@ -48,6 +49,7 @@ void __rwlock_init(rwlock_t *lock, const char *name,
  14298. }
  14299. EXPORT_SYMBOL(__rwlock_init);
  14300. +#endif
  14301. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  14302. {
  14303. @@ -159,6 +161,7 @@ void do_raw_spin_unlock(raw_spinlock_t *lock)
  14304. arch_spin_unlock(&lock->raw_lock);
  14305. }
  14306. +#ifndef CONFIG_PREEMPT_RT_FULL
  14307. static void rwlock_bug(rwlock_t *lock, const char *msg)
  14308. {
  14309. if (!debug_locks_off())
  14310. @@ -300,3 +303,5 @@ void do_raw_write_unlock(rwlock_t *lock)
  14311. debug_write_unlock(lock);
  14312. arch_write_unlock(&lock->raw_lock);
  14313. }
  14314. +
  14315. +#endif
  14316. diff --git a/kernel/panic.c b/kernel/panic.c
  14317. index ca8cea1ef673..6b698115f003 100644
  14318. --- a/kernel/panic.c
  14319. +++ b/kernel/panic.c
  14320. @@ -449,9 +449,11 @@ static u64 oops_id;
  14321. static int init_oops_id(void)
  14322. {
  14323. +#ifndef CONFIG_PREEMPT_RT_FULL
  14324. if (!oops_id)
  14325. get_random_bytes(&oops_id, sizeof(oops_id));
  14326. else
  14327. +#endif
  14328. oops_id++;
  14329. return 0;
  14330. diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
  14331. index 33c79b6105c5..f53375bc77df 100644
  14332. --- a/kernel/power/hibernate.c
  14333. +++ b/kernel/power/hibernate.c
  14334. @@ -286,6 +286,8 @@ static int create_image(int platform_mode)
  14335. local_irq_disable();
  14336. + system_state = SYSTEM_SUSPEND;
  14337. +
  14338. error = syscore_suspend();
  14339. if (error) {
  14340. printk(KERN_ERR "PM: Some system devices failed to power down, "
  14341. @@ -315,6 +317,7 @@ static int create_image(int platform_mode)
  14342. syscore_resume();
  14343. Enable_irqs:
  14344. + system_state = SYSTEM_RUNNING;
  14345. local_irq_enable();
  14346. Enable_cpus:
  14347. @@ -444,6 +447,7 @@ static int resume_target_kernel(bool platform_mode)
  14348. goto Enable_cpus;
  14349. local_irq_disable();
  14350. + system_state = SYSTEM_SUSPEND;
  14351. error = syscore_suspend();
  14352. if (error)
  14353. @@ -477,6 +481,7 @@ static int resume_target_kernel(bool platform_mode)
  14354. syscore_resume();
  14355. Enable_irqs:
  14356. + system_state = SYSTEM_RUNNING;
  14357. local_irq_enable();
  14358. Enable_cpus:
  14359. @@ -562,6 +567,7 @@ int hibernation_platform_enter(void)
  14360. goto Enable_cpus;
  14361. local_irq_disable();
  14362. + system_state = SYSTEM_SUSPEND;
  14363. syscore_suspend();
  14364. if (pm_wakeup_pending()) {
  14365. error = -EAGAIN;
  14366. @@ -574,6 +580,7 @@ int hibernation_platform_enter(void)
  14367. Power_up:
  14368. syscore_resume();
  14369. + system_state = SYSTEM_RUNNING;
  14370. local_irq_enable();
  14371. Enable_cpus:
  14372. @@ -674,6 +681,10 @@ static int load_image_and_restore(void)
  14373. return error;
  14374. }
  14375. +#ifndef CONFIG_SUSPEND
  14376. +bool pm_in_action;
  14377. +#endif
  14378. +
  14379. /**
  14380. * hibernate - Carry out system hibernation, including saving the image.
  14381. */
  14382. @@ -687,6 +698,8 @@ int hibernate(void)
  14383. return -EPERM;
  14384. }
  14385. + pm_in_action = true;
  14386. +
  14387. lock_system_sleep();
  14388. /* The snapshot device should not be opened while we're running */
  14389. if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
  14390. @@ -764,6 +777,7 @@ int hibernate(void)
  14391. atomic_inc(&snapshot_device_available);
  14392. Unlock:
  14393. unlock_system_sleep();
  14394. + pm_in_action = false;
  14395. return error;
  14396. }
  14397. diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c
  14398. index 0acab9d7f96f..aac06aad757c 100644
  14399. --- a/kernel/power/suspend.c
  14400. +++ b/kernel/power/suspend.c
  14401. @@ -361,6 +361,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  14402. arch_suspend_disable_irqs();
  14403. BUG_ON(!irqs_disabled());
  14404. + system_state = SYSTEM_SUSPEND;
  14405. +
  14406. error = syscore_suspend();
  14407. if (!error) {
  14408. *wakeup = pm_wakeup_pending();
  14409. @@ -377,6 +379,8 @@ static int suspend_enter(suspend_state_t state, bool *wakeup)
  14410. syscore_resume();
  14411. }
  14412. + system_state = SYSTEM_RUNNING;
  14413. +
  14414. arch_suspend_enable_irqs();
  14415. BUG_ON(irqs_disabled());
  14416. @@ -519,6 +523,8 @@ static int enter_state(suspend_state_t state)
  14417. return error;
  14418. }
  14419. +bool pm_in_action;
  14420. +
  14421. /**
  14422. * pm_suspend - Externally visible function for suspending the system.
  14423. * @state: System sleep state to enter.
  14424. @@ -533,6 +539,8 @@ int pm_suspend(suspend_state_t state)
  14425. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  14426. return -EINVAL;
  14427. + pm_in_action = true;
  14428. +
  14429. error = enter_state(state);
  14430. if (error) {
  14431. suspend_stats.fail++;
  14432. @@ -540,6 +548,7 @@ int pm_suspend(suspend_state_t state)
  14433. } else {
  14434. suspend_stats.success++;
  14435. }
  14436. + pm_in_action = false;
  14437. return error;
  14438. }
  14439. EXPORT_SYMBOL(pm_suspend);
  14440. diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
  14441. index eea6dbc2d8cf..6f01c7ecb45e 100644
  14442. --- a/kernel/printk/printk.c
  14443. +++ b/kernel/printk/printk.c
  14444. @@ -351,6 +351,65 @@ __packed __aligned(4)
  14445. */
  14446. DEFINE_RAW_SPINLOCK(logbuf_lock);
  14447. +#ifdef CONFIG_EARLY_PRINTK
  14448. +struct console *early_console;
  14449. +
  14450. +static void early_vprintk(const char *fmt, va_list ap)
  14451. +{
  14452. + if (early_console) {
  14453. + char buf[512];
  14454. + int n = vscnprintf(buf, sizeof(buf), fmt, ap);
  14455. +
  14456. + early_console->write(early_console, buf, n);
  14457. + }
  14458. +}
  14459. +
  14460. +asmlinkage void early_printk(const char *fmt, ...)
  14461. +{
  14462. + va_list ap;
  14463. +
  14464. + va_start(ap, fmt);
  14465. + early_vprintk(fmt, ap);
  14466. + va_end(ap);
  14467. +}
  14468. +
  14469. +/*
  14470. + * This is independent of any log levels - a global
  14471. + * kill switch that turns off all of printk.
  14472. + *
  14473. + * Used by the NMI watchdog if early-printk is enabled.
  14474. + */
  14475. +static bool __read_mostly printk_killswitch;
  14476. +
  14477. +static int __init force_early_printk_setup(char *str)
  14478. +{
  14479. + printk_killswitch = true;
  14480. + return 0;
  14481. +}
  14482. +early_param("force_early_printk", force_early_printk_setup);
  14483. +
  14484. +void printk_kill(void)
  14485. +{
  14486. + printk_killswitch = true;
  14487. +}
  14488. +
  14489. +#ifdef CONFIG_PRINTK
  14490. +static int forced_early_printk(const char *fmt, va_list ap)
  14491. +{
  14492. + if (!printk_killswitch)
  14493. + return 0;
  14494. + early_vprintk(fmt, ap);
  14495. + return 1;
  14496. +}
  14497. +#endif
  14498. +
  14499. +#else
  14500. +static inline int forced_early_printk(const char *fmt, va_list ap)
  14501. +{
  14502. + return 0;
  14503. +}
  14504. +#endif
  14505. +
  14506. #ifdef CONFIG_PRINTK
  14507. DECLARE_WAIT_QUEUE_HEAD(log_wait);
  14508. /* the next printk record to read by syslog(READ) or /proc/kmsg */
  14509. @@ -1340,6 +1399,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  14510. {
  14511. char *text;
  14512. int len = 0;
  14513. + int attempts = 0;
  14514. text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
  14515. if (!text)
  14516. @@ -1351,6 +1411,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  14517. u64 seq;
  14518. u32 idx;
  14519. enum log_flags prev;
  14520. + int num_msg;
  14521. +try_again:
  14522. + attempts++;
  14523. + if (attempts > 10) {
  14524. + len = -EBUSY;
  14525. + goto out;
  14526. + }
  14527. + num_msg = 0;
  14528. /*
  14529. * Find first record that fits, including all following records,
  14530. @@ -1366,6 +1434,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  14531. prev = msg->flags;
  14532. idx = log_next(idx);
  14533. seq++;
  14534. + num_msg++;
  14535. + if (num_msg > 5) {
  14536. + num_msg = 0;
  14537. + raw_spin_unlock_irq(&logbuf_lock);
  14538. + raw_spin_lock_irq(&logbuf_lock);
  14539. + if (clear_seq < log_first_seq)
  14540. + goto try_again;
  14541. + }
  14542. }
  14543. /* move first record forward until length fits into the buffer */
  14544. @@ -1379,6 +1455,14 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  14545. prev = msg->flags;
  14546. idx = log_next(idx);
  14547. seq++;
  14548. + num_msg++;
  14549. + if (num_msg > 5) {
  14550. + num_msg = 0;
  14551. + raw_spin_unlock_irq(&logbuf_lock);
  14552. + raw_spin_lock_irq(&logbuf_lock);
  14553. + if (clear_seq < log_first_seq)
  14554. + goto try_again;
  14555. + }
  14556. }
  14557. /* last message fitting into this dump */
  14558. @@ -1419,6 +1503,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
  14559. clear_seq = log_next_seq;
  14560. clear_idx = log_next_idx;
  14561. }
  14562. +out:
  14563. raw_spin_unlock_irq(&logbuf_lock);
  14564. kfree(text);
  14565. @@ -1572,6 +1657,12 @@ static void call_console_drivers(int level,
  14566. if (!console_drivers)
  14567. return;
  14568. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  14569. + if (in_irq() || in_nmi())
  14570. + return;
  14571. + }
  14572. +
  14573. + migrate_disable();
  14574. for_each_console(con) {
  14575. if (exclusive_console && con != exclusive_console)
  14576. continue;
  14577. @@ -1587,6 +1678,7 @@ static void call_console_drivers(int level,
  14578. else
  14579. con->write(con, text, len);
  14580. }
  14581. + migrate_enable();
  14582. }
  14583. /*
  14584. @@ -1750,6 +1842,13 @@ asmlinkage int vprintk_emit(int facility, int level,
  14585. /* cpu currently holding logbuf_lock in this function */
  14586. static unsigned int logbuf_cpu = UINT_MAX;
  14587. + /*
  14588. + * Fall back to early_printk if a debugging subsystem has
  14589. + * killed printk output
  14590. + */
  14591. + if (unlikely(forced_early_printk(fmt, args)))
  14592. + return 1;
  14593. +
  14594. if (level == LOGLEVEL_SCHED) {
  14595. level = LOGLEVEL_DEFAULT;
  14596. in_sched = true;
  14597. @@ -1894,13 +1993,23 @@ asmlinkage int vprintk_emit(int facility, int level,
  14598. /* If called from the scheduler, we can not call up(). */
  14599. if (!in_sched) {
  14600. + int may_trylock = 1;
  14601. +
  14602. lockdep_off();
  14603. +#ifdef CONFIG_PREEMPT_RT_FULL
  14604. + /*
  14605. + * we can't take a sleeping lock with IRQs or preeption disabled
  14606. + * so we can't print in these contexts
  14607. + */
  14608. + if (!(preempt_count() == 0 && !irqs_disabled()))
  14609. + may_trylock = 0;
  14610. +#endif
  14611. /*
  14612. * Try to acquire and then immediately release the console
  14613. * semaphore. The release will print out buffers and wake up
  14614. * /dev/kmsg and syslog() users.
  14615. */
  14616. - if (console_trylock())
  14617. + if (may_trylock && console_trylock())
  14618. console_unlock();
  14619. lockdep_on();
  14620. }
  14621. @@ -2023,26 +2132,6 @@ DEFINE_PER_CPU(printk_func_t, printk_func);
  14622. #endif /* CONFIG_PRINTK */
  14623. -#ifdef CONFIG_EARLY_PRINTK
  14624. -struct console *early_console;
  14625. -
  14626. -asmlinkage __visible void early_printk(const char *fmt, ...)
  14627. -{
  14628. - va_list ap;
  14629. - char buf[512];
  14630. - int n;
  14631. -
  14632. - if (!early_console)
  14633. - return;
  14634. -
  14635. - va_start(ap, fmt);
  14636. - n = vscnprintf(buf, sizeof(buf), fmt, ap);
  14637. - va_end(ap);
  14638. -
  14639. - early_console->write(early_console, buf, n);
  14640. -}
  14641. -#endif
  14642. -
  14643. static int __add_preferred_console(char *name, int idx, char *options,
  14644. char *brl_options)
  14645. {
  14646. @@ -2312,11 +2401,16 @@ static void console_cont_flush(char *text, size_t size)
  14647. goto out;
  14648. len = cont_print_text(text, size);
  14649. +#ifdef CONFIG_PREEMPT_RT_FULL
  14650. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  14651. + call_console_drivers(cont.level, NULL, 0, text, len);
  14652. +#else
  14653. raw_spin_unlock(&logbuf_lock);
  14654. stop_critical_timings();
  14655. call_console_drivers(cont.level, NULL, 0, text, len);
  14656. start_critical_timings();
  14657. local_irq_restore(flags);
  14658. +#endif
  14659. return;
  14660. out:
  14661. raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  14662. @@ -2440,13 +2534,17 @@ void console_unlock(void)
  14663. console_idx = log_next(console_idx);
  14664. console_seq++;
  14665. console_prev = msg->flags;
  14666. +#ifdef CONFIG_PREEMPT_RT_FULL
  14667. + raw_spin_unlock_irqrestore(&logbuf_lock, flags);
  14668. + call_console_drivers(level, ext_text, ext_len, text, len);
  14669. +#else
  14670. raw_spin_unlock(&logbuf_lock);
  14671. stop_critical_timings(); /* don't trace print latency */
  14672. call_console_drivers(level, ext_text, ext_len, text, len);
  14673. start_critical_timings();
  14674. local_irq_restore(flags);
  14675. -
  14676. +#endif
  14677. if (do_cond_resched)
  14678. cond_resched();
  14679. }
  14680. @@ -2498,6 +2596,11 @@ void console_unblank(void)
  14681. {
  14682. struct console *c;
  14683. + if (IS_ENABLED(CONFIG_PREEMPT_RT_BASE)) {
  14684. + if (in_irq() || in_nmi())
  14685. + return;
  14686. + }
  14687. +
  14688. /*
  14689. * console_unblank can no longer be called in interrupt context unless
  14690. * oops_in_progress is set to 1..
  14691. diff --git a/kernel/ptrace.c b/kernel/ptrace.c
  14692. index 1d3b7665d0be..ce666639789d 100644
  14693. --- a/kernel/ptrace.c
  14694. +++ b/kernel/ptrace.c
  14695. @@ -128,7 +128,14 @@ static bool ptrace_freeze_traced(struct task_struct *task)
  14696. spin_lock_irq(&task->sighand->siglock);
  14697. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  14698. - task->state = __TASK_TRACED;
  14699. + unsigned long flags;
  14700. +
  14701. + raw_spin_lock_irqsave(&task->pi_lock, flags);
  14702. + if (task->state & __TASK_TRACED)
  14703. + task->state = __TASK_TRACED;
  14704. + else
  14705. + task->saved_state = __TASK_TRACED;
  14706. + raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  14707. ret = true;
  14708. }
  14709. spin_unlock_irq(&task->sighand->siglock);
  14710. diff --git a/kernel/rcu/rcutorture.c b/kernel/rcu/rcutorture.c
  14711. index 971e2b138063..a304670fb917 100644
  14712. --- a/kernel/rcu/rcutorture.c
  14713. +++ b/kernel/rcu/rcutorture.c
  14714. @@ -404,6 +404,7 @@ static struct rcu_torture_ops rcu_ops = {
  14715. .name = "rcu"
  14716. };
  14717. +#ifndef CONFIG_PREEMPT_RT_FULL
  14718. /*
  14719. * Definitions for rcu_bh torture testing.
  14720. */
  14721. @@ -443,6 +444,12 @@ static struct rcu_torture_ops rcu_bh_ops = {
  14722. .name = "rcu_bh"
  14723. };
  14724. +#else
  14725. +static struct rcu_torture_ops rcu_bh_ops = {
  14726. + .ttype = INVALID_RCU_FLAVOR,
  14727. +};
  14728. +#endif
  14729. +
  14730. /*
  14731. * Don't even think about trying any of these in real life!!!
  14732. * The names includes "busted", and they really means it!
  14733. diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
  14734. index 5d80925e7fc8..2b4bc2b2c25a 100644
  14735. --- a/kernel/rcu/tree.c
  14736. +++ b/kernel/rcu/tree.c
  14737. @@ -56,6 +56,11 @@
  14738. #include <linux/random.h>
  14739. #include <linux/trace_events.h>
  14740. #include <linux/suspend.h>
  14741. +#include <linux/delay.h>
  14742. +#include <linux/gfp.h>
  14743. +#include <linux/oom.h>
  14744. +#include <linux/smpboot.h>
  14745. +#include "../time/tick-internal.h"
  14746. #include "tree.h"
  14747. #include "rcu.h"
  14748. @@ -259,6 +264,19 @@ void rcu_sched_qs(void)
  14749. this_cpu_ptr(&rcu_sched_data), true);
  14750. }
  14751. +#ifdef CONFIG_PREEMPT_RT_FULL
  14752. +static void rcu_preempt_qs(void);
  14753. +
  14754. +void rcu_bh_qs(void)
  14755. +{
  14756. + unsigned long flags;
  14757. +
  14758. + /* Callers to this function, rcu_preempt_qs(), must disable irqs. */
  14759. + local_irq_save(flags);
  14760. + rcu_preempt_qs();
  14761. + local_irq_restore(flags);
  14762. +}
  14763. +#else
  14764. void rcu_bh_qs(void)
  14765. {
  14766. if (__this_cpu_read(rcu_bh_data.cpu_no_qs.s)) {
  14767. @@ -268,6 +286,7 @@ void rcu_bh_qs(void)
  14768. __this_cpu_write(rcu_bh_data.cpu_no_qs.b.norm, false);
  14769. }
  14770. }
  14771. +#endif
  14772. static DEFINE_PER_CPU(int, rcu_sched_qs_mask);
  14773. @@ -448,11 +467,13 @@ EXPORT_SYMBOL_GPL(rcu_batches_started_sched);
  14774. /*
  14775. * Return the number of RCU BH batches started thus far for debug & stats.
  14776. */
  14777. +#ifndef CONFIG_PREEMPT_RT_FULL
  14778. unsigned long rcu_batches_started_bh(void)
  14779. {
  14780. return rcu_bh_state.gpnum;
  14781. }
  14782. EXPORT_SYMBOL_GPL(rcu_batches_started_bh);
  14783. +#endif
  14784. /*
  14785. * Return the number of RCU batches completed thus far for debug & stats.
  14786. @@ -472,6 +493,7 @@ unsigned long rcu_batches_completed_sched(void)
  14787. }
  14788. EXPORT_SYMBOL_GPL(rcu_batches_completed_sched);
  14789. +#ifndef CONFIG_PREEMPT_RT_FULL
  14790. /*
  14791. * Return the number of RCU BH batches completed thus far for debug & stats.
  14792. */
  14793. @@ -480,6 +502,7 @@ unsigned long rcu_batches_completed_bh(void)
  14794. return rcu_bh_state.completed;
  14795. }
  14796. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  14797. +#endif
  14798. /*
  14799. * Return the number of RCU expedited batches completed thus far for
  14800. @@ -503,6 +526,7 @@ unsigned long rcu_exp_batches_completed_sched(void)
  14801. }
  14802. EXPORT_SYMBOL_GPL(rcu_exp_batches_completed_sched);
  14803. +#ifndef CONFIG_PREEMPT_RT_FULL
  14804. /*
  14805. * Force a quiescent state.
  14806. */
  14807. @@ -521,6 +545,13 @@ void rcu_bh_force_quiescent_state(void)
  14808. }
  14809. EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state);
  14810. +#else
  14811. +void rcu_force_quiescent_state(void)
  14812. +{
  14813. +}
  14814. +EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  14815. +#endif
  14816. +
  14817. /*
  14818. * Force a quiescent state for RCU-sched.
  14819. */
  14820. @@ -571,9 +602,11 @@ void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  14821. case RCU_FLAVOR:
  14822. rsp = rcu_state_p;
  14823. break;
  14824. +#ifndef CONFIG_PREEMPT_RT_FULL
  14825. case RCU_BH_FLAVOR:
  14826. rsp = &rcu_bh_state;
  14827. break;
  14828. +#endif
  14829. case RCU_SCHED_FLAVOR:
  14830. rsp = &rcu_sched_state;
  14831. break;
  14832. @@ -3013,18 +3046,17 @@ __rcu_process_callbacks(struct rcu_state *rsp)
  14833. /*
  14834. * Do RCU core processing for the current CPU.
  14835. */
  14836. -static void rcu_process_callbacks(struct softirq_action *unused)
  14837. +static void rcu_process_callbacks(void)
  14838. {
  14839. struct rcu_state *rsp;
  14840. if (cpu_is_offline(smp_processor_id()))
  14841. return;
  14842. - trace_rcu_utilization(TPS("Start RCU core"));
  14843. for_each_rcu_flavor(rsp)
  14844. __rcu_process_callbacks(rsp);
  14845. - trace_rcu_utilization(TPS("End RCU core"));
  14846. }
  14847. +static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  14848. /*
  14849. * Schedule RCU callback invocation. If the specified type of RCU
  14850. * does not support RCU priority boosting, just do a direct call,
  14851. @@ -3036,19 +3068,106 @@ static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
  14852. {
  14853. if (unlikely(!READ_ONCE(rcu_scheduler_fully_active)))
  14854. return;
  14855. - if (likely(!rsp->boost)) {
  14856. - rcu_do_batch(rsp, rdp);
  14857. - return;
  14858. - }
  14859. - invoke_rcu_callbacks_kthread();
  14860. + rcu_do_batch(rsp, rdp);
  14861. }
  14862. +static void rcu_wake_cond(struct task_struct *t, int status)
  14863. +{
  14864. + /*
  14865. + * If the thread is yielding, only wake it when this
  14866. + * is invoked from idle
  14867. + */
  14868. + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)))
  14869. + wake_up_process(t);
  14870. +}
  14871. +
  14872. +/*
  14873. + * Wake up this CPU's rcuc kthread to do RCU core processing.
  14874. + */
  14875. static void invoke_rcu_core(void)
  14876. {
  14877. - if (cpu_online(smp_processor_id()))
  14878. - raise_softirq(RCU_SOFTIRQ);
  14879. + unsigned long flags;
  14880. + struct task_struct *t;
  14881. +
  14882. + if (!cpu_online(smp_processor_id()))
  14883. + return;
  14884. + local_irq_save(flags);
  14885. + __this_cpu_write(rcu_cpu_has_work, 1);
  14886. + t = __this_cpu_read(rcu_cpu_kthread_task);
  14887. + if (t != NULL && current != t)
  14888. + rcu_wake_cond(t, __this_cpu_read(rcu_cpu_kthread_status));
  14889. + local_irq_restore(flags);
  14890. }
  14891. +static void rcu_cpu_kthread_park(unsigned int cpu)
  14892. +{
  14893. + per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  14894. +}
  14895. +
  14896. +static int rcu_cpu_kthread_should_run(unsigned int cpu)
  14897. +{
  14898. + return __this_cpu_read(rcu_cpu_has_work);
  14899. +}
  14900. +
  14901. +/*
  14902. + * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  14903. + * RCU softirq used in flavors and configurations of RCU that do not
  14904. + * support RCU priority boosting.
  14905. + */
  14906. +static void rcu_cpu_kthread(unsigned int cpu)
  14907. +{
  14908. + unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  14909. + char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  14910. + int spincnt;
  14911. +
  14912. + for (spincnt = 0; spincnt < 10; spincnt++) {
  14913. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  14914. + local_bh_disable();
  14915. + *statusp = RCU_KTHREAD_RUNNING;
  14916. + this_cpu_inc(rcu_cpu_kthread_loops);
  14917. + local_irq_disable();
  14918. + work = *workp;
  14919. + *workp = 0;
  14920. + local_irq_enable();
  14921. + if (work)
  14922. + rcu_process_callbacks();
  14923. + local_bh_enable();
  14924. + if (*workp == 0) {
  14925. + trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  14926. + *statusp = RCU_KTHREAD_WAITING;
  14927. + return;
  14928. + }
  14929. + }
  14930. + *statusp = RCU_KTHREAD_YIELDING;
  14931. + trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  14932. + schedule_timeout_interruptible(2);
  14933. + trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  14934. + *statusp = RCU_KTHREAD_WAITING;
  14935. +}
  14936. +
  14937. +static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  14938. + .store = &rcu_cpu_kthread_task,
  14939. + .thread_should_run = rcu_cpu_kthread_should_run,
  14940. + .thread_fn = rcu_cpu_kthread,
  14941. + .thread_comm = "rcuc/%u",
  14942. + .setup = rcu_cpu_kthread_setup,
  14943. + .park = rcu_cpu_kthread_park,
  14944. +};
  14945. +
  14946. +/*
  14947. + * Spawn per-CPU RCU core processing kthreads.
  14948. + */
  14949. +static int __init rcu_spawn_core_kthreads(void)
  14950. +{
  14951. + int cpu;
  14952. +
  14953. + for_each_possible_cpu(cpu)
  14954. + per_cpu(rcu_cpu_has_work, cpu) = 0;
  14955. + BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  14956. + return 0;
  14957. +}
  14958. +early_initcall(rcu_spawn_core_kthreads);
  14959. +
  14960. /*
  14961. * Handle any core-RCU processing required by a call_rcu() invocation.
  14962. */
  14963. @@ -3192,6 +3311,7 @@ void call_rcu_sched(struct rcu_head *head, rcu_callback_t func)
  14964. }
  14965. EXPORT_SYMBOL_GPL(call_rcu_sched);
  14966. +#ifndef CONFIG_PREEMPT_RT_FULL
  14967. /*
  14968. * Queue an RCU callback for invocation after a quicker grace period.
  14969. */
  14970. @@ -3200,6 +3320,7 @@ void call_rcu_bh(struct rcu_head *head, rcu_callback_t func)
  14971. __call_rcu(head, func, &rcu_bh_state, -1, 0);
  14972. }
  14973. EXPORT_SYMBOL_GPL(call_rcu_bh);
  14974. +#endif
  14975. /*
  14976. * Queue an RCU callback for lazy invocation after a grace period.
  14977. @@ -3291,6 +3412,7 @@ void synchronize_sched(void)
  14978. }
  14979. EXPORT_SYMBOL_GPL(synchronize_sched);
  14980. +#ifndef CONFIG_PREEMPT_RT_FULL
  14981. /**
  14982. * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed.
  14983. *
  14984. @@ -3317,6 +3439,7 @@ void synchronize_rcu_bh(void)
  14985. wait_rcu_gp(call_rcu_bh);
  14986. }
  14987. EXPORT_SYMBOL_GPL(synchronize_rcu_bh);
  14988. +#endif
  14989. /**
  14990. * get_state_synchronize_rcu - Snapshot current RCU state
  14991. @@ -3695,6 +3818,7 @@ static void _rcu_barrier(struct rcu_state *rsp)
  14992. mutex_unlock(&rsp->barrier_mutex);
  14993. }
  14994. +#ifndef CONFIG_PREEMPT_RT_FULL
  14995. /**
  14996. * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete.
  14997. */
  14998. @@ -3703,6 +3827,7 @@ void rcu_barrier_bh(void)
  14999. _rcu_barrier(&rcu_bh_state);
  15000. }
  15001. EXPORT_SYMBOL_GPL(rcu_barrier_bh);
  15002. +#endif
  15003. /**
  15004. * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks.
  15005. @@ -4196,12 +4321,13 @@ void __init rcu_init(void)
  15006. rcu_bootup_announce();
  15007. rcu_init_geometry();
  15008. +#ifndef CONFIG_PREEMPT_RT_FULL
  15009. rcu_init_one(&rcu_bh_state);
  15010. +#endif
  15011. rcu_init_one(&rcu_sched_state);
  15012. if (dump_tree)
  15013. rcu_dump_rcu_node_tree(&rcu_sched_state);
  15014. __rcu_init_preempt();
  15015. - open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  15016. /*
  15017. * We don't need protection against CPU-hotplug here because
  15018. diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
  15019. index f714f873bf9d..71631196e66e 100644
  15020. --- a/kernel/rcu/tree.h
  15021. +++ b/kernel/rcu/tree.h
  15022. @@ -587,18 +587,18 @@ extern struct list_head rcu_struct_flavors;
  15023. */
  15024. extern struct rcu_state rcu_sched_state;
  15025. +#ifndef CONFIG_PREEMPT_RT_FULL
  15026. extern struct rcu_state rcu_bh_state;
  15027. +#endif
  15028. #ifdef CONFIG_PREEMPT_RCU
  15029. extern struct rcu_state rcu_preempt_state;
  15030. #endif /* #ifdef CONFIG_PREEMPT_RCU */
  15031. -#ifdef CONFIG_RCU_BOOST
  15032. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15033. DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu);
  15034. DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15035. DECLARE_PER_CPU(char, rcu_cpu_has_work);
  15036. -#endif /* #ifdef CONFIG_RCU_BOOST */
  15037. #ifndef RCU_TREE_NONCORE
  15038. @@ -618,10 +618,9 @@ void call_rcu(struct rcu_head *head, rcu_callback_t func);
  15039. static void __init __rcu_init_preempt(void);
  15040. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags);
  15041. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp);
  15042. -static void invoke_rcu_callbacks_kthread(void);
  15043. static bool rcu_is_callbacks_kthread(void);
  15044. +static void rcu_cpu_kthread_setup(unsigned int cpu);
  15045. #ifdef CONFIG_RCU_BOOST
  15046. -static void rcu_preempt_do_callbacks(void);
  15047. static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  15048. struct rcu_node *rnp);
  15049. #endif /* #ifdef CONFIG_RCU_BOOST */
  15050. diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h
  15051. index 0082fce402a0..e08cddadd9c7 100644
  15052. --- a/kernel/rcu/tree_plugin.h
  15053. +++ b/kernel/rcu/tree_plugin.h
  15054. @@ -24,25 +24,10 @@
  15055. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  15056. */
  15057. -#include <linux/delay.h>
  15058. -#include <linux/gfp.h>
  15059. -#include <linux/oom.h>
  15060. -#include <linux/smpboot.h>
  15061. -#include "../time/tick-internal.h"
  15062. -
  15063. #ifdef CONFIG_RCU_BOOST
  15064. #include "../locking/rtmutex_common.h"
  15065. -/*
  15066. - * Control variables for per-CPU and per-rcu_node kthreads. These
  15067. - * handle all flavors of RCU.
  15068. - */
  15069. -static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task);
  15070. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15071. -DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15072. -DEFINE_PER_CPU(char, rcu_cpu_has_work);
  15073. -
  15074. #else /* #ifdef CONFIG_RCU_BOOST */
  15075. /*
  15076. @@ -55,6 +40,14 @@ DEFINE_PER_CPU(char, rcu_cpu_has_work);
  15077. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  15078. +/*
  15079. + * Control variables for per-CPU and per-rcu_node kthreads. These
  15080. + * handle all flavors of RCU.
  15081. + */
  15082. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status);
  15083. +DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops);
  15084. +DEFINE_PER_CPU(char, rcu_cpu_has_work);
  15085. +
  15086. #ifdef CONFIG_RCU_NOCB_CPU
  15087. static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
  15088. static bool have_rcu_nocb_mask; /* Was rcu_nocb_mask allocated? */
  15089. @@ -426,7 +419,7 @@ void rcu_read_unlock_special(struct task_struct *t)
  15090. }
  15091. /* Hardware IRQ handlers cannot block, complain if they get here. */
  15092. - if (in_irq() || in_serving_softirq()) {
  15093. + if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
  15094. lockdep_rcu_suspicious(__FILE__, __LINE__,
  15095. "rcu_read_unlock() from irq or softirq with blocking in critical section!!!\n");
  15096. pr_alert("->rcu_read_unlock_special: %#x (b: %d, enq: %d nq: %d)\n",
  15097. @@ -632,15 +625,6 @@ static void rcu_preempt_check_callbacks(void)
  15098. t->rcu_read_unlock_special.b.need_qs = true;
  15099. }
  15100. -#ifdef CONFIG_RCU_BOOST
  15101. -
  15102. -static void rcu_preempt_do_callbacks(void)
  15103. -{
  15104. - rcu_do_batch(rcu_state_p, this_cpu_ptr(rcu_data_p));
  15105. -}
  15106. -
  15107. -#endif /* #ifdef CONFIG_RCU_BOOST */
  15108. -
  15109. /*
  15110. * Queue a preemptible-RCU callback for invocation after a grace period.
  15111. */
  15112. @@ -829,6 +813,19 @@ void exit_rcu(void)
  15113. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  15114. +/*
  15115. + * If boosting, set rcuc kthreads to realtime priority.
  15116. + */
  15117. +static void rcu_cpu_kthread_setup(unsigned int cpu)
  15118. +{
  15119. +#ifdef CONFIG_RCU_BOOST
  15120. + struct sched_param sp;
  15121. +
  15122. + sp.sched_priority = kthread_prio;
  15123. + sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  15124. +#endif /* #ifdef CONFIG_RCU_BOOST */
  15125. +}
  15126. +
  15127. #ifdef CONFIG_RCU_BOOST
  15128. #include "../locking/rtmutex_common.h"
  15129. @@ -860,16 +857,6 @@ static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  15130. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  15131. -static void rcu_wake_cond(struct task_struct *t, int status)
  15132. -{
  15133. - /*
  15134. - * If the thread is yielding, only wake it when this
  15135. - * is invoked from idle
  15136. - */
  15137. - if (status != RCU_KTHREAD_YIELDING || is_idle_task(current))
  15138. - wake_up_process(t);
  15139. -}
  15140. -
  15141. /*
  15142. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  15143. * or ->boost_tasks, advancing the pointer to the next task in the
  15144. @@ -1013,23 +1000,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  15145. }
  15146. /*
  15147. - * Wake up the per-CPU kthread to invoke RCU callbacks.
  15148. - */
  15149. -static void invoke_rcu_callbacks_kthread(void)
  15150. -{
  15151. - unsigned long flags;
  15152. -
  15153. - local_irq_save(flags);
  15154. - __this_cpu_write(rcu_cpu_has_work, 1);
  15155. - if (__this_cpu_read(rcu_cpu_kthread_task) != NULL &&
  15156. - current != __this_cpu_read(rcu_cpu_kthread_task)) {
  15157. - rcu_wake_cond(__this_cpu_read(rcu_cpu_kthread_task),
  15158. - __this_cpu_read(rcu_cpu_kthread_status));
  15159. - }
  15160. - local_irq_restore(flags);
  15161. -}
  15162. -
  15163. -/*
  15164. * Is the current CPU running the RCU-callbacks kthread?
  15165. * Caller must have preemption disabled.
  15166. */
  15167. @@ -1083,67 +1053,6 @@ static int rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  15168. return 0;
  15169. }
  15170. -static void rcu_kthread_do_work(void)
  15171. -{
  15172. - rcu_do_batch(&rcu_sched_state, this_cpu_ptr(&rcu_sched_data));
  15173. - rcu_do_batch(&rcu_bh_state, this_cpu_ptr(&rcu_bh_data));
  15174. - rcu_preempt_do_callbacks();
  15175. -}
  15176. -
  15177. -static void rcu_cpu_kthread_setup(unsigned int cpu)
  15178. -{
  15179. - struct sched_param sp;
  15180. -
  15181. - sp.sched_priority = kthread_prio;
  15182. - sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
  15183. -}
  15184. -
  15185. -static void rcu_cpu_kthread_park(unsigned int cpu)
  15186. -{
  15187. - per_cpu(rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU;
  15188. -}
  15189. -
  15190. -static int rcu_cpu_kthread_should_run(unsigned int cpu)
  15191. -{
  15192. - return __this_cpu_read(rcu_cpu_has_work);
  15193. -}
  15194. -
  15195. -/*
  15196. - * Per-CPU kernel thread that invokes RCU callbacks. This replaces the
  15197. - * RCU softirq used in flavors and configurations of RCU that do not
  15198. - * support RCU priority boosting.
  15199. - */
  15200. -static void rcu_cpu_kthread(unsigned int cpu)
  15201. -{
  15202. - unsigned int *statusp = this_cpu_ptr(&rcu_cpu_kthread_status);
  15203. - char work, *workp = this_cpu_ptr(&rcu_cpu_has_work);
  15204. - int spincnt;
  15205. -
  15206. - for (spincnt = 0; spincnt < 10; spincnt++) {
  15207. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_wait"));
  15208. - local_bh_disable();
  15209. - *statusp = RCU_KTHREAD_RUNNING;
  15210. - this_cpu_inc(rcu_cpu_kthread_loops);
  15211. - local_irq_disable();
  15212. - work = *workp;
  15213. - *workp = 0;
  15214. - local_irq_enable();
  15215. - if (work)
  15216. - rcu_kthread_do_work();
  15217. - local_bh_enable();
  15218. - if (*workp == 0) {
  15219. - trace_rcu_utilization(TPS("End CPU kthread@rcu_wait"));
  15220. - *statusp = RCU_KTHREAD_WAITING;
  15221. - return;
  15222. - }
  15223. - }
  15224. - *statusp = RCU_KTHREAD_YIELDING;
  15225. - trace_rcu_utilization(TPS("Start CPU kthread@rcu_yield"));
  15226. - schedule_timeout_interruptible(2);
  15227. - trace_rcu_utilization(TPS("End CPU kthread@rcu_yield"));
  15228. - *statusp = RCU_KTHREAD_WAITING;
  15229. -}
  15230. -
  15231. /*
  15232. * Set the per-rcu_node kthread's affinity to cover all CPUs that are
  15233. * served by the rcu_node in question. The CPU hotplug lock is still
  15234. @@ -1174,26 +1083,12 @@ static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu)
  15235. free_cpumask_var(cm);
  15236. }
  15237. -static struct smp_hotplug_thread rcu_cpu_thread_spec = {
  15238. - .store = &rcu_cpu_kthread_task,
  15239. - .thread_should_run = rcu_cpu_kthread_should_run,
  15240. - .thread_fn = rcu_cpu_kthread,
  15241. - .thread_comm = "rcuc/%u",
  15242. - .setup = rcu_cpu_kthread_setup,
  15243. - .park = rcu_cpu_kthread_park,
  15244. -};
  15245. -
  15246. /*
  15247. * Spawn boost kthreads -- called as soon as the scheduler is running.
  15248. */
  15249. static void __init rcu_spawn_boost_kthreads(void)
  15250. {
  15251. struct rcu_node *rnp;
  15252. - int cpu;
  15253. -
  15254. - for_each_possible_cpu(cpu)
  15255. - per_cpu(rcu_cpu_has_work, cpu) = 0;
  15256. - BUG_ON(smpboot_register_percpu_thread(&rcu_cpu_thread_spec));
  15257. rcu_for_each_leaf_node(rcu_state_p, rnp)
  15258. (void)rcu_spawn_one_boost_kthread(rcu_state_p, rnp);
  15259. }
  15260. @@ -1216,11 +1111,6 @@ static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  15261. raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
  15262. }
  15263. -static void invoke_rcu_callbacks_kthread(void)
  15264. -{
  15265. - WARN_ON_ONCE(1);
  15266. -}
  15267. -
  15268. static bool rcu_is_callbacks_kthread(void)
  15269. {
  15270. return false;
  15271. @@ -1244,7 +1134,7 @@ static void rcu_prepare_kthreads(int cpu)
  15272. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  15273. -#if !defined(CONFIG_RCU_FAST_NO_HZ)
  15274. +#if !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL)
  15275. /*
  15276. * Check to see if any future RCU-related work will need to be done
  15277. @@ -1261,7 +1151,9 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  15278. return IS_ENABLED(CONFIG_RCU_NOCB_CPU_ALL)
  15279. ? 0 : rcu_cpu_has_callbacks(NULL);
  15280. }
  15281. +#endif /* !defined(CONFIG_RCU_FAST_NO_HZ) || defined(CONFIG_PREEMPT_RT_FULL) */
  15282. +#if !defined(CONFIG_RCU_FAST_NO_HZ)
  15283. /*
  15284. * Because we do not have RCU_FAST_NO_HZ, don't bother cleaning up
  15285. * after it.
  15286. @@ -1357,6 +1249,8 @@ static bool __maybe_unused rcu_try_advance_all_cbs(void)
  15287. return cbs_ready;
  15288. }
  15289. +#ifndef CONFIG_PREEMPT_RT_FULL
  15290. +
  15291. /*
  15292. * Allow the CPU to enter dyntick-idle mode unless it has callbacks ready
  15293. * to invoke. If the CPU has callbacks, try to advance them. Tell the
  15294. @@ -1402,6 +1296,7 @@ int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  15295. *nextevt = basemono + dj * TICK_NSEC;
  15296. return 0;
  15297. }
  15298. +#endif /* #ifndef CONFIG_PREEMPT_RT_FULL */
  15299. /*
  15300. * Prepare a CPU for idle from an RCU perspective. The first major task
  15301. diff --git a/kernel/rcu/update.c b/kernel/rcu/update.c
  15302. index f0d8322bc3ec..b40d3468ba4e 100644
  15303. --- a/kernel/rcu/update.c
  15304. +++ b/kernel/rcu/update.c
  15305. @@ -295,6 +295,7 @@ int rcu_read_lock_held(void)
  15306. }
  15307. EXPORT_SYMBOL_GPL(rcu_read_lock_held);
  15308. +#ifndef CONFIG_PREEMPT_RT_FULL
  15309. /**
  15310. * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
  15311. *
  15312. @@ -321,6 +322,7 @@ int rcu_read_lock_bh_held(void)
  15313. return in_softirq() || irqs_disabled();
  15314. }
  15315. EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
  15316. +#endif
  15317. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  15318. diff --git a/kernel/relay.c b/kernel/relay.c
  15319. index d797502140b9..cf05c17ddbed 100644
  15320. --- a/kernel/relay.c
  15321. +++ b/kernel/relay.c
  15322. @@ -336,6 +336,10 @@ static void wakeup_readers(unsigned long data)
  15323. {
  15324. struct rchan_buf *buf = (struct rchan_buf *)data;
  15325. wake_up_interruptible(&buf->read_wait);
  15326. + /*
  15327. + * Stupid polling for now:
  15328. + */
  15329. + mod_timer(&buf->timer, jiffies + 1);
  15330. }
  15331. /**
  15332. @@ -353,6 +357,7 @@ static void __relay_reset(struct rchan_buf *buf, unsigned int init)
  15333. init_waitqueue_head(&buf->read_wait);
  15334. kref_init(&buf->kref);
  15335. setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
  15336. + mod_timer(&buf->timer, jiffies + 1);
  15337. } else
  15338. del_timer_sync(&buf->timer);
  15339. @@ -767,15 +772,6 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
  15340. else
  15341. buf->early_bytes += buf->chan->subbuf_size -
  15342. buf->padding[old_subbuf];
  15343. - smp_mb();
  15344. - if (waitqueue_active(&buf->read_wait))
  15345. - /*
  15346. - * Calling wake_up_interruptible() from here
  15347. - * will deadlock if we happen to be logging
  15348. - * from the scheduler (trying to re-grab
  15349. - * rq->lock), so defer it.
  15350. - */
  15351. - mod_timer(&buf->timer, jiffies + 1);
  15352. }
  15353. old = buf->data;
  15354. diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
  15355. index 5e59b832ae2b..7337a7f60e3f 100644
  15356. --- a/kernel/sched/Makefile
  15357. +++ b/kernel/sched/Makefile
  15358. @@ -17,7 +17,7 @@ endif
  15359. obj-y += core.o loadavg.o clock.o cputime.o
  15360. obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
  15361. -obj-y += wait.o swait.o completion.o idle.o
  15362. +obj-y += wait.o swait.o swork.o completion.o idle.o
  15363. obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
  15364. obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
  15365. obj-$(CONFIG_SCHEDSTATS) += stats.o
  15366. diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c
  15367. index 8d0f35debf35..b62cf6400fe0 100644
  15368. --- a/kernel/sched/completion.c
  15369. +++ b/kernel/sched/completion.c
  15370. @@ -30,10 +30,10 @@ void complete(struct completion *x)
  15371. {
  15372. unsigned long flags;
  15373. - spin_lock_irqsave(&x->wait.lock, flags);
  15374. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15375. x->done++;
  15376. - __wake_up_locked(&x->wait, TASK_NORMAL, 1);
  15377. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15378. + swake_up_locked(&x->wait);
  15379. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15380. }
  15381. EXPORT_SYMBOL(complete);
  15382. @@ -50,10 +50,10 @@ void complete_all(struct completion *x)
  15383. {
  15384. unsigned long flags;
  15385. - spin_lock_irqsave(&x->wait.lock, flags);
  15386. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15387. x->done += UINT_MAX/2;
  15388. - __wake_up_locked(&x->wait, TASK_NORMAL, 0);
  15389. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15390. + swake_up_all_locked(&x->wait);
  15391. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15392. }
  15393. EXPORT_SYMBOL(complete_all);
  15394. @@ -62,20 +62,20 @@ do_wait_for_common(struct completion *x,
  15395. long (*action)(long), long timeout, int state)
  15396. {
  15397. if (!x->done) {
  15398. - DECLARE_WAITQUEUE(wait, current);
  15399. + DECLARE_SWAITQUEUE(wait);
  15400. - __add_wait_queue_tail_exclusive(&x->wait, &wait);
  15401. + __prepare_to_swait(&x->wait, &wait);
  15402. do {
  15403. if (signal_pending_state(state, current)) {
  15404. timeout = -ERESTARTSYS;
  15405. break;
  15406. }
  15407. __set_current_state(state);
  15408. - spin_unlock_irq(&x->wait.lock);
  15409. + raw_spin_unlock_irq(&x->wait.lock);
  15410. timeout = action(timeout);
  15411. - spin_lock_irq(&x->wait.lock);
  15412. + raw_spin_lock_irq(&x->wait.lock);
  15413. } while (!x->done && timeout);
  15414. - __remove_wait_queue(&x->wait, &wait);
  15415. + __finish_swait(&x->wait, &wait);
  15416. if (!x->done)
  15417. return timeout;
  15418. }
  15419. @@ -89,9 +89,9 @@ __wait_for_common(struct completion *x,
  15420. {
  15421. might_sleep();
  15422. - spin_lock_irq(&x->wait.lock);
  15423. + raw_spin_lock_irq(&x->wait.lock);
  15424. timeout = do_wait_for_common(x, action, timeout, state);
  15425. - spin_unlock_irq(&x->wait.lock);
  15426. + raw_spin_unlock_irq(&x->wait.lock);
  15427. return timeout;
  15428. }
  15429. @@ -277,12 +277,12 @@ bool try_wait_for_completion(struct completion *x)
  15430. if (!READ_ONCE(x->done))
  15431. return 0;
  15432. - spin_lock_irqsave(&x->wait.lock, flags);
  15433. + raw_spin_lock_irqsave(&x->wait.lock, flags);
  15434. if (!x->done)
  15435. ret = 0;
  15436. else
  15437. x->done--;
  15438. - spin_unlock_irqrestore(&x->wait.lock, flags);
  15439. + raw_spin_unlock_irqrestore(&x->wait.lock, flags);
  15440. return ret;
  15441. }
  15442. EXPORT_SYMBOL(try_wait_for_completion);
  15443. @@ -311,7 +311,7 @@ bool completion_done(struct completion *x)
  15444. * after it's acquired the lock.
  15445. */
  15446. smp_rmb();
  15447. - spin_unlock_wait(&x->wait.lock);
  15448. + raw_spin_unlock_wait(&x->wait.lock);
  15449. return true;
  15450. }
  15451. EXPORT_SYMBOL(completion_done);
  15452. diff --git a/kernel/sched/core.c b/kernel/sched/core.c
  15453. index 44817c640e99..55aafcff5810 100644
  15454. --- a/kernel/sched/core.c
  15455. +++ b/kernel/sched/core.c
  15456. @@ -129,7 +129,11 @@ const_debug unsigned int sysctl_sched_features =
  15457. * Number of tasks to iterate in a single balance run.
  15458. * Limited because this is done with IRQs disabled.
  15459. */
  15460. +#ifndef CONFIG_PREEMPT_RT_FULL
  15461. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  15462. +#else
  15463. +const_debug unsigned int sysctl_sched_nr_migrate = 8;
  15464. +#endif
  15465. /*
  15466. * period over which we average the RT time consumption, measured
  15467. @@ -345,6 +349,7 @@ static void init_rq_hrtick(struct rq *rq)
  15468. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  15469. rq->hrtick_timer.function = hrtick;
  15470. + rq->hrtick_timer.irqsafe = 1;
  15471. }
  15472. #else /* CONFIG_SCHED_HRTICK */
  15473. static inline void hrtick_clear(struct rq *rq)
  15474. @@ -449,7 +454,7 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  15475. head->lastp = &node->next;
  15476. }
  15477. -void wake_up_q(struct wake_q_head *head)
  15478. +void __wake_up_q(struct wake_q_head *head, bool sleeper)
  15479. {
  15480. struct wake_q_node *node = head->first;
  15481. @@ -466,7 +471,10 @@ void wake_up_q(struct wake_q_head *head)
  15482. * wake_up_process() implies a wmb() to pair with the queueing
  15483. * in wake_q_add() so as not to miss wakeups.
  15484. */
  15485. - wake_up_process(task);
  15486. + if (sleeper)
  15487. + wake_up_lock_sleeper(task);
  15488. + else
  15489. + wake_up_process(task);
  15490. put_task_struct(task);
  15491. }
  15492. }
  15493. @@ -502,6 +510,38 @@ void resched_curr(struct rq *rq)
  15494. trace_sched_wake_idle_without_ipi(cpu);
  15495. }
  15496. +#ifdef CONFIG_PREEMPT_LAZY
  15497. +void resched_curr_lazy(struct rq *rq)
  15498. +{
  15499. + struct task_struct *curr = rq->curr;
  15500. + int cpu;
  15501. +
  15502. + if (!sched_feat(PREEMPT_LAZY)) {
  15503. + resched_curr(rq);
  15504. + return;
  15505. + }
  15506. +
  15507. + lockdep_assert_held(&rq->lock);
  15508. +
  15509. + if (test_tsk_need_resched(curr))
  15510. + return;
  15511. +
  15512. + if (test_tsk_need_resched_lazy(curr))
  15513. + return;
  15514. +
  15515. + set_tsk_need_resched_lazy(curr);
  15516. +
  15517. + cpu = cpu_of(rq);
  15518. + if (cpu == smp_processor_id())
  15519. + return;
  15520. +
  15521. + /* NEED_RESCHED_LAZY must be visible before we test polling */
  15522. + smp_mb();
  15523. + if (!tsk_is_polling(curr))
  15524. + smp_send_reschedule(cpu);
  15525. +}
  15526. +#endif
  15527. +
  15528. void resched_cpu(int cpu)
  15529. {
  15530. struct rq *rq = cpu_rq(cpu);
  15531. @@ -525,11 +565,14 @@ void resched_cpu(int cpu)
  15532. */
  15533. int get_nohz_timer_target(void)
  15534. {
  15535. - int i, cpu = smp_processor_id();
  15536. + int i, cpu;
  15537. struct sched_domain *sd;
  15538. + preempt_disable_rt();
  15539. + cpu = smp_processor_id();
  15540. +
  15541. if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
  15542. - return cpu;
  15543. + goto preempt_en_rt;
  15544. rcu_read_lock();
  15545. for_each_domain(cpu, sd) {
  15546. @@ -548,6 +591,8 @@ int get_nohz_timer_target(void)
  15547. cpu = housekeeping_any_cpu();
  15548. unlock:
  15549. rcu_read_unlock();
  15550. +preempt_en_rt:
  15551. + preempt_enable_rt();
  15552. return cpu;
  15553. }
  15554. /*
  15555. @@ -1089,6 +1134,11 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  15556. lockdep_assert_held(&p->pi_lock);
  15557. + if (__migrate_disabled(p)) {
  15558. + cpumask_copy(&p->cpus_allowed, new_mask);
  15559. + return;
  15560. + }
  15561. +
  15562. queued = task_on_rq_queued(p);
  15563. running = task_current(rq, p);
  15564. @@ -1111,6 +1161,84 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  15565. enqueue_task(rq, p, ENQUEUE_RESTORE);
  15566. }
  15567. +static DEFINE_PER_CPU(struct cpumask, sched_cpumasks);
  15568. +static DEFINE_MUTEX(sched_down_mutex);
  15569. +static cpumask_t sched_down_cpumask;
  15570. +
  15571. +void tell_sched_cpu_down_begin(int cpu)
  15572. +{
  15573. + mutex_lock(&sched_down_mutex);
  15574. + cpumask_set_cpu(cpu, &sched_down_cpumask);
  15575. + mutex_unlock(&sched_down_mutex);
  15576. +}
  15577. +
  15578. +void tell_sched_cpu_down_done(int cpu)
  15579. +{
  15580. + mutex_lock(&sched_down_mutex);
  15581. + cpumask_clear_cpu(cpu, &sched_down_cpumask);
  15582. + mutex_unlock(&sched_down_mutex);
  15583. +}
  15584. +
  15585. +/**
  15586. + * migrate_me - try to move the current task off this cpu
  15587. + *
  15588. + * Used by the pin_current_cpu() code to try to get tasks
  15589. + * to move off the current CPU as it is going down.
  15590. + * It will only move the task if the task isn't pinned to
  15591. + * the CPU (with migrate_disable, affinity or NO_SETAFFINITY)
  15592. + * and the task has to be in a RUNNING state. Otherwise the
  15593. + * movement of the task will wake it up (change its state
  15594. + * to running) when the task did not expect it.
  15595. + *
  15596. + * Returns 1 if it succeeded in moving the current task
  15597. + * 0 otherwise.
  15598. + */
  15599. +int migrate_me(void)
  15600. +{
  15601. + struct task_struct *p = current;
  15602. + struct migration_arg arg;
  15603. + struct cpumask *cpumask;
  15604. + struct cpumask *mask;
  15605. + unsigned int dest_cpu;
  15606. + struct rq_flags rf;
  15607. + struct rq *rq;
  15608. +
  15609. + /*
  15610. + * We can not migrate tasks bounded to a CPU or tasks not
  15611. + * running. The movement of the task will wake it up.
  15612. + */
  15613. + if (p->flags & PF_NO_SETAFFINITY || p->state)
  15614. + return 0;
  15615. +
  15616. + mutex_lock(&sched_down_mutex);
  15617. + rq = task_rq_lock(p, &rf);
  15618. +
  15619. + cpumask = this_cpu_ptr(&sched_cpumasks);
  15620. + mask = &p->cpus_allowed;
  15621. +
  15622. + cpumask_andnot(cpumask, mask, &sched_down_cpumask);
  15623. +
  15624. + if (!cpumask_weight(cpumask)) {
  15625. + /* It's only on this CPU? */
  15626. + task_rq_unlock(rq, p, &rf);
  15627. + mutex_unlock(&sched_down_mutex);
  15628. + return 0;
  15629. + }
  15630. +
  15631. + dest_cpu = cpumask_any_and(cpu_active_mask, cpumask);
  15632. +
  15633. + arg.task = p;
  15634. + arg.dest_cpu = dest_cpu;
  15635. +
  15636. + task_rq_unlock(rq, p, &rf);
  15637. +
  15638. + stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  15639. + tlb_migrate_finish(p->mm);
  15640. + mutex_unlock(&sched_down_mutex);
  15641. +
  15642. + return 1;
  15643. +}
  15644. +
  15645. /*
  15646. * Change a given task's CPU affinity. Migrate the thread to a
  15647. * proper CPU and schedule it away if the CPU it's executing on
  15648. @@ -1168,7 +1296,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
  15649. }
  15650. /* Can the task run on the task's current CPU? If so, we're done */
  15651. - if (cpumask_test_cpu(task_cpu(p), new_mask))
  15652. + if (cpumask_test_cpu(task_cpu(p), new_mask) || __migrate_disabled(p))
  15653. goto out;
  15654. dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
  15655. @@ -1355,6 +1483,18 @@ int migrate_swap(struct task_struct *cur, struct task_struct *p)
  15656. return ret;
  15657. }
  15658. +static bool check_task_state(struct task_struct *p, long match_state)
  15659. +{
  15660. + bool match = false;
  15661. +
  15662. + raw_spin_lock_irq(&p->pi_lock);
  15663. + if (p->state == match_state || p->saved_state == match_state)
  15664. + match = true;
  15665. + raw_spin_unlock_irq(&p->pi_lock);
  15666. +
  15667. + return match;
  15668. +}
  15669. +
  15670. /*
  15671. * wait_task_inactive - wait for a thread to unschedule.
  15672. *
  15673. @@ -1399,7 +1539,7 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  15674. * is actually now running somewhere else!
  15675. */
  15676. while (task_running(rq, p)) {
  15677. - if (match_state && unlikely(p->state != match_state))
  15678. + if (match_state && !check_task_state(p, match_state))
  15679. return 0;
  15680. cpu_relax();
  15681. }
  15682. @@ -1414,7 +1554,8 @@ unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  15683. running = task_running(rq, p);
  15684. queued = task_on_rq_queued(p);
  15685. ncsw = 0;
  15686. - if (!match_state || p->state == match_state)
  15687. + if (!match_state || p->state == match_state ||
  15688. + p->saved_state == match_state)
  15689. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  15690. task_rq_unlock(rq, p, &rf);
  15691. @@ -1670,10 +1811,6 @@ static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_fl
  15692. {
  15693. activate_task(rq, p, en_flags);
  15694. p->on_rq = TASK_ON_RQ_QUEUED;
  15695. -
  15696. - /* if a worker is waking up, notify workqueue */
  15697. - if (p->flags & PF_WQ_WORKER)
  15698. - wq_worker_waking_up(p, cpu_of(rq));
  15699. }
  15700. /*
  15701. @@ -2008,8 +2145,27 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  15702. */
  15703. smp_mb__before_spinlock();
  15704. raw_spin_lock_irqsave(&p->pi_lock, flags);
  15705. - if (!(p->state & state))
  15706. + if (!(p->state & state)) {
  15707. + /*
  15708. + * The task might be running due to a spinlock sleeper
  15709. + * wakeup. Check the saved state and set it to running
  15710. + * if the wakeup condition is true.
  15711. + */
  15712. + if (!(wake_flags & WF_LOCK_SLEEPER)) {
  15713. + if (p->saved_state & state) {
  15714. + p->saved_state = TASK_RUNNING;
  15715. + success = 1;
  15716. + }
  15717. + }
  15718. goto out;
  15719. + }
  15720. +
  15721. + /*
  15722. + * If this is a regular wakeup, then we can unconditionally
  15723. + * clear the saved state of a "lock sleeper".
  15724. + */
  15725. + if (!(wake_flags & WF_LOCK_SLEEPER))
  15726. + p->saved_state = TASK_RUNNING;
  15727. trace_sched_waking(p);
  15728. @@ -2093,53 +2249,6 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  15729. }
  15730. /**
  15731. - * try_to_wake_up_local - try to wake up a local task with rq lock held
  15732. - * @p: the thread to be awakened
  15733. - *
  15734. - * Put @p on the run-queue if it's not already there. The caller must
  15735. - * ensure that this_rq() is locked, @p is bound to this_rq() and not
  15736. - * the current task.
  15737. - */
  15738. -static void try_to_wake_up_local(struct task_struct *p, struct pin_cookie cookie)
  15739. -{
  15740. - struct rq *rq = task_rq(p);
  15741. -
  15742. - if (WARN_ON_ONCE(rq != this_rq()) ||
  15743. - WARN_ON_ONCE(p == current))
  15744. - return;
  15745. -
  15746. - lockdep_assert_held(&rq->lock);
  15747. -
  15748. - if (!raw_spin_trylock(&p->pi_lock)) {
  15749. - /*
  15750. - * This is OK, because current is on_cpu, which avoids it being
  15751. - * picked for load-balance and preemption/IRQs are still
  15752. - * disabled avoiding further scheduler activity on it and we've
  15753. - * not yet picked a replacement task.
  15754. - */
  15755. - lockdep_unpin_lock(&rq->lock, cookie);
  15756. - raw_spin_unlock(&rq->lock);
  15757. - raw_spin_lock(&p->pi_lock);
  15758. - raw_spin_lock(&rq->lock);
  15759. - lockdep_repin_lock(&rq->lock, cookie);
  15760. - }
  15761. -
  15762. - if (!(p->state & TASK_NORMAL))
  15763. - goto out;
  15764. -
  15765. - trace_sched_waking(p);
  15766. -
  15767. - if (!task_on_rq_queued(p))
  15768. - ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  15769. -
  15770. - ttwu_do_wakeup(rq, p, 0, cookie);
  15771. - if (schedstat_enabled())
  15772. - ttwu_stat(p, smp_processor_id(), 0);
  15773. -out:
  15774. - raw_spin_unlock(&p->pi_lock);
  15775. -}
  15776. -
  15777. -/**
  15778. * wake_up_process - Wake up a specific process
  15779. * @p: The process to be woken up.
  15780. *
  15781. @@ -2157,6 +2266,18 @@ int wake_up_process(struct task_struct *p)
  15782. }
  15783. EXPORT_SYMBOL(wake_up_process);
  15784. +/**
  15785. + * wake_up_lock_sleeper - Wake up a specific process blocked on a "sleeping lock"
  15786. + * @p: The process to be woken up.
  15787. + *
  15788. + * Same as wake_up_process() above, but wake_flags=WF_LOCK_SLEEPER to indicate
  15789. + * the nature of the wakeup.
  15790. + */
  15791. +int wake_up_lock_sleeper(struct task_struct *p)
  15792. +{
  15793. + return try_to_wake_up(p, TASK_ALL, WF_LOCK_SLEEPER);
  15794. +}
  15795. +
  15796. int wake_up_state(struct task_struct *p, unsigned int state)
  15797. {
  15798. return try_to_wake_up(p, state, 0);
  15799. @@ -2433,6 +2554,9 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)
  15800. p->on_cpu = 0;
  15801. #endif
  15802. init_task_preempt_count(p);
  15803. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  15804. + task_thread_info(p)->preempt_lazy_count = 0;
  15805. +#endif
  15806. #ifdef CONFIG_SMP
  15807. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  15808. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  15809. @@ -2761,8 +2885,12 @@ static struct rq *finish_task_switch(struct task_struct *prev)
  15810. finish_arch_post_lock_switch();
  15811. fire_sched_in_preempt_notifiers(current);
  15812. + /*
  15813. + * We use mmdrop_delayed() here so we don't have to do the
  15814. + * full __mmdrop() when we are the last user.
  15815. + */
  15816. if (mm)
  15817. - mmdrop(mm);
  15818. + mmdrop_delayed(mm);
  15819. if (unlikely(prev_state == TASK_DEAD)) {
  15820. if (prev->sched_class->task_dead)
  15821. prev->sched_class->task_dead(prev);
  15822. @@ -3237,6 +3365,77 @@ static inline void schedule_debug(struct task_struct *prev)
  15823. schedstat_inc(this_rq(), sched_count);
  15824. }
  15825. +#if defined(CONFIG_PREEMPT_RT_FULL) && defined(CONFIG_SMP)
  15826. +
  15827. +void migrate_disable(void)
  15828. +{
  15829. + struct task_struct *p = current;
  15830. +
  15831. + if (in_atomic() || irqs_disabled()) {
  15832. +#ifdef CONFIG_SCHED_DEBUG
  15833. + p->migrate_disable_atomic++;
  15834. +#endif
  15835. + return;
  15836. + }
  15837. +
  15838. +#ifdef CONFIG_SCHED_DEBUG
  15839. + if (unlikely(p->migrate_disable_atomic)) {
  15840. + tracing_off();
  15841. + WARN_ON_ONCE(1);
  15842. + }
  15843. +#endif
  15844. +
  15845. + if (p->migrate_disable) {
  15846. + p->migrate_disable++;
  15847. + return;
  15848. + }
  15849. +
  15850. + preempt_disable();
  15851. + preempt_lazy_disable();
  15852. + pin_current_cpu();
  15853. + p->migrate_disable = 1;
  15854. + preempt_enable();
  15855. +}
  15856. +EXPORT_SYMBOL(migrate_disable);
  15857. +
  15858. +void migrate_enable(void)
  15859. +{
  15860. + struct task_struct *p = current;
  15861. +
  15862. + if (in_atomic() || irqs_disabled()) {
  15863. +#ifdef CONFIG_SCHED_DEBUG
  15864. + p->migrate_disable_atomic--;
  15865. +#endif
  15866. + return;
  15867. + }
  15868. +
  15869. +#ifdef CONFIG_SCHED_DEBUG
  15870. + if (unlikely(p->migrate_disable_atomic)) {
  15871. + tracing_off();
  15872. + WARN_ON_ONCE(1);
  15873. + }
  15874. +#endif
  15875. + WARN_ON_ONCE(p->migrate_disable <= 0);
  15876. +
  15877. + if (p->migrate_disable > 1) {
  15878. + p->migrate_disable--;
  15879. + return;
  15880. + }
  15881. +
  15882. + preempt_disable();
  15883. + /*
  15884. + * Clearing migrate_disable causes tsk_cpus_allowed to
  15885. + * show the tasks original cpu affinity.
  15886. + */
  15887. + p->migrate_disable = 0;
  15888. +
  15889. + unpin_current_cpu();
  15890. + preempt_enable();
  15891. + preempt_lazy_enable();
  15892. +}
  15893. +EXPORT_SYMBOL(migrate_enable);
  15894. +#endif
  15895. +
  15896. /*
  15897. * Pick up the highest-prio task:
  15898. */
  15899. @@ -3364,19 +3563,6 @@ static void __sched notrace __schedule(bool preempt)
  15900. } else {
  15901. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  15902. prev->on_rq = 0;
  15903. -
  15904. - /*
  15905. - * If a worker went to sleep, notify and ask workqueue
  15906. - * whether it wants to wake up a task to maintain
  15907. - * concurrency.
  15908. - */
  15909. - if (prev->flags & PF_WQ_WORKER) {
  15910. - struct task_struct *to_wakeup;
  15911. -
  15912. - to_wakeup = wq_worker_sleeping(prev);
  15913. - if (to_wakeup)
  15914. - try_to_wake_up_local(to_wakeup, cookie);
  15915. - }
  15916. }
  15917. switch_count = &prev->nvcsw;
  15918. }
  15919. @@ -3386,6 +3572,7 @@ static void __sched notrace __schedule(bool preempt)
  15920. next = pick_next_task(rq, prev, cookie);
  15921. clear_tsk_need_resched(prev);
  15922. + clear_tsk_need_resched_lazy(prev);
  15923. clear_preempt_need_resched();
  15924. rq->clock_skip_update = 0;
  15925. @@ -3407,9 +3594,20 @@ STACK_FRAME_NON_STANDARD(__schedule); /* switch_to() */
  15926. static inline void sched_submit_work(struct task_struct *tsk)
  15927. {
  15928. - if (!tsk->state || tsk_is_pi_blocked(tsk))
  15929. + if (!tsk->state)
  15930. return;
  15931. /*
  15932. + * If a worker went to sleep, notify and ask workqueue whether
  15933. + * it wants to wake up a task to maintain concurrency.
  15934. + */
  15935. + if (tsk->flags & PF_WQ_WORKER)
  15936. + wq_worker_sleeping(tsk);
  15937. +
  15938. +
  15939. + if (tsk_is_pi_blocked(tsk))
  15940. + return;
  15941. +
  15942. + /*
  15943. * If we are going to sleep and we have plugged IO queued,
  15944. * make sure to submit it to avoid deadlocks.
  15945. */
  15946. @@ -3417,6 +3615,12 @@ static inline void sched_submit_work(struct task_struct *tsk)
  15947. blk_schedule_flush_plug(tsk);
  15948. }
  15949. +static void sched_update_worker(struct task_struct *tsk)
  15950. +{
  15951. + if (tsk->flags & PF_WQ_WORKER)
  15952. + wq_worker_running(tsk);
  15953. +}
  15954. +
  15955. asmlinkage __visible void __sched schedule(void)
  15956. {
  15957. struct task_struct *tsk = current;
  15958. @@ -3427,6 +3631,7 @@ asmlinkage __visible void __sched schedule(void)
  15959. __schedule(false);
  15960. sched_preempt_enable_no_resched();
  15961. } while (need_resched());
  15962. + sched_update_worker(tsk);
  15963. }
  15964. EXPORT_SYMBOL(schedule);
  15965. @@ -3490,6 +3695,30 @@ static void __sched notrace preempt_schedule_common(void)
  15966. } while (need_resched());
  15967. }
  15968. +#ifdef CONFIG_PREEMPT_LAZY
  15969. +/*
  15970. + * If TIF_NEED_RESCHED is then we allow to be scheduled away since this is
  15971. + * set by a RT task. Oterwise we try to avoid beeing scheduled out as long as
  15972. + * preempt_lazy_count counter >0.
  15973. + */
  15974. +static __always_inline int preemptible_lazy(void)
  15975. +{
  15976. + if (test_thread_flag(TIF_NEED_RESCHED))
  15977. + return 1;
  15978. + if (current_thread_info()->preempt_lazy_count)
  15979. + return 0;
  15980. + return 1;
  15981. +}
  15982. +
  15983. +#else
  15984. +
  15985. +static inline int preemptible_lazy(void)
  15986. +{
  15987. + return 1;
  15988. +}
  15989. +
  15990. +#endif
  15991. +
  15992. #ifdef CONFIG_PREEMPT
  15993. /*
  15994. * this is the entry point to schedule() from in-kernel preemption
  15995. @@ -3504,7 +3733,8 @@ asmlinkage __visible void __sched notrace preempt_schedule(void)
  15996. */
  15997. if (likely(!preemptible()))
  15998. return;
  15999. -
  16000. + if (!preemptible_lazy())
  16001. + return;
  16002. preempt_schedule_common();
  16003. }
  16004. NOKPROBE_SYMBOL(preempt_schedule);
  16005. @@ -3531,6 +3761,9 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  16006. if (likely(!preemptible()))
  16007. return;
  16008. + if (!preemptible_lazy())
  16009. + return;
  16010. +
  16011. do {
  16012. /*
  16013. * Because the function tracer can trace preempt_count_sub()
  16014. @@ -3553,7 +3786,16 @@ asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  16015. * an infinite recursion.
  16016. */
  16017. prev_ctx = exception_enter();
  16018. + /*
  16019. + * The add/subtract must not be traced by the function
  16020. + * tracer. But we still want to account for the
  16021. + * preempt off latency tracer. Since the _notrace versions
  16022. + * of add/subtract skip the accounting for latency tracer
  16023. + * we must force it manually.
  16024. + */
  16025. + start_critical_timings();
  16026. __schedule(true);
  16027. + stop_critical_timings();
  16028. exception_exit(prev_ctx);
  16029. preempt_latency_stop(1);
  16030. @@ -4901,6 +5143,7 @@ int __cond_resched_lock(spinlock_t *lock)
  16031. }
  16032. EXPORT_SYMBOL(__cond_resched_lock);
  16033. +#ifndef CONFIG_PREEMPT_RT_FULL
  16034. int __sched __cond_resched_softirq(void)
  16035. {
  16036. BUG_ON(!in_softirq());
  16037. @@ -4914,6 +5157,7 @@ int __sched __cond_resched_softirq(void)
  16038. return 0;
  16039. }
  16040. EXPORT_SYMBOL(__cond_resched_softirq);
  16041. +#endif
  16042. /**
  16043. * yield - yield the current processor to other threads.
  16044. @@ -5283,7 +5527,9 @@ void init_idle(struct task_struct *idle, int cpu)
  16045. /* Set the preempt count _outside_ the spinlocks! */
  16046. init_idle_preempt_count(idle, cpu);
  16047. -
  16048. +#ifdef CONFIG_HAVE_PREEMPT_LAZY
  16049. + task_thread_info(idle)->preempt_lazy_count = 0;
  16050. +#endif
  16051. /*
  16052. * The idle tasks have their own, simple scheduling class:
  16053. */
  16054. @@ -5426,6 +5672,8 @@ void sched_setnuma(struct task_struct *p, int nid)
  16055. #endif /* CONFIG_NUMA_BALANCING */
  16056. #ifdef CONFIG_HOTPLUG_CPU
  16057. +static DEFINE_PER_CPU(struct mm_struct *, idle_last_mm);
  16058. +
  16059. /*
  16060. * Ensures that the idle task is using init_mm right before its cpu goes
  16061. * offline.
  16062. @@ -5440,7 +5688,12 @@ void idle_task_exit(void)
  16063. switch_mm_irqs_off(mm, &init_mm, current);
  16064. finish_arch_post_lock_switch();
  16065. }
  16066. - mmdrop(mm);
  16067. + /*
  16068. + * Defer the cleanup to an alive cpu. On RT we can neither
  16069. + * call mmdrop() nor mmdrop_delayed() from here.
  16070. + */
  16071. + per_cpu(idle_last_mm, smp_processor_id()) = mm;
  16072. +
  16073. }
  16074. /*
  16075. @@ -7315,6 +7568,10 @@ int sched_cpu_dying(unsigned int cpu)
  16076. update_max_interval();
  16077. nohz_balance_exit_idle(cpu);
  16078. hrtick_clear(rq);
  16079. + if (per_cpu(idle_last_mm, cpu)) {
  16080. + mmdrop_delayed(per_cpu(idle_last_mm, cpu));
  16081. + per_cpu(idle_last_mm, cpu) = NULL;
  16082. + }
  16083. return 0;
  16084. }
  16085. #endif
  16086. @@ -7566,7 +7823,7 @@ void __init sched_init(void)
  16087. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  16088. static inline int preempt_count_equals(int preempt_offset)
  16089. {
  16090. - int nested = preempt_count() + rcu_preempt_depth();
  16091. + int nested = preempt_count() + sched_rcu_preempt_depth();
  16092. return (nested == preempt_offset);
  16093. }
  16094. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
  16095. index 1ce8867283dc..766da04b06a0 100644
  16096. --- a/kernel/sched/deadline.c
  16097. +++ b/kernel/sched/deadline.c
  16098. @@ -697,6 +697,7 @@ void init_dl_task_timer(struct sched_dl_entity *dl_se)
  16099. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16100. timer->function = dl_task_timer;
  16101. + timer->irqsafe = 1;
  16102. }
  16103. static
  16104. diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
  16105. index 2a0a9995256d..48a9b6f57249 100644
  16106. --- a/kernel/sched/debug.c
  16107. +++ b/kernel/sched/debug.c
  16108. @@ -552,6 +552,9 @@ void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
  16109. P(rt_throttled);
  16110. PN(rt_time);
  16111. PN(rt_runtime);
  16112. +#ifdef CONFIG_SMP
  16113. + P(rt_nr_migratory);
  16114. +#endif
  16115. #undef PN
  16116. #undef P
  16117. @@ -947,6 +950,10 @@ void proc_sched_show_task(struct task_struct *p, struct seq_file *m)
  16118. #endif
  16119. P(policy);
  16120. P(prio);
  16121. +#ifdef CONFIG_PREEMPT_RT_FULL
  16122. + P(migrate_disable);
  16123. +#endif
  16124. + P(nr_cpus_allowed);
  16125. #undef PN
  16126. #undef __PN
  16127. #undef P
  16128. diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
  16129. index 8b3610c871f2..1145079af264 100644
  16130. --- a/kernel/sched/fair.c
  16131. +++ b/kernel/sched/fair.c
  16132. @@ -3508,7 +3508,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  16133. ideal_runtime = sched_slice(cfs_rq, curr);
  16134. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  16135. if (delta_exec > ideal_runtime) {
  16136. - resched_curr(rq_of(cfs_rq));
  16137. + resched_curr_lazy(rq_of(cfs_rq));
  16138. /*
  16139. * The current task ran long enough, ensure it doesn't get
  16140. * re-elected due to buddy favours.
  16141. @@ -3532,7 +3532,7 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  16142. return;
  16143. if (delta > ideal_runtime)
  16144. - resched_curr(rq_of(cfs_rq));
  16145. + resched_curr_lazy(rq_of(cfs_rq));
  16146. }
  16147. static void
  16148. @@ -3677,7 +3677,7 @@ entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  16149. * validating it and just reschedule.
  16150. */
  16151. if (queued) {
  16152. - resched_curr(rq_of(cfs_rq));
  16153. + resched_curr_lazy(rq_of(cfs_rq));
  16154. return;
  16155. }
  16156. /*
  16157. @@ -3859,7 +3859,7 @@ static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, u64 delta_exec)
  16158. * hierarchy can be throttled
  16159. */
  16160. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  16161. - resched_curr(rq_of(cfs_rq));
  16162. + resched_curr_lazy(rq_of(cfs_rq));
  16163. }
  16164. static __always_inline
  16165. @@ -4487,7 +4487,7 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  16166. if (delta < 0) {
  16167. if (rq->curr == p)
  16168. - resched_curr(rq);
  16169. + resched_curr_lazy(rq);
  16170. return;
  16171. }
  16172. hrtick_start(rq, delta);
  16173. @@ -5676,7 +5676,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
  16174. return;
  16175. preempt:
  16176. - resched_curr(rq);
  16177. + resched_curr_lazy(rq);
  16178. /*
  16179. * Only set the backward buddy when the current task is still
  16180. * on the rq. This can happen when a wakeup gets interleaved
  16181. @@ -8402,7 +8402,7 @@ static void task_fork_fair(struct task_struct *p)
  16182. * 'current' within the tree based on its new key value.
  16183. */
  16184. swap(curr->vruntime, se->vruntime);
  16185. - resched_curr(rq);
  16186. + resched_curr_lazy(rq);
  16187. }
  16188. se->vruntime -= cfs_rq->min_vruntime;
  16189. @@ -8426,7 +8426,7 @@ prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  16190. */
  16191. if (rq->curr == p) {
  16192. if (p->prio > oldprio)
  16193. - resched_curr(rq);
  16194. + resched_curr_lazy(rq);
  16195. } else
  16196. check_preempt_curr(rq, p, 0);
  16197. }
  16198. diff --git a/kernel/sched/features.h b/kernel/sched/features.h
  16199. index 69631fa46c2f..6d28fcd08872 100644
  16200. --- a/kernel/sched/features.h
  16201. +++ b/kernel/sched/features.h
  16202. @@ -45,11 +45,19 @@ SCHED_FEAT(LB_BIAS, true)
  16203. */
  16204. SCHED_FEAT(NONTASK_CAPACITY, true)
  16205. +#ifdef CONFIG_PREEMPT_RT_FULL
  16206. +SCHED_FEAT(TTWU_QUEUE, false)
  16207. +# ifdef CONFIG_PREEMPT_LAZY
  16208. +SCHED_FEAT(PREEMPT_LAZY, true)
  16209. +# endif
  16210. +#else
  16211. +
  16212. /*
  16213. * Queue remote wakeups on the target CPU and process them
  16214. * using the scheduler IPI. Reduces rq->lock contention/bounces.
  16215. */
  16216. SCHED_FEAT(TTWU_QUEUE, true)
  16217. +#endif
  16218. #ifdef HAVE_RT_PUSH_IPI
  16219. /*
  16220. diff --git a/kernel/sched/rt.c b/kernel/sched/rt.c
  16221. index d5690b722691..731cd0e98c15 100644
  16222. --- a/kernel/sched/rt.c
  16223. +++ b/kernel/sched/rt.c
  16224. @@ -47,6 +47,7 @@ void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  16225. hrtimer_init(&rt_b->rt_period_timer,
  16226. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  16227. + rt_b->rt_period_timer.irqsafe = 1;
  16228. rt_b->rt_period_timer.function = sched_rt_period_timer;
  16229. }
  16230. @@ -101,6 +102,7 @@ void init_rt_rq(struct rt_rq *rt_rq)
  16231. rt_rq->push_cpu = nr_cpu_ids;
  16232. raw_spin_lock_init(&rt_rq->push_lock);
  16233. init_irq_work(&rt_rq->push_work, push_irq_work_func);
  16234. + rt_rq->push_work.flags |= IRQ_WORK_HARD_IRQ;
  16235. #endif
  16236. #endif /* CONFIG_SMP */
  16237. /* We start is dequeued state, because no RT tasks are queued */
  16238. diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
  16239. index c64fc5114004..af58f9b3ece4 100644
  16240. --- a/kernel/sched/sched.h
  16241. +++ b/kernel/sched/sched.h
  16242. @@ -1138,6 +1138,7 @@ static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  16243. #define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
  16244. #define WF_FORK 0x02 /* child wakeup after fork */
  16245. #define WF_MIGRATED 0x4 /* internal use, task got migrated */
  16246. +#define WF_LOCK_SLEEPER 0x08 /* wakeup spinlock "sleeper" */
  16247. /*
  16248. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  16249. @@ -1316,6 +1317,15 @@ extern void init_sched_fair_class(void);
  16250. extern void resched_curr(struct rq *rq);
  16251. extern void resched_cpu(int cpu);
  16252. +#ifdef CONFIG_PREEMPT_LAZY
  16253. +extern void resched_curr_lazy(struct rq *rq);
  16254. +#else
  16255. +static inline void resched_curr_lazy(struct rq *rq)
  16256. +{
  16257. + resched_curr(rq);
  16258. +}
  16259. +#endif
  16260. +
  16261. extern struct rt_bandwidth def_rt_bandwidth;
  16262. extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
  16263. diff --git a/kernel/sched/swait.c b/kernel/sched/swait.c
  16264. index 82f0dff90030..ef027ff3250a 100644
  16265. --- a/kernel/sched/swait.c
  16266. +++ b/kernel/sched/swait.c
  16267. @@ -1,5 +1,6 @@
  16268. #include <linux/sched.h>
  16269. #include <linux/swait.h>
  16270. +#include <linux/suspend.h>
  16271. void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  16272. struct lock_class_key *key)
  16273. @@ -29,6 +30,25 @@ void swake_up_locked(struct swait_queue_head *q)
  16274. }
  16275. EXPORT_SYMBOL(swake_up_locked);
  16276. +void swake_up_all_locked(struct swait_queue_head *q)
  16277. +{
  16278. + struct swait_queue *curr;
  16279. + int wakes = 0;
  16280. +
  16281. + while (!list_empty(&q->task_list)) {
  16282. +
  16283. + curr = list_first_entry(&q->task_list, typeof(*curr),
  16284. + task_list);
  16285. + wake_up_process(curr->task);
  16286. + list_del_init(&curr->task_list);
  16287. + wakes++;
  16288. + }
  16289. + if (pm_in_action)
  16290. + return;
  16291. + WARN(wakes > 2, "complete_all() with %d waiters\n", wakes);
  16292. +}
  16293. +EXPORT_SYMBOL(swake_up_all_locked);
  16294. +
  16295. void swake_up(struct swait_queue_head *q)
  16296. {
  16297. unsigned long flags;
  16298. @@ -54,6 +74,7 @@ void swake_up_all(struct swait_queue_head *q)
  16299. if (!swait_active(q))
  16300. return;
  16301. + WARN_ON(irqs_disabled());
  16302. raw_spin_lock_irq(&q->lock);
  16303. list_splice_init(&q->task_list, &tmp);
  16304. while (!list_empty(&tmp)) {
  16305. diff --git a/kernel/sched/swork.c b/kernel/sched/swork.c
  16306. new file mode 100644
  16307. index 000000000000..1950f40ca725
  16308. --- /dev/null
  16309. +++ b/kernel/sched/swork.c
  16310. @@ -0,0 +1,173 @@
  16311. +/*
  16312. + * Copyright (C) 2014 BMW Car IT GmbH, Daniel Wagner daniel.wagner@bmw-carit.de
  16313. + *
  16314. + * Provides a framework for enqueuing callbacks from irq context
  16315. + * PREEMPT_RT_FULL safe. The callbacks are executed in kthread context.
  16316. + */
  16317. +
  16318. +#include <linux/swait.h>
  16319. +#include <linux/swork.h>
  16320. +#include <linux/kthread.h>
  16321. +#include <linux/slab.h>
  16322. +#include <linux/spinlock.h>
  16323. +#include <linux/export.h>
  16324. +
  16325. +#define SWORK_EVENT_PENDING (1 << 0)
  16326. +
  16327. +static DEFINE_MUTEX(worker_mutex);
  16328. +static struct sworker *glob_worker;
  16329. +
  16330. +struct sworker {
  16331. + struct list_head events;
  16332. + struct swait_queue_head wq;
  16333. +
  16334. + raw_spinlock_t lock;
  16335. +
  16336. + struct task_struct *task;
  16337. + int refs;
  16338. +};
  16339. +
  16340. +static bool swork_readable(struct sworker *worker)
  16341. +{
  16342. + bool r;
  16343. +
  16344. + if (kthread_should_stop())
  16345. + return true;
  16346. +
  16347. + raw_spin_lock_irq(&worker->lock);
  16348. + r = !list_empty(&worker->events);
  16349. + raw_spin_unlock_irq(&worker->lock);
  16350. +
  16351. + return r;
  16352. +}
  16353. +
  16354. +static int swork_kthread(void *arg)
  16355. +{
  16356. + struct sworker *worker = arg;
  16357. +
  16358. + for (;;) {
  16359. + swait_event_interruptible(worker->wq,
  16360. + swork_readable(worker));
  16361. + if (kthread_should_stop())
  16362. + break;
  16363. +
  16364. + raw_spin_lock_irq(&worker->lock);
  16365. + while (!list_empty(&worker->events)) {
  16366. + struct swork_event *sev;
  16367. +
  16368. + sev = list_first_entry(&worker->events,
  16369. + struct swork_event, item);
  16370. + list_del(&sev->item);
  16371. + raw_spin_unlock_irq(&worker->lock);
  16372. +
  16373. + WARN_ON_ONCE(!test_and_clear_bit(SWORK_EVENT_PENDING,
  16374. + &sev->flags));
  16375. + sev->func(sev);
  16376. + raw_spin_lock_irq(&worker->lock);
  16377. + }
  16378. + raw_spin_unlock_irq(&worker->lock);
  16379. + }
  16380. + return 0;
  16381. +}
  16382. +
  16383. +static struct sworker *swork_create(void)
  16384. +{
  16385. + struct sworker *worker;
  16386. +
  16387. + worker = kzalloc(sizeof(*worker), GFP_KERNEL);
  16388. + if (!worker)
  16389. + return ERR_PTR(-ENOMEM);
  16390. +
  16391. + INIT_LIST_HEAD(&worker->events);
  16392. + raw_spin_lock_init(&worker->lock);
  16393. + init_swait_queue_head(&worker->wq);
  16394. +
  16395. + worker->task = kthread_run(swork_kthread, worker, "kswork");
  16396. + if (IS_ERR(worker->task)) {
  16397. + kfree(worker);
  16398. + return ERR_PTR(-ENOMEM);
  16399. + }
  16400. +
  16401. + return worker;
  16402. +}
  16403. +
  16404. +static void swork_destroy(struct sworker *worker)
  16405. +{
  16406. + kthread_stop(worker->task);
  16407. +
  16408. + WARN_ON(!list_empty(&worker->events));
  16409. + kfree(worker);
  16410. +}
  16411. +
  16412. +/**
  16413. + * swork_queue - queue swork
  16414. + *
  16415. + * Returns %false if @work was already on a queue, %true otherwise.
  16416. + *
  16417. + * The work is queued and processed on a random CPU
  16418. + */
  16419. +bool swork_queue(struct swork_event *sev)
  16420. +{
  16421. + unsigned long flags;
  16422. +
  16423. + if (test_and_set_bit(SWORK_EVENT_PENDING, &sev->flags))
  16424. + return false;
  16425. +
  16426. + raw_spin_lock_irqsave(&glob_worker->lock, flags);
  16427. + list_add_tail(&sev->item, &glob_worker->events);
  16428. + raw_spin_unlock_irqrestore(&glob_worker->lock, flags);
  16429. +
  16430. + swake_up(&glob_worker->wq);
  16431. + return true;
  16432. +}
  16433. +EXPORT_SYMBOL_GPL(swork_queue);
  16434. +
  16435. +/**
  16436. + * swork_get - get an instance of the sworker
  16437. + *
  16438. + * Returns an negative error code if the initialization if the worker did not
  16439. + * work, %0 otherwise.
  16440. + *
  16441. + */
  16442. +int swork_get(void)
  16443. +{
  16444. + struct sworker *worker;
  16445. +
  16446. + mutex_lock(&worker_mutex);
  16447. + if (!glob_worker) {
  16448. + worker = swork_create();
  16449. + if (IS_ERR(worker)) {
  16450. + mutex_unlock(&worker_mutex);
  16451. + return -ENOMEM;
  16452. + }
  16453. +
  16454. + glob_worker = worker;
  16455. + }
  16456. +
  16457. + glob_worker->refs++;
  16458. + mutex_unlock(&worker_mutex);
  16459. +
  16460. + return 0;
  16461. +}
  16462. +EXPORT_SYMBOL_GPL(swork_get);
  16463. +
  16464. +/**
  16465. + * swork_put - puts an instance of the sworker
  16466. + *
  16467. + * Will destroy the sworker thread. This function must not be called until all
  16468. + * queued events have been completed.
  16469. + */
  16470. +void swork_put(void)
  16471. +{
  16472. + mutex_lock(&worker_mutex);
  16473. +
  16474. + glob_worker->refs--;
  16475. + if (glob_worker->refs > 0)
  16476. + goto out;
  16477. +
  16478. + swork_destroy(glob_worker);
  16479. + glob_worker = NULL;
  16480. +out:
  16481. + mutex_unlock(&worker_mutex);
  16482. +}
  16483. +EXPORT_SYMBOL_GPL(swork_put);
  16484. diff --git a/kernel/signal.c b/kernel/signal.c
  16485. index af21afc00d08..7ead97a43298 100644
  16486. --- a/kernel/signal.c
  16487. +++ b/kernel/signal.c
  16488. @@ -14,6 +14,7 @@
  16489. #include <linux/export.h>
  16490. #include <linux/init.h>
  16491. #include <linux/sched.h>
  16492. +#include <linux/sched/rt.h>
  16493. #include <linux/fs.h>
  16494. #include <linux/tty.h>
  16495. #include <linux/binfmts.h>
  16496. @@ -352,13 +353,30 @@ static bool task_participate_group_stop(struct task_struct *task)
  16497. return false;
  16498. }
  16499. +static inline struct sigqueue *get_task_cache(struct task_struct *t)
  16500. +{
  16501. + struct sigqueue *q = t->sigqueue_cache;
  16502. +
  16503. + if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
  16504. + return NULL;
  16505. + return q;
  16506. +}
  16507. +
  16508. +static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
  16509. +{
  16510. + if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
  16511. + return 0;
  16512. + return 1;
  16513. +}
  16514. +
  16515. /*
  16516. * allocate a new signal queue record
  16517. * - this may be called without locks if and only if t == current, otherwise an
  16518. * appropriate lock must be held to stop the target task from exiting
  16519. */
  16520. static struct sigqueue *
  16521. -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  16522. +__sigqueue_do_alloc(int sig, struct task_struct *t, gfp_t flags,
  16523. + int override_rlimit, int fromslab)
  16524. {
  16525. struct sigqueue *q = NULL;
  16526. struct user_struct *user;
  16527. @@ -375,7 +393,10 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  16528. if (override_rlimit ||
  16529. atomic_read(&user->sigpending) <=
  16530. task_rlimit(t, RLIMIT_SIGPENDING)) {
  16531. - q = kmem_cache_alloc(sigqueue_cachep, flags);
  16532. + if (!fromslab)
  16533. + q = get_task_cache(t);
  16534. + if (!q)
  16535. + q = kmem_cache_alloc(sigqueue_cachep, flags);
  16536. } else {
  16537. print_dropped_signal(sig);
  16538. }
  16539. @@ -392,6 +413,13 @@ __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimi
  16540. return q;
  16541. }
  16542. +static struct sigqueue *
  16543. +__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags,
  16544. + int override_rlimit)
  16545. +{
  16546. + return __sigqueue_do_alloc(sig, t, flags, override_rlimit, 0);
  16547. +}
  16548. +
  16549. static void __sigqueue_free(struct sigqueue *q)
  16550. {
  16551. if (q->flags & SIGQUEUE_PREALLOC)
  16552. @@ -401,6 +429,21 @@ static void __sigqueue_free(struct sigqueue *q)
  16553. kmem_cache_free(sigqueue_cachep, q);
  16554. }
  16555. +static void sigqueue_free_current(struct sigqueue *q)
  16556. +{
  16557. + struct user_struct *up;
  16558. +
  16559. + if (q->flags & SIGQUEUE_PREALLOC)
  16560. + return;
  16561. +
  16562. + up = q->user;
  16563. + if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
  16564. + atomic_dec(&up->sigpending);
  16565. + free_uid(up);
  16566. + } else
  16567. + __sigqueue_free(q);
  16568. +}
  16569. +
  16570. void flush_sigqueue(struct sigpending *queue)
  16571. {
  16572. struct sigqueue *q;
  16573. @@ -414,6 +457,21 @@ void flush_sigqueue(struct sigpending *queue)
  16574. }
  16575. /*
  16576. + * Called from __exit_signal. Flush tsk->pending and
  16577. + * tsk->sigqueue_cache
  16578. + */
  16579. +void flush_task_sigqueue(struct task_struct *tsk)
  16580. +{
  16581. + struct sigqueue *q;
  16582. +
  16583. + flush_sigqueue(&tsk->pending);
  16584. +
  16585. + q = get_task_cache(tsk);
  16586. + if (q)
  16587. + kmem_cache_free(sigqueue_cachep, q);
  16588. +}
  16589. +
  16590. +/*
  16591. * Flush all pending signals for this kthread.
  16592. */
  16593. void flush_signals(struct task_struct *t)
  16594. @@ -525,7 +583,7 @@ static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  16595. still_pending:
  16596. list_del_init(&first->list);
  16597. copy_siginfo(info, &first->info);
  16598. - __sigqueue_free(first);
  16599. + sigqueue_free_current(first);
  16600. } else {
  16601. /*
  16602. * Ok, it wasn't in the queue. This must be
  16603. @@ -560,6 +618,8 @@ int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  16604. {
  16605. int signr;
  16606. + WARN_ON_ONCE(tsk != current);
  16607. +
  16608. /* We only dequeue private signals from ourselves, we don't let
  16609. * signalfd steal them
  16610. */
  16611. @@ -1156,8 +1216,8 @@ int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  16612. * We don't want to have recursive SIGSEGV's etc, for example,
  16613. * that is why we also clear SIGNAL_UNKILLABLE.
  16614. */
  16615. -int
  16616. -force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  16617. +static int
  16618. +do_force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  16619. {
  16620. unsigned long int flags;
  16621. int ret, blocked, ignored;
  16622. @@ -1182,6 +1242,39 @@ force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  16623. return ret;
  16624. }
  16625. +int force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  16626. +{
  16627. +/*
  16628. + * On some archs, PREEMPT_RT has to delay sending a signal from a trap
  16629. + * since it can not enable preemption, and the signal code's spin_locks
  16630. + * turn into mutexes. Instead, it must set TIF_NOTIFY_RESUME which will
  16631. + * send the signal on exit of the trap.
  16632. + */
  16633. +#ifdef ARCH_RT_DELAYS_SIGNAL_SEND
  16634. + if (in_atomic()) {
  16635. + if (WARN_ON_ONCE(t != current))
  16636. + return 0;
  16637. + if (WARN_ON_ONCE(t->forced_info.si_signo))
  16638. + return 0;
  16639. +
  16640. + if (is_si_special(info)) {
  16641. + WARN_ON_ONCE(info != SEND_SIG_PRIV);
  16642. + t->forced_info.si_signo = sig;
  16643. + t->forced_info.si_errno = 0;
  16644. + t->forced_info.si_code = SI_KERNEL;
  16645. + t->forced_info.si_pid = 0;
  16646. + t->forced_info.si_uid = 0;
  16647. + } else {
  16648. + t->forced_info = *info;
  16649. + }
  16650. +
  16651. + set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  16652. + return 0;
  16653. + }
  16654. +#endif
  16655. + return do_force_sig_info(sig, info, t);
  16656. +}
  16657. +
  16658. /*
  16659. * Nuke all other threads in the group.
  16660. */
  16661. @@ -1216,12 +1309,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  16662. * Disable interrupts early to avoid deadlocks.
  16663. * See rcu_read_unlock() comment header for details.
  16664. */
  16665. - local_irq_save(*flags);
  16666. + local_irq_save_nort(*flags);
  16667. rcu_read_lock();
  16668. sighand = rcu_dereference(tsk->sighand);
  16669. if (unlikely(sighand == NULL)) {
  16670. rcu_read_unlock();
  16671. - local_irq_restore(*flags);
  16672. + local_irq_restore_nort(*flags);
  16673. break;
  16674. }
  16675. /*
  16676. @@ -1242,7 +1335,7 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  16677. }
  16678. spin_unlock(&sighand->siglock);
  16679. rcu_read_unlock();
  16680. - local_irq_restore(*flags);
  16681. + local_irq_restore_nort(*flags);
  16682. }
  16683. return sighand;
  16684. @@ -1485,7 +1578,8 @@ EXPORT_SYMBOL(kill_pid);
  16685. */
  16686. struct sigqueue *sigqueue_alloc(void)
  16687. {
  16688. - struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  16689. + /* Preallocated sigqueue objects always from the slabcache ! */
  16690. + struct sigqueue *q = __sigqueue_do_alloc(-1, current, GFP_KERNEL, 0, 1);
  16691. if (q)
  16692. q->flags |= SIGQUEUE_PREALLOC;
  16693. @@ -1846,15 +1940,7 @@ static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  16694. if (gstop_done && ptrace_reparented(current))
  16695. do_notify_parent_cldstop(current, false, why);
  16696. - /*
  16697. - * Don't want to allow preemption here, because
  16698. - * sys_ptrace() needs this task to be inactive.
  16699. - *
  16700. - * XXX: implement read_unlock_no_resched().
  16701. - */
  16702. - preempt_disable();
  16703. read_unlock(&tasklist_lock);
  16704. - preempt_enable_no_resched();
  16705. freezable_schedule();
  16706. } else {
  16707. /*
  16708. diff --git a/kernel/softirq.c b/kernel/softirq.c
  16709. index 17caf4b63342..a602b7152de7 100644
  16710. --- a/kernel/softirq.c
  16711. +++ b/kernel/softirq.c
  16712. @@ -21,10 +21,12 @@
  16713. #include <linux/freezer.h>
  16714. #include <linux/kthread.h>
  16715. #include <linux/rcupdate.h>
  16716. +#include <linux/delay.h>
  16717. #include <linux/ftrace.h>
  16718. #include <linux/smp.h>
  16719. #include <linux/smpboot.h>
  16720. #include <linux/tick.h>
  16721. +#include <linux/locallock.h>
  16722. #include <linux/irq.h>
  16723. #define CREATE_TRACE_POINTS
  16724. @@ -56,12 +58,108 @@ EXPORT_SYMBOL(irq_stat);
  16725. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  16726. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  16727. +#ifdef CONFIG_PREEMPT_RT_FULL
  16728. +#define TIMER_SOFTIRQS ((1 << TIMER_SOFTIRQ) | (1 << HRTIMER_SOFTIRQ))
  16729. +DEFINE_PER_CPU(struct task_struct *, ktimer_softirqd);
  16730. +#endif
  16731. const char * const softirq_to_name[NR_SOFTIRQS] = {
  16732. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  16733. "TASKLET", "SCHED", "HRTIMER", "RCU"
  16734. };
  16735. +#ifdef CONFIG_NO_HZ_COMMON
  16736. +# ifdef CONFIG_PREEMPT_RT_FULL
  16737. +
  16738. +struct softirq_runner {
  16739. + struct task_struct *runner[NR_SOFTIRQS];
  16740. +};
  16741. +
  16742. +static DEFINE_PER_CPU(struct softirq_runner, softirq_runners);
  16743. +
  16744. +static inline void softirq_set_runner(unsigned int sirq)
  16745. +{
  16746. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  16747. +
  16748. + sr->runner[sirq] = current;
  16749. +}
  16750. +
  16751. +static inline void softirq_clr_runner(unsigned int sirq)
  16752. +{
  16753. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  16754. +
  16755. + sr->runner[sirq] = NULL;
  16756. +}
  16757. +
  16758. +/*
  16759. + * On preempt-rt a softirq running context might be blocked on a
  16760. + * lock. There might be no other runnable task on this CPU because the
  16761. + * lock owner runs on some other CPU. So we have to go into idle with
  16762. + * the pending bit set. Therefor we need to check this otherwise we
  16763. + * warn about false positives which confuses users and defeats the
  16764. + * whole purpose of this test.
  16765. + *
  16766. + * This code is called with interrupts disabled.
  16767. + */
  16768. +void softirq_check_pending_idle(void)
  16769. +{
  16770. + static int rate_limit;
  16771. + struct softirq_runner *sr = this_cpu_ptr(&softirq_runners);
  16772. + u32 warnpending;
  16773. + int i;
  16774. +
  16775. + if (rate_limit >= 10)
  16776. + return;
  16777. +
  16778. + warnpending = local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK;
  16779. + for (i = 0; i < NR_SOFTIRQS; i++) {
  16780. + struct task_struct *tsk = sr->runner[i];
  16781. +
  16782. + /*
  16783. + * The wakeup code in rtmutex.c wakes up the task
  16784. + * _before_ it sets pi_blocked_on to NULL under
  16785. + * tsk->pi_lock. So we need to check for both: state
  16786. + * and pi_blocked_on.
  16787. + */
  16788. + if (tsk) {
  16789. + raw_spin_lock(&tsk->pi_lock);
  16790. + if (tsk->pi_blocked_on || tsk->state == TASK_RUNNING) {
  16791. + /* Clear all bits pending in that task */
  16792. + warnpending &= ~(tsk->softirqs_raised);
  16793. + warnpending &= ~(1 << i);
  16794. + }
  16795. + raw_spin_unlock(&tsk->pi_lock);
  16796. + }
  16797. + }
  16798. +
  16799. + if (warnpending) {
  16800. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  16801. + warnpending);
  16802. + rate_limit++;
  16803. + }
  16804. +}
  16805. +# else
  16806. +/*
  16807. + * On !PREEMPT_RT we just printk rate limited:
  16808. + */
  16809. +void softirq_check_pending_idle(void)
  16810. +{
  16811. + static int rate_limit;
  16812. +
  16813. + if (rate_limit < 10 &&
  16814. + (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  16815. + printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  16816. + local_softirq_pending());
  16817. + rate_limit++;
  16818. + }
  16819. +}
  16820. +# endif
  16821. +
  16822. +#else /* !CONFIG_NO_HZ_COMMON */
  16823. +static inline void softirq_set_runner(unsigned int sirq) { }
  16824. +static inline void softirq_clr_runner(unsigned int sirq) { }
  16825. +#endif
  16826. +
  16827. /*
  16828. * we cannot loop indefinitely here to avoid userspace starvation,
  16829. * but we also don't want to introduce a worst case 1/HZ latency
  16830. @@ -77,6 +175,79 @@ static void wakeup_softirqd(void)
  16831. wake_up_process(tsk);
  16832. }
  16833. +#ifdef CONFIG_PREEMPT_RT_FULL
  16834. +static void wakeup_timer_softirqd(void)
  16835. +{
  16836. + /* Interrupts are disabled: no need to stop preemption */
  16837. + struct task_struct *tsk = __this_cpu_read(ktimer_softirqd);
  16838. +
  16839. + if (tsk && tsk->state != TASK_RUNNING)
  16840. + wake_up_process(tsk);
  16841. +}
  16842. +#endif
  16843. +
  16844. +static void handle_softirq(unsigned int vec_nr)
  16845. +{
  16846. + struct softirq_action *h = softirq_vec + vec_nr;
  16847. + int prev_count;
  16848. +
  16849. + prev_count = preempt_count();
  16850. +
  16851. + kstat_incr_softirqs_this_cpu(vec_nr);
  16852. +
  16853. + trace_softirq_entry(vec_nr);
  16854. + h->action(h);
  16855. + trace_softirq_exit(vec_nr);
  16856. + if (unlikely(prev_count != preempt_count())) {
  16857. + pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  16858. + vec_nr, softirq_to_name[vec_nr], h->action,
  16859. + prev_count, preempt_count());
  16860. + preempt_count_set(prev_count);
  16861. + }
  16862. +}
  16863. +
  16864. +#ifndef CONFIG_PREEMPT_RT_FULL
  16865. +static inline int ksoftirqd_softirq_pending(void)
  16866. +{
  16867. + return local_softirq_pending();
  16868. +}
  16869. +
  16870. +static void handle_pending_softirqs(u32 pending)
  16871. +{
  16872. + struct softirq_action *h = softirq_vec;
  16873. + int softirq_bit;
  16874. +
  16875. + local_irq_enable();
  16876. +
  16877. + h = softirq_vec;
  16878. +
  16879. + while ((softirq_bit = ffs(pending))) {
  16880. + unsigned int vec_nr;
  16881. +
  16882. + h += softirq_bit - 1;
  16883. + vec_nr = h - softirq_vec;
  16884. + handle_softirq(vec_nr);
  16885. +
  16886. + h++;
  16887. + pending >>= softirq_bit;
  16888. + }
  16889. +
  16890. + rcu_bh_qs();
  16891. + local_irq_disable();
  16892. +}
  16893. +
  16894. +static void run_ksoftirqd(unsigned int cpu)
  16895. +{
  16896. + local_irq_disable();
  16897. + if (ksoftirqd_softirq_pending()) {
  16898. + __do_softirq();
  16899. + local_irq_enable();
  16900. + cond_resched_rcu_qs();
  16901. + return;
  16902. + }
  16903. + local_irq_enable();
  16904. +}
  16905. +
  16906. /*
  16907. * preempt_count and SOFTIRQ_OFFSET usage:
  16908. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  16909. @@ -232,10 +403,8 @@ asmlinkage __visible void __softirq_entry __do_softirq(void)
  16910. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  16911. unsigned long old_flags = current->flags;
  16912. int max_restart = MAX_SOFTIRQ_RESTART;
  16913. - struct softirq_action *h;
  16914. bool in_hardirq;
  16915. __u32 pending;
  16916. - int softirq_bit;
  16917. /*
  16918. * Mask out PF_MEMALLOC s current task context is borrowed for the
  16919. @@ -254,36 +423,7 @@ asmlinkage __visible void __softirq_entry __do_softirq(void)
  16920. /* Reset the pending bitmask before enabling irqs */
  16921. set_softirq_pending(0);
  16922. - local_irq_enable();
  16923. -
  16924. - h = softirq_vec;
  16925. -
  16926. - while ((softirq_bit = ffs(pending))) {
  16927. - unsigned int vec_nr;
  16928. - int prev_count;
  16929. -
  16930. - h += softirq_bit - 1;
  16931. -
  16932. - vec_nr = h - softirq_vec;
  16933. - prev_count = preempt_count();
  16934. -
  16935. - kstat_incr_softirqs_this_cpu(vec_nr);
  16936. -
  16937. - trace_softirq_entry(vec_nr);
  16938. - h->action(h);
  16939. - trace_softirq_exit(vec_nr);
  16940. - if (unlikely(prev_count != preempt_count())) {
  16941. - pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  16942. - vec_nr, softirq_to_name[vec_nr], h->action,
  16943. - prev_count, preempt_count());
  16944. - preempt_count_set(prev_count);
  16945. - }
  16946. - h++;
  16947. - pending >>= softirq_bit;
  16948. - }
  16949. -
  16950. - rcu_bh_qs();
  16951. - local_irq_disable();
  16952. + handle_pending_softirqs(pending);
  16953. pending = local_softirq_pending();
  16954. if (pending) {
  16955. @@ -320,6 +460,310 @@ asmlinkage __visible void do_softirq(void)
  16956. }
  16957. /*
  16958. + * This function must run with irqs disabled!
  16959. + */
  16960. +void raise_softirq_irqoff(unsigned int nr)
  16961. +{
  16962. + __raise_softirq_irqoff(nr);
  16963. +
  16964. + /*
  16965. + * If we're in an interrupt or softirq, we're done
  16966. + * (this also catches softirq-disabled code). We will
  16967. + * actually run the softirq once we return from
  16968. + * the irq or softirq.
  16969. + *
  16970. + * Otherwise we wake up ksoftirqd to make sure we
  16971. + * schedule the softirq soon.
  16972. + */
  16973. + if (!in_interrupt())
  16974. + wakeup_softirqd();
  16975. +}
  16976. +
  16977. +void __raise_softirq_irqoff(unsigned int nr)
  16978. +{
  16979. + trace_softirq_raise(nr);
  16980. + or_softirq_pending(1UL << nr);
  16981. +}
  16982. +
  16983. +static inline void local_bh_disable_nort(void) { local_bh_disable(); }
  16984. +static inline void _local_bh_enable_nort(void) { _local_bh_enable(); }
  16985. +static void ksoftirqd_set_sched_params(unsigned int cpu) { }
  16986. +
  16987. +#else /* !PREEMPT_RT_FULL */
  16988. +
  16989. +/*
  16990. + * On RT we serialize softirq execution with a cpu local lock per softirq
  16991. + */
  16992. +static DEFINE_PER_CPU(struct local_irq_lock [NR_SOFTIRQS], local_softirq_locks);
  16993. +
  16994. +void __init softirq_early_init(void)
  16995. +{
  16996. + int i;
  16997. +
  16998. + for (i = 0; i < NR_SOFTIRQS; i++)
  16999. + local_irq_lock_init(local_softirq_locks[i]);
  17000. +}
  17001. +
  17002. +static void lock_softirq(int which)
  17003. +{
  17004. + local_lock(local_softirq_locks[which]);
  17005. +}
  17006. +
  17007. +static void unlock_softirq(int which)
  17008. +{
  17009. + local_unlock(local_softirq_locks[which]);
  17010. +}
  17011. +
  17012. +static void do_single_softirq(int which)
  17013. +{
  17014. + unsigned long old_flags = current->flags;
  17015. +
  17016. + current->flags &= ~PF_MEMALLOC;
  17017. + vtime_account_irq_enter(current);
  17018. + current->flags |= PF_IN_SOFTIRQ;
  17019. + lockdep_softirq_enter();
  17020. + local_irq_enable();
  17021. + handle_softirq(which);
  17022. + local_irq_disable();
  17023. + lockdep_softirq_exit();
  17024. + current->flags &= ~PF_IN_SOFTIRQ;
  17025. + vtime_account_irq_enter(current);
  17026. + tsk_restore_flags(current, old_flags, PF_MEMALLOC);
  17027. +}
  17028. +
  17029. +/*
  17030. + * Called with interrupts disabled. Process softirqs which were raised
  17031. + * in current context (or on behalf of ksoftirqd).
  17032. + */
  17033. +static void do_current_softirqs(void)
  17034. +{
  17035. + while (current->softirqs_raised) {
  17036. + int i = __ffs(current->softirqs_raised);
  17037. + unsigned int pending, mask = (1U << i);
  17038. +
  17039. + current->softirqs_raised &= ~mask;
  17040. + local_irq_enable();
  17041. +
  17042. + /*
  17043. + * If the lock is contended, we boost the owner to
  17044. + * process the softirq or leave the critical section
  17045. + * now.
  17046. + */
  17047. + lock_softirq(i);
  17048. + local_irq_disable();
  17049. + softirq_set_runner(i);
  17050. + /*
  17051. + * Check with the local_softirq_pending() bits,
  17052. + * whether we need to process this still or if someone
  17053. + * else took care of it.
  17054. + */
  17055. + pending = local_softirq_pending();
  17056. + if (pending & mask) {
  17057. + set_softirq_pending(pending & ~mask);
  17058. + do_single_softirq(i);
  17059. + }
  17060. + softirq_clr_runner(i);
  17061. + WARN_ON(current->softirq_nestcnt != 1);
  17062. + local_irq_enable();
  17063. + unlock_softirq(i);
  17064. + local_irq_disable();
  17065. + }
  17066. +}
  17067. +
  17068. +void __local_bh_disable(void)
  17069. +{
  17070. + if (++current->softirq_nestcnt == 1)
  17071. + migrate_disable();
  17072. +}
  17073. +EXPORT_SYMBOL(__local_bh_disable);
  17074. +
  17075. +void __local_bh_enable(void)
  17076. +{
  17077. + if (WARN_ON(current->softirq_nestcnt == 0))
  17078. + return;
  17079. +
  17080. + local_irq_disable();
  17081. + if (current->softirq_nestcnt == 1 && current->softirqs_raised)
  17082. + do_current_softirqs();
  17083. + local_irq_enable();
  17084. +
  17085. + if (--current->softirq_nestcnt == 0)
  17086. + migrate_enable();
  17087. +}
  17088. +EXPORT_SYMBOL(__local_bh_enable);
  17089. +
  17090. +void _local_bh_enable(void)
  17091. +{
  17092. + if (WARN_ON(current->softirq_nestcnt == 0))
  17093. + return;
  17094. + if (--current->softirq_nestcnt == 0)
  17095. + migrate_enable();
  17096. +}
  17097. +EXPORT_SYMBOL(_local_bh_enable);
  17098. +
  17099. +int in_serving_softirq(void)
  17100. +{
  17101. + return current->flags & PF_IN_SOFTIRQ;
  17102. +}
  17103. +EXPORT_SYMBOL(in_serving_softirq);
  17104. +
  17105. +/* Called with preemption disabled */
  17106. +static void run_ksoftirqd(unsigned int cpu)
  17107. +{
  17108. + local_irq_disable();
  17109. + current->softirq_nestcnt++;
  17110. +
  17111. + do_current_softirqs();
  17112. + current->softirq_nestcnt--;
  17113. + local_irq_enable();
  17114. + cond_resched_rcu_qs();
  17115. +}
  17116. +
  17117. +/*
  17118. + * Called from netif_rx_ni(). Preemption enabled, but migration
  17119. + * disabled. So the cpu can't go away under us.
  17120. + */
  17121. +void thread_do_softirq(void)
  17122. +{
  17123. + if (!in_serving_softirq() && current->softirqs_raised) {
  17124. + current->softirq_nestcnt++;
  17125. + do_current_softirqs();
  17126. + current->softirq_nestcnt--;
  17127. + }
  17128. +}
  17129. +
  17130. +static void do_raise_softirq_irqoff(unsigned int nr)
  17131. +{
  17132. + unsigned int mask;
  17133. +
  17134. + mask = 1UL << nr;
  17135. +
  17136. + trace_softirq_raise(nr);
  17137. + or_softirq_pending(mask);
  17138. +
  17139. + /*
  17140. + * If we are not in a hard interrupt and inside a bh disabled
  17141. + * region, we simply raise the flag on current. local_bh_enable()
  17142. + * will make sure that the softirq is executed. Otherwise we
  17143. + * delegate it to ksoftirqd.
  17144. + */
  17145. + if (!in_irq() && current->softirq_nestcnt)
  17146. + current->softirqs_raised |= mask;
  17147. + else if (!__this_cpu_read(ksoftirqd) || !__this_cpu_read(ktimer_softirqd))
  17148. + return;
  17149. +
  17150. + if (mask & TIMER_SOFTIRQS)
  17151. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  17152. + else
  17153. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  17154. +}
  17155. +
  17156. +static void wakeup_proper_softirq(unsigned int nr)
  17157. +{
  17158. + if ((1UL << nr) & TIMER_SOFTIRQS)
  17159. + wakeup_timer_softirqd();
  17160. + else
  17161. + wakeup_softirqd();
  17162. +}
  17163. +
  17164. +
  17165. +void __raise_softirq_irqoff(unsigned int nr)
  17166. +{
  17167. + do_raise_softirq_irqoff(nr);
  17168. + if (!in_irq() && !current->softirq_nestcnt)
  17169. + wakeup_proper_softirq(nr);
  17170. +}
  17171. +
  17172. +/*
  17173. + * Same as __raise_softirq_irqoff() but will process them in ksoftirqd
  17174. + */
  17175. +void __raise_softirq_irqoff_ksoft(unsigned int nr)
  17176. +{
  17177. + unsigned int mask;
  17178. +
  17179. + if (WARN_ON_ONCE(!__this_cpu_read(ksoftirqd) ||
  17180. + !__this_cpu_read(ktimer_softirqd)))
  17181. + return;
  17182. + mask = 1UL << nr;
  17183. +
  17184. + trace_softirq_raise(nr);
  17185. + or_softirq_pending(mask);
  17186. + if (mask & TIMER_SOFTIRQS)
  17187. + __this_cpu_read(ktimer_softirqd)->softirqs_raised |= mask;
  17188. + else
  17189. + __this_cpu_read(ksoftirqd)->softirqs_raised |= mask;
  17190. + wakeup_proper_softirq(nr);
  17191. +}
  17192. +
  17193. +/*
  17194. + * This function must run with irqs disabled!
  17195. + */
  17196. +void raise_softirq_irqoff(unsigned int nr)
  17197. +{
  17198. + do_raise_softirq_irqoff(nr);
  17199. +
  17200. + /*
  17201. + * If we're in an hard interrupt we let irq return code deal
  17202. + * with the wakeup of ksoftirqd.
  17203. + */
  17204. + if (in_irq())
  17205. + return;
  17206. + /*
  17207. + * If we are in thread context but outside of a bh disabled
  17208. + * region, we need to wake ksoftirqd as well.
  17209. + *
  17210. + * CHECKME: Some of the places which do that could be wrapped
  17211. + * into local_bh_disable/enable pairs. Though it's unclear
  17212. + * whether this is worth the effort. To find those places just
  17213. + * raise a WARN() if the condition is met.
  17214. + */
  17215. + if (!current->softirq_nestcnt)
  17216. + wakeup_proper_softirq(nr);
  17217. +}
  17218. +
  17219. +static inline int ksoftirqd_softirq_pending(void)
  17220. +{
  17221. + return current->softirqs_raised;
  17222. +}
  17223. +
  17224. +static inline void local_bh_disable_nort(void) { }
  17225. +static inline void _local_bh_enable_nort(void) { }
  17226. +
  17227. +static inline void ksoftirqd_set_sched_params(unsigned int cpu)
  17228. +{
  17229. + /* Take over all but timer pending softirqs when starting */
  17230. + local_irq_disable();
  17231. + current->softirqs_raised = local_softirq_pending() & ~TIMER_SOFTIRQS;
  17232. + local_irq_enable();
  17233. +}
  17234. +
  17235. +static inline void ktimer_softirqd_set_sched_params(unsigned int cpu)
  17236. +{
  17237. + struct sched_param param = { .sched_priority = 1 };
  17238. +
  17239. + sched_setscheduler(current, SCHED_FIFO, &param);
  17240. +
  17241. + /* Take over timer pending softirqs when starting */
  17242. + local_irq_disable();
  17243. + current->softirqs_raised = local_softirq_pending() & TIMER_SOFTIRQS;
  17244. + local_irq_enable();
  17245. +}
  17246. +
  17247. +static inline void ktimer_softirqd_clr_sched_params(unsigned int cpu,
  17248. + bool online)
  17249. +{
  17250. + struct sched_param param = { .sched_priority = 0 };
  17251. +
  17252. + sched_setscheduler(current, SCHED_NORMAL, &param);
  17253. +}
  17254. +
  17255. +static int ktimer_softirqd_should_run(unsigned int cpu)
  17256. +{
  17257. + return current->softirqs_raised;
  17258. +}
  17259. +
  17260. +#endif /* PREEMPT_RT_FULL */
  17261. +/*
  17262. * Enter an interrupt context.
  17263. */
  17264. void irq_enter(void)
  17265. @@ -330,9 +774,9 @@ void irq_enter(void)
  17266. * Prevent raise_softirq from needlessly waking up ksoftirqd
  17267. * here, as softirq will be serviced on return from interrupt.
  17268. */
  17269. - local_bh_disable();
  17270. + local_bh_disable_nort();
  17271. tick_irq_enter();
  17272. - _local_bh_enable();
  17273. + _local_bh_enable_nort();
  17274. }
  17275. __irq_enter();
  17276. @@ -340,6 +784,7 @@ void irq_enter(void)
  17277. static inline void invoke_softirq(void)
  17278. {
  17279. +#ifndef CONFIG_PREEMPT_RT_FULL
  17280. if (!force_irqthreads) {
  17281. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  17282. /*
  17283. @@ -359,6 +804,18 @@ static inline void invoke_softirq(void)
  17284. } else {
  17285. wakeup_softirqd();
  17286. }
  17287. +#else /* PREEMPT_RT_FULL */
  17288. + unsigned long flags;
  17289. +
  17290. + local_irq_save(flags);
  17291. + if (__this_cpu_read(ksoftirqd) &&
  17292. + __this_cpu_read(ksoftirqd)->softirqs_raised)
  17293. + wakeup_softirqd();
  17294. + if (__this_cpu_read(ktimer_softirqd) &&
  17295. + __this_cpu_read(ktimer_softirqd)->softirqs_raised)
  17296. + wakeup_timer_softirqd();
  17297. + local_irq_restore(flags);
  17298. +#endif
  17299. }
  17300. static inline void tick_irq_exit(void)
  17301. @@ -395,26 +852,6 @@ void irq_exit(void)
  17302. trace_hardirq_exit(); /* must be last! */
  17303. }
  17304. -/*
  17305. - * This function must run with irqs disabled!
  17306. - */
  17307. -inline void raise_softirq_irqoff(unsigned int nr)
  17308. -{
  17309. - __raise_softirq_irqoff(nr);
  17310. -
  17311. - /*
  17312. - * If we're in an interrupt or softirq, we're done
  17313. - * (this also catches softirq-disabled code). We will
  17314. - * actually run the softirq once we return from
  17315. - * the irq or softirq.
  17316. - *
  17317. - * Otherwise we wake up ksoftirqd to make sure we
  17318. - * schedule the softirq soon.
  17319. - */
  17320. - if (!in_interrupt())
  17321. - wakeup_softirqd();
  17322. -}
  17323. -
  17324. void raise_softirq(unsigned int nr)
  17325. {
  17326. unsigned long flags;
  17327. @@ -424,12 +861,6 @@ void raise_softirq(unsigned int nr)
  17328. local_irq_restore(flags);
  17329. }
  17330. -void __raise_softirq_irqoff(unsigned int nr)
  17331. -{
  17332. - trace_softirq_raise(nr);
  17333. - or_softirq_pending(1UL << nr);
  17334. -}
  17335. -
  17336. void open_softirq(int nr, void (*action)(struct softirq_action *))
  17337. {
  17338. softirq_vec[nr].action = action;
  17339. @@ -446,15 +877,45 @@ struct tasklet_head {
  17340. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  17341. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  17342. +static void inline
  17343. +__tasklet_common_schedule(struct tasklet_struct *t, struct tasklet_head *head, unsigned int nr)
  17344. +{
  17345. + if (tasklet_trylock(t)) {
  17346. +again:
  17347. + /* We may have been preempted before tasklet_trylock
  17348. + * and __tasklet_action may have already run.
  17349. + * So double check the sched bit while the takslet
  17350. + * is locked before adding it to the list.
  17351. + */
  17352. + if (test_bit(TASKLET_STATE_SCHED, &t->state)) {
  17353. + t->next = NULL;
  17354. + *head->tail = t;
  17355. + head->tail = &(t->next);
  17356. + raise_softirq_irqoff(nr);
  17357. + tasklet_unlock(t);
  17358. + } else {
  17359. + /* This is subtle. If we hit the corner case above
  17360. + * It is possible that we get preempted right here,
  17361. + * and another task has successfully called
  17362. + * tasklet_schedule(), then this function, and
  17363. + * failed on the trylock. Thus we must be sure
  17364. + * before releasing the tasklet lock, that the
  17365. + * SCHED_BIT is clear. Otherwise the tasklet
  17366. + * may get its SCHED_BIT set, but not added to the
  17367. + * list
  17368. + */
  17369. + if (!tasklet_tryunlock(t))
  17370. + goto again;
  17371. + }
  17372. + }
  17373. +}
  17374. +
  17375. void __tasklet_schedule(struct tasklet_struct *t)
  17376. {
  17377. unsigned long flags;
  17378. local_irq_save(flags);
  17379. - t->next = NULL;
  17380. - *__this_cpu_read(tasklet_vec.tail) = t;
  17381. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  17382. - raise_softirq_irqoff(TASKLET_SOFTIRQ);
  17383. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  17384. local_irq_restore(flags);
  17385. }
  17386. EXPORT_SYMBOL(__tasklet_schedule);
  17387. @@ -464,10 +925,7 @@ void __tasklet_hi_schedule(struct tasklet_struct *t)
  17388. unsigned long flags;
  17389. local_irq_save(flags);
  17390. - t->next = NULL;
  17391. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  17392. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  17393. - raise_softirq_irqoff(HI_SOFTIRQ);
  17394. + __tasklet_common_schedule(t, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  17395. local_irq_restore(flags);
  17396. }
  17397. EXPORT_SYMBOL(__tasklet_hi_schedule);
  17398. @@ -476,82 +934,122 @@ void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  17399. {
  17400. BUG_ON(!irqs_disabled());
  17401. - t->next = __this_cpu_read(tasklet_hi_vec.head);
  17402. - __this_cpu_write(tasklet_hi_vec.head, t);
  17403. - __raise_softirq_irqoff(HI_SOFTIRQ);
  17404. + __tasklet_hi_schedule(t);
  17405. }
  17406. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  17407. -static void tasklet_action(struct softirq_action *a)
  17408. +void tasklet_enable(struct tasklet_struct *t)
  17409. {
  17410. - struct tasklet_struct *list;
  17411. + if (!atomic_dec_and_test(&t->count))
  17412. + return;
  17413. + if (test_and_clear_bit(TASKLET_STATE_PENDING, &t->state))
  17414. + tasklet_schedule(t);
  17415. +}
  17416. +EXPORT_SYMBOL(tasklet_enable);
  17417. - local_irq_disable();
  17418. - list = __this_cpu_read(tasklet_vec.head);
  17419. - __this_cpu_write(tasklet_vec.head, NULL);
  17420. - __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  17421. - local_irq_enable();
  17422. +static void __tasklet_action(struct softirq_action *a,
  17423. + struct tasklet_struct *list)
  17424. +{
  17425. + int loops = 1000000;
  17426. while (list) {
  17427. struct tasklet_struct *t = list;
  17428. list = list->next;
  17429. - if (tasklet_trylock(t)) {
  17430. - if (!atomic_read(&t->count)) {
  17431. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  17432. - &t->state))
  17433. - BUG();
  17434. - t->func(t->data);
  17435. - tasklet_unlock(t);
  17436. - continue;
  17437. - }
  17438. - tasklet_unlock(t);
  17439. + /*
  17440. + * Should always succeed - after a tasklist got on the
  17441. + * list (after getting the SCHED bit set from 0 to 1),
  17442. + * nothing but the tasklet softirq it got queued to can
  17443. + * lock it:
  17444. + */
  17445. + if (!tasklet_trylock(t)) {
  17446. + WARN_ON(1);
  17447. + continue;
  17448. }
  17449. - local_irq_disable();
  17450. t->next = NULL;
  17451. - *__this_cpu_read(tasklet_vec.tail) = t;
  17452. - __this_cpu_write(tasklet_vec.tail, &(t->next));
  17453. - __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  17454. - local_irq_enable();
  17455. +
  17456. + /*
  17457. + * If we cannot handle the tasklet because it's disabled,
  17458. + * mark it as pending. tasklet_enable() will later
  17459. + * re-schedule the tasklet.
  17460. + */
  17461. + if (unlikely(atomic_read(&t->count))) {
  17462. +out_disabled:
  17463. + /* implicit unlock: */
  17464. + wmb();
  17465. + t->state = TASKLET_STATEF_PENDING;
  17466. + continue;
  17467. + }
  17468. +
  17469. + /*
  17470. + * After this point on the tasklet might be rescheduled
  17471. + * on another CPU, but it can only be added to another
  17472. + * CPU's tasklet list if we unlock the tasklet (which we
  17473. + * dont do yet).
  17474. + */
  17475. + if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  17476. + WARN_ON(1);
  17477. +
  17478. +again:
  17479. + t->func(t->data);
  17480. +
  17481. + /*
  17482. + * Try to unlock the tasklet. We must use cmpxchg, because
  17483. + * another CPU might have scheduled or disabled the tasklet.
  17484. + * We only allow the STATE_RUN -> 0 transition here.
  17485. + */
  17486. + while (!tasklet_tryunlock(t)) {
  17487. + /*
  17488. + * If it got disabled meanwhile, bail out:
  17489. + */
  17490. + if (atomic_read(&t->count))
  17491. + goto out_disabled;
  17492. + /*
  17493. + * If it got scheduled meanwhile, re-execute
  17494. + * the tasklet function:
  17495. + */
  17496. + if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  17497. + goto again;
  17498. + if (!--loops) {
  17499. + printk("hm, tasklet state: %08lx\n", t->state);
  17500. + WARN_ON(1);
  17501. + tasklet_unlock(t);
  17502. + break;
  17503. + }
  17504. + }
  17505. }
  17506. }
  17507. +static void tasklet_action(struct softirq_action *a)
  17508. +{
  17509. + struct tasklet_struct *list;
  17510. +
  17511. + local_irq_disable();
  17512. +
  17513. + list = __this_cpu_read(tasklet_vec.head);
  17514. + __this_cpu_write(tasklet_vec.head, NULL);
  17515. + __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
  17516. +
  17517. + local_irq_enable();
  17518. +
  17519. + __tasklet_action(a, list);
  17520. +}
  17521. +
  17522. static void tasklet_hi_action(struct softirq_action *a)
  17523. {
  17524. struct tasklet_struct *list;
  17525. local_irq_disable();
  17526. +
  17527. list = __this_cpu_read(tasklet_hi_vec.head);
  17528. __this_cpu_write(tasklet_hi_vec.head, NULL);
  17529. __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
  17530. +
  17531. local_irq_enable();
  17532. - while (list) {
  17533. - struct tasklet_struct *t = list;
  17534. -
  17535. - list = list->next;
  17536. -
  17537. - if (tasklet_trylock(t)) {
  17538. - if (!atomic_read(&t->count)) {
  17539. - if (!test_and_clear_bit(TASKLET_STATE_SCHED,
  17540. - &t->state))
  17541. - BUG();
  17542. - t->func(t->data);
  17543. - tasklet_unlock(t);
  17544. - continue;
  17545. - }
  17546. - tasklet_unlock(t);
  17547. - }
  17548. -
  17549. - local_irq_disable();
  17550. - t->next = NULL;
  17551. - *__this_cpu_read(tasklet_hi_vec.tail) = t;
  17552. - __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  17553. - __raise_softirq_irqoff(HI_SOFTIRQ);
  17554. - local_irq_enable();
  17555. - }
  17556. + __tasklet_action(a, list);
  17557. }
  17558. void tasklet_init(struct tasklet_struct *t,
  17559. @@ -572,7 +1070,7 @@ void tasklet_kill(struct tasklet_struct *t)
  17560. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  17561. do {
  17562. - yield();
  17563. + msleep(1);
  17564. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  17565. }
  17566. tasklet_unlock_wait(t);
  17567. @@ -646,25 +1144,26 @@ void __init softirq_init(void)
  17568. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  17569. }
  17570. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  17571. +void tasklet_unlock_wait(struct tasklet_struct *t)
  17572. +{
  17573. + while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  17574. + /*
  17575. + * Hack for now to avoid this busy-loop:
  17576. + */
  17577. +#ifdef CONFIG_PREEMPT_RT_FULL
  17578. + msleep(1);
  17579. +#else
  17580. + barrier();
  17581. +#endif
  17582. + }
  17583. +}
  17584. +EXPORT_SYMBOL(tasklet_unlock_wait);
  17585. +#endif
  17586. +
  17587. static int ksoftirqd_should_run(unsigned int cpu)
  17588. {
  17589. - return local_softirq_pending();
  17590. -}
  17591. -
  17592. -static void run_ksoftirqd(unsigned int cpu)
  17593. -{
  17594. - local_irq_disable();
  17595. - if (local_softirq_pending()) {
  17596. - /*
  17597. - * We can safely run softirq on inline stack, as we are not deep
  17598. - * in the task stack here.
  17599. - */
  17600. - __do_softirq();
  17601. - local_irq_enable();
  17602. - cond_resched_rcu_qs();
  17603. - return;
  17604. - }
  17605. - local_irq_enable();
  17606. + return ksoftirqd_softirq_pending();
  17607. }
  17608. #ifdef CONFIG_HOTPLUG_CPU
  17609. @@ -746,16 +1245,31 @@ static struct notifier_block cpu_nfb = {
  17610. static struct smp_hotplug_thread softirq_threads = {
  17611. .store = &ksoftirqd,
  17612. + .setup = ksoftirqd_set_sched_params,
  17613. .thread_should_run = ksoftirqd_should_run,
  17614. .thread_fn = run_ksoftirqd,
  17615. .thread_comm = "ksoftirqd/%u",
  17616. };
  17617. +#ifdef CONFIG_PREEMPT_RT_FULL
  17618. +static struct smp_hotplug_thread softirq_timer_threads = {
  17619. + .store = &ktimer_softirqd,
  17620. + .setup = ktimer_softirqd_set_sched_params,
  17621. + .cleanup = ktimer_softirqd_clr_sched_params,
  17622. + .thread_should_run = ktimer_softirqd_should_run,
  17623. + .thread_fn = run_ksoftirqd,
  17624. + .thread_comm = "ktimersoftd/%u",
  17625. +};
  17626. +#endif
  17627. +
  17628. static __init int spawn_ksoftirqd(void)
  17629. {
  17630. register_cpu_notifier(&cpu_nfb);
  17631. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  17632. +#ifdef CONFIG_PREEMPT_RT_FULL
  17633. + BUG_ON(smpboot_register_percpu_thread(&softirq_timer_threads));
  17634. +#endif
  17635. return 0;
  17636. }
  17637. diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
  17638. index 4a1ca5f6da7e..3226e22b9e42 100644
  17639. --- a/kernel/stop_machine.c
  17640. +++ b/kernel/stop_machine.c
  17641. @@ -37,7 +37,7 @@ struct cpu_stop_done {
  17642. struct cpu_stopper {
  17643. struct task_struct *thread;
  17644. - spinlock_t lock;
  17645. + raw_spinlock_t lock;
  17646. bool enabled; /* is this stopper enabled? */
  17647. struct list_head works; /* list of pending works */
  17648. @@ -83,14 +83,14 @@ static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work)
  17649. unsigned long flags;
  17650. bool enabled;
  17651. - spin_lock_irqsave(&stopper->lock, flags);
  17652. + raw_spin_lock_irqsave(&stopper->lock, flags);
  17653. enabled = stopper->enabled;
  17654. if (enabled)
  17655. __cpu_stop_queue_work(stopper, work);
  17656. else if (work->done)
  17657. cpu_stop_signal_done(work->done);
  17658. - spin_unlock_irqrestore(&stopper->lock, flags);
  17659. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  17660. return enabled;
  17661. }
  17662. @@ -232,8 +232,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  17663. int err;
  17664. lg_double_lock(&stop_cpus_lock, cpu1, cpu2);
  17665. - spin_lock_irq(&stopper1->lock);
  17666. - spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  17667. + raw_spin_lock_irq(&stopper1->lock);
  17668. + raw_spin_lock_nested(&stopper2->lock, SINGLE_DEPTH_NESTING);
  17669. err = -ENOENT;
  17670. if (!stopper1->enabled || !stopper2->enabled)
  17671. @@ -243,8 +243,8 @@ static int cpu_stop_queue_two_works(int cpu1, struct cpu_stop_work *work1,
  17672. __cpu_stop_queue_work(stopper1, work1);
  17673. __cpu_stop_queue_work(stopper2, work2);
  17674. unlock:
  17675. - spin_unlock(&stopper2->lock);
  17676. - spin_unlock_irq(&stopper1->lock);
  17677. + raw_spin_unlock(&stopper2->lock);
  17678. + raw_spin_unlock_irq(&stopper1->lock);
  17679. lg_double_unlock(&stop_cpus_lock, cpu1, cpu2);
  17680. return err;
  17681. @@ -321,18 +321,21 @@ static DEFINE_MUTEX(stop_cpus_mutex);
  17682. static bool queue_stop_cpus_work(const struct cpumask *cpumask,
  17683. cpu_stop_fn_t fn, void *arg,
  17684. - struct cpu_stop_done *done)
  17685. + struct cpu_stop_done *done, bool inactive)
  17686. {
  17687. struct cpu_stop_work *work;
  17688. unsigned int cpu;
  17689. bool queued = false;
  17690. /*
  17691. - * Disable preemption while queueing to avoid getting
  17692. - * preempted by a stopper which might wait for other stoppers
  17693. - * to enter @fn which can lead to deadlock.
  17694. + * Make sure that all work is queued on all cpus before
  17695. + * any of the cpus can execute it.
  17696. */
  17697. - lg_global_lock(&stop_cpus_lock);
  17698. + if (!inactive)
  17699. + lg_global_lock(&stop_cpus_lock);
  17700. + else
  17701. + lg_global_trylock_relax(&stop_cpus_lock);
  17702. +
  17703. for_each_cpu(cpu, cpumask) {
  17704. work = &per_cpu(cpu_stopper.stop_work, cpu);
  17705. work->fn = fn;
  17706. @@ -352,7 +355,7 @@ static int __stop_cpus(const struct cpumask *cpumask,
  17707. struct cpu_stop_done done;
  17708. cpu_stop_init_done(&done, cpumask_weight(cpumask));
  17709. - if (!queue_stop_cpus_work(cpumask, fn, arg, &done))
  17710. + if (!queue_stop_cpus_work(cpumask, fn, arg, &done, false))
  17711. return -ENOENT;
  17712. wait_for_completion(&done.completion);
  17713. return done.ret;
  17714. @@ -433,9 +436,9 @@ static int cpu_stop_should_run(unsigned int cpu)
  17715. unsigned long flags;
  17716. int run;
  17717. - spin_lock_irqsave(&stopper->lock, flags);
  17718. + raw_spin_lock_irqsave(&stopper->lock, flags);
  17719. run = !list_empty(&stopper->works);
  17720. - spin_unlock_irqrestore(&stopper->lock, flags);
  17721. + raw_spin_unlock_irqrestore(&stopper->lock, flags);
  17722. return run;
  17723. }
  17724. @@ -446,13 +449,13 @@ static void cpu_stopper_thread(unsigned int cpu)
  17725. repeat:
  17726. work = NULL;
  17727. - spin_lock_irq(&stopper->lock);
  17728. + raw_spin_lock_irq(&stopper->lock);
  17729. if (!list_empty(&stopper->works)) {
  17730. work = list_first_entry(&stopper->works,
  17731. struct cpu_stop_work, list);
  17732. list_del_init(&work->list);
  17733. }
  17734. - spin_unlock_irq(&stopper->lock);
  17735. + raw_spin_unlock_irq(&stopper->lock);
  17736. if (work) {
  17737. cpu_stop_fn_t fn = work->fn;
  17738. @@ -460,6 +463,16 @@ static void cpu_stopper_thread(unsigned int cpu)
  17739. struct cpu_stop_done *done = work->done;
  17740. int ret;
  17741. + /*
  17742. + * Wait until the stopper finished scheduling on all
  17743. + * cpus
  17744. + */
  17745. + lg_global_lock(&stop_cpus_lock);
  17746. + /*
  17747. + * Let other cpu threads continue as well
  17748. + */
  17749. + lg_global_unlock(&stop_cpus_lock);
  17750. +
  17751. /* cpu stop callbacks must not sleep, make in_atomic() == T */
  17752. preempt_count_inc();
  17753. ret = fn(arg);
  17754. @@ -526,10 +539,12 @@ static int __init cpu_stop_init(void)
  17755. for_each_possible_cpu(cpu) {
  17756. struct cpu_stopper *stopper = &per_cpu(cpu_stopper, cpu);
  17757. - spin_lock_init(&stopper->lock);
  17758. + raw_spin_lock_init(&stopper->lock);
  17759. INIT_LIST_HEAD(&stopper->works);
  17760. }
  17761. + lg_lock_init(&stop_cpus_lock, "stop_cpus_lock");
  17762. +
  17763. BUG_ON(smpboot_register_percpu_thread(&cpu_stop_threads));
  17764. stop_machine_unpark(raw_smp_processor_id());
  17765. stop_machine_initialized = true;
  17766. @@ -624,7 +639,7 @@ int stop_machine_from_inactive_cpu(cpu_stop_fn_t fn, void *data,
  17767. set_state(&msdata, MULTI_STOP_PREPARE);
  17768. cpu_stop_init_done(&done, num_active_cpus());
  17769. queue_stop_cpus_work(cpu_active_mask, multi_cpu_stop, &msdata,
  17770. - &done);
  17771. + &done, true);
  17772. ret = multi_cpu_stop(&msdata);
  17773. /* Busy wait for completion. */
  17774. diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
  17775. index 9ba7c820fc23..d85f638fd99e 100644
  17776. --- a/kernel/time/hrtimer.c
  17777. +++ b/kernel/time/hrtimer.c
  17778. @@ -53,6 +53,7 @@
  17779. #include <asm/uaccess.h>
  17780. #include <trace/events/timer.h>
  17781. +#include <trace/events/hist.h>
  17782. #include "tick-internal.h"
  17783. @@ -695,6 +696,29 @@ static void hrtimer_switch_to_hres(void)
  17784. retrigger_next_event(NULL);
  17785. }
  17786. +#ifdef CONFIG_PREEMPT_RT_FULL
  17787. +
  17788. +static struct swork_event clock_set_delay_work;
  17789. +
  17790. +static void run_clock_set_delay(struct swork_event *event)
  17791. +{
  17792. + clock_was_set();
  17793. +}
  17794. +
  17795. +void clock_was_set_delayed(void)
  17796. +{
  17797. + swork_queue(&clock_set_delay_work);
  17798. +}
  17799. +
  17800. +static __init int create_clock_set_delay_thread(void)
  17801. +{
  17802. + WARN_ON(swork_get());
  17803. + INIT_SWORK(&clock_set_delay_work, run_clock_set_delay);
  17804. + return 0;
  17805. +}
  17806. +early_initcall(create_clock_set_delay_thread);
  17807. +#else /* PREEMPT_RT_FULL */
  17808. +
  17809. static void clock_was_set_work(struct work_struct *work)
  17810. {
  17811. clock_was_set();
  17812. @@ -710,6 +734,7 @@ void clock_was_set_delayed(void)
  17813. {
  17814. schedule_work(&hrtimer_work);
  17815. }
  17816. +#endif
  17817. #else
  17818. @@ -719,11 +744,8 @@ static inline int hrtimer_is_hres_enabled(void) { return 0; }
  17819. static inline void hrtimer_switch_to_hres(void) { }
  17820. static inline void
  17821. hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
  17822. -static inline int hrtimer_reprogram(struct hrtimer *timer,
  17823. - struct hrtimer_clock_base *base)
  17824. -{
  17825. - return 0;
  17826. -}
  17827. +static inline void hrtimer_reprogram(struct hrtimer *timer,
  17828. + struct hrtimer_clock_base *base) { }
  17829. static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
  17830. static inline void retrigger_next_event(void *arg) { }
  17831. @@ -855,6 +877,32 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
  17832. }
  17833. EXPORT_SYMBOL_GPL(hrtimer_forward);
  17834. +#ifdef CONFIG_PREEMPT_RT_BASE
  17835. +# define wake_up_timer_waiters(b) wake_up(&(b)->wait)
  17836. +
  17837. +/**
  17838. + * hrtimer_wait_for_timer - Wait for a running timer
  17839. + *
  17840. + * @timer: timer to wait for
  17841. + *
  17842. + * The function waits in case the timers callback function is
  17843. + * currently executed on the waitqueue of the timer base. The
  17844. + * waitqueue is woken up after the timer callback function has
  17845. + * finished execution.
  17846. + */
  17847. +void hrtimer_wait_for_timer(const struct hrtimer *timer)
  17848. +{
  17849. + struct hrtimer_clock_base *base = timer->base;
  17850. +
  17851. + if (base && base->cpu_base && !timer->irqsafe)
  17852. + wait_event(base->cpu_base->wait,
  17853. + !(hrtimer_callback_running(timer)));
  17854. +}
  17855. +
  17856. +#else
  17857. +# define wake_up_timer_waiters(b) do { } while (0)
  17858. +#endif
  17859. +
  17860. /*
  17861. * enqueue_hrtimer - internal function to (re)start a timer
  17862. *
  17863. @@ -896,6 +944,11 @@ static void __remove_hrtimer(struct hrtimer *timer,
  17864. if (!(state & HRTIMER_STATE_ENQUEUED))
  17865. return;
  17866. + if (unlikely(!list_empty(&timer->cb_entry))) {
  17867. + list_del_init(&timer->cb_entry);
  17868. + return;
  17869. + }
  17870. +
  17871. if (!timerqueue_del(&base->active, &timer->node))
  17872. cpu_base->active_bases &= ~(1 << base->index);
  17873. @@ -991,7 +1044,16 @@ void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  17874. new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED);
  17875. timer_stats_hrtimer_set_start_info(timer);
  17876. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  17877. + {
  17878. + ktime_t now = new_base->get_time();
  17879. + if (ktime_to_ns(tim) < ktime_to_ns(now))
  17880. + timer->praecox = now;
  17881. + else
  17882. + timer->praecox = ktime_set(0, 0);
  17883. + }
  17884. +#endif
  17885. leftmost = enqueue_hrtimer(timer, new_base);
  17886. if (!leftmost)
  17887. goto unlock;
  17888. @@ -1063,7 +1125,7 @@ int hrtimer_cancel(struct hrtimer *timer)
  17889. if (ret >= 0)
  17890. return ret;
  17891. - cpu_relax();
  17892. + hrtimer_wait_for_timer(timer);
  17893. }
  17894. }
  17895. EXPORT_SYMBOL_GPL(hrtimer_cancel);
  17896. @@ -1127,6 +1189,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
  17897. base = hrtimer_clockid_to_base(clock_id);
  17898. timer->base = &cpu_base->clock_base[base];
  17899. + INIT_LIST_HEAD(&timer->cb_entry);
  17900. timerqueue_init(&timer->node);
  17901. #ifdef CONFIG_TIMER_STATS
  17902. @@ -1167,6 +1230,7 @@ bool hrtimer_active(const struct hrtimer *timer)
  17903. seq = raw_read_seqcount_begin(&cpu_base->seq);
  17904. if (timer->state != HRTIMER_STATE_INACTIVE ||
  17905. + cpu_base->running_soft == timer ||
  17906. cpu_base->running == timer)
  17907. return true;
  17908. @@ -1265,10 +1329,112 @@ static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base,
  17909. cpu_base->running = NULL;
  17910. }
  17911. +#ifdef CONFIG_PREEMPT_RT_BASE
  17912. +static void hrtimer_rt_reprogram(int restart, struct hrtimer *timer,
  17913. + struct hrtimer_clock_base *base)
  17914. +{
  17915. + int leftmost;
  17916. +
  17917. + if (restart != HRTIMER_NORESTART &&
  17918. + !(timer->state & HRTIMER_STATE_ENQUEUED)) {
  17919. +
  17920. + leftmost = enqueue_hrtimer(timer, base);
  17921. + if (!leftmost)
  17922. + return;
  17923. +#ifdef CONFIG_HIGH_RES_TIMERS
  17924. + if (!hrtimer_is_hres_active(timer)) {
  17925. + /*
  17926. + * Kick to reschedule the next tick to handle the new timer
  17927. + * on dynticks target.
  17928. + */
  17929. + if (base->cpu_base->nohz_active)
  17930. + wake_up_nohz_cpu(base->cpu_base->cpu);
  17931. + } else {
  17932. +
  17933. + hrtimer_reprogram(timer, base);
  17934. + }
  17935. +#endif
  17936. + }
  17937. +}
  17938. +
  17939. +/*
  17940. + * The changes in mainline which removed the callback modes from
  17941. + * hrtimer are not yet working with -rt. The non wakeup_process()
  17942. + * based callbacks which involve sleeping locks need to be treated
  17943. + * seperately.
  17944. + */
  17945. +static void hrtimer_rt_run_pending(void)
  17946. +{
  17947. + enum hrtimer_restart (*fn)(struct hrtimer *);
  17948. + struct hrtimer_cpu_base *cpu_base;
  17949. + struct hrtimer_clock_base *base;
  17950. + struct hrtimer *timer;
  17951. + int index, restart;
  17952. +
  17953. + local_irq_disable();
  17954. + cpu_base = &per_cpu(hrtimer_bases, smp_processor_id());
  17955. +
  17956. + raw_spin_lock(&cpu_base->lock);
  17957. +
  17958. + for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
  17959. + base = &cpu_base->clock_base[index];
  17960. +
  17961. + while (!list_empty(&base->expired)) {
  17962. + timer = list_first_entry(&base->expired,
  17963. + struct hrtimer, cb_entry);
  17964. +
  17965. + /*
  17966. + * Same as the above __run_hrtimer function
  17967. + * just we run with interrupts enabled.
  17968. + */
  17969. + debug_deactivate(timer);
  17970. + cpu_base->running_soft = timer;
  17971. + raw_write_seqcount_barrier(&cpu_base->seq);
  17972. +
  17973. + __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0);
  17974. + timer_stats_account_hrtimer(timer);
  17975. + fn = timer->function;
  17976. +
  17977. + raw_spin_unlock_irq(&cpu_base->lock);
  17978. + restart = fn(timer);
  17979. + raw_spin_lock_irq(&cpu_base->lock);
  17980. +
  17981. + hrtimer_rt_reprogram(restart, timer, base);
  17982. + raw_write_seqcount_barrier(&cpu_base->seq);
  17983. +
  17984. + WARN_ON_ONCE(cpu_base->running_soft != timer);
  17985. + cpu_base->running_soft = NULL;
  17986. + }
  17987. + }
  17988. +
  17989. + raw_spin_unlock_irq(&cpu_base->lock);
  17990. +
  17991. + wake_up_timer_waiters(cpu_base);
  17992. +}
  17993. +
  17994. +static int hrtimer_rt_defer(struct hrtimer *timer)
  17995. +{
  17996. + if (timer->irqsafe)
  17997. + return 0;
  17998. +
  17999. + __remove_hrtimer(timer, timer->base, timer->state, 0);
  18000. + list_add_tail(&timer->cb_entry, &timer->base->expired);
  18001. + return 1;
  18002. +}
  18003. +
  18004. +#else
  18005. +
  18006. +static inline int hrtimer_rt_defer(struct hrtimer *timer) { return 0; }
  18007. +
  18008. +#endif
  18009. +
  18010. +static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer);
  18011. +
  18012. static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  18013. {
  18014. struct hrtimer_clock_base *base = cpu_base->clock_base;
  18015. unsigned int active = cpu_base->active_bases;
  18016. + int raise = 0;
  18017. for (; active; base++, active >>= 1) {
  18018. struct timerqueue_node *node;
  18019. @@ -1284,6 +1450,15 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  18020. timer = container_of(node, struct hrtimer, node);
  18021. + trace_hrtimer_interrupt(raw_smp_processor_id(),
  18022. + ktime_to_ns(ktime_sub(ktime_to_ns(timer->praecox) ?
  18023. + timer->praecox : hrtimer_get_expires(timer),
  18024. + basenow)),
  18025. + current,
  18026. + timer->function == hrtimer_wakeup ?
  18027. + container_of(timer, struct hrtimer_sleeper,
  18028. + timer)->task : NULL);
  18029. +
  18030. /*
  18031. * The immediate goal for using the softexpires is
  18032. * minimizing wakeups, not running timers at the
  18033. @@ -1299,9 +1474,14 @@ static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now)
  18034. if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer))
  18035. break;
  18036. - __run_hrtimer(cpu_base, base, timer, &basenow);
  18037. + if (!hrtimer_rt_defer(timer))
  18038. + __run_hrtimer(cpu_base, base, timer, &basenow);
  18039. + else
  18040. + raise = 1;
  18041. }
  18042. }
  18043. + if (raise)
  18044. + raise_softirq_irqoff(HRTIMER_SOFTIRQ);
  18045. }
  18046. #ifdef CONFIG_HIGH_RES_TIMERS
  18047. @@ -1464,16 +1644,18 @@ static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
  18048. void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
  18049. {
  18050. sl->timer.function = hrtimer_wakeup;
  18051. + sl->timer.irqsafe = 1;
  18052. sl->task = task;
  18053. }
  18054. EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
  18055. -static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
  18056. +static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
  18057. + unsigned long state)
  18058. {
  18059. hrtimer_init_sleeper(t, current);
  18060. do {
  18061. - set_current_state(TASK_INTERRUPTIBLE);
  18062. + set_current_state(state);
  18063. hrtimer_start_expires(&t->timer, mode);
  18064. if (likely(t->task))
  18065. @@ -1515,7 +1697,8 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  18066. HRTIMER_MODE_ABS);
  18067. hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
  18068. - if (do_nanosleep(&t, HRTIMER_MODE_ABS))
  18069. + /* cpu_chill() does not care about restart state. */
  18070. + if (do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE))
  18071. goto out;
  18072. rmtp = restart->nanosleep.rmtp;
  18073. @@ -1532,8 +1715,10 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
  18074. return ret;
  18075. }
  18076. -long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18077. - const enum hrtimer_mode mode, const clockid_t clockid)
  18078. +static long
  18079. +__hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18080. + const enum hrtimer_mode mode, const clockid_t clockid,
  18081. + unsigned long state)
  18082. {
  18083. struct restart_block *restart;
  18084. struct hrtimer_sleeper t;
  18085. @@ -1546,7 +1731,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18086. hrtimer_init_on_stack(&t.timer, clockid, mode);
  18087. hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack);
  18088. - if (do_nanosleep(&t, mode))
  18089. + if (do_nanosleep(&t, mode, state))
  18090. goto out;
  18091. /* Absolute timers do not update the rmtp value and restart: */
  18092. @@ -1573,6 +1758,12 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18093. return ret;
  18094. }
  18095. +long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
  18096. + const enum hrtimer_mode mode, const clockid_t clockid)
  18097. +{
  18098. + return __hrtimer_nanosleep(rqtp, rmtp, mode, clockid, TASK_INTERRUPTIBLE);
  18099. +}
  18100. +
  18101. SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  18102. struct timespec __user *, rmtp)
  18103. {
  18104. @@ -1587,6 +1778,26 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
  18105. return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
  18106. }
  18107. +#ifdef CONFIG_PREEMPT_RT_FULL
  18108. +/*
  18109. + * Sleep for 1 ms in hope whoever holds what we want will let it go.
  18110. + */
  18111. +void cpu_chill(void)
  18112. +{
  18113. + struct timespec tu = {
  18114. + .tv_nsec = NSEC_PER_MSEC,
  18115. + };
  18116. + unsigned int freeze_flag = current->flags & PF_NOFREEZE;
  18117. +
  18118. + current->flags |= PF_NOFREEZE;
  18119. + __hrtimer_nanosleep(&tu, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC,
  18120. + TASK_UNINTERRUPTIBLE);
  18121. + if (!freeze_flag)
  18122. + current->flags &= ~PF_NOFREEZE;
  18123. +}
  18124. +EXPORT_SYMBOL(cpu_chill);
  18125. +#endif
  18126. +
  18127. /*
  18128. * Functions related to boot-time initialization:
  18129. */
  18130. @@ -1598,10 +1809,14 @@ int hrtimers_prepare_cpu(unsigned int cpu)
  18131. for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
  18132. cpu_base->clock_base[i].cpu_base = cpu_base;
  18133. timerqueue_init_head(&cpu_base->clock_base[i].active);
  18134. + INIT_LIST_HEAD(&cpu_base->clock_base[i].expired);
  18135. }
  18136. cpu_base->cpu = cpu;
  18137. hrtimer_init_hres(cpu_base);
  18138. +#ifdef CONFIG_PREEMPT_RT_BASE
  18139. + init_waitqueue_head(&cpu_base->wait);
  18140. +#endif
  18141. return 0;
  18142. }
  18143. @@ -1671,9 +1886,26 @@ int hrtimers_dead_cpu(unsigned int scpu)
  18144. #endif /* CONFIG_HOTPLUG_CPU */
  18145. +#ifdef CONFIG_PREEMPT_RT_BASE
  18146. +
  18147. +static void run_hrtimer_softirq(struct softirq_action *h)
  18148. +{
  18149. + hrtimer_rt_run_pending();
  18150. +}
  18151. +
  18152. +static void hrtimers_open_softirq(void)
  18153. +{
  18154. + open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq);
  18155. +}
  18156. +
  18157. +#else
  18158. +static void hrtimers_open_softirq(void) { }
  18159. +#endif
  18160. +
  18161. void __init hrtimers_init(void)
  18162. {
  18163. hrtimers_prepare_cpu(smp_processor_id());
  18164. + hrtimers_open_softirq();
  18165. }
  18166. /**
  18167. diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
  18168. index 1d5c7204ddc9..184de6751180 100644
  18169. --- a/kernel/time/itimer.c
  18170. +++ b/kernel/time/itimer.c
  18171. @@ -213,6 +213,7 @@ int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
  18172. /* We are sharing ->siglock with it_real_fn() */
  18173. if (hrtimer_try_to_cancel(timer) < 0) {
  18174. spin_unlock_irq(&tsk->sighand->siglock);
  18175. + hrtimer_wait_for_timer(&tsk->signal->real_timer);
  18176. goto again;
  18177. }
  18178. expires = timeval_to_ktime(value->it_value);
  18179. diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
  18180. index 555e21f7b966..a5d6435fabbb 100644
  18181. --- a/kernel/time/jiffies.c
  18182. +++ b/kernel/time/jiffies.c
  18183. @@ -74,7 +74,8 @@ static struct clocksource clocksource_jiffies = {
  18184. .max_cycles = 10,
  18185. };
  18186. -__cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
  18187. +__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  18188. +__cacheline_aligned_in_smp seqcount_t jiffies_seq;
  18189. #if (BITS_PER_LONG < 64)
  18190. u64 get_jiffies_64(void)
  18191. @@ -83,9 +84,9 @@ u64 get_jiffies_64(void)
  18192. u64 ret;
  18193. do {
  18194. - seq = read_seqbegin(&jiffies_lock);
  18195. + seq = read_seqcount_begin(&jiffies_seq);
  18196. ret = jiffies_64;
  18197. - } while (read_seqretry(&jiffies_lock, seq));
  18198. + } while (read_seqcount_retry(&jiffies_seq, seq));
  18199. return ret;
  18200. }
  18201. EXPORT_SYMBOL(get_jiffies_64);
  18202. diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
  18203. index 6df8927c58a5..05b7391bf9bd 100644
  18204. --- a/kernel/time/ntp.c
  18205. +++ b/kernel/time/ntp.c
  18206. @@ -17,6 +17,7 @@
  18207. #include <linux/module.h>
  18208. #include <linux/rtc.h>
  18209. #include <linux/math64.h>
  18210. +#include <linux/swork.h>
  18211. #include "ntp_internal.h"
  18212. #include "timekeeping_internal.h"
  18213. @@ -568,10 +569,35 @@ static void sync_cmos_clock(struct work_struct *work)
  18214. &sync_cmos_work, timespec64_to_jiffies(&next));
  18215. }
  18216. +#ifdef CONFIG_PREEMPT_RT_FULL
  18217. +
  18218. +static void run_clock_set_delay(struct swork_event *event)
  18219. +{
  18220. + queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  18221. +}
  18222. +
  18223. +static struct swork_event ntp_cmos_swork;
  18224. +
  18225. +void ntp_notify_cmos_timer(void)
  18226. +{
  18227. + swork_queue(&ntp_cmos_swork);
  18228. +}
  18229. +
  18230. +static __init int create_cmos_delay_thread(void)
  18231. +{
  18232. + WARN_ON(swork_get());
  18233. + INIT_SWORK(&ntp_cmos_swork, run_clock_set_delay);
  18234. + return 0;
  18235. +}
  18236. +early_initcall(create_cmos_delay_thread);
  18237. +
  18238. +#else
  18239. +
  18240. void ntp_notify_cmos_timer(void)
  18241. {
  18242. queue_delayed_work(system_power_efficient_wq, &sync_cmos_work, 0);
  18243. }
  18244. +#endif /* CONFIG_PREEMPT_RT_FULL */
  18245. #else
  18246. void ntp_notify_cmos_timer(void) { }
  18247. diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
  18248. index 39008d78927a..633f4eaca9e7 100644
  18249. --- a/kernel/time/posix-cpu-timers.c
  18250. +++ b/kernel/time/posix-cpu-timers.c
  18251. @@ -3,6 +3,7 @@
  18252. */
  18253. #include <linux/sched.h>
  18254. +#include <linux/sched/rt.h>
  18255. #include <linux/posix-timers.h>
  18256. #include <linux/errno.h>
  18257. #include <linux/math64.h>
  18258. @@ -620,7 +621,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
  18259. /*
  18260. * Disarm any old timer after extracting its expiry time.
  18261. */
  18262. - WARN_ON_ONCE(!irqs_disabled());
  18263. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18264. ret = 0;
  18265. old_incr = timer->it.cpu.incr;
  18266. @@ -1064,7 +1065,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer)
  18267. /*
  18268. * Now re-arm for the new expiry time.
  18269. */
  18270. - WARN_ON_ONCE(!irqs_disabled());
  18271. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18272. arm_timer(timer);
  18273. unlock_task_sighand(p, &flags);
  18274. @@ -1153,13 +1154,13 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
  18275. * already updated our counts. We need to check if any timers fire now.
  18276. * Interrupts are disabled.
  18277. */
  18278. -void run_posix_cpu_timers(struct task_struct *tsk)
  18279. +static void __run_posix_cpu_timers(struct task_struct *tsk)
  18280. {
  18281. LIST_HEAD(firing);
  18282. struct k_itimer *timer, *next;
  18283. unsigned long flags;
  18284. - WARN_ON_ONCE(!irqs_disabled());
  18285. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  18286. /*
  18287. * The fast path checks that there are no expired thread or thread
  18288. @@ -1213,6 +1214,190 @@ void run_posix_cpu_timers(struct task_struct *tsk)
  18289. }
  18290. }
  18291. +#ifdef CONFIG_PREEMPT_RT_BASE
  18292. +#include <linux/kthread.h>
  18293. +#include <linux/cpu.h>
  18294. +DEFINE_PER_CPU(struct task_struct *, posix_timer_task);
  18295. +DEFINE_PER_CPU(struct task_struct *, posix_timer_tasklist);
  18296. +
  18297. +static int posix_cpu_timers_thread(void *data)
  18298. +{
  18299. + int cpu = (long)data;
  18300. +
  18301. + BUG_ON(per_cpu(posix_timer_task,cpu) != current);
  18302. +
  18303. + while (!kthread_should_stop()) {
  18304. + struct task_struct *tsk = NULL;
  18305. + struct task_struct *next = NULL;
  18306. +
  18307. + if (cpu_is_offline(cpu))
  18308. + goto wait_to_die;
  18309. +
  18310. + /* grab task list */
  18311. + raw_local_irq_disable();
  18312. + tsk = per_cpu(posix_timer_tasklist, cpu);
  18313. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  18314. + raw_local_irq_enable();
  18315. +
  18316. + /* its possible the list is empty, just return */
  18317. + if (!tsk) {
  18318. + set_current_state(TASK_INTERRUPTIBLE);
  18319. + schedule();
  18320. + __set_current_state(TASK_RUNNING);
  18321. + continue;
  18322. + }
  18323. +
  18324. + /* Process task list */
  18325. + while (1) {
  18326. + /* save next */
  18327. + next = tsk->posix_timer_list;
  18328. +
  18329. + /* run the task timers, clear its ptr and
  18330. + * unreference it
  18331. + */
  18332. + __run_posix_cpu_timers(tsk);
  18333. + tsk->posix_timer_list = NULL;
  18334. + put_task_struct(tsk);
  18335. +
  18336. + /* check if this is the last on the list */
  18337. + if (next == tsk)
  18338. + break;
  18339. + tsk = next;
  18340. + }
  18341. + }
  18342. + return 0;
  18343. +
  18344. +wait_to_die:
  18345. + /* Wait for kthread_stop */
  18346. + set_current_state(TASK_INTERRUPTIBLE);
  18347. + while (!kthread_should_stop()) {
  18348. + schedule();
  18349. + set_current_state(TASK_INTERRUPTIBLE);
  18350. + }
  18351. + __set_current_state(TASK_RUNNING);
  18352. + return 0;
  18353. +}
  18354. +
  18355. +static inline int __fastpath_timer_check(struct task_struct *tsk)
  18356. +{
  18357. + /* tsk == current, ensure it is safe to use ->signal/sighand */
  18358. + if (unlikely(tsk->exit_state))
  18359. + return 0;
  18360. +
  18361. + if (!task_cputime_zero(&tsk->cputime_expires))
  18362. + return 1;
  18363. +
  18364. + if (!task_cputime_zero(&tsk->signal->cputime_expires))
  18365. + return 1;
  18366. +
  18367. + return 0;
  18368. +}
  18369. +
  18370. +void run_posix_cpu_timers(struct task_struct *tsk)
  18371. +{
  18372. + unsigned long cpu = smp_processor_id();
  18373. + struct task_struct *tasklist;
  18374. +
  18375. + BUG_ON(!irqs_disabled());
  18376. + if(!per_cpu(posix_timer_task, cpu))
  18377. + return;
  18378. + /* get per-cpu references */
  18379. + tasklist = per_cpu(posix_timer_tasklist, cpu);
  18380. +
  18381. + /* check to see if we're already queued */
  18382. + if (!tsk->posix_timer_list && __fastpath_timer_check(tsk)) {
  18383. + get_task_struct(tsk);
  18384. + if (tasklist) {
  18385. + tsk->posix_timer_list = tasklist;
  18386. + } else {
  18387. + /*
  18388. + * The list is terminated by a self-pointing
  18389. + * task_struct
  18390. + */
  18391. + tsk->posix_timer_list = tsk;
  18392. + }
  18393. + per_cpu(posix_timer_tasklist, cpu) = tsk;
  18394. +
  18395. + wake_up_process(per_cpu(posix_timer_task, cpu));
  18396. + }
  18397. +}
  18398. +
  18399. +/*
  18400. + * posix_cpu_thread_call - callback that gets triggered when a CPU is added.
  18401. + * Here we can start up the necessary migration thread for the new CPU.
  18402. + */
  18403. +static int posix_cpu_thread_call(struct notifier_block *nfb,
  18404. + unsigned long action, void *hcpu)
  18405. +{
  18406. + int cpu = (long)hcpu;
  18407. + struct task_struct *p;
  18408. + struct sched_param param;
  18409. +
  18410. + switch (action) {
  18411. + case CPU_UP_PREPARE:
  18412. + p = kthread_create(posix_cpu_timers_thread, hcpu,
  18413. + "posixcputmr/%d",cpu);
  18414. + if (IS_ERR(p))
  18415. + return NOTIFY_BAD;
  18416. + p->flags |= PF_NOFREEZE;
  18417. + kthread_bind(p, cpu);
  18418. + /* Must be high prio to avoid getting starved */
  18419. + param.sched_priority = MAX_RT_PRIO-1;
  18420. + sched_setscheduler(p, SCHED_FIFO, &param);
  18421. + per_cpu(posix_timer_task,cpu) = p;
  18422. + break;
  18423. + case CPU_ONLINE:
  18424. + /* Strictly unneccessary, as first user will wake it. */
  18425. + wake_up_process(per_cpu(posix_timer_task,cpu));
  18426. + break;
  18427. +#ifdef CONFIG_HOTPLUG_CPU
  18428. + case CPU_UP_CANCELED:
  18429. + /* Unbind it from offline cpu so it can run. Fall thru. */
  18430. + kthread_bind(per_cpu(posix_timer_task, cpu),
  18431. + cpumask_any(cpu_online_mask));
  18432. + kthread_stop(per_cpu(posix_timer_task,cpu));
  18433. + per_cpu(posix_timer_task,cpu) = NULL;
  18434. + break;
  18435. + case CPU_DEAD:
  18436. + kthread_stop(per_cpu(posix_timer_task,cpu));
  18437. + per_cpu(posix_timer_task,cpu) = NULL;
  18438. + break;
  18439. +#endif
  18440. + }
  18441. + return NOTIFY_OK;
  18442. +}
  18443. +
  18444. +/* Register at highest priority so that task migration (migrate_all_tasks)
  18445. + * happens before everything else.
  18446. + */
  18447. +static struct notifier_block posix_cpu_thread_notifier = {
  18448. + .notifier_call = posix_cpu_thread_call,
  18449. + .priority = 10
  18450. +};
  18451. +
  18452. +static int __init posix_cpu_thread_init(void)
  18453. +{
  18454. + void *hcpu = (void *)(long)smp_processor_id();
  18455. + /* Start one for boot CPU. */
  18456. + unsigned long cpu;
  18457. +
  18458. + /* init the per-cpu posix_timer_tasklets */
  18459. + for_each_possible_cpu(cpu)
  18460. + per_cpu(posix_timer_tasklist, cpu) = NULL;
  18461. +
  18462. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_UP_PREPARE, hcpu);
  18463. + posix_cpu_thread_call(&posix_cpu_thread_notifier, CPU_ONLINE, hcpu);
  18464. + register_cpu_notifier(&posix_cpu_thread_notifier);
  18465. + return 0;
  18466. +}
  18467. +early_initcall(posix_cpu_thread_init);
  18468. +#else /* CONFIG_PREEMPT_RT_BASE */
  18469. +void run_posix_cpu_timers(struct task_struct *tsk)
  18470. +{
  18471. + __run_posix_cpu_timers(tsk);
  18472. +}
  18473. +#endif /* CONFIG_PREEMPT_RT_BASE */
  18474. +
  18475. /*
  18476. * Set one of the process-wide special case CPU timers or RLIMIT_CPU.
  18477. * The tsk->sighand->siglock must be held by the caller.
  18478. diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
  18479. index f2826c35e918..464a98155a0e 100644
  18480. --- a/kernel/time/posix-timers.c
  18481. +++ b/kernel/time/posix-timers.c
  18482. @@ -506,6 +506,7 @@ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
  18483. static struct pid *good_sigevent(sigevent_t * event)
  18484. {
  18485. struct task_struct *rtn = current->group_leader;
  18486. + int sig = event->sigev_signo;
  18487. if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
  18488. (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
  18489. @@ -514,7 +515,8 @@ static struct pid *good_sigevent(sigevent_t * event)
  18490. return NULL;
  18491. if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
  18492. - ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
  18493. + (sig <= 0 || sig > SIGRTMAX || sig_kernel_only(sig) ||
  18494. + sig_kernel_coredump(sig)))
  18495. return NULL;
  18496. return task_pid(rtn);
  18497. @@ -826,6 +828,20 @@ SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
  18498. return overrun;
  18499. }
  18500. +/*
  18501. + * Protected by RCU!
  18502. + */
  18503. +static void timer_wait_for_callback(struct k_clock *kc, struct k_itimer *timr)
  18504. +{
  18505. +#ifdef CONFIG_PREEMPT_RT_FULL
  18506. + if (kc->timer_set == common_timer_set)
  18507. + hrtimer_wait_for_timer(&timr->it.real.timer);
  18508. + else
  18509. + /* FIXME: Whacky hack for posix-cpu-timers */
  18510. + schedule_timeout(1);
  18511. +#endif
  18512. +}
  18513. +
  18514. /* Set a POSIX.1b interval timer. */
  18515. /* timr->it_lock is taken. */
  18516. static int
  18517. @@ -903,6 +919,7 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  18518. if (!timr)
  18519. return -EINVAL;
  18520. + rcu_read_lock();
  18521. kc = clockid_to_kclock(timr->it_clock);
  18522. if (WARN_ON_ONCE(!kc || !kc->timer_set))
  18523. error = -EINVAL;
  18524. @@ -911,9 +928,12 @@ SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
  18525. unlock_timer(timr, flag);
  18526. if (error == TIMER_RETRY) {
  18527. + timer_wait_for_callback(kc, timr);
  18528. rtn = NULL; // We already got the old time...
  18529. + rcu_read_unlock();
  18530. goto retry;
  18531. }
  18532. + rcu_read_unlock();
  18533. if (old_setting && !error &&
  18534. copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
  18535. @@ -951,10 +971,15 @@ SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
  18536. if (!timer)
  18537. return -EINVAL;
  18538. + rcu_read_lock();
  18539. if (timer_delete_hook(timer) == TIMER_RETRY) {
  18540. unlock_timer(timer, flags);
  18541. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  18542. + timer);
  18543. + rcu_read_unlock();
  18544. goto retry_delete;
  18545. }
  18546. + rcu_read_unlock();
  18547. spin_lock(&current->sighand->siglock);
  18548. list_del(&timer->list);
  18549. @@ -980,8 +1005,18 @@ static void itimer_delete(struct k_itimer *timer)
  18550. retry_delete:
  18551. spin_lock_irqsave(&timer->it_lock, flags);
  18552. - if (timer_delete_hook(timer) == TIMER_RETRY) {
  18553. + /* On RT we can race with a deletion */
  18554. + if (!timer->it_signal) {
  18555. unlock_timer(timer, flags);
  18556. + return;
  18557. + }
  18558. +
  18559. + if (timer_delete_hook(timer) == TIMER_RETRY) {
  18560. + rcu_read_lock();
  18561. + unlock_timer(timer, flags);
  18562. + timer_wait_for_callback(clockid_to_kclock(timer->it_clock),
  18563. + timer);
  18564. + rcu_read_unlock();
  18565. goto retry_delete;
  18566. }
  18567. list_del(&timer->list);
  18568. diff --git a/kernel/time/tick-broadcast-hrtimer.c b/kernel/time/tick-broadcast-hrtimer.c
  18569. index 690b797f522e..fe8ba1619879 100644
  18570. --- a/kernel/time/tick-broadcast-hrtimer.c
  18571. +++ b/kernel/time/tick-broadcast-hrtimer.c
  18572. @@ -107,5 +107,6 @@ void tick_setup_hrtimer_broadcast(void)
  18573. {
  18574. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  18575. bctimer.function = bc_handler;
  18576. + bctimer.irqsafe = true;
  18577. clockevents_register_device(&ce_broadcast_hrtimer);
  18578. }
  18579. diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
  18580. index 4fcd99e12aa0..5a47f2e98faf 100644
  18581. --- a/kernel/time/tick-common.c
  18582. +++ b/kernel/time/tick-common.c
  18583. @@ -79,13 +79,15 @@ int tick_is_oneshot_available(void)
  18584. static void tick_periodic(int cpu)
  18585. {
  18586. if (tick_do_timer_cpu == cpu) {
  18587. - write_seqlock(&jiffies_lock);
  18588. + raw_spin_lock(&jiffies_lock);
  18589. + write_seqcount_begin(&jiffies_seq);
  18590. /* Keep track of the next tick event */
  18591. tick_next_period = ktime_add(tick_next_period, tick_period);
  18592. do_timer(1);
  18593. - write_sequnlock(&jiffies_lock);
  18594. + write_seqcount_end(&jiffies_seq);
  18595. + raw_spin_unlock(&jiffies_lock);
  18596. update_wall_time();
  18597. }
  18598. @@ -157,9 +159,9 @@ void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  18599. ktime_t next;
  18600. do {
  18601. - seq = read_seqbegin(&jiffies_lock);
  18602. + seq = read_seqcount_begin(&jiffies_seq);
  18603. next = tick_next_period;
  18604. - } while (read_seqretry(&jiffies_lock, seq));
  18605. + } while (read_seqcount_retry(&jiffies_seq, seq));
  18606. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  18607. diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
  18608. index 2ec7c00228f3..c1b30b8c671a 100644
  18609. --- a/kernel/time/tick-sched.c
  18610. +++ b/kernel/time/tick-sched.c
  18611. @@ -62,7 +62,8 @@ static void tick_do_update_jiffies64(ktime_t now)
  18612. return;
  18613. /* Reevaluate with jiffies_lock held */
  18614. - write_seqlock(&jiffies_lock);
  18615. + raw_spin_lock(&jiffies_lock);
  18616. + write_seqcount_begin(&jiffies_seq);
  18617. delta = ktime_sub(now, last_jiffies_update);
  18618. if (delta.tv64 >= tick_period.tv64) {
  18619. @@ -85,10 +86,12 @@ static void tick_do_update_jiffies64(ktime_t now)
  18620. /* Keep the tick_next_period variable up to date */
  18621. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  18622. } else {
  18623. - write_sequnlock(&jiffies_lock);
  18624. + write_seqcount_end(&jiffies_seq);
  18625. + raw_spin_unlock(&jiffies_lock);
  18626. return;
  18627. }
  18628. - write_sequnlock(&jiffies_lock);
  18629. + write_seqcount_end(&jiffies_seq);
  18630. + raw_spin_unlock(&jiffies_lock);
  18631. update_wall_time();
  18632. }
  18633. @@ -99,12 +102,14 @@ static ktime_t tick_init_jiffy_update(void)
  18634. {
  18635. ktime_t period;
  18636. - write_seqlock(&jiffies_lock);
  18637. + raw_spin_lock(&jiffies_lock);
  18638. + write_seqcount_begin(&jiffies_seq);
  18639. /* Did we start the jiffies update yet ? */
  18640. if (last_jiffies_update.tv64 == 0)
  18641. last_jiffies_update = tick_next_period;
  18642. period = last_jiffies_update;
  18643. - write_sequnlock(&jiffies_lock);
  18644. + write_seqcount_end(&jiffies_seq);
  18645. + raw_spin_unlock(&jiffies_lock);
  18646. return period;
  18647. }
  18648. @@ -212,6 +217,7 @@ static void nohz_full_kick_func(struct irq_work *work)
  18649. static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
  18650. .func = nohz_full_kick_func,
  18651. + .flags = IRQ_WORK_HARD_IRQ,
  18652. };
  18653. /*
  18654. @@ -670,10 +676,10 @@ static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  18655. /* Read jiffies and the time when jiffies were updated last */
  18656. do {
  18657. - seq = read_seqbegin(&jiffies_lock);
  18658. + seq = read_seqcount_begin(&jiffies_seq);
  18659. basemono = last_jiffies_update.tv64;
  18660. basejiff = jiffies;
  18661. - } while (read_seqretry(&jiffies_lock, seq));
  18662. + } while (read_seqcount_retry(&jiffies_seq, seq));
  18663. ts->last_jiffies = basejiff;
  18664. if (rcu_needs_cpu(basemono, &next_rcu) ||
  18665. @@ -874,14 +880,7 @@ static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  18666. return false;
  18667. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  18668. - static int ratelimit;
  18669. -
  18670. - if (ratelimit < 10 &&
  18671. - (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
  18672. - pr_warn("NOHZ: local_softirq_pending %02x\n",
  18673. - (unsigned int) local_softirq_pending());
  18674. - ratelimit++;
  18675. - }
  18676. + softirq_check_pending_idle();
  18677. return false;
  18678. }
  18679. @@ -1190,6 +1189,7 @@ void tick_setup_sched_timer(void)
  18680. * Emulate tick processing via per-CPU hrtimers:
  18681. */
  18682. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  18683. + ts->sched_timer.irqsafe = 1;
  18684. ts->sched_timer.function = tick_sched_timer;
  18685. /* Get the next period (per-CPU) */
  18686. diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
  18687. index 37dec7e3db43..fa8d90d2acc3 100644
  18688. --- a/kernel/time/timekeeping.c
  18689. +++ b/kernel/time/timekeeping.c
  18690. @@ -2328,8 +2328,10 @@ EXPORT_SYMBOL(hardpps);
  18691. */
  18692. void xtime_update(unsigned long ticks)
  18693. {
  18694. - write_seqlock(&jiffies_lock);
  18695. + raw_spin_lock(&jiffies_lock);
  18696. + write_seqcount_begin(&jiffies_seq);
  18697. do_timer(ticks);
  18698. - write_sequnlock(&jiffies_lock);
  18699. + write_seqcount_end(&jiffies_seq);
  18700. + raw_spin_unlock(&jiffies_lock);
  18701. update_wall_time();
  18702. }
  18703. diff --git a/kernel/time/timekeeping.h b/kernel/time/timekeeping.h
  18704. index 704f595ce83f..763a3e5121ff 100644
  18705. --- a/kernel/time/timekeeping.h
  18706. +++ b/kernel/time/timekeeping.h
  18707. @@ -19,7 +19,8 @@ extern void timekeeping_resume(void);
  18708. extern void do_timer(unsigned long ticks);
  18709. extern void update_wall_time(void);
  18710. -extern seqlock_t jiffies_lock;
  18711. +extern raw_spinlock_t jiffies_lock;
  18712. +extern seqcount_t jiffies_seq;
  18713. #define CS_NAME_LEN 32
  18714. diff --git a/kernel/time/timer.c b/kernel/time/timer.c
  18715. index 32bf6f75a8fe..ba53447a03f5 100644
  18716. --- a/kernel/time/timer.c
  18717. +++ b/kernel/time/timer.c
  18718. @@ -193,8 +193,11 @@ EXPORT_SYMBOL(jiffies_64);
  18719. #endif
  18720. struct timer_base {
  18721. - spinlock_t lock;
  18722. + raw_spinlock_t lock;
  18723. struct timer_list *running_timer;
  18724. +#ifdef CONFIG_PREEMPT_RT_FULL
  18725. + struct swait_queue_head wait_for_running_timer;
  18726. +#endif
  18727. unsigned long clk;
  18728. unsigned long next_expiry;
  18729. unsigned int cpu;
  18730. @@ -947,10 +950,10 @@ static struct timer_base *lock_timer_base(struct timer_list *timer,
  18731. if (!(tf & TIMER_MIGRATING)) {
  18732. base = get_timer_base(tf);
  18733. - spin_lock_irqsave(&base->lock, *flags);
  18734. + raw_spin_lock_irqsave(&base->lock, *flags);
  18735. if (timer->flags == tf)
  18736. return base;
  18737. - spin_unlock_irqrestore(&base->lock, *flags);
  18738. + raw_spin_unlock_irqrestore(&base->lock, *flags);
  18739. }
  18740. cpu_relax();
  18741. }
  18742. @@ -1017,9 +1020,9 @@ __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
  18743. /* See the comment in lock_timer_base() */
  18744. timer->flags |= TIMER_MIGRATING;
  18745. - spin_unlock(&base->lock);
  18746. + raw_spin_unlock(&base->lock);
  18747. base = new_base;
  18748. - spin_lock(&base->lock);
  18749. + raw_spin_lock(&base->lock);
  18750. WRITE_ONCE(timer->flags,
  18751. (timer->flags & ~TIMER_BASEMASK) | base->cpu);
  18752. }
  18753. @@ -1040,7 +1043,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only)
  18754. }
  18755. out_unlock:
  18756. - spin_unlock_irqrestore(&base->lock, flags);
  18757. + raw_spin_unlock_irqrestore(&base->lock, flags);
  18758. return ret;
  18759. }
  18760. @@ -1134,19 +1137,46 @@ void add_timer_on(struct timer_list *timer, int cpu)
  18761. if (base != new_base) {
  18762. timer->flags |= TIMER_MIGRATING;
  18763. - spin_unlock(&base->lock);
  18764. + raw_spin_unlock(&base->lock);
  18765. base = new_base;
  18766. - spin_lock(&base->lock);
  18767. + raw_spin_lock(&base->lock);
  18768. WRITE_ONCE(timer->flags,
  18769. (timer->flags & ~TIMER_BASEMASK) | cpu);
  18770. }
  18771. debug_activate(timer, timer->expires);
  18772. internal_add_timer(base, timer);
  18773. - spin_unlock_irqrestore(&base->lock, flags);
  18774. + raw_spin_unlock_irqrestore(&base->lock, flags);
  18775. }
  18776. EXPORT_SYMBOL_GPL(add_timer_on);
  18777. +#ifdef CONFIG_PREEMPT_RT_FULL
  18778. +/*
  18779. + * Wait for a running timer
  18780. + */
  18781. +static void wait_for_running_timer(struct timer_list *timer)
  18782. +{
  18783. + struct timer_base *base;
  18784. + u32 tf = timer->flags;
  18785. +
  18786. + if (tf & TIMER_MIGRATING)
  18787. + return;
  18788. +
  18789. + base = get_timer_base(tf);
  18790. + swait_event(base->wait_for_running_timer,
  18791. + base->running_timer != timer);
  18792. +}
  18793. +
  18794. +# define wakeup_timer_waiters(b) swake_up_all(&(b)->wait_for_running_timer)
  18795. +#else
  18796. +static inline void wait_for_running_timer(struct timer_list *timer)
  18797. +{
  18798. + cpu_relax();
  18799. +}
  18800. +
  18801. +# define wakeup_timer_waiters(b) do { } while (0)
  18802. +#endif
  18803. +
  18804. /**
  18805. * del_timer - deactive a timer.
  18806. * @timer: the timer to be deactivated
  18807. @@ -1170,7 +1200,7 @@ int del_timer(struct timer_list *timer)
  18808. if (timer_pending(timer)) {
  18809. base = lock_timer_base(timer, &flags);
  18810. ret = detach_if_pending(timer, base, true);
  18811. - spin_unlock_irqrestore(&base->lock, flags);
  18812. + raw_spin_unlock_irqrestore(&base->lock, flags);
  18813. }
  18814. return ret;
  18815. @@ -1198,13 +1228,13 @@ int try_to_del_timer_sync(struct timer_list *timer)
  18816. timer_stats_timer_clear_start_info(timer);
  18817. ret = detach_if_pending(timer, base, true);
  18818. }
  18819. - spin_unlock_irqrestore(&base->lock, flags);
  18820. + raw_spin_unlock_irqrestore(&base->lock, flags);
  18821. return ret;
  18822. }
  18823. EXPORT_SYMBOL(try_to_del_timer_sync);
  18824. -#ifdef CONFIG_SMP
  18825. +#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT_FULL)
  18826. /**
  18827. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  18828. * @timer: the timer to be deactivated
  18829. @@ -1264,7 +1294,7 @@ int del_timer_sync(struct timer_list *timer)
  18830. int ret = try_to_del_timer_sync(timer);
  18831. if (ret >= 0)
  18832. return ret;
  18833. - cpu_relax();
  18834. + wait_for_running_timer(timer);
  18835. }
  18836. }
  18837. EXPORT_SYMBOL(del_timer_sync);
  18838. @@ -1329,14 +1359,17 @@ static void expire_timers(struct timer_base *base, struct hlist_head *head)
  18839. fn = timer->function;
  18840. data = timer->data;
  18841. - if (timer->flags & TIMER_IRQSAFE) {
  18842. - spin_unlock(&base->lock);
  18843. + if (!IS_ENABLED(CONFIG_PREEMPT_RT_FULL) &&
  18844. + timer->flags & TIMER_IRQSAFE) {
  18845. + raw_spin_unlock(&base->lock);
  18846. call_timer_fn(timer, fn, data);
  18847. - spin_lock(&base->lock);
  18848. + base->running_timer = NULL;
  18849. + raw_spin_lock(&base->lock);
  18850. } else {
  18851. - spin_unlock_irq(&base->lock);
  18852. + raw_spin_unlock_irq(&base->lock);
  18853. call_timer_fn(timer, fn, data);
  18854. - spin_lock_irq(&base->lock);
  18855. + base->running_timer = NULL;
  18856. + raw_spin_lock_irq(&base->lock);
  18857. }
  18858. }
  18859. }
  18860. @@ -1505,7 +1538,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  18861. if (cpu_is_offline(smp_processor_id()))
  18862. return expires;
  18863. - spin_lock(&base->lock);
  18864. + raw_spin_lock(&base->lock);
  18865. nextevt = __next_timer_interrupt(base);
  18866. is_max_delta = (nextevt == base->clk + NEXT_TIMER_MAX_DELTA);
  18867. base->next_expiry = nextevt;
  18868. @@ -1529,7 +1562,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
  18869. if ((expires - basem) > TICK_NSEC)
  18870. base->is_idle = true;
  18871. }
  18872. - spin_unlock(&base->lock);
  18873. + raw_spin_unlock(&base->lock);
  18874. return cmp_next_hrtimer_event(basem, expires);
  18875. }
  18876. @@ -1594,13 +1627,13 @@ void update_process_times(int user_tick)
  18877. /* Note: this timer irq context must be accounted for as well. */
  18878. account_process_tick(p, user_tick);
  18879. + scheduler_tick();
  18880. run_local_timers();
  18881. rcu_check_callbacks(user_tick);
  18882. -#ifdef CONFIG_IRQ_WORK
  18883. +#if defined(CONFIG_IRQ_WORK)
  18884. if (in_irq())
  18885. irq_work_tick();
  18886. #endif
  18887. - scheduler_tick();
  18888. run_posix_cpu_timers(p);
  18889. }
  18890. @@ -1616,7 +1649,7 @@ static inline void __run_timers(struct timer_base *base)
  18891. if (!time_after_eq(jiffies, base->clk))
  18892. return;
  18893. - spin_lock_irq(&base->lock);
  18894. + raw_spin_lock_irq(&base->lock);
  18895. while (time_after_eq(jiffies, base->clk)) {
  18896. @@ -1626,8 +1659,8 @@ static inline void __run_timers(struct timer_base *base)
  18897. while (levels--)
  18898. expire_timers(base, heads + levels);
  18899. }
  18900. - base->running_timer = NULL;
  18901. - spin_unlock_irq(&base->lock);
  18902. + raw_spin_unlock_irq(&base->lock);
  18903. + wakeup_timer_waiters(base);
  18904. }
  18905. /*
  18906. @@ -1637,6 +1670,8 @@ static void run_timer_softirq(struct softirq_action *h)
  18907. {
  18908. struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]);
  18909. + irq_work_tick_soft();
  18910. +
  18911. __run_timers(base);
  18912. if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active)
  18913. __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF]));
  18914. @@ -1822,16 +1857,16 @@ int timers_dead_cpu(unsigned int cpu)
  18915. * The caller is globally serialized and nobody else
  18916. * takes two locks at once, deadlock is not possible.
  18917. */
  18918. - spin_lock_irq(&new_base->lock);
  18919. - spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  18920. + raw_spin_lock_irq(&new_base->lock);
  18921. + raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
  18922. BUG_ON(old_base->running_timer);
  18923. for (i = 0; i < WHEEL_SIZE; i++)
  18924. migrate_timer_list(new_base, old_base->vectors + i);
  18925. - spin_unlock(&old_base->lock);
  18926. - spin_unlock_irq(&new_base->lock);
  18927. + raw_spin_unlock(&old_base->lock);
  18928. + raw_spin_unlock_irq(&new_base->lock);
  18929. put_cpu_ptr(&timer_bases);
  18930. }
  18931. return 0;
  18932. @@ -1847,8 +1882,11 @@ static void __init init_timer_cpu(int cpu)
  18933. for (i = 0; i < NR_BASES; i++) {
  18934. base = per_cpu_ptr(&timer_bases[i], cpu);
  18935. base->cpu = cpu;
  18936. - spin_lock_init(&base->lock);
  18937. + raw_spin_lock_init(&base->lock);
  18938. base->clk = jiffies;
  18939. +#ifdef CONFIG_PREEMPT_RT_FULL
  18940. + init_swait_queue_head(&base->wait_for_running_timer);
  18941. +#endif
  18942. }
  18943. }
  18944. diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
  18945. index f4b86e8ca1e7..340f14eef24a 100644
  18946. --- a/kernel/trace/Kconfig
  18947. +++ b/kernel/trace/Kconfig
  18948. @@ -187,6 +187,24 @@ config IRQSOFF_TRACER
  18949. enabled. This option and the preempt-off timing option can be
  18950. used together or separately.)
  18951. +config INTERRUPT_OFF_HIST
  18952. + bool "Interrupts-off Latency Histogram"
  18953. + depends on IRQSOFF_TRACER
  18954. + help
  18955. + This option generates continuously updated histograms (one per cpu)
  18956. + of the duration of time periods with interrupts disabled. The
  18957. + histograms are disabled by default. To enable them, write a non-zero
  18958. + number to
  18959. +
  18960. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  18961. +
  18962. + If PREEMPT_OFF_HIST is also selected, additional histograms (one
  18963. + per cpu) are generated that accumulate the duration of time periods
  18964. + when both interrupts and preemption are disabled. The histogram data
  18965. + will be located in the debug file system at
  18966. +
  18967. + /sys/kernel/debug/tracing/latency_hist/irqsoff
  18968. +
  18969. config PREEMPT_TRACER
  18970. bool "Preemption-off Latency Tracer"
  18971. default n
  18972. @@ -197,6 +215,7 @@ config PREEMPT_TRACER
  18973. select RING_BUFFER_ALLOW_SWAP
  18974. select TRACER_SNAPSHOT
  18975. select TRACER_SNAPSHOT_PER_CPU_SWAP
  18976. + select USING_GET_LOCK_PARENT_IP
  18977. help
  18978. This option measures the time spent in preemption-off critical
  18979. sections, with microsecond accuracy.
  18980. @@ -211,6 +230,24 @@ config PREEMPT_TRACER
  18981. enabled. This option and the irqs-off timing option can be
  18982. used together or separately.)
  18983. +config PREEMPT_OFF_HIST
  18984. + bool "Preemption-off Latency Histogram"
  18985. + depends on PREEMPT_TRACER
  18986. + help
  18987. + This option generates continuously updated histograms (one per cpu)
  18988. + of the duration of time periods with preemption disabled. The
  18989. + histograms are disabled by default. To enable them, write a non-zero
  18990. + number to
  18991. +
  18992. + /sys/kernel/debug/tracing/latency_hist/enable/preemptirqsoff
  18993. +
  18994. + If INTERRUPT_OFF_HIST is also selected, additional histograms (one
  18995. + per cpu) are generated that accumulate the duration of time periods
  18996. + when both interrupts and preemption are disabled. The histogram data
  18997. + will be located in the debug file system at
  18998. +
  18999. + /sys/kernel/debug/tracing/latency_hist/preemptoff
  19000. +
  19001. config SCHED_TRACER
  19002. bool "Scheduling Latency Tracer"
  19003. select GENERIC_TRACER
  19004. @@ -221,6 +258,74 @@ config SCHED_TRACER
  19005. This tracer tracks the latency of the highest priority task
  19006. to be scheduled in, starting from the point it has woken up.
  19007. +config WAKEUP_LATENCY_HIST
  19008. + bool "Scheduling Latency Histogram"
  19009. + depends on SCHED_TRACER
  19010. + help
  19011. + This option generates continuously updated histograms (one per cpu)
  19012. + of the scheduling latency of the highest priority task.
  19013. + The histograms are disabled by default. To enable them, write a
  19014. + non-zero number to
  19015. +
  19016. + /sys/kernel/debug/tracing/latency_hist/enable/wakeup
  19017. +
  19018. + Two different algorithms are used, one to determine the latency of
  19019. + processes that exclusively use the highest priority of the system and
  19020. + another one to determine the latency of processes that share the
  19021. + highest system priority with other processes. The former is used to
  19022. + improve hardware and system software, the latter to optimize the
  19023. + priority design of a given system. The histogram data will be
  19024. + located in the debug file system at
  19025. +
  19026. + /sys/kernel/debug/tracing/latency_hist/wakeup
  19027. +
  19028. + and
  19029. +
  19030. + /sys/kernel/debug/tracing/latency_hist/wakeup/sharedprio
  19031. +
  19032. + If both Scheduling Latency Histogram and Missed Timer Offsets
  19033. + Histogram are selected, additional histogram data will be collected
  19034. + that contain, in addition to the wakeup latency, the timer latency, in
  19035. + case the wakeup was triggered by an expired timer. These histograms
  19036. + are available in the
  19037. +
  19038. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  19039. +
  19040. + directory. They reflect the apparent interrupt and scheduling latency
  19041. + and are best suitable to determine the worst-case latency of a given
  19042. + system. To enable these histograms, write a non-zero number to
  19043. +
  19044. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  19045. +
  19046. +config MISSED_TIMER_OFFSETS_HIST
  19047. + depends on HIGH_RES_TIMERS
  19048. + select GENERIC_TRACER
  19049. + bool "Missed Timer Offsets Histogram"
  19050. + help
  19051. + Generate a histogram of missed timer offsets in microseconds. The
  19052. + histograms are disabled by default. To enable them, write a non-zero
  19053. + number to
  19054. +
  19055. + /sys/kernel/debug/tracing/latency_hist/enable/missed_timer_offsets
  19056. +
  19057. + The histogram data will be located in the debug file system at
  19058. +
  19059. + /sys/kernel/debug/tracing/latency_hist/missed_timer_offsets
  19060. +
  19061. + If both Scheduling Latency Histogram and Missed Timer Offsets
  19062. + Histogram are selected, additional histogram data will be collected
  19063. + that contain, in addition to the wakeup latency, the timer latency, in
  19064. + case the wakeup was triggered by an expired timer. These histograms
  19065. + are available in the
  19066. +
  19067. + /sys/kernel/debug/tracing/latency_hist/timerandwakeup
  19068. +
  19069. + directory. They reflect the apparent interrupt and scheduling latency
  19070. + and are best suitable to determine the worst-case latency of a given
  19071. + system. To enable these histograms, write a non-zero number to
  19072. +
  19073. + /sys/kernel/debug/tracing/latency_hist/enable/timerandwakeup
  19074. +
  19075. config ENABLE_DEFAULT_TRACERS
  19076. bool "Trace process context switches and events"
  19077. depends on !GENERIC_TRACER
  19078. diff --git a/kernel/trace/Makefile b/kernel/trace/Makefile
  19079. index d0a1617b52b4..6bf9e9ff1fa5 100644
  19080. --- a/kernel/trace/Makefile
  19081. +++ b/kernel/trace/Makefile
  19082. @@ -41,6 +41,10 @@ obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o
  19083. obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
  19084. obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
  19085. obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
  19086. +obj-$(CONFIG_INTERRUPT_OFF_HIST) += latency_hist.o
  19087. +obj-$(CONFIG_PREEMPT_OFF_HIST) += latency_hist.o
  19088. +obj-$(CONFIG_WAKEUP_LATENCY_HIST) += latency_hist.o
  19089. +obj-$(CONFIG_MISSED_TIMER_OFFSETS_HIST) += latency_hist.o
  19090. obj-$(CONFIG_NOP_TRACER) += trace_nop.o
  19091. obj-$(CONFIG_STACK_TRACER) += trace_stack.o
  19092. obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
  19093. diff --git a/kernel/trace/latency_hist.c b/kernel/trace/latency_hist.c
  19094. new file mode 100644
  19095. index 000000000000..7f6ee70dea41
  19096. --- /dev/null
  19097. +++ b/kernel/trace/latency_hist.c
  19098. @@ -0,0 +1,1178 @@
  19099. +/*
  19100. + * kernel/trace/latency_hist.c
  19101. + *
  19102. + * Add support for histograms of preemption-off latency and
  19103. + * interrupt-off latency and wakeup latency, it depends on
  19104. + * Real-Time Preemption Support.
  19105. + *
  19106. + * Copyright (C) 2005 MontaVista Software, Inc.
  19107. + * Yi Yang <yyang@ch.mvista.com>
  19108. + *
  19109. + * Converted to work with the new latency tracer.
  19110. + * Copyright (C) 2008 Red Hat, Inc.
  19111. + * Steven Rostedt <srostedt@redhat.com>
  19112. + *
  19113. + */
  19114. +#include <linux/module.h>
  19115. +#include <linux/debugfs.h>
  19116. +#include <linux/seq_file.h>
  19117. +#include <linux/percpu.h>
  19118. +#include <linux/kallsyms.h>
  19119. +#include <linux/uaccess.h>
  19120. +#include <linux/sched.h>
  19121. +#include <linux/sched/rt.h>
  19122. +#include <linux/slab.h>
  19123. +#include <linux/atomic.h>
  19124. +#include <asm/div64.h>
  19125. +
  19126. +#include "trace.h"
  19127. +#include <trace/events/sched.h>
  19128. +
  19129. +#define NSECS_PER_USECS 1000L
  19130. +
  19131. +#define CREATE_TRACE_POINTS
  19132. +#include <trace/events/hist.h>
  19133. +
  19134. +enum {
  19135. + IRQSOFF_LATENCY = 0,
  19136. + PREEMPTOFF_LATENCY,
  19137. + PREEMPTIRQSOFF_LATENCY,
  19138. + WAKEUP_LATENCY,
  19139. + WAKEUP_LATENCY_SHAREDPRIO,
  19140. + MISSED_TIMER_OFFSETS,
  19141. + TIMERANDWAKEUP_LATENCY,
  19142. + MAX_LATENCY_TYPE,
  19143. +};
  19144. +
  19145. +#define MAX_ENTRY_NUM 10240
  19146. +
  19147. +struct hist_data {
  19148. + atomic_t hist_mode; /* 0 log, 1 don't log */
  19149. + long offset; /* set it to MAX_ENTRY_NUM/2 for a bipolar scale */
  19150. + long min_lat;
  19151. + long max_lat;
  19152. + unsigned long long below_hist_bound_samples;
  19153. + unsigned long long above_hist_bound_samples;
  19154. + long long accumulate_lat;
  19155. + unsigned long long total_samples;
  19156. + unsigned long long hist_array[MAX_ENTRY_NUM];
  19157. +};
  19158. +
  19159. +struct enable_data {
  19160. + int latency_type;
  19161. + int enabled;
  19162. +};
  19163. +
  19164. +static char *latency_hist_dir_root = "latency_hist";
  19165. +
  19166. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19167. +static DEFINE_PER_CPU(struct hist_data, irqsoff_hist);
  19168. +static char *irqsoff_hist_dir = "irqsoff";
  19169. +static DEFINE_PER_CPU(cycles_t, hist_irqsoff_start);
  19170. +static DEFINE_PER_CPU(int, hist_irqsoff_counting);
  19171. +#endif
  19172. +
  19173. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19174. +static DEFINE_PER_CPU(struct hist_data, preemptoff_hist);
  19175. +static char *preemptoff_hist_dir = "preemptoff";
  19176. +static DEFINE_PER_CPU(cycles_t, hist_preemptoff_start);
  19177. +static DEFINE_PER_CPU(int, hist_preemptoff_counting);
  19178. +#endif
  19179. +
  19180. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  19181. +static DEFINE_PER_CPU(struct hist_data, preemptirqsoff_hist);
  19182. +static char *preemptirqsoff_hist_dir = "preemptirqsoff";
  19183. +static DEFINE_PER_CPU(cycles_t, hist_preemptirqsoff_start);
  19184. +static DEFINE_PER_CPU(int, hist_preemptirqsoff_counting);
  19185. +#endif
  19186. +
  19187. +#if defined(CONFIG_PREEMPT_OFF_HIST) || defined(CONFIG_INTERRUPT_OFF_HIST)
  19188. +static notrace void probe_preemptirqsoff_hist(void *v, int reason, int start);
  19189. +static struct enable_data preemptirqsoff_enabled_data = {
  19190. + .latency_type = PREEMPTIRQSOFF_LATENCY,
  19191. + .enabled = 0,
  19192. +};
  19193. +#endif
  19194. +
  19195. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19196. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19197. +struct maxlatproc_data {
  19198. + char comm[FIELD_SIZEOF(struct task_struct, comm)];
  19199. + char current_comm[FIELD_SIZEOF(struct task_struct, comm)];
  19200. + int pid;
  19201. + int current_pid;
  19202. + int prio;
  19203. + int current_prio;
  19204. + long latency;
  19205. + long timeroffset;
  19206. + cycle_t timestamp;
  19207. +};
  19208. +#endif
  19209. +
  19210. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19211. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist);
  19212. +static DEFINE_PER_CPU(struct hist_data, wakeup_latency_hist_sharedprio);
  19213. +static char *wakeup_latency_hist_dir = "wakeup";
  19214. +static char *wakeup_latency_hist_dir_sharedprio = "sharedprio";
  19215. +static notrace void probe_wakeup_latency_hist_start(void *v,
  19216. + struct task_struct *p);
  19217. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  19218. + bool preempt, struct task_struct *prev, struct task_struct *next);
  19219. +static notrace void probe_sched_migrate_task(void *,
  19220. + struct task_struct *task, int cpu);
  19221. +static struct enable_data wakeup_latency_enabled_data = {
  19222. + .latency_type = WAKEUP_LATENCY,
  19223. + .enabled = 0,
  19224. +};
  19225. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc);
  19226. +static DEFINE_PER_CPU(struct maxlatproc_data, wakeup_maxlatproc_sharedprio);
  19227. +static DEFINE_PER_CPU(struct task_struct *, wakeup_task);
  19228. +static DEFINE_PER_CPU(int, wakeup_sharedprio);
  19229. +static unsigned long wakeup_pid;
  19230. +#endif
  19231. +
  19232. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19233. +static DEFINE_PER_CPU(struct hist_data, missed_timer_offsets);
  19234. +static char *missed_timer_offsets_dir = "missed_timer_offsets";
  19235. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  19236. + long long offset, struct task_struct *curr, struct task_struct *task);
  19237. +static struct enable_data missed_timer_offsets_enabled_data = {
  19238. + .latency_type = MISSED_TIMER_OFFSETS,
  19239. + .enabled = 0,
  19240. +};
  19241. +static DEFINE_PER_CPU(struct maxlatproc_data, missed_timer_offsets_maxlatproc);
  19242. +static unsigned long missed_timer_offsets_pid;
  19243. +#endif
  19244. +
  19245. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19246. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19247. +static DEFINE_PER_CPU(struct hist_data, timerandwakeup_latency_hist);
  19248. +static char *timerandwakeup_latency_hist_dir = "timerandwakeup";
  19249. +static struct enable_data timerandwakeup_enabled_data = {
  19250. + .latency_type = TIMERANDWAKEUP_LATENCY,
  19251. + .enabled = 0,
  19252. +};
  19253. +static DEFINE_PER_CPU(struct maxlatproc_data, timerandwakeup_maxlatproc);
  19254. +#endif
  19255. +
  19256. +void notrace latency_hist(int latency_type, int cpu, long latency,
  19257. + long timeroffset, cycle_t stop,
  19258. + struct task_struct *p)
  19259. +{
  19260. + struct hist_data *my_hist;
  19261. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19262. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19263. + struct maxlatproc_data *mp = NULL;
  19264. +#endif
  19265. +
  19266. + if (!cpu_possible(cpu) || latency_type < 0 ||
  19267. + latency_type >= MAX_LATENCY_TYPE)
  19268. + return;
  19269. +
  19270. + switch (latency_type) {
  19271. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19272. + case IRQSOFF_LATENCY:
  19273. + my_hist = &per_cpu(irqsoff_hist, cpu);
  19274. + break;
  19275. +#endif
  19276. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19277. + case PREEMPTOFF_LATENCY:
  19278. + my_hist = &per_cpu(preemptoff_hist, cpu);
  19279. + break;
  19280. +#endif
  19281. +#if defined(CONFIG_PREEMPT_OFF_HIST) && defined(CONFIG_INTERRUPT_OFF_HIST)
  19282. + case PREEMPTIRQSOFF_LATENCY:
  19283. + my_hist = &per_cpu(preemptirqsoff_hist, cpu);
  19284. + break;
  19285. +#endif
  19286. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19287. + case WAKEUP_LATENCY:
  19288. + my_hist = &per_cpu(wakeup_latency_hist, cpu);
  19289. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  19290. + break;
  19291. + case WAKEUP_LATENCY_SHAREDPRIO:
  19292. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  19293. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  19294. + break;
  19295. +#endif
  19296. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19297. + case MISSED_TIMER_OFFSETS:
  19298. + my_hist = &per_cpu(missed_timer_offsets, cpu);
  19299. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  19300. + break;
  19301. +#endif
  19302. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19303. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19304. + case TIMERANDWAKEUP_LATENCY:
  19305. + my_hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  19306. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  19307. + break;
  19308. +#endif
  19309. +
  19310. + default:
  19311. + return;
  19312. + }
  19313. +
  19314. + latency += my_hist->offset;
  19315. +
  19316. + if (atomic_read(&my_hist->hist_mode) == 0)
  19317. + return;
  19318. +
  19319. + if (latency < 0 || latency >= MAX_ENTRY_NUM) {
  19320. + if (latency < 0)
  19321. + my_hist->below_hist_bound_samples++;
  19322. + else
  19323. + my_hist->above_hist_bound_samples++;
  19324. + } else
  19325. + my_hist->hist_array[latency]++;
  19326. +
  19327. + if (unlikely(latency > my_hist->max_lat ||
  19328. + my_hist->min_lat == LONG_MAX)) {
  19329. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19330. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19331. + if (latency_type == WAKEUP_LATENCY ||
  19332. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  19333. + latency_type == MISSED_TIMER_OFFSETS ||
  19334. + latency_type == TIMERANDWAKEUP_LATENCY) {
  19335. + strncpy(mp->comm, p->comm, sizeof(mp->comm));
  19336. + strncpy(mp->current_comm, current->comm,
  19337. + sizeof(mp->current_comm));
  19338. + mp->pid = task_pid_nr(p);
  19339. + mp->current_pid = task_pid_nr(current);
  19340. + mp->prio = p->prio;
  19341. + mp->current_prio = current->prio;
  19342. + mp->latency = latency;
  19343. + mp->timeroffset = timeroffset;
  19344. + mp->timestamp = stop;
  19345. + }
  19346. +#endif
  19347. + my_hist->max_lat = latency;
  19348. + }
  19349. + if (unlikely(latency < my_hist->min_lat))
  19350. + my_hist->min_lat = latency;
  19351. + my_hist->total_samples++;
  19352. + my_hist->accumulate_lat += latency;
  19353. +}
  19354. +
  19355. +static void *l_start(struct seq_file *m, loff_t *pos)
  19356. +{
  19357. + loff_t *index_ptr = NULL;
  19358. + loff_t index = *pos;
  19359. + struct hist_data *my_hist = m->private;
  19360. +
  19361. + if (index == 0) {
  19362. + char minstr[32], avgstr[32], maxstr[32];
  19363. +
  19364. + atomic_dec(&my_hist->hist_mode);
  19365. +
  19366. + if (likely(my_hist->total_samples)) {
  19367. + long avg = (long) div64_s64(my_hist->accumulate_lat,
  19368. + my_hist->total_samples);
  19369. + snprintf(minstr, sizeof(minstr), "%ld",
  19370. + my_hist->min_lat - my_hist->offset);
  19371. + snprintf(avgstr, sizeof(avgstr), "%ld",
  19372. + avg - my_hist->offset);
  19373. + snprintf(maxstr, sizeof(maxstr), "%ld",
  19374. + my_hist->max_lat - my_hist->offset);
  19375. + } else {
  19376. + strcpy(minstr, "<undef>");
  19377. + strcpy(avgstr, minstr);
  19378. + strcpy(maxstr, minstr);
  19379. + }
  19380. +
  19381. + seq_printf(m, "#Minimum latency: %s microseconds\n"
  19382. + "#Average latency: %s microseconds\n"
  19383. + "#Maximum latency: %s microseconds\n"
  19384. + "#Total samples: %llu\n"
  19385. + "#There are %llu samples lower than %ld"
  19386. + " microseconds.\n"
  19387. + "#There are %llu samples greater or equal"
  19388. + " than %ld microseconds.\n"
  19389. + "#usecs\t%16s\n",
  19390. + minstr, avgstr, maxstr,
  19391. + my_hist->total_samples,
  19392. + my_hist->below_hist_bound_samples,
  19393. + -my_hist->offset,
  19394. + my_hist->above_hist_bound_samples,
  19395. + MAX_ENTRY_NUM - my_hist->offset,
  19396. + "samples");
  19397. + }
  19398. + if (index < MAX_ENTRY_NUM) {
  19399. + index_ptr = kmalloc(sizeof(loff_t), GFP_KERNEL);
  19400. + if (index_ptr)
  19401. + *index_ptr = index;
  19402. + }
  19403. +
  19404. + return index_ptr;
  19405. +}
  19406. +
  19407. +static void *l_next(struct seq_file *m, void *p, loff_t *pos)
  19408. +{
  19409. + loff_t *index_ptr = p;
  19410. + struct hist_data *my_hist = m->private;
  19411. +
  19412. + if (++*pos >= MAX_ENTRY_NUM) {
  19413. + atomic_inc(&my_hist->hist_mode);
  19414. + return NULL;
  19415. + }
  19416. + *index_ptr = *pos;
  19417. + return index_ptr;
  19418. +}
  19419. +
  19420. +static void l_stop(struct seq_file *m, void *p)
  19421. +{
  19422. + kfree(p);
  19423. +}
  19424. +
  19425. +static int l_show(struct seq_file *m, void *p)
  19426. +{
  19427. + int index = *(loff_t *) p;
  19428. + struct hist_data *my_hist = m->private;
  19429. +
  19430. + seq_printf(m, "%6ld\t%16llu\n", index - my_hist->offset,
  19431. + my_hist->hist_array[index]);
  19432. + return 0;
  19433. +}
  19434. +
  19435. +static const struct seq_operations latency_hist_seq_op = {
  19436. + .start = l_start,
  19437. + .next = l_next,
  19438. + .stop = l_stop,
  19439. + .show = l_show
  19440. +};
  19441. +
  19442. +static int latency_hist_open(struct inode *inode, struct file *file)
  19443. +{
  19444. + int ret;
  19445. +
  19446. + ret = seq_open(file, &latency_hist_seq_op);
  19447. + if (!ret) {
  19448. + struct seq_file *seq = file->private_data;
  19449. + seq->private = inode->i_private;
  19450. + }
  19451. + return ret;
  19452. +}
  19453. +
  19454. +static const struct file_operations latency_hist_fops = {
  19455. + .open = latency_hist_open,
  19456. + .read = seq_read,
  19457. + .llseek = seq_lseek,
  19458. + .release = seq_release,
  19459. +};
  19460. +
  19461. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19462. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19463. +static void clear_maxlatprocdata(struct maxlatproc_data *mp)
  19464. +{
  19465. + mp->comm[0] = mp->current_comm[0] = '\0';
  19466. + mp->prio = mp->current_prio = mp->pid = mp->current_pid =
  19467. + mp->latency = mp->timeroffset = -1;
  19468. + mp->timestamp = 0;
  19469. +}
  19470. +#endif
  19471. +
  19472. +static void hist_reset(struct hist_data *hist)
  19473. +{
  19474. + atomic_dec(&hist->hist_mode);
  19475. +
  19476. + memset(hist->hist_array, 0, sizeof(hist->hist_array));
  19477. + hist->below_hist_bound_samples = 0ULL;
  19478. + hist->above_hist_bound_samples = 0ULL;
  19479. + hist->min_lat = LONG_MAX;
  19480. + hist->max_lat = LONG_MIN;
  19481. + hist->total_samples = 0ULL;
  19482. + hist->accumulate_lat = 0LL;
  19483. +
  19484. + atomic_inc(&hist->hist_mode);
  19485. +}
  19486. +
  19487. +static ssize_t
  19488. +latency_hist_reset(struct file *file, const char __user *a,
  19489. + size_t size, loff_t *off)
  19490. +{
  19491. + int cpu;
  19492. + struct hist_data *hist = NULL;
  19493. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19494. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19495. + struct maxlatproc_data *mp = NULL;
  19496. +#endif
  19497. + off_t latency_type = (off_t) file->private_data;
  19498. +
  19499. + for_each_online_cpu(cpu) {
  19500. +
  19501. + switch (latency_type) {
  19502. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19503. + case PREEMPTOFF_LATENCY:
  19504. + hist = &per_cpu(preemptoff_hist, cpu);
  19505. + break;
  19506. +#endif
  19507. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19508. + case IRQSOFF_LATENCY:
  19509. + hist = &per_cpu(irqsoff_hist, cpu);
  19510. + break;
  19511. +#endif
  19512. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  19513. + case PREEMPTIRQSOFF_LATENCY:
  19514. + hist = &per_cpu(preemptirqsoff_hist, cpu);
  19515. + break;
  19516. +#endif
  19517. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19518. + case WAKEUP_LATENCY:
  19519. + hist = &per_cpu(wakeup_latency_hist, cpu);
  19520. + mp = &per_cpu(wakeup_maxlatproc, cpu);
  19521. + break;
  19522. + case WAKEUP_LATENCY_SHAREDPRIO:
  19523. + hist = &per_cpu(wakeup_latency_hist_sharedprio, cpu);
  19524. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, cpu);
  19525. + break;
  19526. +#endif
  19527. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19528. + case MISSED_TIMER_OFFSETS:
  19529. + hist = &per_cpu(missed_timer_offsets, cpu);
  19530. + mp = &per_cpu(missed_timer_offsets_maxlatproc, cpu);
  19531. + break;
  19532. +#endif
  19533. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19534. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19535. + case TIMERANDWAKEUP_LATENCY:
  19536. + hist = &per_cpu(timerandwakeup_latency_hist, cpu);
  19537. + mp = &per_cpu(timerandwakeup_maxlatproc, cpu);
  19538. + break;
  19539. +#endif
  19540. + }
  19541. +
  19542. + hist_reset(hist);
  19543. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19544. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19545. + if (latency_type == WAKEUP_LATENCY ||
  19546. + latency_type == WAKEUP_LATENCY_SHAREDPRIO ||
  19547. + latency_type == MISSED_TIMER_OFFSETS ||
  19548. + latency_type == TIMERANDWAKEUP_LATENCY)
  19549. + clear_maxlatprocdata(mp);
  19550. +#endif
  19551. + }
  19552. +
  19553. + return size;
  19554. +}
  19555. +
  19556. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19557. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19558. +static ssize_t
  19559. +show_pid(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  19560. +{
  19561. + char buf[64];
  19562. + int r;
  19563. + unsigned long *this_pid = file->private_data;
  19564. +
  19565. + r = snprintf(buf, sizeof(buf), "%lu\n", *this_pid);
  19566. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  19567. +}
  19568. +
  19569. +static ssize_t do_pid(struct file *file, const char __user *ubuf,
  19570. + size_t cnt, loff_t *ppos)
  19571. +{
  19572. + char buf[64];
  19573. + unsigned long pid;
  19574. + unsigned long *this_pid = file->private_data;
  19575. +
  19576. + if (cnt >= sizeof(buf))
  19577. + return -EINVAL;
  19578. +
  19579. + if (copy_from_user(&buf, ubuf, cnt))
  19580. + return -EFAULT;
  19581. +
  19582. + buf[cnt] = '\0';
  19583. +
  19584. + if (kstrtoul(buf, 10, &pid))
  19585. + return -EINVAL;
  19586. +
  19587. + *this_pid = pid;
  19588. +
  19589. + return cnt;
  19590. +}
  19591. +#endif
  19592. +
  19593. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19594. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19595. +static ssize_t
  19596. +show_maxlatproc(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  19597. +{
  19598. + int r;
  19599. + struct maxlatproc_data *mp = file->private_data;
  19600. + int strmaxlen = (TASK_COMM_LEN * 2) + (8 * 8);
  19601. + unsigned long long t;
  19602. + unsigned long usecs, secs;
  19603. + char *buf;
  19604. +
  19605. + if (mp->pid == -1 || mp->current_pid == -1) {
  19606. + buf = "(none)\n";
  19607. + return simple_read_from_buffer(ubuf, cnt, ppos, buf,
  19608. + strlen(buf));
  19609. + }
  19610. +
  19611. + buf = kmalloc(strmaxlen, GFP_KERNEL);
  19612. + if (buf == NULL)
  19613. + return -ENOMEM;
  19614. +
  19615. + t = ns2usecs(mp->timestamp);
  19616. + usecs = do_div(t, USEC_PER_SEC);
  19617. + secs = (unsigned long) t;
  19618. + r = snprintf(buf, strmaxlen,
  19619. + "%d %d %ld (%ld) %s <- %d %d %s %lu.%06lu\n", mp->pid,
  19620. + MAX_RT_PRIO-1 - mp->prio, mp->latency, mp->timeroffset, mp->comm,
  19621. + mp->current_pid, MAX_RT_PRIO-1 - mp->current_prio, mp->current_comm,
  19622. + secs, usecs);
  19623. + r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  19624. + kfree(buf);
  19625. + return r;
  19626. +}
  19627. +#endif
  19628. +
  19629. +static ssize_t
  19630. +show_enable(struct file *file, char __user *ubuf, size_t cnt, loff_t *ppos)
  19631. +{
  19632. + char buf[64];
  19633. + struct enable_data *ed = file->private_data;
  19634. + int r;
  19635. +
  19636. + r = snprintf(buf, sizeof(buf), "%d\n", ed->enabled);
  19637. + return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  19638. +}
  19639. +
  19640. +static ssize_t
  19641. +do_enable(struct file *file, const char __user *ubuf, size_t cnt, loff_t *ppos)
  19642. +{
  19643. + char buf[64];
  19644. + long enable;
  19645. + struct enable_data *ed = file->private_data;
  19646. +
  19647. + if (cnt >= sizeof(buf))
  19648. + return -EINVAL;
  19649. +
  19650. + if (copy_from_user(&buf, ubuf, cnt))
  19651. + return -EFAULT;
  19652. +
  19653. + buf[cnt] = 0;
  19654. +
  19655. + if (kstrtoul(buf, 10, &enable))
  19656. + return -EINVAL;
  19657. +
  19658. + if ((enable && ed->enabled) || (!enable && !ed->enabled))
  19659. + return cnt;
  19660. +
  19661. + if (enable) {
  19662. + int ret;
  19663. +
  19664. + switch (ed->latency_type) {
  19665. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  19666. + case PREEMPTIRQSOFF_LATENCY:
  19667. + ret = register_trace_preemptirqsoff_hist(
  19668. + probe_preemptirqsoff_hist, NULL);
  19669. + if (ret) {
  19670. + pr_info("wakeup trace: Couldn't assign "
  19671. + "probe_preemptirqsoff_hist "
  19672. + "to trace_preemptirqsoff_hist\n");
  19673. + return ret;
  19674. + }
  19675. + break;
  19676. +#endif
  19677. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19678. + case WAKEUP_LATENCY:
  19679. + ret = register_trace_sched_wakeup(
  19680. + probe_wakeup_latency_hist_start, NULL);
  19681. + if (ret) {
  19682. + pr_info("wakeup trace: Couldn't assign "
  19683. + "probe_wakeup_latency_hist_start "
  19684. + "to trace_sched_wakeup\n");
  19685. + return ret;
  19686. + }
  19687. + ret = register_trace_sched_wakeup_new(
  19688. + probe_wakeup_latency_hist_start, NULL);
  19689. + if (ret) {
  19690. + pr_info("wakeup trace: Couldn't assign "
  19691. + "probe_wakeup_latency_hist_start "
  19692. + "to trace_sched_wakeup_new\n");
  19693. + unregister_trace_sched_wakeup(
  19694. + probe_wakeup_latency_hist_start, NULL);
  19695. + return ret;
  19696. + }
  19697. + ret = register_trace_sched_switch(
  19698. + probe_wakeup_latency_hist_stop, NULL);
  19699. + if (ret) {
  19700. + pr_info("wakeup trace: Couldn't assign "
  19701. + "probe_wakeup_latency_hist_stop "
  19702. + "to trace_sched_switch\n");
  19703. + unregister_trace_sched_wakeup(
  19704. + probe_wakeup_latency_hist_start, NULL);
  19705. + unregister_trace_sched_wakeup_new(
  19706. + probe_wakeup_latency_hist_start, NULL);
  19707. + return ret;
  19708. + }
  19709. + ret = register_trace_sched_migrate_task(
  19710. + probe_sched_migrate_task, NULL);
  19711. + if (ret) {
  19712. + pr_info("wakeup trace: Couldn't assign "
  19713. + "probe_sched_migrate_task "
  19714. + "to trace_sched_migrate_task\n");
  19715. + unregister_trace_sched_wakeup(
  19716. + probe_wakeup_latency_hist_start, NULL);
  19717. + unregister_trace_sched_wakeup_new(
  19718. + probe_wakeup_latency_hist_start, NULL);
  19719. + unregister_trace_sched_switch(
  19720. + probe_wakeup_latency_hist_stop, NULL);
  19721. + return ret;
  19722. + }
  19723. + break;
  19724. +#endif
  19725. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19726. + case MISSED_TIMER_OFFSETS:
  19727. + ret = register_trace_hrtimer_interrupt(
  19728. + probe_hrtimer_interrupt, NULL);
  19729. + if (ret) {
  19730. + pr_info("wakeup trace: Couldn't assign "
  19731. + "probe_hrtimer_interrupt "
  19732. + "to trace_hrtimer_interrupt\n");
  19733. + return ret;
  19734. + }
  19735. + break;
  19736. +#endif
  19737. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  19738. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19739. + case TIMERANDWAKEUP_LATENCY:
  19740. + if (!wakeup_latency_enabled_data.enabled ||
  19741. + !missed_timer_offsets_enabled_data.enabled)
  19742. + return -EINVAL;
  19743. + break;
  19744. +#endif
  19745. + default:
  19746. + break;
  19747. + }
  19748. + } else {
  19749. + switch (ed->latency_type) {
  19750. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  19751. + case PREEMPTIRQSOFF_LATENCY:
  19752. + {
  19753. + int cpu;
  19754. +
  19755. + unregister_trace_preemptirqsoff_hist(
  19756. + probe_preemptirqsoff_hist, NULL);
  19757. + for_each_online_cpu(cpu) {
  19758. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19759. + per_cpu(hist_irqsoff_counting,
  19760. + cpu) = 0;
  19761. +#endif
  19762. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19763. + per_cpu(hist_preemptoff_counting,
  19764. + cpu) = 0;
  19765. +#endif
  19766. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  19767. + per_cpu(hist_preemptirqsoff_counting,
  19768. + cpu) = 0;
  19769. +#endif
  19770. + }
  19771. + }
  19772. + break;
  19773. +#endif
  19774. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19775. + case WAKEUP_LATENCY:
  19776. + {
  19777. + int cpu;
  19778. +
  19779. + unregister_trace_sched_wakeup(
  19780. + probe_wakeup_latency_hist_start, NULL);
  19781. + unregister_trace_sched_wakeup_new(
  19782. + probe_wakeup_latency_hist_start, NULL);
  19783. + unregister_trace_sched_switch(
  19784. + probe_wakeup_latency_hist_stop, NULL);
  19785. + unregister_trace_sched_migrate_task(
  19786. + probe_sched_migrate_task, NULL);
  19787. +
  19788. + for_each_online_cpu(cpu) {
  19789. + per_cpu(wakeup_task, cpu) = NULL;
  19790. + per_cpu(wakeup_sharedprio, cpu) = 0;
  19791. + }
  19792. + }
  19793. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19794. + timerandwakeup_enabled_data.enabled = 0;
  19795. +#endif
  19796. + break;
  19797. +#endif
  19798. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  19799. + case MISSED_TIMER_OFFSETS:
  19800. + unregister_trace_hrtimer_interrupt(
  19801. + probe_hrtimer_interrupt, NULL);
  19802. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19803. + timerandwakeup_enabled_data.enabled = 0;
  19804. +#endif
  19805. + break;
  19806. +#endif
  19807. + default:
  19808. + break;
  19809. + }
  19810. + }
  19811. + ed->enabled = enable;
  19812. + return cnt;
  19813. +}
  19814. +
  19815. +static const struct file_operations latency_hist_reset_fops = {
  19816. + .open = tracing_open_generic,
  19817. + .write = latency_hist_reset,
  19818. +};
  19819. +
  19820. +static const struct file_operations enable_fops = {
  19821. + .open = tracing_open_generic,
  19822. + .read = show_enable,
  19823. + .write = do_enable,
  19824. +};
  19825. +
  19826. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  19827. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  19828. +static const struct file_operations pid_fops = {
  19829. + .open = tracing_open_generic,
  19830. + .read = show_pid,
  19831. + .write = do_pid,
  19832. +};
  19833. +
  19834. +static const struct file_operations maxlatproc_fops = {
  19835. + .open = tracing_open_generic,
  19836. + .read = show_maxlatproc,
  19837. +};
  19838. +#endif
  19839. +
  19840. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  19841. +static notrace void probe_preemptirqsoff_hist(void *v, int reason,
  19842. + int starthist)
  19843. +{
  19844. + int cpu = raw_smp_processor_id();
  19845. + int time_set = 0;
  19846. +
  19847. + if (starthist) {
  19848. + cycle_t uninitialized_var(start);
  19849. +
  19850. + if (!preempt_count() && !irqs_disabled())
  19851. + return;
  19852. +
  19853. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19854. + if ((reason == IRQS_OFF || reason == TRACE_START) &&
  19855. + !per_cpu(hist_irqsoff_counting, cpu)) {
  19856. + per_cpu(hist_irqsoff_counting, cpu) = 1;
  19857. + start = ftrace_now(cpu);
  19858. + time_set++;
  19859. + per_cpu(hist_irqsoff_start, cpu) = start;
  19860. + }
  19861. +#endif
  19862. +
  19863. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19864. + if ((reason == PREEMPT_OFF || reason == TRACE_START) &&
  19865. + !per_cpu(hist_preemptoff_counting, cpu)) {
  19866. + per_cpu(hist_preemptoff_counting, cpu) = 1;
  19867. + if (!(time_set++))
  19868. + start = ftrace_now(cpu);
  19869. + per_cpu(hist_preemptoff_start, cpu) = start;
  19870. + }
  19871. +#endif
  19872. +
  19873. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  19874. + if (per_cpu(hist_irqsoff_counting, cpu) &&
  19875. + per_cpu(hist_preemptoff_counting, cpu) &&
  19876. + !per_cpu(hist_preemptirqsoff_counting, cpu)) {
  19877. + per_cpu(hist_preemptirqsoff_counting, cpu) = 1;
  19878. + if (!time_set)
  19879. + start = ftrace_now(cpu);
  19880. + per_cpu(hist_preemptirqsoff_start, cpu) = start;
  19881. + }
  19882. +#endif
  19883. + } else {
  19884. + cycle_t uninitialized_var(stop);
  19885. +
  19886. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  19887. + if ((reason == IRQS_ON || reason == TRACE_STOP) &&
  19888. + per_cpu(hist_irqsoff_counting, cpu)) {
  19889. + cycle_t start = per_cpu(hist_irqsoff_start, cpu);
  19890. +
  19891. + stop = ftrace_now(cpu);
  19892. + time_set++;
  19893. + if (start) {
  19894. + long latency = ((long) (stop - start)) /
  19895. + NSECS_PER_USECS;
  19896. +
  19897. + latency_hist(IRQSOFF_LATENCY, cpu, latency, 0,
  19898. + stop, NULL);
  19899. + }
  19900. + per_cpu(hist_irqsoff_counting, cpu) = 0;
  19901. + }
  19902. +#endif
  19903. +
  19904. +#ifdef CONFIG_PREEMPT_OFF_HIST
  19905. + if ((reason == PREEMPT_ON || reason == TRACE_STOP) &&
  19906. + per_cpu(hist_preemptoff_counting, cpu)) {
  19907. + cycle_t start = per_cpu(hist_preemptoff_start, cpu);
  19908. +
  19909. + if (!(time_set++))
  19910. + stop = ftrace_now(cpu);
  19911. + if (start) {
  19912. + long latency = ((long) (stop - start)) /
  19913. + NSECS_PER_USECS;
  19914. +
  19915. + latency_hist(PREEMPTOFF_LATENCY, cpu, latency,
  19916. + 0, stop, NULL);
  19917. + }
  19918. + per_cpu(hist_preemptoff_counting, cpu) = 0;
  19919. + }
  19920. +#endif
  19921. +
  19922. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  19923. + if ((!per_cpu(hist_irqsoff_counting, cpu) ||
  19924. + !per_cpu(hist_preemptoff_counting, cpu)) &&
  19925. + per_cpu(hist_preemptirqsoff_counting, cpu)) {
  19926. + cycle_t start = per_cpu(hist_preemptirqsoff_start, cpu);
  19927. +
  19928. + if (!time_set)
  19929. + stop = ftrace_now(cpu);
  19930. + if (start) {
  19931. + long latency = ((long) (stop - start)) /
  19932. + NSECS_PER_USECS;
  19933. +
  19934. + latency_hist(PREEMPTIRQSOFF_LATENCY, cpu,
  19935. + latency, 0, stop, NULL);
  19936. + }
  19937. + per_cpu(hist_preemptirqsoff_counting, cpu) = 0;
  19938. + }
  19939. +#endif
  19940. + }
  19941. +}
  19942. +#endif
  19943. +
  19944. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  19945. +static DEFINE_RAW_SPINLOCK(wakeup_lock);
  19946. +static notrace void probe_sched_migrate_task(void *v, struct task_struct *task,
  19947. + int cpu)
  19948. +{
  19949. + int old_cpu = task_cpu(task);
  19950. +
  19951. + if (cpu != old_cpu) {
  19952. + unsigned long flags;
  19953. + struct task_struct *cpu_wakeup_task;
  19954. +
  19955. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  19956. +
  19957. + cpu_wakeup_task = per_cpu(wakeup_task, old_cpu);
  19958. + if (task == cpu_wakeup_task) {
  19959. + put_task_struct(cpu_wakeup_task);
  19960. + per_cpu(wakeup_task, old_cpu) = NULL;
  19961. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = task;
  19962. + get_task_struct(cpu_wakeup_task);
  19963. + }
  19964. +
  19965. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  19966. + }
  19967. +}
  19968. +
  19969. +static notrace void probe_wakeup_latency_hist_start(void *v,
  19970. + struct task_struct *p)
  19971. +{
  19972. + unsigned long flags;
  19973. + struct task_struct *curr = current;
  19974. + int cpu = task_cpu(p);
  19975. + struct task_struct *cpu_wakeup_task;
  19976. +
  19977. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  19978. +
  19979. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  19980. +
  19981. + if (wakeup_pid) {
  19982. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  19983. + p->prio == curr->prio)
  19984. + per_cpu(wakeup_sharedprio, cpu) = 1;
  19985. + if (likely(wakeup_pid != task_pid_nr(p)))
  19986. + goto out;
  19987. + } else {
  19988. + if (likely(!rt_task(p)) ||
  19989. + (cpu_wakeup_task && p->prio > cpu_wakeup_task->prio) ||
  19990. + p->prio > curr->prio)
  19991. + goto out;
  19992. + if ((cpu_wakeup_task && p->prio == cpu_wakeup_task->prio) ||
  19993. + p->prio == curr->prio)
  19994. + per_cpu(wakeup_sharedprio, cpu) = 1;
  19995. + }
  19996. +
  19997. + if (cpu_wakeup_task)
  19998. + put_task_struct(cpu_wakeup_task);
  19999. + cpu_wakeup_task = per_cpu(wakeup_task, cpu) = p;
  20000. + get_task_struct(cpu_wakeup_task);
  20001. + cpu_wakeup_task->preempt_timestamp_hist =
  20002. + ftrace_now(raw_smp_processor_id());
  20003. +out:
  20004. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  20005. +}
  20006. +
  20007. +static notrace void probe_wakeup_latency_hist_stop(void *v,
  20008. + bool preempt, struct task_struct *prev, struct task_struct *next)
  20009. +{
  20010. + unsigned long flags;
  20011. + int cpu = task_cpu(next);
  20012. + long latency;
  20013. + cycle_t stop;
  20014. + struct task_struct *cpu_wakeup_task;
  20015. +
  20016. + raw_spin_lock_irqsave(&wakeup_lock, flags);
  20017. +
  20018. + cpu_wakeup_task = per_cpu(wakeup_task, cpu);
  20019. +
  20020. + if (cpu_wakeup_task == NULL)
  20021. + goto out;
  20022. +
  20023. + /* Already running? */
  20024. + if (unlikely(current == cpu_wakeup_task))
  20025. + goto out_reset;
  20026. +
  20027. + if (next != cpu_wakeup_task) {
  20028. + if (next->prio < cpu_wakeup_task->prio)
  20029. + goto out_reset;
  20030. +
  20031. + if (next->prio == cpu_wakeup_task->prio)
  20032. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20033. +
  20034. + goto out;
  20035. + }
  20036. +
  20037. + if (current->prio == cpu_wakeup_task->prio)
  20038. + per_cpu(wakeup_sharedprio, cpu) = 1;
  20039. +
  20040. + /*
  20041. + * The task we are waiting for is about to be switched to.
  20042. + * Calculate latency and store it in histogram.
  20043. + */
  20044. + stop = ftrace_now(raw_smp_processor_id());
  20045. +
  20046. + latency = ((long) (stop - next->preempt_timestamp_hist)) /
  20047. + NSECS_PER_USECS;
  20048. +
  20049. + if (per_cpu(wakeup_sharedprio, cpu)) {
  20050. + latency_hist(WAKEUP_LATENCY_SHAREDPRIO, cpu, latency, 0, stop,
  20051. + next);
  20052. + per_cpu(wakeup_sharedprio, cpu) = 0;
  20053. + } else {
  20054. + latency_hist(WAKEUP_LATENCY, cpu, latency, 0, stop, next);
  20055. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20056. + if (timerandwakeup_enabled_data.enabled) {
  20057. + latency_hist(TIMERANDWAKEUP_LATENCY, cpu,
  20058. + next->timer_offset + latency, next->timer_offset,
  20059. + stop, next);
  20060. + }
  20061. +#endif
  20062. + }
  20063. +
  20064. +out_reset:
  20065. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20066. + next->timer_offset = 0;
  20067. +#endif
  20068. + put_task_struct(cpu_wakeup_task);
  20069. + per_cpu(wakeup_task, cpu) = NULL;
  20070. +out:
  20071. + raw_spin_unlock_irqrestore(&wakeup_lock, flags);
  20072. +}
  20073. +#endif
  20074. +
  20075. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20076. +static notrace void probe_hrtimer_interrupt(void *v, int cpu,
  20077. + long long latency_ns, struct task_struct *curr,
  20078. + struct task_struct *task)
  20079. +{
  20080. + if (latency_ns <= 0 && task != NULL && rt_task(task) &&
  20081. + (task->prio < curr->prio ||
  20082. + (task->prio == curr->prio &&
  20083. + !cpumask_test_cpu(cpu, &task->cpus_allowed)))) {
  20084. + long latency;
  20085. + cycle_t now;
  20086. +
  20087. + if (missed_timer_offsets_pid) {
  20088. + if (likely(missed_timer_offsets_pid !=
  20089. + task_pid_nr(task)))
  20090. + return;
  20091. + }
  20092. +
  20093. + now = ftrace_now(cpu);
  20094. + latency = (long) div_s64(-latency_ns, NSECS_PER_USECS);
  20095. + latency_hist(MISSED_TIMER_OFFSETS, cpu, latency, latency, now,
  20096. + task);
  20097. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20098. + task->timer_offset = latency;
  20099. +#endif
  20100. + }
  20101. +}
  20102. +#endif
  20103. +
  20104. +static __init int latency_hist_init(void)
  20105. +{
  20106. + struct dentry *latency_hist_root = NULL;
  20107. + struct dentry *dentry;
  20108. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20109. + struct dentry *dentry_sharedprio;
  20110. +#endif
  20111. + struct dentry *entry;
  20112. + struct dentry *enable_root;
  20113. + int i = 0;
  20114. + struct hist_data *my_hist;
  20115. + char name[64];
  20116. + char *cpufmt = "CPU%d";
  20117. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) || \
  20118. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20119. + char *cpufmt_maxlatproc = "max_latency-CPU%d";
  20120. + struct maxlatproc_data *mp = NULL;
  20121. +#endif
  20122. +
  20123. + dentry = tracing_init_dentry();
  20124. + latency_hist_root = debugfs_create_dir(latency_hist_dir_root, dentry);
  20125. + enable_root = debugfs_create_dir("enable", latency_hist_root);
  20126. +
  20127. +#ifdef CONFIG_INTERRUPT_OFF_HIST
  20128. + dentry = debugfs_create_dir(irqsoff_hist_dir, latency_hist_root);
  20129. + for_each_possible_cpu(i) {
  20130. + sprintf(name, cpufmt, i);
  20131. + entry = debugfs_create_file(name, 0444, dentry,
  20132. + &per_cpu(irqsoff_hist, i), &latency_hist_fops);
  20133. + my_hist = &per_cpu(irqsoff_hist, i);
  20134. + atomic_set(&my_hist->hist_mode, 1);
  20135. + my_hist->min_lat = LONG_MAX;
  20136. + }
  20137. + entry = debugfs_create_file("reset", 0644, dentry,
  20138. + (void *)IRQSOFF_LATENCY, &latency_hist_reset_fops);
  20139. +#endif
  20140. +
  20141. +#ifdef CONFIG_PREEMPT_OFF_HIST
  20142. + dentry = debugfs_create_dir(preemptoff_hist_dir,
  20143. + latency_hist_root);
  20144. + for_each_possible_cpu(i) {
  20145. + sprintf(name, cpufmt, i);
  20146. + entry = debugfs_create_file(name, 0444, dentry,
  20147. + &per_cpu(preemptoff_hist, i), &latency_hist_fops);
  20148. + my_hist = &per_cpu(preemptoff_hist, i);
  20149. + atomic_set(&my_hist->hist_mode, 1);
  20150. + my_hist->min_lat = LONG_MAX;
  20151. + }
  20152. + entry = debugfs_create_file("reset", 0644, dentry,
  20153. + (void *)PREEMPTOFF_LATENCY, &latency_hist_reset_fops);
  20154. +#endif
  20155. +
  20156. +#if defined(CONFIG_INTERRUPT_OFF_HIST) && defined(CONFIG_PREEMPT_OFF_HIST)
  20157. + dentry = debugfs_create_dir(preemptirqsoff_hist_dir,
  20158. + latency_hist_root);
  20159. + for_each_possible_cpu(i) {
  20160. + sprintf(name, cpufmt, i);
  20161. + entry = debugfs_create_file(name, 0444, dentry,
  20162. + &per_cpu(preemptirqsoff_hist, i), &latency_hist_fops);
  20163. + my_hist = &per_cpu(preemptirqsoff_hist, i);
  20164. + atomic_set(&my_hist->hist_mode, 1);
  20165. + my_hist->min_lat = LONG_MAX;
  20166. + }
  20167. + entry = debugfs_create_file("reset", 0644, dentry,
  20168. + (void *)PREEMPTIRQSOFF_LATENCY, &latency_hist_reset_fops);
  20169. +#endif
  20170. +
  20171. +#if defined(CONFIG_INTERRUPT_OFF_HIST) || defined(CONFIG_PREEMPT_OFF_HIST)
  20172. + entry = debugfs_create_file("preemptirqsoff", 0644,
  20173. + enable_root, (void *)&preemptirqsoff_enabled_data,
  20174. + &enable_fops);
  20175. +#endif
  20176. +
  20177. +#ifdef CONFIG_WAKEUP_LATENCY_HIST
  20178. + dentry = debugfs_create_dir(wakeup_latency_hist_dir,
  20179. + latency_hist_root);
  20180. + dentry_sharedprio = debugfs_create_dir(
  20181. + wakeup_latency_hist_dir_sharedprio, dentry);
  20182. + for_each_possible_cpu(i) {
  20183. + sprintf(name, cpufmt, i);
  20184. +
  20185. + entry = debugfs_create_file(name, 0444, dentry,
  20186. + &per_cpu(wakeup_latency_hist, i),
  20187. + &latency_hist_fops);
  20188. + my_hist = &per_cpu(wakeup_latency_hist, i);
  20189. + atomic_set(&my_hist->hist_mode, 1);
  20190. + my_hist->min_lat = LONG_MAX;
  20191. +
  20192. + entry = debugfs_create_file(name, 0444, dentry_sharedprio,
  20193. + &per_cpu(wakeup_latency_hist_sharedprio, i),
  20194. + &latency_hist_fops);
  20195. + my_hist = &per_cpu(wakeup_latency_hist_sharedprio, i);
  20196. + atomic_set(&my_hist->hist_mode, 1);
  20197. + my_hist->min_lat = LONG_MAX;
  20198. +
  20199. + sprintf(name, cpufmt_maxlatproc, i);
  20200. +
  20201. + mp = &per_cpu(wakeup_maxlatproc, i);
  20202. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20203. + &maxlatproc_fops);
  20204. + clear_maxlatprocdata(mp);
  20205. +
  20206. + mp = &per_cpu(wakeup_maxlatproc_sharedprio, i);
  20207. + entry = debugfs_create_file(name, 0444, dentry_sharedprio, mp,
  20208. + &maxlatproc_fops);
  20209. + clear_maxlatprocdata(mp);
  20210. + }
  20211. + entry = debugfs_create_file("pid", 0644, dentry,
  20212. + (void *)&wakeup_pid, &pid_fops);
  20213. + entry = debugfs_create_file("reset", 0644, dentry,
  20214. + (void *)WAKEUP_LATENCY, &latency_hist_reset_fops);
  20215. + entry = debugfs_create_file("reset", 0644, dentry_sharedprio,
  20216. + (void *)WAKEUP_LATENCY_SHAREDPRIO, &latency_hist_reset_fops);
  20217. + entry = debugfs_create_file("wakeup", 0644,
  20218. + enable_root, (void *)&wakeup_latency_enabled_data,
  20219. + &enable_fops);
  20220. +#endif
  20221. +
  20222. +#ifdef CONFIG_MISSED_TIMER_OFFSETS_HIST
  20223. + dentry = debugfs_create_dir(missed_timer_offsets_dir,
  20224. + latency_hist_root);
  20225. + for_each_possible_cpu(i) {
  20226. + sprintf(name, cpufmt, i);
  20227. + entry = debugfs_create_file(name, 0444, dentry,
  20228. + &per_cpu(missed_timer_offsets, i), &latency_hist_fops);
  20229. + my_hist = &per_cpu(missed_timer_offsets, i);
  20230. + atomic_set(&my_hist->hist_mode, 1);
  20231. + my_hist->min_lat = LONG_MAX;
  20232. +
  20233. + sprintf(name, cpufmt_maxlatproc, i);
  20234. + mp = &per_cpu(missed_timer_offsets_maxlatproc, i);
  20235. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20236. + &maxlatproc_fops);
  20237. + clear_maxlatprocdata(mp);
  20238. + }
  20239. + entry = debugfs_create_file("pid", 0644, dentry,
  20240. + (void *)&missed_timer_offsets_pid, &pid_fops);
  20241. + entry = debugfs_create_file("reset", 0644, dentry,
  20242. + (void *)MISSED_TIMER_OFFSETS, &latency_hist_reset_fops);
  20243. + entry = debugfs_create_file("missed_timer_offsets", 0644,
  20244. + enable_root, (void *)&missed_timer_offsets_enabled_data,
  20245. + &enable_fops);
  20246. +#endif
  20247. +
  20248. +#if defined(CONFIG_WAKEUP_LATENCY_HIST) && \
  20249. + defined(CONFIG_MISSED_TIMER_OFFSETS_HIST)
  20250. + dentry = debugfs_create_dir(timerandwakeup_latency_hist_dir,
  20251. + latency_hist_root);
  20252. + for_each_possible_cpu(i) {
  20253. + sprintf(name, cpufmt, i);
  20254. + entry = debugfs_create_file(name, 0444, dentry,
  20255. + &per_cpu(timerandwakeup_latency_hist, i),
  20256. + &latency_hist_fops);
  20257. + my_hist = &per_cpu(timerandwakeup_latency_hist, i);
  20258. + atomic_set(&my_hist->hist_mode, 1);
  20259. + my_hist->min_lat = LONG_MAX;
  20260. +
  20261. + sprintf(name, cpufmt_maxlatproc, i);
  20262. + mp = &per_cpu(timerandwakeup_maxlatproc, i);
  20263. + entry = debugfs_create_file(name, 0444, dentry, mp,
  20264. + &maxlatproc_fops);
  20265. + clear_maxlatprocdata(mp);
  20266. + }
  20267. + entry = debugfs_create_file("reset", 0644, dentry,
  20268. + (void *)TIMERANDWAKEUP_LATENCY, &latency_hist_reset_fops);
  20269. + entry = debugfs_create_file("timerandwakeup", 0644,
  20270. + enable_root, (void *)&timerandwakeup_enabled_data,
  20271. + &enable_fops);
  20272. +#endif
  20273. + return 0;
  20274. +}
  20275. +
  20276. +device_initcall(latency_hist_init);
  20277. diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
  20278. index 7bc56762ca35..84ffcb813263 100644
  20279. --- a/kernel/trace/trace.c
  20280. +++ b/kernel/trace/trace.c
  20281. @@ -1897,6 +1897,7 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  20282. struct task_struct *tsk = current;
  20283. entry->preempt_count = pc & 0xff;
  20284. + entry->preempt_lazy_count = preempt_lazy_count();
  20285. entry->pid = (tsk) ? tsk->pid : 0;
  20286. entry->flags =
  20287. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  20288. @@ -1907,8 +1908,11 @@ tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  20289. ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) |
  20290. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  20291. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  20292. - (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
  20293. + (tif_need_resched_now() ? TRACE_FLAG_NEED_RESCHED : 0) |
  20294. + (need_resched_lazy() ? TRACE_FLAG_NEED_RESCHED_LAZY : 0) |
  20295. (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
  20296. +
  20297. + entry->migrate_disable = (tsk) ? __migrate_disabled(tsk) & 0xFF : 0;
  20298. }
  20299. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  20300. @@ -2892,14 +2896,17 @@ get_total_entries(struct trace_buffer *buf,
  20301. static void print_lat_help_header(struct seq_file *m)
  20302. {
  20303. - seq_puts(m, "# _------=> CPU# \n"
  20304. - "# / _-----=> irqs-off \n"
  20305. - "# | / _----=> need-resched \n"
  20306. - "# || / _---=> hardirq/softirq \n"
  20307. - "# ||| / _--=> preempt-depth \n"
  20308. - "# |||| / delay \n"
  20309. - "# cmd pid ||||| time | caller \n"
  20310. - "# \\ / ||||| \\ | / \n");
  20311. + seq_puts(m, "# _--------=> CPU# \n"
  20312. + "# / _-------=> irqs-off \n"
  20313. + "# | / _------=> need-resched \n"
  20314. + "# || / _-----=> need-resched_lazy \n"
  20315. + "# ||| / _----=> hardirq/softirq \n"
  20316. + "# |||| / _---=> preempt-depth \n"
  20317. + "# ||||| / _--=> preempt-lazy-depth\n"
  20318. + "# |||||| / _-=> migrate-disable \n"
  20319. + "# ||||||| / delay \n"
  20320. + "# cmd pid |||||||| time | caller \n"
  20321. + "# \\ / |||||||| \\ | / \n");
  20322. }
  20323. static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
  20324. @@ -2925,11 +2932,14 @@ static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file
  20325. print_event_info(buf, m);
  20326. seq_puts(m, "# _-----=> irqs-off\n"
  20327. "# / _----=> need-resched\n"
  20328. - "# | / _---=> hardirq/softirq\n"
  20329. - "# || / _--=> preempt-depth\n"
  20330. - "# ||| / delay\n"
  20331. - "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n"
  20332. - "# | | | |||| | |\n");
  20333. + "# |/ _-----=> need-resched_lazy\n"
  20334. + "# || / _---=> hardirq/softirq\n"
  20335. + "# ||| / _--=> preempt-depth\n"
  20336. + "# |||| / _-=> preempt-lazy-depth\n"
  20337. + "# ||||| / _-=> migrate-disable \n"
  20338. + "# |||||| / delay\n"
  20339. + "# TASK-PID CPU# ||||||| TIMESTAMP FUNCTION\n"
  20340. + "# | | | ||||||| | |\n");
  20341. }
  20342. void
  20343. diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
  20344. index f783df416726..6f2d0fa4fbf1 100644
  20345. --- a/kernel/trace/trace.h
  20346. +++ b/kernel/trace/trace.h
  20347. @@ -123,6 +123,7 @@ struct kretprobe_trace_entry_head {
  20348. * NEED_RESCHED - reschedule is requested
  20349. * HARDIRQ - inside an interrupt handler
  20350. * SOFTIRQ - inside a softirq handler
  20351. + * NEED_RESCHED_LAZY - lazy reschedule is requested
  20352. */
  20353. enum trace_flag_type {
  20354. TRACE_FLAG_IRQS_OFF = 0x01,
  20355. @@ -132,6 +133,7 @@ enum trace_flag_type {
  20356. TRACE_FLAG_SOFTIRQ = 0x10,
  20357. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  20358. TRACE_FLAG_NMI = 0x40,
  20359. + TRACE_FLAG_NEED_RESCHED_LAZY = 0x80,
  20360. };
  20361. #define TRACE_BUF_SIZE 1024
  20362. diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
  20363. index 03c0a48c3ac4..0b85d516b491 100644
  20364. --- a/kernel/trace/trace_events.c
  20365. +++ b/kernel/trace/trace_events.c
  20366. @@ -187,6 +187,8 @@ static int trace_define_common_fields(void)
  20367. __common_field(unsigned char, flags);
  20368. __common_field(unsigned char, preempt_count);
  20369. __common_field(int, pid);
  20370. + __common_field(unsigned short, migrate_disable);
  20371. + __common_field(unsigned short, padding);
  20372. return ret;
  20373. }
  20374. diff --git a/kernel/trace/trace_irqsoff.c b/kernel/trace/trace_irqsoff.c
  20375. index 03cdff84d026..940bd10b4406 100644
  20376. --- a/kernel/trace/trace_irqsoff.c
  20377. +++ b/kernel/trace/trace_irqsoff.c
  20378. @@ -13,6 +13,7 @@
  20379. #include <linux/uaccess.h>
  20380. #include <linux/module.h>
  20381. #include <linux/ftrace.h>
  20382. +#include <trace/events/hist.h>
  20383. #include "trace.h"
  20384. @@ -424,11 +425,13 @@ void start_critical_timings(void)
  20385. {
  20386. if (preempt_trace() || irq_trace())
  20387. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  20388. + trace_preemptirqsoff_hist_rcuidle(TRACE_START, 1);
  20389. }
  20390. EXPORT_SYMBOL_GPL(start_critical_timings);
  20391. void stop_critical_timings(void)
  20392. {
  20393. + trace_preemptirqsoff_hist_rcuidle(TRACE_STOP, 0);
  20394. if (preempt_trace() || irq_trace())
  20395. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  20396. }
  20397. @@ -438,6 +441,7 @@ EXPORT_SYMBOL_GPL(stop_critical_timings);
  20398. #ifdef CONFIG_PROVE_LOCKING
  20399. void time_hardirqs_on(unsigned long a0, unsigned long a1)
  20400. {
  20401. + trace_preemptirqsoff_hist_rcuidle(IRQS_ON, 0);
  20402. if (!preempt_trace() && irq_trace())
  20403. stop_critical_timing(a0, a1);
  20404. }
  20405. @@ -446,6 +450,7 @@ void time_hardirqs_off(unsigned long a0, unsigned long a1)
  20406. {
  20407. if (!preempt_trace() && irq_trace())
  20408. start_critical_timing(a0, a1);
  20409. + trace_preemptirqsoff_hist_rcuidle(IRQS_OFF, 1);
  20410. }
  20411. #else /* !CONFIG_PROVE_LOCKING */
  20412. @@ -471,6 +476,7 @@ inline void print_irqtrace_events(struct task_struct *curr)
  20413. */
  20414. void trace_hardirqs_on(void)
  20415. {
  20416. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  20417. if (!preempt_trace() && irq_trace())
  20418. stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  20419. }
  20420. @@ -480,11 +486,13 @@ void trace_hardirqs_off(void)
  20421. {
  20422. if (!preempt_trace() && irq_trace())
  20423. start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
  20424. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  20425. }
  20426. EXPORT_SYMBOL(trace_hardirqs_off);
  20427. __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
  20428. {
  20429. + trace_preemptirqsoff_hist(IRQS_ON, 0);
  20430. if (!preempt_trace() && irq_trace())
  20431. stop_critical_timing(CALLER_ADDR0, caller_addr);
  20432. }
  20433. @@ -494,6 +502,7 @@ __visible void trace_hardirqs_off_caller(unsigned long caller_addr)
  20434. {
  20435. if (!preempt_trace() && irq_trace())
  20436. start_critical_timing(CALLER_ADDR0, caller_addr);
  20437. + trace_preemptirqsoff_hist(IRQS_OFF, 1);
  20438. }
  20439. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  20440. @@ -503,12 +512,14 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller);
  20441. #ifdef CONFIG_PREEMPT_TRACER
  20442. void trace_preempt_on(unsigned long a0, unsigned long a1)
  20443. {
  20444. + trace_preemptirqsoff_hist(PREEMPT_ON, 0);
  20445. if (preempt_trace() && !irq_trace())
  20446. stop_critical_timing(a0, a1);
  20447. }
  20448. void trace_preempt_off(unsigned long a0, unsigned long a1)
  20449. {
  20450. + trace_preemptirqsoff_hist(PREEMPT_ON, 1);
  20451. if (preempt_trace() && !irq_trace())
  20452. start_critical_timing(a0, a1);
  20453. }
  20454. diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
  20455. index 0bb9cf2d53e6..455a7464772f 100644
  20456. --- a/kernel/trace/trace_output.c
  20457. +++ b/kernel/trace/trace_output.c
  20458. @@ -386,6 +386,7 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  20459. {
  20460. char hardsoft_irq;
  20461. char need_resched;
  20462. + char need_resched_lazy;
  20463. char irqs_off;
  20464. int hardirq;
  20465. int softirq;
  20466. @@ -416,6 +417,9 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  20467. break;
  20468. }
  20469. + need_resched_lazy =
  20470. + (entry->flags & TRACE_FLAG_NEED_RESCHED_LAZY) ? 'L' : '.';
  20471. +
  20472. hardsoft_irq =
  20473. (nmi && hardirq) ? 'Z' :
  20474. nmi ? 'z' :
  20475. @@ -424,14 +428,25 @@ int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  20476. softirq ? 's' :
  20477. '.' ;
  20478. - trace_seq_printf(s, "%c%c%c",
  20479. - irqs_off, need_resched, hardsoft_irq);
  20480. + trace_seq_printf(s, "%c%c%c%c",
  20481. + irqs_off, need_resched, need_resched_lazy,
  20482. + hardsoft_irq);
  20483. if (entry->preempt_count)
  20484. trace_seq_printf(s, "%x", entry->preempt_count);
  20485. else
  20486. trace_seq_putc(s, '.');
  20487. + if (entry->preempt_lazy_count)
  20488. + trace_seq_printf(s, "%x", entry->preempt_lazy_count);
  20489. + else
  20490. + trace_seq_putc(s, '.');
  20491. +
  20492. + if (entry->migrate_disable)
  20493. + trace_seq_printf(s, "%x", entry->migrate_disable);
  20494. + else
  20495. + trace_seq_putc(s, '.');
  20496. +
  20497. return !trace_seq_has_overflowed(s);
  20498. }
  20499. diff --git a/kernel/user.c b/kernel/user.c
  20500. index b069ccbfb0b0..1a2e88e98b5e 100644
  20501. --- a/kernel/user.c
  20502. +++ b/kernel/user.c
  20503. @@ -161,11 +161,11 @@ void free_uid(struct user_struct *up)
  20504. if (!up)
  20505. return;
  20506. - local_irq_save(flags);
  20507. + local_irq_save_nort(flags);
  20508. if (atomic_dec_and_lock(&up->__count, &uidhash_lock))
  20509. free_user(up, flags);
  20510. else
  20511. - local_irq_restore(flags);
  20512. + local_irq_restore_nort(flags);
  20513. }
  20514. struct user_struct *alloc_uid(kuid_t uid)
  20515. diff --git a/kernel/watchdog.c b/kernel/watchdog.c
  20516. index 9acb29f280ec..caba62080411 100644
  20517. --- a/kernel/watchdog.c
  20518. +++ b/kernel/watchdog.c
  20519. @@ -315,6 +315,8 @@ static int is_softlockup(unsigned long touch_ts)
  20520. #ifdef CONFIG_HARDLOCKUP_DETECTOR
  20521. +static DEFINE_RAW_SPINLOCK(watchdog_output_lock);
  20522. +
  20523. static struct perf_event_attr wd_hw_attr = {
  20524. .type = PERF_TYPE_HARDWARE,
  20525. .config = PERF_COUNT_HW_CPU_CYCLES,
  20526. @@ -349,6 +351,13 @@ static void watchdog_overflow_callback(struct perf_event *event,
  20527. /* only print hardlockups once */
  20528. if (__this_cpu_read(hard_watchdog_warn) == true)
  20529. return;
  20530. + /*
  20531. + * If early-printk is enabled then make sure we do not
  20532. + * lock up in printk() and kill console logging:
  20533. + */
  20534. + printk_kill();
  20535. +
  20536. + raw_spin_lock(&watchdog_output_lock);
  20537. pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
  20538. print_modules();
  20539. @@ -366,6 +375,7 @@ static void watchdog_overflow_callback(struct perf_event *event,
  20540. !test_and_set_bit(0, &hardlockup_allcpu_dumped))
  20541. trigger_allbutself_cpu_backtrace();
  20542. + raw_spin_unlock(&watchdog_output_lock);
  20543. if (hardlockup_panic)
  20544. nmi_panic(regs, "Hard LOCKUP");
  20545. @@ -513,6 +523,7 @@ static void watchdog_enable(unsigned int cpu)
  20546. /* kick off the timer for the hardlockup detector */
  20547. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  20548. hrtimer->function = watchdog_timer_fn;
  20549. + hrtimer->irqsafe = 1;
  20550. /* Enable the perf event */
  20551. watchdog_nmi_enable(cpu);
  20552. diff --git a/kernel/workqueue.c b/kernel/workqueue.c
  20553. index ef071ca73fc3..c7a62d6adb00 100644
  20554. --- a/kernel/workqueue.c
  20555. +++ b/kernel/workqueue.c
  20556. @@ -48,6 +48,8 @@
  20557. #include <linux/nodemask.h>
  20558. #include <linux/moduleparam.h>
  20559. #include <linux/uaccess.h>
  20560. +#include <linux/locallock.h>
  20561. +#include <linux/delay.h>
  20562. #include "workqueue_internal.h"
  20563. @@ -121,11 +123,16 @@ enum {
  20564. * cpu or grabbing pool->lock is enough for read access. If
  20565. * POOL_DISASSOCIATED is set, it's identical to L.
  20566. *
  20567. + * On RT we need the extra protection via rt_lock_idle_list() for
  20568. + * the list manipulations against read access from
  20569. + * wq_worker_sleeping(). All other places are nicely serialized via
  20570. + * pool->lock.
  20571. + *
  20572. * A: pool->attach_mutex protected.
  20573. *
  20574. * PL: wq_pool_mutex protected.
  20575. *
  20576. - * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
  20577. + * PR: wq_pool_mutex protected for writes. RCU protected for reads.
  20578. *
  20579. * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads.
  20580. *
  20581. @@ -134,7 +141,7 @@ enum {
  20582. *
  20583. * WQ: wq->mutex protected.
  20584. *
  20585. - * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
  20586. + * WR: wq->mutex protected for writes. RCU protected for reads.
  20587. *
  20588. * MD: wq_mayday_lock protected.
  20589. */
  20590. @@ -185,7 +192,7 @@ struct worker_pool {
  20591. atomic_t nr_running ____cacheline_aligned_in_smp;
  20592. /*
  20593. - * Destruction of pool is sched-RCU protected to allow dereferences
  20594. + * Destruction of pool is RCU protected to allow dereferences
  20595. * from get_work_pool().
  20596. */
  20597. struct rcu_head rcu;
  20598. @@ -214,7 +221,7 @@ struct pool_workqueue {
  20599. /*
  20600. * Release of unbound pwq is punted to system_wq. See put_pwq()
  20601. * and pwq_unbound_release_workfn() for details. pool_workqueue
  20602. - * itself is also sched-RCU protected so that the first pwq can be
  20603. + * itself is also RCU protected so that the first pwq can be
  20604. * determined without grabbing wq->mutex.
  20605. */
  20606. struct work_struct unbound_release_work;
  20607. @@ -348,6 +355,8 @@ EXPORT_SYMBOL_GPL(system_power_efficient_wq);
  20608. struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly;
  20609. EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);
  20610. +static DEFINE_LOCAL_IRQ_LOCK(pendingb_lock);
  20611. +
  20612. static int worker_thread(void *__worker);
  20613. static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  20614. @@ -355,20 +364,20 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  20615. #include <trace/events/workqueue.h>
  20616. #define assert_rcu_or_pool_mutex() \
  20617. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  20618. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  20619. !lockdep_is_held(&wq_pool_mutex), \
  20620. - "sched RCU or wq_pool_mutex should be held")
  20621. + "RCU or wq_pool_mutex should be held")
  20622. #define assert_rcu_or_wq_mutex(wq) \
  20623. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  20624. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  20625. !lockdep_is_held(&wq->mutex), \
  20626. - "sched RCU or wq->mutex should be held")
  20627. + "RCU or wq->mutex should be held")
  20628. #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \
  20629. - RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \
  20630. + RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \
  20631. !lockdep_is_held(&wq->mutex) && \
  20632. !lockdep_is_held(&wq_pool_mutex), \
  20633. - "sched RCU, wq->mutex or wq_pool_mutex should be held")
  20634. + "RCU, wq->mutex or wq_pool_mutex should be held")
  20635. #define for_each_cpu_worker_pool(pool, cpu) \
  20636. for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
  20637. @@ -380,7 +389,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  20638. * @pool: iteration cursor
  20639. * @pi: integer used for iteration
  20640. *
  20641. - * This must be called either with wq_pool_mutex held or sched RCU read
  20642. + * This must be called either with wq_pool_mutex held or RCU read
  20643. * locked. If the pool needs to be used beyond the locking in effect, the
  20644. * caller is responsible for guaranteeing that the pool stays online.
  20645. *
  20646. @@ -412,7 +421,7 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  20647. * @pwq: iteration cursor
  20648. * @wq: the target workqueue
  20649. *
  20650. - * This must be called either with wq->mutex held or sched RCU read locked.
  20651. + * This must be called either with wq->mutex held or RCU read locked.
  20652. * If the pwq needs to be used beyond the locking in effect, the caller is
  20653. * responsible for guaranteeing that the pwq stays online.
  20654. *
  20655. @@ -424,6 +433,31 @@ static void workqueue_sysfs_unregister(struct workqueue_struct *wq);
  20656. if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
  20657. else
  20658. +#ifdef CONFIG_PREEMPT_RT_BASE
  20659. +static inline void rt_lock_idle_list(struct worker_pool *pool)
  20660. +{
  20661. + preempt_disable();
  20662. +}
  20663. +static inline void rt_unlock_idle_list(struct worker_pool *pool)
  20664. +{
  20665. + preempt_enable();
  20666. +}
  20667. +static inline void sched_lock_idle_list(struct worker_pool *pool) { }
  20668. +static inline void sched_unlock_idle_list(struct worker_pool *pool) { }
  20669. +#else
  20670. +static inline void rt_lock_idle_list(struct worker_pool *pool) { }
  20671. +static inline void rt_unlock_idle_list(struct worker_pool *pool) { }
  20672. +static inline void sched_lock_idle_list(struct worker_pool *pool)
  20673. +{
  20674. + spin_lock_irq(&pool->lock);
  20675. +}
  20676. +static inline void sched_unlock_idle_list(struct worker_pool *pool)
  20677. +{
  20678. + spin_unlock_irq(&pool->lock);
  20679. +}
  20680. +#endif
  20681. +
  20682. +
  20683. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  20684. static struct debug_obj_descr work_debug_descr;
  20685. @@ -548,7 +582,7 @@ static int worker_pool_assign_id(struct worker_pool *pool)
  20686. * @wq: the target workqueue
  20687. * @node: the node ID
  20688. *
  20689. - * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU
  20690. + * This must be called with any of wq_pool_mutex, wq->mutex or RCU
  20691. * read locked.
  20692. * If the pwq needs to be used beyond the locking in effect, the caller is
  20693. * responsible for guaranteeing that the pwq stays online.
  20694. @@ -692,8 +726,8 @@ static struct pool_workqueue *get_work_pwq(struct work_struct *work)
  20695. * @work: the work item of interest
  20696. *
  20697. * Pools are created and destroyed under wq_pool_mutex, and allows read
  20698. - * access under sched-RCU read lock. As such, this function should be
  20699. - * called under wq_pool_mutex or with preemption disabled.
  20700. + * access under RCU read lock. As such, this function should be
  20701. + * called under wq_pool_mutex or inside of a rcu_read_lock() region.
  20702. *
  20703. * All fields of the returned pool are accessible as long as the above
  20704. * mentioned locking is in effect. If the returned pool needs to be used
  20705. @@ -830,50 +864,45 @@ static struct worker *first_idle_worker(struct worker_pool *pool)
  20706. */
  20707. static void wake_up_worker(struct worker_pool *pool)
  20708. {
  20709. - struct worker *worker = first_idle_worker(pool);
  20710. + struct worker *worker;
  20711. +
  20712. + rt_lock_idle_list(pool);
  20713. +
  20714. + worker = first_idle_worker(pool);
  20715. if (likely(worker))
  20716. wake_up_process(worker->task);
  20717. +
  20718. + rt_unlock_idle_list(pool);
  20719. }
  20720. /**
  20721. - * wq_worker_waking_up - a worker is waking up
  20722. + * wq_worker_running - a worker is running again
  20723. * @task: task waking up
  20724. - * @cpu: CPU @task is waking up to
  20725. *
  20726. - * This function is called during try_to_wake_up() when a worker is
  20727. - * being awoken.
  20728. - *
  20729. - * CONTEXT:
  20730. - * spin_lock_irq(rq->lock)
  20731. + * This function is called when a worker returns from schedule()
  20732. */
  20733. -void wq_worker_waking_up(struct task_struct *task, int cpu)
  20734. +void wq_worker_running(struct task_struct *task)
  20735. {
  20736. struct worker *worker = kthread_data(task);
  20737. - if (!(worker->flags & WORKER_NOT_RUNNING)) {
  20738. - WARN_ON_ONCE(worker->pool->cpu != cpu);
  20739. + if (!worker->sleeping)
  20740. + return;
  20741. + if (!(worker->flags & WORKER_NOT_RUNNING))
  20742. atomic_inc(&worker->pool->nr_running);
  20743. - }
  20744. + worker->sleeping = 0;
  20745. }
  20746. /**
  20747. * wq_worker_sleeping - a worker is going to sleep
  20748. * @task: task going to sleep
  20749. *
  20750. - * This function is called during schedule() when a busy worker is
  20751. - * going to sleep. Worker on the same cpu can be woken up by
  20752. - * returning pointer to its task.
  20753. - *
  20754. - * CONTEXT:
  20755. - * spin_lock_irq(rq->lock)
  20756. - *
  20757. - * Return:
  20758. - * Worker task on @cpu to wake up, %NULL if none.
  20759. + * This function is called from schedule() when a busy worker is
  20760. + * going to sleep.
  20761. */
  20762. -struct task_struct *wq_worker_sleeping(struct task_struct *task)
  20763. +void wq_worker_sleeping(struct task_struct *task)
  20764. {
  20765. - struct worker *worker = kthread_data(task), *to_wakeup = NULL;
  20766. + struct worker *worker = kthread_data(task);
  20767. struct worker_pool *pool;
  20768. /*
  20769. @@ -882,29 +911,26 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task)
  20770. * checking NOT_RUNNING.
  20771. */
  20772. if (worker->flags & WORKER_NOT_RUNNING)
  20773. - return NULL;
  20774. + return;
  20775. pool = worker->pool;
  20776. - /* this can only happen on the local cpu */
  20777. - if (WARN_ON_ONCE(pool->cpu != raw_smp_processor_id()))
  20778. - return NULL;
  20779. + if (WARN_ON_ONCE(worker->sleeping))
  20780. + return;
  20781. +
  20782. + worker->sleeping = 1;
  20783. /*
  20784. * The counterpart of the following dec_and_test, implied mb,
  20785. * worklist not empty test sequence is in insert_work().
  20786. * Please read comment there.
  20787. - *
  20788. - * NOT_RUNNING is clear. This means that we're bound to and
  20789. - * running on the local cpu w/ rq lock held and preemption
  20790. - * disabled, which in turn means that none else could be
  20791. - * manipulating idle_list, so dereferencing idle_list without pool
  20792. - * lock is safe.
  20793. */
  20794. if (atomic_dec_and_test(&pool->nr_running) &&
  20795. - !list_empty(&pool->worklist))
  20796. - to_wakeup = first_idle_worker(pool);
  20797. - return to_wakeup ? to_wakeup->task : NULL;
  20798. + !list_empty(&pool->worklist)) {
  20799. + sched_lock_idle_list(pool);
  20800. + wake_up_worker(pool);
  20801. + sched_unlock_idle_list(pool);
  20802. + }
  20803. }
  20804. /**
  20805. @@ -1098,12 +1124,12 @@ static void put_pwq_unlocked(struct pool_workqueue *pwq)
  20806. {
  20807. if (pwq) {
  20808. /*
  20809. - * As both pwqs and pools are sched-RCU protected, the
  20810. + * As both pwqs and pools are RCU protected, the
  20811. * following lock operations are safe.
  20812. */
  20813. - spin_lock_irq(&pwq->pool->lock);
  20814. + local_spin_lock_irq(pendingb_lock, &pwq->pool->lock);
  20815. put_pwq(pwq);
  20816. - spin_unlock_irq(&pwq->pool->lock);
  20817. + local_spin_unlock_irq(pendingb_lock, &pwq->pool->lock);
  20818. }
  20819. }
  20820. @@ -1207,7 +1233,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  20821. struct worker_pool *pool;
  20822. struct pool_workqueue *pwq;
  20823. - local_irq_save(*flags);
  20824. + local_lock_irqsave(pendingb_lock, *flags);
  20825. /* try to steal the timer if it exists */
  20826. if (is_dwork) {
  20827. @@ -1226,6 +1252,7 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  20828. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
  20829. return 0;
  20830. + rcu_read_lock();
  20831. /*
  20832. * The queueing is in progress, or it is already queued. Try to
  20833. * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
  20834. @@ -1264,14 +1291,16 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
  20835. set_work_pool_and_keep_pending(work, pool->id);
  20836. spin_unlock(&pool->lock);
  20837. + rcu_read_unlock();
  20838. return 1;
  20839. }
  20840. spin_unlock(&pool->lock);
  20841. fail:
  20842. - local_irq_restore(*flags);
  20843. + rcu_read_unlock();
  20844. + local_unlock_irqrestore(pendingb_lock, *flags);
  20845. if (work_is_canceling(work))
  20846. return -ENOENT;
  20847. - cpu_relax();
  20848. + cpu_chill();
  20849. return -EAGAIN;
  20850. }
  20851. @@ -1373,7 +1402,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  20852. * queued or lose PENDING. Grabbing PENDING and queueing should
  20853. * happen with IRQ disabled.
  20854. */
  20855. - WARN_ON_ONCE(!irqs_disabled());
  20856. + WARN_ON_ONCE_NONRT(!irqs_disabled());
  20857. debug_work_activate(work);
  20858. @@ -1381,6 +1410,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  20859. if (unlikely(wq->flags & __WQ_DRAINING) &&
  20860. WARN_ON_ONCE(!is_chained_work(wq)))
  20861. return;
  20862. + rcu_read_lock();
  20863. retry:
  20864. if (req_cpu == WORK_CPU_UNBOUND)
  20865. cpu = wq_select_unbound_cpu(raw_smp_processor_id());
  20866. @@ -1437,10 +1467,8 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  20867. /* pwq determined, queue */
  20868. trace_workqueue_queue_work(req_cpu, pwq, work);
  20869. - if (WARN_ON(!list_empty(&work->entry))) {
  20870. - spin_unlock(&pwq->pool->lock);
  20871. - return;
  20872. - }
  20873. + if (WARN_ON(!list_empty(&work->entry)))
  20874. + goto out;
  20875. pwq->nr_in_flight[pwq->work_color]++;
  20876. work_flags = work_color_to_flags(pwq->work_color);
  20877. @@ -1458,7 +1486,9 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
  20878. insert_work(pwq, work, worklist, work_flags);
  20879. +out:
  20880. spin_unlock(&pwq->pool->lock);
  20881. + rcu_read_unlock();
  20882. }
  20883. /**
  20884. @@ -1478,14 +1508,14 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
  20885. bool ret = false;
  20886. unsigned long flags;
  20887. - local_irq_save(flags);
  20888. + local_lock_irqsave(pendingb_lock,flags);
  20889. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  20890. __queue_work(cpu, wq, work);
  20891. ret = true;
  20892. }
  20893. - local_irq_restore(flags);
  20894. + local_unlock_irqrestore(pendingb_lock, flags);
  20895. return ret;
  20896. }
  20897. EXPORT_SYMBOL(queue_work_on);
  20898. @@ -1552,14 +1582,14 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  20899. unsigned long flags;
  20900. /* read the comment in __queue_work() */
  20901. - local_irq_save(flags);
  20902. + local_lock_irqsave(pendingb_lock, flags);
  20903. if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
  20904. __queue_delayed_work(cpu, wq, dwork, delay);
  20905. ret = true;
  20906. }
  20907. - local_irq_restore(flags);
  20908. + local_unlock_irqrestore(pendingb_lock, flags);
  20909. return ret;
  20910. }
  20911. EXPORT_SYMBOL(queue_delayed_work_on);
  20912. @@ -1594,7 +1624,7 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
  20913. if (likely(ret >= 0)) {
  20914. __queue_delayed_work(cpu, wq, dwork, delay);
  20915. - local_irq_restore(flags);
  20916. + local_unlock_irqrestore(pendingb_lock, flags);
  20917. }
  20918. /* -ENOENT from try_to_grab_pending() becomes %true */
  20919. @@ -1627,7 +1657,9 @@ static void worker_enter_idle(struct worker *worker)
  20920. worker->last_active = jiffies;
  20921. /* idle_list is LIFO */
  20922. + rt_lock_idle_list(pool);
  20923. list_add(&worker->entry, &pool->idle_list);
  20924. + rt_unlock_idle_list(pool);
  20925. if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))
  20926. mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT);
  20927. @@ -1660,7 +1692,9 @@ static void worker_leave_idle(struct worker *worker)
  20928. return;
  20929. worker_clr_flags(worker, WORKER_IDLE);
  20930. pool->nr_idle--;
  20931. + rt_lock_idle_list(pool);
  20932. list_del_init(&worker->entry);
  20933. + rt_unlock_idle_list(pool);
  20934. }
  20935. static struct worker *alloc_worker(int node)
  20936. @@ -1826,7 +1860,9 @@ static void destroy_worker(struct worker *worker)
  20937. pool->nr_workers--;
  20938. pool->nr_idle--;
  20939. + rt_lock_idle_list(pool);
  20940. list_del_init(&worker->entry);
  20941. + rt_unlock_idle_list(pool);
  20942. worker->flags |= WORKER_DIE;
  20943. wake_up_process(worker->task);
  20944. }
  20945. @@ -2785,14 +2821,14 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  20946. might_sleep();
  20947. - local_irq_disable();
  20948. + rcu_read_lock();
  20949. pool = get_work_pool(work);
  20950. if (!pool) {
  20951. - local_irq_enable();
  20952. + rcu_read_unlock();
  20953. return false;
  20954. }
  20955. - spin_lock(&pool->lock);
  20956. + spin_lock_irq(&pool->lock);
  20957. /* see the comment in try_to_grab_pending() with the same code */
  20958. pwq = get_work_pwq(work);
  20959. if (pwq) {
  20960. @@ -2821,10 +2857,11 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
  20961. else
  20962. lock_map_acquire_read(&pwq->wq->lockdep_map);
  20963. lock_map_release(&pwq->wq->lockdep_map);
  20964. -
  20965. + rcu_read_unlock();
  20966. return true;
  20967. already_gone:
  20968. spin_unlock_irq(&pool->lock);
  20969. + rcu_read_unlock();
  20970. return false;
  20971. }
  20972. @@ -2911,7 +2948,7 @@ static bool __cancel_work_timer(struct work_struct *work, bool is_dwork)
  20973. /* tell other tasks trying to grab @work to back off */
  20974. mark_work_canceling(work);
  20975. - local_irq_restore(flags);
  20976. + local_unlock_irqrestore(pendingb_lock, flags);
  20977. flush_work(work);
  20978. clear_work_data(work);
  20979. @@ -2966,10 +3003,10 @@ EXPORT_SYMBOL_GPL(cancel_work_sync);
  20980. */
  20981. bool flush_delayed_work(struct delayed_work *dwork)
  20982. {
  20983. - local_irq_disable();
  20984. + local_lock_irq(pendingb_lock);
  20985. if (del_timer_sync(&dwork->timer))
  20986. __queue_work(dwork->cpu, dwork->wq, &dwork->work);
  20987. - local_irq_enable();
  20988. + local_unlock_irq(pendingb_lock);
  20989. return flush_work(&dwork->work);
  20990. }
  20991. EXPORT_SYMBOL(flush_delayed_work);
  20992. @@ -3004,7 +3041,7 @@ bool cancel_delayed_work(struct delayed_work *dwork)
  20993. set_work_pool_and_clear_pending(&dwork->work,
  20994. get_work_pool_id(&dwork->work));
  20995. - local_irq_restore(flags);
  20996. + local_unlock_irqrestore(pendingb_lock, flags);
  20997. return ret;
  20998. }
  20999. EXPORT_SYMBOL(cancel_delayed_work);
  21000. @@ -3233,7 +3270,7 @@ static void rcu_free_pool(struct rcu_head *rcu)
  21001. * put_unbound_pool - put a worker_pool
  21002. * @pool: worker_pool to put
  21003. *
  21004. - * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
  21005. + * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU
  21006. * safe manner. get_unbound_pool() calls this function on its failure path
  21007. * and this function should be able to release pools which went through,
  21008. * successfully or not, init_worker_pool().
  21009. @@ -3287,8 +3324,8 @@ static void put_unbound_pool(struct worker_pool *pool)
  21010. del_timer_sync(&pool->idle_timer);
  21011. del_timer_sync(&pool->mayday_timer);
  21012. - /* sched-RCU protected to allow dereferences from get_work_pool() */
  21013. - call_rcu_sched(&pool->rcu, rcu_free_pool);
  21014. + /* RCU protected to allow dereferences from get_work_pool() */
  21015. + call_rcu(&pool->rcu, rcu_free_pool);
  21016. }
  21017. /**
  21018. @@ -3395,14 +3432,14 @@ static void pwq_unbound_release_workfn(struct work_struct *work)
  21019. put_unbound_pool(pool);
  21020. mutex_unlock(&wq_pool_mutex);
  21021. - call_rcu_sched(&pwq->rcu, rcu_free_pwq);
  21022. + call_rcu(&pwq->rcu, rcu_free_pwq);
  21023. /*
  21024. * If we're the last pwq going away, @wq is already dead and no one
  21025. * is gonna access it anymore. Schedule RCU free.
  21026. */
  21027. if (is_last)
  21028. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  21029. + call_rcu(&wq->rcu, rcu_free_wq);
  21030. }
  21031. /**
  21032. @@ -4052,7 +4089,7 @@ void destroy_workqueue(struct workqueue_struct *wq)
  21033. * The base ref is never dropped on per-cpu pwqs. Directly
  21034. * schedule RCU free.
  21035. */
  21036. - call_rcu_sched(&wq->rcu, rcu_free_wq);
  21037. + call_rcu(&wq->rcu, rcu_free_wq);
  21038. } else {
  21039. /*
  21040. * We're the sole accessor of @wq at this point. Directly
  21041. @@ -4145,7 +4182,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  21042. struct pool_workqueue *pwq;
  21043. bool ret;
  21044. - rcu_read_lock_sched();
  21045. + rcu_read_lock();
  21046. + preempt_disable();
  21047. if (cpu == WORK_CPU_UNBOUND)
  21048. cpu = smp_processor_id();
  21049. @@ -4156,7 +4194,8 @@ bool workqueue_congested(int cpu, struct workqueue_struct *wq)
  21050. pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
  21051. ret = !list_empty(&pwq->delayed_works);
  21052. - rcu_read_unlock_sched();
  21053. + preempt_enable();
  21054. + rcu_read_unlock();
  21055. return ret;
  21056. }
  21057. @@ -4182,15 +4221,15 @@ unsigned int work_busy(struct work_struct *work)
  21058. if (work_pending(work))
  21059. ret |= WORK_BUSY_PENDING;
  21060. - local_irq_save(flags);
  21061. + rcu_read_lock();
  21062. pool = get_work_pool(work);
  21063. if (pool) {
  21064. - spin_lock(&pool->lock);
  21065. + spin_lock_irqsave(&pool->lock, flags);
  21066. if (find_worker_executing_work(pool, work))
  21067. ret |= WORK_BUSY_RUNNING;
  21068. - spin_unlock(&pool->lock);
  21069. + spin_unlock_irqrestore(&pool->lock, flags);
  21070. }
  21071. - local_irq_restore(flags);
  21072. + rcu_read_unlock();
  21073. return ret;
  21074. }
  21075. @@ -4379,7 +4418,7 @@ void show_workqueue_state(void)
  21076. unsigned long flags;
  21077. int pi;
  21078. - rcu_read_lock_sched();
  21079. + rcu_read_lock();
  21080. pr_info("Showing busy workqueues and worker pools:\n");
  21081. @@ -4432,7 +4471,7 @@ void show_workqueue_state(void)
  21082. spin_unlock_irqrestore(&pool->lock, flags);
  21083. }
  21084. - rcu_read_unlock_sched();
  21085. + rcu_read_unlock();
  21086. }
  21087. /*
  21088. @@ -4770,16 +4809,16 @@ bool freeze_workqueues_busy(void)
  21089. * nr_active is monotonically decreasing. It's safe
  21090. * to peek without lock.
  21091. */
  21092. - rcu_read_lock_sched();
  21093. + rcu_read_lock();
  21094. for_each_pwq(pwq, wq) {
  21095. WARN_ON_ONCE(pwq->nr_active < 0);
  21096. if (pwq->nr_active) {
  21097. busy = true;
  21098. - rcu_read_unlock_sched();
  21099. + rcu_read_unlock();
  21100. goto out_unlock;
  21101. }
  21102. }
  21103. - rcu_read_unlock_sched();
  21104. + rcu_read_unlock();
  21105. }
  21106. out_unlock:
  21107. mutex_unlock(&wq_pool_mutex);
  21108. @@ -4969,7 +5008,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  21109. const char *delim = "";
  21110. int node, written = 0;
  21111. - rcu_read_lock_sched();
  21112. + get_online_cpus();
  21113. + rcu_read_lock();
  21114. for_each_node(node) {
  21115. written += scnprintf(buf + written, PAGE_SIZE - written,
  21116. "%s%d:%d", delim, node,
  21117. @@ -4977,7 +5017,8 @@ static ssize_t wq_pool_ids_show(struct device *dev,
  21118. delim = " ";
  21119. }
  21120. written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
  21121. - rcu_read_unlock_sched();
  21122. + rcu_read_unlock();
  21123. + put_online_cpus();
  21124. return written;
  21125. }
  21126. diff --git a/kernel/workqueue_internal.h b/kernel/workqueue_internal.h
  21127. index 8635417c587b..f000c4d6917e 100644
  21128. --- a/kernel/workqueue_internal.h
  21129. +++ b/kernel/workqueue_internal.h
  21130. @@ -43,6 +43,7 @@ struct worker {
  21131. unsigned long last_active; /* L: last active timestamp */
  21132. unsigned int flags; /* X: flags */
  21133. int id; /* I: worker id */
  21134. + int sleeping; /* None */
  21135. /*
  21136. * Opaque string set with work_set_desc(). Printed out with task
  21137. @@ -68,7 +69,7 @@ static inline struct worker *current_wq_worker(void)
  21138. * Scheduler hooks for concurrency managed workqueue. Only to be used from
  21139. * sched/core.c and workqueue.c.
  21140. */
  21141. -void wq_worker_waking_up(struct task_struct *task, int cpu);
  21142. -struct task_struct *wq_worker_sleeping(struct task_struct *task);
  21143. +void wq_worker_running(struct task_struct *task);
  21144. +void wq_worker_sleeping(struct task_struct *task);
  21145. #endif /* _KERNEL_WORKQUEUE_INTERNAL_H */
  21146. diff --git a/lib/Kconfig b/lib/Kconfig
  21147. index d79909dc01ec..fd2accb2f2bb 100644
  21148. --- a/lib/Kconfig
  21149. +++ b/lib/Kconfig
  21150. @@ -400,6 +400,7 @@ config CHECK_SIGNATURE
  21151. config CPUMASK_OFFSTACK
  21152. bool "Force CPU masks off stack" if DEBUG_PER_CPU_MAPS
  21153. + depends on !PREEMPT_RT_FULL
  21154. help
  21155. Use dynamic allocation for cpumask_var_t, instead of putting
  21156. them on the stack. This is a bit more expensive, but avoids
  21157. diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
  21158. index cab7405f48d2..dbc49c48ff53 100644
  21159. --- a/lib/Kconfig.debug
  21160. +++ b/lib/Kconfig.debug
  21161. @@ -977,6 +977,7 @@ config TIMER_STATS
  21162. config DEBUG_PREEMPT
  21163. bool "Debug preemptible kernel"
  21164. depends on DEBUG_KERNEL && PREEMPT && TRACE_IRQFLAGS_SUPPORT
  21165. + select USING_GET_LOCK_PARENT_IP
  21166. default y
  21167. help
  21168. If you say Y here then the kernel will use a debug variant of the
  21169. @@ -1159,8 +1160,17 @@ config LOCK_TORTURE_TEST
  21170. endmenu # lock debugging
  21171. +config USING_GET_LOCK_PARENT_IP
  21172. + bool
  21173. + help
  21174. + Enables the use of the function get_lock_parent_ip() that
  21175. + will use __builtin_return_address(n) with n > 0 causing
  21176. + some gcc warnings. When this is selected, those warnings
  21177. + will be suppressed.
  21178. +
  21179. config TRACE_IRQFLAGS
  21180. bool
  21181. + select USING_GET_LOCK_PARENT_IP
  21182. help
  21183. Enables hooks to interrupt enabling and disabling for
  21184. either tracing or lock debugging.
  21185. diff --git a/lib/debugobjects.c b/lib/debugobjects.c
  21186. index a8e12601eb37..c76d5f0beafe 100644
  21187. --- a/lib/debugobjects.c
  21188. +++ b/lib/debugobjects.c
  21189. @@ -308,7 +308,10 @@ __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  21190. struct debug_obj *obj;
  21191. unsigned long flags;
  21192. - fill_pool();
  21193. +#ifdef CONFIG_PREEMPT_RT_FULL
  21194. + if (preempt_count() == 0 && !irqs_disabled())
  21195. +#endif
  21196. + fill_pool();
  21197. db = get_bucket((unsigned long) addr);
  21198. diff --git a/lib/idr.c b/lib/idr.c
  21199. index 6098336df267..9decbe914595 100644
  21200. --- a/lib/idr.c
  21201. +++ b/lib/idr.c
  21202. @@ -30,6 +30,7 @@
  21203. #include <linux/idr.h>
  21204. #include <linux/spinlock.h>
  21205. #include <linux/percpu.h>
  21206. +#include <linux/locallock.h>
  21207. #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
  21208. #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
  21209. @@ -45,6 +46,37 @@ static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
  21210. static DEFINE_PER_CPU(int, idr_preload_cnt);
  21211. static DEFINE_SPINLOCK(simple_ida_lock);
  21212. +#ifdef CONFIG_PREEMPT_RT_FULL
  21213. +static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
  21214. +
  21215. +static inline void idr_preload_lock(void)
  21216. +{
  21217. + local_lock(idr_lock);
  21218. +}
  21219. +
  21220. +static inline void idr_preload_unlock(void)
  21221. +{
  21222. + local_unlock(idr_lock);
  21223. +}
  21224. +
  21225. +void idr_preload_end(void)
  21226. +{
  21227. + idr_preload_unlock();
  21228. +}
  21229. +EXPORT_SYMBOL(idr_preload_end);
  21230. +#else
  21231. +static inline void idr_preload_lock(void)
  21232. +{
  21233. + preempt_disable();
  21234. +}
  21235. +
  21236. +static inline void idr_preload_unlock(void)
  21237. +{
  21238. + preempt_enable();
  21239. +}
  21240. +#endif
  21241. +
  21242. +
  21243. /* the maximum ID which can be allocated given idr->layers */
  21244. static int idr_max(int layers)
  21245. {
  21246. @@ -115,14 +147,14 @@ static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
  21247. * context. See idr_preload() for details.
  21248. */
  21249. if (!in_interrupt()) {
  21250. - preempt_disable();
  21251. + idr_preload_lock();
  21252. new = __this_cpu_read(idr_preload_head);
  21253. if (new) {
  21254. __this_cpu_write(idr_preload_head, new->ary[0]);
  21255. __this_cpu_dec(idr_preload_cnt);
  21256. new->ary[0] = NULL;
  21257. }
  21258. - preempt_enable();
  21259. + idr_preload_unlock();
  21260. if (new)
  21261. return new;
  21262. }
  21263. @@ -366,7 +398,6 @@ static void idr_fill_slot(struct idr *idr, void *ptr, int id,
  21264. idr_mark_full(pa, id);
  21265. }
  21266. -
  21267. /**
  21268. * idr_preload - preload for idr_alloc()
  21269. * @gfp_mask: allocation mask to use for preloading
  21270. @@ -401,7 +432,7 @@ void idr_preload(gfp_t gfp_mask)
  21271. WARN_ON_ONCE(in_interrupt());
  21272. might_sleep_if(gfpflags_allow_blocking(gfp_mask));
  21273. - preempt_disable();
  21274. + idr_preload_lock();
  21275. /*
  21276. * idr_alloc() is likely to succeed w/o full idr_layer buffer and
  21277. @@ -413,9 +444,9 @@ void idr_preload(gfp_t gfp_mask)
  21278. while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
  21279. struct idr_layer *new;
  21280. - preempt_enable();
  21281. + idr_preload_unlock();
  21282. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  21283. - preempt_disable();
  21284. + idr_preload_lock();
  21285. if (!new)
  21286. break;
  21287. diff --git a/lib/irq_poll.c b/lib/irq_poll.c
  21288. index 836f7db4e548..709d4eed1df9 100644
  21289. --- a/lib/irq_poll.c
  21290. +++ b/lib/irq_poll.c
  21291. @@ -36,6 +36,7 @@ void irq_poll_sched(struct irq_poll *iop)
  21292. list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
  21293. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21294. local_irq_restore(flags);
  21295. + preempt_check_resched_rt();
  21296. }
  21297. EXPORT_SYMBOL(irq_poll_sched);
  21298. @@ -71,6 +72,7 @@ void irq_poll_complete(struct irq_poll *iop)
  21299. local_irq_save(flags);
  21300. __irq_poll_complete(iop);
  21301. local_irq_restore(flags);
  21302. + preempt_check_resched_rt();
  21303. }
  21304. EXPORT_SYMBOL(irq_poll_complete);
  21305. @@ -95,6 +97,7 @@ static void irq_poll_softirq(struct softirq_action *h)
  21306. }
  21307. local_irq_enable();
  21308. + preempt_check_resched_rt();
  21309. /* Even though interrupts have been re-enabled, this
  21310. * access is safe because interrupts can only add new
  21311. @@ -132,6 +135,7 @@ static void irq_poll_softirq(struct softirq_action *h)
  21312. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21313. local_irq_enable();
  21314. + preempt_check_resched_rt();
  21315. }
  21316. /**
  21317. @@ -199,6 +203,7 @@ static int irq_poll_cpu_notify(struct notifier_block *self,
  21318. this_cpu_ptr(&blk_cpu_iopoll));
  21319. __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
  21320. local_irq_enable();
  21321. + preempt_check_resched_rt();
  21322. }
  21323. return NOTIFY_OK;
  21324. diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c
  21325. index 872a15a2a637..b93a6103fa4d 100644
  21326. --- a/lib/locking-selftest.c
  21327. +++ b/lib/locking-selftest.c
  21328. @@ -590,6 +590,8 @@ GENERATE_TESTCASE(init_held_rsem)
  21329. #include "locking-selftest-spin-hardirq.h"
  21330. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_spin)
  21331. +#ifndef CONFIG_PREEMPT_RT_FULL
  21332. +
  21333. #include "locking-selftest-rlock-hardirq.h"
  21334. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_hard_rlock)
  21335. @@ -605,9 +607,12 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_rlock)
  21336. #include "locking-selftest-wlock-softirq.h"
  21337. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe1_soft_wlock)
  21338. +#endif
  21339. +
  21340. #undef E1
  21341. #undef E2
  21342. +#ifndef CONFIG_PREEMPT_RT_FULL
  21343. /*
  21344. * Enabling hardirqs with a softirq-safe lock held:
  21345. */
  21346. @@ -640,6 +645,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  21347. #undef E1
  21348. #undef E2
  21349. +#endif
  21350. +
  21351. /*
  21352. * Enabling irqs with an irq-safe lock held:
  21353. */
  21354. @@ -663,6 +670,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2A_rlock)
  21355. #include "locking-selftest-spin-hardirq.h"
  21356. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_spin)
  21357. +#ifndef CONFIG_PREEMPT_RT_FULL
  21358. +
  21359. #include "locking-selftest-rlock-hardirq.h"
  21360. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_hard_rlock)
  21361. @@ -678,6 +687,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_rlock)
  21362. #include "locking-selftest-wlock-softirq.h"
  21363. GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  21364. +#endif
  21365. +
  21366. #undef E1
  21367. #undef E2
  21368. @@ -709,6 +720,8 @@ GENERATE_PERMUTATIONS_2_EVENTS(irqsafe2B_soft_wlock)
  21369. #include "locking-selftest-spin-hardirq.h"
  21370. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_spin)
  21371. +#ifndef CONFIG_PREEMPT_RT_FULL
  21372. +
  21373. #include "locking-selftest-rlock-hardirq.h"
  21374. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_hard_rlock)
  21375. @@ -724,6 +737,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_rlock)
  21376. #include "locking-selftest-wlock-softirq.h"
  21377. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  21378. +#endif
  21379. +
  21380. #undef E1
  21381. #undef E2
  21382. #undef E3
  21383. @@ -757,6 +772,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe3_soft_wlock)
  21384. #include "locking-selftest-spin-hardirq.h"
  21385. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_spin)
  21386. +#ifndef CONFIG_PREEMPT_RT_FULL
  21387. +
  21388. #include "locking-selftest-rlock-hardirq.h"
  21389. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_hard_rlock)
  21390. @@ -772,10 +789,14 @@ GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_rlock)
  21391. #include "locking-selftest-wlock-softirq.h"
  21392. GENERATE_PERMUTATIONS_3_EVENTS(irqsafe4_soft_wlock)
  21393. +#endif
  21394. +
  21395. #undef E1
  21396. #undef E2
  21397. #undef E3
  21398. +#ifndef CONFIG_PREEMPT_RT_FULL
  21399. +
  21400. /*
  21401. * read-lock / write-lock irq inversion.
  21402. *
  21403. @@ -838,6 +859,10 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_inversion_soft_wlock)
  21404. #undef E2
  21405. #undef E3
  21406. +#endif
  21407. +
  21408. +#ifndef CONFIG_PREEMPT_RT_FULL
  21409. +
  21410. /*
  21411. * read-lock / write-lock recursion that is actually safe.
  21412. */
  21413. @@ -876,6 +901,8 @@ GENERATE_PERMUTATIONS_3_EVENTS(irq_read_recursion_soft)
  21414. #undef E2
  21415. #undef E3
  21416. +#endif
  21417. +
  21418. /*
  21419. * read-lock / write-lock recursion that is unsafe.
  21420. */
  21421. @@ -1858,6 +1885,7 @@ void locking_selftest(void)
  21422. printk(" --------------------------------------------------------------------------\n");
  21423. +#ifndef CONFIG_PREEMPT_RT_FULL
  21424. /*
  21425. * irq-context testcases:
  21426. */
  21427. @@ -1870,6 +1898,28 @@ void locking_selftest(void)
  21428. DO_TESTCASE_6x2("irq read-recursion", irq_read_recursion);
  21429. // DO_TESTCASE_6x2B("irq read-recursion #2", irq_read_recursion2);
  21430. +#else
  21431. + /* On -rt, we only do hardirq context test for raw spinlock */
  21432. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 12);
  21433. + DO_TESTCASE_1B("hard-irqs-on + irq-safe-A", irqsafe1_hard_spin, 21);
  21434. +
  21435. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 12);
  21436. + DO_TESTCASE_1B("hard-safe-A + irqs-on", irqsafe2B_hard_spin, 21);
  21437. +
  21438. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 123);
  21439. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 132);
  21440. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 213);
  21441. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 231);
  21442. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 312);
  21443. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #1", irqsafe3_hard_spin, 321);
  21444. +
  21445. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 123);
  21446. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 132);
  21447. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 213);
  21448. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 231);
  21449. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 312);
  21450. + DO_TESTCASE_1B("hard-safe-A + unsafe-B #2", irqsafe4_hard_spin, 321);
  21451. +#endif
  21452. ww_tests();
  21453. diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
  21454. index 6d40944960de..822a2c027e72 100644
  21455. --- a/lib/percpu_ida.c
  21456. +++ b/lib/percpu_ida.c
  21457. @@ -26,6 +26,9 @@
  21458. #include <linux/string.h>
  21459. #include <linux/spinlock.h>
  21460. #include <linux/percpu_ida.h>
  21461. +#include <linux/locallock.h>
  21462. +
  21463. +static DEFINE_LOCAL_IRQ_LOCK(irq_off_lock);
  21464. struct percpu_ida_cpu {
  21465. /*
  21466. @@ -148,13 +151,13 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  21467. unsigned long flags;
  21468. int tag;
  21469. - local_irq_save(flags);
  21470. + local_lock_irqsave(irq_off_lock, flags);
  21471. tags = this_cpu_ptr(pool->tag_cpu);
  21472. /* Fastpath */
  21473. tag = alloc_local_tag(tags);
  21474. if (likely(tag >= 0)) {
  21475. - local_irq_restore(flags);
  21476. + local_unlock_irqrestore(irq_off_lock, flags);
  21477. return tag;
  21478. }
  21479. @@ -173,6 +176,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  21480. if (!tags->nr_free)
  21481. alloc_global_tags(pool, tags);
  21482. +
  21483. if (!tags->nr_free)
  21484. steal_tags(pool, tags);
  21485. @@ -184,7 +188,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  21486. }
  21487. spin_unlock(&pool->lock);
  21488. - local_irq_restore(flags);
  21489. + local_unlock_irqrestore(irq_off_lock, flags);
  21490. if (tag >= 0 || state == TASK_RUNNING)
  21491. break;
  21492. @@ -196,7 +200,7 @@ int percpu_ida_alloc(struct percpu_ida *pool, int state)
  21493. schedule();
  21494. - local_irq_save(flags);
  21495. + local_lock_irqsave(irq_off_lock, flags);
  21496. tags = this_cpu_ptr(pool->tag_cpu);
  21497. }
  21498. if (state != TASK_RUNNING)
  21499. @@ -221,7 +225,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  21500. BUG_ON(tag >= pool->nr_tags);
  21501. - local_irq_save(flags);
  21502. + local_lock_irqsave(irq_off_lock, flags);
  21503. tags = this_cpu_ptr(pool->tag_cpu);
  21504. spin_lock(&tags->lock);
  21505. @@ -253,7 +257,7 @@ void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
  21506. spin_unlock(&pool->lock);
  21507. }
  21508. - local_irq_restore(flags);
  21509. + local_unlock_irqrestore(irq_off_lock, flags);
  21510. }
  21511. EXPORT_SYMBOL_GPL(percpu_ida_free);
  21512. @@ -345,7 +349,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  21513. struct percpu_ida_cpu *remote;
  21514. unsigned cpu, i, err = 0;
  21515. - local_irq_save(flags);
  21516. + local_lock_irqsave(irq_off_lock, flags);
  21517. for_each_possible_cpu(cpu) {
  21518. remote = per_cpu_ptr(pool->tag_cpu, cpu);
  21519. spin_lock(&remote->lock);
  21520. @@ -367,7 +371,7 @@ int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
  21521. }
  21522. spin_unlock(&pool->lock);
  21523. out:
  21524. - local_irq_restore(flags);
  21525. + local_unlock_irqrestore(irq_off_lock, flags);
  21526. return err;
  21527. }
  21528. EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
  21529. diff --git a/lib/radix-tree.c b/lib/radix-tree.c
  21530. index 8e6d552c40dd..881cc195d85f 100644
  21531. --- a/lib/radix-tree.c
  21532. +++ b/lib/radix-tree.c
  21533. @@ -290,13 +290,14 @@ radix_tree_node_alloc(struct radix_tree_root *root)
  21534. * succeed in getting a node here (and never reach
  21535. * kmem_cache_alloc)
  21536. */
  21537. - rtp = this_cpu_ptr(&radix_tree_preloads);
  21538. + rtp = &get_cpu_var(radix_tree_preloads);
  21539. if (rtp->nr) {
  21540. ret = rtp->nodes;
  21541. rtp->nodes = ret->private_data;
  21542. ret->private_data = NULL;
  21543. rtp->nr--;
  21544. }
  21545. + put_cpu_var(radix_tree_preloads);
  21546. /*
  21547. * Update the allocation stack trace as this is more useful
  21548. * for debugging.
  21549. @@ -336,6 +337,7 @@ radix_tree_node_free(struct radix_tree_node *node)
  21550. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  21551. }
  21552. +#ifndef CONFIG_PREEMPT_RT_FULL
  21553. /*
  21554. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  21555. * ensure that the addition of a single element in the tree cannot fail. On
  21556. @@ -455,6 +457,7 @@ int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
  21557. return __radix_tree_preload(gfp_mask, nr_nodes);
  21558. }
  21559. +#endif
  21560. /*
  21561. * The maximum index which can be stored in a radix tree
  21562. diff --git a/lib/scatterlist.c b/lib/scatterlist.c
  21563. index 004fc70fc56a..ccc46992a517 100644
  21564. --- a/lib/scatterlist.c
  21565. +++ b/lib/scatterlist.c
  21566. @@ -620,7 +620,7 @@ void sg_miter_stop(struct sg_mapping_iter *miter)
  21567. flush_kernel_dcache_page(miter->page);
  21568. if (miter->__flags & SG_MITER_ATOMIC) {
  21569. - WARN_ON_ONCE(preemptible());
  21570. + WARN_ON_ONCE(!pagefault_disabled());
  21571. kunmap_atomic(miter->addr);
  21572. } else
  21573. kunmap(miter->page);
  21574. @@ -664,7 +664,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  21575. if (!sg_miter_skip(&miter, skip))
  21576. return false;
  21577. - local_irq_save(flags);
  21578. + local_irq_save_nort(flags);
  21579. while (sg_miter_next(&miter) && offset < buflen) {
  21580. unsigned int len;
  21581. @@ -681,7 +681,7 @@ size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  21582. sg_miter_stop(&miter);
  21583. - local_irq_restore(flags);
  21584. + local_irq_restore_nort(flags);
  21585. return offset;
  21586. }
  21587. EXPORT_SYMBOL(sg_copy_buffer);
  21588. diff --git a/lib/smp_processor_id.c b/lib/smp_processor_id.c
  21589. index 1afec32de6f2..11fa431046a8 100644
  21590. --- a/lib/smp_processor_id.c
  21591. +++ b/lib/smp_processor_id.c
  21592. @@ -39,8 +39,9 @@ notrace static unsigned int check_preemption_disabled(const char *what1,
  21593. if (!printk_ratelimit())
  21594. goto out_enable;
  21595. - printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x] code: %s/%d\n",
  21596. - what1, what2, preempt_count() - 1, current->comm, current->pid);
  21597. + printk(KERN_ERR "BUG: using %s%s() in preemptible [%08x %08x] code: %s/%d\n",
  21598. + what1, what2, preempt_count() - 1, __migrate_disabled(current),
  21599. + current->comm, current->pid);
  21600. print_symbol("caller is %s\n", (long)__builtin_return_address(0));
  21601. dump_stack();
  21602. diff --git a/mm/Kconfig b/mm/Kconfig
  21603. index be0ee11fa0d9..fe2857d67973 100644
  21604. --- a/mm/Kconfig
  21605. +++ b/mm/Kconfig
  21606. @@ -410,7 +410,7 @@ config NOMMU_INITIAL_TRIM_EXCESS
  21607. config TRANSPARENT_HUGEPAGE
  21608. bool "Transparent Hugepage Support"
  21609. - depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE
  21610. + depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT_FULL
  21611. select COMPACTION
  21612. select RADIX_TREE_MULTIORDER
  21613. help
  21614. diff --git a/mm/backing-dev.c b/mm/backing-dev.c
  21615. index 8fde443f36d7..d7a863b0ec20 100644
  21616. --- a/mm/backing-dev.c
  21617. +++ b/mm/backing-dev.c
  21618. @@ -457,9 +457,9 @@ void wb_congested_put(struct bdi_writeback_congested *congested)
  21619. {
  21620. unsigned long flags;
  21621. - local_irq_save(flags);
  21622. + local_irq_save_nort(flags);
  21623. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  21624. - local_irq_restore(flags);
  21625. + local_irq_restore_nort(flags);
  21626. return;
  21627. }
  21628. diff --git a/mm/compaction.c b/mm/compaction.c
  21629. index 9affb2908304..d5eb0e52e96f 100644
  21630. --- a/mm/compaction.c
  21631. +++ b/mm/compaction.c
  21632. @@ -1585,10 +1585,12 @@ static enum compact_result compact_zone(struct zone *zone, struct compact_contro
  21633. block_start_pfn(cc->migrate_pfn, cc->order);
  21634. if (cc->last_migrated_pfn < current_block_start) {
  21635. - cpu = get_cpu();
  21636. + cpu = get_cpu_light();
  21637. + local_lock_irq(swapvec_lock);
  21638. lru_add_drain_cpu(cpu);
  21639. + local_unlock_irq(swapvec_lock);
  21640. drain_local_pages(zone);
  21641. - put_cpu();
  21642. + put_cpu_light();
  21643. /* No more flushing until we migrate again */
  21644. cc->last_migrated_pfn = 0;
  21645. }
  21646. diff --git a/mm/filemap.c b/mm/filemap.c
  21647. index ced9ef6c06b0..19f6f0d77604 100644
  21648. --- a/mm/filemap.c
  21649. +++ b/mm/filemap.c
  21650. @@ -159,9 +159,12 @@ static int page_cache_tree_insert(struct address_space *mapping,
  21651. * node->private_list is protected by
  21652. * mapping->tree_lock.
  21653. */
  21654. - if (!list_empty(&node->private_list))
  21655. - list_lru_del(&workingset_shadow_nodes,
  21656. + if (!list_empty(&node->private_list)) {
  21657. + local_lock(workingset_shadow_lock);
  21658. + list_lru_del(&__workingset_shadow_nodes,
  21659. &node->private_list);
  21660. + local_unlock(workingset_shadow_lock);
  21661. + }
  21662. }
  21663. return 0;
  21664. }
  21665. @@ -217,8 +220,10 @@ static void page_cache_tree_delete(struct address_space *mapping,
  21666. if (!dax_mapping(mapping) && !workingset_node_pages(node) &&
  21667. list_empty(&node->private_list)) {
  21668. node->private_data = mapping;
  21669. - list_lru_add(&workingset_shadow_nodes,
  21670. - &node->private_list);
  21671. + local_lock(workingset_shadow_lock);
  21672. + list_lru_add(&__workingset_shadow_nodes,
  21673. + &node->private_list);
  21674. + local_unlock(workingset_shadow_lock);
  21675. }
  21676. }
  21677. diff --git a/mm/highmem.c b/mm/highmem.c
  21678. index 50b4ca6787f0..77518a3b35a1 100644
  21679. --- a/mm/highmem.c
  21680. +++ b/mm/highmem.c
  21681. @@ -29,10 +29,11 @@
  21682. #include <linux/kgdb.h>
  21683. #include <asm/tlbflush.h>
  21684. -
  21685. +#ifndef CONFIG_PREEMPT_RT_FULL
  21686. #if defined(CONFIG_HIGHMEM) || defined(CONFIG_X86_32)
  21687. DEFINE_PER_CPU(int, __kmap_atomic_idx);
  21688. #endif
  21689. +#endif
  21690. /*
  21691. * Virtual_count is not a pure "count".
  21692. @@ -107,8 +108,9 @@ static inline wait_queue_head_t *get_pkmap_wait_queue_head(unsigned int color)
  21693. unsigned long totalhigh_pages __read_mostly;
  21694. EXPORT_SYMBOL(totalhigh_pages);
  21695. -
  21696. +#ifndef CONFIG_PREEMPT_RT_FULL
  21697. EXPORT_PER_CPU_SYMBOL(__kmap_atomic_idx);
  21698. +#endif
  21699. unsigned int nr_free_highpages (void)
  21700. {
  21701. diff --git a/mm/memcontrol.c b/mm/memcontrol.c
  21702. index 4be518d4e68a..724240ca2f35 100644
  21703. --- a/mm/memcontrol.c
  21704. +++ b/mm/memcontrol.c
  21705. @@ -67,6 +67,7 @@
  21706. #include <net/sock.h>
  21707. #include <net/ip.h>
  21708. #include "slab.h"
  21709. +#include <linux/locallock.h>
  21710. #include <asm/uaccess.h>
  21711. @@ -92,6 +93,8 @@ int do_swap_account __read_mostly;
  21712. #define do_swap_account 0
  21713. #endif
  21714. +static DEFINE_LOCAL_IRQ_LOCK(event_lock);
  21715. +
  21716. /* Whether legacy memory+swap accounting is active */
  21717. static bool do_memsw_account(void)
  21718. {
  21719. @@ -1724,6 +1727,7 @@ struct memcg_stock_pcp {
  21720. #define FLUSHING_CACHED_CHARGE 0
  21721. };
  21722. static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
  21723. +static DEFINE_LOCAL_IRQ_LOCK(memcg_stock_ll);
  21724. static DEFINE_MUTEX(percpu_charge_mutex);
  21725. /**
  21726. @@ -1746,7 +1750,7 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  21727. if (nr_pages > CHARGE_BATCH)
  21728. return ret;
  21729. - local_irq_save(flags);
  21730. + local_lock_irqsave(memcg_stock_ll, flags);
  21731. stock = this_cpu_ptr(&memcg_stock);
  21732. if (memcg == stock->cached && stock->nr_pages >= nr_pages) {
  21733. @@ -1754,7 +1758,7 @@ static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  21734. ret = true;
  21735. }
  21736. - local_irq_restore(flags);
  21737. + local_unlock_irqrestore(memcg_stock_ll, flags);
  21738. return ret;
  21739. }
  21740. @@ -1781,13 +1785,13 @@ static void drain_local_stock(struct work_struct *dummy)
  21741. struct memcg_stock_pcp *stock;
  21742. unsigned long flags;
  21743. - local_irq_save(flags);
  21744. + local_lock_irqsave(memcg_stock_ll, flags);
  21745. stock = this_cpu_ptr(&memcg_stock);
  21746. drain_stock(stock);
  21747. clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
  21748. - local_irq_restore(flags);
  21749. + local_unlock_irqrestore(memcg_stock_ll, flags);
  21750. }
  21751. /*
  21752. @@ -1799,7 +1803,7 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  21753. struct memcg_stock_pcp *stock;
  21754. unsigned long flags;
  21755. - local_irq_save(flags);
  21756. + local_lock_irqsave(memcg_stock_ll, flags);
  21757. stock = this_cpu_ptr(&memcg_stock);
  21758. if (stock->cached != memcg) { /* reset if necessary */
  21759. @@ -1808,7 +1812,7 @@ static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
  21760. }
  21761. stock->nr_pages += nr_pages;
  21762. - local_irq_restore(flags);
  21763. + local_unlock_irqrestore(memcg_stock_ll, flags);
  21764. }
  21765. /*
  21766. @@ -1824,7 +1828,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  21767. return;
  21768. /* Notify other cpus that system-wide "drain" is running */
  21769. get_online_cpus();
  21770. - curcpu = get_cpu();
  21771. + curcpu = get_cpu_light();
  21772. for_each_online_cpu(cpu) {
  21773. struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
  21774. struct mem_cgroup *memcg;
  21775. @@ -1841,7 +1845,7 @@ static void drain_all_stock(struct mem_cgroup *root_memcg)
  21776. schedule_work_on(cpu, &stock->work);
  21777. }
  21778. }
  21779. - put_cpu();
  21780. + put_cpu_light();
  21781. put_online_cpus();
  21782. mutex_unlock(&percpu_charge_mutex);
  21783. }
  21784. @@ -4566,12 +4570,12 @@ static int mem_cgroup_move_account(struct page *page,
  21785. ret = 0;
  21786. - local_irq_disable();
  21787. + local_lock_irq(event_lock);
  21788. mem_cgroup_charge_statistics(to, page, compound, nr_pages);
  21789. memcg_check_events(to, page);
  21790. mem_cgroup_charge_statistics(from, page, compound, -nr_pages);
  21791. memcg_check_events(from, page);
  21792. - local_irq_enable();
  21793. + local_unlock_irq(event_lock);
  21794. out_unlock:
  21795. unlock_page(page);
  21796. out:
  21797. @@ -5444,10 +5448,10 @@ void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
  21798. commit_charge(page, memcg, lrucare);
  21799. - local_irq_disable();
  21800. + local_lock_irq(event_lock);
  21801. mem_cgroup_charge_statistics(memcg, page, compound, nr_pages);
  21802. memcg_check_events(memcg, page);
  21803. - local_irq_enable();
  21804. + local_unlock_irq(event_lock);
  21805. if (do_memsw_account() && PageSwapCache(page)) {
  21806. swp_entry_t entry = { .val = page_private(page) };
  21807. @@ -5503,14 +5507,14 @@ static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
  21808. memcg_oom_recover(memcg);
  21809. }
  21810. - local_irq_save(flags);
  21811. + local_lock_irqsave(event_lock, flags);
  21812. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS], nr_anon);
  21813. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_CACHE], nr_file);
  21814. __this_cpu_sub(memcg->stat->count[MEM_CGROUP_STAT_RSS_HUGE], nr_huge);
  21815. __this_cpu_add(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT], pgpgout);
  21816. __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
  21817. memcg_check_events(memcg, dummy_page);
  21818. - local_irq_restore(flags);
  21819. + local_unlock_irqrestore(event_lock, flags);
  21820. if (!mem_cgroup_is_root(memcg))
  21821. css_put_many(&memcg->css, nr_pages);
  21822. @@ -5665,10 +5669,10 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
  21823. commit_charge(newpage, memcg, false);
  21824. - local_irq_save(flags);
  21825. + local_lock_irqsave(event_lock, flags);
  21826. mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages);
  21827. memcg_check_events(memcg, newpage);
  21828. - local_irq_restore(flags);
  21829. + local_unlock_irqrestore(event_lock, flags);
  21830. }
  21831. DEFINE_STATIC_KEY_FALSE(memcg_sockets_enabled_key);
  21832. @@ -5845,6 +5849,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  21833. {
  21834. struct mem_cgroup *memcg, *swap_memcg;
  21835. unsigned short oldid;
  21836. + unsigned long flags;
  21837. VM_BUG_ON_PAGE(PageLRU(page), page);
  21838. VM_BUG_ON_PAGE(page_count(page), page);
  21839. @@ -5885,12 +5890,16 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
  21840. * important here to have the interrupts disabled because it is the
  21841. * only synchronisation we have for udpating the per-CPU variables.
  21842. */
  21843. + local_lock_irqsave(event_lock, flags);
  21844. +#ifndef CONFIG_PREEMPT_RT_BASE
  21845. VM_BUG_ON(!irqs_disabled());
  21846. +#endif
  21847. mem_cgroup_charge_statistics(memcg, page, false, -1);
  21848. memcg_check_events(memcg, page);
  21849. if (!mem_cgroup_is_root(memcg))
  21850. css_put(&memcg->css);
  21851. + local_unlock_irqrestore(event_lock, flags);
  21852. }
  21853. /*
  21854. diff --git a/mm/mmu_context.c b/mm/mmu_context.c
  21855. index 6f4d27c5bb32..5cd25c745a8f 100644
  21856. --- a/mm/mmu_context.c
  21857. +++ b/mm/mmu_context.c
  21858. @@ -23,6 +23,7 @@ void use_mm(struct mm_struct *mm)
  21859. struct task_struct *tsk = current;
  21860. task_lock(tsk);
  21861. + preempt_disable_rt();
  21862. active_mm = tsk->active_mm;
  21863. if (active_mm != mm) {
  21864. atomic_inc(&mm->mm_count);
  21865. @@ -30,6 +31,7 @@ void use_mm(struct mm_struct *mm)
  21866. }
  21867. tsk->mm = mm;
  21868. switch_mm(active_mm, mm, tsk);
  21869. + preempt_enable_rt();
  21870. task_unlock(tsk);
  21871. #ifdef finish_arch_post_lock_switch
  21872. finish_arch_post_lock_switch();
  21873. diff --git a/mm/page_alloc.c b/mm/page_alloc.c
  21874. index a2214c64ed3c..4be4d5d66f73 100644
  21875. --- a/mm/page_alloc.c
  21876. +++ b/mm/page_alloc.c
  21877. @@ -61,6 +61,7 @@
  21878. #include <linux/page_ext.h>
  21879. #include <linux/hugetlb.h>
  21880. #include <linux/sched/rt.h>
  21881. +#include <linux/locallock.h>
  21882. #include <linux/page_owner.h>
  21883. #include <linux/kthread.h>
  21884. #include <linux/memcontrol.h>
  21885. @@ -276,6 +277,18 @@ EXPORT_SYMBOL(nr_node_ids);
  21886. EXPORT_SYMBOL(nr_online_nodes);
  21887. #endif
  21888. +static DEFINE_LOCAL_IRQ_LOCK(pa_lock);
  21889. +
  21890. +#ifdef CONFIG_PREEMPT_RT_BASE
  21891. +# define cpu_lock_irqsave(cpu, flags) \
  21892. + local_lock_irqsave_on(pa_lock, flags, cpu)
  21893. +# define cpu_unlock_irqrestore(cpu, flags) \
  21894. + local_unlock_irqrestore_on(pa_lock, flags, cpu)
  21895. +#else
  21896. +# define cpu_lock_irqsave(cpu, flags) local_irq_save(flags)
  21897. +# define cpu_unlock_irqrestore(cpu, flags) local_irq_restore(flags)
  21898. +#endif
  21899. +
  21900. int page_group_by_mobility_disabled __read_mostly;
  21901. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  21902. @@ -1056,7 +1069,7 @@ static bool bulkfree_pcp_prepare(struct page *page)
  21903. #endif /* CONFIG_DEBUG_VM */
  21904. /*
  21905. - * Frees a number of pages from the PCP lists
  21906. + * Frees a number of pages which have been collected from the pcp lists.
  21907. * Assumes all pages on list are in same zone, and of same order.
  21908. * count is the number of pages to free.
  21909. *
  21910. @@ -1067,19 +1080,58 @@ static bool bulkfree_pcp_prepare(struct page *page)
  21911. * pinned" detection logic.
  21912. */
  21913. static void free_pcppages_bulk(struct zone *zone, int count,
  21914. - struct per_cpu_pages *pcp)
  21915. + struct list_head *list)
  21916. {
  21917. - int migratetype = 0;
  21918. - int batch_free = 0;
  21919. unsigned long nr_scanned;
  21920. bool isolated_pageblocks;
  21921. + unsigned long flags;
  21922. +
  21923. + spin_lock_irqsave(&zone->lock, flags);
  21924. - spin_lock(&zone->lock);
  21925. isolated_pageblocks = has_isolate_pageblock(zone);
  21926. nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
  21927. if (nr_scanned)
  21928. __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
  21929. + while (!list_empty(list)) {
  21930. + struct page *page;
  21931. + int mt; /* migratetype of the to-be-freed page */
  21932. +
  21933. + page = list_first_entry(list, struct page, lru);
  21934. + /* must delete as __free_one_page list manipulates */
  21935. + list_del(&page->lru);
  21936. +
  21937. + mt = get_pcppage_migratetype(page);
  21938. + /* MIGRATE_ISOLATE page should not go to pcplists */
  21939. + VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  21940. + /* Pageblock could have been isolated meanwhile */
  21941. + if (unlikely(isolated_pageblocks))
  21942. + mt = get_pageblock_migratetype(page);
  21943. +
  21944. + if (bulkfree_pcp_prepare(page))
  21945. + continue;
  21946. +
  21947. + __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  21948. + trace_mm_page_pcpu_drain(page, 0, mt);
  21949. + count--;
  21950. + }
  21951. + WARN_ON(count != 0);
  21952. + spin_unlock_irqrestore(&zone->lock, flags);
  21953. +}
  21954. +
  21955. +/*
  21956. + * Moves a number of pages from the PCP lists to free list which
  21957. + * is freed outside of the locked region.
  21958. + *
  21959. + * Assumes all pages on list are in same zone, and of same order.
  21960. + * count is the number of pages to free.
  21961. + */
  21962. +static void isolate_pcp_pages(int count, struct per_cpu_pages *src,
  21963. + struct list_head *dst)
  21964. +{
  21965. + int migratetype = 0;
  21966. + int batch_free = 0;
  21967. +
  21968. while (count) {
  21969. struct page *page;
  21970. struct list_head *list;
  21971. @@ -1095,7 +1147,7 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  21972. batch_free++;
  21973. if (++migratetype == MIGRATE_PCPTYPES)
  21974. migratetype = 0;
  21975. - list = &pcp->lists[migratetype];
  21976. + list = &src->lists[migratetype];
  21977. } while (list_empty(list));
  21978. /* This is the only non-empty list. Free them all. */
  21979. @@ -1103,27 +1155,12 @@ static void free_pcppages_bulk(struct zone *zone, int count,
  21980. batch_free = count;
  21981. do {
  21982. - int mt; /* migratetype of the to-be-freed page */
  21983. -
  21984. page = list_last_entry(list, struct page, lru);
  21985. - /* must delete as __free_one_page list manipulates */
  21986. list_del(&page->lru);
  21987. - mt = get_pcppage_migratetype(page);
  21988. - /* MIGRATE_ISOLATE page should not go to pcplists */
  21989. - VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  21990. - /* Pageblock could have been isolated meanwhile */
  21991. - if (unlikely(isolated_pageblocks))
  21992. - mt = get_pageblock_migratetype(page);
  21993. -
  21994. - if (bulkfree_pcp_prepare(page))
  21995. - continue;
  21996. -
  21997. - __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  21998. - trace_mm_page_pcpu_drain(page, 0, mt);
  21999. + list_add(&page->lru, dst);
  22000. } while (--count && --batch_free && !list_empty(list));
  22001. }
  22002. - spin_unlock(&zone->lock);
  22003. }
  22004. static void free_one_page(struct zone *zone,
  22005. @@ -1132,7 +1169,9 @@ static void free_one_page(struct zone *zone,
  22006. int migratetype)
  22007. {
  22008. unsigned long nr_scanned;
  22009. - spin_lock(&zone->lock);
  22010. + unsigned long flags;
  22011. +
  22012. + spin_lock_irqsave(&zone->lock, flags);
  22013. nr_scanned = node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED);
  22014. if (nr_scanned)
  22015. __mod_node_page_state(zone->zone_pgdat, NR_PAGES_SCANNED, -nr_scanned);
  22016. @@ -1142,7 +1181,7 @@ static void free_one_page(struct zone *zone,
  22017. migratetype = get_pfnblock_migratetype(page, pfn);
  22018. }
  22019. __free_one_page(page, pfn, zone, order, migratetype);
  22020. - spin_unlock(&zone->lock);
  22021. + spin_unlock_irqrestore(&zone->lock, flags);
  22022. }
  22023. static void __meminit __init_single_page(struct page *page, unsigned long pfn,
  22024. @@ -1228,10 +1267,10 @@ static void __free_pages_ok(struct page *page, unsigned int order)
  22025. return;
  22026. migratetype = get_pfnblock_migratetype(page, pfn);
  22027. - local_irq_save(flags);
  22028. + local_lock_irqsave(pa_lock, flags);
  22029. __count_vm_events(PGFREE, 1 << order);
  22030. free_one_page(page_zone(page), page, pfn, order, migratetype);
  22031. - local_irq_restore(flags);
  22032. + local_unlock_irqrestore(pa_lock, flags);
  22033. }
  22034. static void __init __free_pages_boot_core(struct page *page, unsigned int order)
  22035. @@ -2219,16 +2258,18 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
  22036. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  22037. {
  22038. unsigned long flags;
  22039. + LIST_HEAD(dst);
  22040. int to_drain, batch;
  22041. - local_irq_save(flags);
  22042. + local_lock_irqsave(pa_lock, flags);
  22043. batch = READ_ONCE(pcp->batch);
  22044. to_drain = min(pcp->count, batch);
  22045. if (to_drain > 0) {
  22046. - free_pcppages_bulk(zone, to_drain, pcp);
  22047. + isolate_pcp_pages(to_drain, pcp, &dst);
  22048. pcp->count -= to_drain;
  22049. }
  22050. - local_irq_restore(flags);
  22051. + local_unlock_irqrestore(pa_lock, flags);
  22052. + free_pcppages_bulk(zone, to_drain, &dst);
  22053. }
  22054. #endif
  22055. @@ -2244,16 +2285,21 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)
  22056. unsigned long flags;
  22057. struct per_cpu_pageset *pset;
  22058. struct per_cpu_pages *pcp;
  22059. + LIST_HEAD(dst);
  22060. + int count;
  22061. - local_irq_save(flags);
  22062. + cpu_lock_irqsave(cpu, flags);
  22063. pset = per_cpu_ptr(zone->pageset, cpu);
  22064. pcp = &pset->pcp;
  22065. - if (pcp->count) {
  22066. - free_pcppages_bulk(zone, pcp->count, pcp);
  22067. + count = pcp->count;
  22068. + if (count) {
  22069. + isolate_pcp_pages(count, pcp, &dst);
  22070. pcp->count = 0;
  22071. }
  22072. - local_irq_restore(flags);
  22073. + cpu_unlock_irqrestore(cpu, flags);
  22074. + if (count)
  22075. + free_pcppages_bulk(zone, count, &dst);
  22076. }
  22077. /*
  22078. @@ -2339,8 +2385,17 @@ void drain_all_pages(struct zone *zone)
  22079. else
  22080. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  22081. }
  22082. +#ifndef CONFIG_PREEMPT_RT_BASE
  22083. on_each_cpu_mask(&cpus_with_pcps, (smp_call_func_t) drain_local_pages,
  22084. zone, 1);
  22085. +#else
  22086. + for_each_cpu(cpu, &cpus_with_pcps) {
  22087. + if (zone)
  22088. + drain_pages_zone(cpu, zone);
  22089. + else
  22090. + drain_pages(cpu);
  22091. + }
  22092. +#endif
  22093. }
  22094. #ifdef CONFIG_HIBERNATION
  22095. @@ -2400,7 +2455,7 @@ void free_hot_cold_page(struct page *page, bool cold)
  22096. migratetype = get_pfnblock_migratetype(page, pfn);
  22097. set_pcppage_migratetype(page, migratetype);
  22098. - local_irq_save(flags);
  22099. + local_lock_irqsave(pa_lock, flags);
  22100. __count_vm_event(PGFREE);
  22101. /*
  22102. @@ -2426,12 +2481,17 @@ void free_hot_cold_page(struct page *page, bool cold)
  22103. pcp->count++;
  22104. if (pcp->count >= pcp->high) {
  22105. unsigned long batch = READ_ONCE(pcp->batch);
  22106. - free_pcppages_bulk(zone, batch, pcp);
  22107. + LIST_HEAD(dst);
  22108. +
  22109. + isolate_pcp_pages(batch, pcp, &dst);
  22110. pcp->count -= batch;
  22111. + local_unlock_irqrestore(pa_lock, flags);
  22112. + free_pcppages_bulk(zone, batch, &dst);
  22113. + return;
  22114. }
  22115. out:
  22116. - local_irq_restore(flags);
  22117. + local_unlock_irqrestore(pa_lock, flags);
  22118. }
  22119. /*
  22120. @@ -2568,7 +2628,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  22121. struct per_cpu_pages *pcp;
  22122. struct list_head *list;
  22123. - local_irq_save(flags);
  22124. + local_lock_irqsave(pa_lock, flags);
  22125. do {
  22126. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  22127. list = &pcp->lists[migratetype];
  22128. @@ -2595,7 +2655,7 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  22129. * allocate greater than order-1 page units with __GFP_NOFAIL.
  22130. */
  22131. WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
  22132. - spin_lock_irqsave(&zone->lock, flags);
  22133. + local_spin_lock_irqsave(pa_lock, &zone->lock, flags);
  22134. do {
  22135. page = NULL;
  22136. @@ -2607,22 +2667,24 @@ struct page *buffered_rmqueue(struct zone *preferred_zone,
  22137. if (!page)
  22138. page = __rmqueue(zone, order, migratetype);
  22139. } while (page && check_new_pages(page, order));
  22140. - spin_unlock(&zone->lock);
  22141. - if (!page)
  22142. + if (!page) {
  22143. + spin_unlock(&zone->lock);
  22144. goto failed;
  22145. + }
  22146. __mod_zone_freepage_state(zone, -(1 << order),
  22147. get_pcppage_migratetype(page));
  22148. + spin_unlock(&zone->lock);
  22149. }
  22150. __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
  22151. zone_statistics(preferred_zone, zone, gfp_flags);
  22152. - local_irq_restore(flags);
  22153. + local_unlock_irqrestore(pa_lock, flags);
  22154. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  22155. return page;
  22156. failed:
  22157. - local_irq_restore(flags);
  22158. + local_unlock_irqrestore(pa_lock, flags);
  22159. return NULL;
  22160. }
  22161. @@ -6528,7 +6590,9 @@ static int page_alloc_cpu_notify(struct notifier_block *self,
  22162. int cpu = (unsigned long)hcpu;
  22163. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  22164. + local_lock_irq_on(swapvec_lock, cpu);
  22165. lru_add_drain_cpu(cpu);
  22166. + local_unlock_irq_on(swapvec_lock, cpu);
  22167. drain_pages(cpu);
  22168. /*
  22169. @@ -6554,6 +6618,7 @@ static int page_alloc_cpu_notify(struct notifier_block *self,
  22170. void __init page_alloc_init(void)
  22171. {
  22172. hotcpu_notifier(page_alloc_cpu_notify, 0);
  22173. + local_irq_lock_init(pa_lock);
  22174. }
  22175. /*
  22176. @@ -7370,7 +7435,7 @@ void zone_pcp_reset(struct zone *zone)
  22177. struct per_cpu_pageset *pset;
  22178. /* avoid races with drain_pages() */
  22179. - local_irq_save(flags);
  22180. + local_lock_irqsave(pa_lock, flags);
  22181. if (zone->pageset != &boot_pageset) {
  22182. for_each_online_cpu(cpu) {
  22183. pset = per_cpu_ptr(zone->pageset, cpu);
  22184. @@ -7379,7 +7444,7 @@ void zone_pcp_reset(struct zone *zone)
  22185. free_percpu(zone->pageset);
  22186. zone->pageset = &boot_pageset;
  22187. }
  22188. - local_irq_restore(flags);
  22189. + local_unlock_irqrestore(pa_lock, flags);
  22190. }
  22191. #ifdef CONFIG_MEMORY_HOTREMOVE
  22192. diff --git a/mm/slab.h b/mm/slab.h
  22193. index 9653f2e2591a..b7371e026627 100644
  22194. --- a/mm/slab.h
  22195. +++ b/mm/slab.h
  22196. @@ -426,7 +426,11 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
  22197. * The slab lists for all objects.
  22198. */
  22199. struct kmem_cache_node {
  22200. +#ifdef CONFIG_SLUB
  22201. + raw_spinlock_t list_lock;
  22202. +#else
  22203. spinlock_t list_lock;
  22204. +#endif
  22205. #ifdef CONFIG_SLAB
  22206. struct list_head slabs_partial; /* partial list first, better asm code */
  22207. diff --git a/mm/slub.c b/mm/slub.c
  22208. index 9adae58462f8..4b386747f050 100644
  22209. --- a/mm/slub.c
  22210. +++ b/mm/slub.c
  22211. @@ -1145,7 +1145,7 @@ static noinline int free_debug_processing(
  22212. unsigned long uninitialized_var(flags);
  22213. int ret = 0;
  22214. - spin_lock_irqsave(&n->list_lock, flags);
  22215. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22216. slab_lock(page);
  22217. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  22218. @@ -1180,7 +1180,7 @@ static noinline int free_debug_processing(
  22219. bulk_cnt, cnt);
  22220. slab_unlock(page);
  22221. - spin_unlock_irqrestore(&n->list_lock, flags);
  22222. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22223. if (!ret)
  22224. slab_fix(s, "Object at 0x%p not freed", object);
  22225. return ret;
  22226. @@ -1308,6 +1308,12 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
  22227. #endif /* CONFIG_SLUB_DEBUG */
  22228. +struct slub_free_list {
  22229. + raw_spinlock_t lock;
  22230. + struct list_head list;
  22231. +};
  22232. +static DEFINE_PER_CPU(struct slub_free_list, slub_free_list);
  22233. +
  22234. /*
  22235. * Hooks for other subsystems that check memory allocations. In a typical
  22236. * production configuration these hooks all should produce no code at all.
  22237. @@ -1527,10 +1533,17 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  22238. void *start, *p;
  22239. int idx, order;
  22240. bool shuffle;
  22241. + bool enableirqs = false;
  22242. flags &= gfp_allowed_mask;
  22243. if (gfpflags_allow_blocking(flags))
  22244. + enableirqs = true;
  22245. +#ifdef CONFIG_PREEMPT_RT_FULL
  22246. + if (system_state == SYSTEM_RUNNING)
  22247. + enableirqs = true;
  22248. +#endif
  22249. + if (enableirqs)
  22250. local_irq_enable();
  22251. flags |= s->allocflags;
  22252. @@ -1605,7 +1618,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  22253. page->frozen = 1;
  22254. out:
  22255. - if (gfpflags_allow_blocking(flags))
  22256. + if (enableirqs)
  22257. local_irq_disable();
  22258. if (!page)
  22259. return NULL;
  22260. @@ -1664,6 +1677,16 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
  22261. __free_pages(page, order);
  22262. }
  22263. +static void free_delayed(struct list_head *h)
  22264. +{
  22265. + while(!list_empty(h)) {
  22266. + struct page *page = list_first_entry(h, struct page, lru);
  22267. +
  22268. + list_del(&page->lru);
  22269. + __free_slab(page->slab_cache, page);
  22270. + }
  22271. +}
  22272. +
  22273. #define need_reserve_slab_rcu \
  22274. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  22275. @@ -1695,6 +1718,12 @@ static void free_slab(struct kmem_cache *s, struct page *page)
  22276. }
  22277. call_rcu(head, rcu_free_slab);
  22278. + } else if (irqs_disabled()) {
  22279. + struct slub_free_list *f = this_cpu_ptr(&slub_free_list);
  22280. +
  22281. + raw_spin_lock(&f->lock);
  22282. + list_add(&page->lru, &f->list);
  22283. + raw_spin_unlock(&f->lock);
  22284. } else
  22285. __free_slab(s, page);
  22286. }
  22287. @@ -1802,7 +1831,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  22288. if (!n || !n->nr_partial)
  22289. return NULL;
  22290. - spin_lock(&n->list_lock);
  22291. + raw_spin_lock(&n->list_lock);
  22292. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  22293. void *t;
  22294. @@ -1827,7 +1856,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  22295. break;
  22296. }
  22297. - spin_unlock(&n->list_lock);
  22298. + raw_spin_unlock(&n->list_lock);
  22299. return object;
  22300. }
  22301. @@ -2073,7 +2102,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  22302. * that acquire_slab() will see a slab page that
  22303. * is frozen
  22304. */
  22305. - spin_lock(&n->list_lock);
  22306. + raw_spin_lock(&n->list_lock);
  22307. }
  22308. } else {
  22309. m = M_FULL;
  22310. @@ -2084,7 +2113,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  22311. * slabs from diagnostic functions will not see
  22312. * any frozen slabs.
  22313. */
  22314. - spin_lock(&n->list_lock);
  22315. + raw_spin_lock(&n->list_lock);
  22316. }
  22317. }
  22318. @@ -2119,7 +2148,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
  22319. goto redo;
  22320. if (lock)
  22321. - spin_unlock(&n->list_lock);
  22322. + raw_spin_unlock(&n->list_lock);
  22323. if (m == M_FREE) {
  22324. stat(s, DEACTIVATE_EMPTY);
  22325. @@ -2151,10 +2180,10 @@ static void unfreeze_partials(struct kmem_cache *s,
  22326. n2 = get_node(s, page_to_nid(page));
  22327. if (n != n2) {
  22328. if (n)
  22329. - spin_unlock(&n->list_lock);
  22330. + raw_spin_unlock(&n->list_lock);
  22331. n = n2;
  22332. - spin_lock(&n->list_lock);
  22333. + raw_spin_lock(&n->list_lock);
  22334. }
  22335. do {
  22336. @@ -2183,7 +2212,7 @@ static void unfreeze_partials(struct kmem_cache *s,
  22337. }
  22338. if (n)
  22339. - spin_unlock(&n->list_lock);
  22340. + raw_spin_unlock(&n->list_lock);
  22341. while (discard_page) {
  22342. page = discard_page;
  22343. @@ -2222,14 +2251,21 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  22344. pobjects = oldpage->pobjects;
  22345. pages = oldpage->pages;
  22346. if (drain && pobjects > s->cpu_partial) {
  22347. + struct slub_free_list *f;
  22348. unsigned long flags;
  22349. + LIST_HEAD(tofree);
  22350. /*
  22351. * partial array is full. Move the existing
  22352. * set to the per node partial list.
  22353. */
  22354. local_irq_save(flags);
  22355. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  22356. + f = this_cpu_ptr(&slub_free_list);
  22357. + raw_spin_lock(&f->lock);
  22358. + list_splice_init(&f->list, &tofree);
  22359. + raw_spin_unlock(&f->lock);
  22360. local_irq_restore(flags);
  22361. + free_delayed(&tofree);
  22362. oldpage = NULL;
  22363. pobjects = 0;
  22364. pages = 0;
  22365. @@ -2301,7 +2337,22 @@ static bool has_cpu_slab(int cpu, void *info)
  22366. static void flush_all(struct kmem_cache *s)
  22367. {
  22368. + LIST_HEAD(tofree);
  22369. + int cpu;
  22370. +
  22371. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  22372. + for_each_online_cpu(cpu) {
  22373. + struct slub_free_list *f;
  22374. +
  22375. + if (!has_cpu_slab(cpu, s))
  22376. + continue;
  22377. +
  22378. + f = &per_cpu(slub_free_list, cpu);
  22379. + raw_spin_lock_irq(&f->lock);
  22380. + list_splice_init(&f->list, &tofree);
  22381. + raw_spin_unlock_irq(&f->lock);
  22382. + free_delayed(&tofree);
  22383. + }
  22384. }
  22385. /*
  22386. @@ -2337,10 +2388,10 @@ static unsigned long count_partial(struct kmem_cache_node *n,
  22387. unsigned long x = 0;
  22388. struct page *page;
  22389. - spin_lock_irqsave(&n->list_lock, flags);
  22390. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22391. list_for_each_entry(page, &n->partial, lru)
  22392. x += get_count(page);
  22393. - spin_unlock_irqrestore(&n->list_lock, flags);
  22394. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22395. return x;
  22396. }
  22397. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  22398. @@ -2478,8 +2529,10 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  22399. * already disabled (which is the case for bulk allocation).
  22400. */
  22401. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  22402. - unsigned long addr, struct kmem_cache_cpu *c)
  22403. + unsigned long addr, struct kmem_cache_cpu *c,
  22404. + struct list_head *to_free)
  22405. {
  22406. + struct slub_free_list *f;
  22407. void *freelist;
  22408. struct page *page;
  22409. @@ -2539,6 +2592,13 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  22410. VM_BUG_ON(!c->page->frozen);
  22411. c->freelist = get_freepointer(s, freelist);
  22412. c->tid = next_tid(c->tid);
  22413. +
  22414. +out:
  22415. + f = this_cpu_ptr(&slub_free_list);
  22416. + raw_spin_lock(&f->lock);
  22417. + list_splice_init(&f->list, to_free);
  22418. + raw_spin_unlock(&f->lock);
  22419. +
  22420. return freelist;
  22421. new_slab:
  22422. @@ -2570,7 +2630,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  22423. deactivate_slab(s, page, get_freepointer(s, freelist));
  22424. c->page = NULL;
  22425. c->freelist = NULL;
  22426. - return freelist;
  22427. + goto out;
  22428. }
  22429. /*
  22430. @@ -2582,6 +2642,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  22431. {
  22432. void *p;
  22433. unsigned long flags;
  22434. + LIST_HEAD(tofree);
  22435. local_irq_save(flags);
  22436. #ifdef CONFIG_PREEMPT
  22437. @@ -2593,8 +2654,9 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  22438. c = this_cpu_ptr(s->cpu_slab);
  22439. #endif
  22440. - p = ___slab_alloc(s, gfpflags, node, addr, c);
  22441. + p = ___slab_alloc(s, gfpflags, node, addr, c, &tofree);
  22442. local_irq_restore(flags);
  22443. + free_delayed(&tofree);
  22444. return p;
  22445. }
  22446. @@ -2780,7 +2842,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  22447. do {
  22448. if (unlikely(n)) {
  22449. - spin_unlock_irqrestore(&n->list_lock, flags);
  22450. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22451. n = NULL;
  22452. }
  22453. prior = page->freelist;
  22454. @@ -2812,7 +2874,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  22455. * Otherwise the list_lock will synchronize with
  22456. * other processors updating the list of slabs.
  22457. */
  22458. - spin_lock_irqsave(&n->list_lock, flags);
  22459. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22460. }
  22461. }
  22462. @@ -2854,7 +2916,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  22463. add_partial(n, page, DEACTIVATE_TO_TAIL);
  22464. stat(s, FREE_ADD_PARTIAL);
  22465. }
  22466. - spin_unlock_irqrestore(&n->list_lock, flags);
  22467. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22468. return;
  22469. slab_empty:
  22470. @@ -2869,7 +2931,7 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
  22471. remove_full(s, n, page);
  22472. }
  22473. - spin_unlock_irqrestore(&n->list_lock, flags);
  22474. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22475. stat(s, FREE_SLAB);
  22476. discard_slab(s, page);
  22477. }
  22478. @@ -3074,6 +3136,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  22479. void **p)
  22480. {
  22481. struct kmem_cache_cpu *c;
  22482. + LIST_HEAD(to_free);
  22483. int i;
  22484. /* memcg and kmem_cache debug support */
  22485. @@ -3097,7 +3160,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  22486. * of re-populating per CPU c->freelist
  22487. */
  22488. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  22489. - _RET_IP_, c);
  22490. + _RET_IP_, c, &to_free);
  22491. if (unlikely(!p[i]))
  22492. goto error;
  22493. @@ -3109,6 +3172,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  22494. }
  22495. c->tid = next_tid(c->tid);
  22496. local_irq_enable();
  22497. + free_delayed(&to_free);
  22498. /* Clear memory outside IRQ disabled fastpath loop */
  22499. if (unlikely(flags & __GFP_ZERO)) {
  22500. @@ -3256,7 +3320,7 @@ static void
  22501. init_kmem_cache_node(struct kmem_cache_node *n)
  22502. {
  22503. n->nr_partial = 0;
  22504. - spin_lock_init(&n->list_lock);
  22505. + raw_spin_lock_init(&n->list_lock);
  22506. INIT_LIST_HEAD(&n->partial);
  22507. #ifdef CONFIG_SLUB_DEBUG
  22508. atomic_long_set(&n->nr_slabs, 0);
  22509. @@ -3600,6 +3664,10 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,
  22510. const char *text)
  22511. {
  22512. #ifdef CONFIG_SLUB_DEBUG
  22513. +#ifdef CONFIG_PREEMPT_RT_BASE
  22514. + /* XXX move out of irq-off section */
  22515. + slab_err(s, page, text, s->name);
  22516. +#else
  22517. void *addr = page_address(page);
  22518. void *p;
  22519. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  22520. @@ -3620,6 +3688,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,
  22521. slab_unlock(page);
  22522. kfree(map);
  22523. #endif
  22524. +#endif
  22525. }
  22526. /*
  22527. @@ -3633,7 +3702,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  22528. struct page *page, *h;
  22529. BUG_ON(irqs_disabled());
  22530. - spin_lock_irq(&n->list_lock);
  22531. + raw_spin_lock_irq(&n->list_lock);
  22532. list_for_each_entry_safe(page, h, &n->partial, lru) {
  22533. if (!page->inuse) {
  22534. remove_partial(n, page);
  22535. @@ -3643,7 +3712,7 @@ static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  22536. "Objects remaining in %s on __kmem_cache_shutdown()");
  22537. }
  22538. }
  22539. - spin_unlock_irq(&n->list_lock);
  22540. + raw_spin_unlock_irq(&n->list_lock);
  22541. list_for_each_entry_safe(page, h, &discard, lru)
  22542. discard_slab(s, page);
  22543. @@ -3901,7 +3970,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  22544. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  22545. INIT_LIST_HEAD(promote + i);
  22546. - spin_lock_irqsave(&n->list_lock, flags);
  22547. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22548. /*
  22549. * Build lists of slabs to discard or promote.
  22550. @@ -3932,7 +4001,7 @@ int __kmem_cache_shrink(struct kmem_cache *s, bool deactivate)
  22551. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  22552. list_splice(promote + i, &n->partial);
  22553. - spin_unlock_irqrestore(&n->list_lock, flags);
  22554. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22555. /* Release empty slabs */
  22556. list_for_each_entry_safe(page, t, &discard, lru)
  22557. @@ -4108,6 +4177,12 @@ void __init kmem_cache_init(void)
  22558. {
  22559. static __initdata struct kmem_cache boot_kmem_cache,
  22560. boot_kmem_cache_node;
  22561. + int cpu;
  22562. +
  22563. + for_each_possible_cpu(cpu) {
  22564. + raw_spin_lock_init(&per_cpu(slub_free_list, cpu).lock);
  22565. + INIT_LIST_HEAD(&per_cpu(slub_free_list, cpu).list);
  22566. + }
  22567. if (debug_guardpage_minorder())
  22568. slub_max_order = 0;
  22569. @@ -4354,7 +4429,7 @@ static int validate_slab_node(struct kmem_cache *s,
  22570. struct page *page;
  22571. unsigned long flags;
  22572. - spin_lock_irqsave(&n->list_lock, flags);
  22573. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22574. list_for_each_entry(page, &n->partial, lru) {
  22575. validate_slab_slab(s, page, map);
  22576. @@ -4376,7 +4451,7 @@ static int validate_slab_node(struct kmem_cache *s,
  22577. s->name, count, atomic_long_read(&n->nr_slabs));
  22578. out:
  22579. - spin_unlock_irqrestore(&n->list_lock, flags);
  22580. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22581. return count;
  22582. }
  22583. @@ -4564,12 +4639,12 @@ static int list_locations(struct kmem_cache *s, char *buf,
  22584. if (!atomic_long_read(&n->nr_slabs))
  22585. continue;
  22586. - spin_lock_irqsave(&n->list_lock, flags);
  22587. + raw_spin_lock_irqsave(&n->list_lock, flags);
  22588. list_for_each_entry(page, &n->partial, lru)
  22589. process_slab(&t, s, page, alloc, map);
  22590. list_for_each_entry(page, &n->full, lru)
  22591. process_slab(&t, s, page, alloc, map);
  22592. - spin_unlock_irqrestore(&n->list_lock, flags);
  22593. + raw_spin_unlock_irqrestore(&n->list_lock, flags);
  22594. }
  22595. for (i = 0; i < t.count; i++) {
  22596. diff --git a/mm/swap.c b/mm/swap.c
  22597. index 75c63bb2a1da..93fe549eb11e 100644
  22598. --- a/mm/swap.c
  22599. +++ b/mm/swap.c
  22600. @@ -32,6 +32,7 @@
  22601. #include <linux/memcontrol.h>
  22602. #include <linux/gfp.h>
  22603. #include <linux/uio.h>
  22604. +#include <linux/locallock.h>
  22605. #include <linux/hugetlb.h>
  22606. #include <linux/page_idle.h>
  22607. @@ -50,6 +51,8 @@ static DEFINE_PER_CPU(struct pagevec, lru_deactivate_pvecs);
  22608. #ifdef CONFIG_SMP
  22609. static DEFINE_PER_CPU(struct pagevec, activate_page_pvecs);
  22610. #endif
  22611. +static DEFINE_LOCAL_IRQ_LOCK(rotate_lock);
  22612. +DEFINE_LOCAL_IRQ_LOCK(swapvec_lock);
  22613. /*
  22614. * This path almost never happens for VM activity - pages are normally
  22615. @@ -240,11 +243,11 @@ void rotate_reclaimable_page(struct page *page)
  22616. unsigned long flags;
  22617. get_page(page);
  22618. - local_irq_save(flags);
  22619. + local_lock_irqsave(rotate_lock, flags);
  22620. pvec = this_cpu_ptr(&lru_rotate_pvecs);
  22621. if (!pagevec_add(pvec, page) || PageCompound(page))
  22622. pagevec_move_tail(pvec);
  22623. - local_irq_restore(flags);
  22624. + local_unlock_irqrestore(rotate_lock, flags);
  22625. }
  22626. }
  22627. @@ -294,12 +297,13 @@ void activate_page(struct page *page)
  22628. {
  22629. page = compound_head(page);
  22630. if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) {
  22631. - struct pagevec *pvec = &get_cpu_var(activate_page_pvecs);
  22632. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  22633. + activate_page_pvecs);
  22634. get_page(page);
  22635. if (!pagevec_add(pvec, page) || PageCompound(page))
  22636. pagevec_lru_move_fn(pvec, __activate_page, NULL);
  22637. - put_cpu_var(activate_page_pvecs);
  22638. + put_locked_var(swapvec_lock, activate_page_pvecs);
  22639. }
  22640. }
  22641. @@ -326,7 +330,7 @@ void activate_page(struct page *page)
  22642. static void __lru_cache_activate_page(struct page *page)
  22643. {
  22644. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  22645. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  22646. int i;
  22647. /*
  22648. @@ -348,7 +352,7 @@ static void __lru_cache_activate_page(struct page *page)
  22649. }
  22650. }
  22651. - put_cpu_var(lru_add_pvec);
  22652. + put_locked_var(swapvec_lock, lru_add_pvec);
  22653. }
  22654. /*
  22655. @@ -390,12 +394,12 @@ EXPORT_SYMBOL(mark_page_accessed);
  22656. static void __lru_cache_add(struct page *page)
  22657. {
  22658. - struct pagevec *pvec = &get_cpu_var(lru_add_pvec);
  22659. + struct pagevec *pvec = &get_locked_var(swapvec_lock, lru_add_pvec);
  22660. get_page(page);
  22661. if (!pagevec_add(pvec, page) || PageCompound(page))
  22662. __pagevec_lru_add(pvec);
  22663. - put_cpu_var(lru_add_pvec);
  22664. + put_locked_var(swapvec_lock, lru_add_pvec);
  22665. }
  22666. /**
  22667. @@ -593,9 +597,15 @@ void lru_add_drain_cpu(int cpu)
  22668. unsigned long flags;
  22669. /* No harm done if a racing interrupt already did this */
  22670. - local_irq_save(flags);
  22671. +#ifdef CONFIG_PREEMPT_RT_BASE
  22672. + local_lock_irqsave_on(rotate_lock, flags, cpu);
  22673. pagevec_move_tail(pvec);
  22674. - local_irq_restore(flags);
  22675. + local_unlock_irqrestore_on(rotate_lock, flags, cpu);
  22676. +#else
  22677. + local_lock_irqsave(rotate_lock, flags);
  22678. + pagevec_move_tail(pvec);
  22679. + local_unlock_irqrestore(rotate_lock, flags);
  22680. +#endif
  22681. }
  22682. pvec = &per_cpu(lru_deactivate_file_pvecs, cpu);
  22683. @@ -627,11 +637,12 @@ void deactivate_file_page(struct page *page)
  22684. return;
  22685. if (likely(get_page_unless_zero(page))) {
  22686. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_file_pvecs);
  22687. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  22688. + lru_deactivate_file_pvecs);
  22689. if (!pagevec_add(pvec, page) || PageCompound(page))
  22690. pagevec_lru_move_fn(pvec, lru_deactivate_file_fn, NULL);
  22691. - put_cpu_var(lru_deactivate_file_pvecs);
  22692. + put_locked_var(swapvec_lock, lru_deactivate_file_pvecs);
  22693. }
  22694. }
  22695. @@ -646,27 +657,31 @@ void deactivate_file_page(struct page *page)
  22696. void deactivate_page(struct page *page)
  22697. {
  22698. if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) {
  22699. - struct pagevec *pvec = &get_cpu_var(lru_deactivate_pvecs);
  22700. + struct pagevec *pvec = &get_locked_var(swapvec_lock,
  22701. + lru_deactivate_pvecs);
  22702. get_page(page);
  22703. if (!pagevec_add(pvec, page) || PageCompound(page))
  22704. pagevec_lru_move_fn(pvec, lru_deactivate_fn, NULL);
  22705. - put_cpu_var(lru_deactivate_pvecs);
  22706. + put_locked_var(swapvec_lock, lru_deactivate_pvecs);
  22707. }
  22708. }
  22709. void lru_add_drain(void)
  22710. {
  22711. - lru_add_drain_cpu(get_cpu());
  22712. - put_cpu();
  22713. + lru_add_drain_cpu(local_lock_cpu(swapvec_lock));
  22714. + local_unlock_cpu(swapvec_lock);
  22715. }
  22716. -static void lru_add_drain_per_cpu(struct work_struct *dummy)
  22717. +#ifdef CONFIG_PREEMPT_RT_BASE
  22718. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  22719. {
  22720. - lru_add_drain();
  22721. + local_lock_on(swapvec_lock, cpu);
  22722. + lru_add_drain_cpu(cpu);
  22723. + local_unlock_on(swapvec_lock, cpu);
  22724. }
  22725. -static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  22726. +#else
  22727. /*
  22728. * lru_add_drain_wq is used to do lru_add_drain_all() from a WQ_MEM_RECLAIM
  22729. @@ -686,6 +701,22 @@ static int __init lru_init(void)
  22730. }
  22731. early_initcall(lru_init);
  22732. +static void lru_add_drain_per_cpu(struct work_struct *dummy)
  22733. +{
  22734. + lru_add_drain();
  22735. +}
  22736. +
  22737. +static DEFINE_PER_CPU(struct work_struct, lru_add_drain_work);
  22738. +static inline void remote_lru_add_drain(int cpu, struct cpumask *has_work)
  22739. +{
  22740. + struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  22741. +
  22742. + INIT_WORK(work, lru_add_drain_per_cpu);
  22743. + queue_work_on(cpu, lru_add_drain_wq, work);
  22744. + cpumask_set_cpu(cpu, has_work);
  22745. +}
  22746. +#endif
  22747. +
  22748. void lru_add_drain_all(void)
  22749. {
  22750. static DEFINE_MUTEX(lock);
  22751. @@ -697,21 +728,18 @@ void lru_add_drain_all(void)
  22752. cpumask_clear(&has_work);
  22753. for_each_online_cpu(cpu) {
  22754. - struct work_struct *work = &per_cpu(lru_add_drain_work, cpu);
  22755. -
  22756. if (pagevec_count(&per_cpu(lru_add_pvec, cpu)) ||
  22757. pagevec_count(&per_cpu(lru_rotate_pvecs, cpu)) ||
  22758. pagevec_count(&per_cpu(lru_deactivate_file_pvecs, cpu)) ||
  22759. pagevec_count(&per_cpu(lru_deactivate_pvecs, cpu)) ||
  22760. - need_activate_page_drain(cpu)) {
  22761. - INIT_WORK(work, lru_add_drain_per_cpu);
  22762. - queue_work_on(cpu, lru_add_drain_wq, work);
  22763. - cpumask_set_cpu(cpu, &has_work);
  22764. - }
  22765. + need_activate_page_drain(cpu))
  22766. + remote_lru_add_drain(cpu, &has_work);
  22767. }
  22768. +#ifndef CONFIG_PREEMPT_RT_BASE
  22769. for_each_cpu(cpu, &has_work)
  22770. flush_work(&per_cpu(lru_add_drain_work, cpu));
  22771. +#endif
  22772. put_online_cpus();
  22773. mutex_unlock(&lock);
  22774. diff --git a/mm/truncate.c b/mm/truncate.c
  22775. index a01cce450a26..4bda37604f99 100644
  22776. --- a/mm/truncate.c
  22777. +++ b/mm/truncate.c
  22778. @@ -62,9 +62,12 @@ static void clear_exceptional_entry(struct address_space *mapping,
  22779. * protected by mapping->tree_lock.
  22780. */
  22781. if (!workingset_node_shadows(node) &&
  22782. - !list_empty(&node->private_list))
  22783. - list_lru_del(&workingset_shadow_nodes,
  22784. + !list_empty(&node->private_list)) {
  22785. + local_lock(workingset_shadow_lock);
  22786. + list_lru_del(&__workingset_shadow_nodes,
  22787. &node->private_list);
  22788. + local_unlock(workingset_shadow_lock);
  22789. + }
  22790. __radix_tree_delete_node(&mapping->page_tree, node);
  22791. unlock:
  22792. spin_unlock_irq(&mapping->tree_lock);
  22793. diff --git a/mm/vmalloc.c b/mm/vmalloc.c
  22794. index 91f44e78c516..06ec393bb97d 100644
  22795. --- a/mm/vmalloc.c
  22796. +++ b/mm/vmalloc.c
  22797. @@ -845,7 +845,7 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  22798. struct vmap_block *vb;
  22799. struct vmap_area *va;
  22800. unsigned long vb_idx;
  22801. - int node, err;
  22802. + int node, err, cpu;
  22803. void *vaddr;
  22804. node = numa_node_id();
  22805. @@ -888,11 +888,12 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
  22806. BUG_ON(err);
  22807. radix_tree_preload_end();
  22808. - vbq = &get_cpu_var(vmap_block_queue);
  22809. + cpu = get_cpu_light();
  22810. + vbq = this_cpu_ptr(&vmap_block_queue);
  22811. spin_lock(&vbq->lock);
  22812. list_add_tail_rcu(&vb->free_list, &vbq->free);
  22813. spin_unlock(&vbq->lock);
  22814. - put_cpu_var(vmap_block_queue);
  22815. + put_cpu_light();
  22816. return vaddr;
  22817. }
  22818. @@ -961,6 +962,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  22819. struct vmap_block *vb;
  22820. void *vaddr = NULL;
  22821. unsigned int order;
  22822. + int cpu;
  22823. BUG_ON(offset_in_page(size));
  22824. BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
  22825. @@ -975,7 +977,8 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  22826. order = get_order(size);
  22827. rcu_read_lock();
  22828. - vbq = &get_cpu_var(vmap_block_queue);
  22829. + cpu = get_cpu_light();
  22830. + vbq = this_cpu_ptr(&vmap_block_queue);
  22831. list_for_each_entry_rcu(vb, &vbq->free, free_list) {
  22832. unsigned long pages_off;
  22833. @@ -998,7 +1001,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
  22834. break;
  22835. }
  22836. - put_cpu_var(vmap_block_queue);
  22837. + put_cpu_light();
  22838. rcu_read_unlock();
  22839. /* Allocate new block if nothing was found */
  22840. diff --git a/mm/vmstat.c b/mm/vmstat.c
  22841. index 89cec42d19ff..fb73631fb90b 100644
  22842. --- a/mm/vmstat.c
  22843. +++ b/mm/vmstat.c
  22844. @@ -245,6 +245,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  22845. long x;
  22846. long t;
  22847. + preempt_disable_rt();
  22848. x = delta + __this_cpu_read(*p);
  22849. t = __this_cpu_read(pcp->stat_threshold);
  22850. @@ -254,6 +255,7 @@ void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
  22851. x = 0;
  22852. }
  22853. __this_cpu_write(*p, x);
  22854. + preempt_enable_rt();
  22855. }
  22856. EXPORT_SYMBOL(__mod_zone_page_state);
  22857. @@ -265,6 +267,7 @@ void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
  22858. long x;
  22859. long t;
  22860. + preempt_disable_rt();
  22861. x = delta + __this_cpu_read(*p);
  22862. t = __this_cpu_read(pcp->stat_threshold);
  22863. @@ -274,6 +277,7 @@ void __mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
  22864. x = 0;
  22865. }
  22866. __this_cpu_write(*p, x);
  22867. + preempt_enable_rt();
  22868. }
  22869. EXPORT_SYMBOL(__mod_node_page_state);
  22870. @@ -306,6 +310,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  22871. s8 __percpu *p = pcp->vm_stat_diff + item;
  22872. s8 v, t;
  22873. + preempt_disable_rt();
  22874. v = __this_cpu_inc_return(*p);
  22875. t = __this_cpu_read(pcp->stat_threshold);
  22876. if (unlikely(v > t)) {
  22877. @@ -314,6 +319,7 @@ void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  22878. zone_page_state_add(v + overstep, zone, item);
  22879. __this_cpu_write(*p, -overstep);
  22880. }
  22881. + preempt_enable_rt();
  22882. }
  22883. void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22884. @@ -322,6 +328,7 @@ void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22885. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  22886. s8 v, t;
  22887. + preempt_disable_rt();
  22888. v = __this_cpu_inc_return(*p);
  22889. t = __this_cpu_read(pcp->stat_threshold);
  22890. if (unlikely(v > t)) {
  22891. @@ -330,6 +337,7 @@ void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22892. node_page_state_add(v + overstep, pgdat, item);
  22893. __this_cpu_write(*p, -overstep);
  22894. }
  22895. + preempt_enable_rt();
  22896. }
  22897. void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
  22898. @@ -350,6 +358,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  22899. s8 __percpu *p = pcp->vm_stat_diff + item;
  22900. s8 v, t;
  22901. + preempt_disable_rt();
  22902. v = __this_cpu_dec_return(*p);
  22903. t = __this_cpu_read(pcp->stat_threshold);
  22904. if (unlikely(v < - t)) {
  22905. @@ -358,6 +367,7 @@ void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  22906. zone_page_state_add(v - overstep, zone, item);
  22907. __this_cpu_write(*p, overstep);
  22908. }
  22909. + preempt_enable_rt();
  22910. }
  22911. void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22912. @@ -366,6 +376,7 @@ void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22913. s8 __percpu *p = pcp->vm_node_stat_diff + item;
  22914. s8 v, t;
  22915. + preempt_disable_rt();
  22916. v = __this_cpu_dec_return(*p);
  22917. t = __this_cpu_read(pcp->stat_threshold);
  22918. if (unlikely(v < - t)) {
  22919. @@ -374,6 +385,7 @@ void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
  22920. node_page_state_add(v - overstep, pgdat, item);
  22921. __this_cpu_write(*p, overstep);
  22922. }
  22923. + preempt_enable_rt();
  22924. }
  22925. void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
  22926. diff --git a/mm/workingset.c b/mm/workingset.c
  22927. index 617475f529f4..48674bf36fb1 100644
  22928. --- a/mm/workingset.c
  22929. +++ b/mm/workingset.c
  22930. @@ -334,7 +334,8 @@ void workingset_activation(struct page *page)
  22931. * point where they would still be useful.
  22932. */
  22933. -struct list_lru workingset_shadow_nodes;
  22934. +struct list_lru __workingset_shadow_nodes;
  22935. +DEFINE_LOCAL_IRQ_LOCK(workingset_shadow_lock);
  22936. static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  22937. struct shrink_control *sc)
  22938. @@ -344,9 +345,9 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker,
  22939. unsigned long pages;
  22940. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  22941. - local_irq_disable();
  22942. - shadow_nodes = list_lru_shrink_count(&workingset_shadow_nodes, sc);
  22943. - local_irq_enable();
  22944. + local_lock_irq(workingset_shadow_lock);
  22945. + shadow_nodes = list_lru_shrink_count(&__workingset_shadow_nodes, sc);
  22946. + local_unlock_irq(workingset_shadow_lock);
  22947. if (memcg_kmem_enabled()) {
  22948. pages = mem_cgroup_node_nr_lru_pages(sc->memcg, sc->nid,
  22949. @@ -438,9 +439,9 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,
  22950. spin_unlock(&mapping->tree_lock);
  22951. ret = LRU_REMOVED_RETRY;
  22952. out:
  22953. - local_irq_enable();
  22954. + local_unlock_irq(workingset_shadow_lock);
  22955. cond_resched();
  22956. - local_irq_disable();
  22957. + local_lock_irq(workingset_shadow_lock);
  22958. spin_lock(lru_lock);
  22959. return ret;
  22960. }
  22961. @@ -451,10 +452,10 @@ static unsigned long scan_shadow_nodes(struct shrinker *shrinker,
  22962. unsigned long ret;
  22963. /* list_lru lock nests inside IRQ-safe mapping->tree_lock */
  22964. - local_irq_disable();
  22965. - ret = list_lru_shrink_walk(&workingset_shadow_nodes, sc,
  22966. + local_lock_irq(workingset_shadow_lock);
  22967. + ret = list_lru_shrink_walk(&__workingset_shadow_nodes, sc,
  22968. shadow_lru_isolate, NULL);
  22969. - local_irq_enable();
  22970. + local_unlock_irq(workingset_shadow_lock);
  22971. return ret;
  22972. }
  22973. @@ -492,7 +493,7 @@ static int __init workingset_init(void)
  22974. pr_info("workingset: timestamp_bits=%d max_order=%d bucket_order=%u\n",
  22975. timestamp_bits, max_order, bucket_order);
  22976. - ret = list_lru_init_key(&workingset_shadow_nodes, &shadow_nodes_key);
  22977. + ret = list_lru_init_key(&__workingset_shadow_nodes, &shadow_nodes_key);
  22978. if (ret)
  22979. goto err;
  22980. ret = register_shrinker(&workingset_shadow_shrinker);
  22981. @@ -500,7 +501,7 @@ static int __init workingset_init(void)
  22982. goto err_list_lru;
  22983. return 0;
  22984. err_list_lru:
  22985. - list_lru_destroy(&workingset_shadow_nodes);
  22986. + list_lru_destroy(&__workingset_shadow_nodes);
  22987. err:
  22988. return ret;
  22989. }
  22990. diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
  22991. index b0bc023d25c5..5af6426fbcbe 100644
  22992. --- a/mm/zsmalloc.c
  22993. +++ b/mm/zsmalloc.c
  22994. @@ -53,6 +53,7 @@
  22995. #include <linux/mount.h>
  22996. #include <linux/migrate.h>
  22997. #include <linux/pagemap.h>
  22998. +#include <linux/locallock.h>
  22999. #define ZSPAGE_MAGIC 0x58
  23000. @@ -70,9 +71,22 @@
  23001. */
  23002. #define ZS_MAX_ZSPAGE_ORDER 2
  23003. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  23004. -
  23005. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  23006. +#ifdef CONFIG_PREEMPT_RT_FULL
  23007. +
  23008. +struct zsmalloc_handle {
  23009. + unsigned long addr;
  23010. + struct mutex lock;
  23011. +};
  23012. +
  23013. +#define ZS_HANDLE_ALLOC_SIZE (sizeof(struct zsmalloc_handle))
  23014. +
  23015. +#else
  23016. +
  23017. +#define ZS_HANDLE_ALLOC_SIZE (sizeof(unsigned long))
  23018. +#endif
  23019. +
  23020. /*
  23021. * Object location (<PFN>, <obj_idx>) is encoded as
  23022. * as single (unsigned long) handle value.
  23023. @@ -327,7 +341,7 @@ static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
  23024. static int create_cache(struct zs_pool *pool)
  23025. {
  23026. - pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  23027. + pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_ALLOC_SIZE,
  23028. 0, 0, NULL);
  23029. if (!pool->handle_cachep)
  23030. return 1;
  23031. @@ -351,10 +365,27 @@ static void destroy_cache(struct zs_pool *pool)
  23032. static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
  23033. {
  23034. - return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  23035. - gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  23036. + void *p;
  23037. +
  23038. + p = kmem_cache_alloc(pool->handle_cachep,
  23039. + gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  23040. +#ifdef CONFIG_PREEMPT_RT_FULL
  23041. + if (p) {
  23042. + struct zsmalloc_handle *zh = p;
  23043. +
  23044. + mutex_init(&zh->lock);
  23045. + }
  23046. +#endif
  23047. + return (unsigned long)p;
  23048. }
  23049. +#ifdef CONFIG_PREEMPT_RT_FULL
  23050. +static struct zsmalloc_handle *zs_get_pure_handle(unsigned long handle)
  23051. +{
  23052. + return (void *)(handle &~((1 << OBJ_TAG_BITS) - 1));
  23053. +}
  23054. +#endif
  23055. +
  23056. static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
  23057. {
  23058. kmem_cache_free(pool->handle_cachep, (void *)handle);
  23059. @@ -373,12 +404,18 @@ static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
  23060. static void record_obj(unsigned long handle, unsigned long obj)
  23061. {
  23062. +#ifdef CONFIG_PREEMPT_RT_FULL
  23063. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23064. +
  23065. + WRITE_ONCE(zh->addr, obj);
  23066. +#else
  23067. /*
  23068. * lsb of @obj represents handle lock while other bits
  23069. * represent object value the handle is pointing so
  23070. * updating shouldn't do store tearing.
  23071. */
  23072. WRITE_ONCE(*(unsigned long *)handle, obj);
  23073. +#endif
  23074. }
  23075. /* zpool driver */
  23076. @@ -467,6 +504,7 @@ MODULE_ALIAS("zpool-zsmalloc");
  23077. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  23078. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  23079. +static DEFINE_LOCAL_IRQ_LOCK(zs_map_area_lock);
  23080. static bool is_zspage_isolated(struct zspage *zspage)
  23081. {
  23082. @@ -902,7 +940,13 @@ static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
  23083. static unsigned long handle_to_obj(unsigned long handle)
  23084. {
  23085. +#ifdef CONFIG_PREEMPT_RT_FULL
  23086. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23087. +
  23088. + return zh->addr;
  23089. +#else
  23090. return *(unsigned long *)handle;
  23091. +#endif
  23092. }
  23093. static unsigned long obj_to_head(struct page *page, void *obj)
  23094. @@ -916,22 +960,46 @@ static unsigned long obj_to_head(struct page *page, void *obj)
  23095. static inline int testpin_tag(unsigned long handle)
  23096. {
  23097. +#ifdef CONFIG_PREEMPT_RT_FULL
  23098. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23099. +
  23100. + return mutex_is_locked(&zh->lock);
  23101. +#else
  23102. return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
  23103. +#endif
  23104. }
  23105. static inline int trypin_tag(unsigned long handle)
  23106. {
  23107. +#ifdef CONFIG_PREEMPT_RT_FULL
  23108. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23109. +
  23110. + return mutex_trylock(&zh->lock);
  23111. +#else
  23112. return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23113. +#endif
  23114. }
  23115. static void pin_tag(unsigned long handle)
  23116. {
  23117. +#ifdef CONFIG_PREEMPT_RT_FULL
  23118. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23119. +
  23120. + return mutex_lock(&zh->lock);
  23121. +#else
  23122. bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23123. +#endif
  23124. }
  23125. static void unpin_tag(unsigned long handle)
  23126. {
  23127. +#ifdef CONFIG_PREEMPT_RT_FULL
  23128. + struct zsmalloc_handle *zh = zs_get_pure_handle(handle);
  23129. +
  23130. + return mutex_unlock(&zh->lock);
  23131. +#else
  23132. bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
  23133. +#endif
  23134. }
  23135. static void reset_page(struct page *page)
  23136. @@ -1423,7 +1491,7 @@ void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  23137. class = pool->size_class[class_idx];
  23138. off = (class->size * obj_idx) & ~PAGE_MASK;
  23139. - area = &get_cpu_var(zs_map_area);
  23140. + area = &get_locked_var(zs_map_area_lock, zs_map_area);
  23141. area->vm_mm = mm;
  23142. if (off + class->size <= PAGE_SIZE) {
  23143. /* this object is contained entirely within a page */
  23144. @@ -1477,7 +1545,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  23145. __zs_unmap_object(area, pages, off, class->size);
  23146. }
  23147. - put_cpu_var(zs_map_area);
  23148. + put_locked_var(zs_map_area_lock, zs_map_area);
  23149. migrate_read_unlock(zspage);
  23150. unpin_tag(handle);
  23151. diff --git a/net/core/dev.c b/net/core/dev.c
  23152. index ea6312057a71..d114a4692cde 100644
  23153. --- a/net/core/dev.c
  23154. +++ b/net/core/dev.c
  23155. @@ -190,6 +190,7 @@ static unsigned int napi_gen_id = NR_CPUS;
  23156. static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
  23157. static seqcount_t devnet_rename_seq;
  23158. +static DEFINE_MUTEX(devnet_rename_mutex);
  23159. static inline void dev_base_seq_inc(struct net *net)
  23160. {
  23161. @@ -211,14 +212,14 @@ static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
  23162. static inline void rps_lock(struct softnet_data *sd)
  23163. {
  23164. #ifdef CONFIG_RPS
  23165. - spin_lock(&sd->input_pkt_queue.lock);
  23166. + raw_spin_lock(&sd->input_pkt_queue.raw_lock);
  23167. #endif
  23168. }
  23169. static inline void rps_unlock(struct softnet_data *sd)
  23170. {
  23171. #ifdef CONFIG_RPS
  23172. - spin_unlock(&sd->input_pkt_queue.lock);
  23173. + raw_spin_unlock(&sd->input_pkt_queue.raw_lock);
  23174. #endif
  23175. }
  23176. @@ -888,7 +889,8 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
  23177. strcpy(name, dev->name);
  23178. rcu_read_unlock();
  23179. if (read_seqcount_retry(&devnet_rename_seq, seq)) {
  23180. - cond_resched();
  23181. + mutex_lock(&devnet_rename_mutex);
  23182. + mutex_unlock(&devnet_rename_mutex);
  23183. goto retry;
  23184. }
  23185. @@ -1157,20 +1159,17 @@ int dev_change_name(struct net_device *dev, const char *newname)
  23186. if (dev->flags & IFF_UP)
  23187. return -EBUSY;
  23188. - write_seqcount_begin(&devnet_rename_seq);
  23189. + mutex_lock(&devnet_rename_mutex);
  23190. + __raw_write_seqcount_begin(&devnet_rename_seq);
  23191. - if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
  23192. - write_seqcount_end(&devnet_rename_seq);
  23193. - return 0;
  23194. - }
  23195. + if (strncmp(newname, dev->name, IFNAMSIZ) == 0)
  23196. + goto outunlock;
  23197. memcpy(oldname, dev->name, IFNAMSIZ);
  23198. err = dev_get_valid_name(net, dev, newname);
  23199. - if (err < 0) {
  23200. - write_seqcount_end(&devnet_rename_seq);
  23201. - return err;
  23202. - }
  23203. + if (err < 0)
  23204. + goto outunlock;
  23205. if (oldname[0] && !strchr(oldname, '%'))
  23206. netdev_info(dev, "renamed from %s\n", oldname);
  23207. @@ -1183,11 +1182,12 @@ int dev_change_name(struct net_device *dev, const char *newname)
  23208. if (ret) {
  23209. memcpy(dev->name, oldname, IFNAMSIZ);
  23210. dev->name_assign_type = old_assign_type;
  23211. - write_seqcount_end(&devnet_rename_seq);
  23212. - return ret;
  23213. + err = ret;
  23214. + goto outunlock;
  23215. }
  23216. - write_seqcount_end(&devnet_rename_seq);
  23217. + __raw_write_seqcount_end(&devnet_rename_seq);
  23218. + mutex_unlock(&devnet_rename_mutex);
  23219. netdev_adjacent_rename_links(dev, oldname);
  23220. @@ -1208,7 +1208,8 @@ int dev_change_name(struct net_device *dev, const char *newname)
  23221. /* err >= 0 after dev_alloc_name() or stores the first errno */
  23222. if (err >= 0) {
  23223. err = ret;
  23224. - write_seqcount_begin(&devnet_rename_seq);
  23225. + mutex_lock(&devnet_rename_mutex);
  23226. + __raw_write_seqcount_begin(&devnet_rename_seq);
  23227. memcpy(dev->name, oldname, IFNAMSIZ);
  23228. memcpy(oldname, newname, IFNAMSIZ);
  23229. dev->name_assign_type = old_assign_type;
  23230. @@ -1221,6 +1222,11 @@ int dev_change_name(struct net_device *dev, const char *newname)
  23231. }
  23232. return err;
  23233. +
  23234. +outunlock:
  23235. + __raw_write_seqcount_end(&devnet_rename_seq);
  23236. + mutex_unlock(&devnet_rename_mutex);
  23237. + return err;
  23238. }
  23239. /**
  23240. @@ -2268,6 +2274,7 @@ static void __netif_reschedule(struct Qdisc *q)
  23241. sd->output_queue_tailp = &q->next_sched;
  23242. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  23243. local_irq_restore(flags);
  23244. + preempt_check_resched_rt();
  23245. }
  23246. void __netif_schedule(struct Qdisc *q)
  23247. @@ -2349,6 +2356,7 @@ void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
  23248. __this_cpu_write(softnet_data.completion_queue, skb);
  23249. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  23250. local_irq_restore(flags);
  23251. + preempt_check_resched_rt();
  23252. }
  23253. EXPORT_SYMBOL(__dev_kfree_skb_irq);
  23254. @@ -3082,7 +3090,11 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
  23255. * This permits qdisc->running owner to get the lock more
  23256. * often and dequeue packets faster.
  23257. */
  23258. +#ifdef CONFIG_PREEMPT_RT_FULL
  23259. + contended = true;
  23260. +#else
  23261. contended = qdisc_is_running(q);
  23262. +#endif
  23263. if (unlikely(contended))
  23264. spin_lock(&q->busylock);
  23265. @@ -3145,8 +3157,10 @@ static void skb_update_prio(struct sk_buff *skb)
  23266. #define skb_update_prio(skb)
  23267. #endif
  23268. +#ifndef CONFIG_PREEMPT_RT_FULL
  23269. DEFINE_PER_CPU(int, xmit_recursion);
  23270. EXPORT_SYMBOL(xmit_recursion);
  23271. +#endif
  23272. /**
  23273. * dev_loopback_xmit - loop back @skb
  23274. @@ -3390,8 +3404,7 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  23275. int cpu = smp_processor_id(); /* ok because BHs are off */
  23276. if (txq->xmit_lock_owner != cpu) {
  23277. - if (unlikely(__this_cpu_read(xmit_recursion) >
  23278. - XMIT_RECURSION_LIMIT))
  23279. + if (unlikely(xmit_rec_read() > XMIT_RECURSION_LIMIT))
  23280. goto recursion_alert;
  23281. skb = validate_xmit_skb(skb, dev);
  23282. @@ -3401,9 +3414,9 @@ static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
  23283. HARD_TX_LOCK(dev, txq, cpu);
  23284. if (!netif_xmit_stopped(txq)) {
  23285. - __this_cpu_inc(xmit_recursion);
  23286. + xmit_rec_inc();
  23287. skb = dev_hard_start_xmit(skb, dev, txq, &rc);
  23288. - __this_cpu_dec(xmit_recursion);
  23289. + xmit_rec_dec();
  23290. if (dev_xmit_complete(rc)) {
  23291. HARD_TX_UNLOCK(dev, txq);
  23292. goto out;
  23293. @@ -3777,6 +3790,7 @@ static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
  23294. rps_unlock(sd);
  23295. local_irq_restore(flags);
  23296. + preempt_check_resched_rt();
  23297. atomic_long_inc(&skb->dev->rx_dropped);
  23298. kfree_skb(skb);
  23299. @@ -3795,7 +3809,7 @@ static int netif_rx_internal(struct sk_buff *skb)
  23300. struct rps_dev_flow voidflow, *rflow = &voidflow;
  23301. int cpu;
  23302. - preempt_disable();
  23303. + migrate_disable();
  23304. rcu_read_lock();
  23305. cpu = get_rps_cpu(skb->dev, skb, &rflow);
  23306. @@ -3805,13 +3819,13 @@ static int netif_rx_internal(struct sk_buff *skb)
  23307. ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
  23308. rcu_read_unlock();
  23309. - preempt_enable();
  23310. + migrate_enable();
  23311. } else
  23312. #endif
  23313. {
  23314. unsigned int qtail;
  23315. - ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
  23316. - put_cpu();
  23317. + ret = enqueue_to_backlog(skb, get_cpu_light(), &qtail);
  23318. + put_cpu_light();
  23319. }
  23320. return ret;
  23321. }
  23322. @@ -3845,11 +3859,9 @@ int netif_rx_ni(struct sk_buff *skb)
  23323. trace_netif_rx_ni_entry(skb);
  23324. - preempt_disable();
  23325. + local_bh_disable();
  23326. err = netif_rx_internal(skb);
  23327. - if (local_softirq_pending())
  23328. - do_softirq();
  23329. - preempt_enable();
  23330. + local_bh_enable();
  23331. return err;
  23332. }
  23333. @@ -4321,7 +4333,7 @@ static void flush_backlog(void *arg)
  23334. skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
  23335. if (skb->dev == dev) {
  23336. __skb_unlink(skb, &sd->input_pkt_queue);
  23337. - kfree_skb(skb);
  23338. + __skb_queue_tail(&sd->tofree_queue, skb);
  23339. input_queue_head_incr(sd);
  23340. }
  23341. }
  23342. @@ -4330,10 +4342,13 @@ static void flush_backlog(void *arg)
  23343. skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
  23344. if (skb->dev == dev) {
  23345. __skb_unlink(skb, &sd->process_queue);
  23346. - kfree_skb(skb);
  23347. + __skb_queue_tail(&sd->tofree_queue, skb);
  23348. input_queue_head_incr(sd);
  23349. }
  23350. }
  23351. +
  23352. + if (!skb_queue_empty(&sd->tofree_queue))
  23353. + raise_softirq_irqoff(NET_RX_SOFTIRQ);
  23354. }
  23355. static int napi_gro_complete(struct sk_buff *skb)
  23356. @@ -4795,6 +4810,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  23357. sd->rps_ipi_list = NULL;
  23358. local_irq_enable();
  23359. + preempt_check_resched_rt();
  23360. /* Send pending IPI's to kick RPS processing on remote cpus. */
  23361. while (remsd) {
  23362. @@ -4808,6 +4824,7 @@ static void net_rps_action_and_irq_enable(struct softnet_data *sd)
  23363. } else
  23364. #endif
  23365. local_irq_enable();
  23366. + preempt_check_resched_rt();
  23367. }
  23368. static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
  23369. @@ -4889,6 +4906,7 @@ void __napi_schedule(struct napi_struct *n)
  23370. local_irq_save(flags);
  23371. ____napi_schedule(this_cpu_ptr(&softnet_data), n);
  23372. local_irq_restore(flags);
  23373. + preempt_check_resched_rt();
  23374. }
  23375. EXPORT_SYMBOL(__napi_schedule);
  23376. @@ -5229,7 +5247,7 @@ static void net_rx_action(struct softirq_action *h)
  23377. list_splice_tail(&repoll, &list);
  23378. list_splice(&list, &sd->poll_list);
  23379. if (!list_empty(&sd->poll_list))
  23380. - __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  23381. + __raise_softirq_irqoff_ksoft(NET_RX_SOFTIRQ);
  23382. net_rps_action_and_irq_enable(sd);
  23383. }
  23384. @@ -7736,7 +7754,7 @@ EXPORT_SYMBOL(free_netdev);
  23385. void synchronize_net(void)
  23386. {
  23387. might_sleep();
  23388. - if (rtnl_is_locked())
  23389. + if (rtnl_is_locked() && !IS_ENABLED(CONFIG_PREEMPT_RT_FULL))
  23390. synchronize_rcu_expedited();
  23391. else
  23392. synchronize_rcu();
  23393. @@ -7977,16 +7995,20 @@ static int dev_cpu_callback(struct notifier_block *nfb,
  23394. raise_softirq_irqoff(NET_TX_SOFTIRQ);
  23395. local_irq_enable();
  23396. + preempt_check_resched_rt();
  23397. /* Process offline CPU's input_pkt_queue */
  23398. while ((skb = __skb_dequeue(&oldsd->process_queue))) {
  23399. netif_rx_ni(skb);
  23400. input_queue_head_incr(oldsd);
  23401. }
  23402. - while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
  23403. + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) {
  23404. netif_rx_ni(skb);
  23405. input_queue_head_incr(oldsd);
  23406. }
  23407. + while ((skb = __skb_dequeue(&oldsd->tofree_queue))) {
  23408. + kfree_skb(skb);
  23409. + }
  23410. return NOTIFY_OK;
  23411. }
  23412. @@ -8288,8 +8310,9 @@ static int __init net_dev_init(void)
  23413. for_each_possible_cpu(i) {
  23414. struct softnet_data *sd = &per_cpu(softnet_data, i);
  23415. - skb_queue_head_init(&sd->input_pkt_queue);
  23416. - skb_queue_head_init(&sd->process_queue);
  23417. + skb_queue_head_init_raw(&sd->input_pkt_queue);
  23418. + skb_queue_head_init_raw(&sd->process_queue);
  23419. + skb_queue_head_init_raw(&sd->tofree_queue);
  23420. INIT_LIST_HEAD(&sd->poll_list);
  23421. sd->output_queue_tailp = &sd->output_queue;
  23422. #ifdef CONFIG_RPS
  23423. diff --git a/net/core/filter.c b/net/core/filter.c
  23424. index cb06aceb512a..3585a8982287 100644
  23425. --- a/net/core/filter.c
  23426. +++ b/net/core/filter.c
  23427. @@ -1592,7 +1592,7 @@ static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
  23428. {
  23429. int ret;
  23430. - if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
  23431. + if (unlikely(xmit_rec_read() > XMIT_RECURSION_LIMIT)) {
  23432. net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
  23433. kfree_skb(skb);
  23434. return -ENETDOWN;
  23435. @@ -1600,9 +1600,9 @@ static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
  23436. skb->dev = dev;
  23437. - __this_cpu_inc(xmit_recursion);
  23438. + xmit_rec_inc();
  23439. ret = dev_queue_xmit(skb);
  23440. - __this_cpu_dec(xmit_recursion);
  23441. + xmit_rec_dec();
  23442. return ret;
  23443. }
  23444. diff --git a/net/core/gen_estimator.c b/net/core/gen_estimator.c
  23445. index cad8e791f28e..2a9364fe62a5 100644
  23446. --- a/net/core/gen_estimator.c
  23447. +++ b/net/core/gen_estimator.c
  23448. @@ -84,7 +84,7 @@ struct gen_estimator
  23449. struct gnet_stats_basic_packed *bstats;
  23450. struct gnet_stats_rate_est64 *rate_est;
  23451. spinlock_t *stats_lock;
  23452. - seqcount_t *running;
  23453. + net_seqlock_t *running;
  23454. int ewma_log;
  23455. u32 last_packets;
  23456. unsigned long avpps;
  23457. @@ -213,7 +213,7 @@ int gen_new_estimator(struct gnet_stats_basic_packed *bstats,
  23458. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  23459. struct gnet_stats_rate_est64 *rate_est,
  23460. spinlock_t *stats_lock,
  23461. - seqcount_t *running,
  23462. + net_seqlock_t *running,
  23463. struct nlattr *opt)
  23464. {
  23465. struct gen_estimator *est;
  23466. @@ -309,7 +309,7 @@ int gen_replace_estimator(struct gnet_stats_basic_packed *bstats,
  23467. struct gnet_stats_basic_cpu __percpu *cpu_bstats,
  23468. struct gnet_stats_rate_est64 *rate_est,
  23469. spinlock_t *stats_lock,
  23470. - seqcount_t *running, struct nlattr *opt)
  23471. + net_seqlock_t *running, struct nlattr *opt)
  23472. {
  23473. gen_kill_estimator(bstats, rate_est);
  23474. return gen_new_estimator(bstats, cpu_bstats, rate_est, stats_lock, running, opt);
  23475. diff --git a/net/core/gen_stats.c b/net/core/gen_stats.c
  23476. index 508e051304fb..bc3b17b78c94 100644
  23477. --- a/net/core/gen_stats.c
  23478. +++ b/net/core/gen_stats.c
  23479. @@ -130,7 +130,7 @@ __gnet_stats_copy_basic_cpu(struct gnet_stats_basic_packed *bstats,
  23480. }
  23481. void
  23482. -__gnet_stats_copy_basic(const seqcount_t *running,
  23483. +__gnet_stats_copy_basic(net_seqlock_t *running,
  23484. struct gnet_stats_basic_packed *bstats,
  23485. struct gnet_stats_basic_cpu __percpu *cpu,
  23486. struct gnet_stats_basic_packed *b)
  23487. @@ -143,10 +143,10 @@ __gnet_stats_copy_basic(const seqcount_t *running,
  23488. }
  23489. do {
  23490. if (running)
  23491. - seq = read_seqcount_begin(running);
  23492. + seq = net_seq_begin(running);
  23493. bstats->bytes = b->bytes;
  23494. bstats->packets = b->packets;
  23495. - } while (running && read_seqcount_retry(running, seq));
  23496. + } while (running && net_seq_retry(running, seq));
  23497. }
  23498. EXPORT_SYMBOL(__gnet_stats_copy_basic);
  23499. @@ -164,7 +164,7 @@ EXPORT_SYMBOL(__gnet_stats_copy_basic);
  23500. * if the room in the socket buffer was not sufficient.
  23501. */
  23502. int
  23503. -gnet_stats_copy_basic(const seqcount_t *running,
  23504. +gnet_stats_copy_basic(net_seqlock_t *running,
  23505. struct gnet_dump *d,
  23506. struct gnet_stats_basic_cpu __percpu *cpu,
  23507. struct gnet_stats_basic_packed *b)
  23508. diff --git a/net/core/skbuff.c b/net/core/skbuff.c
  23509. index 3864b4b68fa1..55c73ade9faa 100644
  23510. --- a/net/core/skbuff.c
  23511. +++ b/net/core/skbuff.c
  23512. @@ -64,6 +64,7 @@
  23513. #include <linux/errqueue.h>
  23514. #include <linux/prefetch.h>
  23515. #include <linux/if_vlan.h>
  23516. +#include <linux/locallock.h>
  23517. #include <net/protocol.h>
  23518. #include <net/dst.h>
  23519. @@ -360,6 +361,8 @@ struct napi_alloc_cache {
  23520. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  23521. static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
  23522. +static DEFINE_LOCAL_IRQ_LOCK(netdev_alloc_lock);
  23523. +static DEFINE_LOCAL_IRQ_LOCK(napi_alloc_cache_lock);
  23524. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  23525. {
  23526. @@ -367,10 +370,10 @@ static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  23527. unsigned long flags;
  23528. void *data;
  23529. - local_irq_save(flags);
  23530. + local_lock_irqsave(netdev_alloc_lock, flags);
  23531. nc = this_cpu_ptr(&netdev_alloc_cache);
  23532. data = __alloc_page_frag(nc, fragsz, gfp_mask);
  23533. - local_irq_restore(flags);
  23534. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  23535. return data;
  23536. }
  23537. @@ -389,9 +392,13 @@ EXPORT_SYMBOL(netdev_alloc_frag);
  23538. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  23539. {
  23540. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  23541. + struct napi_alloc_cache *nc;
  23542. + void *data;
  23543. - return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
  23544. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23545. + data = __alloc_page_frag(&nc->page, fragsz, gfp_mask);
  23546. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23547. + return data;
  23548. }
  23549. void *napi_alloc_frag(unsigned int fragsz)
  23550. @@ -438,13 +445,13 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  23551. if (sk_memalloc_socks())
  23552. gfp_mask |= __GFP_MEMALLOC;
  23553. - local_irq_save(flags);
  23554. + local_lock_irqsave(netdev_alloc_lock, flags);
  23555. nc = this_cpu_ptr(&netdev_alloc_cache);
  23556. data = __alloc_page_frag(nc, len, gfp_mask);
  23557. pfmemalloc = nc->pfmemalloc;
  23558. - local_irq_restore(flags);
  23559. + local_unlock_irqrestore(netdev_alloc_lock, flags);
  23560. if (unlikely(!data))
  23561. return NULL;
  23562. @@ -485,9 +492,10 @@ EXPORT_SYMBOL(__netdev_alloc_skb);
  23563. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  23564. gfp_t gfp_mask)
  23565. {
  23566. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  23567. + struct napi_alloc_cache *nc;
  23568. struct sk_buff *skb;
  23569. void *data;
  23570. + bool pfmemalloc;
  23571. len += NET_SKB_PAD + NET_IP_ALIGN;
  23572. @@ -505,7 +513,10 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  23573. if (sk_memalloc_socks())
  23574. gfp_mask |= __GFP_MEMALLOC;
  23575. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23576. data = __alloc_page_frag(&nc->page, len, gfp_mask);
  23577. + pfmemalloc = nc->page.pfmemalloc;
  23578. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23579. if (unlikely(!data))
  23580. return NULL;
  23581. @@ -516,7 +527,7 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  23582. }
  23583. /* use OR instead of assignment to avoid clearing of bits in mask */
  23584. - if (nc->page.pfmemalloc)
  23585. + if (pfmemalloc)
  23586. skb->pfmemalloc = 1;
  23587. skb->head_frag = 1;
  23588. @@ -760,23 +771,26 @@ EXPORT_SYMBOL(consume_skb);
  23589. void __kfree_skb_flush(void)
  23590. {
  23591. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  23592. + struct napi_alloc_cache *nc;
  23593. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23594. /* flush skb_cache if containing objects */
  23595. if (nc->skb_count) {
  23596. kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
  23597. nc->skb_cache);
  23598. nc->skb_count = 0;
  23599. }
  23600. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23601. }
  23602. static inline void _kfree_skb_defer(struct sk_buff *skb)
  23603. {
  23604. - struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  23605. + struct napi_alloc_cache *nc;
  23606. /* drop skb->head and call any destructors for packet */
  23607. skb_release_all(skb);
  23608. + nc = &get_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23609. /* record skb to CPU local list */
  23610. nc->skb_cache[nc->skb_count++] = skb;
  23611. @@ -791,6 +805,7 @@ static inline void _kfree_skb_defer(struct sk_buff *skb)
  23612. nc->skb_cache);
  23613. nc->skb_count = 0;
  23614. }
  23615. + put_locked_var(napi_alloc_cache_lock, napi_alloc_cache);
  23616. }
  23617. void __kfree_skb_defer(struct sk_buff *skb)
  23618. {
  23619. diff --git a/net/core/sock.c b/net/core/sock.c
  23620. index fd7b41edf1ce..e425d259a9f0 100644
  23621. --- a/net/core/sock.c
  23622. +++ b/net/core/sock.c
  23623. @@ -2508,12 +2508,11 @@ void lock_sock_nested(struct sock *sk, int subclass)
  23624. if (sk->sk_lock.owned)
  23625. __lock_sock(sk);
  23626. sk->sk_lock.owned = 1;
  23627. - spin_unlock(&sk->sk_lock.slock);
  23628. + spin_unlock_bh(&sk->sk_lock.slock);
  23629. /*
  23630. * The sk_lock has mutex_lock() semantics here:
  23631. */
  23632. mutex_acquire(&sk->sk_lock.dep_map, subclass, 0, _RET_IP_);
  23633. - local_bh_enable();
  23634. }
  23635. EXPORT_SYMBOL(lock_sock_nested);
  23636. diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
  23637. index 38abe70e595f..443259a04862 100644
  23638. --- a/net/ipv4/icmp.c
  23639. +++ b/net/ipv4/icmp.c
  23640. @@ -69,6 +69,7 @@
  23641. #include <linux/jiffies.h>
  23642. #include <linux/kernel.h>
  23643. #include <linux/fcntl.h>
  23644. +#include <linux/sysrq.h>
  23645. #include <linux/socket.h>
  23646. #include <linux/in.h>
  23647. #include <linux/inet.h>
  23648. @@ -77,6 +78,7 @@
  23649. #include <linux/string.h>
  23650. #include <linux/netfilter_ipv4.h>
  23651. #include <linux/slab.h>
  23652. +#include <linux/locallock.h>
  23653. #include <net/snmp.h>
  23654. #include <net/ip.h>
  23655. #include <net/route.h>
  23656. @@ -204,6 +206,8 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
  23657. *
  23658. * On SMP we have one ICMP socket per-cpu.
  23659. */
  23660. +static DEFINE_LOCAL_IRQ_LOCK(icmp_sk_lock);
  23661. +
  23662. static struct sock *icmp_sk(struct net *net)
  23663. {
  23664. return *this_cpu_ptr(net->ipv4.icmp_sk);
  23665. @@ -215,12 +219,14 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  23666. local_bh_disable();
  23667. + local_lock(icmp_sk_lock);
  23668. sk = icmp_sk(net);
  23669. if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
  23670. /* This can happen if the output path signals a
  23671. * dst_link_failure() for an outgoing ICMP packet.
  23672. */
  23673. + local_unlock(icmp_sk_lock);
  23674. local_bh_enable();
  23675. return NULL;
  23676. }
  23677. @@ -230,6 +236,7 @@ static inline struct sock *icmp_xmit_lock(struct net *net)
  23678. static inline void icmp_xmit_unlock(struct sock *sk)
  23679. {
  23680. spin_unlock_bh(&sk->sk_lock.slock);
  23681. + local_unlock(icmp_sk_lock);
  23682. }
  23683. int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
  23684. @@ -358,6 +365,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  23685. struct sock *sk;
  23686. struct sk_buff *skb;
  23687. + local_lock(icmp_sk_lock);
  23688. sk = icmp_sk(dev_net((*rt)->dst.dev));
  23689. if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
  23690. icmp_param->data_len+icmp_param->head_len,
  23691. @@ -380,6 +388,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,
  23692. skb->ip_summed = CHECKSUM_NONE;
  23693. ip_push_pending_frames(sk, fl4);
  23694. }
  23695. + local_unlock(icmp_sk_lock);
  23696. }
  23697. /*
  23698. @@ -891,6 +900,30 @@ static bool icmp_redirect(struct sk_buff *skb)
  23699. }
  23700. /*
  23701. + * 32bit and 64bit have different timestamp length, so we check for
  23702. + * the cookie at offset 20 and verify it is repeated at offset 50
  23703. + */
  23704. +#define CO_POS0 20
  23705. +#define CO_POS1 50
  23706. +#define CO_SIZE sizeof(int)
  23707. +#define ICMP_SYSRQ_SIZE 57
  23708. +
  23709. +/*
  23710. + * We got a ICMP_SYSRQ_SIZE sized ping request. Check for the cookie
  23711. + * pattern and if it matches send the next byte as a trigger to sysrq.
  23712. + */
  23713. +static void icmp_check_sysrq(struct net *net, struct sk_buff *skb)
  23714. +{
  23715. + int cookie = htonl(net->ipv4.sysctl_icmp_echo_sysrq);
  23716. + char *p = skb->data;
  23717. +
  23718. + if (!memcmp(&cookie, p + CO_POS0, CO_SIZE) &&
  23719. + !memcmp(&cookie, p + CO_POS1, CO_SIZE) &&
  23720. + p[CO_POS0 + CO_SIZE] == p[CO_POS1 + CO_SIZE])
  23721. + handle_sysrq(p[CO_POS0 + CO_SIZE]);
  23722. +}
  23723. +
  23724. +/*
  23725. * Handle ICMP_ECHO ("ping") requests.
  23726. *
  23727. * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
  23728. @@ -917,6 +950,11 @@ static bool icmp_echo(struct sk_buff *skb)
  23729. icmp_param.data_len = skb->len;
  23730. icmp_param.head_len = sizeof(struct icmphdr);
  23731. icmp_reply(&icmp_param, skb);
  23732. +
  23733. + if (skb->len == ICMP_SYSRQ_SIZE &&
  23734. + net->ipv4.sysctl_icmp_echo_sysrq) {
  23735. + icmp_check_sysrq(net, skb);
  23736. + }
  23737. }
  23738. /* should there be an ICMP stat for ignored echos? */
  23739. return true;
  23740. diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
  23741. index 1cb67de106fe..332a485323f0 100644
  23742. --- a/net/ipv4/sysctl_net_ipv4.c
  23743. +++ b/net/ipv4/sysctl_net_ipv4.c
  23744. @@ -681,6 +681,13 @@ static struct ctl_table ipv4_net_table[] = {
  23745. .proc_handler = proc_dointvec
  23746. },
  23747. {
  23748. + .procname = "icmp_echo_sysrq",
  23749. + .data = &init_net.ipv4.sysctl_icmp_echo_sysrq,
  23750. + .maxlen = sizeof(int),
  23751. + .mode = 0644,
  23752. + .proc_handler = proc_dointvec
  23753. + },
  23754. + {
  23755. .procname = "icmp_ignore_bogus_error_responses",
  23756. .data = &init_net.ipv4.sysctl_icmp_ignore_bogus_error_responses,
  23757. .maxlen = sizeof(int),
  23758. diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
  23759. index 7158d4f8dae4..0dc007fc6704 100644
  23760. --- a/net/ipv4/tcp_ipv4.c
  23761. +++ b/net/ipv4/tcp_ipv4.c
  23762. @@ -62,6 +62,7 @@
  23763. #include <linux/init.h>
  23764. #include <linux/times.h>
  23765. #include <linux/slab.h>
  23766. +#include <linux/locallock.h>
  23767. #include <net/net_namespace.h>
  23768. #include <net/icmp.h>
  23769. @@ -565,6 +566,7 @@ void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  23770. }
  23771. EXPORT_SYMBOL(tcp_v4_send_check);
  23772. +static DEFINE_LOCAL_IRQ_LOCK(tcp_sk_lock);
  23773. /*
  23774. * This routine will send an RST to the other tcp.
  23775. *
  23776. @@ -692,6 +694,8 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
  23777. offsetof(struct inet_timewait_sock, tw_bound_dev_if));
  23778. arg.tos = ip_hdr(skb)->tos;
  23779. +
  23780. + local_lock(tcp_sk_lock);
  23781. local_bh_disable();
  23782. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  23783. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  23784. @@ -701,6 +705,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
  23785. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  23786. __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
  23787. local_bh_enable();
  23788. + local_unlock(tcp_sk_lock);
  23789. #ifdef CONFIG_TCP_MD5SIG
  23790. out:
  23791. @@ -776,6 +781,7 @@ static void tcp_v4_send_ack(struct net *net,
  23792. if (oif)
  23793. arg.bound_dev_if = oif;
  23794. arg.tos = tos;
  23795. + local_lock(tcp_sk_lock);
  23796. local_bh_disable();
  23797. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  23798. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  23799. @@ -784,6 +790,7 @@ static void tcp_v4_send_ack(struct net *net,
  23800. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  23801. local_bh_enable();
  23802. + local_unlock(tcp_sk_lock);
  23803. }
  23804. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  23805. diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
  23806. index 9dce3b157908..525efa5309ac 100644
  23807. --- a/net/mac80211/rx.c
  23808. +++ b/net/mac80211/rx.c
  23809. @@ -4064,7 +4064,7 @@ void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
  23810. struct ieee80211_supported_band *sband;
  23811. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  23812. - WARN_ON_ONCE(softirq_count() == 0);
  23813. + WARN_ON_ONCE_NONRT(softirq_count() == 0);
  23814. if (WARN_ON(status->band >= NUM_NL80211_BANDS))
  23815. goto drop;
  23816. diff --git a/net/netfilter/core.c b/net/netfilter/core.c
  23817. index f39276d1c2d7..10880c89d62f 100644
  23818. --- a/net/netfilter/core.c
  23819. +++ b/net/netfilter/core.c
  23820. @@ -22,11 +22,17 @@
  23821. #include <linux/proc_fs.h>
  23822. #include <linux/mutex.h>
  23823. #include <linux/slab.h>
  23824. +#include <linux/locallock.h>
  23825. #include <net/net_namespace.h>
  23826. #include <net/sock.h>
  23827. #include "nf_internals.h"
  23828. +#ifdef CONFIG_PREEMPT_RT_BASE
  23829. +DEFINE_LOCAL_IRQ_LOCK(xt_write_lock);
  23830. +EXPORT_PER_CPU_SYMBOL(xt_write_lock);
  23831. +#endif
  23832. +
  23833. static DEFINE_MUTEX(afinfo_mutex);
  23834. const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
  23835. diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
  23836. index 33a4697d5539..475cb74bf825 100644
  23837. --- a/net/packet/af_packet.c
  23838. +++ b/net/packet/af_packet.c
  23839. @@ -63,6 +63,7 @@
  23840. #include <linux/if_packet.h>
  23841. #include <linux/wireless.h>
  23842. #include <linux/kernel.h>
  23843. +#include <linux/delay.h>
  23844. #include <linux/kmod.h>
  23845. #include <linux/slab.h>
  23846. #include <linux/vmalloc.h>
  23847. @@ -695,7 +696,7 @@ static void prb_retire_rx_blk_timer_expired(unsigned long data)
  23848. if (BLOCK_NUM_PKTS(pbd)) {
  23849. while (atomic_read(&pkc->blk_fill_in_prog)) {
  23850. /* Waiting for skb_copy_bits to finish... */
  23851. - cpu_relax();
  23852. + cpu_chill();
  23853. }
  23854. }
  23855. @@ -957,7 +958,7 @@ static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  23856. if (!(status & TP_STATUS_BLK_TMO)) {
  23857. while (atomic_read(&pkc->blk_fill_in_prog)) {
  23858. /* Waiting for skb_copy_bits to finish... */
  23859. - cpu_relax();
  23860. + cpu_chill();
  23861. }
  23862. }
  23863. prb_close_block(pkc, pbd, po, status);
  23864. diff --git a/net/rds/ib_rdma.c b/net/rds/ib_rdma.c
  23865. index 977f69886c00..f3e7a36b0396 100644
  23866. --- a/net/rds/ib_rdma.c
  23867. +++ b/net/rds/ib_rdma.c
  23868. @@ -34,6 +34,7 @@
  23869. #include <linux/slab.h>
  23870. #include <linux/rculist.h>
  23871. #include <linux/llist.h>
  23872. +#include <linux/delay.h>
  23873. #include "rds_single_path.h"
  23874. #include "ib_mr.h"
  23875. @@ -210,7 +211,7 @@ static inline void wait_clean_list_grace(void)
  23876. for_each_online_cpu(cpu) {
  23877. flag = &per_cpu(clean_list_grace, cpu);
  23878. while (test_bit(CLEAN_LIST_BUSY_BIT, flag))
  23879. - cpu_relax();
  23880. + cpu_chill();
  23881. }
  23882. }
  23883. diff --git a/net/rxrpc/security.c b/net/rxrpc/security.c
  23884. index 814d285ff802..d4d088e9be85 100644
  23885. --- a/net/rxrpc/security.c
  23886. +++ b/net/rxrpc/security.c
  23887. @@ -19,9 +19,6 @@
  23888. #include <keys/rxrpc-type.h>
  23889. #include "ar-internal.h"
  23890. -static LIST_HEAD(rxrpc_security_methods);
  23891. -static DECLARE_RWSEM(rxrpc_security_sem);
  23892. -
  23893. static const struct rxrpc_security *rxrpc_security_types[] = {
  23894. [RXRPC_SECURITY_NONE] = &rxrpc_no_security,
  23895. #ifdef CONFIG_RXKAD
  23896. diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
  23897. index 12ebde845523..99f3ce50c6c4 100644
  23898. --- a/net/sched/sch_api.c
  23899. +++ b/net/sched/sch_api.c
  23900. @@ -975,7 +975,7 @@ qdisc_create(struct net_device *dev, struct netdev_queue *dev_queue,
  23901. rcu_assign_pointer(sch->stab, stab);
  23902. }
  23903. if (tca[TCA_RATE]) {
  23904. - seqcount_t *running;
  23905. + net_seqlock_t *running;
  23906. err = -EOPNOTSUPP;
  23907. if (sch->flags & TCQ_F_MQROOT)
  23908. diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
  23909. index 657c13362b19..cbab8d4d5864 100644
  23910. --- a/net/sched/sch_generic.c
  23911. +++ b/net/sched/sch_generic.c
  23912. @@ -426,7 +426,11 @@ struct Qdisc noop_qdisc = {
  23913. .list = LIST_HEAD_INIT(noop_qdisc.list),
  23914. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  23915. .dev_queue = &noop_netdev_queue,
  23916. +#ifdef CONFIG_PREEMPT_RT_BASE
  23917. + .running = __SEQLOCK_UNLOCKED(noop_qdisc.running),
  23918. +#else
  23919. .running = SEQCNT_ZERO(noop_qdisc.running),
  23920. +#endif
  23921. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  23922. };
  23923. EXPORT_SYMBOL(noop_qdisc);
  23924. @@ -620,9 +624,17 @@ struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  23925. lockdep_set_class(&sch->busylock,
  23926. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  23927. +#ifdef CONFIG_PREEMPT_RT_BASE
  23928. + seqlock_init(&sch->running);
  23929. + lockdep_set_class(&sch->running.seqcount,
  23930. + dev->qdisc_running_key ?: &qdisc_running_key);
  23931. + lockdep_set_class(&sch->running.lock,
  23932. + dev->qdisc_running_key ?: &qdisc_running_key);
  23933. +#else
  23934. seqcount_init(&sch->running);
  23935. lockdep_set_class(&sch->running,
  23936. dev->qdisc_running_key ?: &qdisc_running_key);
  23937. +#endif
  23938. sch->ops = ops;
  23939. sch->enqueue = ops->enqueue;
  23940. @@ -917,7 +929,7 @@ void dev_deactivate_many(struct list_head *head)
  23941. /* Wait for outstanding qdisc_run calls. */
  23942. list_for_each_entry(dev, head, close_list)
  23943. while (some_qdisc_is_busy(dev))
  23944. - yield();
  23945. + msleep(1);
  23946. }
  23947. void dev_deactivate(struct net_device *dev)
  23948. diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
  23949. index c3f652395a80..2dd84493528e 100644
  23950. --- a/net/sunrpc/svc_xprt.c
  23951. +++ b/net/sunrpc/svc_xprt.c
  23952. @@ -396,7 +396,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  23953. goto out;
  23954. }
  23955. - cpu = get_cpu();
  23956. + cpu = get_cpu_light();
  23957. pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
  23958. atomic_long_inc(&pool->sp_stats.packets);
  23959. @@ -432,7 +432,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  23960. atomic_long_inc(&pool->sp_stats.threads_woken);
  23961. wake_up_process(rqstp->rq_task);
  23962. - put_cpu();
  23963. + put_cpu_light();
  23964. goto out;
  23965. }
  23966. rcu_read_unlock();
  23967. @@ -453,7 +453,7 @@ void svc_xprt_do_enqueue(struct svc_xprt *xprt)
  23968. goto redo_search;
  23969. }
  23970. rqstp = NULL;
  23971. - put_cpu();
  23972. + put_cpu_light();
  23973. out:
  23974. trace_svc_xprt_do_enqueue(xprt, rqstp);
  23975. }
  23976. diff --git a/scripts/mkcompile_h b/scripts/mkcompile_h
  23977. index 6fdc97ef6023..523e0420d7f0 100755
  23978. --- a/scripts/mkcompile_h
  23979. +++ b/scripts/mkcompile_h
  23980. @@ -4,7 +4,8 @@ TARGET=$1
  23981. ARCH=$2
  23982. SMP=$3
  23983. PREEMPT=$4
  23984. -CC=$5
  23985. +RT=$5
  23986. +CC=$6
  23987. vecho() { [ "${quiet}" = "silent_" ] || echo "$@" ; }
  23988. @@ -57,6 +58,7 @@ UTS_VERSION="#$VERSION"
  23989. CONFIG_FLAGS=""
  23990. if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
  23991. if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
  23992. +if [ -n "$RT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS RT"; fi
  23993. UTS_VERSION="$UTS_VERSION $CONFIG_FLAGS $TIMESTAMP"
  23994. # Truncate to maximum length
  23995. diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
  23996. index c61fd50f771f..1583de410f62 100644
  23997. --- a/sound/core/pcm_native.c
  23998. +++ b/sound/core/pcm_native.c
  23999. @@ -135,7 +135,7 @@ EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
  24000. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  24001. {
  24002. if (!substream->pcm->nonatomic)
  24003. - local_irq_disable();
  24004. + local_irq_disable_nort();
  24005. snd_pcm_stream_lock(substream);
  24006. }
  24007. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  24008. @@ -150,7 +150,7 @@ void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
  24009. {
  24010. snd_pcm_stream_unlock(substream);
  24011. if (!substream->pcm->nonatomic)
  24012. - local_irq_enable();
  24013. + local_irq_enable_nort();
  24014. }
  24015. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  24016. @@ -158,7 +158,7 @@ unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
  24017. {
  24018. unsigned long flags = 0;
  24019. if (!substream->pcm->nonatomic)
  24020. - local_irq_save(flags);
  24021. + local_irq_save_nort(flags);
  24022. snd_pcm_stream_lock(substream);
  24023. return flags;
  24024. }
  24025. @@ -176,7 +176,7 @@ void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  24026. {
  24027. snd_pcm_stream_unlock(substream);
  24028. if (!substream->pcm->nonatomic)
  24029. - local_irq_restore(flags);
  24030. + local_irq_restore_nort(flags);
  24031. }
  24032. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);