maple_tree.c 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Maple Tree implementation
  4. * Copyright (c) 2018-2022 Oracle Corporation
  5. * Authors: Liam R. Howlett <Liam.Howlett@oracle.com>
  6. * Matthew Wilcox <willy@infradead.org>
  7. * Copyright (c) 2023 ByteDance
  8. * Author: Peng Zhang <zhangpeng.00@bytedance.com>
  9. */
  10. /*
  11. * DOC: Interesting implementation details of the Maple Tree
  12. *
  13. * Each node type has a number of slots for entries and a number of slots for
  14. * pivots. In the case of dense nodes, the pivots are implied by the position
  15. * and are simply the slot index + the minimum of the node.
  16. *
  17. * In regular B-Tree terms, pivots are called keys. The term pivot is used to
  18. * indicate that the tree is specifying ranges. Pivots may appear in the
  19. * subtree with an entry attached to the value whereas keys are unique to a
  20. * specific position of a B-tree. Pivot values are inclusive of the slot with
  21. * the same index.
  22. *
  23. *
  24. * The following illustrates the layout of a range64 nodes slots and pivots.
  25. *
  26. *
  27. * Slots -> | 0 | 1 | 2 | ... | 12 | 13 | 14 | 15 |
  28. * ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬
  29. * │ │ │ │ │ │ │ │ └─ Implied maximum
  30. * │ │ │ │ │ │ │ └─ Pivot 14
  31. * │ │ │ │ │ │ └─ Pivot 13
  32. * │ │ │ │ │ └─ Pivot 12
  33. * │ │ │ │ └─ Pivot 11
  34. * │ │ │ └─ Pivot 2
  35. * │ │ └─ Pivot 1
  36. * │ └─ Pivot 0
  37. * └─ Implied minimum
  38. *
  39. * Slot contents:
  40. * Internal (non-leaf) nodes contain pointers to other nodes.
  41. * Leaf nodes contain entries.
  42. *
  43. * The location of interest is often referred to as an offset. All offsets have
  44. * a slot, but the last offset has an implied pivot from the node above (or
  45. * UINT_MAX for the root node.
  46. *
  47. * Ranges complicate certain write activities. When modifying any of
  48. * the B-tree variants, it is known that one entry will either be added or
  49. * deleted. When modifying the Maple Tree, one store operation may overwrite
  50. * the entire data set, or one half of the tree, or the middle half of the tree.
  51. *
  52. */
  53. #include <linux/maple_tree.h>
  54. #include <linux/xarray.h>
  55. #include <linux/types.h>
  56. #include <linux/export.h>
  57. #include <linux/slab.h>
  58. #include <linux/limits.h>
  59. #include <asm/barrier.h>
  60. #define CREATE_TRACE_POINTS
  61. #include <trace/events/maple_tree.h>
  62. #define TP_FCT tracepoint_string(__func__)
  63. /*
  64. * Kernel pointer hashing renders much of the maple tree dump useless as tagged
  65. * pointers get hashed to arbitrary values.
  66. *
  67. * If CONFIG_DEBUG_VM_MAPLE_TREE is set we are in a debug mode where it is
  68. * permissible to bypass this. Otherwise remain cautious and retain the hashing.
  69. *
  70. * Userland doesn't know about %px so also use %p there.
  71. */
  72. #if defined(__KERNEL__) && defined(CONFIG_DEBUG_VM_MAPLE_TREE)
  73. #define PTR_FMT "%px"
  74. #else
  75. #define PTR_FMT "%p"
  76. #endif
  77. #define MA_ROOT_PARENT 1
  78. /*
  79. * Maple state flags
  80. * * MA_STATE_PREALLOC - Preallocated nodes, WARN_ON allocation
  81. */
  82. #define MA_STATE_PREALLOC 1
  83. #define ma_parent_ptr(x) ((struct maple_pnode *)(x))
  84. #define mas_tree_parent(x) ((unsigned long)(x->tree) | MA_ROOT_PARENT)
  85. #define ma_mnode_ptr(x) ((struct maple_node *)(x))
  86. #define ma_enode_ptr(x) ((struct maple_enode *)(x))
  87. static struct kmem_cache *maple_node_cache;
  88. #ifdef CONFIG_DEBUG_MAPLE_TREE
  89. static const unsigned long mt_max[] = {
  90. [maple_dense] = MAPLE_NODE_SLOTS,
  91. [maple_leaf_64] = ULONG_MAX,
  92. [maple_range_64] = ULONG_MAX,
  93. [maple_arange_64] = ULONG_MAX,
  94. };
  95. #define mt_node_max(x) mt_max[mte_node_type(x)]
  96. #endif
  97. static const unsigned char mt_slots[] = {
  98. [maple_dense] = MAPLE_NODE_SLOTS,
  99. [maple_leaf_64] = MAPLE_RANGE64_SLOTS,
  100. [maple_range_64] = MAPLE_RANGE64_SLOTS,
  101. [maple_arange_64] = MAPLE_ARANGE64_SLOTS,
  102. };
  103. #define mt_slot_count(x) mt_slots[mte_node_type(x)]
  104. static const unsigned char mt_pivots[] = {
  105. [maple_dense] = 0,
  106. [maple_leaf_64] = MAPLE_RANGE64_SLOTS - 1,
  107. [maple_range_64] = MAPLE_RANGE64_SLOTS - 1,
  108. [maple_arange_64] = MAPLE_ARANGE64_SLOTS - 1,
  109. };
  110. #define mt_pivot_count(x) mt_pivots[mte_node_type(x)]
  111. static const unsigned char mt_min_slots[] = {
  112. [maple_dense] = MAPLE_NODE_SLOTS / 2,
  113. [maple_leaf_64] = (MAPLE_RANGE64_SLOTS / 2) - 2,
  114. [maple_range_64] = (MAPLE_RANGE64_SLOTS / 2) - 2,
  115. [maple_arange_64] = (MAPLE_ARANGE64_SLOTS / 2) - 1,
  116. };
  117. #define mt_min_slot_count(x) mt_min_slots[mte_node_type(x)]
  118. #define MAPLE_BIG_NODE_SLOTS (MAPLE_RANGE64_SLOTS * 2 + 2)
  119. #define MAPLE_BIG_NODE_GAPS (MAPLE_ARANGE64_SLOTS * 2 + 1)
  120. struct maple_big_node {
  121. unsigned long pivot[MAPLE_BIG_NODE_SLOTS - 1];
  122. union {
  123. struct maple_enode *slot[MAPLE_BIG_NODE_SLOTS];
  124. struct {
  125. unsigned long padding[MAPLE_BIG_NODE_GAPS];
  126. unsigned long gap[MAPLE_BIG_NODE_GAPS];
  127. };
  128. };
  129. unsigned char b_end;
  130. enum maple_type type;
  131. };
  132. /*
  133. * The maple_subtree_state is used to build a tree to replace a segment of an
  134. * existing tree in a more atomic way. Any walkers of the older tree will hit a
  135. * dead node and restart on updates.
  136. */
  137. struct maple_subtree_state {
  138. struct ma_state *orig_l; /* Original left side of subtree */
  139. struct ma_state *orig_r; /* Original right side of subtree */
  140. struct ma_state *l; /* New left side of subtree */
  141. struct ma_state *m; /* New middle of subtree (rare) */
  142. struct ma_state *r; /* New right side of subtree */
  143. struct ma_topiary *free; /* nodes to be freed */
  144. struct ma_topiary *destroy; /* Nodes to be destroyed (walked and freed) */
  145. struct maple_big_node *bn;
  146. };
  147. #ifdef CONFIG_KASAN_STACK
  148. /* Prevent mas_wr_bnode() from exceeding the stack frame limit */
  149. #define noinline_for_kasan noinline_for_stack
  150. #else
  151. #define noinline_for_kasan inline
  152. #endif
  153. /* Functions */
  154. static inline struct maple_node *mt_alloc_one(gfp_t gfp)
  155. {
  156. return kmem_cache_alloc(maple_node_cache, gfp);
  157. }
  158. static inline void mt_free_bulk(size_t size, void __rcu **nodes)
  159. {
  160. kmem_cache_free_bulk(maple_node_cache, size, (void **)nodes);
  161. }
  162. static void mt_return_sheaf(struct slab_sheaf *sheaf)
  163. {
  164. kmem_cache_return_sheaf(maple_node_cache, GFP_NOWAIT, sheaf);
  165. }
  166. static struct slab_sheaf *mt_get_sheaf(gfp_t gfp, int count)
  167. {
  168. return kmem_cache_prefill_sheaf(maple_node_cache, gfp, count);
  169. }
  170. static int mt_refill_sheaf(gfp_t gfp, struct slab_sheaf **sheaf,
  171. unsigned int size)
  172. {
  173. return kmem_cache_refill_sheaf(maple_node_cache, gfp, sheaf, size);
  174. }
  175. /*
  176. * ma_free_rcu() - Use rcu callback to free a maple node
  177. * @node: The node to free
  178. *
  179. * The maple tree uses the parent pointer to indicate this node is no longer in
  180. * use and will be freed.
  181. */
  182. static void ma_free_rcu(struct maple_node *node)
  183. {
  184. WARN_ON(node->parent != ma_parent_ptr(node));
  185. kfree_rcu(node, rcu);
  186. }
  187. static void mt_set_height(struct maple_tree *mt, unsigned char height)
  188. {
  189. unsigned int new_flags = mt->ma_flags;
  190. new_flags &= ~MT_FLAGS_HEIGHT_MASK;
  191. MT_BUG_ON(mt, height > MAPLE_HEIGHT_MAX);
  192. new_flags |= height << MT_FLAGS_HEIGHT_OFFSET;
  193. mt->ma_flags = new_flags;
  194. }
  195. static unsigned int mas_mt_height(struct ma_state *mas)
  196. {
  197. return mt_height(mas->tree);
  198. }
  199. static inline unsigned int mt_attr(struct maple_tree *mt)
  200. {
  201. return mt->ma_flags & ~MT_FLAGS_HEIGHT_MASK;
  202. }
  203. static __always_inline enum maple_type mte_node_type(
  204. const struct maple_enode *entry)
  205. {
  206. return ((unsigned long)entry >> MAPLE_NODE_TYPE_SHIFT) &
  207. MAPLE_NODE_TYPE_MASK;
  208. }
  209. static __always_inline bool ma_is_dense(const enum maple_type type)
  210. {
  211. return type < maple_leaf_64;
  212. }
  213. static __always_inline bool ma_is_leaf(const enum maple_type type)
  214. {
  215. return type < maple_range_64;
  216. }
  217. static __always_inline bool mte_is_leaf(const struct maple_enode *entry)
  218. {
  219. return ma_is_leaf(mte_node_type(entry));
  220. }
  221. /*
  222. * We also reserve values with the bottom two bits set to '10' which are
  223. * below 4096
  224. */
  225. static __always_inline bool mt_is_reserved(const void *entry)
  226. {
  227. return ((unsigned long)entry < MAPLE_RESERVED_RANGE) &&
  228. xa_is_internal(entry);
  229. }
  230. static __always_inline void mas_set_err(struct ma_state *mas, long err)
  231. {
  232. mas->node = MA_ERROR(err);
  233. mas->status = ma_error;
  234. }
  235. static __always_inline bool mas_is_ptr(const struct ma_state *mas)
  236. {
  237. return mas->status == ma_root;
  238. }
  239. static __always_inline bool mas_is_start(const struct ma_state *mas)
  240. {
  241. return mas->status == ma_start;
  242. }
  243. static __always_inline bool mas_is_none(const struct ma_state *mas)
  244. {
  245. return mas->status == ma_none;
  246. }
  247. static __always_inline bool mas_is_paused(const struct ma_state *mas)
  248. {
  249. return mas->status == ma_pause;
  250. }
  251. static __always_inline bool mas_is_overflow(struct ma_state *mas)
  252. {
  253. return mas->status == ma_overflow;
  254. }
  255. static inline bool mas_is_underflow(struct ma_state *mas)
  256. {
  257. return mas->status == ma_underflow;
  258. }
  259. static __always_inline struct maple_node *mte_to_node(
  260. const struct maple_enode *entry)
  261. {
  262. return (struct maple_node *)((unsigned long)entry & ~MAPLE_NODE_MASK);
  263. }
  264. /*
  265. * mte_to_mat() - Convert a maple encoded node to a maple topiary node.
  266. * @entry: The maple encoded node
  267. *
  268. * Return: a maple topiary pointer
  269. */
  270. static inline struct maple_topiary *mte_to_mat(const struct maple_enode *entry)
  271. {
  272. return (struct maple_topiary *)
  273. ((unsigned long)entry & ~MAPLE_NODE_MASK);
  274. }
  275. /*
  276. * mas_mn() - Get the maple state node.
  277. * @mas: The maple state
  278. *
  279. * Return: the maple node (not encoded - bare pointer).
  280. */
  281. static inline struct maple_node *mas_mn(const struct ma_state *mas)
  282. {
  283. return mte_to_node(mas->node);
  284. }
  285. /*
  286. * mte_set_node_dead() - Set a maple encoded node as dead.
  287. * @mn: The maple encoded node.
  288. */
  289. static inline void mte_set_node_dead(struct maple_enode *mn)
  290. {
  291. mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
  292. smp_wmb(); /* Needed for RCU */
  293. }
  294. /* Bit 1 indicates the root is a node */
  295. #define MAPLE_ROOT_NODE 0x02
  296. /* maple_type stored bit 3-6 */
  297. #define MAPLE_ENODE_TYPE_SHIFT 0x03
  298. /* Bit 2 means a NULL somewhere below */
  299. #define MAPLE_ENODE_NULL 0x04
  300. static inline struct maple_enode *mt_mk_node(const struct maple_node *node,
  301. enum maple_type type)
  302. {
  303. return (void *)((unsigned long)node |
  304. (type << MAPLE_ENODE_TYPE_SHIFT) | MAPLE_ENODE_NULL);
  305. }
  306. static inline void *mte_mk_root(const struct maple_enode *node)
  307. {
  308. return (void *)((unsigned long)node | MAPLE_ROOT_NODE);
  309. }
  310. static inline void *mte_safe_root(const struct maple_enode *node)
  311. {
  312. return (void *)((unsigned long)node & ~MAPLE_ROOT_NODE);
  313. }
  314. static inline void __maybe_unused *mte_set_full(const struct maple_enode *node)
  315. {
  316. return (void *)((unsigned long)node & ~MAPLE_ENODE_NULL);
  317. }
  318. static inline void __maybe_unused *mte_clear_full(const struct maple_enode *node)
  319. {
  320. return (void *)((unsigned long)node | MAPLE_ENODE_NULL);
  321. }
  322. static inline bool __maybe_unused mte_has_null(const struct maple_enode *node)
  323. {
  324. return (unsigned long)node & MAPLE_ENODE_NULL;
  325. }
  326. static __always_inline bool ma_is_root(struct maple_node *node)
  327. {
  328. return ((unsigned long)node->parent & MA_ROOT_PARENT);
  329. }
  330. static __always_inline bool mte_is_root(const struct maple_enode *node)
  331. {
  332. return ma_is_root(mte_to_node(node));
  333. }
  334. static inline bool mas_is_root_limits(const struct ma_state *mas)
  335. {
  336. return !mas->min && mas->max == ULONG_MAX;
  337. }
  338. static __always_inline bool mt_is_alloc(struct maple_tree *mt)
  339. {
  340. return (mt->ma_flags & MT_FLAGS_ALLOC_RANGE);
  341. }
  342. /*
  343. * The Parent Pointer
  344. * Excluding root, the parent pointer is 256B aligned like all other tree nodes.
  345. * When storing a 32 or 64 bit values, the offset can fit into 5 bits. The 16
  346. * bit values need an extra bit to store the offset. This extra bit comes from
  347. * a reuse of the last bit in the node type. This is possible by using bit 1 to
  348. * indicate if bit 2 is part of the type or the slot.
  349. *
  350. * Node types:
  351. * 0b??1 = Root
  352. * 0b?00 = 16 bit nodes
  353. * 0b010 = 32 bit nodes
  354. * 0b110 = 64 bit nodes
  355. *
  356. * Slot size and alignment
  357. * 0b??1 : Root
  358. * 0b?00 : 16 bit values, type in 0-1, slot in 2-7
  359. * 0b010 : 32 bit values, type in 0-2, slot in 3-7
  360. * 0b110 : 64 bit values, type in 0-2, slot in 3-7
  361. */
  362. #define MAPLE_PARENT_ROOT 0x01
  363. #define MAPLE_PARENT_SLOT_SHIFT 0x03
  364. #define MAPLE_PARENT_SLOT_MASK 0xF8
  365. #define MAPLE_PARENT_16B_SLOT_SHIFT 0x02
  366. #define MAPLE_PARENT_16B_SLOT_MASK 0xFC
  367. #define MAPLE_PARENT_RANGE64 0x06
  368. #define MAPLE_PARENT_RANGE32 0x02
  369. #define MAPLE_PARENT_NOT_RANGE16 0x02
  370. /*
  371. * mte_parent_shift() - Get the parent shift for the slot storage.
  372. * @parent: The parent pointer cast as an unsigned long
  373. * Return: The shift into that pointer to the star to of the slot
  374. */
  375. static inline unsigned long mte_parent_shift(unsigned long parent)
  376. {
  377. /* Note bit 1 == 0 means 16B */
  378. if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
  379. return MAPLE_PARENT_SLOT_SHIFT;
  380. return MAPLE_PARENT_16B_SLOT_SHIFT;
  381. }
  382. /*
  383. * mte_parent_slot_mask() - Get the slot mask for the parent.
  384. * @parent: The parent pointer cast as an unsigned long.
  385. * Return: The slot mask for that parent.
  386. */
  387. static inline unsigned long mte_parent_slot_mask(unsigned long parent)
  388. {
  389. /* Note bit 1 == 0 means 16B */
  390. if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
  391. return MAPLE_PARENT_SLOT_MASK;
  392. return MAPLE_PARENT_16B_SLOT_MASK;
  393. }
  394. /*
  395. * mas_parent_type() - Return the maple_type of the parent from the stored
  396. * parent type.
  397. * @mas: The maple state
  398. * @enode: The maple_enode to extract the parent's enum
  399. * Return: The node->parent maple_type
  400. */
  401. static inline
  402. enum maple_type mas_parent_type(struct ma_state *mas, struct maple_enode *enode)
  403. {
  404. unsigned long p_type;
  405. p_type = (unsigned long)mte_to_node(enode)->parent;
  406. if (WARN_ON(p_type & MAPLE_PARENT_ROOT))
  407. return 0;
  408. p_type &= MAPLE_NODE_MASK;
  409. p_type &= ~mte_parent_slot_mask(p_type);
  410. switch (p_type) {
  411. case MAPLE_PARENT_RANGE64: /* or MAPLE_PARENT_ARANGE64 */
  412. if (mt_is_alloc(mas->tree))
  413. return maple_arange_64;
  414. return maple_range_64;
  415. }
  416. return 0;
  417. }
  418. /*
  419. * mas_set_parent() - Set the parent node and encode the slot
  420. * @mas: The maple state
  421. * @enode: The encoded maple node.
  422. * @parent: The encoded maple node that is the parent of @enode.
  423. * @slot: The slot that @enode resides in @parent.
  424. *
  425. * Slot number is encoded in the enode->parent bit 3-6 or 2-6, depending on the
  426. * parent type.
  427. */
  428. static inline
  429. void mas_set_parent(struct ma_state *mas, struct maple_enode *enode,
  430. const struct maple_enode *parent, unsigned char slot)
  431. {
  432. unsigned long val = (unsigned long)parent;
  433. unsigned long shift;
  434. unsigned long type;
  435. enum maple_type p_type = mte_node_type(parent);
  436. MAS_BUG_ON(mas, p_type == maple_dense);
  437. MAS_BUG_ON(mas, p_type == maple_leaf_64);
  438. switch (p_type) {
  439. case maple_range_64:
  440. case maple_arange_64:
  441. shift = MAPLE_PARENT_SLOT_SHIFT;
  442. type = MAPLE_PARENT_RANGE64;
  443. break;
  444. default:
  445. case maple_dense:
  446. case maple_leaf_64:
  447. shift = type = 0;
  448. break;
  449. }
  450. val &= ~MAPLE_NODE_MASK; /* Clear all node metadata in parent */
  451. val |= (slot << shift) | type;
  452. mte_to_node(enode)->parent = ma_parent_ptr(val);
  453. }
  454. /*
  455. * mte_parent_slot() - get the parent slot of @enode.
  456. * @enode: The encoded maple node.
  457. *
  458. * Return: The slot in the parent node where @enode resides.
  459. */
  460. static __always_inline
  461. unsigned int mte_parent_slot(const struct maple_enode *enode)
  462. {
  463. unsigned long val = (unsigned long)mte_to_node(enode)->parent;
  464. if (unlikely(val & MA_ROOT_PARENT))
  465. return 0;
  466. /*
  467. * Okay to use MAPLE_PARENT_16B_SLOT_MASK as the last bit will be lost
  468. * by shift if the parent shift is MAPLE_PARENT_SLOT_SHIFT
  469. */
  470. return (val & MAPLE_PARENT_16B_SLOT_MASK) >> mte_parent_shift(val);
  471. }
  472. /*
  473. * mte_parent() - Get the parent of @node.
  474. * @enode: The encoded maple node.
  475. *
  476. * Return: The parent maple node.
  477. */
  478. static __always_inline
  479. struct maple_node *mte_parent(const struct maple_enode *enode)
  480. {
  481. return (void *)((unsigned long)
  482. (mte_to_node(enode)->parent) & ~MAPLE_NODE_MASK);
  483. }
  484. /*
  485. * ma_dead_node() - check if the @enode is dead.
  486. * @enode: The encoded maple node
  487. *
  488. * Return: true if dead, false otherwise.
  489. */
  490. static __always_inline bool ma_dead_node(const struct maple_node *node)
  491. {
  492. struct maple_node *parent;
  493. /* Do not reorder reads from the node prior to the parent check */
  494. smp_rmb();
  495. parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);
  496. return (parent == node);
  497. }
  498. /*
  499. * mte_dead_node() - check if the @enode is dead.
  500. * @enode: The encoded maple node
  501. *
  502. * Return: true if dead, false otherwise.
  503. */
  504. static __always_inline bool mte_dead_node(const struct maple_enode *enode)
  505. {
  506. struct maple_node *node;
  507. node = mte_to_node(enode);
  508. return ma_dead_node(node);
  509. }
  510. /*
  511. * ma_pivots() - Get a pointer to the maple node pivots.
  512. * @node: the maple node
  513. * @type: the node type
  514. *
  515. * In the event of a dead node, this array may be %NULL
  516. *
  517. * Return: A pointer to the maple node pivots
  518. */
  519. static inline unsigned long *ma_pivots(struct maple_node *node,
  520. enum maple_type type)
  521. {
  522. switch (type) {
  523. case maple_arange_64:
  524. return node->ma64.pivot;
  525. case maple_range_64:
  526. case maple_leaf_64:
  527. return node->mr64.pivot;
  528. case maple_dense:
  529. return NULL;
  530. }
  531. return NULL;
  532. }
  533. /*
  534. * ma_gaps() - Get a pointer to the maple node gaps.
  535. * @node: the maple node
  536. * @type: the node type
  537. *
  538. * Return: A pointer to the maple node gaps
  539. */
  540. static inline unsigned long *ma_gaps(struct maple_node *node,
  541. enum maple_type type)
  542. {
  543. switch (type) {
  544. case maple_arange_64:
  545. return node->ma64.gap;
  546. case maple_range_64:
  547. case maple_leaf_64:
  548. case maple_dense:
  549. return NULL;
  550. }
  551. return NULL;
  552. }
  553. /*
  554. * mas_safe_pivot() - get the pivot at @piv or mas->max.
  555. * @mas: The maple state
  556. * @pivots: The pointer to the maple node pivots
  557. * @piv: The pivot to fetch
  558. * @type: The maple node type
  559. *
  560. * Return: The pivot at @piv within the limit of the @pivots array, @mas->max
  561. * otherwise.
  562. */
  563. static __always_inline unsigned long
  564. mas_safe_pivot(const struct ma_state *mas, unsigned long *pivots,
  565. unsigned char piv, enum maple_type type)
  566. {
  567. if (piv >= mt_pivots[type])
  568. return mas->max;
  569. return pivots[piv];
  570. }
  571. /*
  572. * mas_safe_min() - Return the minimum for a given offset.
  573. * @mas: The maple state
  574. * @pivots: The pointer to the maple node pivots
  575. * @offset: The offset into the pivot array
  576. *
  577. * Return: The minimum range value that is contained in @offset.
  578. */
  579. static inline unsigned long
  580. mas_safe_min(struct ma_state *mas, unsigned long *pivots, unsigned char offset)
  581. {
  582. if (likely(offset))
  583. return pivots[offset - 1] + 1;
  584. return mas->min;
  585. }
  586. /*
  587. * mte_set_pivot() - Set a pivot to a value in an encoded maple node.
  588. * @mn: The encoded maple node
  589. * @piv: The pivot offset
  590. * @val: The value of the pivot
  591. */
  592. static inline void mte_set_pivot(struct maple_enode *mn, unsigned char piv,
  593. unsigned long val)
  594. {
  595. struct maple_node *node = mte_to_node(mn);
  596. enum maple_type type = mte_node_type(mn);
  597. BUG_ON(piv >= mt_pivots[type]);
  598. switch (type) {
  599. case maple_range_64:
  600. case maple_leaf_64:
  601. node->mr64.pivot[piv] = val;
  602. break;
  603. case maple_arange_64:
  604. node->ma64.pivot[piv] = val;
  605. break;
  606. case maple_dense:
  607. break;
  608. }
  609. }
  610. /*
  611. * ma_slots() - Get a pointer to the maple node slots.
  612. * @mn: The maple node
  613. * @mt: The maple node type
  614. *
  615. * Return: A pointer to the maple node slots
  616. */
  617. static inline void __rcu **ma_slots(struct maple_node *mn, enum maple_type mt)
  618. {
  619. switch (mt) {
  620. case maple_arange_64:
  621. return mn->ma64.slot;
  622. case maple_range_64:
  623. case maple_leaf_64:
  624. return mn->mr64.slot;
  625. case maple_dense:
  626. return mn->slot;
  627. }
  628. return NULL;
  629. }
  630. static inline bool mt_write_locked(const struct maple_tree *mt)
  631. {
  632. return mt_external_lock(mt) ? mt_write_lock_is_held(mt) :
  633. lockdep_is_held(&mt->ma_lock);
  634. }
  635. static __always_inline bool mt_locked(const struct maple_tree *mt)
  636. {
  637. return mt_external_lock(mt) ? mt_lock_is_held(mt) :
  638. lockdep_is_held(&mt->ma_lock);
  639. }
  640. static __always_inline void *mt_slot(const struct maple_tree *mt,
  641. void __rcu **slots, unsigned char offset)
  642. {
  643. return rcu_dereference_check(slots[offset], mt_locked(mt));
  644. }
  645. static __always_inline void *mt_slot_locked(struct maple_tree *mt,
  646. void __rcu **slots, unsigned char offset)
  647. {
  648. return rcu_dereference_protected(slots[offset], mt_write_locked(mt));
  649. }
  650. /*
  651. * mas_slot_locked() - Get the slot value when holding the maple tree lock.
  652. * @mas: The maple state
  653. * @slots: The pointer to the slots
  654. * @offset: The offset into the slots array to fetch
  655. *
  656. * Return: The entry stored in @slots at the @offset.
  657. */
  658. static __always_inline void *mas_slot_locked(struct ma_state *mas,
  659. void __rcu **slots, unsigned char offset)
  660. {
  661. return mt_slot_locked(mas->tree, slots, offset);
  662. }
  663. /*
  664. * mas_slot() - Get the slot value when not holding the maple tree lock.
  665. * @mas: The maple state
  666. * @slots: The pointer to the slots
  667. * @offset: The offset into the slots array to fetch
  668. *
  669. * Return: The entry stored in @slots at the @offset
  670. */
  671. static __always_inline void *mas_slot(struct ma_state *mas, void __rcu **slots,
  672. unsigned char offset)
  673. {
  674. return mt_slot(mas->tree, slots, offset);
  675. }
  676. /*
  677. * mas_root() - Get the maple tree root.
  678. * @mas: The maple state.
  679. *
  680. * Return: The pointer to the root of the tree
  681. */
  682. static __always_inline void *mas_root(struct ma_state *mas)
  683. {
  684. return rcu_dereference_check(mas->tree->ma_root, mt_locked(mas->tree));
  685. }
  686. static inline void *mt_root_locked(struct maple_tree *mt)
  687. {
  688. return rcu_dereference_protected(mt->ma_root, mt_write_locked(mt));
  689. }
  690. /*
  691. * mas_root_locked() - Get the maple tree root when holding the maple tree lock.
  692. * @mas: The maple state.
  693. *
  694. * Return: The pointer to the root of the tree
  695. */
  696. static inline void *mas_root_locked(struct ma_state *mas)
  697. {
  698. return mt_root_locked(mas->tree);
  699. }
  700. static inline struct maple_metadata *ma_meta(struct maple_node *mn,
  701. enum maple_type mt)
  702. {
  703. switch (mt) {
  704. case maple_arange_64:
  705. return &mn->ma64.meta;
  706. default:
  707. return &mn->mr64.meta;
  708. }
  709. }
  710. /*
  711. * ma_set_meta() - Set the metadata information of a node.
  712. * @mn: The maple node
  713. * @mt: The maple node type
  714. * @offset: The offset of the highest sub-gap in this node.
  715. * @end: The end of the data in this node.
  716. */
  717. static inline void ma_set_meta(struct maple_node *mn, enum maple_type mt,
  718. unsigned char offset, unsigned char end)
  719. {
  720. struct maple_metadata *meta = ma_meta(mn, mt);
  721. meta->gap = offset;
  722. meta->end = end;
  723. }
  724. /*
  725. * mt_clear_meta() - clear the metadata information of a node, if it exists
  726. * @mt: The maple tree
  727. * @mn: The maple node
  728. * @type: The maple node type
  729. */
  730. static inline void mt_clear_meta(struct maple_tree *mt, struct maple_node *mn,
  731. enum maple_type type)
  732. {
  733. struct maple_metadata *meta;
  734. unsigned long *pivots;
  735. void __rcu **slots;
  736. void *next;
  737. switch (type) {
  738. case maple_range_64:
  739. pivots = mn->mr64.pivot;
  740. if (unlikely(pivots[MAPLE_RANGE64_SLOTS - 2])) {
  741. slots = mn->mr64.slot;
  742. next = mt_slot_locked(mt, slots,
  743. MAPLE_RANGE64_SLOTS - 1);
  744. if (unlikely((mte_to_node(next) &&
  745. mte_node_type(next))))
  746. return; /* no metadata, could be node */
  747. }
  748. fallthrough;
  749. case maple_arange_64:
  750. meta = ma_meta(mn, type);
  751. break;
  752. default:
  753. return;
  754. }
  755. meta->gap = 0;
  756. meta->end = 0;
  757. }
  758. /*
  759. * ma_meta_end() - Get the data end of a node from the metadata
  760. * @mn: The maple node
  761. * @mt: The maple node type
  762. */
  763. static inline unsigned char ma_meta_end(struct maple_node *mn,
  764. enum maple_type mt)
  765. {
  766. struct maple_metadata *meta = ma_meta(mn, mt);
  767. return meta->end;
  768. }
  769. /*
  770. * ma_meta_gap() - Get the largest gap location of a node from the metadata
  771. * @mn: The maple node
  772. */
  773. static inline unsigned char ma_meta_gap(struct maple_node *mn)
  774. {
  775. return mn->ma64.meta.gap;
  776. }
  777. /*
  778. * ma_set_meta_gap() - Set the largest gap location in a nodes metadata
  779. * @mn: The maple node
  780. * @mt: The maple node type
  781. * @offset: The location of the largest gap.
  782. */
  783. static inline void ma_set_meta_gap(struct maple_node *mn, enum maple_type mt,
  784. unsigned char offset)
  785. {
  786. struct maple_metadata *meta = ma_meta(mn, mt);
  787. meta->gap = offset;
  788. }
  789. /*
  790. * mat_add() - Add a @dead_enode to the ma_topiary of a list of dead nodes.
  791. * @mat: the ma_topiary, a linked list of dead nodes.
  792. * @dead_enode: the node to be marked as dead and added to the tail of the list
  793. *
  794. * Add the @dead_enode to the linked list in @mat.
  795. */
  796. static inline void mat_add(struct ma_topiary *mat,
  797. struct maple_enode *dead_enode)
  798. {
  799. mte_set_node_dead(dead_enode);
  800. mte_to_mat(dead_enode)->next = NULL;
  801. if (!mat->tail) {
  802. mat->tail = mat->head = dead_enode;
  803. return;
  804. }
  805. mte_to_mat(mat->tail)->next = dead_enode;
  806. mat->tail = dead_enode;
  807. }
  808. static void mt_free_walk(struct rcu_head *head);
  809. static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
  810. bool free);
  811. /*
  812. * mas_mat_destroy() - Free all nodes and subtrees in a dead list.
  813. * @mas: the maple state
  814. * @mat: the ma_topiary linked list of dead nodes to free.
  815. *
  816. * Destroy walk a dead list.
  817. */
  818. static void mas_mat_destroy(struct ma_state *mas, struct ma_topiary *mat)
  819. {
  820. struct maple_enode *next;
  821. struct maple_node *node;
  822. bool in_rcu = mt_in_rcu(mas->tree);
  823. while (mat->head) {
  824. next = mte_to_mat(mat->head)->next;
  825. node = mte_to_node(mat->head);
  826. mt_destroy_walk(mat->head, mas->tree, !in_rcu);
  827. if (in_rcu)
  828. call_rcu(&node->rcu, mt_free_walk);
  829. mat->head = next;
  830. }
  831. }
  832. /*
  833. * mas_descend() - Descend into the slot stored in the ma_state.
  834. * @mas: the maple state.
  835. *
  836. * Note: Not RCU safe, only use in write side or debug code.
  837. */
  838. static inline void mas_descend(struct ma_state *mas)
  839. {
  840. enum maple_type type;
  841. unsigned long *pivots;
  842. struct maple_node *node;
  843. void __rcu **slots;
  844. node = mas_mn(mas);
  845. type = mte_node_type(mas->node);
  846. pivots = ma_pivots(node, type);
  847. slots = ma_slots(node, type);
  848. if (mas->offset)
  849. mas->min = pivots[mas->offset - 1] + 1;
  850. mas->max = mas_safe_pivot(mas, pivots, mas->offset, type);
  851. mas->node = mas_slot(mas, slots, mas->offset);
  852. }
  853. /*
  854. * mas_ascend() - Walk up a level of the tree.
  855. * @mas: The maple state
  856. *
  857. * Sets the @mas->max and @mas->min for the parent node of mas->node. This
  858. * may cause several levels of walking up to find the correct min and max.
  859. * May find a dead node which will cause a premature return.
  860. * Return: 1 on dead node, 0 otherwise
  861. */
  862. static int mas_ascend(struct ma_state *mas)
  863. {
  864. struct maple_enode *p_enode; /* parent enode. */
  865. struct maple_enode *a_enode; /* ancestor enode. */
  866. struct maple_node *a_node; /* ancestor node. */
  867. struct maple_node *p_node; /* parent node. */
  868. unsigned char a_slot;
  869. enum maple_type a_type;
  870. unsigned long min, max;
  871. unsigned long *pivots;
  872. bool set_max = false, set_min = false;
  873. a_node = mas_mn(mas);
  874. if (ma_is_root(a_node)) {
  875. mas->offset = 0;
  876. return 0;
  877. }
  878. p_node = mte_parent(mas->node);
  879. if (unlikely(a_node == p_node))
  880. return 1;
  881. a_type = mas_parent_type(mas, mas->node);
  882. mas->offset = mte_parent_slot(mas->node);
  883. a_enode = mt_mk_node(p_node, a_type);
  884. /* Check to make sure all parent information is still accurate */
  885. if (p_node != mte_parent(mas->node))
  886. return 1;
  887. mas->node = a_enode;
  888. if (mte_is_root(a_enode)) {
  889. mas->max = ULONG_MAX;
  890. mas->min = 0;
  891. return 0;
  892. }
  893. min = 0;
  894. max = ULONG_MAX;
  895. /*
  896. * !mas->offset implies that parent node min == mas->min.
  897. * mas->offset > 0 implies that we need to walk up to find the
  898. * implied pivot min.
  899. */
  900. if (!mas->offset) {
  901. min = mas->min;
  902. set_min = true;
  903. }
  904. if (mas->max == ULONG_MAX)
  905. set_max = true;
  906. do {
  907. p_enode = a_enode;
  908. a_type = mas_parent_type(mas, p_enode);
  909. a_node = mte_parent(p_enode);
  910. a_slot = mte_parent_slot(p_enode);
  911. a_enode = mt_mk_node(a_node, a_type);
  912. pivots = ma_pivots(a_node, a_type);
  913. if (unlikely(ma_dead_node(a_node)))
  914. return 1;
  915. if (!set_min && a_slot) {
  916. set_min = true;
  917. min = pivots[a_slot - 1] + 1;
  918. }
  919. if (!set_max && a_slot < mt_pivots[a_type]) {
  920. set_max = true;
  921. max = pivots[a_slot];
  922. }
  923. if (unlikely(ma_dead_node(a_node)))
  924. return 1;
  925. if (unlikely(ma_is_root(a_node)))
  926. break;
  927. } while (!set_min || !set_max);
  928. mas->max = max;
  929. mas->min = min;
  930. return 0;
  931. }
  932. /*
  933. * mas_pop_node() - Get a previously allocated maple node from the maple state.
  934. * @mas: The maple state
  935. *
  936. * Return: A pointer to a maple node.
  937. */
  938. static __always_inline struct maple_node *mas_pop_node(struct ma_state *mas)
  939. {
  940. struct maple_node *ret;
  941. if (mas->alloc) {
  942. ret = mas->alloc;
  943. mas->alloc = NULL;
  944. goto out;
  945. }
  946. if (WARN_ON_ONCE(!mas->sheaf))
  947. return NULL;
  948. ret = kmem_cache_alloc_from_sheaf(maple_node_cache, GFP_NOWAIT, mas->sheaf);
  949. out:
  950. memset(ret, 0, sizeof(*ret));
  951. return ret;
  952. }
  953. /*
  954. * mas_alloc_nodes() - Allocate nodes into a maple state
  955. * @mas: The maple state
  956. * @gfp: The GFP Flags
  957. */
  958. static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
  959. {
  960. if (!mas->node_request)
  961. return;
  962. if (mas->node_request == 1) {
  963. if (mas->sheaf)
  964. goto use_sheaf;
  965. if (mas->alloc)
  966. return;
  967. mas->alloc = mt_alloc_one(gfp);
  968. if (!mas->alloc)
  969. goto error;
  970. mas->node_request = 0;
  971. return;
  972. }
  973. use_sheaf:
  974. if (unlikely(mas->alloc)) {
  975. kfree(mas->alloc);
  976. mas->alloc = NULL;
  977. }
  978. if (mas->sheaf) {
  979. unsigned long refill;
  980. refill = mas->node_request;
  981. if (kmem_cache_sheaf_size(mas->sheaf) >= refill) {
  982. mas->node_request = 0;
  983. return;
  984. }
  985. if (mt_refill_sheaf(gfp, &mas->sheaf, refill))
  986. goto error;
  987. mas->node_request = 0;
  988. return;
  989. }
  990. mas->sheaf = mt_get_sheaf(gfp, mas->node_request);
  991. if (likely(mas->sheaf)) {
  992. mas->node_request = 0;
  993. return;
  994. }
  995. error:
  996. mas_set_err(mas, -ENOMEM);
  997. }
  998. static inline void mas_empty_nodes(struct ma_state *mas)
  999. {
  1000. mas->node_request = 0;
  1001. if (mas->sheaf) {
  1002. mt_return_sheaf(mas->sheaf);
  1003. mas->sheaf = NULL;
  1004. }
  1005. if (mas->alloc) {
  1006. kfree(mas->alloc);
  1007. mas->alloc = NULL;
  1008. }
  1009. }
  1010. /*
  1011. * mas_free() - Free an encoded maple node
  1012. * @mas: The maple state
  1013. * @used: The encoded maple node to free.
  1014. *
  1015. * Uses rcu free if necessary, pushes @used back on the maple state allocations
  1016. * otherwise.
  1017. */
  1018. static inline void mas_free(struct ma_state *mas, struct maple_enode *used)
  1019. {
  1020. ma_free_rcu(mte_to_node(used));
  1021. }
  1022. /*
  1023. * mas_start() - Sets up maple state for operations.
  1024. * @mas: The maple state.
  1025. *
  1026. * If mas->status == ma_start, then set the min, max and depth to
  1027. * defaults.
  1028. *
  1029. * Return:
  1030. * - If mas->node is an error or not mas_start, return NULL.
  1031. * - If it's an empty tree: NULL & mas->status == ma_none
  1032. * - If it's a single entry: The entry & mas->status == ma_root
  1033. * - If it's a tree: NULL & mas->status == ma_active
  1034. */
  1035. static inline struct maple_enode *mas_start(struct ma_state *mas)
  1036. {
  1037. if (likely(mas_is_start(mas))) {
  1038. struct maple_enode *root;
  1039. mas->min = 0;
  1040. mas->max = ULONG_MAX;
  1041. retry:
  1042. mas->depth = 0;
  1043. root = mas_root(mas);
  1044. /* Tree with nodes */
  1045. if (likely(xa_is_node(root))) {
  1046. mas->depth = 0;
  1047. mas->status = ma_active;
  1048. mas->node = mte_safe_root(root);
  1049. mas->offset = 0;
  1050. if (mte_dead_node(mas->node))
  1051. goto retry;
  1052. return NULL;
  1053. }
  1054. mas->node = NULL;
  1055. /* empty tree */
  1056. if (unlikely(!root)) {
  1057. mas->status = ma_none;
  1058. mas->offset = MAPLE_NODE_SLOTS;
  1059. return NULL;
  1060. }
  1061. /* Single entry tree */
  1062. mas->status = ma_root;
  1063. mas->offset = MAPLE_NODE_SLOTS;
  1064. /* Single entry tree. */
  1065. if (mas->index > 0)
  1066. return NULL;
  1067. return root;
  1068. }
  1069. return NULL;
  1070. }
  1071. /*
  1072. * ma_data_end() - Find the end of the data in a node.
  1073. * @node: The maple node
  1074. * @type: The maple node type
  1075. * @pivots: The array of pivots in the node
  1076. * @max: The maximum value in the node
  1077. *
  1078. * Uses metadata to find the end of the data when possible.
  1079. * Return: The zero indexed last slot with data (may be null).
  1080. */
  1081. static __always_inline unsigned char ma_data_end(struct maple_node *node,
  1082. enum maple_type type, unsigned long *pivots, unsigned long max)
  1083. {
  1084. unsigned char offset;
  1085. if (!pivots)
  1086. return 0;
  1087. if (type == maple_arange_64)
  1088. return ma_meta_end(node, type);
  1089. offset = mt_pivots[type] - 1;
  1090. if (likely(!pivots[offset]))
  1091. return ma_meta_end(node, type);
  1092. if (likely(pivots[offset] == max))
  1093. return offset;
  1094. return mt_pivots[type];
  1095. }
  1096. /*
  1097. * mas_data_end() - Find the end of the data (slot).
  1098. * @mas: the maple state
  1099. *
  1100. * This method is optimized to check the metadata of a node if the node type
  1101. * supports data end metadata.
  1102. *
  1103. * Return: The zero indexed last slot with data (may be null).
  1104. */
  1105. static inline unsigned char mas_data_end(struct ma_state *mas)
  1106. {
  1107. enum maple_type type;
  1108. struct maple_node *node;
  1109. unsigned char offset;
  1110. unsigned long *pivots;
  1111. type = mte_node_type(mas->node);
  1112. node = mas_mn(mas);
  1113. if (type == maple_arange_64)
  1114. return ma_meta_end(node, type);
  1115. pivots = ma_pivots(node, type);
  1116. if (unlikely(ma_dead_node(node)))
  1117. return 0;
  1118. offset = mt_pivots[type] - 1;
  1119. if (likely(!pivots[offset]))
  1120. return ma_meta_end(node, type);
  1121. if (likely(pivots[offset] == mas->max))
  1122. return offset;
  1123. return mt_pivots[type];
  1124. }
  1125. /*
  1126. * mas_leaf_max_gap() - Returns the largest gap in a leaf node
  1127. * @mas: the maple state
  1128. *
  1129. * Return: The maximum gap in the leaf.
  1130. */
  1131. static unsigned long mas_leaf_max_gap(struct ma_state *mas)
  1132. {
  1133. enum maple_type mt;
  1134. unsigned long pstart, gap, max_gap;
  1135. struct maple_node *mn;
  1136. unsigned long *pivots;
  1137. void __rcu **slots;
  1138. unsigned char i;
  1139. unsigned char max_piv;
  1140. mt = mte_node_type(mas->node);
  1141. mn = mas_mn(mas);
  1142. slots = ma_slots(mn, mt);
  1143. max_gap = 0;
  1144. if (unlikely(ma_is_dense(mt))) {
  1145. gap = 0;
  1146. for (i = 0; i < mt_slots[mt]; i++) {
  1147. if (slots[i]) {
  1148. if (gap > max_gap)
  1149. max_gap = gap;
  1150. gap = 0;
  1151. } else {
  1152. gap++;
  1153. }
  1154. }
  1155. if (gap > max_gap)
  1156. max_gap = gap;
  1157. return max_gap;
  1158. }
  1159. /*
  1160. * Check the first implied pivot optimizes the loop below and slot 1 may
  1161. * be skipped if there is a gap in slot 0.
  1162. */
  1163. pivots = ma_pivots(mn, mt);
  1164. if (likely(!slots[0])) {
  1165. max_gap = pivots[0] - mas->min + 1;
  1166. i = 2;
  1167. } else {
  1168. i = 1;
  1169. }
  1170. /* reduce max_piv as the special case is checked before the loop */
  1171. max_piv = ma_data_end(mn, mt, pivots, mas->max) - 1;
  1172. /*
  1173. * Check end implied pivot which can only be a gap on the right most
  1174. * node.
  1175. */
  1176. if (unlikely(mas->max == ULONG_MAX) && !slots[max_piv + 1]) {
  1177. gap = ULONG_MAX - pivots[max_piv];
  1178. if (gap > max_gap)
  1179. max_gap = gap;
  1180. if (max_gap > pivots[max_piv] - mas->min)
  1181. return max_gap;
  1182. }
  1183. for (; i <= max_piv; i++) {
  1184. /* data == no gap. */
  1185. if (likely(slots[i]))
  1186. continue;
  1187. pstart = pivots[i - 1];
  1188. gap = pivots[i] - pstart;
  1189. if (gap > max_gap)
  1190. max_gap = gap;
  1191. /* There cannot be two gaps in a row. */
  1192. i++;
  1193. }
  1194. return max_gap;
  1195. }
  1196. /*
  1197. * ma_max_gap() - Get the maximum gap in a maple node (non-leaf)
  1198. * @node: The maple node
  1199. * @gaps: The pointer to the gaps
  1200. * @mt: The maple node type
  1201. * @off: Pointer to store the offset location of the gap.
  1202. *
  1203. * Uses the metadata data end to scan backwards across set gaps.
  1204. *
  1205. * Return: The maximum gap value
  1206. */
  1207. static inline unsigned long
  1208. ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
  1209. unsigned char *off)
  1210. {
  1211. unsigned char offset, i;
  1212. unsigned long max_gap = 0;
  1213. i = offset = ma_meta_end(node, mt);
  1214. do {
  1215. if (gaps[i] > max_gap) {
  1216. max_gap = gaps[i];
  1217. offset = i;
  1218. }
  1219. } while (i--);
  1220. *off = offset;
  1221. return max_gap;
  1222. }
  1223. /*
  1224. * mas_max_gap() - find the largest gap in a non-leaf node and set the slot.
  1225. * @mas: The maple state.
  1226. *
  1227. * Return: The gap value.
  1228. */
  1229. static inline unsigned long mas_max_gap(struct ma_state *mas)
  1230. {
  1231. unsigned long *gaps;
  1232. unsigned char offset;
  1233. enum maple_type mt;
  1234. struct maple_node *node;
  1235. mt = mte_node_type(mas->node);
  1236. if (ma_is_leaf(mt))
  1237. return mas_leaf_max_gap(mas);
  1238. node = mas_mn(mas);
  1239. MAS_BUG_ON(mas, mt != maple_arange_64);
  1240. offset = ma_meta_gap(node);
  1241. gaps = ma_gaps(node, mt);
  1242. return gaps[offset];
  1243. }
  1244. /*
  1245. * mas_parent_gap() - Set the parent gap and any gaps above, as needed
  1246. * @mas: The maple state
  1247. * @offset: The gap offset in the parent to set
  1248. * @new: The new gap value.
  1249. *
  1250. * Set the parent gap then continue to set the gap upwards, using the metadata
  1251. * of the parent to see if it is necessary to check the node above.
  1252. */
  1253. static inline void mas_parent_gap(struct ma_state *mas, unsigned char offset,
  1254. unsigned long new)
  1255. {
  1256. unsigned long meta_gap = 0;
  1257. struct maple_node *pnode;
  1258. struct maple_enode *penode;
  1259. unsigned long *pgaps;
  1260. unsigned char meta_offset;
  1261. enum maple_type pmt;
  1262. pnode = mte_parent(mas->node);
  1263. pmt = mas_parent_type(mas, mas->node);
  1264. penode = mt_mk_node(pnode, pmt);
  1265. pgaps = ma_gaps(pnode, pmt);
  1266. ascend:
  1267. MAS_BUG_ON(mas, pmt != maple_arange_64);
  1268. meta_offset = ma_meta_gap(pnode);
  1269. meta_gap = pgaps[meta_offset];
  1270. pgaps[offset] = new;
  1271. if (meta_gap == new)
  1272. return;
  1273. if (offset != meta_offset) {
  1274. if (meta_gap > new)
  1275. return;
  1276. ma_set_meta_gap(pnode, pmt, offset);
  1277. } else if (new < meta_gap) {
  1278. new = ma_max_gap(pnode, pgaps, pmt, &meta_offset);
  1279. ma_set_meta_gap(pnode, pmt, meta_offset);
  1280. }
  1281. if (ma_is_root(pnode))
  1282. return;
  1283. /* Go to the parent node. */
  1284. pnode = mte_parent(penode);
  1285. pmt = mas_parent_type(mas, penode);
  1286. pgaps = ma_gaps(pnode, pmt);
  1287. offset = mte_parent_slot(penode);
  1288. penode = mt_mk_node(pnode, pmt);
  1289. goto ascend;
  1290. }
  1291. /*
  1292. * mas_update_gap() - Update a nodes gaps and propagate up if necessary.
  1293. * @mas: the maple state.
  1294. */
  1295. static inline void mas_update_gap(struct ma_state *mas)
  1296. {
  1297. unsigned char pslot;
  1298. unsigned long p_gap;
  1299. unsigned long max_gap;
  1300. if (!mt_is_alloc(mas->tree))
  1301. return;
  1302. if (mte_is_root(mas->node))
  1303. return;
  1304. max_gap = mas_max_gap(mas);
  1305. pslot = mte_parent_slot(mas->node);
  1306. p_gap = ma_gaps(mte_parent(mas->node),
  1307. mas_parent_type(mas, mas->node))[pslot];
  1308. if (p_gap != max_gap)
  1309. mas_parent_gap(mas, pslot, max_gap);
  1310. }
  1311. /*
  1312. * mas_adopt_children() - Set the parent pointer of all nodes in @parent to
  1313. * @parent with the slot encoded.
  1314. * @mas: the maple state (for the tree)
  1315. * @parent: the maple encoded node containing the children.
  1316. */
  1317. static inline void mas_adopt_children(struct ma_state *mas,
  1318. struct maple_enode *parent)
  1319. {
  1320. enum maple_type type = mte_node_type(parent);
  1321. struct maple_node *node = mte_to_node(parent);
  1322. void __rcu **slots = ma_slots(node, type);
  1323. unsigned long *pivots = ma_pivots(node, type);
  1324. struct maple_enode *child;
  1325. unsigned char offset;
  1326. offset = ma_data_end(node, type, pivots, mas->max);
  1327. do {
  1328. child = mas_slot_locked(mas, slots, offset);
  1329. mas_set_parent(mas, child, parent, offset);
  1330. } while (offset--);
  1331. }
  1332. /*
  1333. * mas_put_in_tree() - Put a new node in the tree, smp_wmb(), and mark the old
  1334. * node as dead.
  1335. * @mas: the maple state with the new node
  1336. * @old_enode: The old maple encoded node to replace.
  1337. * @new_height: if we are inserting a root node, update the height of the tree
  1338. */
  1339. static inline void mas_put_in_tree(struct ma_state *mas,
  1340. struct maple_enode *old_enode, char new_height)
  1341. __must_hold(mas->tree->ma_lock)
  1342. {
  1343. unsigned char offset;
  1344. void __rcu **slots;
  1345. if (mte_is_root(mas->node)) {
  1346. mas_mn(mas)->parent = ma_parent_ptr(mas_tree_parent(mas));
  1347. rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
  1348. mt_set_height(mas->tree, new_height);
  1349. } else {
  1350. offset = mte_parent_slot(mas->node);
  1351. slots = ma_slots(mte_parent(mas->node),
  1352. mas_parent_type(mas, mas->node));
  1353. rcu_assign_pointer(slots[offset], mas->node);
  1354. }
  1355. mte_set_node_dead(old_enode);
  1356. }
  1357. /*
  1358. * mas_replace_node() - Replace a node by putting it in the tree, marking it
  1359. * dead, and freeing it.
  1360. * the parent encoding to locate the maple node in the tree.
  1361. * @mas: the ma_state with @mas->node pointing to the new node.
  1362. * @old_enode: The old maple encoded node.
  1363. * @new_height: The new height of the tree as a result of the operation
  1364. */
  1365. static inline void mas_replace_node(struct ma_state *mas,
  1366. struct maple_enode *old_enode, unsigned char new_height)
  1367. __must_hold(mas->tree->ma_lock)
  1368. {
  1369. mas_put_in_tree(mas, old_enode, new_height);
  1370. mas_free(mas, old_enode);
  1371. }
  1372. /*
  1373. * mas_find_child() - Find a child who has the parent @mas->node.
  1374. * @mas: the maple state with the parent.
  1375. * @child: the maple state to store the child.
  1376. */
  1377. static inline bool mas_find_child(struct ma_state *mas, struct ma_state *child)
  1378. __must_hold(mas->tree->ma_lock)
  1379. {
  1380. enum maple_type mt;
  1381. unsigned char offset;
  1382. unsigned char end;
  1383. unsigned long *pivots;
  1384. struct maple_enode *entry;
  1385. struct maple_node *node;
  1386. void __rcu **slots;
  1387. mt = mte_node_type(mas->node);
  1388. node = mas_mn(mas);
  1389. slots = ma_slots(node, mt);
  1390. pivots = ma_pivots(node, mt);
  1391. end = ma_data_end(node, mt, pivots, mas->max);
  1392. for (offset = mas->offset; offset <= end; offset++) {
  1393. entry = mas_slot_locked(mas, slots, offset);
  1394. if (mte_parent(entry) == node) {
  1395. *child = *mas;
  1396. mas->offset = offset + 1;
  1397. child->offset = offset;
  1398. mas_descend(child);
  1399. child->offset = 0;
  1400. return true;
  1401. }
  1402. }
  1403. return false;
  1404. }
  1405. /*
  1406. * mab_shift_right() - Shift the data in mab right. Note, does not clean out the
  1407. * old data or set b_node->b_end.
  1408. * @b_node: the maple_big_node
  1409. * @shift: the shift count
  1410. */
  1411. static inline void mab_shift_right(struct maple_big_node *b_node,
  1412. unsigned char shift)
  1413. {
  1414. unsigned long size = b_node->b_end * sizeof(unsigned long);
  1415. memmove(b_node->pivot + shift, b_node->pivot, size);
  1416. memmove(b_node->slot + shift, b_node->slot, size);
  1417. if (b_node->type == maple_arange_64)
  1418. memmove(b_node->gap + shift, b_node->gap, size);
  1419. }
  1420. /*
  1421. * mab_middle_node() - Check if a middle node is needed (unlikely)
  1422. * @b_node: the maple_big_node that contains the data.
  1423. * @split: the potential split location
  1424. * @slot_count: the size that can be stored in a single node being considered.
  1425. *
  1426. * Return: true if a middle node is required.
  1427. */
  1428. static inline bool mab_middle_node(struct maple_big_node *b_node, int split,
  1429. unsigned char slot_count)
  1430. {
  1431. unsigned char size = b_node->b_end;
  1432. if (size >= 2 * slot_count)
  1433. return true;
  1434. if (!b_node->slot[split] && (size >= 2 * slot_count - 1))
  1435. return true;
  1436. return false;
  1437. }
  1438. /*
  1439. * mab_no_null_split() - ensure the split doesn't fall on a NULL
  1440. * @b_node: the maple_big_node with the data
  1441. * @split: the suggested split location
  1442. * @slot_count: the number of slots in the node being considered.
  1443. *
  1444. * Return: the split location.
  1445. */
  1446. static inline int mab_no_null_split(struct maple_big_node *b_node,
  1447. unsigned char split, unsigned char slot_count)
  1448. {
  1449. if (!b_node->slot[split]) {
  1450. /*
  1451. * If the split is less than the max slot && the right side will
  1452. * still be sufficient, then increment the split on NULL.
  1453. */
  1454. if ((split < slot_count - 1) &&
  1455. (b_node->b_end - split) > (mt_min_slots[b_node->type]))
  1456. split++;
  1457. else
  1458. split--;
  1459. }
  1460. return split;
  1461. }
  1462. /*
  1463. * mab_calc_split() - Calculate the split location and if there needs to be two
  1464. * splits.
  1465. * @mas: The maple state
  1466. * @bn: The maple_big_node with the data
  1467. * @mid_split: The second split, if required. 0 otherwise.
  1468. *
  1469. * Return: The first split location. The middle split is set in @mid_split.
  1470. */
  1471. static inline int mab_calc_split(struct ma_state *mas,
  1472. struct maple_big_node *bn, unsigned char *mid_split)
  1473. {
  1474. unsigned char b_end = bn->b_end;
  1475. int split = b_end / 2; /* Assume equal split. */
  1476. unsigned char slot_count = mt_slots[bn->type];
  1477. /*
  1478. * To support gap tracking, all NULL entries are kept together and a node cannot
  1479. * end on a NULL entry, with the exception of the left-most leaf. The
  1480. * limitation means that the split of a node must be checked for this condition
  1481. * and be able to put more data in one direction or the other.
  1482. *
  1483. * Although extremely rare, it is possible to enter what is known as the 3-way
  1484. * split scenario. The 3-way split comes about by means of a store of a range
  1485. * that overwrites the end and beginning of two full nodes. The result is a set
  1486. * of entries that cannot be stored in 2 nodes. Sometimes, these two nodes can
  1487. * also be located in different parent nodes which are also full. This can
  1488. * carry upwards all the way to the root in the worst case.
  1489. */
  1490. if (unlikely(mab_middle_node(bn, split, slot_count))) {
  1491. split = b_end / 3;
  1492. *mid_split = split * 2;
  1493. } else {
  1494. *mid_split = 0;
  1495. }
  1496. /* Avoid ending a node on a NULL entry */
  1497. split = mab_no_null_split(bn, split, slot_count);
  1498. if (unlikely(*mid_split))
  1499. *mid_split = mab_no_null_split(bn, *mid_split, slot_count);
  1500. return split;
  1501. }
  1502. /*
  1503. * mas_mab_cp() - Copy data from a maple state inclusively to a maple_big_node
  1504. * and set @b_node->b_end to the next free slot.
  1505. * @mas: The maple state
  1506. * @mas_start: The starting slot to copy
  1507. * @mas_end: The end slot to copy (inclusively)
  1508. * @b_node: The maple_big_node to place the data
  1509. * @mab_start: The starting location in maple_big_node to store the data.
  1510. */
  1511. static inline void mas_mab_cp(struct ma_state *mas, unsigned char mas_start,
  1512. unsigned char mas_end, struct maple_big_node *b_node,
  1513. unsigned char mab_start)
  1514. {
  1515. enum maple_type mt;
  1516. struct maple_node *node;
  1517. void __rcu **slots;
  1518. unsigned long *pivots, *gaps;
  1519. int i = mas_start, j = mab_start;
  1520. unsigned char piv_end;
  1521. node = mas_mn(mas);
  1522. mt = mte_node_type(mas->node);
  1523. pivots = ma_pivots(node, mt);
  1524. if (!i) {
  1525. b_node->pivot[j] = pivots[i++];
  1526. if (unlikely(i > mas_end))
  1527. goto complete;
  1528. j++;
  1529. }
  1530. piv_end = min(mas_end, mt_pivots[mt]);
  1531. for (; i < piv_end; i++, j++) {
  1532. b_node->pivot[j] = pivots[i];
  1533. if (unlikely(!b_node->pivot[j]))
  1534. goto complete;
  1535. if (unlikely(mas->max == b_node->pivot[j]))
  1536. goto complete;
  1537. }
  1538. b_node->pivot[j] = mas_safe_pivot(mas, pivots, i, mt);
  1539. complete:
  1540. b_node->b_end = ++j;
  1541. j -= mab_start;
  1542. slots = ma_slots(node, mt);
  1543. memcpy(b_node->slot + mab_start, slots + mas_start, sizeof(void *) * j);
  1544. if (!ma_is_leaf(mt) && mt_is_alloc(mas->tree)) {
  1545. gaps = ma_gaps(node, mt);
  1546. memcpy(b_node->gap + mab_start, gaps + mas_start,
  1547. sizeof(unsigned long) * j);
  1548. }
  1549. }
  1550. /*
  1551. * mas_leaf_set_meta() - Set the metadata of a leaf if possible.
  1552. * @node: The maple node
  1553. * @mt: The maple type
  1554. * @end: The node end
  1555. */
  1556. static inline void mas_leaf_set_meta(struct maple_node *node,
  1557. enum maple_type mt, unsigned char end)
  1558. {
  1559. if (end < mt_slots[mt] - 1)
  1560. ma_set_meta(node, mt, 0, end);
  1561. }
  1562. /*
  1563. * mab_mas_cp() - Copy data from maple_big_node to a maple encoded node.
  1564. * @b_node: the maple_big_node that has the data
  1565. * @mab_start: the start location in @b_node.
  1566. * @mab_end: The end location in @b_node (inclusively)
  1567. * @mas: The maple state with the maple encoded node.
  1568. */
  1569. static inline void mab_mas_cp(struct maple_big_node *b_node,
  1570. unsigned char mab_start, unsigned char mab_end,
  1571. struct ma_state *mas, bool new_max)
  1572. {
  1573. int i, j = 0;
  1574. enum maple_type mt = mte_node_type(mas->node);
  1575. struct maple_node *node = mte_to_node(mas->node);
  1576. void __rcu **slots = ma_slots(node, mt);
  1577. unsigned long *pivots = ma_pivots(node, mt);
  1578. unsigned long *gaps = NULL;
  1579. unsigned char end;
  1580. if (mab_end - mab_start > mt_pivots[mt])
  1581. mab_end--;
  1582. if (!pivots[mt_pivots[mt] - 1])
  1583. slots[mt_pivots[mt]] = NULL;
  1584. i = mab_start;
  1585. do {
  1586. pivots[j++] = b_node->pivot[i++];
  1587. } while (i <= mab_end && likely(b_node->pivot[i]));
  1588. memcpy(slots, b_node->slot + mab_start,
  1589. sizeof(void *) * (i - mab_start));
  1590. if (new_max)
  1591. mas->max = b_node->pivot[i - 1];
  1592. end = j - 1;
  1593. if (likely(!ma_is_leaf(mt) && mt_is_alloc(mas->tree))) {
  1594. unsigned long max_gap = 0;
  1595. unsigned char offset = 0;
  1596. gaps = ma_gaps(node, mt);
  1597. do {
  1598. gaps[--j] = b_node->gap[--i];
  1599. if (gaps[j] > max_gap) {
  1600. offset = j;
  1601. max_gap = gaps[j];
  1602. }
  1603. } while (j);
  1604. ma_set_meta(node, mt, offset, end);
  1605. } else {
  1606. mas_leaf_set_meta(node, mt, end);
  1607. }
  1608. }
  1609. /*
  1610. * mas_store_b_node() - Store an @entry into the b_node while also copying the
  1611. * data from a maple encoded node.
  1612. * @wr_mas: the maple write state
  1613. * @b_node: the maple_big_node to fill with data
  1614. * @offset_end: the offset to end copying
  1615. *
  1616. * Return: The actual end of the data stored in @b_node
  1617. */
  1618. static noinline_for_kasan void mas_store_b_node(struct ma_wr_state *wr_mas,
  1619. struct maple_big_node *b_node, unsigned char offset_end)
  1620. {
  1621. unsigned char slot;
  1622. unsigned char b_end;
  1623. /* Possible underflow of piv will wrap back to 0 before use. */
  1624. unsigned long piv;
  1625. struct ma_state *mas = wr_mas->mas;
  1626. b_node->type = wr_mas->type;
  1627. b_end = 0;
  1628. slot = mas->offset;
  1629. if (slot) {
  1630. /* Copy start data up to insert. */
  1631. mas_mab_cp(mas, 0, slot - 1, b_node, 0);
  1632. b_end = b_node->b_end;
  1633. piv = b_node->pivot[b_end - 1];
  1634. } else
  1635. piv = mas->min - 1;
  1636. if (piv + 1 < mas->index) {
  1637. /* Handle range starting after old range */
  1638. b_node->slot[b_end] = wr_mas->content;
  1639. if (!wr_mas->content)
  1640. b_node->gap[b_end] = mas->index - 1 - piv;
  1641. b_node->pivot[b_end++] = mas->index - 1;
  1642. }
  1643. /* Store the new entry. */
  1644. mas->offset = b_end;
  1645. b_node->slot[b_end] = wr_mas->entry;
  1646. b_node->pivot[b_end] = mas->last;
  1647. /* Appended. */
  1648. if (mas->last >= mas->max)
  1649. goto b_end;
  1650. /* Handle new range ending before old range ends */
  1651. piv = mas_safe_pivot(mas, wr_mas->pivots, offset_end, wr_mas->type);
  1652. if (piv > mas->last) {
  1653. if (offset_end != slot)
  1654. wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
  1655. offset_end);
  1656. b_node->slot[++b_end] = wr_mas->content;
  1657. if (!wr_mas->content)
  1658. b_node->gap[b_end] = piv - mas->last + 1;
  1659. b_node->pivot[b_end] = piv;
  1660. }
  1661. slot = offset_end + 1;
  1662. if (slot > mas->end)
  1663. goto b_end;
  1664. /* Copy end data to the end of the node. */
  1665. mas_mab_cp(mas, slot, mas->end + 1, b_node, ++b_end);
  1666. b_node->b_end--;
  1667. return;
  1668. b_end:
  1669. b_node->b_end = b_end;
  1670. }
  1671. /*
  1672. * mas_prev_sibling() - Find the previous node with the same parent.
  1673. * @mas: the maple state
  1674. *
  1675. * Return: True if there is a previous sibling, false otherwise.
  1676. */
  1677. static inline bool mas_prev_sibling(struct ma_state *mas)
  1678. {
  1679. unsigned int p_slot = mte_parent_slot(mas->node);
  1680. /* For root node, p_slot is set to 0 by mte_parent_slot(). */
  1681. if (!p_slot)
  1682. return false;
  1683. mas_ascend(mas);
  1684. mas->offset = p_slot - 1;
  1685. mas_descend(mas);
  1686. return true;
  1687. }
  1688. /*
  1689. * mas_next_sibling() - Find the next node with the same parent.
  1690. * @mas: the maple state
  1691. *
  1692. * Return: true if there is a next sibling, false otherwise.
  1693. */
  1694. static inline bool mas_next_sibling(struct ma_state *mas)
  1695. {
  1696. MA_STATE(parent, mas->tree, mas->index, mas->last);
  1697. if (mte_is_root(mas->node))
  1698. return false;
  1699. parent = *mas;
  1700. mas_ascend(&parent);
  1701. parent.offset = mte_parent_slot(mas->node) + 1;
  1702. if (parent.offset > mas_data_end(&parent))
  1703. return false;
  1704. *mas = parent;
  1705. mas_descend(mas);
  1706. return true;
  1707. }
  1708. /*
  1709. * mas_node_or_none() - Set the enode and state.
  1710. * @mas: the maple state
  1711. * @enode: The encoded maple node.
  1712. *
  1713. * Set the node to the enode and the status.
  1714. */
  1715. static inline void mas_node_or_none(struct ma_state *mas,
  1716. struct maple_enode *enode)
  1717. {
  1718. if (enode) {
  1719. mas->node = enode;
  1720. mas->status = ma_active;
  1721. } else {
  1722. mas->node = NULL;
  1723. mas->status = ma_none;
  1724. }
  1725. }
  1726. /*
  1727. * mas_wr_node_walk() - Find the correct offset for the index in the @mas.
  1728. * If @mas->index cannot be found within the containing
  1729. * node, we traverse to the last entry in the node.
  1730. * @wr_mas: The maple write state
  1731. *
  1732. * Uses mas_slot_locked() and does not need to worry about dead nodes.
  1733. */
  1734. static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
  1735. {
  1736. struct ma_state *mas = wr_mas->mas;
  1737. unsigned char count, offset;
  1738. if (unlikely(ma_is_dense(wr_mas->type))) {
  1739. wr_mas->r_max = wr_mas->r_min = mas->index;
  1740. mas->offset = mas->index = mas->min;
  1741. return;
  1742. }
  1743. wr_mas->node = mas_mn(wr_mas->mas);
  1744. wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
  1745. count = mas->end = ma_data_end(wr_mas->node, wr_mas->type,
  1746. wr_mas->pivots, mas->max);
  1747. offset = mas->offset;
  1748. while (offset < count && mas->index > wr_mas->pivots[offset])
  1749. offset++;
  1750. wr_mas->r_max = offset < count ? wr_mas->pivots[offset] : mas->max;
  1751. wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, offset);
  1752. wr_mas->offset_end = mas->offset = offset;
  1753. }
  1754. /*
  1755. * mast_rebalance_next() - Rebalance against the next node
  1756. * @mast: The maple subtree state
  1757. */
  1758. static inline void mast_rebalance_next(struct maple_subtree_state *mast)
  1759. {
  1760. unsigned char b_end = mast->bn->b_end;
  1761. mas_mab_cp(mast->orig_r, 0, mt_slot_count(mast->orig_r->node),
  1762. mast->bn, b_end);
  1763. mast->orig_r->last = mast->orig_r->max;
  1764. }
  1765. /*
  1766. * mast_rebalance_prev() - Rebalance against the previous node
  1767. * @mast: The maple subtree state
  1768. */
  1769. static inline void mast_rebalance_prev(struct maple_subtree_state *mast)
  1770. {
  1771. unsigned char end = mas_data_end(mast->orig_l) + 1;
  1772. unsigned char b_end = mast->bn->b_end;
  1773. mab_shift_right(mast->bn, end);
  1774. mas_mab_cp(mast->orig_l, 0, end - 1, mast->bn, 0);
  1775. mast->l->min = mast->orig_l->min;
  1776. mast->orig_l->index = mast->orig_l->min;
  1777. mast->bn->b_end = end + b_end;
  1778. mast->l->offset += end;
  1779. }
  1780. /*
  1781. * mast_spanning_rebalance() - Rebalance nodes with nearest neighbour favouring
  1782. * the node to the right. Checking the nodes to the right then the left at each
  1783. * level upwards until root is reached.
  1784. * Data is copied into the @mast->bn.
  1785. * @mast: The maple_subtree_state.
  1786. */
  1787. static inline
  1788. bool mast_spanning_rebalance(struct maple_subtree_state *mast)
  1789. {
  1790. struct ma_state r_tmp = *mast->orig_r;
  1791. struct ma_state l_tmp = *mast->orig_l;
  1792. unsigned char depth = 0;
  1793. do {
  1794. mas_ascend(mast->orig_r);
  1795. mas_ascend(mast->orig_l);
  1796. depth++;
  1797. if (mast->orig_r->offset < mas_data_end(mast->orig_r)) {
  1798. mast->orig_r->offset++;
  1799. do {
  1800. mas_descend(mast->orig_r);
  1801. mast->orig_r->offset = 0;
  1802. } while (--depth);
  1803. mast_rebalance_next(mast);
  1804. *mast->orig_l = l_tmp;
  1805. return true;
  1806. } else if (mast->orig_l->offset != 0) {
  1807. mast->orig_l->offset--;
  1808. do {
  1809. mas_descend(mast->orig_l);
  1810. mast->orig_l->offset =
  1811. mas_data_end(mast->orig_l);
  1812. } while (--depth);
  1813. mast_rebalance_prev(mast);
  1814. *mast->orig_r = r_tmp;
  1815. return true;
  1816. }
  1817. } while (!mte_is_root(mast->orig_r->node));
  1818. *mast->orig_r = r_tmp;
  1819. *mast->orig_l = l_tmp;
  1820. return false;
  1821. }
  1822. /*
  1823. * mast_ascend() - Ascend the original left and right maple states.
  1824. * @mast: the maple subtree state.
  1825. *
  1826. * Ascend the original left and right sides. Set the offsets to point to the
  1827. * data already in the new tree (@mast->l and @mast->r).
  1828. */
  1829. static inline void mast_ascend(struct maple_subtree_state *mast)
  1830. {
  1831. MA_WR_STATE(wr_mas, mast->orig_r, NULL);
  1832. mas_ascend(mast->orig_l);
  1833. mas_ascend(mast->orig_r);
  1834. mast->orig_r->offset = 0;
  1835. mast->orig_r->index = mast->r->max;
  1836. /* last should be larger than or equal to index */
  1837. if (mast->orig_r->last < mast->orig_r->index)
  1838. mast->orig_r->last = mast->orig_r->index;
  1839. wr_mas.type = mte_node_type(mast->orig_r->node);
  1840. mas_wr_node_walk(&wr_mas);
  1841. /* Set up the left side of things */
  1842. mast->orig_l->offset = 0;
  1843. mast->orig_l->index = mast->l->min;
  1844. wr_mas.mas = mast->orig_l;
  1845. wr_mas.type = mte_node_type(mast->orig_l->node);
  1846. mas_wr_node_walk(&wr_mas);
  1847. mast->bn->type = wr_mas.type;
  1848. }
  1849. /*
  1850. * mas_new_ma_node() - Create and return a new maple node. Helper function.
  1851. * @mas: the maple state with the allocations.
  1852. * @b_node: the maple_big_node with the type encoding.
  1853. *
  1854. * Use the node type from the maple_big_node to allocate a new node from the
  1855. * ma_state. This function exists mainly for code readability.
  1856. *
  1857. * Return: A new maple encoded node
  1858. */
  1859. static inline struct maple_enode
  1860. *mas_new_ma_node(struct ma_state *mas, struct maple_big_node *b_node)
  1861. {
  1862. return mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)), b_node->type);
  1863. }
  1864. /*
  1865. * mas_mab_to_node() - Set up right and middle nodes
  1866. *
  1867. * @mas: the maple state that contains the allocations.
  1868. * @b_node: the node which contains the data.
  1869. * @left: The pointer which will have the left node
  1870. * @right: The pointer which may have the right node
  1871. * @middle: the pointer which may have the middle node (rare)
  1872. * @mid_split: the split location for the middle node
  1873. *
  1874. * Return: the split of left.
  1875. */
  1876. static inline unsigned char mas_mab_to_node(struct ma_state *mas,
  1877. struct maple_big_node *b_node, struct maple_enode **left,
  1878. struct maple_enode **right, struct maple_enode **middle,
  1879. unsigned char *mid_split)
  1880. {
  1881. unsigned char split = 0;
  1882. unsigned char slot_count = mt_slots[b_node->type];
  1883. *left = mas_new_ma_node(mas, b_node);
  1884. *right = NULL;
  1885. *middle = NULL;
  1886. *mid_split = 0;
  1887. if (b_node->b_end < slot_count) {
  1888. split = b_node->b_end;
  1889. } else {
  1890. split = mab_calc_split(mas, b_node, mid_split);
  1891. *right = mas_new_ma_node(mas, b_node);
  1892. }
  1893. if (*mid_split)
  1894. *middle = mas_new_ma_node(mas, b_node);
  1895. return split;
  1896. }
  1897. /*
  1898. * mab_set_b_end() - Add entry to b_node at b_node->b_end and increment the end
  1899. * pointer.
  1900. * @b_node: the big node to add the entry
  1901. * @mas: the maple state to get the pivot (mas->max)
  1902. * @entry: the entry to add, if NULL nothing happens.
  1903. */
  1904. static inline void mab_set_b_end(struct maple_big_node *b_node,
  1905. struct ma_state *mas,
  1906. void *entry)
  1907. {
  1908. if (!entry)
  1909. return;
  1910. b_node->slot[b_node->b_end] = entry;
  1911. if (mt_is_alloc(mas->tree))
  1912. b_node->gap[b_node->b_end] = mas_max_gap(mas);
  1913. b_node->pivot[b_node->b_end++] = mas->max;
  1914. }
  1915. /*
  1916. * mas_set_split_parent() - combine_then_separate helper function. Sets the parent
  1917. * of @mas->node to either @left or @right, depending on @slot and @split
  1918. *
  1919. * @mas: the maple state with the node that needs a parent
  1920. * @left: possible parent 1
  1921. * @right: possible parent 2
  1922. * @slot: the slot the mas->node was placed
  1923. * @split: the split location between @left and @right
  1924. */
  1925. static inline void mas_set_split_parent(struct ma_state *mas,
  1926. struct maple_enode *left,
  1927. struct maple_enode *right,
  1928. unsigned char *slot, unsigned char split)
  1929. {
  1930. if (mas_is_none(mas))
  1931. return;
  1932. if ((*slot) <= split)
  1933. mas_set_parent(mas, mas->node, left, *slot);
  1934. else if (right)
  1935. mas_set_parent(mas, mas->node, right, (*slot) - split - 1);
  1936. (*slot)++;
  1937. }
  1938. /*
  1939. * mte_mid_split_check() - Check if the next node passes the mid-split
  1940. * @l: Pointer to left encoded maple node.
  1941. * @m: Pointer to middle encoded maple node.
  1942. * @r: Pointer to right encoded maple node.
  1943. * @slot: The offset
  1944. * @split: The split location.
  1945. * @mid_split: The middle split.
  1946. */
  1947. static inline void mte_mid_split_check(struct maple_enode **l,
  1948. struct maple_enode **r,
  1949. struct maple_enode *right,
  1950. unsigned char slot,
  1951. unsigned char *split,
  1952. unsigned char mid_split)
  1953. {
  1954. if (*r == right)
  1955. return;
  1956. if (slot < mid_split)
  1957. return;
  1958. *l = *r;
  1959. *r = right;
  1960. *split = mid_split;
  1961. }
  1962. /*
  1963. * mast_set_split_parents() - Helper function to set three nodes parents. Slot
  1964. * is taken from @mast->l.
  1965. * @mast: the maple subtree state
  1966. * @left: the left node
  1967. * @right: the right node
  1968. * @split: the split location.
  1969. */
  1970. static inline void mast_set_split_parents(struct maple_subtree_state *mast,
  1971. struct maple_enode *left,
  1972. struct maple_enode *middle,
  1973. struct maple_enode *right,
  1974. unsigned char split,
  1975. unsigned char mid_split)
  1976. {
  1977. unsigned char slot;
  1978. struct maple_enode *l = left;
  1979. struct maple_enode *r = right;
  1980. if (mas_is_none(mast->l))
  1981. return;
  1982. if (middle)
  1983. r = middle;
  1984. slot = mast->l->offset;
  1985. mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
  1986. mas_set_split_parent(mast->l, l, r, &slot, split);
  1987. mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
  1988. mas_set_split_parent(mast->m, l, r, &slot, split);
  1989. mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
  1990. mas_set_split_parent(mast->r, l, r, &slot, split);
  1991. }
  1992. /*
  1993. * mas_topiary_node() - Dispose of a single node
  1994. * @mas: The maple state for pushing nodes
  1995. * @in_rcu: If the tree is in rcu mode
  1996. *
  1997. * The node will either be RCU freed or pushed back on the maple state.
  1998. */
  1999. static inline void mas_topiary_node(struct ma_state *mas,
  2000. struct ma_state *tmp_mas, bool in_rcu)
  2001. {
  2002. struct maple_node *tmp;
  2003. struct maple_enode *enode;
  2004. if (mas_is_none(tmp_mas))
  2005. return;
  2006. enode = tmp_mas->node;
  2007. tmp = mte_to_node(enode);
  2008. mte_set_node_dead(enode);
  2009. ma_free_rcu(tmp);
  2010. }
  2011. /*
  2012. * mas_topiary_replace() - Replace the data with new data, then repair the
  2013. * parent links within the new tree. Iterate over the dead sub-tree and collect
  2014. * the dead subtrees and topiary the nodes that are no longer of use.
  2015. *
  2016. * The new tree will have up to three children with the correct parent. Keep
  2017. * track of the new entries as they need to be followed to find the next level
  2018. * of new entries.
  2019. *
  2020. * The old tree will have up to three children with the old parent. Keep track
  2021. * of the old entries as they may have more nodes below replaced. Nodes within
  2022. * [index, last] are dead subtrees, others need to be freed and followed.
  2023. *
  2024. * @mas: The maple state pointing at the new data
  2025. * @old_enode: The maple encoded node being replaced
  2026. * @new_height: The new height of the tree as a result of the operation
  2027. *
  2028. */
  2029. static inline void mas_topiary_replace(struct ma_state *mas,
  2030. struct maple_enode *old_enode, unsigned char new_height)
  2031. {
  2032. struct ma_state tmp[3], tmp_next[3];
  2033. MA_TOPIARY(subtrees, mas->tree);
  2034. bool in_rcu;
  2035. int i, n;
  2036. /* Place data in tree & then mark node as old */
  2037. mas_put_in_tree(mas, old_enode, new_height);
  2038. /* Update the parent pointers in the tree */
  2039. tmp[0] = *mas;
  2040. tmp[0].offset = 0;
  2041. tmp[1].status = ma_none;
  2042. tmp[2].status = ma_none;
  2043. while (!mte_is_leaf(tmp[0].node)) {
  2044. n = 0;
  2045. for (i = 0; i < 3; i++) {
  2046. if (mas_is_none(&tmp[i]))
  2047. continue;
  2048. while (n < 3) {
  2049. if (!mas_find_child(&tmp[i], &tmp_next[n]))
  2050. break;
  2051. n++;
  2052. }
  2053. mas_adopt_children(&tmp[i], tmp[i].node);
  2054. }
  2055. if (MAS_WARN_ON(mas, n == 0))
  2056. break;
  2057. while (n < 3)
  2058. tmp_next[n++].status = ma_none;
  2059. for (i = 0; i < 3; i++)
  2060. tmp[i] = tmp_next[i];
  2061. }
  2062. /* Collect the old nodes that need to be discarded */
  2063. if (mte_is_leaf(old_enode))
  2064. return mas_free(mas, old_enode);
  2065. tmp[0] = *mas;
  2066. tmp[0].offset = 0;
  2067. tmp[0].node = old_enode;
  2068. tmp[1].status = ma_none;
  2069. tmp[2].status = ma_none;
  2070. in_rcu = mt_in_rcu(mas->tree);
  2071. do {
  2072. n = 0;
  2073. for (i = 0; i < 3; i++) {
  2074. if (mas_is_none(&tmp[i]))
  2075. continue;
  2076. while (n < 3) {
  2077. if (!mas_find_child(&tmp[i], &tmp_next[n]))
  2078. break;
  2079. if ((tmp_next[n].min >= tmp_next->index) &&
  2080. (tmp_next[n].max <= tmp_next->last)) {
  2081. mat_add(&subtrees, tmp_next[n].node);
  2082. tmp_next[n].status = ma_none;
  2083. } else {
  2084. n++;
  2085. }
  2086. }
  2087. }
  2088. if (MAS_WARN_ON(mas, n == 0))
  2089. break;
  2090. while (n < 3)
  2091. tmp_next[n++].status = ma_none;
  2092. for (i = 0; i < 3; i++) {
  2093. mas_topiary_node(mas, &tmp[i], in_rcu);
  2094. tmp[i] = tmp_next[i];
  2095. }
  2096. } while (!mte_is_leaf(tmp[0].node));
  2097. for (i = 0; i < 3; i++)
  2098. mas_topiary_node(mas, &tmp[i], in_rcu);
  2099. mas_mat_destroy(mas, &subtrees);
  2100. }
  2101. /*
  2102. * mas_wmb_replace() - Write memory barrier and replace
  2103. * @mas: The maple state
  2104. * @old_enode: The old maple encoded node that is being replaced.
  2105. * @new_height: The new height of the tree as a result of the operation
  2106. *
  2107. * Updates gap as necessary.
  2108. */
  2109. static inline void mas_wmb_replace(struct ma_state *mas,
  2110. struct maple_enode *old_enode, unsigned char new_height)
  2111. {
  2112. /* Insert the new data in the tree */
  2113. mas_topiary_replace(mas, old_enode, new_height);
  2114. if (mte_is_leaf(mas->node))
  2115. return;
  2116. mas_update_gap(mas);
  2117. }
  2118. /*
  2119. * mast_cp_to_nodes() - Copy data out to nodes.
  2120. * @mast: The maple subtree state
  2121. * @left: The left encoded maple node
  2122. * @middle: The middle encoded maple node
  2123. * @right: The right encoded maple node
  2124. * @split: The location to split between left and (middle ? middle : right)
  2125. * @mid_split: The location to split between middle and right.
  2126. */
  2127. static inline void mast_cp_to_nodes(struct maple_subtree_state *mast,
  2128. struct maple_enode *left, struct maple_enode *middle,
  2129. struct maple_enode *right, unsigned char split, unsigned char mid_split)
  2130. {
  2131. bool new_lmax = true;
  2132. mas_node_or_none(mast->l, left);
  2133. mas_node_or_none(mast->m, middle);
  2134. mas_node_or_none(mast->r, right);
  2135. mast->l->min = mast->orig_l->min;
  2136. if (split == mast->bn->b_end) {
  2137. mast->l->max = mast->orig_r->max;
  2138. new_lmax = false;
  2139. }
  2140. mab_mas_cp(mast->bn, 0, split, mast->l, new_lmax);
  2141. if (middle) {
  2142. mab_mas_cp(mast->bn, 1 + split, mid_split, mast->m, true);
  2143. mast->m->min = mast->bn->pivot[split] + 1;
  2144. split = mid_split;
  2145. }
  2146. mast->r->max = mast->orig_r->max;
  2147. if (right) {
  2148. mab_mas_cp(mast->bn, 1 + split, mast->bn->b_end, mast->r, false);
  2149. mast->r->min = mast->bn->pivot[split] + 1;
  2150. }
  2151. }
  2152. /*
  2153. * mast_combine_cp_left - Copy in the original left side of the tree into the
  2154. * combined data set in the maple subtree state big node.
  2155. * @mast: The maple subtree state
  2156. */
  2157. static inline void mast_combine_cp_left(struct maple_subtree_state *mast)
  2158. {
  2159. unsigned char l_slot = mast->orig_l->offset;
  2160. if (!l_slot)
  2161. return;
  2162. mas_mab_cp(mast->orig_l, 0, l_slot - 1, mast->bn, 0);
  2163. }
  2164. /*
  2165. * mast_combine_cp_right: Copy in the original right side of the tree into the
  2166. * combined data set in the maple subtree state big node.
  2167. * @mast: The maple subtree state
  2168. */
  2169. static inline void mast_combine_cp_right(struct maple_subtree_state *mast)
  2170. {
  2171. if (mast->bn->pivot[mast->bn->b_end - 1] >= mast->orig_r->max)
  2172. return;
  2173. mas_mab_cp(mast->orig_r, mast->orig_r->offset + 1,
  2174. mt_slot_count(mast->orig_r->node), mast->bn,
  2175. mast->bn->b_end);
  2176. mast->orig_r->last = mast->orig_r->max;
  2177. }
  2178. /*
  2179. * mast_sufficient: Check if the maple subtree state has enough data in the big
  2180. * node to create at least one sufficient node
  2181. * @mast: the maple subtree state
  2182. */
  2183. static inline bool mast_sufficient(struct maple_subtree_state *mast)
  2184. {
  2185. if (mast->bn->b_end > mt_min_slot_count(mast->orig_l->node))
  2186. return true;
  2187. return false;
  2188. }
  2189. /*
  2190. * mast_overflow: Check if there is too much data in the subtree state for a
  2191. * single node.
  2192. * @mast: The maple subtree state
  2193. */
  2194. static inline bool mast_overflow(struct maple_subtree_state *mast)
  2195. {
  2196. if (mast->bn->b_end > mt_slot_count(mast->orig_l->node))
  2197. return true;
  2198. return false;
  2199. }
  2200. static inline void *mtree_range_walk(struct ma_state *mas)
  2201. {
  2202. unsigned long *pivots;
  2203. unsigned char offset;
  2204. struct maple_node *node;
  2205. struct maple_enode *next, *last;
  2206. enum maple_type type;
  2207. void __rcu **slots;
  2208. unsigned char end;
  2209. unsigned long max, min;
  2210. unsigned long prev_max, prev_min;
  2211. next = mas->node;
  2212. min = mas->min;
  2213. max = mas->max;
  2214. do {
  2215. last = next;
  2216. node = mte_to_node(next);
  2217. type = mte_node_type(next);
  2218. pivots = ma_pivots(node, type);
  2219. end = ma_data_end(node, type, pivots, max);
  2220. prev_min = min;
  2221. prev_max = max;
  2222. if (pivots[0] >= mas->index) {
  2223. offset = 0;
  2224. max = pivots[0];
  2225. goto next;
  2226. }
  2227. offset = 1;
  2228. while (offset < end) {
  2229. if (pivots[offset] >= mas->index) {
  2230. max = pivots[offset];
  2231. break;
  2232. }
  2233. offset++;
  2234. }
  2235. min = pivots[offset - 1] + 1;
  2236. next:
  2237. slots = ma_slots(node, type);
  2238. next = mt_slot(mas->tree, slots, offset);
  2239. if (unlikely(ma_dead_node(node)))
  2240. goto dead_node;
  2241. } while (!ma_is_leaf(type));
  2242. mas->end = end;
  2243. mas->offset = offset;
  2244. mas->index = min;
  2245. mas->last = max;
  2246. mas->min = prev_min;
  2247. mas->max = prev_max;
  2248. mas->node = last;
  2249. return (void *)next;
  2250. dead_node:
  2251. mas_reset(mas);
  2252. return NULL;
  2253. }
  2254. /*
  2255. * mas_spanning_rebalance() - Rebalance across two nodes which may not be peers.
  2256. * @mas: The starting maple state
  2257. * @mast: The maple_subtree_state, keeps track of 4 maple states.
  2258. * @count: The estimated count of iterations needed.
  2259. *
  2260. * Follow the tree upwards from @l_mas and @r_mas for @count, or until the root
  2261. * is hit. First @b_node is split into two entries which are inserted into the
  2262. * next iteration of the loop. @b_node is returned populated with the final
  2263. * iteration. @mas is used to obtain allocations. orig_l_mas keeps track of the
  2264. * nodes that will remain active by using orig_l_mas->index and orig_l_mas->last
  2265. * to account of what has been copied into the new sub-tree. The update of
  2266. * orig_l_mas->last is used in mas_consume to find the slots that will need to
  2267. * be either freed or destroyed. orig_l_mas->depth keeps track of the height of
  2268. * the new sub-tree in case the sub-tree becomes the full tree.
  2269. */
  2270. static void mas_spanning_rebalance(struct ma_state *mas,
  2271. struct maple_subtree_state *mast, unsigned char count)
  2272. {
  2273. unsigned char split, mid_split;
  2274. unsigned char slot = 0;
  2275. unsigned char new_height = 0; /* used if node is a new root */
  2276. struct maple_enode *left = NULL, *middle = NULL, *right = NULL;
  2277. struct maple_enode *old_enode;
  2278. MA_STATE(l_mas, mas->tree, mas->index, mas->index);
  2279. MA_STATE(r_mas, mas->tree, mas->index, mas->last);
  2280. MA_STATE(m_mas, mas->tree, mas->index, mas->index);
  2281. /*
  2282. * The tree needs to be rebalanced and leaves need to be kept at the same level.
  2283. * Rebalancing is done by use of the ``struct maple_topiary``.
  2284. */
  2285. mast->l = &l_mas;
  2286. mast->m = &m_mas;
  2287. mast->r = &r_mas;
  2288. l_mas.status = r_mas.status = m_mas.status = ma_none;
  2289. /* Check if this is not root and has sufficient data. */
  2290. if (((mast->orig_l->min != 0) || (mast->orig_r->max != ULONG_MAX)) &&
  2291. unlikely(mast->bn->b_end <= mt_min_slots[mast->bn->type]))
  2292. mast_spanning_rebalance(mast);
  2293. /*
  2294. * Each level of the tree is examined and balanced, pushing data to the left or
  2295. * right, or rebalancing against left or right nodes is employed to avoid
  2296. * rippling up the tree to limit the amount of churn. Once a new sub-section of
  2297. * the tree is created, there may be a mix of new and old nodes. The old nodes
  2298. * will have the incorrect parent pointers and currently be in two trees: the
  2299. * original tree and the partially new tree. To remedy the parent pointers in
  2300. * the old tree, the new data is swapped into the active tree and a walk down
  2301. * the tree is performed and the parent pointers are updated.
  2302. * See mas_topiary_replace() for more information.
  2303. */
  2304. while (count--) {
  2305. mast->bn->b_end--;
  2306. mast->bn->type = mte_node_type(mast->orig_l->node);
  2307. split = mas_mab_to_node(mas, mast->bn, &left, &right, &middle,
  2308. &mid_split);
  2309. mast_set_split_parents(mast, left, middle, right, split,
  2310. mid_split);
  2311. mast_cp_to_nodes(mast, left, middle, right, split, mid_split);
  2312. new_height++;
  2313. /*
  2314. * Copy data from next level in the tree to mast->bn from next
  2315. * iteration
  2316. */
  2317. memset(mast->bn, 0, sizeof(struct maple_big_node));
  2318. mast->bn->type = mte_node_type(left);
  2319. /* Root already stored in l->node. */
  2320. if (mas_is_root_limits(mast->l))
  2321. goto new_root;
  2322. mast_ascend(mast);
  2323. mast_combine_cp_left(mast);
  2324. l_mas.offset = mast->bn->b_end;
  2325. mab_set_b_end(mast->bn, &l_mas, left);
  2326. mab_set_b_end(mast->bn, &m_mas, middle);
  2327. mab_set_b_end(mast->bn, &r_mas, right);
  2328. /* Copy anything necessary out of the right node. */
  2329. mast_combine_cp_right(mast);
  2330. mast->orig_l->last = mast->orig_l->max;
  2331. if (mast_sufficient(mast)) {
  2332. if (mast_overflow(mast))
  2333. continue;
  2334. if (mast->orig_l->node == mast->orig_r->node) {
  2335. /*
  2336. * The data in b_node should be stored in one
  2337. * node and in the tree
  2338. */
  2339. slot = mast->l->offset;
  2340. break;
  2341. }
  2342. continue;
  2343. }
  2344. /* May be a new root stored in mast->bn */
  2345. if (mas_is_root_limits(mast->orig_l))
  2346. break;
  2347. mast_spanning_rebalance(mast);
  2348. /* rebalancing from other nodes may require another loop. */
  2349. if (!count)
  2350. count++;
  2351. }
  2352. l_mas.node = mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)),
  2353. mte_node_type(mast->orig_l->node));
  2354. mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, &l_mas, true);
  2355. new_height++;
  2356. mas_set_parent(mas, left, l_mas.node, slot);
  2357. if (middle)
  2358. mas_set_parent(mas, middle, l_mas.node, ++slot);
  2359. if (right)
  2360. mas_set_parent(mas, right, l_mas.node, ++slot);
  2361. if (mas_is_root_limits(mast->l)) {
  2362. new_root:
  2363. mas_mn(mast->l)->parent = ma_parent_ptr(mas_tree_parent(mas));
  2364. while (!mte_is_root(mast->orig_l->node))
  2365. mast_ascend(mast);
  2366. } else {
  2367. mas_mn(&l_mas)->parent = mas_mn(mast->orig_l)->parent;
  2368. }
  2369. old_enode = mast->orig_l->node;
  2370. mas->depth = l_mas.depth;
  2371. mas->node = l_mas.node;
  2372. mas->min = l_mas.min;
  2373. mas->max = l_mas.max;
  2374. mas->offset = l_mas.offset;
  2375. mas_wmb_replace(mas, old_enode, new_height);
  2376. mtree_range_walk(mas);
  2377. return;
  2378. }
  2379. /*
  2380. * mas_rebalance() - Rebalance a given node.
  2381. * @mas: The maple state
  2382. * @b_node: The big maple node.
  2383. *
  2384. * Rebalance two nodes into a single node or two new nodes that are sufficient.
  2385. * Continue upwards until tree is sufficient.
  2386. */
  2387. static inline void mas_rebalance(struct ma_state *mas,
  2388. struct maple_big_node *b_node)
  2389. {
  2390. char empty_count = mas_mt_height(mas);
  2391. struct maple_subtree_state mast;
  2392. unsigned char shift, b_end = ++b_node->b_end;
  2393. MA_STATE(l_mas, mas->tree, mas->index, mas->last);
  2394. MA_STATE(r_mas, mas->tree, mas->index, mas->last);
  2395. trace_ma_op(TP_FCT, mas);
  2396. /*
  2397. * Rebalancing occurs if a node is insufficient. Data is rebalanced
  2398. * against the node to the right if it exists, otherwise the node to the
  2399. * left of this node is rebalanced against this node. If rebalancing
  2400. * causes just one node to be produced instead of two, then the parent
  2401. * is also examined and rebalanced if it is insufficient. Every level
  2402. * tries to combine the data in the same way. If one node contains the
  2403. * entire range of the tree, then that node is used as a new root node.
  2404. */
  2405. mast.orig_l = &l_mas;
  2406. mast.orig_r = &r_mas;
  2407. mast.bn = b_node;
  2408. mast.bn->type = mte_node_type(mas->node);
  2409. l_mas = r_mas = *mas;
  2410. if (mas_next_sibling(&r_mas)) {
  2411. mas_mab_cp(&r_mas, 0, mt_slot_count(r_mas.node), b_node, b_end);
  2412. r_mas.last = r_mas.index = r_mas.max;
  2413. } else {
  2414. mas_prev_sibling(&l_mas);
  2415. shift = mas_data_end(&l_mas) + 1;
  2416. mab_shift_right(b_node, shift);
  2417. mas->offset += shift;
  2418. mas_mab_cp(&l_mas, 0, shift - 1, b_node, 0);
  2419. b_node->b_end = shift + b_end;
  2420. l_mas.index = l_mas.last = l_mas.min;
  2421. }
  2422. return mas_spanning_rebalance(mas, &mast, empty_count);
  2423. }
  2424. /*
  2425. * mas_split_final_node() - Split the final node in a subtree operation.
  2426. * @mast: the maple subtree state
  2427. * @mas: The maple state
  2428. */
  2429. static inline void mas_split_final_node(struct maple_subtree_state *mast,
  2430. struct ma_state *mas)
  2431. {
  2432. struct maple_enode *ancestor;
  2433. if (mte_is_root(mas->node)) {
  2434. if (mt_is_alloc(mas->tree))
  2435. mast->bn->type = maple_arange_64;
  2436. else
  2437. mast->bn->type = maple_range_64;
  2438. }
  2439. /*
  2440. * Only a single node is used here, could be root.
  2441. * The Big_node data should just fit in a single node.
  2442. */
  2443. ancestor = mas_new_ma_node(mas, mast->bn);
  2444. mas_set_parent(mas, mast->l->node, ancestor, mast->l->offset);
  2445. mas_set_parent(mas, mast->r->node, ancestor, mast->r->offset);
  2446. mte_to_node(ancestor)->parent = mas_mn(mas)->parent;
  2447. mast->l->node = ancestor;
  2448. mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, mast->l, true);
  2449. mas->offset = mast->bn->b_end - 1;
  2450. }
  2451. /*
  2452. * mast_fill_bnode() - Copy data into the big node in the subtree state
  2453. * @mast: The maple subtree state
  2454. * @mas: the maple state
  2455. * @skip: The number of entries to skip for new nodes insertion.
  2456. */
  2457. static inline void mast_fill_bnode(struct maple_subtree_state *mast,
  2458. struct ma_state *mas,
  2459. unsigned char skip)
  2460. {
  2461. bool cp = true;
  2462. unsigned char split;
  2463. memset(mast->bn, 0, sizeof(struct maple_big_node));
  2464. if (mte_is_root(mas->node)) {
  2465. cp = false;
  2466. } else {
  2467. mas_ascend(mas);
  2468. mas->offset = mte_parent_slot(mas->node);
  2469. }
  2470. if (cp && mast->l->offset)
  2471. mas_mab_cp(mas, 0, mast->l->offset - 1, mast->bn, 0);
  2472. split = mast->bn->b_end;
  2473. mab_set_b_end(mast->bn, mast->l, mast->l->node);
  2474. mast->r->offset = mast->bn->b_end;
  2475. mab_set_b_end(mast->bn, mast->r, mast->r->node);
  2476. if (mast->bn->pivot[mast->bn->b_end - 1] == mas->max)
  2477. cp = false;
  2478. if (cp)
  2479. mas_mab_cp(mas, split + skip, mt_slot_count(mas->node) - 1,
  2480. mast->bn, mast->bn->b_end);
  2481. mast->bn->b_end--;
  2482. mast->bn->type = mte_node_type(mas->node);
  2483. }
  2484. /*
  2485. * mast_split_data() - Split the data in the subtree state big node into regular
  2486. * nodes.
  2487. * @mast: The maple subtree state
  2488. * @mas: The maple state
  2489. * @split: The location to split the big node
  2490. */
  2491. static inline void mast_split_data(struct maple_subtree_state *mast,
  2492. struct ma_state *mas, unsigned char split)
  2493. {
  2494. unsigned char p_slot;
  2495. mab_mas_cp(mast->bn, 0, split, mast->l, true);
  2496. mte_set_pivot(mast->r->node, 0, mast->r->max);
  2497. mab_mas_cp(mast->bn, split + 1, mast->bn->b_end, mast->r, false);
  2498. mast->l->offset = mte_parent_slot(mas->node);
  2499. mast->l->max = mast->bn->pivot[split];
  2500. mast->r->min = mast->l->max + 1;
  2501. if (mte_is_leaf(mas->node))
  2502. return;
  2503. p_slot = mast->orig_l->offset;
  2504. mas_set_split_parent(mast->orig_l, mast->l->node, mast->r->node,
  2505. &p_slot, split);
  2506. mas_set_split_parent(mast->orig_r, mast->l->node, mast->r->node,
  2507. &p_slot, split);
  2508. }
  2509. /*
  2510. * mas_push_data() - Instead of splitting a node, it is beneficial to push the
  2511. * data to the right or left node if there is room.
  2512. * @mas: The maple state
  2513. * @mast: The maple subtree state
  2514. * @left: Push left or not.
  2515. *
  2516. * Keeping the height of the tree low means faster lookups.
  2517. *
  2518. * Return: True if pushed, false otherwise.
  2519. */
  2520. static inline bool mas_push_data(struct ma_state *mas,
  2521. struct maple_subtree_state *mast, bool left)
  2522. {
  2523. unsigned char slot_total = mast->bn->b_end;
  2524. unsigned char end, space, split;
  2525. MA_STATE(tmp_mas, mas->tree, mas->index, mas->last);
  2526. tmp_mas = *mas;
  2527. tmp_mas.depth = mast->l->depth;
  2528. if (left && !mas_prev_sibling(&tmp_mas))
  2529. return false;
  2530. else if (!left && !mas_next_sibling(&tmp_mas))
  2531. return false;
  2532. end = mas_data_end(&tmp_mas);
  2533. slot_total += end;
  2534. space = 2 * mt_slot_count(mas->node) - 2;
  2535. /* -2 instead of -1 to ensure there isn't a triple split */
  2536. if (ma_is_leaf(mast->bn->type))
  2537. space--;
  2538. if (mas->max == ULONG_MAX)
  2539. space--;
  2540. if (slot_total >= space)
  2541. return false;
  2542. /* Get the data; Fill mast->bn */
  2543. mast->bn->b_end++;
  2544. if (left) {
  2545. mab_shift_right(mast->bn, end + 1);
  2546. mas_mab_cp(&tmp_mas, 0, end, mast->bn, 0);
  2547. mast->bn->b_end = slot_total + 1;
  2548. } else {
  2549. mas_mab_cp(&tmp_mas, 0, end, mast->bn, mast->bn->b_end);
  2550. }
  2551. /* Configure mast for splitting of mast->bn */
  2552. split = mt_slots[mast->bn->type] - 2;
  2553. if (left) {
  2554. /* Switch mas to prev node */
  2555. *mas = tmp_mas;
  2556. /* Start using mast->l for the left side. */
  2557. tmp_mas.node = mast->l->node;
  2558. *mast->l = tmp_mas;
  2559. } else {
  2560. tmp_mas.node = mast->r->node;
  2561. *mast->r = tmp_mas;
  2562. split = slot_total - split;
  2563. }
  2564. split = mab_no_null_split(mast->bn, split, mt_slots[mast->bn->type]);
  2565. /* Update parent slot for split calculation. */
  2566. if (left)
  2567. mast->orig_l->offset += end + 1;
  2568. mast_split_data(mast, mas, split);
  2569. mast_fill_bnode(mast, mas, 2);
  2570. mas_split_final_node(mast, mas);
  2571. return true;
  2572. }
  2573. /*
  2574. * mas_split() - Split data that is too big for one node into two.
  2575. * @mas: The maple state
  2576. * @b_node: The maple big node
  2577. */
  2578. static void mas_split(struct ma_state *mas, struct maple_big_node *b_node)
  2579. {
  2580. struct maple_subtree_state mast;
  2581. int height = 0;
  2582. unsigned int orig_height = mas_mt_height(mas);
  2583. unsigned char mid_split, split = 0;
  2584. struct maple_enode *old;
  2585. /*
  2586. * Splitting is handled differently from any other B-tree; the Maple
  2587. * Tree splits upwards. Splitting up means that the split operation
  2588. * occurs when the walk of the tree hits the leaves and not on the way
  2589. * down. The reason for splitting up is that it is impossible to know
  2590. * how much space will be needed until the leaf is (or leaves are)
  2591. * reached. Since overwriting data is allowed and a range could
  2592. * overwrite more than one range or result in changing one entry into 3
  2593. * entries, it is impossible to know if a split is required until the
  2594. * data is examined.
  2595. *
  2596. * Splitting is a balancing act between keeping allocations to a minimum
  2597. * and avoiding a 'jitter' event where a tree is expanded to make room
  2598. * for an entry followed by a contraction when the entry is removed. To
  2599. * accomplish the balance, there are empty slots remaining in both left
  2600. * and right nodes after a split.
  2601. */
  2602. MA_STATE(l_mas, mas->tree, mas->index, mas->last);
  2603. MA_STATE(r_mas, mas->tree, mas->index, mas->last);
  2604. MA_STATE(prev_l_mas, mas->tree, mas->index, mas->last);
  2605. MA_STATE(prev_r_mas, mas->tree, mas->index, mas->last);
  2606. trace_ma_op(TP_FCT, mas);
  2607. mast.l = &l_mas;
  2608. mast.r = &r_mas;
  2609. mast.orig_l = &prev_l_mas;
  2610. mast.orig_r = &prev_r_mas;
  2611. mast.bn = b_node;
  2612. while (height++ <= orig_height) {
  2613. if (mt_slots[b_node->type] > b_node->b_end) {
  2614. mas_split_final_node(&mast, mas);
  2615. break;
  2616. }
  2617. l_mas = r_mas = *mas;
  2618. l_mas.node = mas_new_ma_node(mas, b_node);
  2619. r_mas.node = mas_new_ma_node(mas, b_node);
  2620. /*
  2621. * Another way that 'jitter' is avoided is to terminate a split up early if the
  2622. * left or right node has space to spare. This is referred to as "pushing left"
  2623. * or "pushing right" and is similar to the B* tree, except the nodes left or
  2624. * right can rarely be reused due to RCU, but the ripple upwards is halted which
  2625. * is a significant savings.
  2626. */
  2627. /* Try to push left. */
  2628. if (mas_push_data(mas, &mast, true)) {
  2629. height++;
  2630. break;
  2631. }
  2632. /* Try to push right. */
  2633. if (mas_push_data(mas, &mast, false)) {
  2634. height++;
  2635. break;
  2636. }
  2637. split = mab_calc_split(mas, b_node, &mid_split);
  2638. mast_split_data(&mast, mas, split);
  2639. /*
  2640. * Usually correct, mab_mas_cp in the above call overwrites
  2641. * r->max.
  2642. */
  2643. mast.r->max = mas->max;
  2644. mast_fill_bnode(&mast, mas, 1);
  2645. prev_l_mas = *mast.l;
  2646. prev_r_mas = *mast.r;
  2647. }
  2648. /* Set the original node as dead */
  2649. old = mas->node;
  2650. mas->node = l_mas.node;
  2651. mas_wmb_replace(mas, old, height);
  2652. mtree_range_walk(mas);
  2653. return;
  2654. }
  2655. /*
  2656. * mas_commit_b_node() - Commit the big node into the tree.
  2657. * @wr_mas: The maple write state
  2658. * @b_node: The maple big node
  2659. */
  2660. static noinline_for_kasan void mas_commit_b_node(struct ma_wr_state *wr_mas,
  2661. struct maple_big_node *b_node)
  2662. {
  2663. enum store_type type = wr_mas->mas->store_type;
  2664. WARN_ON_ONCE(type != wr_rebalance && type != wr_split_store);
  2665. if (type == wr_rebalance)
  2666. return mas_rebalance(wr_mas->mas, b_node);
  2667. return mas_split(wr_mas->mas, b_node);
  2668. }
  2669. /*
  2670. * mas_root_expand() - Expand a root to a node
  2671. * @mas: The maple state
  2672. * @entry: The entry to store into the tree
  2673. */
  2674. static inline void mas_root_expand(struct ma_state *mas, void *entry)
  2675. {
  2676. void *contents = mas_root_locked(mas);
  2677. enum maple_type type = maple_leaf_64;
  2678. struct maple_node *node;
  2679. void __rcu **slots;
  2680. unsigned long *pivots;
  2681. int slot = 0;
  2682. node = mas_pop_node(mas);
  2683. pivots = ma_pivots(node, type);
  2684. slots = ma_slots(node, type);
  2685. node->parent = ma_parent_ptr(mas_tree_parent(mas));
  2686. mas->node = mt_mk_node(node, type);
  2687. mas->status = ma_active;
  2688. if (mas->index) {
  2689. if (contents) {
  2690. rcu_assign_pointer(slots[slot], contents);
  2691. if (likely(mas->index > 1))
  2692. slot++;
  2693. }
  2694. pivots[slot++] = mas->index - 1;
  2695. }
  2696. rcu_assign_pointer(slots[slot], entry);
  2697. mas->offset = slot;
  2698. pivots[slot] = mas->last;
  2699. if (mas->last != ULONG_MAX)
  2700. pivots[++slot] = ULONG_MAX;
  2701. mt_set_height(mas->tree, 1);
  2702. ma_set_meta(node, maple_leaf_64, 0, slot);
  2703. /* swap the new root into the tree */
  2704. rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
  2705. return;
  2706. }
  2707. /*
  2708. * mas_store_root() - Storing value into root.
  2709. * @mas: The maple state
  2710. * @entry: The entry to store.
  2711. *
  2712. * There is no root node now and we are storing a value into the root - this
  2713. * function either assigns the pointer or expands into a node.
  2714. */
  2715. static inline void mas_store_root(struct ma_state *mas, void *entry)
  2716. {
  2717. if (!entry) {
  2718. if (!mas->index)
  2719. rcu_assign_pointer(mas->tree->ma_root, NULL);
  2720. } else if (likely((mas->last != 0) || (mas->index != 0)))
  2721. mas_root_expand(mas, entry);
  2722. else if (((unsigned long) (entry) & 3) == 2)
  2723. mas_root_expand(mas, entry);
  2724. else {
  2725. rcu_assign_pointer(mas->tree->ma_root, entry);
  2726. mas->status = ma_start;
  2727. }
  2728. }
  2729. /*
  2730. * mas_is_span_wr() - Check if the write needs to be treated as a write that
  2731. * spans the node.
  2732. * @wr_mas: The maple write state
  2733. *
  2734. * Spanning writes are writes that start in one node and end in another OR if
  2735. * the write of a %NULL will cause the node to end with a %NULL.
  2736. *
  2737. * Return: True if this is a spanning write, false otherwise.
  2738. */
  2739. static bool mas_is_span_wr(struct ma_wr_state *wr_mas)
  2740. {
  2741. unsigned long max = wr_mas->r_max;
  2742. unsigned long last = wr_mas->mas->last;
  2743. enum maple_type type = wr_mas->type;
  2744. void *entry = wr_mas->entry;
  2745. /* Contained in this pivot, fast path */
  2746. if (last < max)
  2747. return false;
  2748. if (ma_is_leaf(type)) {
  2749. max = wr_mas->mas->max;
  2750. if (last < max)
  2751. return false;
  2752. }
  2753. if (last == max) {
  2754. /*
  2755. * The last entry of leaf node cannot be NULL unless it is the
  2756. * rightmost node (writing ULONG_MAX), otherwise it spans slots.
  2757. */
  2758. if (entry || last == ULONG_MAX)
  2759. return false;
  2760. }
  2761. trace_ma_write(TP_FCT, wr_mas->mas, wr_mas->r_max, entry);
  2762. return true;
  2763. }
  2764. static inline void mas_wr_walk_descend(struct ma_wr_state *wr_mas)
  2765. {
  2766. wr_mas->type = mte_node_type(wr_mas->mas->node);
  2767. mas_wr_node_walk(wr_mas);
  2768. wr_mas->slots = ma_slots(wr_mas->node, wr_mas->type);
  2769. }
  2770. static inline void mas_wr_walk_traverse(struct ma_wr_state *wr_mas)
  2771. {
  2772. wr_mas->mas->max = wr_mas->r_max;
  2773. wr_mas->mas->min = wr_mas->r_min;
  2774. wr_mas->mas->node = wr_mas->content;
  2775. wr_mas->mas->offset = 0;
  2776. wr_mas->mas->depth++;
  2777. }
  2778. /*
  2779. * mas_wr_walk() - Walk the tree for a write.
  2780. * @wr_mas: The maple write state
  2781. *
  2782. * Uses mas_slot_locked() and does not need to worry about dead nodes.
  2783. *
  2784. * Return: True if it's contained in a node, false on spanning write.
  2785. */
  2786. static bool mas_wr_walk(struct ma_wr_state *wr_mas)
  2787. {
  2788. struct ma_state *mas = wr_mas->mas;
  2789. while (true) {
  2790. mas_wr_walk_descend(wr_mas);
  2791. if (unlikely(mas_is_span_wr(wr_mas)))
  2792. return false;
  2793. wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
  2794. mas->offset);
  2795. if (ma_is_leaf(wr_mas->type))
  2796. return true;
  2797. if (mas->end < mt_slots[wr_mas->type] - 1)
  2798. wr_mas->vacant_height = mas->depth + 1;
  2799. if (ma_is_root(mas_mn(mas))) {
  2800. /* root needs more than 2 entries to be sufficient + 1 */
  2801. if (mas->end > 2)
  2802. wr_mas->sufficient_height = 1;
  2803. } else if (mas->end > mt_min_slots[wr_mas->type] + 1)
  2804. wr_mas->sufficient_height = mas->depth + 1;
  2805. mas_wr_walk_traverse(wr_mas);
  2806. }
  2807. return true;
  2808. }
  2809. static void mas_wr_walk_index(struct ma_wr_state *wr_mas)
  2810. {
  2811. struct ma_state *mas = wr_mas->mas;
  2812. while (true) {
  2813. mas_wr_walk_descend(wr_mas);
  2814. wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
  2815. mas->offset);
  2816. if (ma_is_leaf(wr_mas->type))
  2817. return;
  2818. mas_wr_walk_traverse(wr_mas);
  2819. }
  2820. }
  2821. /*
  2822. * mas_extend_spanning_null() - Extend a store of a %NULL to include surrounding %NULLs.
  2823. * @l_wr_mas: The left maple write state
  2824. * @r_wr_mas: The right maple write state
  2825. */
  2826. static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
  2827. struct ma_wr_state *r_wr_mas)
  2828. {
  2829. struct ma_state *r_mas = r_wr_mas->mas;
  2830. struct ma_state *l_mas = l_wr_mas->mas;
  2831. unsigned char l_slot;
  2832. l_slot = l_mas->offset;
  2833. if (!l_wr_mas->content)
  2834. l_mas->index = l_wr_mas->r_min;
  2835. if ((l_mas->index == l_wr_mas->r_min) &&
  2836. (l_slot &&
  2837. !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
  2838. if (l_slot > 1)
  2839. l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
  2840. else
  2841. l_mas->index = l_mas->min;
  2842. l_mas->offset = l_slot - 1;
  2843. }
  2844. if (!r_wr_mas->content) {
  2845. if (r_mas->last < r_wr_mas->r_max)
  2846. r_mas->last = r_wr_mas->r_max;
  2847. r_mas->offset++;
  2848. } else if ((r_mas->last == r_wr_mas->r_max) &&
  2849. (r_mas->last < r_mas->max) &&
  2850. !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
  2851. r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
  2852. r_wr_mas->type, r_mas->offset + 1);
  2853. r_mas->offset++;
  2854. }
  2855. }
  2856. static inline void *mas_state_walk(struct ma_state *mas)
  2857. {
  2858. void *entry;
  2859. entry = mas_start(mas);
  2860. if (mas_is_none(mas))
  2861. return NULL;
  2862. if (mas_is_ptr(mas))
  2863. return entry;
  2864. return mtree_range_walk(mas);
  2865. }
  2866. /*
  2867. * mtree_lookup_walk() - Internal quick lookup that does not keep maple state up
  2868. * to date.
  2869. *
  2870. * @mas: The maple state.
  2871. *
  2872. * Note: Leaves mas in undesirable state.
  2873. * Return: The entry for @mas->index or %NULL on dead node.
  2874. */
  2875. static inline void *mtree_lookup_walk(struct ma_state *mas)
  2876. {
  2877. unsigned long *pivots;
  2878. unsigned char offset;
  2879. struct maple_node *node;
  2880. struct maple_enode *next;
  2881. enum maple_type type;
  2882. void __rcu **slots;
  2883. unsigned char end;
  2884. next = mas->node;
  2885. do {
  2886. node = mte_to_node(next);
  2887. type = mte_node_type(next);
  2888. pivots = ma_pivots(node, type);
  2889. end = mt_pivots[type];
  2890. offset = 0;
  2891. do {
  2892. if (pivots[offset] >= mas->index)
  2893. break;
  2894. } while (++offset < end);
  2895. slots = ma_slots(node, type);
  2896. next = mt_slot(mas->tree, slots, offset);
  2897. if (unlikely(ma_dead_node(node)))
  2898. goto dead_node;
  2899. } while (!ma_is_leaf(type));
  2900. return (void *)next;
  2901. dead_node:
  2902. mas_reset(mas);
  2903. return NULL;
  2904. }
  2905. static void mte_destroy_walk(struct maple_enode *, struct maple_tree *);
  2906. /*
  2907. * mas_new_root() - Create a new root node that only contains the entry passed
  2908. * in.
  2909. * @mas: The maple state
  2910. * @entry: The entry to store.
  2911. *
  2912. * Only valid when the index == 0 and the last == ULONG_MAX
  2913. */
  2914. static inline void mas_new_root(struct ma_state *mas, void *entry)
  2915. {
  2916. struct maple_enode *root = mas_root_locked(mas);
  2917. enum maple_type type = maple_leaf_64;
  2918. struct maple_node *node;
  2919. void __rcu **slots;
  2920. unsigned long *pivots;
  2921. WARN_ON_ONCE(mas->index || mas->last != ULONG_MAX);
  2922. if (!entry) {
  2923. mt_set_height(mas->tree, 0);
  2924. rcu_assign_pointer(mas->tree->ma_root, entry);
  2925. mas->status = ma_start;
  2926. goto done;
  2927. }
  2928. node = mas_pop_node(mas);
  2929. pivots = ma_pivots(node, type);
  2930. slots = ma_slots(node, type);
  2931. node->parent = ma_parent_ptr(mas_tree_parent(mas));
  2932. mas->node = mt_mk_node(node, type);
  2933. mas->status = ma_active;
  2934. rcu_assign_pointer(slots[0], entry);
  2935. pivots[0] = mas->last;
  2936. mt_set_height(mas->tree, 1);
  2937. rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
  2938. done:
  2939. if (xa_is_node(root))
  2940. mte_destroy_walk(root, mas->tree);
  2941. return;
  2942. }
  2943. /*
  2944. * mas_wr_spanning_store() - Create a subtree with the store operation completed
  2945. * and new nodes where necessary, then place the sub-tree in the actual tree.
  2946. * Note that mas is expected to point to the node which caused the store to
  2947. * span.
  2948. * @wr_mas: The maple write state
  2949. */
  2950. static noinline void mas_wr_spanning_store(struct ma_wr_state *wr_mas)
  2951. {
  2952. struct maple_subtree_state mast;
  2953. struct maple_big_node b_node;
  2954. struct ma_state *mas;
  2955. unsigned char height;
  2956. /* Left and Right side of spanning store */
  2957. MA_STATE(l_mas, NULL, 0, 0);
  2958. MA_STATE(r_mas, NULL, 0, 0);
  2959. MA_WR_STATE(r_wr_mas, &r_mas, wr_mas->entry);
  2960. MA_WR_STATE(l_wr_mas, &l_mas, wr_mas->entry);
  2961. /*
  2962. * A store operation that spans multiple nodes is called a spanning
  2963. * store and is handled early in the store call stack by the function
  2964. * mas_is_span_wr(). When a spanning store is identified, the maple
  2965. * state is duplicated. The first maple state walks the left tree path
  2966. * to ``index``, the duplicate walks the right tree path to ``last``.
  2967. * The data in the two nodes are combined into a single node, two nodes,
  2968. * or possibly three nodes (see the 3-way split above). A ``NULL``
  2969. * written to the last entry of a node is considered a spanning store as
  2970. * a rebalance is required for the operation to complete and an overflow
  2971. * of data may happen.
  2972. */
  2973. mas = wr_mas->mas;
  2974. trace_ma_op(TP_FCT, mas);
  2975. if (unlikely(!mas->index && mas->last == ULONG_MAX))
  2976. return mas_new_root(mas, wr_mas->entry);
  2977. /*
  2978. * Node rebalancing may occur due to this store, so there may be three new
  2979. * entries per level plus a new root.
  2980. */
  2981. height = mas_mt_height(mas);
  2982. /*
  2983. * Set up right side. Need to get to the next offset after the spanning
  2984. * store to ensure it's not NULL and to combine both the next node and
  2985. * the node with the start together.
  2986. */
  2987. r_mas = *mas;
  2988. /* Avoid overflow, walk to next slot in the tree. */
  2989. if (r_mas.last + 1)
  2990. r_mas.last++;
  2991. r_mas.index = r_mas.last;
  2992. mas_wr_walk_index(&r_wr_mas);
  2993. r_mas.last = r_mas.index = mas->last;
  2994. /* Set up left side. */
  2995. l_mas = *mas;
  2996. mas_wr_walk_index(&l_wr_mas);
  2997. if (!wr_mas->entry) {
  2998. mas_extend_spanning_null(&l_wr_mas, &r_wr_mas);
  2999. mas->offset = l_mas.offset;
  3000. mas->index = l_mas.index;
  3001. mas->last = l_mas.last = r_mas.last;
  3002. }
  3003. /* expanding NULLs may make this cover the entire range */
  3004. if (!l_mas.index && r_mas.last == ULONG_MAX) {
  3005. mas_set_range(mas, 0, ULONG_MAX);
  3006. return mas_new_root(mas, wr_mas->entry);
  3007. }
  3008. memset(&b_node, 0, sizeof(struct maple_big_node));
  3009. /* Copy l_mas and store the value in b_node. */
  3010. mas_store_b_node(&l_wr_mas, &b_node, l_mas.end);
  3011. /* Copy r_mas into b_node if there is anything to copy. */
  3012. if (r_mas.max > r_mas.last)
  3013. mas_mab_cp(&r_mas, r_mas.offset, r_mas.end,
  3014. &b_node, b_node.b_end + 1);
  3015. else
  3016. b_node.b_end++;
  3017. /* Stop spanning searches by searching for just index. */
  3018. l_mas.index = l_mas.last = mas->index;
  3019. mast.bn = &b_node;
  3020. mast.orig_l = &l_mas;
  3021. mast.orig_r = &r_mas;
  3022. /* Combine l_mas and r_mas and split them up evenly again. */
  3023. return mas_spanning_rebalance(mas, &mast, height + 1);
  3024. }
  3025. /*
  3026. * mas_wr_node_store() - Attempt to store the value in a node
  3027. * @wr_mas: The maple write state
  3028. *
  3029. * Attempts to reuse the node, but may allocate.
  3030. */
  3031. static inline void mas_wr_node_store(struct ma_wr_state *wr_mas,
  3032. unsigned char new_end)
  3033. {
  3034. struct ma_state *mas = wr_mas->mas;
  3035. void __rcu **dst_slots;
  3036. unsigned long *dst_pivots;
  3037. unsigned char dst_offset, offset_end = wr_mas->offset_end;
  3038. struct maple_node reuse, *newnode;
  3039. unsigned char copy_size, node_pivots = mt_pivots[wr_mas->type];
  3040. bool in_rcu = mt_in_rcu(mas->tree);
  3041. unsigned char height = mas_mt_height(mas);
  3042. if (mas->last == wr_mas->end_piv)
  3043. offset_end++; /* don't copy this offset */
  3044. /* set up node. */
  3045. if (in_rcu) {
  3046. newnode = mas_pop_node(mas);
  3047. } else {
  3048. memset(&reuse, 0, sizeof(struct maple_node));
  3049. newnode = &reuse;
  3050. }
  3051. newnode->parent = mas_mn(mas)->parent;
  3052. dst_pivots = ma_pivots(newnode, wr_mas->type);
  3053. dst_slots = ma_slots(newnode, wr_mas->type);
  3054. /* Copy from start to insert point */
  3055. memcpy(dst_pivots, wr_mas->pivots, sizeof(unsigned long) * mas->offset);
  3056. memcpy(dst_slots, wr_mas->slots, sizeof(void *) * mas->offset);
  3057. /* Handle insert of new range starting after old range */
  3058. if (wr_mas->r_min < mas->index) {
  3059. rcu_assign_pointer(dst_slots[mas->offset], wr_mas->content);
  3060. dst_pivots[mas->offset++] = mas->index - 1;
  3061. }
  3062. /* Store the new entry and range end. */
  3063. if (mas->offset < node_pivots)
  3064. dst_pivots[mas->offset] = mas->last;
  3065. rcu_assign_pointer(dst_slots[mas->offset], wr_mas->entry);
  3066. /*
  3067. * this range wrote to the end of the node or it overwrote the rest of
  3068. * the data
  3069. */
  3070. if (offset_end > mas->end)
  3071. goto done;
  3072. dst_offset = mas->offset + 1;
  3073. /* Copy to the end of node if necessary. */
  3074. copy_size = mas->end - offset_end + 1;
  3075. memcpy(dst_slots + dst_offset, wr_mas->slots + offset_end,
  3076. sizeof(void *) * copy_size);
  3077. memcpy(dst_pivots + dst_offset, wr_mas->pivots + offset_end,
  3078. sizeof(unsigned long) * (copy_size - 1));
  3079. if (new_end < node_pivots)
  3080. dst_pivots[new_end] = mas->max;
  3081. done:
  3082. mas_leaf_set_meta(newnode, maple_leaf_64, new_end);
  3083. if (in_rcu) {
  3084. struct maple_enode *old_enode = mas->node;
  3085. mas->node = mt_mk_node(newnode, wr_mas->type);
  3086. mas_replace_node(mas, old_enode, height);
  3087. } else {
  3088. memcpy(wr_mas->node, newnode, sizeof(struct maple_node));
  3089. }
  3090. trace_ma_write(TP_FCT, mas, 0, wr_mas->entry);
  3091. mas_update_gap(mas);
  3092. mas->end = new_end;
  3093. return;
  3094. }
  3095. /*
  3096. * mas_wr_slot_store: Attempt to store a value in a slot.
  3097. * @wr_mas: the maple write state
  3098. */
  3099. static inline void mas_wr_slot_store(struct ma_wr_state *wr_mas)
  3100. {
  3101. struct ma_state *mas = wr_mas->mas;
  3102. unsigned char offset = mas->offset;
  3103. void __rcu **slots = wr_mas->slots;
  3104. bool gap = false;
  3105. gap |= !mt_slot_locked(mas->tree, slots, offset);
  3106. gap |= !mt_slot_locked(mas->tree, slots, offset + 1);
  3107. if (wr_mas->offset_end - offset == 1) {
  3108. if (mas->index == wr_mas->r_min) {
  3109. /* Overwriting the range and a part of the next one */
  3110. rcu_assign_pointer(slots[offset], wr_mas->entry);
  3111. wr_mas->pivots[offset] = mas->last;
  3112. } else {
  3113. /* Overwriting a part of the range and the next one */
  3114. rcu_assign_pointer(slots[offset + 1], wr_mas->entry);
  3115. wr_mas->pivots[offset] = mas->index - 1;
  3116. mas->offset++; /* Keep mas accurate. */
  3117. }
  3118. } else {
  3119. WARN_ON_ONCE(mt_in_rcu(mas->tree));
  3120. /*
  3121. * Expand the range, only partially overwriting the previous and
  3122. * next ranges
  3123. */
  3124. gap |= !mt_slot_locked(mas->tree, slots, offset + 2);
  3125. rcu_assign_pointer(slots[offset + 1], wr_mas->entry);
  3126. wr_mas->pivots[offset] = mas->index - 1;
  3127. wr_mas->pivots[offset + 1] = mas->last;
  3128. mas->offset++; /* Keep mas accurate. */
  3129. }
  3130. trace_ma_write(TP_FCT, mas, 0, wr_mas->entry);
  3131. /*
  3132. * Only update gap when the new entry is empty or there is an empty
  3133. * entry in the original two ranges.
  3134. */
  3135. if (!wr_mas->entry || gap)
  3136. mas_update_gap(mas);
  3137. return;
  3138. }
  3139. static inline void mas_wr_extend_null(struct ma_wr_state *wr_mas)
  3140. {
  3141. struct ma_state *mas = wr_mas->mas;
  3142. if (!wr_mas->slots[wr_mas->offset_end]) {
  3143. /* If this one is null, the next and prev are not */
  3144. mas->last = wr_mas->end_piv;
  3145. } else {
  3146. /* Check next slot(s) if we are overwriting the end */
  3147. if ((mas->last == wr_mas->end_piv) &&
  3148. (mas->end != wr_mas->offset_end) &&
  3149. !wr_mas->slots[wr_mas->offset_end + 1]) {
  3150. wr_mas->offset_end++;
  3151. if (wr_mas->offset_end == mas->end)
  3152. mas->last = mas->max;
  3153. else
  3154. mas->last = wr_mas->pivots[wr_mas->offset_end];
  3155. wr_mas->end_piv = mas->last;
  3156. }
  3157. }
  3158. if (!wr_mas->content) {
  3159. /* If this one is null, the next and prev are not */
  3160. mas->index = wr_mas->r_min;
  3161. } else {
  3162. /* Check prev slot if we are overwriting the start */
  3163. if (mas->index == wr_mas->r_min && mas->offset &&
  3164. !wr_mas->slots[mas->offset - 1]) {
  3165. mas->offset--;
  3166. wr_mas->r_min = mas->index =
  3167. mas_safe_min(mas, wr_mas->pivots, mas->offset);
  3168. wr_mas->r_max = wr_mas->pivots[mas->offset];
  3169. }
  3170. }
  3171. }
  3172. static inline void mas_wr_end_piv(struct ma_wr_state *wr_mas)
  3173. {
  3174. while ((wr_mas->offset_end < wr_mas->mas->end) &&
  3175. (wr_mas->mas->last > wr_mas->pivots[wr_mas->offset_end]))
  3176. wr_mas->offset_end++;
  3177. if (wr_mas->offset_end < wr_mas->mas->end)
  3178. wr_mas->end_piv = wr_mas->pivots[wr_mas->offset_end];
  3179. else
  3180. wr_mas->end_piv = wr_mas->mas->max;
  3181. }
  3182. static inline unsigned char mas_wr_new_end(struct ma_wr_state *wr_mas)
  3183. {
  3184. struct ma_state *mas = wr_mas->mas;
  3185. unsigned char new_end = mas->end + 2;
  3186. new_end -= wr_mas->offset_end - mas->offset;
  3187. if (wr_mas->r_min == mas->index)
  3188. new_end--;
  3189. if (wr_mas->end_piv == mas->last)
  3190. new_end--;
  3191. return new_end;
  3192. }
  3193. /*
  3194. * mas_wr_append: Attempt to append
  3195. * @wr_mas: the maple write state
  3196. * @new_end: The end of the node after the modification
  3197. *
  3198. * This is currently unsafe in rcu mode since the end of the node may be cached
  3199. * by readers while the node contents may be updated which could result in
  3200. * inaccurate information.
  3201. */
  3202. static inline void mas_wr_append(struct ma_wr_state *wr_mas,
  3203. unsigned char new_end)
  3204. {
  3205. struct ma_state *mas = wr_mas->mas;
  3206. void __rcu **slots;
  3207. unsigned char end = mas->end;
  3208. if (new_end < mt_pivots[wr_mas->type]) {
  3209. wr_mas->pivots[new_end] = wr_mas->pivots[end];
  3210. ma_set_meta(wr_mas->node, wr_mas->type, 0, new_end);
  3211. }
  3212. slots = wr_mas->slots;
  3213. if (new_end == end + 1) {
  3214. if (mas->last == wr_mas->r_max) {
  3215. /* Append to end of range */
  3216. rcu_assign_pointer(slots[new_end], wr_mas->entry);
  3217. wr_mas->pivots[end] = mas->index - 1;
  3218. mas->offset = new_end;
  3219. } else {
  3220. /* Append to start of range */
  3221. rcu_assign_pointer(slots[new_end], wr_mas->content);
  3222. wr_mas->pivots[end] = mas->last;
  3223. rcu_assign_pointer(slots[end], wr_mas->entry);
  3224. }
  3225. } else {
  3226. /* Append to the range without touching any boundaries. */
  3227. rcu_assign_pointer(slots[new_end], wr_mas->content);
  3228. wr_mas->pivots[end + 1] = mas->last;
  3229. rcu_assign_pointer(slots[end + 1], wr_mas->entry);
  3230. wr_mas->pivots[end] = mas->index - 1;
  3231. mas->offset = end + 1;
  3232. }
  3233. if (!wr_mas->content || !wr_mas->entry)
  3234. mas_update_gap(mas);
  3235. mas->end = new_end;
  3236. trace_ma_write(TP_FCT, mas, new_end, wr_mas->entry);
  3237. return;
  3238. }
  3239. /*
  3240. * mas_wr_bnode() - Slow path for a modification.
  3241. * @wr_mas: The write maple state
  3242. *
  3243. * This is where split, rebalance end up.
  3244. */
  3245. static void mas_wr_bnode(struct ma_wr_state *wr_mas)
  3246. {
  3247. struct maple_big_node b_node;
  3248. trace_ma_write(TP_FCT, wr_mas->mas, 0, wr_mas->entry);
  3249. memset(&b_node, 0, sizeof(struct maple_big_node));
  3250. mas_store_b_node(wr_mas, &b_node, wr_mas->offset_end);
  3251. mas_commit_b_node(wr_mas, &b_node);
  3252. }
  3253. /*
  3254. * mas_wr_store_entry() - Internal call to store a value
  3255. * @wr_mas: The maple write state
  3256. */
  3257. static inline void mas_wr_store_entry(struct ma_wr_state *wr_mas)
  3258. {
  3259. struct ma_state *mas = wr_mas->mas;
  3260. unsigned char new_end = mas_wr_new_end(wr_mas);
  3261. switch (mas->store_type) {
  3262. case wr_exact_fit:
  3263. rcu_assign_pointer(wr_mas->slots[mas->offset], wr_mas->entry);
  3264. if (!!wr_mas->entry ^ !!wr_mas->content)
  3265. mas_update_gap(mas);
  3266. break;
  3267. case wr_append:
  3268. mas_wr_append(wr_mas, new_end);
  3269. break;
  3270. case wr_slot_store:
  3271. mas_wr_slot_store(wr_mas);
  3272. break;
  3273. case wr_node_store:
  3274. mas_wr_node_store(wr_mas, new_end);
  3275. break;
  3276. case wr_spanning_store:
  3277. mas_wr_spanning_store(wr_mas);
  3278. break;
  3279. case wr_split_store:
  3280. case wr_rebalance:
  3281. mas_wr_bnode(wr_mas);
  3282. break;
  3283. case wr_new_root:
  3284. mas_new_root(mas, wr_mas->entry);
  3285. break;
  3286. case wr_store_root:
  3287. mas_store_root(mas, wr_mas->entry);
  3288. break;
  3289. case wr_invalid:
  3290. MT_BUG_ON(mas->tree, 1);
  3291. }
  3292. return;
  3293. }
  3294. static inline void mas_wr_prealloc_setup(struct ma_wr_state *wr_mas)
  3295. {
  3296. struct ma_state *mas = wr_mas->mas;
  3297. if (!mas_is_active(mas)) {
  3298. if (mas_is_start(mas))
  3299. goto set_content;
  3300. if (unlikely(mas_is_paused(mas)))
  3301. goto reset;
  3302. if (unlikely(mas_is_none(mas)))
  3303. goto reset;
  3304. if (unlikely(mas_is_overflow(mas)))
  3305. goto reset;
  3306. if (unlikely(mas_is_underflow(mas)))
  3307. goto reset;
  3308. }
  3309. /*
  3310. * A less strict version of mas_is_span_wr() where we allow spanning
  3311. * writes within this node. This is to stop partial walks in
  3312. * mas_prealloc() from being reset.
  3313. */
  3314. if (mas->last > mas->max)
  3315. goto reset;
  3316. if (wr_mas->entry)
  3317. goto set_content;
  3318. if (mte_is_leaf(mas->node) && mas->last == mas->max)
  3319. goto reset;
  3320. goto set_content;
  3321. reset:
  3322. mas_reset(mas);
  3323. set_content:
  3324. wr_mas->content = mas_start(mas);
  3325. }
  3326. /**
  3327. * mas_prealloc_calc() - Calculate number of nodes needed for a
  3328. * given store oepration
  3329. * @wr_mas: The maple write state
  3330. * @entry: The entry to store into the tree
  3331. *
  3332. * Return: Number of nodes required for preallocation.
  3333. */
  3334. static inline void mas_prealloc_calc(struct ma_wr_state *wr_mas, void *entry)
  3335. {
  3336. struct ma_state *mas = wr_mas->mas;
  3337. unsigned char height = mas_mt_height(mas);
  3338. int ret = height * 3 + 1;
  3339. unsigned char delta = height - wr_mas->vacant_height;
  3340. switch (mas->store_type) {
  3341. case wr_exact_fit:
  3342. case wr_append:
  3343. case wr_slot_store:
  3344. ret = 0;
  3345. break;
  3346. case wr_spanning_store:
  3347. if (wr_mas->sufficient_height < wr_mas->vacant_height)
  3348. ret = (height - wr_mas->sufficient_height) * 3 + 1;
  3349. else
  3350. ret = delta * 3 + 1;
  3351. break;
  3352. case wr_split_store:
  3353. ret = delta * 2 + 1;
  3354. break;
  3355. case wr_rebalance:
  3356. if (wr_mas->sufficient_height < wr_mas->vacant_height)
  3357. ret = (height - wr_mas->sufficient_height) * 2 + 1;
  3358. else
  3359. ret = delta * 2 + 1;
  3360. break;
  3361. case wr_node_store:
  3362. ret = mt_in_rcu(mas->tree) ? 1 : 0;
  3363. break;
  3364. case wr_new_root:
  3365. ret = 1;
  3366. break;
  3367. case wr_store_root:
  3368. if (likely((mas->last != 0) || (mas->index != 0)))
  3369. ret = 1;
  3370. else if (((unsigned long) (entry) & 3) == 2)
  3371. ret = 1;
  3372. else
  3373. ret = 0;
  3374. break;
  3375. case wr_invalid:
  3376. WARN_ON_ONCE(1);
  3377. }
  3378. mas->node_request = ret;
  3379. }
  3380. /*
  3381. * mas_wr_store_type() - Determine the store type for a given
  3382. * store operation.
  3383. * @wr_mas: The maple write state
  3384. *
  3385. * Return: the type of store needed for the operation
  3386. */
  3387. static inline enum store_type mas_wr_store_type(struct ma_wr_state *wr_mas)
  3388. {
  3389. struct ma_state *mas = wr_mas->mas;
  3390. unsigned char new_end;
  3391. if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
  3392. return wr_store_root;
  3393. if (unlikely(!mas_wr_walk(wr_mas)))
  3394. return wr_spanning_store;
  3395. /* At this point, we are at the leaf node that needs to be altered. */
  3396. mas_wr_end_piv(wr_mas);
  3397. if (!wr_mas->entry)
  3398. mas_wr_extend_null(wr_mas);
  3399. if ((wr_mas->r_min == mas->index) && (wr_mas->r_max == mas->last))
  3400. return wr_exact_fit;
  3401. if (unlikely(!mas->index && mas->last == ULONG_MAX))
  3402. return wr_new_root;
  3403. new_end = mas_wr_new_end(wr_mas);
  3404. /* Potential spanning rebalance collapsing a node */
  3405. if (new_end < mt_min_slots[wr_mas->type]) {
  3406. if (!mte_is_root(mas->node))
  3407. return wr_rebalance;
  3408. return wr_node_store;
  3409. }
  3410. if (new_end >= mt_slots[wr_mas->type])
  3411. return wr_split_store;
  3412. if (!mt_in_rcu(mas->tree) && (mas->offset == mas->end))
  3413. return wr_append;
  3414. if ((new_end == mas->end) && (!mt_in_rcu(mas->tree) ||
  3415. (wr_mas->offset_end - mas->offset == 1)))
  3416. return wr_slot_store;
  3417. return wr_node_store;
  3418. }
  3419. /**
  3420. * mas_wr_preallocate() - Preallocate enough nodes for a store operation
  3421. * @wr_mas: The maple write state
  3422. * @entry: The entry that will be stored
  3423. *
  3424. */
  3425. static inline void mas_wr_preallocate(struct ma_wr_state *wr_mas, void *entry)
  3426. {
  3427. struct ma_state *mas = wr_mas->mas;
  3428. mas_wr_prealloc_setup(wr_mas);
  3429. mas->store_type = mas_wr_store_type(wr_mas);
  3430. mas_prealloc_calc(wr_mas, entry);
  3431. if (!mas->node_request)
  3432. return;
  3433. mas_alloc_nodes(mas, GFP_NOWAIT);
  3434. }
  3435. /**
  3436. * mas_insert() - Internal call to insert a value
  3437. * @mas: The maple state
  3438. * @entry: The entry to store
  3439. *
  3440. * Return: %NULL or the contents that already exists at the requested index
  3441. * otherwise. The maple state needs to be checked for error conditions.
  3442. */
  3443. static inline void *mas_insert(struct ma_state *mas, void *entry)
  3444. {
  3445. MA_WR_STATE(wr_mas, mas, entry);
  3446. /*
  3447. * Inserting a new range inserts either 0, 1, or 2 pivots within the
  3448. * tree. If the insert fits exactly into an existing gap with a value
  3449. * of NULL, then the slot only needs to be written with the new value.
  3450. * If the range being inserted is adjacent to another range, then only a
  3451. * single pivot needs to be inserted (as well as writing the entry). If
  3452. * the new range is within a gap but does not touch any other ranges,
  3453. * then two pivots need to be inserted: the start - 1, and the end. As
  3454. * usual, the entry must be written. Most operations require a new node
  3455. * to be allocated and replace an existing node to ensure RCU safety,
  3456. * when in RCU mode. The exception to requiring a newly allocated node
  3457. * is when inserting at the end of a node (appending). When done
  3458. * carefully, appending can reuse the node in place.
  3459. */
  3460. wr_mas.content = mas_start(mas);
  3461. if (wr_mas.content)
  3462. goto exists;
  3463. mas_wr_preallocate(&wr_mas, entry);
  3464. if (mas_is_err(mas))
  3465. return NULL;
  3466. /* spanning writes always overwrite something */
  3467. if (mas->store_type == wr_spanning_store)
  3468. goto exists;
  3469. /* At this point, we are at the leaf node that needs to be altered. */
  3470. if (mas->store_type != wr_new_root && mas->store_type != wr_store_root) {
  3471. wr_mas.offset_end = mas->offset;
  3472. wr_mas.end_piv = wr_mas.r_max;
  3473. if (wr_mas.content || (mas->last > wr_mas.r_max))
  3474. goto exists;
  3475. }
  3476. mas_wr_store_entry(&wr_mas);
  3477. return wr_mas.content;
  3478. exists:
  3479. mas_set_err(mas, -EEXIST);
  3480. return wr_mas.content;
  3481. }
  3482. /**
  3483. * mas_alloc_cyclic() - Internal call to find somewhere to store an entry
  3484. * @mas: The maple state.
  3485. * @startp: Pointer to ID.
  3486. * @range_lo: Lower bound of range to search.
  3487. * @range_hi: Upper bound of range to search.
  3488. * @entry: The entry to store.
  3489. * @next: Pointer to next ID to allocate.
  3490. * @gfp: The GFP_FLAGS to use for allocations.
  3491. *
  3492. * Return: 0 if the allocation succeeded without wrapping, 1 if the
  3493. * allocation succeeded after wrapping, or -EBUSY if there are no
  3494. * free entries.
  3495. */
  3496. int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
  3497. void *entry, unsigned long range_lo, unsigned long range_hi,
  3498. unsigned long *next, gfp_t gfp)
  3499. {
  3500. unsigned long min = range_lo;
  3501. int ret = 0;
  3502. range_lo = max(min, *next);
  3503. ret = mas_empty_area(mas, range_lo, range_hi, 1);
  3504. if ((mas->tree->ma_flags & MT_FLAGS_ALLOC_WRAPPED) && ret == 0) {
  3505. mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED;
  3506. ret = 1;
  3507. }
  3508. if (ret < 0 && range_lo > min) {
  3509. mas_reset(mas);
  3510. ret = mas_empty_area(mas, min, range_hi, 1);
  3511. if (ret == 0)
  3512. ret = 1;
  3513. }
  3514. if (ret < 0)
  3515. return ret;
  3516. do {
  3517. mas_insert(mas, entry);
  3518. } while (mas_nomem(mas, gfp));
  3519. if (mas_is_err(mas))
  3520. return xa_err(mas->node);
  3521. *startp = mas->index;
  3522. *next = *startp + 1;
  3523. if (*next == 0)
  3524. mas->tree->ma_flags |= MT_FLAGS_ALLOC_WRAPPED;
  3525. mas_destroy(mas);
  3526. return ret;
  3527. }
  3528. EXPORT_SYMBOL(mas_alloc_cyclic);
  3529. static __always_inline void mas_rewalk(struct ma_state *mas, unsigned long index)
  3530. {
  3531. retry:
  3532. mas_set(mas, index);
  3533. mas_state_walk(mas);
  3534. if (mas_is_start(mas))
  3535. goto retry;
  3536. }
  3537. static __always_inline bool mas_rewalk_if_dead(struct ma_state *mas,
  3538. struct maple_node *node, const unsigned long index)
  3539. {
  3540. if (unlikely(ma_dead_node(node))) {
  3541. mas_rewalk(mas, index);
  3542. return true;
  3543. }
  3544. return false;
  3545. }
  3546. /*
  3547. * mas_prev_node() - Find the prev non-null entry at the same level in the
  3548. * tree. The prev value will be mas->node[mas->offset] or the status will be
  3549. * ma_none.
  3550. * @mas: The maple state
  3551. * @min: The lower limit to search
  3552. *
  3553. * The prev node value will be mas->node[mas->offset] or the status will be
  3554. * ma_none.
  3555. * Return: 1 if the node is dead, 0 otherwise.
  3556. */
  3557. static int mas_prev_node(struct ma_state *mas, unsigned long min)
  3558. {
  3559. enum maple_type mt;
  3560. int offset, level;
  3561. void __rcu **slots;
  3562. struct maple_node *node;
  3563. unsigned long *pivots;
  3564. unsigned long max;
  3565. node = mas_mn(mas);
  3566. if (!mas->min)
  3567. goto no_entry;
  3568. max = mas->min - 1;
  3569. if (max < min)
  3570. goto no_entry;
  3571. level = 0;
  3572. do {
  3573. if (ma_is_root(node))
  3574. goto no_entry;
  3575. /* Walk up. */
  3576. if (unlikely(mas_ascend(mas)))
  3577. return 1;
  3578. offset = mas->offset;
  3579. level++;
  3580. node = mas_mn(mas);
  3581. } while (!offset);
  3582. offset--;
  3583. mt = mte_node_type(mas->node);
  3584. while (level > 1) {
  3585. level--;
  3586. slots = ma_slots(node, mt);
  3587. mas->node = mas_slot(mas, slots, offset);
  3588. if (unlikely(ma_dead_node(node)))
  3589. return 1;
  3590. mt = mte_node_type(mas->node);
  3591. node = mas_mn(mas);
  3592. pivots = ma_pivots(node, mt);
  3593. offset = ma_data_end(node, mt, pivots, max);
  3594. if (unlikely(ma_dead_node(node)))
  3595. return 1;
  3596. }
  3597. slots = ma_slots(node, mt);
  3598. mas->node = mas_slot(mas, slots, offset);
  3599. pivots = ma_pivots(node, mt);
  3600. if (unlikely(ma_dead_node(node)))
  3601. return 1;
  3602. if (likely(offset))
  3603. mas->min = pivots[offset - 1] + 1;
  3604. mas->max = max;
  3605. mas->offset = mas_data_end(mas);
  3606. if (unlikely(mte_dead_node(mas->node)))
  3607. return 1;
  3608. mas->end = mas->offset;
  3609. return 0;
  3610. no_entry:
  3611. if (unlikely(ma_dead_node(node)))
  3612. return 1;
  3613. mas->status = ma_underflow;
  3614. return 0;
  3615. }
  3616. /*
  3617. * mas_prev_slot() - Get the entry in the previous slot
  3618. *
  3619. * @mas: The maple state
  3620. * @min: The minimum starting range
  3621. * @empty: Can be empty
  3622. *
  3623. * Return: The entry in the previous slot which is possibly NULL
  3624. */
  3625. static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty)
  3626. {
  3627. void *entry;
  3628. void __rcu **slots;
  3629. unsigned long pivot;
  3630. enum maple_type type;
  3631. unsigned long *pivots;
  3632. struct maple_node *node;
  3633. unsigned long save_point = mas->index;
  3634. retry:
  3635. node = mas_mn(mas);
  3636. type = mte_node_type(mas->node);
  3637. pivots = ma_pivots(node, type);
  3638. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3639. goto retry;
  3640. if (mas->min <= min) {
  3641. pivot = mas_safe_min(mas, pivots, mas->offset);
  3642. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3643. goto retry;
  3644. if (pivot <= min)
  3645. goto underflow;
  3646. }
  3647. again:
  3648. if (likely(mas->offset)) {
  3649. mas->offset--;
  3650. mas->last = mas->index - 1;
  3651. mas->index = mas_safe_min(mas, pivots, mas->offset);
  3652. } else {
  3653. if (mas->index <= min)
  3654. goto underflow;
  3655. if (mas_prev_node(mas, min)) {
  3656. mas_rewalk(mas, save_point);
  3657. goto retry;
  3658. }
  3659. if (WARN_ON_ONCE(mas_is_underflow(mas)))
  3660. return NULL;
  3661. mas->last = mas->max;
  3662. node = mas_mn(mas);
  3663. type = mte_node_type(mas->node);
  3664. pivots = ma_pivots(node, type);
  3665. mas->index = pivots[mas->offset - 1] + 1;
  3666. }
  3667. slots = ma_slots(node, type);
  3668. entry = mas_slot(mas, slots, mas->offset);
  3669. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3670. goto retry;
  3671. if (likely(entry))
  3672. return entry;
  3673. if (!empty) {
  3674. if (mas->index <= min)
  3675. goto underflow;
  3676. goto again;
  3677. }
  3678. return entry;
  3679. underflow:
  3680. mas->status = ma_underflow;
  3681. return NULL;
  3682. }
  3683. /*
  3684. * mas_next_node() - Get the next node at the same level in the tree.
  3685. * @mas: The maple state
  3686. * @node: The maple node
  3687. * @max: The maximum pivot value to check.
  3688. *
  3689. * The next value will be mas->node[mas->offset] or the status will have
  3690. * overflowed.
  3691. * Return: 1 on dead node, 0 otherwise.
  3692. */
  3693. static int mas_next_node(struct ma_state *mas, struct maple_node *node,
  3694. unsigned long max)
  3695. {
  3696. unsigned long min;
  3697. unsigned long *pivots;
  3698. struct maple_enode *enode;
  3699. struct maple_node *tmp;
  3700. int level = 0;
  3701. unsigned char node_end;
  3702. enum maple_type mt;
  3703. void __rcu **slots;
  3704. if (mas->max >= max)
  3705. goto overflow;
  3706. min = mas->max + 1;
  3707. level = 0;
  3708. do {
  3709. if (ma_is_root(node))
  3710. goto overflow;
  3711. /* Walk up. */
  3712. if (unlikely(mas_ascend(mas)))
  3713. return 1;
  3714. level++;
  3715. node = mas_mn(mas);
  3716. mt = mte_node_type(mas->node);
  3717. pivots = ma_pivots(node, mt);
  3718. node_end = ma_data_end(node, mt, pivots, mas->max);
  3719. if (unlikely(ma_dead_node(node)))
  3720. return 1;
  3721. } while (unlikely(mas->offset == node_end));
  3722. slots = ma_slots(node, mt);
  3723. mas->offset++;
  3724. enode = mas_slot(mas, slots, mas->offset);
  3725. if (unlikely(ma_dead_node(node)))
  3726. return 1;
  3727. if (level > 1)
  3728. mas->offset = 0;
  3729. while (unlikely(level > 1)) {
  3730. level--;
  3731. mas->node = enode;
  3732. node = mas_mn(mas);
  3733. mt = mte_node_type(mas->node);
  3734. slots = ma_slots(node, mt);
  3735. enode = mas_slot(mas, slots, 0);
  3736. if (unlikely(ma_dead_node(node)))
  3737. return 1;
  3738. }
  3739. if (!mas->offset)
  3740. pivots = ma_pivots(node, mt);
  3741. mas->max = mas_safe_pivot(mas, pivots, mas->offset, mt);
  3742. tmp = mte_to_node(enode);
  3743. mt = mte_node_type(enode);
  3744. pivots = ma_pivots(tmp, mt);
  3745. mas->end = ma_data_end(tmp, mt, pivots, mas->max);
  3746. if (unlikely(ma_dead_node(node)))
  3747. return 1;
  3748. mas->node = enode;
  3749. mas->min = min;
  3750. return 0;
  3751. overflow:
  3752. if (unlikely(ma_dead_node(node)))
  3753. return 1;
  3754. mas->status = ma_overflow;
  3755. return 0;
  3756. }
  3757. /*
  3758. * mas_next_slot() - Get the entry in the next slot
  3759. *
  3760. * @mas: The maple state
  3761. * @max: The maximum starting range
  3762. * @empty: Can be empty
  3763. *
  3764. * Return: The entry in the next slot which is possibly NULL
  3765. */
  3766. static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)
  3767. {
  3768. void __rcu **slots;
  3769. unsigned long *pivots;
  3770. unsigned long pivot;
  3771. enum maple_type type;
  3772. struct maple_node *node;
  3773. unsigned long save_point = mas->last;
  3774. void *entry;
  3775. retry:
  3776. node = mas_mn(mas);
  3777. type = mte_node_type(mas->node);
  3778. pivots = ma_pivots(node, type);
  3779. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3780. goto retry;
  3781. if (mas->max >= max) {
  3782. if (likely(mas->offset < mas->end))
  3783. pivot = pivots[mas->offset];
  3784. else
  3785. pivot = mas->max;
  3786. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3787. goto retry;
  3788. if (pivot >= max) { /* Was at the limit, next will extend beyond */
  3789. mas->status = ma_overflow;
  3790. return NULL;
  3791. }
  3792. }
  3793. if (likely(mas->offset < mas->end)) {
  3794. mas->index = pivots[mas->offset] + 1;
  3795. again:
  3796. mas->offset++;
  3797. if (likely(mas->offset < mas->end))
  3798. mas->last = pivots[mas->offset];
  3799. else
  3800. mas->last = mas->max;
  3801. } else {
  3802. if (mas->last >= max) {
  3803. mas->status = ma_overflow;
  3804. return NULL;
  3805. }
  3806. if (mas_next_node(mas, node, max)) {
  3807. mas_rewalk(mas, save_point);
  3808. goto retry;
  3809. }
  3810. if (WARN_ON_ONCE(mas_is_overflow(mas)))
  3811. return NULL;
  3812. mas->offset = 0;
  3813. mas->index = mas->min;
  3814. node = mas_mn(mas);
  3815. type = mte_node_type(mas->node);
  3816. pivots = ma_pivots(node, type);
  3817. mas->last = pivots[0];
  3818. }
  3819. slots = ma_slots(node, type);
  3820. entry = mt_slot(mas->tree, slots, mas->offset);
  3821. if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
  3822. goto retry;
  3823. if (entry)
  3824. return entry;
  3825. if (!empty) {
  3826. if (mas->last >= max) {
  3827. mas->status = ma_overflow;
  3828. return NULL;
  3829. }
  3830. mas->index = mas->last + 1;
  3831. goto again;
  3832. }
  3833. return entry;
  3834. }
  3835. /*
  3836. * mas_rev_awalk() - Internal function. Reverse allocation walk. Find the
  3837. * highest gap address of a given size in a given node and descend.
  3838. * @mas: The maple state
  3839. * @size: The needed size.
  3840. *
  3841. * Return: True if found in a leaf, false otherwise.
  3842. *
  3843. */
  3844. static bool mas_rev_awalk(struct ma_state *mas, unsigned long size,
  3845. unsigned long *gap_min, unsigned long *gap_max)
  3846. {
  3847. enum maple_type type = mte_node_type(mas->node);
  3848. struct maple_node *node = mas_mn(mas);
  3849. unsigned long *pivots, *gaps;
  3850. void __rcu **slots;
  3851. unsigned long gap = 0;
  3852. unsigned long max, min;
  3853. unsigned char offset;
  3854. if (unlikely(mas_is_err(mas)))
  3855. return true;
  3856. if (ma_is_dense(type)) {
  3857. /* dense nodes. */
  3858. mas->offset = (unsigned char)(mas->index - mas->min);
  3859. return true;
  3860. }
  3861. pivots = ma_pivots(node, type);
  3862. slots = ma_slots(node, type);
  3863. gaps = ma_gaps(node, type);
  3864. offset = mas->offset;
  3865. min = mas_safe_min(mas, pivots, offset);
  3866. /* Skip out of bounds. */
  3867. while (mas->last < min)
  3868. min = mas_safe_min(mas, pivots, --offset);
  3869. max = mas_safe_pivot(mas, pivots, offset, type);
  3870. while (mas->index <= max) {
  3871. gap = 0;
  3872. if (gaps)
  3873. gap = gaps[offset];
  3874. else if (!mas_slot(mas, slots, offset))
  3875. gap = max - min + 1;
  3876. if (gap) {
  3877. if ((size <= gap) && (size <= mas->last - min + 1))
  3878. break;
  3879. if (!gaps) {
  3880. /* Skip the next slot, it cannot be a gap. */
  3881. if (offset < 2)
  3882. goto ascend;
  3883. offset -= 2;
  3884. max = pivots[offset];
  3885. min = mas_safe_min(mas, pivots, offset);
  3886. continue;
  3887. }
  3888. }
  3889. if (!offset)
  3890. goto ascend;
  3891. offset--;
  3892. max = min - 1;
  3893. min = mas_safe_min(mas, pivots, offset);
  3894. }
  3895. if (unlikely((mas->index > max) || (size - 1 > max - mas->index)))
  3896. goto no_space;
  3897. if (unlikely(ma_is_leaf(type))) {
  3898. mas->offset = offset;
  3899. *gap_min = min;
  3900. *gap_max = min + gap - 1;
  3901. return true;
  3902. }
  3903. /* descend, only happens under lock. */
  3904. mas->node = mas_slot(mas, slots, offset);
  3905. mas->min = min;
  3906. mas->max = max;
  3907. mas->offset = mas_data_end(mas);
  3908. return false;
  3909. ascend:
  3910. if (!mte_is_root(mas->node))
  3911. return false;
  3912. no_space:
  3913. mas_set_err(mas, -EBUSY);
  3914. return false;
  3915. }
  3916. static inline bool mas_anode_descend(struct ma_state *mas, unsigned long size)
  3917. {
  3918. enum maple_type type = mte_node_type(mas->node);
  3919. unsigned long pivot, min, gap = 0;
  3920. unsigned char offset, data_end;
  3921. unsigned long *gaps, *pivots;
  3922. void __rcu **slots;
  3923. struct maple_node *node;
  3924. bool found = false;
  3925. if (ma_is_dense(type)) {
  3926. mas->offset = (unsigned char)(mas->index - mas->min);
  3927. return true;
  3928. }
  3929. node = mas_mn(mas);
  3930. pivots = ma_pivots(node, type);
  3931. slots = ma_slots(node, type);
  3932. gaps = ma_gaps(node, type);
  3933. offset = mas->offset;
  3934. min = mas_safe_min(mas, pivots, offset);
  3935. data_end = ma_data_end(node, type, pivots, mas->max);
  3936. for (; offset <= data_end; offset++) {
  3937. pivot = mas_safe_pivot(mas, pivots, offset, type);
  3938. /* Not within lower bounds */
  3939. if (mas->index > pivot)
  3940. goto next_slot;
  3941. if (gaps)
  3942. gap = gaps[offset];
  3943. else if (!mas_slot(mas, slots, offset))
  3944. gap = min(pivot, mas->last) - max(mas->index, min) + 1;
  3945. else
  3946. goto next_slot;
  3947. if (gap >= size) {
  3948. if (ma_is_leaf(type)) {
  3949. found = true;
  3950. break;
  3951. }
  3952. mas->node = mas_slot(mas, slots, offset);
  3953. mas->min = min;
  3954. mas->max = pivot;
  3955. offset = 0;
  3956. break;
  3957. }
  3958. next_slot:
  3959. min = pivot + 1;
  3960. if (mas->last <= pivot) {
  3961. mas_set_err(mas, -EBUSY);
  3962. return true;
  3963. }
  3964. }
  3965. mas->offset = offset;
  3966. return found;
  3967. }
  3968. /**
  3969. * mas_walk() - Search for @mas->index in the tree.
  3970. * @mas: The maple state.
  3971. *
  3972. * mas->index and mas->last will be set to the range if there is a value. If
  3973. * mas->status is ma_none, reset to ma_start
  3974. *
  3975. * Return: the entry at the location or %NULL.
  3976. */
  3977. void *mas_walk(struct ma_state *mas)
  3978. {
  3979. void *entry;
  3980. if (!mas_is_active(mas) && !mas_is_start(mas))
  3981. mas->status = ma_start;
  3982. retry:
  3983. entry = mas_state_walk(mas);
  3984. if (mas_is_start(mas)) {
  3985. goto retry;
  3986. } else if (mas_is_none(mas)) {
  3987. mas->index = 0;
  3988. mas->last = ULONG_MAX;
  3989. } else if (mas_is_ptr(mas)) {
  3990. if (!mas->index) {
  3991. mas->last = 0;
  3992. return entry;
  3993. }
  3994. mas->index = 1;
  3995. mas->last = ULONG_MAX;
  3996. mas->status = ma_none;
  3997. return NULL;
  3998. }
  3999. return entry;
  4000. }
  4001. EXPORT_SYMBOL_GPL(mas_walk);
  4002. static inline bool mas_rewind_node(struct ma_state *mas)
  4003. {
  4004. unsigned char slot;
  4005. do {
  4006. if (mte_is_root(mas->node)) {
  4007. slot = mas->offset;
  4008. if (!slot)
  4009. return false;
  4010. } else {
  4011. mas_ascend(mas);
  4012. slot = mas->offset;
  4013. }
  4014. } while (!slot);
  4015. mas->offset = --slot;
  4016. return true;
  4017. }
  4018. /*
  4019. * mas_skip_node() - Internal function. Skip over a node.
  4020. * @mas: The maple state.
  4021. *
  4022. * Return: true if there is another node, false otherwise.
  4023. */
  4024. static inline bool mas_skip_node(struct ma_state *mas)
  4025. {
  4026. if (mas_is_err(mas))
  4027. return false;
  4028. do {
  4029. if (mte_is_root(mas->node)) {
  4030. if (mas->offset >= mas_data_end(mas)) {
  4031. mas_set_err(mas, -EBUSY);
  4032. return false;
  4033. }
  4034. } else {
  4035. mas_ascend(mas);
  4036. }
  4037. } while (mas->offset >= mas_data_end(mas));
  4038. mas->offset++;
  4039. return true;
  4040. }
  4041. /*
  4042. * mas_awalk() - Allocation walk. Search from low address to high, for a gap of
  4043. * @size
  4044. * @mas: The maple state
  4045. * @size: The size of the gap required
  4046. *
  4047. * Search between @mas->index and @mas->last for a gap of @size.
  4048. */
  4049. static inline void mas_awalk(struct ma_state *mas, unsigned long size)
  4050. {
  4051. struct maple_enode *last = NULL;
  4052. /*
  4053. * There are 4 options:
  4054. * go to child (descend)
  4055. * go back to parent (ascend)
  4056. * no gap found. (return, error == -EBUSY)
  4057. * found the gap. (return)
  4058. */
  4059. while (!mas_is_err(mas) && !mas_anode_descend(mas, size)) {
  4060. if (last == mas->node)
  4061. mas_skip_node(mas);
  4062. else
  4063. last = mas->node;
  4064. }
  4065. }
  4066. /*
  4067. * mas_sparse_area() - Internal function. Return upper or lower limit when
  4068. * searching for a gap in an empty tree.
  4069. * @mas: The maple state
  4070. * @min: the minimum range
  4071. * @max: The maximum range
  4072. * @size: The size of the gap
  4073. * @fwd: Searching forward or back
  4074. */
  4075. static inline int mas_sparse_area(struct ma_state *mas, unsigned long min,
  4076. unsigned long max, unsigned long size, bool fwd)
  4077. {
  4078. if (!unlikely(mas_is_none(mas)) && min == 0) {
  4079. min++;
  4080. /*
  4081. * At this time, min is increased, we need to recheck whether
  4082. * the size is satisfied.
  4083. */
  4084. if (min > max || max - min + 1 < size)
  4085. return -EBUSY;
  4086. }
  4087. /* mas_is_ptr */
  4088. if (fwd) {
  4089. mas->index = min;
  4090. mas->last = min + size - 1;
  4091. } else {
  4092. mas->last = max;
  4093. mas->index = max - size + 1;
  4094. }
  4095. return 0;
  4096. }
  4097. /*
  4098. * mas_empty_area() - Get the lowest address within the range that is
  4099. * sufficient for the size requested.
  4100. * @mas: The maple state
  4101. * @min: The lowest value of the range
  4102. * @max: The highest value of the range
  4103. * @size: The size needed
  4104. */
  4105. int mas_empty_area(struct ma_state *mas, unsigned long min,
  4106. unsigned long max, unsigned long size)
  4107. {
  4108. unsigned char offset;
  4109. unsigned long *pivots;
  4110. enum maple_type mt;
  4111. struct maple_node *node;
  4112. if (min > max)
  4113. return -EINVAL;
  4114. if (size == 0 || max - min < size - 1)
  4115. return -EINVAL;
  4116. if (mas_is_start(mas))
  4117. mas_start(mas);
  4118. else if (mas->offset >= 2)
  4119. mas->offset -= 2;
  4120. else if (!mas_skip_node(mas))
  4121. return -EBUSY;
  4122. /* Empty set */
  4123. if (mas_is_none(mas) || mas_is_ptr(mas))
  4124. return mas_sparse_area(mas, min, max, size, true);
  4125. /* The start of the window can only be within these values */
  4126. mas->index = min;
  4127. mas->last = max;
  4128. mas_awalk(mas, size);
  4129. if (unlikely(mas_is_err(mas)))
  4130. return xa_err(mas->node);
  4131. offset = mas->offset;
  4132. node = mas_mn(mas);
  4133. mt = mte_node_type(mas->node);
  4134. pivots = ma_pivots(node, mt);
  4135. min = mas_safe_min(mas, pivots, offset);
  4136. if (mas->index < min)
  4137. mas->index = min;
  4138. mas->last = mas->index + size - 1;
  4139. mas->end = ma_data_end(node, mt, pivots, mas->max);
  4140. return 0;
  4141. }
  4142. EXPORT_SYMBOL_GPL(mas_empty_area);
  4143. /*
  4144. * mas_empty_area_rev() - Get the highest address within the range that is
  4145. * sufficient for the size requested.
  4146. * @mas: The maple state
  4147. * @min: The lowest value of the range
  4148. * @max: The highest value of the range
  4149. * @size: The size needed
  4150. */
  4151. int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
  4152. unsigned long max, unsigned long size)
  4153. {
  4154. struct maple_enode *last = mas->node;
  4155. if (min > max)
  4156. return -EINVAL;
  4157. if (size == 0 || max - min < size - 1)
  4158. return -EINVAL;
  4159. if (mas_is_start(mas))
  4160. mas_start(mas);
  4161. else if ((mas->offset < 2) && (!mas_rewind_node(mas)))
  4162. return -EBUSY;
  4163. if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
  4164. return mas_sparse_area(mas, min, max, size, false);
  4165. else if (mas->offset >= 2)
  4166. mas->offset -= 2;
  4167. else
  4168. mas->offset = mas_data_end(mas);
  4169. /* The start of the window can only be within these values. */
  4170. mas->index = min;
  4171. mas->last = max;
  4172. while (!mas_rev_awalk(mas, size, &min, &max)) {
  4173. if (last == mas->node) {
  4174. if (!mas_rewind_node(mas))
  4175. return -EBUSY;
  4176. } else {
  4177. last = mas->node;
  4178. }
  4179. }
  4180. if (mas_is_err(mas))
  4181. return xa_err(mas->node);
  4182. if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
  4183. return -EBUSY;
  4184. /* Trim the upper limit to the max. */
  4185. if (max < mas->last)
  4186. mas->last = max;
  4187. mas->index = mas->last - size + 1;
  4188. mas->end = mas_data_end(mas);
  4189. return 0;
  4190. }
  4191. EXPORT_SYMBOL_GPL(mas_empty_area_rev);
  4192. /*
  4193. * mte_dead_leaves() - Mark all leaves of a node as dead.
  4194. * @enode: the encoded node
  4195. * @mt: the maple tree
  4196. * @slots: Pointer to the slot array
  4197. *
  4198. * Must hold the write lock.
  4199. *
  4200. * Return: The number of leaves marked as dead.
  4201. */
  4202. static inline
  4203. unsigned char mte_dead_leaves(struct maple_enode *enode, struct maple_tree *mt,
  4204. void __rcu **slots)
  4205. {
  4206. struct maple_node *node;
  4207. enum maple_type type;
  4208. void *entry;
  4209. int offset;
  4210. for (offset = 0; offset < mt_slot_count(enode); offset++) {
  4211. entry = mt_slot(mt, slots, offset);
  4212. type = mte_node_type(entry);
  4213. node = mte_to_node(entry);
  4214. /* Use both node and type to catch LE & BE metadata */
  4215. if (!node || !type)
  4216. break;
  4217. mte_set_node_dead(entry);
  4218. node->type = type;
  4219. rcu_assign_pointer(slots[offset], node);
  4220. }
  4221. return offset;
  4222. }
  4223. /**
  4224. * mte_dead_walk() - Walk down a dead tree to just before the leaves
  4225. * @enode: The maple encoded node
  4226. * @offset: The starting offset
  4227. *
  4228. * Note: This can only be used from the RCU callback context.
  4229. */
  4230. static void __rcu **mte_dead_walk(struct maple_enode **enode, unsigned char offset)
  4231. {
  4232. struct maple_node *node, *next;
  4233. void __rcu **slots = NULL;
  4234. next = mte_to_node(*enode);
  4235. do {
  4236. *enode = ma_enode_ptr(next);
  4237. node = mte_to_node(*enode);
  4238. slots = ma_slots(node, node->type);
  4239. next = rcu_dereference_protected(slots[offset],
  4240. lock_is_held(&rcu_callback_map));
  4241. offset = 0;
  4242. } while (!ma_is_leaf(next->type));
  4243. return slots;
  4244. }
  4245. /**
  4246. * mt_free_walk() - Walk & free a tree in the RCU callback context
  4247. * @head: The RCU head that's within the node.
  4248. *
  4249. * Note: This can only be used from the RCU callback context.
  4250. */
  4251. static void mt_free_walk(struct rcu_head *head)
  4252. {
  4253. void __rcu **slots;
  4254. struct maple_node *node, *start;
  4255. struct maple_enode *enode;
  4256. unsigned char offset;
  4257. enum maple_type type;
  4258. node = container_of(head, struct maple_node, rcu);
  4259. if (ma_is_leaf(node->type))
  4260. goto free_leaf;
  4261. start = node;
  4262. enode = mt_mk_node(node, node->type);
  4263. slots = mte_dead_walk(&enode, 0);
  4264. node = mte_to_node(enode);
  4265. do {
  4266. mt_free_bulk(node->slot_len, slots);
  4267. offset = node->parent_slot + 1;
  4268. enode = node->piv_parent;
  4269. if (mte_to_node(enode) == node)
  4270. goto free_leaf;
  4271. type = mte_node_type(enode);
  4272. slots = ma_slots(mte_to_node(enode), type);
  4273. if ((offset < mt_slots[type]) &&
  4274. rcu_dereference_protected(slots[offset],
  4275. lock_is_held(&rcu_callback_map)))
  4276. slots = mte_dead_walk(&enode, offset);
  4277. node = mte_to_node(enode);
  4278. } while ((node != start) || (node->slot_len < offset));
  4279. slots = ma_slots(node, node->type);
  4280. mt_free_bulk(node->slot_len, slots);
  4281. free_leaf:
  4282. kfree(node);
  4283. }
  4284. static inline void __rcu **mte_destroy_descend(struct maple_enode **enode,
  4285. struct maple_tree *mt, struct maple_enode *prev, unsigned char offset)
  4286. {
  4287. struct maple_node *node;
  4288. struct maple_enode *next = *enode;
  4289. void __rcu **slots = NULL;
  4290. enum maple_type type;
  4291. unsigned char next_offset = 0;
  4292. do {
  4293. *enode = next;
  4294. node = mte_to_node(*enode);
  4295. type = mte_node_type(*enode);
  4296. slots = ma_slots(node, type);
  4297. next = mt_slot_locked(mt, slots, next_offset);
  4298. if ((mte_dead_node(next)))
  4299. next = mt_slot_locked(mt, slots, ++next_offset);
  4300. mte_set_node_dead(*enode);
  4301. node->type = type;
  4302. node->piv_parent = prev;
  4303. node->parent_slot = offset;
  4304. offset = next_offset;
  4305. next_offset = 0;
  4306. prev = *enode;
  4307. } while (!mte_is_leaf(next));
  4308. return slots;
  4309. }
  4310. static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
  4311. bool free)
  4312. {
  4313. void __rcu **slots;
  4314. struct maple_node *node = mte_to_node(enode);
  4315. struct maple_enode *start;
  4316. if (mte_is_leaf(enode)) {
  4317. mte_set_node_dead(enode);
  4318. node->type = mte_node_type(enode);
  4319. goto free_leaf;
  4320. }
  4321. start = enode;
  4322. slots = mte_destroy_descend(&enode, mt, start, 0);
  4323. node = mte_to_node(enode); // Updated in the above call.
  4324. do {
  4325. enum maple_type type;
  4326. unsigned char offset;
  4327. struct maple_enode *parent, *tmp;
  4328. node->slot_len = mte_dead_leaves(enode, mt, slots);
  4329. if (free)
  4330. mt_free_bulk(node->slot_len, slots);
  4331. offset = node->parent_slot + 1;
  4332. enode = node->piv_parent;
  4333. if (mte_to_node(enode) == node)
  4334. goto free_leaf;
  4335. type = mte_node_type(enode);
  4336. slots = ma_slots(mte_to_node(enode), type);
  4337. if (offset >= mt_slots[type])
  4338. goto next;
  4339. tmp = mt_slot_locked(mt, slots, offset);
  4340. if (mte_node_type(tmp) && mte_to_node(tmp)) {
  4341. parent = enode;
  4342. enode = tmp;
  4343. slots = mte_destroy_descend(&enode, mt, parent, offset);
  4344. }
  4345. next:
  4346. node = mte_to_node(enode);
  4347. } while (start != enode);
  4348. node = mte_to_node(enode);
  4349. node->slot_len = mte_dead_leaves(enode, mt, slots);
  4350. if (free)
  4351. mt_free_bulk(node->slot_len, slots);
  4352. free_leaf:
  4353. if (free)
  4354. kfree(node);
  4355. else
  4356. mt_clear_meta(mt, node, node->type);
  4357. }
  4358. /*
  4359. * mte_destroy_walk() - Free a tree or sub-tree.
  4360. * @enode: the encoded maple node (maple_enode) to start
  4361. * @mt: the tree to free - needed for node types.
  4362. *
  4363. * Must hold the write lock.
  4364. */
  4365. static inline void mte_destroy_walk(struct maple_enode *enode,
  4366. struct maple_tree *mt)
  4367. {
  4368. struct maple_node *node = mte_to_node(enode);
  4369. if (mt_in_rcu(mt)) {
  4370. mt_destroy_walk(enode, mt, false);
  4371. call_rcu(&node->rcu, mt_free_walk);
  4372. } else {
  4373. mt_destroy_walk(enode, mt, true);
  4374. }
  4375. }
  4376. /* Interface */
  4377. /**
  4378. * mas_store() - Store an @entry.
  4379. * @mas: The maple state.
  4380. * @entry: The entry to store.
  4381. *
  4382. * The @mas->index and @mas->last is used to set the range for the @entry.
  4383. *
  4384. * Return: the first entry between mas->index and mas->last or %NULL.
  4385. */
  4386. void *mas_store(struct ma_state *mas, void *entry)
  4387. {
  4388. MA_WR_STATE(wr_mas, mas, entry);
  4389. trace_ma_write(TP_FCT, mas, 0, entry);
  4390. #ifdef CONFIG_DEBUG_MAPLE_TREE
  4391. if (MAS_WARN_ON(mas, mas->index > mas->last))
  4392. pr_err("Error %lX > %lX " PTR_FMT "\n", mas->index, mas->last,
  4393. entry);
  4394. if (mas->index > mas->last) {
  4395. mas_set_err(mas, -EINVAL);
  4396. return NULL;
  4397. }
  4398. #endif
  4399. /*
  4400. * Storing is the same operation as insert with the added caveat that it
  4401. * can overwrite entries. Although this seems simple enough, one may
  4402. * want to examine what happens if a single store operation was to
  4403. * overwrite multiple entries within a self-balancing B-Tree.
  4404. */
  4405. mas_wr_prealloc_setup(&wr_mas);
  4406. mas->store_type = mas_wr_store_type(&wr_mas);
  4407. if (mas->mas_flags & MA_STATE_PREALLOC) {
  4408. mas_wr_store_entry(&wr_mas);
  4409. MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
  4410. return wr_mas.content;
  4411. }
  4412. mas_prealloc_calc(&wr_mas, entry);
  4413. if (!mas->node_request)
  4414. goto store;
  4415. mas_alloc_nodes(mas, GFP_NOWAIT);
  4416. if (mas_is_err(mas))
  4417. return NULL;
  4418. store:
  4419. mas_wr_store_entry(&wr_mas);
  4420. mas_destroy(mas);
  4421. return wr_mas.content;
  4422. }
  4423. EXPORT_SYMBOL_GPL(mas_store);
  4424. /**
  4425. * mas_store_gfp() - Store a value into the tree.
  4426. * @mas: The maple state
  4427. * @entry: The entry to store
  4428. * @gfp: The GFP_FLAGS to use for allocations if necessary.
  4429. *
  4430. * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
  4431. * be allocated.
  4432. */
  4433. int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp)
  4434. {
  4435. unsigned long index = mas->index;
  4436. unsigned long last = mas->last;
  4437. MA_WR_STATE(wr_mas, mas, entry);
  4438. int ret = 0;
  4439. retry:
  4440. mas_wr_preallocate(&wr_mas, entry);
  4441. if (unlikely(mas_nomem(mas, gfp))) {
  4442. if (!entry)
  4443. __mas_set_range(mas, index, last);
  4444. goto retry;
  4445. }
  4446. if (mas_is_err(mas)) {
  4447. ret = xa_err(mas->node);
  4448. goto out;
  4449. }
  4450. mas_wr_store_entry(&wr_mas);
  4451. out:
  4452. mas_destroy(mas);
  4453. return ret;
  4454. }
  4455. EXPORT_SYMBOL_GPL(mas_store_gfp);
  4456. /**
  4457. * mas_store_prealloc() - Store a value into the tree using memory
  4458. * preallocated in the maple state.
  4459. * @mas: The maple state
  4460. * @entry: The entry to store.
  4461. */
  4462. void mas_store_prealloc(struct ma_state *mas, void *entry)
  4463. {
  4464. MA_WR_STATE(wr_mas, mas, entry);
  4465. if (mas->store_type == wr_store_root) {
  4466. mas_wr_prealloc_setup(&wr_mas);
  4467. goto store;
  4468. }
  4469. mas_wr_walk_descend(&wr_mas);
  4470. if (mas->store_type != wr_spanning_store) {
  4471. /* set wr_mas->content to current slot */
  4472. wr_mas.content = mas_slot_locked(mas, wr_mas.slots, mas->offset);
  4473. mas_wr_end_piv(&wr_mas);
  4474. }
  4475. store:
  4476. trace_ma_write(TP_FCT, mas, 0, entry);
  4477. mas_wr_store_entry(&wr_mas);
  4478. MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
  4479. mas_destroy(mas);
  4480. }
  4481. EXPORT_SYMBOL_GPL(mas_store_prealloc);
  4482. /**
  4483. * mas_preallocate() - Preallocate enough nodes for a store operation
  4484. * @mas: The maple state
  4485. * @entry: The entry that will be stored
  4486. * @gfp: The GFP_FLAGS to use for allocations.
  4487. *
  4488. * Return: 0 on success, -ENOMEM if memory could not be allocated.
  4489. */
  4490. int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)
  4491. {
  4492. MA_WR_STATE(wr_mas, mas, entry);
  4493. mas_wr_prealloc_setup(&wr_mas);
  4494. mas->store_type = mas_wr_store_type(&wr_mas);
  4495. mas_prealloc_calc(&wr_mas, entry);
  4496. if (!mas->node_request)
  4497. goto set_flag;
  4498. mas->mas_flags &= ~MA_STATE_PREALLOC;
  4499. mas_alloc_nodes(mas, gfp);
  4500. if (mas_is_err(mas)) {
  4501. int ret = xa_err(mas->node);
  4502. mas->node_request = 0;
  4503. mas_destroy(mas);
  4504. mas_reset(mas);
  4505. return ret;
  4506. }
  4507. set_flag:
  4508. mas->mas_flags |= MA_STATE_PREALLOC;
  4509. return 0;
  4510. }
  4511. EXPORT_SYMBOL_GPL(mas_preallocate);
  4512. /*
  4513. * mas_destroy() - destroy a maple state.
  4514. * @mas: The maple state
  4515. *
  4516. * Upon completion, check the left-most node and rebalance against the node to
  4517. * the right if necessary. Frees any allocated nodes associated with this maple
  4518. * state.
  4519. */
  4520. void mas_destroy(struct ma_state *mas)
  4521. {
  4522. mas->mas_flags &= ~MA_STATE_PREALLOC;
  4523. mas_empty_nodes(mas);
  4524. }
  4525. EXPORT_SYMBOL_GPL(mas_destroy);
  4526. static void mas_may_activate(struct ma_state *mas)
  4527. {
  4528. if (!mas->node) {
  4529. mas->status = ma_start;
  4530. } else if (mas->index > mas->max || mas->index < mas->min) {
  4531. mas->status = ma_start;
  4532. } else {
  4533. mas->status = ma_active;
  4534. }
  4535. }
  4536. static bool mas_next_setup(struct ma_state *mas, unsigned long max,
  4537. void **entry)
  4538. {
  4539. bool was_none = mas_is_none(mas);
  4540. if (unlikely(mas->last >= max)) {
  4541. mas->status = ma_overflow;
  4542. return true;
  4543. }
  4544. switch (mas->status) {
  4545. case ma_active:
  4546. return false;
  4547. case ma_none:
  4548. fallthrough;
  4549. case ma_pause:
  4550. mas->status = ma_start;
  4551. fallthrough;
  4552. case ma_start:
  4553. mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
  4554. break;
  4555. case ma_overflow:
  4556. /* Overflowed before, but the max changed */
  4557. mas_may_activate(mas);
  4558. break;
  4559. case ma_underflow:
  4560. /* The user expects the mas to be one before where it is */
  4561. mas_may_activate(mas);
  4562. *entry = mas_walk(mas);
  4563. if (*entry)
  4564. return true;
  4565. break;
  4566. case ma_root:
  4567. break;
  4568. case ma_error:
  4569. return true;
  4570. }
  4571. if (likely(mas_is_active(mas))) /* Fast path */
  4572. return false;
  4573. if (mas_is_ptr(mas)) {
  4574. *entry = NULL;
  4575. if (was_none && mas->index == 0) {
  4576. mas->index = mas->last = 0;
  4577. return true;
  4578. }
  4579. mas->index = 1;
  4580. mas->last = ULONG_MAX;
  4581. mas->status = ma_none;
  4582. return true;
  4583. }
  4584. if (mas_is_none(mas))
  4585. return true;
  4586. return false;
  4587. }
  4588. /**
  4589. * mas_next() - Get the next entry.
  4590. * @mas: The maple state
  4591. * @max: The maximum index to check.
  4592. *
  4593. * Returns the next entry after @mas->index.
  4594. * Must hold rcu_read_lock or the write lock.
  4595. * Can return the zero entry.
  4596. *
  4597. * Return: The next entry or %NULL
  4598. */
  4599. void *mas_next(struct ma_state *mas, unsigned long max)
  4600. {
  4601. void *entry = NULL;
  4602. if (mas_next_setup(mas, max, &entry))
  4603. return entry;
  4604. /* Retries on dead nodes handled by mas_next_slot */
  4605. return mas_next_slot(mas, max, false);
  4606. }
  4607. EXPORT_SYMBOL_GPL(mas_next);
  4608. /**
  4609. * mas_next_range() - Advance the maple state to the next range
  4610. * @mas: The maple state
  4611. * @max: The maximum index to check.
  4612. *
  4613. * Sets @mas->index and @mas->last to the range.
  4614. * Must hold rcu_read_lock or the write lock.
  4615. * Can return the zero entry.
  4616. *
  4617. * Return: The next entry or %NULL
  4618. */
  4619. void *mas_next_range(struct ma_state *mas, unsigned long max)
  4620. {
  4621. void *entry = NULL;
  4622. if (mas_next_setup(mas, max, &entry))
  4623. return entry;
  4624. /* Retries on dead nodes handled by mas_next_slot */
  4625. return mas_next_slot(mas, max, true);
  4626. }
  4627. EXPORT_SYMBOL_GPL(mas_next_range);
  4628. /**
  4629. * mt_next() - get the next value in the maple tree
  4630. * @mt: The maple tree
  4631. * @index: The start index
  4632. * @max: The maximum index to check
  4633. *
  4634. * Takes RCU read lock internally to protect the search, which does not
  4635. * protect the returned pointer after dropping RCU read lock.
  4636. * See also: Documentation/core-api/maple_tree.rst
  4637. *
  4638. * Return: The entry higher than @index or %NULL if nothing is found.
  4639. */
  4640. void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
  4641. {
  4642. void *entry = NULL;
  4643. MA_STATE(mas, mt, index, index);
  4644. rcu_read_lock();
  4645. entry = mas_next(&mas, max);
  4646. rcu_read_unlock();
  4647. return entry;
  4648. }
  4649. EXPORT_SYMBOL_GPL(mt_next);
  4650. static bool mas_prev_setup(struct ma_state *mas, unsigned long min, void **entry)
  4651. {
  4652. if (unlikely(mas->index <= min)) {
  4653. mas->status = ma_underflow;
  4654. return true;
  4655. }
  4656. switch (mas->status) {
  4657. case ma_active:
  4658. return false;
  4659. case ma_start:
  4660. break;
  4661. case ma_none:
  4662. fallthrough;
  4663. case ma_pause:
  4664. mas->status = ma_start;
  4665. break;
  4666. case ma_underflow:
  4667. /* underflowed before but the min changed */
  4668. mas_may_activate(mas);
  4669. break;
  4670. case ma_overflow:
  4671. /* User expects mas to be one after where it is */
  4672. mas_may_activate(mas);
  4673. *entry = mas_walk(mas);
  4674. if (*entry)
  4675. return true;
  4676. break;
  4677. case ma_root:
  4678. break;
  4679. case ma_error:
  4680. return true;
  4681. }
  4682. if (mas_is_start(mas))
  4683. mas_walk(mas);
  4684. if (unlikely(mas_is_ptr(mas))) {
  4685. if (!mas->index) {
  4686. mas->status = ma_none;
  4687. return true;
  4688. }
  4689. mas->index = mas->last = 0;
  4690. *entry = mas_root(mas);
  4691. return true;
  4692. }
  4693. if (mas_is_none(mas)) {
  4694. if (mas->index) {
  4695. /* Walked to out-of-range pointer? */
  4696. mas->index = mas->last = 0;
  4697. mas->status = ma_root;
  4698. *entry = mas_root(mas);
  4699. return true;
  4700. }
  4701. return true;
  4702. }
  4703. return false;
  4704. }
  4705. /**
  4706. * mas_prev() - Get the previous entry
  4707. * @mas: The maple state
  4708. * @min: The minimum value to check.
  4709. *
  4710. * Must hold rcu_read_lock or the write lock.
  4711. * Will reset mas to ma_start if the status is ma_none. Will stop on not
  4712. * searchable nodes.
  4713. *
  4714. * Return: the previous value or %NULL.
  4715. */
  4716. void *mas_prev(struct ma_state *mas, unsigned long min)
  4717. {
  4718. void *entry = NULL;
  4719. if (mas_prev_setup(mas, min, &entry))
  4720. return entry;
  4721. return mas_prev_slot(mas, min, false);
  4722. }
  4723. EXPORT_SYMBOL_GPL(mas_prev);
  4724. /**
  4725. * mas_prev_range() - Advance to the previous range
  4726. * @mas: The maple state
  4727. * @min: The minimum value to check.
  4728. *
  4729. * Sets @mas->index and @mas->last to the range.
  4730. * Must hold rcu_read_lock or the write lock.
  4731. * Will reset mas to ma_start if the node is ma_none. Will stop on not
  4732. * searchable nodes.
  4733. *
  4734. * Return: the previous value or %NULL.
  4735. */
  4736. void *mas_prev_range(struct ma_state *mas, unsigned long min)
  4737. {
  4738. void *entry = NULL;
  4739. if (mas_prev_setup(mas, min, &entry))
  4740. return entry;
  4741. return mas_prev_slot(mas, min, true);
  4742. }
  4743. EXPORT_SYMBOL_GPL(mas_prev_range);
  4744. /**
  4745. * mt_prev() - get the previous value in the maple tree
  4746. * @mt: The maple tree
  4747. * @index: The start index
  4748. * @min: The minimum index to check
  4749. *
  4750. * Takes RCU read lock internally to protect the search, which does not
  4751. * protect the returned pointer after dropping RCU read lock.
  4752. * See also: Documentation/core-api/maple_tree.rst
  4753. *
  4754. * Return: The entry before @index or %NULL if nothing is found.
  4755. */
  4756. void *mt_prev(struct maple_tree *mt, unsigned long index, unsigned long min)
  4757. {
  4758. void *entry = NULL;
  4759. MA_STATE(mas, mt, index, index);
  4760. rcu_read_lock();
  4761. entry = mas_prev(&mas, min);
  4762. rcu_read_unlock();
  4763. return entry;
  4764. }
  4765. EXPORT_SYMBOL_GPL(mt_prev);
  4766. /**
  4767. * mas_pause() - Pause a mas_find/mas_for_each to drop the lock.
  4768. * @mas: The maple state to pause
  4769. *
  4770. * Some users need to pause a walk and drop the lock they're holding in
  4771. * order to yield to a higher priority thread or carry out an operation
  4772. * on an entry. Those users should call this function before they drop
  4773. * the lock. It resets the @mas to be suitable for the next iteration
  4774. * of the loop after the user has reacquired the lock. If most entries
  4775. * found during a walk require you to call mas_pause(), the mt_for_each()
  4776. * iterator may be more appropriate.
  4777. *
  4778. */
  4779. void mas_pause(struct ma_state *mas)
  4780. {
  4781. mas->status = ma_pause;
  4782. mas->node = NULL;
  4783. }
  4784. EXPORT_SYMBOL_GPL(mas_pause);
  4785. /**
  4786. * mas_find_setup() - Internal function to set up mas_find*().
  4787. * @mas: The maple state
  4788. * @max: The maximum index
  4789. * @entry: Pointer to the entry
  4790. *
  4791. * Returns: True if entry is the answer, false otherwise.
  4792. */
  4793. static __always_inline bool mas_find_setup(struct ma_state *mas, unsigned long max, void **entry)
  4794. {
  4795. switch (mas->status) {
  4796. case ma_active:
  4797. if (mas->last < max)
  4798. return false;
  4799. return true;
  4800. case ma_start:
  4801. break;
  4802. case ma_pause:
  4803. if (unlikely(mas->last >= max))
  4804. return true;
  4805. mas->index = ++mas->last;
  4806. mas->status = ma_start;
  4807. break;
  4808. case ma_none:
  4809. if (unlikely(mas->last >= max))
  4810. return true;
  4811. mas->index = mas->last;
  4812. mas->status = ma_start;
  4813. break;
  4814. case ma_underflow:
  4815. /* mas is pointing at entry before unable to go lower */
  4816. if (unlikely(mas->index >= max)) {
  4817. mas->status = ma_overflow;
  4818. return true;
  4819. }
  4820. mas_may_activate(mas);
  4821. *entry = mas_walk(mas);
  4822. if (*entry)
  4823. return true;
  4824. break;
  4825. case ma_overflow:
  4826. if (unlikely(mas->last >= max))
  4827. return true;
  4828. mas_may_activate(mas);
  4829. *entry = mas_walk(mas);
  4830. if (*entry)
  4831. return true;
  4832. break;
  4833. case ma_root:
  4834. break;
  4835. case ma_error:
  4836. return true;
  4837. }
  4838. if (mas_is_start(mas)) {
  4839. /* First run or continue */
  4840. if (mas->index > max)
  4841. return true;
  4842. *entry = mas_walk(mas);
  4843. if (*entry)
  4844. return true;
  4845. }
  4846. if (unlikely(mas_is_ptr(mas)))
  4847. goto ptr_out_of_range;
  4848. if (unlikely(mas_is_none(mas)))
  4849. return true;
  4850. if (mas->index == max)
  4851. return true;
  4852. return false;
  4853. ptr_out_of_range:
  4854. mas->status = ma_none;
  4855. mas->index = 1;
  4856. mas->last = ULONG_MAX;
  4857. return true;
  4858. }
  4859. /**
  4860. * mas_find() - On the first call, find the entry at or after mas->index up to
  4861. * %max. Otherwise, find the entry after mas->index.
  4862. * @mas: The maple state
  4863. * @max: The maximum value to check.
  4864. *
  4865. * Must hold rcu_read_lock or the write lock.
  4866. * If an entry exists, last and index are updated accordingly.
  4867. * May set @mas->status to ma_overflow.
  4868. *
  4869. * Return: The entry or %NULL.
  4870. */
  4871. void *mas_find(struct ma_state *mas, unsigned long max)
  4872. {
  4873. void *entry = NULL;
  4874. if (mas_find_setup(mas, max, &entry))
  4875. return entry;
  4876. /* Retries on dead nodes handled by mas_next_slot */
  4877. entry = mas_next_slot(mas, max, false);
  4878. /* Ignore overflow */
  4879. mas->status = ma_active;
  4880. return entry;
  4881. }
  4882. EXPORT_SYMBOL_GPL(mas_find);
  4883. /**
  4884. * mas_find_range() - On the first call, find the entry at or after
  4885. * mas->index up to %max. Otherwise, advance to the next slot mas->index.
  4886. * @mas: The maple state
  4887. * @max: The maximum value to check.
  4888. *
  4889. * Must hold rcu_read_lock or the write lock.
  4890. * If an entry exists, last and index are updated accordingly.
  4891. * May set @mas->status to ma_overflow.
  4892. *
  4893. * Return: The entry or %NULL.
  4894. */
  4895. void *mas_find_range(struct ma_state *mas, unsigned long max)
  4896. {
  4897. void *entry = NULL;
  4898. if (mas_find_setup(mas, max, &entry))
  4899. return entry;
  4900. /* Retries on dead nodes handled by mas_next_slot */
  4901. return mas_next_slot(mas, max, true);
  4902. }
  4903. EXPORT_SYMBOL_GPL(mas_find_range);
  4904. /**
  4905. * mas_find_rev_setup() - Internal function to set up mas_find_*_rev()
  4906. * @mas: The maple state
  4907. * @min: The minimum index
  4908. * @entry: Pointer to the entry
  4909. *
  4910. * Returns: True if entry is the answer, false otherwise.
  4911. */
  4912. static bool mas_find_rev_setup(struct ma_state *mas, unsigned long min,
  4913. void **entry)
  4914. {
  4915. switch (mas->status) {
  4916. case ma_active:
  4917. goto active;
  4918. case ma_start:
  4919. break;
  4920. case ma_pause:
  4921. if (unlikely(mas->index <= min)) {
  4922. mas->status = ma_underflow;
  4923. return true;
  4924. }
  4925. mas->last = --mas->index;
  4926. mas->status = ma_start;
  4927. break;
  4928. case ma_none:
  4929. if (mas->index <= min)
  4930. goto none;
  4931. mas->last = mas->index;
  4932. mas->status = ma_start;
  4933. break;
  4934. case ma_overflow: /* user expects the mas to be one after where it is */
  4935. if (unlikely(mas->index <= min)) {
  4936. mas->status = ma_underflow;
  4937. return true;
  4938. }
  4939. mas->status = ma_active;
  4940. break;
  4941. case ma_underflow: /* user expects the mas to be one before where it is */
  4942. if (unlikely(mas->index <= min))
  4943. return true;
  4944. mas->status = ma_active;
  4945. break;
  4946. case ma_root:
  4947. break;
  4948. case ma_error:
  4949. return true;
  4950. }
  4951. if (mas_is_start(mas)) {
  4952. /* First run or continue */
  4953. if (mas->index < min)
  4954. return true;
  4955. *entry = mas_walk(mas);
  4956. if (*entry)
  4957. return true;
  4958. }
  4959. if (unlikely(mas_is_ptr(mas)))
  4960. goto none;
  4961. if (unlikely(mas_is_none(mas))) {
  4962. /*
  4963. * Walked to the location, and there was nothing so the previous
  4964. * location is 0.
  4965. */
  4966. mas->last = mas->index = 0;
  4967. mas->status = ma_root;
  4968. *entry = mas_root(mas);
  4969. return true;
  4970. }
  4971. active:
  4972. if (mas->index < min)
  4973. return true;
  4974. return false;
  4975. none:
  4976. mas->status = ma_none;
  4977. return true;
  4978. }
  4979. /**
  4980. * mas_find_rev: On the first call, find the first non-null entry at or below
  4981. * mas->index down to %min. Otherwise find the first non-null entry below
  4982. * mas->index down to %min.
  4983. * @mas: The maple state
  4984. * @min: The minimum value to check.
  4985. *
  4986. * Must hold rcu_read_lock or the write lock.
  4987. * If an entry exists, last and index are updated accordingly.
  4988. * May set @mas->status to ma_underflow.
  4989. *
  4990. * Return: The entry or %NULL.
  4991. */
  4992. void *mas_find_rev(struct ma_state *mas, unsigned long min)
  4993. {
  4994. void *entry = NULL;
  4995. if (mas_find_rev_setup(mas, min, &entry))
  4996. return entry;
  4997. /* Retries on dead nodes handled by mas_prev_slot */
  4998. return mas_prev_slot(mas, min, false);
  4999. }
  5000. EXPORT_SYMBOL_GPL(mas_find_rev);
  5001. /**
  5002. * mas_find_range_rev: On the first call, find the first non-null entry at or
  5003. * below mas->index down to %min. Otherwise advance to the previous slot after
  5004. * mas->index down to %min.
  5005. * @mas: The maple state
  5006. * @min: The minimum value to check.
  5007. *
  5008. * Must hold rcu_read_lock or the write lock.
  5009. * If an entry exists, last and index are updated accordingly.
  5010. * May set @mas->status to ma_underflow.
  5011. *
  5012. * Return: The entry or %NULL.
  5013. */
  5014. void *mas_find_range_rev(struct ma_state *mas, unsigned long min)
  5015. {
  5016. void *entry = NULL;
  5017. if (mas_find_rev_setup(mas, min, &entry))
  5018. return entry;
  5019. /* Retries on dead nodes handled by mas_prev_slot */
  5020. return mas_prev_slot(mas, min, true);
  5021. }
  5022. EXPORT_SYMBOL_GPL(mas_find_range_rev);
  5023. /**
  5024. * mas_erase() - Find the range in which index resides and erase the entire
  5025. * range.
  5026. * @mas: The maple state
  5027. *
  5028. * Must hold the write lock.
  5029. * Searches for @mas->index, sets @mas->index and @mas->last to the range and
  5030. * erases that range.
  5031. *
  5032. * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated.
  5033. */
  5034. void *mas_erase(struct ma_state *mas)
  5035. {
  5036. void *entry;
  5037. unsigned long index = mas->index;
  5038. MA_WR_STATE(wr_mas, mas, NULL);
  5039. if (!mas_is_active(mas) || !mas_is_start(mas))
  5040. mas->status = ma_start;
  5041. write_retry:
  5042. entry = mas_state_walk(mas);
  5043. if (!entry)
  5044. return NULL;
  5045. /* Must reset to ensure spanning writes of last slot are detected */
  5046. mas_reset(mas);
  5047. mas_wr_preallocate(&wr_mas, NULL);
  5048. if (mas_nomem(mas, GFP_KERNEL)) {
  5049. /* in case the range of entry changed when unlocked */
  5050. mas->index = mas->last = index;
  5051. goto write_retry;
  5052. }
  5053. if (mas_is_err(mas))
  5054. goto out;
  5055. mas_wr_store_entry(&wr_mas);
  5056. out:
  5057. mas_destroy(mas);
  5058. return entry;
  5059. }
  5060. EXPORT_SYMBOL_GPL(mas_erase);
  5061. /**
  5062. * mas_nomem() - Check if there was an error allocating and do the allocation
  5063. * if necessary If there are allocations, then free them.
  5064. * @mas: The maple state
  5065. * @gfp: The GFP_FLAGS to use for allocations
  5066. * Return: true on allocation, false otherwise.
  5067. */
  5068. bool mas_nomem(struct ma_state *mas, gfp_t gfp)
  5069. __must_hold(mas->tree->ma_lock)
  5070. {
  5071. if (likely(mas->node != MA_ERROR(-ENOMEM)))
  5072. return false;
  5073. if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
  5074. mtree_unlock(mas->tree);
  5075. mas_alloc_nodes(mas, gfp);
  5076. mtree_lock(mas->tree);
  5077. } else {
  5078. mas_alloc_nodes(mas, gfp);
  5079. }
  5080. if (!mas->sheaf && !mas->alloc)
  5081. return false;
  5082. mas->status = ma_start;
  5083. return true;
  5084. }
  5085. void __init maple_tree_init(void)
  5086. {
  5087. struct kmem_cache_args args = {
  5088. .align = sizeof(struct maple_node),
  5089. .sheaf_capacity = 32,
  5090. };
  5091. maple_node_cache = kmem_cache_create("maple_node",
  5092. sizeof(struct maple_node), &args,
  5093. SLAB_PANIC);
  5094. }
  5095. /**
  5096. * mtree_load() - Load a value stored in a maple tree
  5097. * @mt: The maple tree
  5098. * @index: The index to load
  5099. *
  5100. * Return: the entry or %NULL
  5101. */
  5102. void *mtree_load(struct maple_tree *mt, unsigned long index)
  5103. {
  5104. MA_STATE(mas, mt, index, index);
  5105. void *entry;
  5106. trace_ma_read(TP_FCT, &mas);
  5107. rcu_read_lock();
  5108. retry:
  5109. entry = mas_start(&mas);
  5110. if (unlikely(mas_is_none(&mas)))
  5111. goto unlock;
  5112. if (unlikely(mas_is_ptr(&mas))) {
  5113. if (index)
  5114. entry = NULL;
  5115. goto unlock;
  5116. }
  5117. entry = mtree_lookup_walk(&mas);
  5118. if (!entry && unlikely(mas_is_start(&mas)))
  5119. goto retry;
  5120. unlock:
  5121. rcu_read_unlock();
  5122. if (xa_is_zero(entry))
  5123. return NULL;
  5124. return entry;
  5125. }
  5126. EXPORT_SYMBOL(mtree_load);
  5127. /**
  5128. * mtree_store_range() - Store an entry at a given range.
  5129. * @mt: The maple tree
  5130. * @index: The start of the range
  5131. * @last: The end of the range
  5132. * @entry: The entry to store
  5133. * @gfp: The GFP_FLAGS to use for allocations
  5134. *
  5135. * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
  5136. * be allocated.
  5137. */
  5138. int mtree_store_range(struct maple_tree *mt, unsigned long index,
  5139. unsigned long last, void *entry, gfp_t gfp)
  5140. {
  5141. MA_STATE(mas, mt, index, last);
  5142. int ret = 0;
  5143. trace_ma_write(TP_FCT, &mas, 0, entry);
  5144. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  5145. return -EINVAL;
  5146. if (index > last)
  5147. return -EINVAL;
  5148. mtree_lock(mt);
  5149. ret = mas_store_gfp(&mas, entry, gfp);
  5150. mtree_unlock(mt);
  5151. return ret;
  5152. }
  5153. EXPORT_SYMBOL(mtree_store_range);
  5154. /**
  5155. * mtree_store() - Store an entry at a given index.
  5156. * @mt: The maple tree
  5157. * @index: The index to store the value
  5158. * @entry: The entry to store
  5159. * @gfp: The GFP_FLAGS to use for allocations
  5160. *
  5161. * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
  5162. * be allocated.
  5163. */
  5164. int mtree_store(struct maple_tree *mt, unsigned long index, void *entry,
  5165. gfp_t gfp)
  5166. {
  5167. return mtree_store_range(mt, index, index, entry, gfp);
  5168. }
  5169. EXPORT_SYMBOL(mtree_store);
  5170. /**
  5171. * mtree_insert_range() - Insert an entry at a given range if there is no value.
  5172. * @mt: The maple tree
  5173. * @first: The start of the range
  5174. * @last: The end of the range
  5175. * @entry: The entry to store
  5176. * @gfp: The GFP_FLAGS to use for allocations.
  5177. *
  5178. * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
  5179. * request, -ENOMEM if memory could not be allocated.
  5180. */
  5181. int mtree_insert_range(struct maple_tree *mt, unsigned long first,
  5182. unsigned long last, void *entry, gfp_t gfp)
  5183. {
  5184. MA_STATE(ms, mt, first, last);
  5185. int ret = 0;
  5186. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  5187. return -EINVAL;
  5188. if (first > last)
  5189. return -EINVAL;
  5190. mtree_lock(mt);
  5191. retry:
  5192. mas_insert(&ms, entry);
  5193. if (mas_nomem(&ms, gfp))
  5194. goto retry;
  5195. mtree_unlock(mt);
  5196. if (mas_is_err(&ms))
  5197. ret = xa_err(ms.node);
  5198. mas_destroy(&ms);
  5199. return ret;
  5200. }
  5201. EXPORT_SYMBOL(mtree_insert_range);
  5202. /**
  5203. * mtree_insert() - Insert an entry at a given index if there is no value.
  5204. * @mt: The maple tree
  5205. * @index : The index to store the value
  5206. * @entry: The entry to store
  5207. * @gfp: The GFP_FLAGS to use for allocations.
  5208. *
  5209. * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
  5210. * request, -ENOMEM if memory could not be allocated.
  5211. */
  5212. int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry,
  5213. gfp_t gfp)
  5214. {
  5215. return mtree_insert_range(mt, index, index, entry, gfp);
  5216. }
  5217. EXPORT_SYMBOL(mtree_insert);
  5218. int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
  5219. void *entry, unsigned long size, unsigned long min,
  5220. unsigned long max, gfp_t gfp)
  5221. {
  5222. int ret = 0;
  5223. MA_STATE(mas, mt, 0, 0);
  5224. if (!mt_is_alloc(mt))
  5225. return -EINVAL;
  5226. if (WARN_ON_ONCE(mt_is_reserved(entry)))
  5227. return -EINVAL;
  5228. mtree_lock(mt);
  5229. retry:
  5230. ret = mas_empty_area(&mas, min, max, size);
  5231. if (ret)
  5232. goto unlock;
  5233. mas_insert(&mas, entry);
  5234. /*
  5235. * mas_nomem() may release the lock, causing the allocated area
  5236. * to be unavailable, so try to allocate a free area again.
  5237. */
  5238. if (mas_nomem(&mas, gfp))
  5239. goto retry;
  5240. if (mas_is_err(&mas))
  5241. ret = xa_err(mas.node);
  5242. else
  5243. *startp = mas.index;
  5244. unlock:
  5245. mtree_unlock(mt);
  5246. mas_destroy(&mas);
  5247. return ret;
  5248. }
  5249. EXPORT_SYMBOL(mtree_alloc_range);
  5250. /**
  5251. * mtree_alloc_cyclic() - Find somewhere to store this entry in the tree.
  5252. * @mt: The maple tree.
  5253. * @startp: Pointer to ID.
  5254. * @range_lo: Lower bound of range to search.
  5255. * @range_hi: Upper bound of range to search.
  5256. * @entry: The entry to store.
  5257. * @next: Pointer to next ID to allocate.
  5258. * @gfp: The GFP_FLAGS to use for allocations.
  5259. *
  5260. * Finds an empty entry in @mt after @next, stores the new index into
  5261. * the @id pointer, stores the entry at that index, then updates @next.
  5262. *
  5263. * @mt must be initialized with the MT_FLAGS_ALLOC_RANGE flag.
  5264. *
  5265. * Context: Any context. Takes and releases the mt.lock. May sleep if
  5266. * the @gfp flags permit.
  5267. *
  5268. * Return: 0 if the allocation succeeded without wrapping, 1 if the
  5269. * allocation succeeded after wrapping, -ENOMEM if memory could not be
  5270. * allocated, -EINVAL if @mt cannot be used, or -EBUSY if there are no
  5271. * free entries.
  5272. */
  5273. int mtree_alloc_cyclic(struct maple_tree *mt, unsigned long *startp,
  5274. void *entry, unsigned long range_lo, unsigned long range_hi,
  5275. unsigned long *next, gfp_t gfp)
  5276. {
  5277. int ret;
  5278. MA_STATE(mas, mt, 0, 0);
  5279. if (!mt_is_alloc(mt))
  5280. return -EINVAL;
  5281. if (WARN_ON_ONCE(mt_is_reserved(entry)))
  5282. return -EINVAL;
  5283. mtree_lock(mt);
  5284. ret = mas_alloc_cyclic(&mas, startp, entry, range_lo, range_hi,
  5285. next, gfp);
  5286. mtree_unlock(mt);
  5287. return ret;
  5288. }
  5289. EXPORT_SYMBOL(mtree_alloc_cyclic);
  5290. int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
  5291. void *entry, unsigned long size, unsigned long min,
  5292. unsigned long max, gfp_t gfp)
  5293. {
  5294. int ret = 0;
  5295. MA_STATE(mas, mt, 0, 0);
  5296. if (!mt_is_alloc(mt))
  5297. return -EINVAL;
  5298. if (WARN_ON_ONCE(mt_is_reserved(entry)))
  5299. return -EINVAL;
  5300. mtree_lock(mt);
  5301. retry:
  5302. ret = mas_empty_area_rev(&mas, min, max, size);
  5303. if (ret)
  5304. goto unlock;
  5305. mas_insert(&mas, entry);
  5306. /*
  5307. * mas_nomem() may release the lock, causing the allocated area
  5308. * to be unavailable, so try to allocate a free area again.
  5309. */
  5310. if (mas_nomem(&mas, gfp))
  5311. goto retry;
  5312. if (mas_is_err(&mas))
  5313. ret = xa_err(mas.node);
  5314. else
  5315. *startp = mas.index;
  5316. unlock:
  5317. mtree_unlock(mt);
  5318. mas_destroy(&mas);
  5319. return ret;
  5320. }
  5321. EXPORT_SYMBOL(mtree_alloc_rrange);
  5322. /**
  5323. * mtree_erase() - Find an index and erase the entire range.
  5324. * @mt: The maple tree
  5325. * @index: The index to erase
  5326. *
  5327. * Erasing is the same as a walk to an entry then a store of a NULL to that
  5328. * ENTIRE range. In fact, it is implemented as such using the advanced API.
  5329. *
  5330. * Return: The entry stored at the @index or %NULL
  5331. */
  5332. void *mtree_erase(struct maple_tree *mt, unsigned long index)
  5333. {
  5334. void *entry = NULL;
  5335. MA_STATE(mas, mt, index, index);
  5336. trace_ma_op(TP_FCT, &mas);
  5337. mtree_lock(mt);
  5338. entry = mas_erase(&mas);
  5339. mtree_unlock(mt);
  5340. return entry;
  5341. }
  5342. EXPORT_SYMBOL(mtree_erase);
  5343. /*
  5344. * mas_dup_free() - Free an incomplete duplication of a tree.
  5345. * @mas: The maple state of a incomplete tree.
  5346. *
  5347. * The parameter @mas->node passed in indicates that the allocation failed on
  5348. * this node. This function frees all nodes starting from @mas->node in the
  5349. * reverse order of mas_dup_build(). There is no need to hold the source tree
  5350. * lock at this time.
  5351. */
  5352. static void mas_dup_free(struct ma_state *mas)
  5353. {
  5354. struct maple_node *node;
  5355. enum maple_type type;
  5356. void __rcu **slots;
  5357. unsigned char count, i;
  5358. /* Maybe the first node allocation failed. */
  5359. if (mas_is_none(mas))
  5360. return;
  5361. while (!mte_is_root(mas->node)) {
  5362. mas_ascend(mas);
  5363. if (mas->offset) {
  5364. mas->offset--;
  5365. do {
  5366. mas_descend(mas);
  5367. mas->offset = mas_data_end(mas);
  5368. } while (!mte_is_leaf(mas->node));
  5369. mas_ascend(mas);
  5370. }
  5371. node = mte_to_node(mas->node);
  5372. type = mte_node_type(mas->node);
  5373. slots = ma_slots(node, type);
  5374. count = mas_data_end(mas) + 1;
  5375. for (i = 0; i < count; i++)
  5376. ((unsigned long *)slots)[i] &= ~MAPLE_NODE_MASK;
  5377. mt_free_bulk(count, slots);
  5378. }
  5379. node = mte_to_node(mas->node);
  5380. kfree(node);
  5381. }
  5382. /*
  5383. * mas_copy_node() - Copy a maple node and replace the parent.
  5384. * @mas: The maple state of source tree.
  5385. * @new_mas: The maple state of new tree.
  5386. * @parent: The parent of the new node.
  5387. *
  5388. * Copy @mas->node to @new_mas->node, set @parent to be the parent of
  5389. * @new_mas->node. If memory allocation fails, @mas is set to -ENOMEM.
  5390. */
  5391. static inline void mas_copy_node(struct ma_state *mas, struct ma_state *new_mas,
  5392. struct maple_pnode *parent)
  5393. {
  5394. struct maple_node *node = mte_to_node(mas->node);
  5395. struct maple_node *new_node = mte_to_node(new_mas->node);
  5396. unsigned long val;
  5397. /* Copy the node completely. */
  5398. memcpy(new_node, node, sizeof(struct maple_node));
  5399. /* Update the parent node pointer. */
  5400. val = (unsigned long)node->parent & MAPLE_NODE_MASK;
  5401. new_node->parent = ma_parent_ptr(val | (unsigned long)parent);
  5402. }
  5403. /*
  5404. * mas_dup_alloc() - Allocate child nodes for a maple node.
  5405. * @mas: The maple state of source tree.
  5406. * @new_mas: The maple state of new tree.
  5407. * @gfp: The GFP_FLAGS to use for allocations.
  5408. *
  5409. * This function allocates child nodes for @new_mas->node during the duplication
  5410. * process. If memory allocation fails, @mas is set to -ENOMEM.
  5411. */
  5412. static inline void mas_dup_alloc(struct ma_state *mas, struct ma_state *new_mas,
  5413. gfp_t gfp)
  5414. {
  5415. struct maple_node *node = mte_to_node(mas->node);
  5416. struct maple_node *new_node = mte_to_node(new_mas->node);
  5417. enum maple_type type;
  5418. unsigned char count, i;
  5419. void __rcu **slots;
  5420. void __rcu **new_slots;
  5421. unsigned long val;
  5422. /* Allocate memory for child nodes. */
  5423. type = mte_node_type(mas->node);
  5424. new_slots = ma_slots(new_node, type);
  5425. count = mas->node_request = mas_data_end(mas) + 1;
  5426. mas_alloc_nodes(mas, gfp);
  5427. if (unlikely(mas_is_err(mas)))
  5428. return;
  5429. slots = ma_slots(node, type);
  5430. for (i = 0; i < count; i++) {
  5431. val = (unsigned long)mt_slot_locked(mas->tree, slots, i);
  5432. val &= MAPLE_NODE_MASK;
  5433. new_slots[i] = ma_mnode_ptr((unsigned long)mas_pop_node(mas) |
  5434. val);
  5435. }
  5436. }
  5437. /*
  5438. * mas_dup_build() - Build a new maple tree from a source tree
  5439. * @mas: The maple state of source tree, need to be in MAS_START state.
  5440. * @new_mas: The maple state of new tree, need to be in MAS_START state.
  5441. * @gfp: The GFP_FLAGS to use for allocations.
  5442. *
  5443. * This function builds a new tree in DFS preorder. If the memory allocation
  5444. * fails, the error code -ENOMEM will be set in @mas, and @new_mas points to the
  5445. * last node. mas_dup_free() will free the incomplete duplication of a tree.
  5446. *
  5447. * Note that the attributes of the two trees need to be exactly the same, and the
  5448. * new tree needs to be empty, otherwise -EINVAL will be set in @mas.
  5449. */
  5450. static inline void mas_dup_build(struct ma_state *mas, struct ma_state *new_mas,
  5451. gfp_t gfp)
  5452. {
  5453. struct maple_node *node;
  5454. struct maple_pnode *parent = NULL;
  5455. struct maple_enode *root;
  5456. enum maple_type type;
  5457. if (unlikely(mt_attr(mas->tree) != mt_attr(new_mas->tree)) ||
  5458. unlikely(!mtree_empty(new_mas->tree))) {
  5459. mas_set_err(mas, -EINVAL);
  5460. return;
  5461. }
  5462. root = mas_start(mas);
  5463. if (mas_is_ptr(mas) || mas_is_none(mas))
  5464. goto set_new_tree;
  5465. node = mt_alloc_one(gfp);
  5466. if (!node) {
  5467. new_mas->status = ma_none;
  5468. mas_set_err(mas, -ENOMEM);
  5469. return;
  5470. }
  5471. type = mte_node_type(mas->node);
  5472. root = mt_mk_node(node, type);
  5473. new_mas->node = root;
  5474. new_mas->min = 0;
  5475. new_mas->max = ULONG_MAX;
  5476. root = mte_mk_root(root);
  5477. while (1) {
  5478. mas_copy_node(mas, new_mas, parent);
  5479. if (!mte_is_leaf(mas->node)) {
  5480. /* Only allocate child nodes for non-leaf nodes. */
  5481. mas_dup_alloc(mas, new_mas, gfp);
  5482. if (unlikely(mas_is_err(mas)))
  5483. goto empty_mas;
  5484. } else {
  5485. /*
  5486. * This is the last leaf node and duplication is
  5487. * completed.
  5488. */
  5489. if (mas->max == ULONG_MAX)
  5490. goto done;
  5491. /* This is not the last leaf node and needs to go up. */
  5492. do {
  5493. mas_ascend(mas);
  5494. mas_ascend(new_mas);
  5495. } while (mas->offset == mas_data_end(mas));
  5496. /* Move to the next subtree. */
  5497. mas->offset++;
  5498. new_mas->offset++;
  5499. }
  5500. mas_descend(mas);
  5501. parent = ma_parent_ptr(mte_to_node(new_mas->node));
  5502. mas_descend(new_mas);
  5503. mas->offset = 0;
  5504. new_mas->offset = 0;
  5505. }
  5506. done:
  5507. /* Specially handle the parent of the root node. */
  5508. mte_to_node(root)->parent = ma_parent_ptr(mas_tree_parent(new_mas));
  5509. set_new_tree:
  5510. /* Make them the same height */
  5511. new_mas->tree->ma_flags = mas->tree->ma_flags;
  5512. rcu_assign_pointer(new_mas->tree->ma_root, root);
  5513. empty_mas:
  5514. mas_empty_nodes(mas);
  5515. }
  5516. /**
  5517. * __mt_dup(): Duplicate an entire maple tree
  5518. * @mt: The source maple tree
  5519. * @new: The new maple tree
  5520. * @gfp: The GFP_FLAGS to use for allocations
  5521. *
  5522. * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
  5523. * traversal. It uses memcpy() to copy nodes in the source tree and allocate
  5524. * new child nodes in non-leaf nodes. The new node is exactly the same as the
  5525. * source node except for all the addresses stored in it. It will be faster than
  5526. * traversing all elements in the source tree and inserting them one by one into
  5527. * the new tree.
  5528. * The user needs to ensure that the attributes of the source tree and the new
  5529. * tree are the same, and the new tree needs to be an empty tree, otherwise
  5530. * -EINVAL will be returned.
  5531. * Note that the user needs to manually lock the source tree and the new tree.
  5532. *
  5533. * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
  5534. * the attributes of the two trees are different or the new tree is not an empty
  5535. * tree.
  5536. */
  5537. int __mt_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
  5538. {
  5539. int ret = 0;
  5540. MA_STATE(mas, mt, 0, 0);
  5541. MA_STATE(new_mas, new, 0, 0);
  5542. mas_dup_build(&mas, &new_mas, gfp);
  5543. if (unlikely(mas_is_err(&mas))) {
  5544. ret = xa_err(mas.node);
  5545. if (ret == -ENOMEM)
  5546. mas_dup_free(&new_mas);
  5547. }
  5548. return ret;
  5549. }
  5550. EXPORT_SYMBOL(__mt_dup);
  5551. /**
  5552. * mtree_dup(): Duplicate an entire maple tree
  5553. * @mt: The source maple tree
  5554. * @new: The new maple tree
  5555. * @gfp: The GFP_FLAGS to use for allocations
  5556. *
  5557. * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
  5558. * traversal. It uses memcpy() to copy nodes in the source tree and allocate
  5559. * new child nodes in non-leaf nodes. The new node is exactly the same as the
  5560. * source node except for all the addresses stored in it. It will be faster than
  5561. * traversing all elements in the source tree and inserting them one by one into
  5562. * the new tree.
  5563. * The user needs to ensure that the attributes of the source tree and the new
  5564. * tree are the same, and the new tree needs to be an empty tree, otherwise
  5565. * -EINVAL will be returned.
  5566. *
  5567. * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
  5568. * the attributes of the two trees are different or the new tree is not an empty
  5569. * tree.
  5570. */
  5571. int mtree_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
  5572. {
  5573. int ret = 0;
  5574. MA_STATE(mas, mt, 0, 0);
  5575. MA_STATE(new_mas, new, 0, 0);
  5576. mas_lock(&new_mas);
  5577. mas_lock_nested(&mas, SINGLE_DEPTH_NESTING);
  5578. mas_dup_build(&mas, &new_mas, gfp);
  5579. mas_unlock(&mas);
  5580. if (unlikely(mas_is_err(&mas))) {
  5581. ret = xa_err(mas.node);
  5582. if (ret == -ENOMEM)
  5583. mas_dup_free(&new_mas);
  5584. }
  5585. mas_unlock(&new_mas);
  5586. return ret;
  5587. }
  5588. EXPORT_SYMBOL(mtree_dup);
  5589. /**
  5590. * __mt_destroy() - Walk and free all nodes of a locked maple tree.
  5591. * @mt: The maple tree
  5592. *
  5593. * Note: Does not handle locking.
  5594. */
  5595. void __mt_destroy(struct maple_tree *mt)
  5596. {
  5597. void *root = mt_root_locked(mt);
  5598. rcu_assign_pointer(mt->ma_root, NULL);
  5599. if (xa_is_node(root))
  5600. mte_destroy_walk(root, mt);
  5601. mt->ma_flags = mt_attr(mt);
  5602. }
  5603. EXPORT_SYMBOL_GPL(__mt_destroy);
  5604. /**
  5605. * mtree_destroy() - Destroy a maple tree
  5606. * @mt: The maple tree
  5607. *
  5608. * Frees all resources used by the tree. Handles locking.
  5609. */
  5610. void mtree_destroy(struct maple_tree *mt)
  5611. {
  5612. mtree_lock(mt);
  5613. __mt_destroy(mt);
  5614. mtree_unlock(mt);
  5615. }
  5616. EXPORT_SYMBOL(mtree_destroy);
  5617. /**
  5618. * mt_find() - Search from the start up until an entry is found.
  5619. * @mt: The maple tree
  5620. * @index: Pointer which contains the start location of the search
  5621. * @max: The maximum value of the search range
  5622. *
  5623. * Takes RCU read lock internally to protect the search, which does not
  5624. * protect the returned pointer after dropping RCU read lock.
  5625. * See also: Documentation/core-api/maple_tree.rst
  5626. *
  5627. * In case that an entry is found @index is updated to point to the next
  5628. * possible entry independent whether the found entry is occupying a
  5629. * single index or a range if indices.
  5630. *
  5631. * Return: The entry at or after the @index or %NULL
  5632. */
  5633. void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
  5634. {
  5635. MA_STATE(mas, mt, *index, *index);
  5636. void *entry;
  5637. #ifdef CONFIG_DEBUG_MAPLE_TREE
  5638. unsigned long copy = *index;
  5639. #endif
  5640. trace_ma_read(TP_FCT, &mas);
  5641. if ((*index) > max)
  5642. return NULL;
  5643. rcu_read_lock();
  5644. retry:
  5645. entry = mas_state_walk(&mas);
  5646. if (mas_is_start(&mas))
  5647. goto retry;
  5648. if (unlikely(xa_is_zero(entry)))
  5649. entry = NULL;
  5650. if (entry)
  5651. goto unlock;
  5652. while (mas_is_active(&mas) && (mas.last < max)) {
  5653. entry = mas_next_slot(&mas, max, false);
  5654. if (likely(entry && !xa_is_zero(entry)))
  5655. break;
  5656. }
  5657. if (unlikely(xa_is_zero(entry)))
  5658. entry = NULL;
  5659. unlock:
  5660. rcu_read_unlock();
  5661. if (likely(entry)) {
  5662. *index = mas.last + 1;
  5663. #ifdef CONFIG_DEBUG_MAPLE_TREE
  5664. if (MT_WARN_ON(mt, (*index) && ((*index) <= copy)))
  5665. pr_err("index not increased! %lx <= %lx\n",
  5666. *index, copy);
  5667. #endif
  5668. }
  5669. return entry;
  5670. }
  5671. EXPORT_SYMBOL(mt_find);
  5672. /**
  5673. * mt_find_after() - Search from the start up until an entry is found.
  5674. * @mt: The maple tree
  5675. * @index: Pointer which contains the start location of the search
  5676. * @max: The maximum value to check
  5677. *
  5678. * Same as mt_find() except that it checks @index for 0 before
  5679. * searching. If @index == 0, the search is aborted. This covers a wrap
  5680. * around of @index to 0 in an iterator loop.
  5681. *
  5682. * Return: The entry at or after the @index or %NULL
  5683. */
  5684. void *mt_find_after(struct maple_tree *mt, unsigned long *index,
  5685. unsigned long max)
  5686. {
  5687. if (!(*index))
  5688. return NULL;
  5689. return mt_find(mt, index, max);
  5690. }
  5691. EXPORT_SYMBOL(mt_find_after);
  5692. #ifdef CONFIG_DEBUG_MAPLE_TREE
  5693. atomic_t maple_tree_tests_run;
  5694. EXPORT_SYMBOL_GPL(maple_tree_tests_run);
  5695. atomic_t maple_tree_tests_passed;
  5696. EXPORT_SYMBOL_GPL(maple_tree_tests_passed);
  5697. #ifndef __KERNEL__
  5698. extern void kmem_cache_set_non_kernel(struct kmem_cache *, unsigned int);
  5699. void mt_set_non_kernel(unsigned int val)
  5700. {
  5701. kmem_cache_set_non_kernel(maple_node_cache, val);
  5702. }
  5703. extern void kmem_cache_set_callback(struct kmem_cache *cachep,
  5704. void (*callback)(void *));
  5705. void mt_set_callback(void (*callback)(void *))
  5706. {
  5707. kmem_cache_set_callback(maple_node_cache, callback);
  5708. }
  5709. extern void kmem_cache_set_private(struct kmem_cache *cachep, void *private);
  5710. void mt_set_private(void *private)
  5711. {
  5712. kmem_cache_set_private(maple_node_cache, private);
  5713. }
  5714. extern unsigned long kmem_cache_get_alloc(struct kmem_cache *);
  5715. unsigned long mt_get_alloc_size(void)
  5716. {
  5717. return kmem_cache_get_alloc(maple_node_cache);
  5718. }
  5719. extern void kmem_cache_zero_nr_tallocated(struct kmem_cache *);
  5720. void mt_zero_nr_tallocated(void)
  5721. {
  5722. kmem_cache_zero_nr_tallocated(maple_node_cache);
  5723. }
  5724. extern unsigned int kmem_cache_nr_tallocated(struct kmem_cache *);
  5725. unsigned int mt_nr_tallocated(void)
  5726. {
  5727. return kmem_cache_nr_tallocated(maple_node_cache);
  5728. }
  5729. extern unsigned int kmem_cache_nr_allocated(struct kmem_cache *);
  5730. unsigned int mt_nr_allocated(void)
  5731. {
  5732. return kmem_cache_nr_allocated(maple_node_cache);
  5733. }
  5734. void mt_cache_shrink(void)
  5735. {
  5736. }
  5737. #else
  5738. /*
  5739. * mt_cache_shrink() - For testing, don't use this.
  5740. *
  5741. * Certain testcases can trigger an OOM when combined with other memory
  5742. * debugging configuration options. This function is used to reduce the
  5743. * possibility of an out of memory even due to kmem_cache objects remaining
  5744. * around for longer than usual.
  5745. */
  5746. void mt_cache_shrink(void)
  5747. {
  5748. kmem_cache_shrink(maple_node_cache);
  5749. }
  5750. EXPORT_SYMBOL_GPL(mt_cache_shrink);
  5751. #endif /* not defined __KERNEL__ */
  5752. /*
  5753. * mas_get_slot() - Get the entry in the maple state node stored at @offset.
  5754. * @mas: The maple state
  5755. * @offset: The offset into the slot array to fetch.
  5756. *
  5757. * Return: The entry stored at @offset.
  5758. */
  5759. static inline struct maple_enode *mas_get_slot(struct ma_state *mas,
  5760. unsigned char offset)
  5761. {
  5762. return mas_slot(mas, ma_slots(mas_mn(mas), mte_node_type(mas->node)),
  5763. offset);
  5764. }
  5765. /* Depth first search, post-order */
  5766. static void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
  5767. {
  5768. struct maple_enode *p, *mn = mas->node;
  5769. unsigned long p_min, p_max;
  5770. mas_next_node(mas, mas_mn(mas), max);
  5771. if (!mas_is_overflow(mas))
  5772. return;
  5773. if (mte_is_root(mn))
  5774. return;
  5775. mas->node = mn;
  5776. mas_ascend(mas);
  5777. do {
  5778. p = mas->node;
  5779. p_min = mas->min;
  5780. p_max = mas->max;
  5781. mas_prev_node(mas, 0);
  5782. } while (!mas_is_underflow(mas));
  5783. mas->node = p;
  5784. mas->max = p_max;
  5785. mas->min = p_min;
  5786. }
  5787. /* Tree validations */
  5788. static void mt_dump_node(const struct maple_tree *mt, void *entry,
  5789. unsigned long min, unsigned long max, unsigned int depth,
  5790. enum mt_dump_format format);
  5791. static void mt_dump_range(unsigned long min, unsigned long max,
  5792. unsigned int depth, enum mt_dump_format format)
  5793. {
  5794. static const char spaces[] = " ";
  5795. switch(format) {
  5796. case mt_dump_hex:
  5797. if (min == max)
  5798. pr_info("%.*s%lx: ", depth * 2, spaces, min);
  5799. else
  5800. pr_info("%.*s%lx-%lx: ", depth * 2, spaces, min, max);
  5801. break;
  5802. case mt_dump_dec:
  5803. if (min == max)
  5804. pr_info("%.*s%lu: ", depth * 2, spaces, min);
  5805. else
  5806. pr_info("%.*s%lu-%lu: ", depth * 2, spaces, min, max);
  5807. }
  5808. }
  5809. static void mt_dump_entry(void *entry, unsigned long min, unsigned long max,
  5810. unsigned int depth, enum mt_dump_format format)
  5811. {
  5812. mt_dump_range(min, max, depth, format);
  5813. if (xa_is_value(entry))
  5814. pr_cont("value %ld (0x%lx) [" PTR_FMT "]\n", xa_to_value(entry),
  5815. xa_to_value(entry), entry);
  5816. else if (xa_is_zero(entry))
  5817. pr_cont("zero (%ld)\n", xa_to_internal(entry));
  5818. else if (mt_is_reserved(entry))
  5819. pr_cont("UNKNOWN ENTRY (" PTR_FMT ")\n", entry);
  5820. else
  5821. pr_cont(PTR_FMT "\n", entry);
  5822. }
  5823. static void mt_dump_range64(const struct maple_tree *mt, void *entry,
  5824. unsigned long min, unsigned long max, unsigned int depth,
  5825. enum mt_dump_format format)
  5826. {
  5827. struct maple_range_64 *node = &mte_to_node(entry)->mr64;
  5828. bool leaf = mte_is_leaf(entry);
  5829. unsigned long first = min;
  5830. int i;
  5831. pr_cont(" contents: ");
  5832. for (i = 0; i < MAPLE_RANGE64_SLOTS - 1; i++) {
  5833. switch(format) {
  5834. case mt_dump_hex:
  5835. pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
  5836. break;
  5837. case mt_dump_dec:
  5838. pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
  5839. }
  5840. }
  5841. pr_cont(PTR_FMT "\n", node->slot[i]);
  5842. for (i = 0; i < MAPLE_RANGE64_SLOTS; i++) {
  5843. unsigned long last = max;
  5844. if (i < (MAPLE_RANGE64_SLOTS - 1))
  5845. last = node->pivot[i];
  5846. else if (!node->slot[i] && max != mt_node_max(entry))
  5847. break;
  5848. if (last == 0 && i > 0)
  5849. break;
  5850. if (leaf)
  5851. mt_dump_entry(mt_slot(mt, node->slot, i),
  5852. first, last, depth + 1, format);
  5853. else if (node->slot[i])
  5854. mt_dump_node(mt, mt_slot(mt, node->slot, i),
  5855. first, last, depth + 1, format);
  5856. if (last == max)
  5857. break;
  5858. if (last > max) {
  5859. switch(format) {
  5860. case mt_dump_hex:
  5861. pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
  5862. node, last, max, i);
  5863. break;
  5864. case mt_dump_dec:
  5865. pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
  5866. node, last, max, i);
  5867. }
  5868. }
  5869. first = last + 1;
  5870. }
  5871. }
  5872. static void mt_dump_arange64(const struct maple_tree *mt, void *entry,
  5873. unsigned long min, unsigned long max, unsigned int depth,
  5874. enum mt_dump_format format)
  5875. {
  5876. struct maple_arange_64 *node = &mte_to_node(entry)->ma64;
  5877. unsigned long first = min;
  5878. int i;
  5879. pr_cont(" contents: ");
  5880. for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
  5881. switch (format) {
  5882. case mt_dump_hex:
  5883. pr_cont("%lx ", node->gap[i]);
  5884. break;
  5885. case mt_dump_dec:
  5886. pr_cont("%lu ", node->gap[i]);
  5887. }
  5888. }
  5889. pr_cont("| %02X %02X| ", node->meta.end, node->meta.gap);
  5890. for (i = 0; i < MAPLE_ARANGE64_SLOTS - 1; i++) {
  5891. switch (format) {
  5892. case mt_dump_hex:
  5893. pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
  5894. break;
  5895. case mt_dump_dec:
  5896. pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
  5897. }
  5898. }
  5899. pr_cont(PTR_FMT "\n", node->slot[i]);
  5900. for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
  5901. unsigned long last = max;
  5902. if (i < (MAPLE_ARANGE64_SLOTS - 1))
  5903. last = node->pivot[i];
  5904. else if (!node->slot[i])
  5905. break;
  5906. if (last == 0 && i > 0)
  5907. break;
  5908. if (node->slot[i])
  5909. mt_dump_node(mt, mt_slot(mt, node->slot, i),
  5910. first, last, depth + 1, format);
  5911. if (last == max)
  5912. break;
  5913. if (last > max) {
  5914. switch(format) {
  5915. case mt_dump_hex:
  5916. pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
  5917. node, last, max, i);
  5918. break;
  5919. case mt_dump_dec:
  5920. pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
  5921. node, last, max, i);
  5922. }
  5923. }
  5924. first = last + 1;
  5925. }
  5926. }
  5927. static void mt_dump_node(const struct maple_tree *mt, void *entry,
  5928. unsigned long min, unsigned long max, unsigned int depth,
  5929. enum mt_dump_format format)
  5930. {
  5931. struct maple_node *node = mte_to_node(entry);
  5932. unsigned int type = mte_node_type(entry);
  5933. unsigned int i;
  5934. mt_dump_range(min, max, depth, format);
  5935. pr_cont("node " PTR_FMT " depth %d type %d parent " PTR_FMT, node,
  5936. depth, type, node ? node->parent : NULL);
  5937. switch (type) {
  5938. case maple_dense:
  5939. pr_cont("\n");
  5940. for (i = 0; i < MAPLE_NODE_SLOTS; i++) {
  5941. if (min + i > max)
  5942. pr_cont("OUT OF RANGE: ");
  5943. mt_dump_entry(mt_slot(mt, node->slot, i),
  5944. min + i, min + i, depth, format);
  5945. }
  5946. break;
  5947. case maple_leaf_64:
  5948. case maple_range_64:
  5949. mt_dump_range64(mt, entry, min, max, depth, format);
  5950. break;
  5951. case maple_arange_64:
  5952. mt_dump_arange64(mt, entry, min, max, depth, format);
  5953. break;
  5954. default:
  5955. pr_cont(" UNKNOWN TYPE\n");
  5956. }
  5957. }
  5958. void mt_dump(const struct maple_tree *mt, enum mt_dump_format format)
  5959. {
  5960. void *entry = rcu_dereference_check(mt->ma_root, mt_locked(mt));
  5961. pr_info("maple_tree(" PTR_FMT ") flags %X, height %u root " PTR_FMT "\n",
  5962. mt, mt->ma_flags, mt_height(mt), entry);
  5963. if (xa_is_node(entry))
  5964. mt_dump_node(mt, entry, 0, mt_node_max(entry), 0, format);
  5965. else if (entry)
  5966. mt_dump_entry(entry, 0, 0, 0, format);
  5967. else
  5968. pr_info("(empty)\n");
  5969. }
  5970. EXPORT_SYMBOL_GPL(mt_dump);
  5971. /*
  5972. * Calculate the maximum gap in a node and check if that's what is reported in
  5973. * the parent (unless root).
  5974. */
  5975. static void mas_validate_gaps(struct ma_state *mas)
  5976. {
  5977. struct maple_enode *mte = mas->node;
  5978. struct maple_node *p_mn, *node = mte_to_node(mte);
  5979. enum maple_type mt = mte_node_type(mas->node);
  5980. unsigned long gap = 0, max_gap = 0;
  5981. unsigned long p_end, p_start = mas->min;
  5982. unsigned char p_slot, offset;
  5983. unsigned long *gaps = NULL;
  5984. unsigned long *pivots = ma_pivots(node, mt);
  5985. unsigned int i;
  5986. if (ma_is_dense(mt)) {
  5987. for (i = 0; i < mt_slot_count(mte); i++) {
  5988. if (mas_get_slot(mas, i)) {
  5989. if (gap > max_gap)
  5990. max_gap = gap;
  5991. gap = 0;
  5992. continue;
  5993. }
  5994. gap++;
  5995. }
  5996. goto counted;
  5997. }
  5998. gaps = ma_gaps(node, mt);
  5999. for (i = 0; i < mt_slot_count(mte); i++) {
  6000. p_end = mas_safe_pivot(mas, pivots, i, mt);
  6001. if (!gaps) {
  6002. if (!mas_get_slot(mas, i))
  6003. gap = p_end - p_start + 1;
  6004. } else {
  6005. void *entry = mas_get_slot(mas, i);
  6006. gap = gaps[i];
  6007. MT_BUG_ON(mas->tree, !entry);
  6008. if (gap > p_end - p_start + 1) {
  6009. pr_err(PTR_FMT "[%u] %lu >= %lu - %lu + 1 (%lu)\n",
  6010. mas_mn(mas), i, gap, p_end, p_start,
  6011. p_end - p_start + 1);
  6012. MT_BUG_ON(mas->tree, gap > p_end - p_start + 1);
  6013. }
  6014. }
  6015. if (gap > max_gap)
  6016. max_gap = gap;
  6017. p_start = p_end + 1;
  6018. if (p_end >= mas->max)
  6019. break;
  6020. }
  6021. counted:
  6022. if (mt == maple_arange_64) {
  6023. MT_BUG_ON(mas->tree, !gaps);
  6024. offset = ma_meta_gap(node);
  6025. if (offset > i) {
  6026. pr_err("gap offset " PTR_FMT "[%u] is invalid\n", node, offset);
  6027. MT_BUG_ON(mas->tree, 1);
  6028. }
  6029. if (gaps[offset] != max_gap) {
  6030. pr_err("gap " PTR_FMT "[%u] is not the largest gap %lu\n",
  6031. node, offset, max_gap);
  6032. MT_BUG_ON(mas->tree, 1);
  6033. }
  6034. for (i++ ; i < mt_slot_count(mte); i++) {
  6035. if (gaps[i] != 0) {
  6036. pr_err("gap " PTR_FMT "[%u] beyond node limit != 0\n",
  6037. node, i);
  6038. MT_BUG_ON(mas->tree, 1);
  6039. }
  6040. }
  6041. }
  6042. if (mte_is_root(mte))
  6043. return;
  6044. p_slot = mte_parent_slot(mas->node);
  6045. p_mn = mte_parent(mte);
  6046. MT_BUG_ON(mas->tree, max_gap > mas->max);
  6047. if (ma_gaps(p_mn, mas_parent_type(mas, mte))[p_slot] != max_gap) {
  6048. pr_err("gap " PTR_FMT "[%u] != %lu\n", p_mn, p_slot, max_gap);
  6049. mt_dump(mas->tree, mt_dump_hex);
  6050. MT_BUG_ON(mas->tree, 1);
  6051. }
  6052. }
  6053. static void mas_validate_parent_slot(struct ma_state *mas)
  6054. {
  6055. struct maple_node *parent;
  6056. struct maple_enode *node;
  6057. enum maple_type p_type;
  6058. unsigned char p_slot;
  6059. void __rcu **slots;
  6060. int i;
  6061. if (mte_is_root(mas->node))
  6062. return;
  6063. p_slot = mte_parent_slot(mas->node);
  6064. p_type = mas_parent_type(mas, mas->node);
  6065. parent = mte_parent(mas->node);
  6066. slots = ma_slots(parent, p_type);
  6067. MT_BUG_ON(mas->tree, mas_mn(mas) == parent);
  6068. /* Check prev/next parent slot for duplicate node entry */
  6069. for (i = 0; i < mt_slots[p_type]; i++) {
  6070. node = mas_slot(mas, slots, i);
  6071. if (i == p_slot) {
  6072. if (node != mas->node)
  6073. pr_err("parent " PTR_FMT "[%u] does not have " PTR_FMT "\n",
  6074. parent, i, mas_mn(mas));
  6075. MT_BUG_ON(mas->tree, node != mas->node);
  6076. } else if (node == mas->node) {
  6077. pr_err("Invalid child " PTR_FMT " at parent " PTR_FMT "[%u] p_slot %u\n",
  6078. mas_mn(mas), parent, i, p_slot);
  6079. MT_BUG_ON(mas->tree, node == mas->node);
  6080. }
  6081. }
  6082. }
  6083. static void mas_validate_child_slot(struct ma_state *mas)
  6084. {
  6085. enum maple_type type = mte_node_type(mas->node);
  6086. void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
  6087. unsigned long *pivots = ma_pivots(mte_to_node(mas->node), type);
  6088. struct maple_enode *child;
  6089. unsigned char i;
  6090. if (mte_is_leaf(mas->node))
  6091. return;
  6092. for (i = 0; i < mt_slots[type]; i++) {
  6093. child = mas_slot(mas, slots, i);
  6094. if (!child) {
  6095. pr_err("Non-leaf node lacks child at " PTR_FMT "[%u]\n",
  6096. mas_mn(mas), i);
  6097. MT_BUG_ON(mas->tree, 1);
  6098. }
  6099. if (mte_parent_slot(child) != i) {
  6100. pr_err("Slot error at " PTR_FMT "[%u]: child " PTR_FMT " has pslot %u\n",
  6101. mas_mn(mas), i, mte_to_node(child),
  6102. mte_parent_slot(child));
  6103. MT_BUG_ON(mas->tree, 1);
  6104. }
  6105. if (mte_parent(child) != mte_to_node(mas->node)) {
  6106. pr_err("child " PTR_FMT " has parent " PTR_FMT " not " PTR_FMT "\n",
  6107. mte_to_node(child), mte_parent(child),
  6108. mte_to_node(mas->node));
  6109. MT_BUG_ON(mas->tree, 1);
  6110. }
  6111. if (i < mt_pivots[type] && pivots[i] == mas->max)
  6112. break;
  6113. }
  6114. }
  6115. /*
  6116. * Validate all pivots are within mas->min and mas->max, check metadata ends
  6117. * where the maximum ends and ensure there is no slots or pivots set outside of
  6118. * the end of the data.
  6119. */
  6120. static void mas_validate_limits(struct ma_state *mas)
  6121. {
  6122. int i;
  6123. unsigned long prev_piv = 0;
  6124. enum maple_type type = mte_node_type(mas->node);
  6125. void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
  6126. unsigned long *pivots = ma_pivots(mas_mn(mas), type);
  6127. for (i = 0; i < mt_slots[type]; i++) {
  6128. unsigned long piv;
  6129. piv = mas_safe_pivot(mas, pivots, i, type);
  6130. if (!piv && (i != 0)) {
  6131. pr_err("Missing node limit pivot at " PTR_FMT "[%u]",
  6132. mas_mn(mas), i);
  6133. MAS_WARN_ON(mas, 1);
  6134. }
  6135. if (prev_piv > piv) {
  6136. pr_err(PTR_FMT "[%u] piv %lu < prev_piv %lu\n",
  6137. mas_mn(mas), i, piv, prev_piv);
  6138. MAS_WARN_ON(mas, piv < prev_piv);
  6139. }
  6140. if (piv < mas->min) {
  6141. pr_err(PTR_FMT "[%u] %lu < %lu\n", mas_mn(mas), i,
  6142. piv, mas->min);
  6143. MAS_WARN_ON(mas, piv < mas->min);
  6144. }
  6145. if (piv > mas->max) {
  6146. pr_err(PTR_FMT "[%u] %lu > %lu\n", mas_mn(mas), i,
  6147. piv, mas->max);
  6148. MAS_WARN_ON(mas, piv > mas->max);
  6149. }
  6150. prev_piv = piv;
  6151. if (piv == mas->max)
  6152. break;
  6153. }
  6154. if (mas_data_end(mas) != i) {
  6155. pr_err("node" PTR_FMT ": data_end %u != the last slot offset %u\n",
  6156. mas_mn(mas), mas_data_end(mas), i);
  6157. MT_BUG_ON(mas->tree, 1);
  6158. }
  6159. for (i += 1; i < mt_slots[type]; i++) {
  6160. void *entry = mas_slot(mas, slots, i);
  6161. if (entry && (i != mt_slots[type] - 1)) {
  6162. pr_err(PTR_FMT "[%u] should not have entry " PTR_FMT "\n",
  6163. mas_mn(mas), i, entry);
  6164. MT_BUG_ON(mas->tree, entry != NULL);
  6165. }
  6166. if (i < mt_pivots[type]) {
  6167. unsigned long piv = pivots[i];
  6168. if (!piv)
  6169. continue;
  6170. pr_err(PTR_FMT "[%u] should not have piv %lu\n",
  6171. mas_mn(mas), i, piv);
  6172. MAS_WARN_ON(mas, i < mt_pivots[type] - 1);
  6173. }
  6174. }
  6175. }
  6176. static void mt_validate_nulls(struct maple_tree *mt)
  6177. {
  6178. void *entry, *last = (void *)1;
  6179. unsigned char offset = 0;
  6180. void __rcu **slots;
  6181. MA_STATE(mas, mt, 0, 0);
  6182. mas_start(&mas);
  6183. if (mas_is_none(&mas) || (mas_is_ptr(&mas)))
  6184. return;
  6185. while (!mte_is_leaf(mas.node))
  6186. mas_descend(&mas);
  6187. slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
  6188. do {
  6189. entry = mas_slot(&mas, slots, offset);
  6190. if (!last && !entry) {
  6191. pr_err("Sequential nulls end at " PTR_FMT "[%u]\n",
  6192. mas_mn(&mas), offset);
  6193. }
  6194. MT_BUG_ON(mt, !last && !entry);
  6195. last = entry;
  6196. if (offset == mas_data_end(&mas)) {
  6197. mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
  6198. if (mas_is_overflow(&mas))
  6199. return;
  6200. offset = 0;
  6201. slots = ma_slots(mte_to_node(mas.node),
  6202. mte_node_type(mas.node));
  6203. } else {
  6204. offset++;
  6205. }
  6206. } while (!mas_is_overflow(&mas));
  6207. }
  6208. /*
  6209. * validate a maple tree by checking:
  6210. * 1. The limits (pivots are within mas->min to mas->max)
  6211. * 2. The gap is correctly set in the parents
  6212. */
  6213. void mt_validate(struct maple_tree *mt)
  6214. __must_hold(mas->tree->ma_lock)
  6215. {
  6216. unsigned char end;
  6217. MA_STATE(mas, mt, 0, 0);
  6218. mas_start(&mas);
  6219. if (!mas_is_active(&mas))
  6220. return;
  6221. while (!mte_is_leaf(mas.node))
  6222. mas_descend(&mas);
  6223. while (!mas_is_overflow(&mas)) {
  6224. MAS_WARN_ON(&mas, mte_dead_node(mas.node));
  6225. end = mas_data_end(&mas);
  6226. if (MAS_WARN_ON(&mas, (end < mt_min_slot_count(mas.node)) &&
  6227. (!mte_is_root(mas.node)))) {
  6228. pr_err("Invalid size %u of " PTR_FMT "\n",
  6229. end, mas_mn(&mas));
  6230. }
  6231. mas_validate_parent_slot(&mas);
  6232. mas_validate_limits(&mas);
  6233. mas_validate_child_slot(&mas);
  6234. if (mt_is_alloc(mt))
  6235. mas_validate_gaps(&mas);
  6236. mas_dfs_postorder(&mas, ULONG_MAX);
  6237. }
  6238. mt_validate_nulls(mt);
  6239. }
  6240. EXPORT_SYMBOL_GPL(mt_validate);
  6241. void mas_dump(const struct ma_state *mas)
  6242. {
  6243. pr_err("MAS: tree=" PTR_FMT " enode=" PTR_FMT " ",
  6244. mas->tree, mas->node);
  6245. switch (mas->status) {
  6246. case ma_active:
  6247. pr_err("(ma_active)");
  6248. break;
  6249. case ma_none:
  6250. pr_err("(ma_none)");
  6251. break;
  6252. case ma_root:
  6253. pr_err("(ma_root)");
  6254. break;
  6255. case ma_start:
  6256. pr_err("(ma_start) ");
  6257. break;
  6258. case ma_pause:
  6259. pr_err("(ma_pause) ");
  6260. break;
  6261. case ma_overflow:
  6262. pr_err("(ma_overflow) ");
  6263. break;
  6264. case ma_underflow:
  6265. pr_err("(ma_underflow) ");
  6266. break;
  6267. case ma_error:
  6268. pr_err("(ma_error) ");
  6269. break;
  6270. }
  6271. pr_err("Store Type: ");
  6272. switch (mas->store_type) {
  6273. case wr_invalid:
  6274. pr_err("invalid store type\n");
  6275. break;
  6276. case wr_new_root:
  6277. pr_err("new_root\n");
  6278. break;
  6279. case wr_store_root:
  6280. pr_err("store_root\n");
  6281. break;
  6282. case wr_exact_fit:
  6283. pr_err("exact_fit\n");
  6284. break;
  6285. case wr_split_store:
  6286. pr_err("split_store\n");
  6287. break;
  6288. case wr_slot_store:
  6289. pr_err("slot_store\n");
  6290. break;
  6291. case wr_append:
  6292. pr_err("append\n");
  6293. break;
  6294. case wr_node_store:
  6295. pr_err("node_store\n");
  6296. break;
  6297. case wr_spanning_store:
  6298. pr_err("spanning_store\n");
  6299. break;
  6300. case wr_rebalance:
  6301. pr_err("rebalance\n");
  6302. break;
  6303. }
  6304. pr_err("[%u/%u] index=%lx last=%lx\n", mas->offset, mas->end,
  6305. mas->index, mas->last);
  6306. pr_err(" min=%lx max=%lx sheaf=" PTR_FMT ", request %lu depth=%u, flags=%x\n",
  6307. mas->min, mas->max, mas->sheaf, mas->node_request, mas->depth,
  6308. mas->mas_flags);
  6309. if (mas->index > mas->last)
  6310. pr_err("Check index & last\n");
  6311. }
  6312. EXPORT_SYMBOL_GPL(mas_dump);
  6313. void mas_wr_dump(const struct ma_wr_state *wr_mas)
  6314. {
  6315. pr_err("WR_MAS: node=" PTR_FMT " r_min=%lx r_max=%lx\n",
  6316. wr_mas->node, wr_mas->r_min, wr_mas->r_max);
  6317. pr_err(" type=%u off_end=%u, node_end=%u, end_piv=%lx\n",
  6318. wr_mas->type, wr_mas->offset_end, wr_mas->mas->end,
  6319. wr_mas->end_piv);
  6320. }
  6321. EXPORT_SYMBOL_GPL(mas_wr_dump);
  6322. #endif /* CONFIG_DEBUG_MAPLE_TREE */