maple_tree.c 189 KB

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