slub.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SLUB: A slab allocator that limits cache line use instead of queuing
  4. * objects in per cpu and per node lists.
  5. *
  6. * The allocator synchronizes using per slab locks or atomic operatios
  7. * and only uses a centralized lock to manage a pool of partial slabs.
  8. *
  9. * (C) 2007 SGI, Christoph Lameter
  10. * (C) 2011 Linux Foundation, Christoph Lameter
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/swap.h> /* struct reclaim_state */
  14. #include <linux/module.h>
  15. #include <linux/bit_spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/bitops.h>
  18. #include <linux/slab.h>
  19. #include "slab.h"
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/kasan.h>
  23. #include <linux/cpu.h>
  24. #include <linux/cpuset.h>
  25. #include <linux/mempolicy.h>
  26. #include <linux/ctype.h>
  27. #include <linux/debugobjects.h>
  28. #include <linux/kallsyms.h>
  29. #include <linux/memory.h>
  30. #include <linux/math64.h>
  31. #include <linux/fault-inject.h>
  32. #include <linux/stacktrace.h>
  33. #include <linux/prefetch.h>
  34. #include <linux/memcontrol.h>
  35. #include <linux/random.h>
  36. #include <trace/events/kmem.h>
  37. #include "internal.h"
  38. /*
  39. * Lock order:
  40. * 1. slab_mutex (Global Mutex)
  41. * 2. node->list_lock
  42. * 3. slab_lock(page) (Only on some arches and for debugging)
  43. *
  44. * slab_mutex
  45. *
  46. * The role of the slab_mutex is to protect the list of all the slabs
  47. * and to synchronize major metadata changes to slab cache structures.
  48. *
  49. * The slab_lock is only used for debugging and on arches that do not
  50. * have the ability to do a cmpxchg_double. It only protects:
  51. * A. page->freelist -> List of object free in a page
  52. * B. page->inuse -> Number of objects in use
  53. * C. page->objects -> Number of objects in page
  54. * D. page->frozen -> frozen state
  55. *
  56. * If a slab is frozen then it is exempt from list management. It is not
  57. * on any list. The processor that froze the slab is the one who can
  58. * perform list operations on the page. Other processors may put objects
  59. * onto the freelist but the processor that froze the slab is the only
  60. * one that can retrieve the objects from the page's freelist.
  61. *
  62. * The list_lock protects the partial and full list on each node and
  63. * the partial slab counter. If taken then no new slabs may be added or
  64. * removed from the lists nor make the number of partial slabs be modified.
  65. * (Note that the total number of slabs is an atomic value that may be
  66. * modified without taking the list lock).
  67. *
  68. * The list_lock is a centralized lock and thus we avoid taking it as
  69. * much as possible. As long as SLUB does not have to handle partial
  70. * slabs, operations can continue without any centralized lock. F.e.
  71. * allocating a long series of objects that fill up slabs does not require
  72. * the list lock.
  73. * Interrupts are disabled during allocation and deallocation in order to
  74. * make the slab allocator safe to use in the context of an irq. In addition
  75. * interrupts are disabled to ensure that the processor does not change
  76. * while handling per_cpu slabs, due to kernel preemption.
  77. *
  78. * SLUB assigns one slab for allocation to each processor.
  79. * Allocations only occur from these slabs called cpu slabs.
  80. *
  81. * Slabs with free elements are kept on a partial list and during regular
  82. * operations no list for full slabs is used. If an object in a full slab is
  83. * freed then the slab will show up again on the partial lists.
  84. * We track full slabs for debugging purposes though because otherwise we
  85. * cannot scan all objects.
  86. *
  87. * Slabs are freed when they become empty. Teardown and setup is
  88. * minimal so we rely on the page allocators per cpu caches for
  89. * fast frees and allocs.
  90. *
  91. * Overloading of page flags that are otherwise used for LRU management.
  92. *
  93. * PageActive The slab is frozen and exempt from list processing.
  94. * This means that the slab is dedicated to a purpose
  95. * such as satisfying allocations for a specific
  96. * processor. Objects may be freed in the slab while
  97. * it is frozen but slab_free will then skip the usual
  98. * list operations. It is up to the processor holding
  99. * the slab to integrate the slab into the slab lists
  100. * when the slab is no longer needed.
  101. *
  102. * One use of this flag is to mark slabs that are
  103. * used for allocations. Then such a slab becomes a cpu
  104. * slab. The cpu slab may be equipped with an additional
  105. * freelist that allows lockless access to
  106. * free objects in addition to the regular freelist
  107. * that requires the slab lock.
  108. *
  109. * PageError Slab requires special handling due to debug
  110. * options set. This moves slab handling out of
  111. * the fast path and disables lockless freelists.
  112. */
  113. static inline int kmem_cache_debug(struct kmem_cache *s)
  114. {
  115. #ifdef CONFIG_SLUB_DEBUG
  116. return unlikely(s->flags & SLAB_DEBUG_FLAGS);
  117. #else
  118. return 0;
  119. #endif
  120. }
  121. void *fixup_red_left(struct kmem_cache *s, void *p)
  122. {
  123. if (kmem_cache_debug(s) && s->flags & SLAB_RED_ZONE)
  124. p += s->red_left_pad;
  125. return p;
  126. }
  127. static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)
  128. {
  129. #ifdef CONFIG_SLUB_CPU_PARTIAL
  130. return !kmem_cache_debug(s);
  131. #else
  132. return false;
  133. #endif
  134. }
  135. /*
  136. * Issues still to be resolved:
  137. *
  138. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  139. *
  140. * - Variable sizing of the per node arrays
  141. */
  142. /* Enable to test recovery from slab corruption on boot */
  143. #undef SLUB_RESILIENCY_TEST
  144. /* Enable to log cmpxchg failures */
  145. #undef SLUB_DEBUG_CMPXCHG
  146. /*
  147. * Mininum number of partial slabs. These will be left on the partial
  148. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  149. */
  150. #define MIN_PARTIAL 5
  151. /*
  152. * Maximum number of desirable partial slabs.
  153. * The existence of more partial slabs makes kmem_cache_shrink
  154. * sort the partial list by the number of objects in use.
  155. */
  156. #define MAX_PARTIAL 10
  157. #define DEBUG_DEFAULT_FLAGS (SLAB_CONSISTENCY_CHECKS | SLAB_RED_ZONE | \
  158. SLAB_POISON | SLAB_STORE_USER)
  159. /*
  160. * These debug flags cannot use CMPXCHG because there might be consistency
  161. * issues when checking or reading debug information
  162. */
  163. #define SLAB_NO_CMPXCHG (SLAB_CONSISTENCY_CHECKS | SLAB_STORE_USER | \
  164. SLAB_TRACE)
  165. /*
  166. * Debugging flags that require metadata to be stored in the slab. These get
  167. * disabled when slub_debug=O is used and a cache's min order increases with
  168. * metadata.
  169. */
  170. #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  171. #define OO_SHIFT 16
  172. #define OO_MASK ((1 << OO_SHIFT) - 1)
  173. #define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
  174. /* Internal SLUB flags */
  175. /* Poison object */
  176. #define __OBJECT_POISON ((slab_flags_t __force)0x80000000U)
  177. /* Use cmpxchg_double */
  178. #define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U)
  179. /*
  180. * Tracking user of a slab.
  181. */
  182. #define TRACK_ADDRS_COUNT 16
  183. struct track {
  184. unsigned long addr; /* Called from address */
  185. #ifdef CONFIG_STACKTRACE
  186. unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
  187. #endif
  188. int cpu; /* Was running on cpu */
  189. int pid; /* Pid context */
  190. unsigned long when; /* When did the operation occur */
  191. };
  192. enum track_item { TRACK_ALLOC, TRACK_FREE };
  193. #ifdef CONFIG_SYSFS
  194. static int sysfs_slab_add(struct kmem_cache *);
  195. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  196. static void memcg_propagate_slab_attrs(struct kmem_cache *s);
  197. static void sysfs_slab_remove(struct kmem_cache *s);
  198. #else
  199. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  200. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  201. { return 0; }
  202. static inline void memcg_propagate_slab_attrs(struct kmem_cache *s) { }
  203. static inline void sysfs_slab_remove(struct kmem_cache *s) { }
  204. #endif
  205. static inline void stat(const struct kmem_cache *s, enum stat_item si)
  206. {
  207. #ifdef CONFIG_SLUB_STATS
  208. /*
  209. * The rmw is racy on a preemptible kernel but this is acceptable, so
  210. * avoid this_cpu_add()'s irq-disable overhead.
  211. */
  212. raw_cpu_inc(s->cpu_slab->stat[si]);
  213. #endif
  214. }
  215. /********************************************************************
  216. * Core slab cache functions
  217. *******************************************************************/
  218. /*
  219. * Returns freelist pointer (ptr). With hardening, this is obfuscated
  220. * with an XOR of the address where the pointer is held and a per-cache
  221. * random number.
  222. */
  223. static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr,
  224. unsigned long ptr_addr)
  225. {
  226. #ifdef CONFIG_SLAB_FREELIST_HARDENED
  227. return (void *)((unsigned long)ptr ^ s->random ^ ptr_addr);
  228. #else
  229. return ptr;
  230. #endif
  231. }
  232. /* Returns the freelist pointer recorded at location ptr_addr. */
  233. static inline void *freelist_dereference(const struct kmem_cache *s,
  234. void *ptr_addr)
  235. {
  236. return freelist_ptr(s, (void *)*(unsigned long *)(ptr_addr),
  237. (unsigned long)ptr_addr);
  238. }
  239. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  240. {
  241. return freelist_dereference(s, object + s->offset);
  242. }
  243. static void prefetch_freepointer(const struct kmem_cache *s, void *object)
  244. {
  245. prefetch(object + s->offset);
  246. }
  247. static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
  248. {
  249. unsigned long freepointer_addr;
  250. void *p;
  251. if (!debug_pagealloc_enabled())
  252. return get_freepointer(s, object);
  253. freepointer_addr = (unsigned long)object + s->offset;
  254. probe_kernel_read(&p, (void **)freepointer_addr, sizeof(p));
  255. return freelist_ptr(s, p, freepointer_addr);
  256. }
  257. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  258. {
  259. unsigned long freeptr_addr = (unsigned long)object + s->offset;
  260. #ifdef CONFIG_SLAB_FREELIST_HARDENED
  261. BUG_ON(object == fp); /* naive detection of double free or corruption */
  262. #endif
  263. *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr);
  264. }
  265. /* Loop over all objects in a slab */
  266. #define for_each_object(__p, __s, __addr, __objects) \
  267. for (__p = fixup_red_left(__s, __addr); \
  268. __p < (__addr) + (__objects) * (__s)->size; \
  269. __p += (__s)->size)
  270. #define for_each_object_idx(__p, __idx, __s, __addr, __objects) \
  271. for (__p = fixup_red_left(__s, __addr), __idx = 1; \
  272. __idx <= __objects; \
  273. __p += (__s)->size, __idx++)
  274. /* Determine object index from a given position */
  275. static inline unsigned int slab_index(void *p, struct kmem_cache *s, void *addr)
  276. {
  277. return (p - addr) / s->size;
  278. }
  279. static inline unsigned int order_objects(unsigned int order, unsigned int size)
  280. {
  281. return ((unsigned int)PAGE_SIZE << order) / size;
  282. }
  283. static inline struct kmem_cache_order_objects oo_make(unsigned int order,
  284. unsigned int size)
  285. {
  286. struct kmem_cache_order_objects x = {
  287. (order << OO_SHIFT) + order_objects(order, size)
  288. };
  289. return x;
  290. }
  291. static inline unsigned int oo_order(struct kmem_cache_order_objects x)
  292. {
  293. return x.x >> OO_SHIFT;
  294. }
  295. static inline unsigned int oo_objects(struct kmem_cache_order_objects x)
  296. {
  297. return x.x & OO_MASK;
  298. }
  299. /*
  300. * Per slab locking using the pagelock
  301. */
  302. static __always_inline void slab_lock(struct page *page)
  303. {
  304. VM_BUG_ON_PAGE(PageTail(page), page);
  305. bit_spin_lock(PG_locked, &page->flags);
  306. }
  307. static __always_inline void slab_unlock(struct page *page)
  308. {
  309. VM_BUG_ON_PAGE(PageTail(page), page);
  310. __bit_spin_unlock(PG_locked, &page->flags);
  311. }
  312. /* Interrupts must be disabled (for the fallback code to work right) */
  313. static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
  314. void *freelist_old, unsigned long counters_old,
  315. void *freelist_new, unsigned long counters_new,
  316. const char *n)
  317. {
  318. VM_BUG_ON(!irqs_disabled());
  319. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
  320. defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  321. if (s->flags & __CMPXCHG_DOUBLE) {
  322. if (cmpxchg_double(&page->freelist, &page->counters,
  323. freelist_old, counters_old,
  324. freelist_new, counters_new))
  325. return true;
  326. } else
  327. #endif
  328. {
  329. slab_lock(page);
  330. if (page->freelist == freelist_old &&
  331. page->counters == counters_old) {
  332. page->freelist = freelist_new;
  333. page->counters = counters_new;
  334. slab_unlock(page);
  335. return true;
  336. }
  337. slab_unlock(page);
  338. }
  339. cpu_relax();
  340. stat(s, CMPXCHG_DOUBLE_FAIL);
  341. #ifdef SLUB_DEBUG_CMPXCHG
  342. pr_info("%s %s: cmpxchg double redo ", n, s->name);
  343. #endif
  344. return false;
  345. }
  346. static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
  347. void *freelist_old, unsigned long counters_old,
  348. void *freelist_new, unsigned long counters_new,
  349. const char *n)
  350. {
  351. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
  352. defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  353. if (s->flags & __CMPXCHG_DOUBLE) {
  354. if (cmpxchg_double(&page->freelist, &page->counters,
  355. freelist_old, counters_old,
  356. freelist_new, counters_new))
  357. return true;
  358. } else
  359. #endif
  360. {
  361. unsigned long flags;
  362. local_irq_save(flags);
  363. slab_lock(page);
  364. if (page->freelist == freelist_old &&
  365. page->counters == counters_old) {
  366. page->freelist = freelist_new;
  367. page->counters = counters_new;
  368. slab_unlock(page);
  369. local_irq_restore(flags);
  370. return true;
  371. }
  372. slab_unlock(page);
  373. local_irq_restore(flags);
  374. }
  375. cpu_relax();
  376. stat(s, CMPXCHG_DOUBLE_FAIL);
  377. #ifdef SLUB_DEBUG_CMPXCHG
  378. pr_info("%s %s: cmpxchg double redo ", n, s->name);
  379. #endif
  380. return false;
  381. }
  382. #ifdef CONFIG_SLUB_DEBUG
  383. /*
  384. * Determine a map of object in use on a page.
  385. *
  386. * Node listlock must be held to guarantee that the page does
  387. * not vanish from under us.
  388. */
  389. static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
  390. {
  391. void *p;
  392. void *addr = page_address(page);
  393. for (p = page->freelist; p; p = get_freepointer(s, p))
  394. set_bit(slab_index(p, s, addr), map);
  395. }
  396. static inline unsigned int size_from_object(struct kmem_cache *s)
  397. {
  398. if (s->flags & SLAB_RED_ZONE)
  399. return s->size - s->red_left_pad;
  400. return s->size;
  401. }
  402. static inline void *restore_red_left(struct kmem_cache *s, void *p)
  403. {
  404. if (s->flags & SLAB_RED_ZONE)
  405. p -= s->red_left_pad;
  406. return p;
  407. }
  408. /*
  409. * Debug settings:
  410. */
  411. #if defined(CONFIG_SLUB_DEBUG_ON)
  412. static slab_flags_t slub_debug = DEBUG_DEFAULT_FLAGS;
  413. #else
  414. static slab_flags_t slub_debug;
  415. #endif
  416. static char *slub_debug_slabs;
  417. static int disable_higher_order_debug;
  418. /*
  419. * slub is about to manipulate internal object metadata. This memory lies
  420. * outside the range of the allocated object, so accessing it would normally
  421. * be reported by kasan as a bounds error. metadata_access_enable() is used
  422. * to tell kasan that these accesses are OK.
  423. */
  424. static inline void metadata_access_enable(void)
  425. {
  426. kasan_disable_current();
  427. }
  428. static inline void metadata_access_disable(void)
  429. {
  430. kasan_enable_current();
  431. }
  432. /*
  433. * Object debugging
  434. */
  435. /* Verify that a pointer has an address that is valid within a slab page */
  436. static inline int check_valid_pointer(struct kmem_cache *s,
  437. struct page *page, void *object)
  438. {
  439. void *base;
  440. if (!object)
  441. return 1;
  442. base = page_address(page);
  443. object = restore_red_left(s, object);
  444. if (object < base || object >= base + page->objects * s->size ||
  445. (object - base) % s->size) {
  446. return 0;
  447. }
  448. return 1;
  449. }
  450. static void print_section(char *level, char *text, u8 *addr,
  451. unsigned int length)
  452. {
  453. metadata_access_enable();
  454. print_hex_dump(level, text, DUMP_PREFIX_ADDRESS, 16, 1, addr,
  455. length, 1);
  456. metadata_access_disable();
  457. }
  458. static struct track *get_track(struct kmem_cache *s, void *object,
  459. enum track_item alloc)
  460. {
  461. struct track *p;
  462. if (s->offset)
  463. p = object + s->offset + sizeof(void *);
  464. else
  465. p = object + s->inuse;
  466. return p + alloc;
  467. }
  468. static void set_track(struct kmem_cache *s, void *object,
  469. enum track_item alloc, unsigned long addr)
  470. {
  471. struct track *p = get_track(s, object, alloc);
  472. if (addr) {
  473. #ifdef CONFIG_STACKTRACE
  474. struct stack_trace trace;
  475. int i;
  476. trace.nr_entries = 0;
  477. trace.max_entries = TRACK_ADDRS_COUNT;
  478. trace.entries = p->addrs;
  479. trace.skip = 3;
  480. metadata_access_enable();
  481. save_stack_trace(&trace);
  482. metadata_access_disable();
  483. /* See rant in lockdep.c */
  484. if (trace.nr_entries != 0 &&
  485. trace.entries[trace.nr_entries - 1] == ULONG_MAX)
  486. trace.nr_entries--;
  487. for (i = trace.nr_entries; i < TRACK_ADDRS_COUNT; i++)
  488. p->addrs[i] = 0;
  489. #endif
  490. p->addr = addr;
  491. p->cpu = smp_processor_id();
  492. p->pid = current->pid;
  493. p->when = jiffies;
  494. } else
  495. memset(p, 0, sizeof(struct track));
  496. }
  497. static void init_tracking(struct kmem_cache *s, void *object)
  498. {
  499. if (!(s->flags & SLAB_STORE_USER))
  500. return;
  501. set_track(s, object, TRACK_FREE, 0UL);
  502. set_track(s, object, TRACK_ALLOC, 0UL);
  503. }
  504. static void print_track(const char *s, struct track *t, unsigned long pr_time)
  505. {
  506. if (!t->addr)
  507. return;
  508. pr_err("INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
  509. s, (void *)t->addr, pr_time - t->when, t->cpu, t->pid);
  510. #ifdef CONFIG_STACKTRACE
  511. {
  512. int i;
  513. for (i = 0; i < TRACK_ADDRS_COUNT; i++)
  514. if (t->addrs[i])
  515. pr_err("\t%pS\n", (void *)t->addrs[i]);
  516. else
  517. break;
  518. }
  519. #endif
  520. }
  521. static void print_tracking(struct kmem_cache *s, void *object)
  522. {
  523. unsigned long pr_time = jiffies;
  524. if (!(s->flags & SLAB_STORE_USER))
  525. return;
  526. print_track("Allocated", get_track(s, object, TRACK_ALLOC), pr_time);
  527. print_track("Freed", get_track(s, object, TRACK_FREE), pr_time);
  528. }
  529. static void print_page_info(struct page *page)
  530. {
  531. pr_err("INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
  532. page, page->objects, page->inuse, page->freelist, page->flags);
  533. }
  534. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  535. {
  536. struct va_format vaf;
  537. va_list args;
  538. va_start(args, fmt);
  539. vaf.fmt = fmt;
  540. vaf.va = &args;
  541. pr_err("=============================================================================\n");
  542. pr_err("BUG %s (%s): %pV\n", s->name, print_tainted(), &vaf);
  543. pr_err("-----------------------------------------------------------------------------\n\n");
  544. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  545. va_end(args);
  546. }
  547. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  548. {
  549. struct va_format vaf;
  550. va_list args;
  551. va_start(args, fmt);
  552. vaf.fmt = fmt;
  553. vaf.va = &args;
  554. pr_err("FIX %s: %pV\n", s->name, &vaf);
  555. va_end(args);
  556. }
  557. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  558. {
  559. unsigned int off; /* Offset of last byte */
  560. u8 *addr = page_address(page);
  561. print_tracking(s, p);
  562. print_page_info(page);
  563. pr_err("INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  564. p, p - addr, get_freepointer(s, p));
  565. if (s->flags & SLAB_RED_ZONE)
  566. print_section(KERN_ERR, "Redzone ", p - s->red_left_pad,
  567. s->red_left_pad);
  568. else if (p > addr + 16)
  569. print_section(KERN_ERR, "Bytes b4 ", p - 16, 16);
  570. print_section(KERN_ERR, "Object ", p,
  571. min_t(unsigned int, s->object_size, PAGE_SIZE));
  572. if (s->flags & SLAB_RED_ZONE)
  573. print_section(KERN_ERR, "Redzone ", p + s->object_size,
  574. s->inuse - s->object_size);
  575. if (s->offset)
  576. off = s->offset + sizeof(void *);
  577. else
  578. off = s->inuse;
  579. if (s->flags & SLAB_STORE_USER)
  580. off += 2 * sizeof(struct track);
  581. off += kasan_metadata_size(s);
  582. if (off != size_from_object(s))
  583. /* Beginning of the filler is the free pointer */
  584. print_section(KERN_ERR, "Padding ", p + off,
  585. size_from_object(s) - off);
  586. dump_stack();
  587. }
  588. void object_err(struct kmem_cache *s, struct page *page,
  589. u8 *object, char *reason)
  590. {
  591. slab_bug(s, "%s", reason);
  592. print_trailer(s, page, object);
  593. }
  594. static __printf(3, 4) void slab_err(struct kmem_cache *s, struct page *page,
  595. const char *fmt, ...)
  596. {
  597. va_list args;
  598. char buf[100];
  599. va_start(args, fmt);
  600. vsnprintf(buf, sizeof(buf), fmt, args);
  601. va_end(args);
  602. slab_bug(s, "%s", buf);
  603. print_page_info(page);
  604. dump_stack();
  605. }
  606. static void init_object(struct kmem_cache *s, void *object, u8 val)
  607. {
  608. u8 *p = object;
  609. if (s->flags & SLAB_RED_ZONE)
  610. memset(p - s->red_left_pad, val, s->red_left_pad);
  611. if (s->flags & __OBJECT_POISON) {
  612. memset(p, POISON_FREE, s->object_size - 1);
  613. p[s->object_size - 1] = POISON_END;
  614. }
  615. if (s->flags & SLAB_RED_ZONE)
  616. memset(p + s->object_size, val, s->inuse - s->object_size);
  617. }
  618. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  619. void *from, void *to)
  620. {
  621. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  622. memset(from, data, to - from);
  623. }
  624. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  625. u8 *object, char *what,
  626. u8 *start, unsigned int value, unsigned int bytes)
  627. {
  628. u8 *fault;
  629. u8 *end;
  630. metadata_access_enable();
  631. fault = memchr_inv(start, value, bytes);
  632. metadata_access_disable();
  633. if (!fault)
  634. return 1;
  635. end = start + bytes;
  636. while (end > fault && end[-1] == value)
  637. end--;
  638. slab_bug(s, "%s overwritten", what);
  639. pr_err("INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  640. fault, end - 1, fault[0], value);
  641. print_trailer(s, page, object);
  642. restore_bytes(s, what, value, fault, end);
  643. return 0;
  644. }
  645. /*
  646. * Object layout:
  647. *
  648. * object address
  649. * Bytes of the object to be managed.
  650. * If the freepointer may overlay the object then the free
  651. * pointer is the first word of the object.
  652. *
  653. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  654. * 0xa5 (POISON_END)
  655. *
  656. * object + s->object_size
  657. * Padding to reach word boundary. This is also used for Redzoning.
  658. * Padding is extended by another word if Redzoning is enabled and
  659. * object_size == inuse.
  660. *
  661. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  662. * 0xcc (RED_ACTIVE) for objects in use.
  663. *
  664. * object + s->inuse
  665. * Meta data starts here.
  666. *
  667. * A. Free pointer (if we cannot overwrite object on free)
  668. * B. Tracking data for SLAB_STORE_USER
  669. * C. Padding to reach required alignment boundary or at mininum
  670. * one word if debugging is on to be able to detect writes
  671. * before the word boundary.
  672. *
  673. * Padding is done using 0x5a (POISON_INUSE)
  674. *
  675. * object + s->size
  676. * Nothing is used beyond s->size.
  677. *
  678. * If slabcaches are merged then the object_size and inuse boundaries are mostly
  679. * ignored. And therefore no slab options that rely on these boundaries
  680. * may be used with merged slabcaches.
  681. */
  682. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  683. {
  684. unsigned long off = s->inuse; /* The end of info */
  685. if (s->offset)
  686. /* Freepointer is placed after the object. */
  687. off += sizeof(void *);
  688. if (s->flags & SLAB_STORE_USER)
  689. /* We also have user information there */
  690. off += 2 * sizeof(struct track);
  691. off += kasan_metadata_size(s);
  692. if (size_from_object(s) == off)
  693. return 1;
  694. return check_bytes_and_report(s, page, p, "Object padding",
  695. p + off, POISON_INUSE, size_from_object(s) - off);
  696. }
  697. /* Check the pad bytes at the end of a slab page */
  698. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  699. {
  700. u8 *start;
  701. u8 *fault;
  702. u8 *end;
  703. u8 *pad;
  704. int length;
  705. int remainder;
  706. if (!(s->flags & SLAB_POISON))
  707. return 1;
  708. start = page_address(page);
  709. length = PAGE_SIZE << compound_order(page);
  710. end = start + length;
  711. remainder = length % s->size;
  712. if (!remainder)
  713. return 1;
  714. pad = end - remainder;
  715. metadata_access_enable();
  716. fault = memchr_inv(pad, POISON_INUSE, remainder);
  717. metadata_access_disable();
  718. if (!fault)
  719. return 1;
  720. while (end > fault && end[-1] == POISON_INUSE)
  721. end--;
  722. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  723. print_section(KERN_ERR, "Padding ", pad, remainder);
  724. restore_bytes(s, "slab padding", POISON_INUSE, fault, end);
  725. return 0;
  726. }
  727. static int check_object(struct kmem_cache *s, struct page *page,
  728. void *object, u8 val)
  729. {
  730. u8 *p = object;
  731. u8 *endobject = object + s->object_size;
  732. if (s->flags & SLAB_RED_ZONE) {
  733. if (!check_bytes_and_report(s, page, object, "Redzone",
  734. object - s->red_left_pad, val, s->red_left_pad))
  735. return 0;
  736. if (!check_bytes_and_report(s, page, object, "Redzone",
  737. endobject, val, s->inuse - s->object_size))
  738. return 0;
  739. } else {
  740. if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
  741. check_bytes_and_report(s, page, p, "Alignment padding",
  742. endobject, POISON_INUSE,
  743. s->inuse - s->object_size);
  744. }
  745. }
  746. if (s->flags & SLAB_POISON) {
  747. if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
  748. (!check_bytes_and_report(s, page, p, "Poison", p,
  749. POISON_FREE, s->object_size - 1) ||
  750. !check_bytes_and_report(s, page, p, "Poison",
  751. p + s->object_size - 1, POISON_END, 1)))
  752. return 0;
  753. /*
  754. * check_pad_bytes cleans up on its own.
  755. */
  756. check_pad_bytes(s, page, p);
  757. }
  758. if (!s->offset && val == SLUB_RED_ACTIVE)
  759. /*
  760. * Object and freepointer overlap. Cannot check
  761. * freepointer while object is allocated.
  762. */
  763. return 1;
  764. /* Check free pointer validity */
  765. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  766. object_err(s, page, p, "Freepointer corrupt");
  767. /*
  768. * No choice but to zap it and thus lose the remainder
  769. * of the free objects in this slab. May cause
  770. * another error because the object count is now wrong.
  771. */
  772. set_freepointer(s, p, NULL);
  773. return 0;
  774. }
  775. return 1;
  776. }
  777. static int check_slab(struct kmem_cache *s, struct page *page)
  778. {
  779. int maxobj;
  780. VM_BUG_ON(!irqs_disabled());
  781. if (!PageSlab(page)) {
  782. slab_err(s, page, "Not a valid slab page");
  783. return 0;
  784. }
  785. maxobj = order_objects(compound_order(page), s->size);
  786. if (page->objects > maxobj) {
  787. slab_err(s, page, "objects %u > max %u",
  788. page->objects, maxobj);
  789. return 0;
  790. }
  791. if (page->inuse > page->objects) {
  792. slab_err(s, page, "inuse %u > max %u",
  793. page->inuse, page->objects);
  794. return 0;
  795. }
  796. /* Slab_pad_check fixes things up after itself */
  797. slab_pad_check(s, page);
  798. return 1;
  799. }
  800. /*
  801. * Determine if a certain object on a page is on the freelist. Must hold the
  802. * slab lock to guarantee that the chains are in a consistent state.
  803. */
  804. static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
  805. {
  806. int nr = 0;
  807. void *fp;
  808. void *object = NULL;
  809. int max_objects;
  810. fp = page->freelist;
  811. while (fp && nr <= page->objects) {
  812. if (fp == search)
  813. return 1;
  814. if (!check_valid_pointer(s, page, fp)) {
  815. if (object) {
  816. object_err(s, page, object,
  817. "Freechain corrupt");
  818. set_freepointer(s, object, NULL);
  819. } else {
  820. slab_err(s, page, "Freepointer corrupt");
  821. page->freelist = NULL;
  822. page->inuse = page->objects;
  823. slab_fix(s, "Freelist cleared");
  824. return 0;
  825. }
  826. break;
  827. }
  828. object = fp;
  829. fp = get_freepointer(s, object);
  830. nr++;
  831. }
  832. max_objects = order_objects(compound_order(page), s->size);
  833. if (max_objects > MAX_OBJS_PER_PAGE)
  834. max_objects = MAX_OBJS_PER_PAGE;
  835. if (page->objects != max_objects) {
  836. slab_err(s, page, "Wrong number of objects. Found %d but should be %d",
  837. page->objects, max_objects);
  838. page->objects = max_objects;
  839. slab_fix(s, "Number of objects adjusted.");
  840. }
  841. if (page->inuse != page->objects - nr) {
  842. slab_err(s, page, "Wrong object count. Counter is %d but counted were %d",
  843. page->inuse, page->objects - nr);
  844. page->inuse = page->objects - nr;
  845. slab_fix(s, "Object count adjusted.");
  846. }
  847. return search == NULL;
  848. }
  849. static void trace(struct kmem_cache *s, struct page *page, void *object,
  850. int alloc)
  851. {
  852. if (s->flags & SLAB_TRACE) {
  853. pr_info("TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  854. s->name,
  855. alloc ? "alloc" : "free",
  856. object, page->inuse,
  857. page->freelist);
  858. if (!alloc)
  859. print_section(KERN_INFO, "Object ", (void *)object,
  860. s->object_size);
  861. dump_stack();
  862. }
  863. }
  864. /*
  865. * Tracking of fully allocated slabs for debugging purposes.
  866. */
  867. static void add_full(struct kmem_cache *s,
  868. struct kmem_cache_node *n, struct page *page)
  869. {
  870. if (!(s->flags & SLAB_STORE_USER))
  871. return;
  872. lockdep_assert_held(&n->list_lock);
  873. list_add(&page->lru, &n->full);
  874. }
  875. static void remove_full(struct kmem_cache *s, struct kmem_cache_node *n, struct page *page)
  876. {
  877. if (!(s->flags & SLAB_STORE_USER))
  878. return;
  879. lockdep_assert_held(&n->list_lock);
  880. list_del(&page->lru);
  881. }
  882. /* Tracking of the number of slabs for debugging purposes */
  883. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  884. {
  885. struct kmem_cache_node *n = get_node(s, node);
  886. return atomic_long_read(&n->nr_slabs);
  887. }
  888. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  889. {
  890. return atomic_long_read(&n->nr_slabs);
  891. }
  892. static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
  893. {
  894. struct kmem_cache_node *n = get_node(s, node);
  895. /*
  896. * May be called early in order to allocate a slab for the
  897. * kmem_cache_node structure. Solve the chicken-egg
  898. * dilemma by deferring the increment of the count during
  899. * bootstrap (see early_kmem_cache_node_alloc).
  900. */
  901. if (likely(n)) {
  902. atomic_long_inc(&n->nr_slabs);
  903. atomic_long_add(objects, &n->total_objects);
  904. }
  905. }
  906. static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
  907. {
  908. struct kmem_cache_node *n = get_node(s, node);
  909. atomic_long_dec(&n->nr_slabs);
  910. atomic_long_sub(objects, &n->total_objects);
  911. }
  912. /* Object debug checks for alloc/free paths */
  913. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  914. void *object)
  915. {
  916. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  917. return;
  918. init_object(s, object, SLUB_RED_INACTIVE);
  919. init_tracking(s, object);
  920. }
  921. static inline int alloc_consistency_checks(struct kmem_cache *s,
  922. struct page *page,
  923. void *object, unsigned long addr)
  924. {
  925. if (!check_slab(s, page))
  926. return 0;
  927. if (!check_valid_pointer(s, page, object)) {
  928. object_err(s, page, object, "Freelist Pointer check fails");
  929. return 0;
  930. }
  931. if (!check_object(s, page, object, SLUB_RED_INACTIVE))
  932. return 0;
  933. return 1;
  934. }
  935. static noinline int alloc_debug_processing(struct kmem_cache *s,
  936. struct page *page,
  937. void *object, unsigned long addr)
  938. {
  939. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  940. if (!alloc_consistency_checks(s, page, object, addr))
  941. goto bad;
  942. }
  943. /* Success perform special debug activities for allocs */
  944. if (s->flags & SLAB_STORE_USER)
  945. set_track(s, object, TRACK_ALLOC, addr);
  946. trace(s, page, object, 1);
  947. init_object(s, object, SLUB_RED_ACTIVE);
  948. return 1;
  949. bad:
  950. if (PageSlab(page)) {
  951. /*
  952. * If this is a slab page then lets do the best we can
  953. * to avoid issues in the future. Marking all objects
  954. * as used avoids touching the remaining objects.
  955. */
  956. slab_fix(s, "Marking all objects used");
  957. page->inuse = page->objects;
  958. page->freelist = NULL;
  959. }
  960. return 0;
  961. }
  962. static inline int free_consistency_checks(struct kmem_cache *s,
  963. struct page *page, void *object, unsigned long addr)
  964. {
  965. if (!check_valid_pointer(s, page, object)) {
  966. slab_err(s, page, "Invalid object pointer 0x%p", object);
  967. return 0;
  968. }
  969. if (on_freelist(s, page, object)) {
  970. object_err(s, page, object, "Object already free");
  971. return 0;
  972. }
  973. if (!check_object(s, page, object, SLUB_RED_ACTIVE))
  974. return 0;
  975. if (unlikely(s != page->slab_cache)) {
  976. if (!PageSlab(page)) {
  977. slab_err(s, page, "Attempt to free object(0x%p) outside of slab",
  978. object);
  979. } else if (!page->slab_cache) {
  980. pr_err("SLUB <none>: no slab for object 0x%p.\n",
  981. object);
  982. dump_stack();
  983. } else
  984. object_err(s, page, object,
  985. "page slab pointer corrupt.");
  986. return 0;
  987. }
  988. return 1;
  989. }
  990. /* Supports checking bulk free of a constructed freelist */
  991. static noinline int free_debug_processing(
  992. struct kmem_cache *s, struct page *page,
  993. void *head, void *tail, int bulk_cnt,
  994. unsigned long addr)
  995. {
  996. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  997. void *object = head;
  998. int cnt = 0;
  999. unsigned long uninitialized_var(flags);
  1000. int ret = 0;
  1001. spin_lock_irqsave(&n->list_lock, flags);
  1002. slab_lock(page);
  1003. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  1004. if (!check_slab(s, page))
  1005. goto out;
  1006. }
  1007. next_object:
  1008. cnt++;
  1009. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  1010. if (!free_consistency_checks(s, page, object, addr))
  1011. goto out;
  1012. }
  1013. if (s->flags & SLAB_STORE_USER)
  1014. set_track(s, object, TRACK_FREE, addr);
  1015. trace(s, page, object, 0);
  1016. /* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
  1017. init_object(s, object, SLUB_RED_INACTIVE);
  1018. /* Reached end of constructed freelist yet? */
  1019. if (object != tail) {
  1020. object = get_freepointer(s, object);
  1021. goto next_object;
  1022. }
  1023. ret = 1;
  1024. out:
  1025. if (cnt != bulk_cnt)
  1026. slab_err(s, page, "Bulk freelist count(%d) invalid(%d)\n",
  1027. bulk_cnt, cnt);
  1028. slab_unlock(page);
  1029. spin_unlock_irqrestore(&n->list_lock, flags);
  1030. if (!ret)
  1031. slab_fix(s, "Object at 0x%p not freed", object);
  1032. return ret;
  1033. }
  1034. static int __init setup_slub_debug(char *str)
  1035. {
  1036. slub_debug = DEBUG_DEFAULT_FLAGS;
  1037. if (*str++ != '=' || !*str)
  1038. /*
  1039. * No options specified. Switch on full debugging.
  1040. */
  1041. goto out;
  1042. if (*str == ',')
  1043. /*
  1044. * No options but restriction on slabs. This means full
  1045. * debugging for slabs matching a pattern.
  1046. */
  1047. goto check_slabs;
  1048. slub_debug = 0;
  1049. if (*str == '-')
  1050. /*
  1051. * Switch off all debugging measures.
  1052. */
  1053. goto out;
  1054. /*
  1055. * Determine which debug features should be switched on
  1056. */
  1057. for (; *str && *str != ','; str++) {
  1058. switch (tolower(*str)) {
  1059. case 'f':
  1060. slub_debug |= SLAB_CONSISTENCY_CHECKS;
  1061. break;
  1062. case 'z':
  1063. slub_debug |= SLAB_RED_ZONE;
  1064. break;
  1065. case 'p':
  1066. slub_debug |= SLAB_POISON;
  1067. break;
  1068. case 'u':
  1069. slub_debug |= SLAB_STORE_USER;
  1070. break;
  1071. case 't':
  1072. slub_debug |= SLAB_TRACE;
  1073. break;
  1074. case 'a':
  1075. slub_debug |= SLAB_FAILSLAB;
  1076. break;
  1077. case 'o':
  1078. /*
  1079. * Avoid enabling debugging on caches if its minimum
  1080. * order would increase as a result.
  1081. */
  1082. disable_higher_order_debug = 1;
  1083. break;
  1084. default:
  1085. pr_err("slub_debug option '%c' unknown. skipped\n",
  1086. *str);
  1087. }
  1088. }
  1089. check_slabs:
  1090. if (*str == ',')
  1091. slub_debug_slabs = str + 1;
  1092. out:
  1093. return 1;
  1094. }
  1095. __setup("slub_debug", setup_slub_debug);
  1096. /*
  1097. * kmem_cache_flags - apply debugging options to the cache
  1098. * @object_size: the size of an object without meta data
  1099. * @flags: flags to set
  1100. * @name: name of the cache
  1101. * @ctor: constructor function
  1102. *
  1103. * Debug option(s) are applied to @flags. In addition to the debug
  1104. * option(s), if a slab name (or multiple) is specified i.e.
  1105. * slub_debug=<Debug-Options>,<slab name1>,<slab name2> ...
  1106. * then only the select slabs will receive the debug option(s).
  1107. */
  1108. slab_flags_t kmem_cache_flags(unsigned int object_size,
  1109. slab_flags_t flags, const char *name,
  1110. void (*ctor)(void *))
  1111. {
  1112. char *iter;
  1113. size_t len;
  1114. /* If slub_debug = 0, it folds into the if conditional. */
  1115. if (!slub_debug_slabs)
  1116. return flags | slub_debug;
  1117. len = strlen(name);
  1118. iter = slub_debug_slabs;
  1119. while (*iter) {
  1120. char *end, *glob;
  1121. size_t cmplen;
  1122. end = strchr(iter, ',');
  1123. if (!end)
  1124. end = iter + strlen(iter);
  1125. glob = strnchr(iter, end - iter, '*');
  1126. if (glob)
  1127. cmplen = glob - iter;
  1128. else
  1129. cmplen = max_t(size_t, len, (end - iter));
  1130. if (!strncmp(name, iter, cmplen)) {
  1131. flags |= slub_debug;
  1132. break;
  1133. }
  1134. if (!*end)
  1135. break;
  1136. iter = end + 1;
  1137. }
  1138. return flags;
  1139. }
  1140. #else /* !CONFIG_SLUB_DEBUG */
  1141. static inline void setup_object_debug(struct kmem_cache *s,
  1142. struct page *page, void *object) {}
  1143. static inline int alloc_debug_processing(struct kmem_cache *s,
  1144. struct page *page, void *object, unsigned long addr) { return 0; }
  1145. static inline int free_debug_processing(
  1146. struct kmem_cache *s, struct page *page,
  1147. void *head, void *tail, int bulk_cnt,
  1148. unsigned long addr) { return 0; }
  1149. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  1150. { return 1; }
  1151. static inline int check_object(struct kmem_cache *s, struct page *page,
  1152. void *object, u8 val) { return 1; }
  1153. static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
  1154. struct page *page) {}
  1155. static inline void remove_full(struct kmem_cache *s, struct kmem_cache_node *n,
  1156. struct page *page) {}
  1157. slab_flags_t kmem_cache_flags(unsigned int object_size,
  1158. slab_flags_t flags, const char *name,
  1159. void (*ctor)(void *))
  1160. {
  1161. return flags;
  1162. }
  1163. #define slub_debug 0
  1164. #define disable_higher_order_debug 0
  1165. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  1166. { return 0; }
  1167. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  1168. { return 0; }
  1169. static inline void inc_slabs_node(struct kmem_cache *s, int node,
  1170. int objects) {}
  1171. static inline void dec_slabs_node(struct kmem_cache *s, int node,
  1172. int objects) {}
  1173. #endif /* CONFIG_SLUB_DEBUG */
  1174. /*
  1175. * Hooks for other subsystems that check memory allocations. In a typical
  1176. * production configuration these hooks all should produce no code at all.
  1177. */
  1178. static inline void kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
  1179. {
  1180. kmemleak_alloc(ptr, size, 1, flags);
  1181. kasan_kmalloc_large(ptr, size, flags);
  1182. }
  1183. static __always_inline void kfree_hook(void *x)
  1184. {
  1185. kmemleak_free(x);
  1186. kasan_kfree_large(x, _RET_IP_);
  1187. }
  1188. static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x)
  1189. {
  1190. kmemleak_free_recursive(x, s->flags);
  1191. /*
  1192. * Trouble is that we may no longer disable interrupts in the fast path
  1193. * So in order to make the debug calls that expect irqs to be
  1194. * disabled we need to disable interrupts temporarily.
  1195. */
  1196. #ifdef CONFIG_LOCKDEP
  1197. {
  1198. unsigned long flags;
  1199. local_irq_save(flags);
  1200. debug_check_no_locks_freed(x, s->object_size);
  1201. local_irq_restore(flags);
  1202. }
  1203. #endif
  1204. if (!(s->flags & SLAB_DEBUG_OBJECTS))
  1205. debug_check_no_obj_freed(x, s->object_size);
  1206. /* KASAN might put x into memory quarantine, delaying its reuse */
  1207. return kasan_slab_free(s, x, _RET_IP_);
  1208. }
  1209. static inline bool slab_free_freelist_hook(struct kmem_cache *s,
  1210. void **head, void **tail)
  1211. {
  1212. /*
  1213. * Compiler cannot detect this function can be removed if slab_free_hook()
  1214. * evaluates to nothing. Thus, catch all relevant config debug options here.
  1215. */
  1216. #if defined(CONFIG_LOCKDEP) || \
  1217. defined(CONFIG_DEBUG_KMEMLEAK) || \
  1218. defined(CONFIG_DEBUG_OBJECTS_FREE) || \
  1219. defined(CONFIG_KASAN)
  1220. void *object;
  1221. void *next = *head;
  1222. void *old_tail = *tail ? *tail : *head;
  1223. /* Head and tail of the reconstructed freelist */
  1224. *head = NULL;
  1225. *tail = NULL;
  1226. do {
  1227. object = next;
  1228. next = get_freepointer(s, object);
  1229. /* If object's reuse doesn't have to be delayed */
  1230. if (!slab_free_hook(s, object)) {
  1231. /* Move object to the new freelist */
  1232. set_freepointer(s, object, *head);
  1233. *head = object;
  1234. if (!*tail)
  1235. *tail = object;
  1236. }
  1237. } while (object != old_tail);
  1238. if (*head == *tail)
  1239. *tail = NULL;
  1240. return *head != NULL;
  1241. #else
  1242. return true;
  1243. #endif
  1244. }
  1245. static void setup_object(struct kmem_cache *s, struct page *page,
  1246. void *object)
  1247. {
  1248. setup_object_debug(s, page, object);
  1249. kasan_init_slab_obj(s, object);
  1250. if (unlikely(s->ctor)) {
  1251. kasan_unpoison_object_data(s, object);
  1252. s->ctor(object);
  1253. kasan_poison_object_data(s, object);
  1254. }
  1255. }
  1256. /*
  1257. * Slab allocation and freeing
  1258. */
  1259. static inline struct page *alloc_slab_page(struct kmem_cache *s,
  1260. gfp_t flags, int node, struct kmem_cache_order_objects oo)
  1261. {
  1262. struct page *page;
  1263. unsigned int order = oo_order(oo);
  1264. if (node == NUMA_NO_NODE)
  1265. page = alloc_pages(flags, order);
  1266. else
  1267. page = __alloc_pages_node(node, flags, order);
  1268. if (page && memcg_charge_slab(page, flags, order, s)) {
  1269. __free_pages(page, order);
  1270. page = NULL;
  1271. }
  1272. return page;
  1273. }
  1274. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  1275. /* Pre-initialize the random sequence cache */
  1276. static int init_cache_random_seq(struct kmem_cache *s)
  1277. {
  1278. unsigned int count = oo_objects(s->oo);
  1279. int err;
  1280. /* Bailout if already initialised */
  1281. if (s->random_seq)
  1282. return 0;
  1283. err = cache_random_seq_create(s, count, GFP_KERNEL);
  1284. if (err) {
  1285. pr_err("SLUB: Unable to initialize free list for %s\n",
  1286. s->name);
  1287. return err;
  1288. }
  1289. /* Transform to an offset on the set of pages */
  1290. if (s->random_seq) {
  1291. unsigned int i;
  1292. for (i = 0; i < count; i++)
  1293. s->random_seq[i] *= s->size;
  1294. }
  1295. return 0;
  1296. }
  1297. /* Initialize each random sequence freelist per cache */
  1298. static void __init init_freelist_randomization(void)
  1299. {
  1300. struct kmem_cache *s;
  1301. mutex_lock(&slab_mutex);
  1302. list_for_each_entry(s, &slab_caches, list)
  1303. init_cache_random_seq(s);
  1304. mutex_unlock(&slab_mutex);
  1305. }
  1306. /* Get the next entry on the pre-computed freelist randomized */
  1307. static void *next_freelist_entry(struct kmem_cache *s, struct page *page,
  1308. unsigned long *pos, void *start,
  1309. unsigned long page_limit,
  1310. unsigned long freelist_count)
  1311. {
  1312. unsigned int idx;
  1313. /*
  1314. * If the target page allocation failed, the number of objects on the
  1315. * page might be smaller than the usual size defined by the cache.
  1316. */
  1317. do {
  1318. idx = s->random_seq[*pos];
  1319. *pos += 1;
  1320. if (*pos >= freelist_count)
  1321. *pos = 0;
  1322. } while (unlikely(idx >= page_limit));
  1323. return (char *)start + idx;
  1324. }
  1325. /* Shuffle the single linked freelist based on a random pre-computed sequence */
  1326. static bool shuffle_freelist(struct kmem_cache *s, struct page *page)
  1327. {
  1328. void *start;
  1329. void *cur;
  1330. void *next;
  1331. unsigned long idx, pos, page_limit, freelist_count;
  1332. if (page->objects < 2 || !s->random_seq)
  1333. return false;
  1334. freelist_count = oo_objects(s->oo);
  1335. pos = get_random_int() % freelist_count;
  1336. page_limit = page->objects * s->size;
  1337. start = fixup_red_left(s, page_address(page));
  1338. /* First entry is used as the base of the freelist */
  1339. cur = next_freelist_entry(s, page, &pos, start, page_limit,
  1340. freelist_count);
  1341. page->freelist = cur;
  1342. for (idx = 1; idx < page->objects; idx++) {
  1343. setup_object(s, page, cur);
  1344. next = next_freelist_entry(s, page, &pos, start, page_limit,
  1345. freelist_count);
  1346. set_freepointer(s, cur, next);
  1347. cur = next;
  1348. }
  1349. setup_object(s, page, cur);
  1350. set_freepointer(s, cur, NULL);
  1351. return true;
  1352. }
  1353. #else
  1354. static inline int init_cache_random_seq(struct kmem_cache *s)
  1355. {
  1356. return 0;
  1357. }
  1358. static inline void init_freelist_randomization(void) { }
  1359. static inline bool shuffle_freelist(struct kmem_cache *s, struct page *page)
  1360. {
  1361. return false;
  1362. }
  1363. #endif /* CONFIG_SLAB_FREELIST_RANDOM */
  1364. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  1365. {
  1366. struct page *page;
  1367. struct kmem_cache_order_objects oo = s->oo;
  1368. gfp_t alloc_gfp;
  1369. void *start, *p;
  1370. int idx, order;
  1371. bool shuffle;
  1372. flags &= gfp_allowed_mask;
  1373. if (gfpflags_allow_blocking(flags))
  1374. local_irq_enable();
  1375. flags |= s->allocflags;
  1376. /*
  1377. * Let the initial higher-order allocation fail under memory pressure
  1378. * so we fall-back to the minimum order allocation.
  1379. */
  1380. alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
  1381. if ((alloc_gfp & __GFP_DIRECT_RECLAIM) && oo_order(oo) > oo_order(s->min))
  1382. alloc_gfp = (alloc_gfp | __GFP_NOMEMALLOC) & ~(__GFP_RECLAIM|__GFP_NOFAIL);
  1383. page = alloc_slab_page(s, alloc_gfp, node, oo);
  1384. if (unlikely(!page)) {
  1385. oo = s->min;
  1386. alloc_gfp = flags;
  1387. /*
  1388. * Allocation may have failed due to fragmentation.
  1389. * Try a lower order alloc if possible
  1390. */
  1391. page = alloc_slab_page(s, alloc_gfp, node, oo);
  1392. if (unlikely(!page))
  1393. goto out;
  1394. stat(s, ORDER_FALLBACK);
  1395. }
  1396. page->objects = oo_objects(oo);
  1397. order = compound_order(page);
  1398. page->slab_cache = s;
  1399. __SetPageSlab(page);
  1400. if (page_is_pfmemalloc(page))
  1401. SetPageSlabPfmemalloc(page);
  1402. start = page_address(page);
  1403. if (unlikely(s->flags & SLAB_POISON))
  1404. memset(start, POISON_INUSE, PAGE_SIZE << order);
  1405. kasan_poison_slab(page);
  1406. shuffle = shuffle_freelist(s, page);
  1407. if (!shuffle) {
  1408. for_each_object_idx(p, idx, s, start, page->objects) {
  1409. setup_object(s, page, p);
  1410. if (likely(idx < page->objects))
  1411. set_freepointer(s, p, p + s->size);
  1412. else
  1413. set_freepointer(s, p, NULL);
  1414. }
  1415. page->freelist = fixup_red_left(s, start);
  1416. }
  1417. page->inuse = page->objects;
  1418. page->frozen = 1;
  1419. out:
  1420. if (gfpflags_allow_blocking(flags))
  1421. local_irq_disable();
  1422. if (!page)
  1423. return NULL;
  1424. mod_lruvec_page_state(page,
  1425. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  1426. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  1427. 1 << oo_order(oo));
  1428. inc_slabs_node(s, page_to_nid(page), page->objects);
  1429. return page;
  1430. }
  1431. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  1432. {
  1433. if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  1434. gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
  1435. flags &= ~GFP_SLAB_BUG_MASK;
  1436. pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
  1437. invalid_mask, &invalid_mask, flags, &flags);
  1438. dump_stack();
  1439. }
  1440. return allocate_slab(s,
  1441. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  1442. }
  1443. static void __free_slab(struct kmem_cache *s, struct page *page)
  1444. {
  1445. int order = compound_order(page);
  1446. int pages = 1 << order;
  1447. if (s->flags & SLAB_CONSISTENCY_CHECKS) {
  1448. void *p;
  1449. slab_pad_check(s, page);
  1450. for_each_object(p, s, page_address(page),
  1451. page->objects)
  1452. check_object(s, page, p, SLUB_RED_INACTIVE);
  1453. }
  1454. mod_lruvec_page_state(page,
  1455. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  1456. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  1457. -pages);
  1458. __ClearPageSlabPfmemalloc(page);
  1459. __ClearPageSlab(page);
  1460. page->mapping = NULL;
  1461. if (current->reclaim_state)
  1462. current->reclaim_state->reclaimed_slab += pages;
  1463. memcg_uncharge_slab(page, order, s);
  1464. __free_pages(page, order);
  1465. }
  1466. static void rcu_free_slab(struct rcu_head *h)
  1467. {
  1468. struct page *page = container_of(h, struct page, rcu_head);
  1469. __free_slab(page->slab_cache, page);
  1470. }
  1471. static void free_slab(struct kmem_cache *s, struct page *page)
  1472. {
  1473. if (unlikely(s->flags & SLAB_TYPESAFE_BY_RCU)) {
  1474. call_rcu(&page->rcu_head, rcu_free_slab);
  1475. } else
  1476. __free_slab(s, page);
  1477. }
  1478. static void discard_slab(struct kmem_cache *s, struct page *page)
  1479. {
  1480. dec_slabs_node(s, page_to_nid(page), page->objects);
  1481. free_slab(s, page);
  1482. }
  1483. /*
  1484. * Management of partially allocated slabs.
  1485. */
  1486. static inline void
  1487. __add_partial(struct kmem_cache_node *n, struct page *page, int tail)
  1488. {
  1489. n->nr_partial++;
  1490. if (tail == DEACTIVATE_TO_TAIL)
  1491. list_add_tail(&page->lru, &n->partial);
  1492. else
  1493. list_add(&page->lru, &n->partial);
  1494. }
  1495. static inline void add_partial(struct kmem_cache_node *n,
  1496. struct page *page, int tail)
  1497. {
  1498. lockdep_assert_held(&n->list_lock);
  1499. __add_partial(n, page, tail);
  1500. }
  1501. static inline void remove_partial(struct kmem_cache_node *n,
  1502. struct page *page)
  1503. {
  1504. lockdep_assert_held(&n->list_lock);
  1505. list_del(&page->lru);
  1506. n->nr_partial--;
  1507. }
  1508. /*
  1509. * Remove slab from the partial list, freeze it and
  1510. * return the pointer to the freelist.
  1511. *
  1512. * Returns a list of objects or NULL if it fails.
  1513. */
  1514. static inline void *acquire_slab(struct kmem_cache *s,
  1515. struct kmem_cache_node *n, struct page *page,
  1516. int mode, int *objects)
  1517. {
  1518. void *freelist;
  1519. unsigned long counters;
  1520. struct page new;
  1521. lockdep_assert_held(&n->list_lock);
  1522. /*
  1523. * Zap the freelist and set the frozen bit.
  1524. * The old freelist is the list of objects for the
  1525. * per cpu allocation list.
  1526. */
  1527. freelist = page->freelist;
  1528. counters = page->counters;
  1529. new.counters = counters;
  1530. *objects = new.objects - new.inuse;
  1531. if (mode) {
  1532. new.inuse = page->objects;
  1533. new.freelist = NULL;
  1534. } else {
  1535. new.freelist = freelist;
  1536. }
  1537. VM_BUG_ON(new.frozen);
  1538. new.frozen = 1;
  1539. if (!__cmpxchg_double_slab(s, page,
  1540. freelist, counters,
  1541. new.freelist, new.counters,
  1542. "acquire_slab"))
  1543. return NULL;
  1544. remove_partial(n, page);
  1545. WARN_ON(!freelist);
  1546. return freelist;
  1547. }
  1548. static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
  1549. static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
  1550. /*
  1551. * Try to allocate a partial slab from a specific node.
  1552. */
  1553. static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  1554. struct kmem_cache_cpu *c, gfp_t flags)
  1555. {
  1556. struct page *page, *page2;
  1557. void *object = NULL;
  1558. unsigned int available = 0;
  1559. int objects;
  1560. /*
  1561. * Racy check. If we mistakenly see no partial slabs then we
  1562. * just allocate an empty slab. If we mistakenly try to get a
  1563. * partial slab and there is none available then get_partials()
  1564. * will return NULL.
  1565. */
  1566. if (!n || !n->nr_partial)
  1567. return NULL;
  1568. spin_lock(&n->list_lock);
  1569. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  1570. void *t;
  1571. if (!pfmemalloc_match(page, flags))
  1572. continue;
  1573. t = acquire_slab(s, n, page, object == NULL, &objects);
  1574. if (!t)
  1575. break;
  1576. available += objects;
  1577. if (!object) {
  1578. c->page = page;
  1579. stat(s, ALLOC_FROM_PARTIAL);
  1580. object = t;
  1581. } else {
  1582. put_cpu_partial(s, page, 0);
  1583. stat(s, CPU_PARTIAL_NODE);
  1584. }
  1585. if (!kmem_cache_has_cpu_partial(s)
  1586. || available > slub_cpu_partial(s) / 2)
  1587. break;
  1588. }
  1589. spin_unlock(&n->list_lock);
  1590. return object;
  1591. }
  1592. /*
  1593. * Get a page from somewhere. Search in increasing NUMA distances.
  1594. */
  1595. static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
  1596. struct kmem_cache_cpu *c)
  1597. {
  1598. #ifdef CONFIG_NUMA
  1599. struct zonelist *zonelist;
  1600. struct zoneref *z;
  1601. struct zone *zone;
  1602. enum zone_type high_zoneidx = gfp_zone(flags);
  1603. void *object;
  1604. unsigned int cpuset_mems_cookie;
  1605. /*
  1606. * The defrag ratio allows a configuration of the tradeoffs between
  1607. * inter node defragmentation and node local allocations. A lower
  1608. * defrag_ratio increases the tendency to do local allocations
  1609. * instead of attempting to obtain partial slabs from other nodes.
  1610. *
  1611. * If the defrag_ratio is set to 0 then kmalloc() always
  1612. * returns node local objects. If the ratio is higher then kmalloc()
  1613. * may return off node objects because partial slabs are obtained
  1614. * from other nodes and filled up.
  1615. *
  1616. * If /sys/kernel/slab/xx/remote_node_defrag_ratio is set to 100
  1617. * (which makes defrag_ratio = 1000) then every (well almost)
  1618. * allocation will first attempt to defrag slab caches on other nodes.
  1619. * This means scanning over all nodes to look for partial slabs which
  1620. * may be expensive if we do it every time we are trying to find a slab
  1621. * with available objects.
  1622. */
  1623. if (!s->remote_node_defrag_ratio ||
  1624. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1625. return NULL;
  1626. do {
  1627. cpuset_mems_cookie = read_mems_allowed_begin();
  1628. zonelist = node_zonelist(mempolicy_slab_node(), flags);
  1629. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1630. struct kmem_cache_node *n;
  1631. n = get_node(s, zone_to_nid(zone));
  1632. if (n && cpuset_zone_allowed(zone, flags) &&
  1633. n->nr_partial > s->min_partial) {
  1634. object = get_partial_node(s, n, c, flags);
  1635. if (object) {
  1636. /*
  1637. * Don't check read_mems_allowed_retry()
  1638. * here - if mems_allowed was updated in
  1639. * parallel, that was a harmless race
  1640. * between allocation and the cpuset
  1641. * update
  1642. */
  1643. return object;
  1644. }
  1645. }
  1646. }
  1647. } while (read_mems_allowed_retry(cpuset_mems_cookie));
  1648. #endif
  1649. return NULL;
  1650. }
  1651. /*
  1652. * Get a partial page, lock it and return it.
  1653. */
  1654. static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
  1655. struct kmem_cache_cpu *c)
  1656. {
  1657. void *object;
  1658. int searchnode = node;
  1659. if (node == NUMA_NO_NODE)
  1660. searchnode = numa_mem_id();
  1661. else if (!node_present_pages(node))
  1662. searchnode = node_to_mem_node(node);
  1663. object = get_partial_node(s, get_node(s, searchnode), c, flags);
  1664. if (object || node != NUMA_NO_NODE)
  1665. return object;
  1666. return get_any_partial(s, flags, c);
  1667. }
  1668. #ifdef CONFIG_PREEMPT
  1669. /*
  1670. * Calculate the next globally unique transaction for disambiguiation
  1671. * during cmpxchg. The transactions start with the cpu number and are then
  1672. * incremented by CONFIG_NR_CPUS.
  1673. */
  1674. #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
  1675. #else
  1676. /*
  1677. * No preemption supported therefore also no need to check for
  1678. * different cpus.
  1679. */
  1680. #define TID_STEP 1
  1681. #endif
  1682. static inline unsigned long next_tid(unsigned long tid)
  1683. {
  1684. return tid + TID_STEP;
  1685. }
  1686. static inline unsigned int tid_to_cpu(unsigned long tid)
  1687. {
  1688. return tid % TID_STEP;
  1689. }
  1690. static inline unsigned long tid_to_event(unsigned long tid)
  1691. {
  1692. return tid / TID_STEP;
  1693. }
  1694. static inline unsigned int init_tid(int cpu)
  1695. {
  1696. return cpu;
  1697. }
  1698. static inline void note_cmpxchg_failure(const char *n,
  1699. const struct kmem_cache *s, unsigned long tid)
  1700. {
  1701. #ifdef SLUB_DEBUG_CMPXCHG
  1702. unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
  1703. pr_info("%s %s: cmpxchg redo ", n, s->name);
  1704. #ifdef CONFIG_PREEMPT
  1705. if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
  1706. pr_warn("due to cpu change %d -> %d\n",
  1707. tid_to_cpu(tid), tid_to_cpu(actual_tid));
  1708. else
  1709. #endif
  1710. if (tid_to_event(tid) != tid_to_event(actual_tid))
  1711. pr_warn("due to cpu running other code. Event %ld->%ld\n",
  1712. tid_to_event(tid), tid_to_event(actual_tid));
  1713. else
  1714. pr_warn("for unknown reason: actual=%lx was=%lx target=%lx\n",
  1715. actual_tid, tid, next_tid(tid));
  1716. #endif
  1717. stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
  1718. }
  1719. static void init_kmem_cache_cpus(struct kmem_cache *s)
  1720. {
  1721. int cpu;
  1722. for_each_possible_cpu(cpu)
  1723. per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
  1724. }
  1725. /*
  1726. * Remove the cpu slab
  1727. */
  1728. static void deactivate_slab(struct kmem_cache *s, struct page *page,
  1729. void *freelist, struct kmem_cache_cpu *c)
  1730. {
  1731. enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
  1732. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1733. int lock = 0;
  1734. enum slab_modes l = M_NONE, m = M_NONE;
  1735. void *nextfree;
  1736. int tail = DEACTIVATE_TO_HEAD;
  1737. struct page new;
  1738. struct page old;
  1739. if (page->freelist) {
  1740. stat(s, DEACTIVATE_REMOTE_FREES);
  1741. tail = DEACTIVATE_TO_TAIL;
  1742. }
  1743. /*
  1744. * Stage one: Free all available per cpu objects back
  1745. * to the page freelist while it is still frozen. Leave the
  1746. * last one.
  1747. *
  1748. * There is no need to take the list->lock because the page
  1749. * is still frozen.
  1750. */
  1751. while (freelist && (nextfree = get_freepointer(s, freelist))) {
  1752. void *prior;
  1753. unsigned long counters;
  1754. do {
  1755. prior = page->freelist;
  1756. counters = page->counters;
  1757. set_freepointer(s, freelist, prior);
  1758. new.counters = counters;
  1759. new.inuse--;
  1760. VM_BUG_ON(!new.frozen);
  1761. } while (!__cmpxchg_double_slab(s, page,
  1762. prior, counters,
  1763. freelist, new.counters,
  1764. "drain percpu freelist"));
  1765. freelist = nextfree;
  1766. }
  1767. /*
  1768. * Stage two: Ensure that the page is unfrozen while the
  1769. * list presence reflects the actual number of objects
  1770. * during unfreeze.
  1771. *
  1772. * We setup the list membership and then perform a cmpxchg
  1773. * with the count. If there is a mismatch then the page
  1774. * is not unfrozen but the page is on the wrong list.
  1775. *
  1776. * Then we restart the process which may have to remove
  1777. * the page from the list that we just put it on again
  1778. * because the number of objects in the slab may have
  1779. * changed.
  1780. */
  1781. redo:
  1782. old.freelist = page->freelist;
  1783. old.counters = page->counters;
  1784. VM_BUG_ON(!old.frozen);
  1785. /* Determine target state of the slab */
  1786. new.counters = old.counters;
  1787. if (freelist) {
  1788. new.inuse--;
  1789. set_freepointer(s, freelist, old.freelist);
  1790. new.freelist = freelist;
  1791. } else
  1792. new.freelist = old.freelist;
  1793. new.frozen = 0;
  1794. if (!new.inuse && n->nr_partial >= s->min_partial)
  1795. m = M_FREE;
  1796. else if (new.freelist) {
  1797. m = M_PARTIAL;
  1798. if (!lock) {
  1799. lock = 1;
  1800. /*
  1801. * Taking the spinlock removes the possiblity
  1802. * that acquire_slab() will see a slab page that
  1803. * is frozen
  1804. */
  1805. spin_lock(&n->list_lock);
  1806. }
  1807. } else {
  1808. m = M_FULL;
  1809. if (kmem_cache_debug(s) && !lock) {
  1810. lock = 1;
  1811. /*
  1812. * This also ensures that the scanning of full
  1813. * slabs from diagnostic functions will not see
  1814. * any frozen slabs.
  1815. */
  1816. spin_lock(&n->list_lock);
  1817. }
  1818. }
  1819. if (l != m) {
  1820. if (l == M_PARTIAL)
  1821. remove_partial(n, page);
  1822. else if (l == M_FULL)
  1823. remove_full(s, n, page);
  1824. if (m == M_PARTIAL) {
  1825. add_partial(n, page, tail);
  1826. stat(s, tail);
  1827. } else if (m == M_FULL) {
  1828. stat(s, DEACTIVATE_FULL);
  1829. add_full(s, n, page);
  1830. }
  1831. }
  1832. l = m;
  1833. if (!__cmpxchg_double_slab(s, page,
  1834. old.freelist, old.counters,
  1835. new.freelist, new.counters,
  1836. "unfreezing slab"))
  1837. goto redo;
  1838. if (lock)
  1839. spin_unlock(&n->list_lock);
  1840. if (m == M_FREE) {
  1841. stat(s, DEACTIVATE_EMPTY);
  1842. discard_slab(s, page);
  1843. stat(s, FREE_SLAB);
  1844. }
  1845. c->page = NULL;
  1846. c->freelist = NULL;
  1847. }
  1848. /*
  1849. * Unfreeze all the cpu partial slabs.
  1850. *
  1851. * This function must be called with interrupts disabled
  1852. * for the cpu using c (or some other guarantee must be there
  1853. * to guarantee no concurrent accesses).
  1854. */
  1855. static void unfreeze_partials(struct kmem_cache *s,
  1856. struct kmem_cache_cpu *c)
  1857. {
  1858. #ifdef CONFIG_SLUB_CPU_PARTIAL
  1859. struct kmem_cache_node *n = NULL, *n2 = NULL;
  1860. struct page *page, *discard_page = NULL;
  1861. while ((page = c->partial)) {
  1862. struct page new;
  1863. struct page old;
  1864. c->partial = page->next;
  1865. n2 = get_node(s, page_to_nid(page));
  1866. if (n != n2) {
  1867. if (n)
  1868. spin_unlock(&n->list_lock);
  1869. n = n2;
  1870. spin_lock(&n->list_lock);
  1871. }
  1872. do {
  1873. old.freelist = page->freelist;
  1874. old.counters = page->counters;
  1875. VM_BUG_ON(!old.frozen);
  1876. new.counters = old.counters;
  1877. new.freelist = old.freelist;
  1878. new.frozen = 0;
  1879. } while (!__cmpxchg_double_slab(s, page,
  1880. old.freelist, old.counters,
  1881. new.freelist, new.counters,
  1882. "unfreezing slab"));
  1883. if (unlikely(!new.inuse && n->nr_partial >= s->min_partial)) {
  1884. page->next = discard_page;
  1885. discard_page = page;
  1886. } else {
  1887. add_partial(n, page, DEACTIVATE_TO_TAIL);
  1888. stat(s, FREE_ADD_PARTIAL);
  1889. }
  1890. }
  1891. if (n)
  1892. spin_unlock(&n->list_lock);
  1893. while (discard_page) {
  1894. page = discard_page;
  1895. discard_page = discard_page->next;
  1896. stat(s, DEACTIVATE_EMPTY);
  1897. discard_slab(s, page);
  1898. stat(s, FREE_SLAB);
  1899. }
  1900. #endif
  1901. }
  1902. /*
  1903. * Put a page that was just frozen (in __slab_free) into a partial page
  1904. * slot if available.
  1905. *
  1906. * If we did not find a slot then simply move all the partials to the
  1907. * per node partial list.
  1908. */
  1909. static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  1910. {
  1911. #ifdef CONFIG_SLUB_CPU_PARTIAL
  1912. struct page *oldpage;
  1913. int pages;
  1914. int pobjects;
  1915. preempt_disable();
  1916. do {
  1917. pages = 0;
  1918. pobjects = 0;
  1919. oldpage = this_cpu_read(s->cpu_slab->partial);
  1920. if (oldpage) {
  1921. pobjects = oldpage->pobjects;
  1922. pages = oldpage->pages;
  1923. if (drain && pobjects > s->cpu_partial) {
  1924. unsigned long flags;
  1925. /*
  1926. * partial array is full. Move the existing
  1927. * set to the per node partial list.
  1928. */
  1929. local_irq_save(flags);
  1930. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  1931. local_irq_restore(flags);
  1932. oldpage = NULL;
  1933. pobjects = 0;
  1934. pages = 0;
  1935. stat(s, CPU_PARTIAL_DRAIN);
  1936. }
  1937. }
  1938. pages++;
  1939. pobjects += page->objects - page->inuse;
  1940. page->pages = pages;
  1941. page->pobjects = pobjects;
  1942. page->next = oldpage;
  1943. } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
  1944. != oldpage);
  1945. if (unlikely(!s->cpu_partial)) {
  1946. unsigned long flags;
  1947. local_irq_save(flags);
  1948. unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
  1949. local_irq_restore(flags);
  1950. }
  1951. preempt_enable();
  1952. #endif
  1953. }
  1954. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1955. {
  1956. stat(s, CPUSLAB_FLUSH);
  1957. deactivate_slab(s, c->page, c->freelist, c);
  1958. c->tid = next_tid(c->tid);
  1959. }
  1960. /*
  1961. * Flush cpu slab.
  1962. *
  1963. * Called from IPI handler with interrupts disabled.
  1964. */
  1965. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1966. {
  1967. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1968. if (likely(c)) {
  1969. if (c->page)
  1970. flush_slab(s, c);
  1971. unfreeze_partials(s, c);
  1972. }
  1973. }
  1974. static void flush_cpu_slab(void *d)
  1975. {
  1976. struct kmem_cache *s = d;
  1977. __flush_cpu_slab(s, smp_processor_id());
  1978. }
  1979. static bool has_cpu_slab(int cpu, void *info)
  1980. {
  1981. struct kmem_cache *s = info;
  1982. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1983. return c->page || slub_percpu_partial(c);
  1984. }
  1985. static void flush_all(struct kmem_cache *s)
  1986. {
  1987. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  1988. }
  1989. /*
  1990. * Use the cpu notifier to insure that the cpu slabs are flushed when
  1991. * necessary.
  1992. */
  1993. static int slub_cpu_dead(unsigned int cpu)
  1994. {
  1995. struct kmem_cache *s;
  1996. unsigned long flags;
  1997. mutex_lock(&slab_mutex);
  1998. list_for_each_entry(s, &slab_caches, list) {
  1999. local_irq_save(flags);
  2000. __flush_cpu_slab(s, cpu);
  2001. local_irq_restore(flags);
  2002. }
  2003. mutex_unlock(&slab_mutex);
  2004. return 0;
  2005. }
  2006. /*
  2007. * Check if the objects in a per cpu structure fit numa
  2008. * locality expectations.
  2009. */
  2010. static inline int node_match(struct page *page, int node)
  2011. {
  2012. #ifdef CONFIG_NUMA
  2013. if (!page || (node != NUMA_NO_NODE && page_to_nid(page) != node))
  2014. return 0;
  2015. #endif
  2016. return 1;
  2017. }
  2018. #ifdef CONFIG_SLUB_DEBUG
  2019. static int count_free(struct page *page)
  2020. {
  2021. return page->objects - page->inuse;
  2022. }
  2023. static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
  2024. {
  2025. return atomic_long_read(&n->total_objects);
  2026. }
  2027. #endif /* CONFIG_SLUB_DEBUG */
  2028. #if defined(CONFIG_SLUB_DEBUG) || defined(CONFIG_SYSFS)
  2029. static unsigned long count_partial(struct kmem_cache_node *n,
  2030. int (*get_count)(struct page *))
  2031. {
  2032. unsigned long flags;
  2033. unsigned long x = 0;
  2034. struct page *page;
  2035. spin_lock_irqsave(&n->list_lock, flags);
  2036. list_for_each_entry(page, &n->partial, lru)
  2037. x += get_count(page);
  2038. spin_unlock_irqrestore(&n->list_lock, flags);
  2039. return x;
  2040. }
  2041. #endif /* CONFIG_SLUB_DEBUG || CONFIG_SYSFS */
  2042. static noinline void
  2043. slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
  2044. {
  2045. #ifdef CONFIG_SLUB_DEBUG
  2046. static DEFINE_RATELIMIT_STATE(slub_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
  2047. DEFAULT_RATELIMIT_BURST);
  2048. int node;
  2049. struct kmem_cache_node *n;
  2050. if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slub_oom_rs))
  2051. return;
  2052. pr_warn("SLUB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
  2053. nid, gfpflags, &gfpflags);
  2054. pr_warn(" cache: %s, object size: %u, buffer size: %u, default order: %u, min order: %u\n",
  2055. s->name, s->object_size, s->size, oo_order(s->oo),
  2056. oo_order(s->min));
  2057. if (oo_order(s->min) > get_order(s->object_size))
  2058. pr_warn(" %s debugging increased min order, use slub_debug=O to disable.\n",
  2059. s->name);
  2060. for_each_kmem_cache_node(s, node, n) {
  2061. unsigned long nr_slabs;
  2062. unsigned long nr_objs;
  2063. unsigned long nr_free;
  2064. nr_free = count_partial(n, count_free);
  2065. nr_slabs = node_nr_slabs(n);
  2066. nr_objs = node_nr_objs(n);
  2067. pr_warn(" node %d: slabs: %ld, objs: %ld, free: %ld\n",
  2068. node, nr_slabs, nr_objs, nr_free);
  2069. }
  2070. #endif
  2071. }
  2072. static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
  2073. int node, struct kmem_cache_cpu **pc)
  2074. {
  2075. void *freelist;
  2076. struct kmem_cache_cpu *c = *pc;
  2077. struct page *page;
  2078. WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
  2079. freelist = get_partial(s, flags, node, c);
  2080. if (freelist)
  2081. return freelist;
  2082. page = new_slab(s, flags, node);
  2083. if (page) {
  2084. c = raw_cpu_ptr(s->cpu_slab);
  2085. if (c->page)
  2086. flush_slab(s, c);
  2087. /*
  2088. * No other reference to the page yet so we can
  2089. * muck around with it freely without cmpxchg
  2090. */
  2091. freelist = page->freelist;
  2092. page->freelist = NULL;
  2093. stat(s, ALLOC_SLAB);
  2094. c->page = page;
  2095. *pc = c;
  2096. } else
  2097. freelist = NULL;
  2098. return freelist;
  2099. }
  2100. static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
  2101. {
  2102. if (unlikely(PageSlabPfmemalloc(page)))
  2103. return gfp_pfmemalloc_allowed(gfpflags);
  2104. return true;
  2105. }
  2106. /*
  2107. * Check the page->freelist of a page and either transfer the freelist to the
  2108. * per cpu freelist or deactivate the page.
  2109. *
  2110. * The page is still frozen if the return value is not NULL.
  2111. *
  2112. * If this function returns NULL then the page has been unfrozen.
  2113. *
  2114. * This function must be called with interrupt disabled.
  2115. */
  2116. static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  2117. {
  2118. struct page new;
  2119. unsigned long counters;
  2120. void *freelist;
  2121. do {
  2122. freelist = page->freelist;
  2123. counters = page->counters;
  2124. new.counters = counters;
  2125. VM_BUG_ON(!new.frozen);
  2126. new.inuse = page->objects;
  2127. new.frozen = freelist != NULL;
  2128. } while (!__cmpxchg_double_slab(s, page,
  2129. freelist, counters,
  2130. NULL, new.counters,
  2131. "get_freelist"));
  2132. return freelist;
  2133. }
  2134. /*
  2135. * Slow path. The lockless freelist is empty or we need to perform
  2136. * debugging duties.
  2137. *
  2138. * Processing is still very fast if new objects have been freed to the
  2139. * regular freelist. In that case we simply take over the regular freelist
  2140. * as the lockless freelist and zap the regular freelist.
  2141. *
  2142. * If that is not working then we fall back to the partial lists. We take the
  2143. * first element of the freelist as the object to allocate now and move the
  2144. * rest of the freelist to the lockless freelist.
  2145. *
  2146. * And if we were unable to get a new slab from the partial slab lists then
  2147. * we need to allocate a new slab. This is the slowest path since it involves
  2148. * a call to the page allocator and the setup of a new slab.
  2149. *
  2150. * Version of __slab_alloc to use when we know that interrupts are
  2151. * already disabled (which is the case for bulk allocation).
  2152. */
  2153. static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  2154. unsigned long addr, struct kmem_cache_cpu *c)
  2155. {
  2156. void *freelist;
  2157. struct page *page;
  2158. page = c->page;
  2159. if (!page)
  2160. goto new_slab;
  2161. redo:
  2162. if (unlikely(!node_match(page, node))) {
  2163. int searchnode = node;
  2164. if (node != NUMA_NO_NODE && !node_present_pages(node))
  2165. searchnode = node_to_mem_node(node);
  2166. if (unlikely(!node_match(page, searchnode))) {
  2167. stat(s, ALLOC_NODE_MISMATCH);
  2168. deactivate_slab(s, page, c->freelist, c);
  2169. goto new_slab;
  2170. }
  2171. }
  2172. /*
  2173. * By rights, we should be searching for a slab page that was
  2174. * PFMEMALLOC but right now, we are losing the pfmemalloc
  2175. * information when the page leaves the per-cpu allocator
  2176. */
  2177. if (unlikely(!pfmemalloc_match(page, gfpflags))) {
  2178. deactivate_slab(s, page, c->freelist, c);
  2179. goto new_slab;
  2180. }
  2181. /* must check again c->freelist in case of cpu migration or IRQ */
  2182. freelist = c->freelist;
  2183. if (freelist)
  2184. goto load_freelist;
  2185. freelist = get_freelist(s, page);
  2186. if (!freelist) {
  2187. c->page = NULL;
  2188. stat(s, DEACTIVATE_BYPASS);
  2189. goto new_slab;
  2190. }
  2191. stat(s, ALLOC_REFILL);
  2192. load_freelist:
  2193. /*
  2194. * freelist is pointing to the list of objects to be used.
  2195. * page is pointing to the page from which the objects are obtained.
  2196. * That page must be frozen for per cpu allocations to work.
  2197. */
  2198. VM_BUG_ON(!c->page->frozen);
  2199. c->freelist = get_freepointer(s, freelist);
  2200. c->tid = next_tid(c->tid);
  2201. return freelist;
  2202. new_slab:
  2203. if (slub_percpu_partial(c)) {
  2204. page = c->page = slub_percpu_partial(c);
  2205. slub_set_percpu_partial(c, page);
  2206. stat(s, CPU_PARTIAL_ALLOC);
  2207. goto redo;
  2208. }
  2209. freelist = new_slab_objects(s, gfpflags, node, &c);
  2210. if (unlikely(!freelist)) {
  2211. slab_out_of_memory(s, gfpflags, node);
  2212. return NULL;
  2213. }
  2214. page = c->page;
  2215. if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
  2216. goto load_freelist;
  2217. /* Only entered in the debug case */
  2218. if (kmem_cache_debug(s) &&
  2219. !alloc_debug_processing(s, page, freelist, addr))
  2220. goto new_slab; /* Slab failed checks. Next slab needed */
  2221. deactivate_slab(s, page, get_freepointer(s, freelist), c);
  2222. return freelist;
  2223. }
  2224. /*
  2225. * Another one that disabled interrupt and compensates for possible
  2226. * cpu changes by refetching the per cpu area pointer.
  2227. */
  2228. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  2229. unsigned long addr, struct kmem_cache_cpu *c)
  2230. {
  2231. void *p;
  2232. unsigned long flags;
  2233. local_irq_save(flags);
  2234. #ifdef CONFIG_PREEMPT
  2235. /*
  2236. * We may have been preempted and rescheduled on a different
  2237. * cpu before disabling interrupts. Need to reload cpu area
  2238. * pointer.
  2239. */
  2240. c = this_cpu_ptr(s->cpu_slab);
  2241. #endif
  2242. p = ___slab_alloc(s, gfpflags, node, addr, c);
  2243. local_irq_restore(flags);
  2244. return p;
  2245. }
  2246. /*
  2247. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  2248. * have the fastpath folded into their functions. So no function call
  2249. * overhead for requests that can be satisfied on the fastpath.
  2250. *
  2251. * The fastpath works by first checking if the lockless freelist can be used.
  2252. * If not then __slab_alloc is called for slow processing.
  2253. *
  2254. * Otherwise we can simply pick the next object from the lockless free list.
  2255. */
  2256. static __always_inline void *slab_alloc_node(struct kmem_cache *s,
  2257. gfp_t gfpflags, int node, unsigned long addr)
  2258. {
  2259. void *object;
  2260. struct kmem_cache_cpu *c;
  2261. struct page *page;
  2262. unsigned long tid;
  2263. s = slab_pre_alloc_hook(s, gfpflags);
  2264. if (!s)
  2265. return NULL;
  2266. redo:
  2267. /*
  2268. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  2269. * enabled. We may switch back and forth between cpus while
  2270. * reading from one cpu area. That does not matter as long
  2271. * as we end up on the original cpu again when doing the cmpxchg.
  2272. *
  2273. * We should guarantee that tid and kmem_cache are retrieved on
  2274. * the same cpu. It could be different if CONFIG_PREEMPT so we need
  2275. * to check if it is matched or not.
  2276. */
  2277. do {
  2278. tid = this_cpu_read(s->cpu_slab->tid);
  2279. c = raw_cpu_ptr(s->cpu_slab);
  2280. } while (IS_ENABLED(CONFIG_PREEMPT) &&
  2281. unlikely(tid != READ_ONCE(c->tid)));
  2282. /*
  2283. * Irqless object alloc/free algorithm used here depends on sequence
  2284. * of fetching cpu_slab's data. tid should be fetched before anything
  2285. * on c to guarantee that object and page associated with previous tid
  2286. * won't be used with current tid. If we fetch tid first, object and
  2287. * page could be one associated with next tid and our alloc/free
  2288. * request will be failed. In this case, we will retry. So, no problem.
  2289. */
  2290. barrier();
  2291. /*
  2292. * The transaction ids are globally unique per cpu and per operation on
  2293. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  2294. * occurs on the right processor and that there was no operation on the
  2295. * linked list in between.
  2296. */
  2297. object = c->freelist;
  2298. page = c->page;
  2299. if (unlikely(!object || !node_match(page, node))) {
  2300. object = __slab_alloc(s, gfpflags, node, addr, c);
  2301. stat(s, ALLOC_SLOWPATH);
  2302. } else {
  2303. void *next_object = get_freepointer_safe(s, object);
  2304. /*
  2305. * The cmpxchg will only match if there was no additional
  2306. * operation and if we are on the right processor.
  2307. *
  2308. * The cmpxchg does the following atomically (without lock
  2309. * semantics!)
  2310. * 1. Relocate first pointer to the current per cpu area.
  2311. * 2. Verify that tid and freelist have not been changed
  2312. * 3. If they were not changed replace tid and freelist
  2313. *
  2314. * Since this is without lock semantics the protection is only
  2315. * against code executing on this cpu *not* from access by
  2316. * other cpus.
  2317. */
  2318. if (unlikely(!this_cpu_cmpxchg_double(
  2319. s->cpu_slab->freelist, s->cpu_slab->tid,
  2320. object, tid,
  2321. next_object, next_tid(tid)))) {
  2322. note_cmpxchg_failure("slab_alloc", s, tid);
  2323. goto redo;
  2324. }
  2325. prefetch_freepointer(s, next_object);
  2326. stat(s, ALLOC_FASTPATH);
  2327. }
  2328. if (unlikely(gfpflags & __GFP_ZERO) && object)
  2329. memset(object, 0, s->object_size);
  2330. slab_post_alloc_hook(s, gfpflags, 1, &object);
  2331. return object;
  2332. }
  2333. static __always_inline void *slab_alloc(struct kmem_cache *s,
  2334. gfp_t gfpflags, unsigned long addr)
  2335. {
  2336. return slab_alloc_node(s, gfpflags, NUMA_NO_NODE, addr);
  2337. }
  2338. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  2339. {
  2340. void *ret = slab_alloc(s, gfpflags, _RET_IP_);
  2341. trace_kmem_cache_alloc(_RET_IP_, ret, s->object_size,
  2342. s->size, gfpflags);
  2343. return ret;
  2344. }
  2345. EXPORT_SYMBOL(kmem_cache_alloc);
  2346. #ifdef CONFIG_TRACING
  2347. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  2348. {
  2349. void *ret = slab_alloc(s, gfpflags, _RET_IP_);
  2350. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  2351. kasan_kmalloc(s, ret, size, gfpflags);
  2352. return ret;
  2353. }
  2354. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  2355. #endif
  2356. #ifdef CONFIG_NUMA
  2357. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  2358. {
  2359. void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
  2360. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  2361. s->object_size, s->size, gfpflags, node);
  2362. return ret;
  2363. }
  2364. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2365. #ifdef CONFIG_TRACING
  2366. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  2367. gfp_t gfpflags,
  2368. int node, size_t size)
  2369. {
  2370. void *ret = slab_alloc_node(s, gfpflags, node, _RET_IP_);
  2371. trace_kmalloc_node(_RET_IP_, ret,
  2372. size, s->size, gfpflags, node);
  2373. kasan_kmalloc(s, ret, size, gfpflags);
  2374. return ret;
  2375. }
  2376. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  2377. #endif
  2378. #endif
  2379. /*
  2380. * Slow path handling. This may still be called frequently since objects
  2381. * have a longer lifetime than the cpu slabs in most processing loads.
  2382. *
  2383. * So we still attempt to reduce cache line usage. Just take the slab
  2384. * lock and free the item. If there is no additional partial page
  2385. * handling required then we can return immediately.
  2386. */
  2387. static void __slab_free(struct kmem_cache *s, struct page *page,
  2388. void *head, void *tail, int cnt,
  2389. unsigned long addr)
  2390. {
  2391. void *prior;
  2392. int was_frozen;
  2393. struct page new;
  2394. unsigned long counters;
  2395. struct kmem_cache_node *n = NULL;
  2396. unsigned long uninitialized_var(flags);
  2397. stat(s, FREE_SLOWPATH);
  2398. if (kmem_cache_debug(s) &&
  2399. !free_debug_processing(s, page, head, tail, cnt, addr))
  2400. return;
  2401. do {
  2402. if (unlikely(n)) {
  2403. spin_unlock_irqrestore(&n->list_lock, flags);
  2404. n = NULL;
  2405. }
  2406. prior = page->freelist;
  2407. counters = page->counters;
  2408. set_freepointer(s, tail, prior);
  2409. new.counters = counters;
  2410. was_frozen = new.frozen;
  2411. new.inuse -= cnt;
  2412. if ((!new.inuse || !prior) && !was_frozen) {
  2413. if (kmem_cache_has_cpu_partial(s) && !prior) {
  2414. /*
  2415. * Slab was on no list before and will be
  2416. * partially empty
  2417. * We can defer the list move and instead
  2418. * freeze it.
  2419. */
  2420. new.frozen = 1;
  2421. } else { /* Needs to be taken off a list */
  2422. n = get_node(s, page_to_nid(page));
  2423. /*
  2424. * Speculatively acquire the list_lock.
  2425. * If the cmpxchg does not succeed then we may
  2426. * drop the list_lock without any processing.
  2427. *
  2428. * Otherwise the list_lock will synchronize with
  2429. * other processors updating the list of slabs.
  2430. */
  2431. spin_lock_irqsave(&n->list_lock, flags);
  2432. }
  2433. }
  2434. } while (!cmpxchg_double_slab(s, page,
  2435. prior, counters,
  2436. head, new.counters,
  2437. "__slab_free"));
  2438. if (likely(!n)) {
  2439. /*
  2440. * If we just froze the page then put it onto the
  2441. * per cpu partial list.
  2442. */
  2443. if (new.frozen && !was_frozen) {
  2444. put_cpu_partial(s, page, 1);
  2445. stat(s, CPU_PARTIAL_FREE);
  2446. }
  2447. /*
  2448. * The list lock was not taken therefore no list
  2449. * activity can be necessary.
  2450. */
  2451. if (was_frozen)
  2452. stat(s, FREE_FROZEN);
  2453. return;
  2454. }
  2455. if (unlikely(!new.inuse && n->nr_partial >= s->min_partial))
  2456. goto slab_empty;
  2457. /*
  2458. * Objects left in the slab. If it was not on the partial list before
  2459. * then add it.
  2460. */
  2461. if (!kmem_cache_has_cpu_partial(s) && unlikely(!prior)) {
  2462. if (kmem_cache_debug(s))
  2463. remove_full(s, n, page);
  2464. add_partial(n, page, DEACTIVATE_TO_TAIL);
  2465. stat(s, FREE_ADD_PARTIAL);
  2466. }
  2467. spin_unlock_irqrestore(&n->list_lock, flags);
  2468. return;
  2469. slab_empty:
  2470. if (prior) {
  2471. /*
  2472. * Slab on the partial list.
  2473. */
  2474. remove_partial(n, page);
  2475. stat(s, FREE_REMOVE_PARTIAL);
  2476. } else {
  2477. /* Slab must be on the full list */
  2478. remove_full(s, n, page);
  2479. }
  2480. spin_unlock_irqrestore(&n->list_lock, flags);
  2481. stat(s, FREE_SLAB);
  2482. discard_slab(s, page);
  2483. }
  2484. /*
  2485. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  2486. * can perform fastpath freeing without additional function calls.
  2487. *
  2488. * The fastpath is only possible if we are freeing to the current cpu slab
  2489. * of this processor. This typically the case if we have just allocated
  2490. * the item before.
  2491. *
  2492. * If fastpath is not possible then fall back to __slab_free where we deal
  2493. * with all sorts of special processing.
  2494. *
  2495. * Bulk free of a freelist with several objects (all pointing to the
  2496. * same page) possible by specifying head and tail ptr, plus objects
  2497. * count (cnt). Bulk free indicated by tail pointer being set.
  2498. */
  2499. static __always_inline void do_slab_free(struct kmem_cache *s,
  2500. struct page *page, void *head, void *tail,
  2501. int cnt, unsigned long addr)
  2502. {
  2503. void *tail_obj = tail ? : head;
  2504. struct kmem_cache_cpu *c;
  2505. unsigned long tid;
  2506. redo:
  2507. /*
  2508. * Determine the currently cpus per cpu slab.
  2509. * The cpu may change afterward. However that does not matter since
  2510. * data is retrieved via this pointer. If we are on the same cpu
  2511. * during the cmpxchg then the free will succeed.
  2512. */
  2513. do {
  2514. tid = this_cpu_read(s->cpu_slab->tid);
  2515. c = raw_cpu_ptr(s->cpu_slab);
  2516. } while (IS_ENABLED(CONFIG_PREEMPT) &&
  2517. unlikely(tid != READ_ONCE(c->tid)));
  2518. /* Same with comment on barrier() in slab_alloc_node() */
  2519. barrier();
  2520. if (likely(page == c->page)) {
  2521. set_freepointer(s, tail_obj, c->freelist);
  2522. if (unlikely(!this_cpu_cmpxchg_double(
  2523. s->cpu_slab->freelist, s->cpu_slab->tid,
  2524. c->freelist, tid,
  2525. head, next_tid(tid)))) {
  2526. note_cmpxchg_failure("slab_free", s, tid);
  2527. goto redo;
  2528. }
  2529. stat(s, FREE_FASTPATH);
  2530. } else
  2531. __slab_free(s, page, head, tail_obj, cnt, addr);
  2532. }
  2533. static __always_inline void slab_free(struct kmem_cache *s, struct page *page,
  2534. void *head, void *tail, int cnt,
  2535. unsigned long addr)
  2536. {
  2537. /*
  2538. * With KASAN enabled slab_free_freelist_hook modifies the freelist
  2539. * to remove objects, whose reuse must be delayed.
  2540. */
  2541. if (slab_free_freelist_hook(s, &head, &tail))
  2542. do_slab_free(s, page, head, tail, cnt, addr);
  2543. }
  2544. #ifdef CONFIG_KASAN
  2545. void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr)
  2546. {
  2547. do_slab_free(cache, virt_to_head_page(x), x, NULL, 1, addr);
  2548. }
  2549. #endif
  2550. void kmem_cache_free(struct kmem_cache *s, void *x)
  2551. {
  2552. s = cache_from_obj(s, x);
  2553. if (!s)
  2554. return;
  2555. slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
  2556. trace_kmem_cache_free(_RET_IP_, x);
  2557. }
  2558. EXPORT_SYMBOL(kmem_cache_free);
  2559. struct detached_freelist {
  2560. struct page *page;
  2561. void *tail;
  2562. void *freelist;
  2563. int cnt;
  2564. struct kmem_cache *s;
  2565. };
  2566. /*
  2567. * This function progressively scans the array with free objects (with
  2568. * a limited look ahead) and extract objects belonging to the same
  2569. * page. It builds a detached freelist directly within the given
  2570. * page/objects. This can happen without any need for
  2571. * synchronization, because the objects are owned by running process.
  2572. * The freelist is build up as a single linked list in the objects.
  2573. * The idea is, that this detached freelist can then be bulk
  2574. * transferred to the real freelist(s), but only requiring a single
  2575. * synchronization primitive. Look ahead in the array is limited due
  2576. * to performance reasons.
  2577. */
  2578. static inline
  2579. int build_detached_freelist(struct kmem_cache *s, size_t size,
  2580. void **p, struct detached_freelist *df)
  2581. {
  2582. size_t first_skipped_index = 0;
  2583. int lookahead = 3;
  2584. void *object;
  2585. struct page *page;
  2586. /* Always re-init detached_freelist */
  2587. df->page = NULL;
  2588. do {
  2589. object = p[--size];
  2590. /* Do we need !ZERO_OR_NULL_PTR(object) here? (for kfree) */
  2591. } while (!object && size);
  2592. if (!object)
  2593. return 0;
  2594. page = virt_to_head_page(object);
  2595. if (!s) {
  2596. /* Handle kalloc'ed objects */
  2597. if (unlikely(!PageSlab(page))) {
  2598. BUG_ON(!PageCompound(page));
  2599. kfree_hook(object);
  2600. __free_pages(page, compound_order(page));
  2601. p[size] = NULL; /* mark object processed */
  2602. return size;
  2603. }
  2604. /* Derive kmem_cache from object */
  2605. df->s = page->slab_cache;
  2606. } else {
  2607. df->s = cache_from_obj(s, object); /* Support for memcg */
  2608. }
  2609. /* Start new detached freelist */
  2610. df->page = page;
  2611. set_freepointer(df->s, object, NULL);
  2612. df->tail = object;
  2613. df->freelist = object;
  2614. p[size] = NULL; /* mark object processed */
  2615. df->cnt = 1;
  2616. while (size) {
  2617. object = p[--size];
  2618. if (!object)
  2619. continue; /* Skip processed objects */
  2620. /* df->page is always set at this point */
  2621. if (df->page == virt_to_head_page(object)) {
  2622. /* Opportunity build freelist */
  2623. set_freepointer(df->s, object, df->freelist);
  2624. df->freelist = object;
  2625. df->cnt++;
  2626. p[size] = NULL; /* mark object processed */
  2627. continue;
  2628. }
  2629. /* Limit look ahead search */
  2630. if (!--lookahead)
  2631. break;
  2632. if (!first_skipped_index)
  2633. first_skipped_index = size + 1;
  2634. }
  2635. return first_skipped_index;
  2636. }
  2637. /* Note that interrupts must be enabled when calling this function. */
  2638. void kmem_cache_free_bulk(struct kmem_cache *s, size_t size, void **p)
  2639. {
  2640. if (WARN_ON(!size))
  2641. return;
  2642. do {
  2643. struct detached_freelist df;
  2644. size = build_detached_freelist(s, size, p, &df);
  2645. if (!df.page)
  2646. continue;
  2647. slab_free(df.s, df.page, df.freelist, df.tail, df.cnt,_RET_IP_);
  2648. } while (likely(size));
  2649. }
  2650. EXPORT_SYMBOL(kmem_cache_free_bulk);
  2651. /* Note that interrupts must be enabled when calling this function. */
  2652. int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  2653. void **p)
  2654. {
  2655. struct kmem_cache_cpu *c;
  2656. int i;
  2657. /* memcg and kmem_cache debug support */
  2658. s = slab_pre_alloc_hook(s, flags);
  2659. if (unlikely(!s))
  2660. return false;
  2661. /*
  2662. * Drain objects in the per cpu slab, while disabling local
  2663. * IRQs, which protects against PREEMPT and interrupts
  2664. * handlers invoking normal fastpath.
  2665. */
  2666. local_irq_disable();
  2667. c = this_cpu_ptr(s->cpu_slab);
  2668. for (i = 0; i < size; i++) {
  2669. void *object = c->freelist;
  2670. if (unlikely(!object)) {
  2671. /*
  2672. * Invoking slow path likely have side-effect
  2673. * of re-populating per CPU c->freelist
  2674. */
  2675. p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
  2676. _RET_IP_, c);
  2677. if (unlikely(!p[i]))
  2678. goto error;
  2679. c = this_cpu_ptr(s->cpu_slab);
  2680. continue; /* goto for-loop */
  2681. }
  2682. c->freelist = get_freepointer(s, object);
  2683. p[i] = object;
  2684. }
  2685. c->tid = next_tid(c->tid);
  2686. local_irq_enable();
  2687. /* Clear memory outside IRQ disabled fastpath loop */
  2688. if (unlikely(flags & __GFP_ZERO)) {
  2689. int j;
  2690. for (j = 0; j < i; j++)
  2691. memset(p[j], 0, s->object_size);
  2692. }
  2693. /* memcg and kmem_cache debug support */
  2694. slab_post_alloc_hook(s, flags, size, p);
  2695. return i;
  2696. error:
  2697. local_irq_enable();
  2698. slab_post_alloc_hook(s, flags, i, p);
  2699. __kmem_cache_free_bulk(s, i, p);
  2700. return 0;
  2701. }
  2702. EXPORT_SYMBOL(kmem_cache_alloc_bulk);
  2703. /*
  2704. * Object placement in a slab is made very easy because we always start at
  2705. * offset 0. If we tune the size of the object to the alignment then we can
  2706. * get the required alignment by putting one properly sized object after
  2707. * another.
  2708. *
  2709. * Notice that the allocation order determines the sizes of the per cpu
  2710. * caches. Each processor has always one slab available for allocations.
  2711. * Increasing the allocation order reduces the number of times that slabs
  2712. * must be moved on and off the partial lists and is therefore a factor in
  2713. * locking overhead.
  2714. */
  2715. /*
  2716. * Mininum / Maximum order of slab pages. This influences locking overhead
  2717. * and slab fragmentation. A higher order reduces the number of partial slabs
  2718. * and increases the number of allocations possible without having to
  2719. * take the list_lock.
  2720. */
  2721. static unsigned int slub_min_order;
  2722. static unsigned int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  2723. static unsigned int slub_min_objects;
  2724. /*
  2725. * Calculate the order of allocation given an slab object size.
  2726. *
  2727. * The order of allocation has significant impact on performance and other
  2728. * system components. Generally order 0 allocations should be preferred since
  2729. * order 0 does not cause fragmentation in the page allocator. Larger objects
  2730. * be problematic to put into order 0 slabs because there may be too much
  2731. * unused space left. We go to a higher order if more than 1/16th of the slab
  2732. * would be wasted.
  2733. *
  2734. * In order to reach satisfactory performance we must ensure that a minimum
  2735. * number of objects is in one slab. Otherwise we may generate too much
  2736. * activity on the partial lists which requires taking the list_lock. This is
  2737. * less a concern for large slabs though which are rarely used.
  2738. *
  2739. * slub_max_order specifies the order where we begin to stop considering the
  2740. * number of objects in a slab as critical. If we reach slub_max_order then
  2741. * we try to keep the page order as low as possible. So we accept more waste
  2742. * of space in favor of a small page order.
  2743. *
  2744. * Higher order allocations also allow the placement of more objects in a
  2745. * slab and thereby reduce object handling overhead. If the user has
  2746. * requested a higher mininum order then we start with that one instead of
  2747. * the smallest order which will fit the object.
  2748. */
  2749. static inline unsigned int slab_order(unsigned int size,
  2750. unsigned int min_objects, unsigned int max_order,
  2751. unsigned int fract_leftover)
  2752. {
  2753. unsigned int min_order = slub_min_order;
  2754. unsigned int order;
  2755. if (order_objects(min_order, size) > MAX_OBJS_PER_PAGE)
  2756. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  2757. for (order = max(min_order, (unsigned int)get_order(min_objects * size));
  2758. order <= max_order; order++) {
  2759. unsigned int slab_size = (unsigned int)PAGE_SIZE << order;
  2760. unsigned int rem;
  2761. rem = slab_size % size;
  2762. if (rem <= slab_size / fract_leftover)
  2763. break;
  2764. }
  2765. return order;
  2766. }
  2767. static inline int calculate_order(unsigned int size)
  2768. {
  2769. unsigned int order;
  2770. unsigned int min_objects;
  2771. unsigned int max_objects;
  2772. /*
  2773. * Attempt to find best configuration for a slab. This
  2774. * works by first attempting to generate a layout with
  2775. * the best configuration and backing off gradually.
  2776. *
  2777. * First we increase the acceptable waste in a slab. Then
  2778. * we reduce the minimum objects required in a slab.
  2779. */
  2780. min_objects = slub_min_objects;
  2781. if (!min_objects)
  2782. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  2783. max_objects = order_objects(slub_max_order, size);
  2784. min_objects = min(min_objects, max_objects);
  2785. while (min_objects > 1) {
  2786. unsigned int fraction;
  2787. fraction = 16;
  2788. while (fraction >= 4) {
  2789. order = slab_order(size, min_objects,
  2790. slub_max_order, fraction);
  2791. if (order <= slub_max_order)
  2792. return order;
  2793. fraction /= 2;
  2794. }
  2795. min_objects--;
  2796. }
  2797. /*
  2798. * We were unable to place multiple objects in a slab. Now
  2799. * lets see if we can place a single object there.
  2800. */
  2801. order = slab_order(size, 1, slub_max_order, 1);
  2802. if (order <= slub_max_order)
  2803. return order;
  2804. /*
  2805. * Doh this slab cannot be placed using slub_max_order.
  2806. */
  2807. order = slab_order(size, 1, MAX_ORDER, 1);
  2808. if (order < MAX_ORDER)
  2809. return order;
  2810. return -ENOSYS;
  2811. }
  2812. static void
  2813. init_kmem_cache_node(struct kmem_cache_node *n)
  2814. {
  2815. n->nr_partial = 0;
  2816. spin_lock_init(&n->list_lock);
  2817. INIT_LIST_HEAD(&n->partial);
  2818. #ifdef CONFIG_SLUB_DEBUG
  2819. atomic_long_set(&n->nr_slabs, 0);
  2820. atomic_long_set(&n->total_objects, 0);
  2821. INIT_LIST_HEAD(&n->full);
  2822. #endif
  2823. }
  2824. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2825. {
  2826. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2827. KMALLOC_SHIFT_HIGH * sizeof(struct kmem_cache_cpu));
  2828. /*
  2829. * Must align to double word boundary for the double cmpxchg
  2830. * instructions to work; see __pcpu_double_call_return_bool().
  2831. */
  2832. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  2833. 2 * sizeof(void *));
  2834. if (!s->cpu_slab)
  2835. return 0;
  2836. init_kmem_cache_cpus(s);
  2837. return 1;
  2838. }
  2839. static struct kmem_cache *kmem_cache_node;
  2840. /*
  2841. * No kmalloc_node yet so do it by hand. We know that this is the first
  2842. * slab on the node for this slabcache. There are no concurrent accesses
  2843. * possible.
  2844. *
  2845. * Note that this function only works on the kmem_cache_node
  2846. * when allocating for the kmem_cache_node. This is used for bootstrapping
  2847. * memory on a fresh node that has no slab structures yet.
  2848. */
  2849. static void early_kmem_cache_node_alloc(int node)
  2850. {
  2851. struct page *page;
  2852. struct kmem_cache_node *n;
  2853. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2854. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2855. BUG_ON(!page);
  2856. if (page_to_nid(page) != node) {
  2857. pr_err("SLUB: Unable to allocate memory from node %d\n", node);
  2858. pr_err("SLUB: Allocating a useless per node structure in order to be able to continue\n");
  2859. }
  2860. n = page->freelist;
  2861. BUG_ON(!n);
  2862. page->freelist = get_freepointer(kmem_cache_node, n);
  2863. page->inuse = 1;
  2864. page->frozen = 0;
  2865. kmem_cache_node->node[node] = n;
  2866. #ifdef CONFIG_SLUB_DEBUG
  2867. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2868. init_tracking(kmem_cache_node, n);
  2869. #endif
  2870. kasan_kmalloc(kmem_cache_node, n, sizeof(struct kmem_cache_node),
  2871. GFP_KERNEL);
  2872. init_kmem_cache_node(n);
  2873. inc_slabs_node(kmem_cache_node, node, page->objects);
  2874. /*
  2875. * No locks need to be taken here as it has just been
  2876. * initialized and there is no concurrent access.
  2877. */
  2878. __add_partial(n, page, DEACTIVATE_TO_HEAD);
  2879. }
  2880. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2881. {
  2882. int node;
  2883. struct kmem_cache_node *n;
  2884. for_each_kmem_cache_node(s, node, n) {
  2885. s->node[node] = NULL;
  2886. kmem_cache_free(kmem_cache_node, n);
  2887. }
  2888. }
  2889. void __kmem_cache_release(struct kmem_cache *s)
  2890. {
  2891. cache_random_seq_destroy(s);
  2892. free_percpu(s->cpu_slab);
  2893. free_kmem_cache_nodes(s);
  2894. }
  2895. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2896. {
  2897. int node;
  2898. for_each_node_state(node, N_NORMAL_MEMORY) {
  2899. struct kmem_cache_node *n;
  2900. if (slab_state == DOWN) {
  2901. early_kmem_cache_node_alloc(node);
  2902. continue;
  2903. }
  2904. n = kmem_cache_alloc_node(kmem_cache_node,
  2905. GFP_KERNEL, node);
  2906. if (!n) {
  2907. free_kmem_cache_nodes(s);
  2908. return 0;
  2909. }
  2910. init_kmem_cache_node(n);
  2911. s->node[node] = n;
  2912. }
  2913. return 1;
  2914. }
  2915. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2916. {
  2917. if (min < MIN_PARTIAL)
  2918. min = MIN_PARTIAL;
  2919. else if (min > MAX_PARTIAL)
  2920. min = MAX_PARTIAL;
  2921. s->min_partial = min;
  2922. }
  2923. static void set_cpu_partial(struct kmem_cache *s)
  2924. {
  2925. #ifdef CONFIG_SLUB_CPU_PARTIAL
  2926. /*
  2927. * cpu_partial determined the maximum number of objects kept in the
  2928. * per cpu partial lists of a processor.
  2929. *
  2930. * Per cpu partial lists mainly contain slabs that just have one
  2931. * object freed. If they are used for allocation then they can be
  2932. * filled up again with minimal effort. The slab will never hit the
  2933. * per node partial lists and therefore no locking will be required.
  2934. *
  2935. * This setting also determines
  2936. *
  2937. * A) The number of objects from per cpu partial slabs dumped to the
  2938. * per node list when we reach the limit.
  2939. * B) The number of objects in cpu partial slabs to extract from the
  2940. * per node list when we run out of per cpu objects. We only fetch
  2941. * 50% to keep some capacity around for frees.
  2942. */
  2943. if (!kmem_cache_has_cpu_partial(s))
  2944. s->cpu_partial = 0;
  2945. else if (s->size >= PAGE_SIZE)
  2946. s->cpu_partial = 2;
  2947. else if (s->size >= 1024)
  2948. s->cpu_partial = 6;
  2949. else if (s->size >= 256)
  2950. s->cpu_partial = 13;
  2951. else
  2952. s->cpu_partial = 30;
  2953. #endif
  2954. }
  2955. /*
  2956. * calculate_sizes() determines the order and the distribution of data within
  2957. * a slab object.
  2958. */
  2959. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2960. {
  2961. slab_flags_t flags = s->flags;
  2962. unsigned int size = s->object_size;
  2963. unsigned int order;
  2964. /*
  2965. * Round up object size to the next word boundary. We can only
  2966. * place the free pointer at word boundaries and this determines
  2967. * the possible location of the free pointer.
  2968. */
  2969. size = ALIGN(size, sizeof(void *));
  2970. #ifdef CONFIG_SLUB_DEBUG
  2971. /*
  2972. * Determine if we can poison the object itself. If the user of
  2973. * the slab may touch the object after free or before allocation
  2974. * then we should never poison the object itself.
  2975. */
  2976. if ((flags & SLAB_POISON) && !(flags & SLAB_TYPESAFE_BY_RCU) &&
  2977. !s->ctor)
  2978. s->flags |= __OBJECT_POISON;
  2979. else
  2980. s->flags &= ~__OBJECT_POISON;
  2981. /*
  2982. * If we are Redzoning then check if there is some space between the
  2983. * end of the object and the free pointer. If not then add an
  2984. * additional word to have some bytes to store Redzone information.
  2985. */
  2986. if ((flags & SLAB_RED_ZONE) && size == s->object_size)
  2987. size += sizeof(void *);
  2988. #endif
  2989. /*
  2990. * With that we have determined the number of bytes in actual use
  2991. * by the object. This is the potential offset to the free pointer.
  2992. */
  2993. s->inuse = size;
  2994. if (((flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
  2995. s->ctor)) {
  2996. /*
  2997. * Relocate free pointer after the object if it is not
  2998. * permitted to overwrite the first word of the object on
  2999. * kmem_cache_free.
  3000. *
  3001. * This is the case if we do RCU, have a constructor or
  3002. * destructor or are poisoning the objects.
  3003. */
  3004. s->offset = size;
  3005. size += sizeof(void *);
  3006. }
  3007. #ifdef CONFIG_SLUB_DEBUG
  3008. if (flags & SLAB_STORE_USER)
  3009. /*
  3010. * Need to store information about allocs and frees after
  3011. * the object.
  3012. */
  3013. size += 2 * sizeof(struct track);
  3014. #endif
  3015. kasan_cache_create(s, &size, &s->flags);
  3016. #ifdef CONFIG_SLUB_DEBUG
  3017. if (flags & SLAB_RED_ZONE) {
  3018. /*
  3019. * Add some empty padding so that we can catch
  3020. * overwrites from earlier objects rather than let
  3021. * tracking information or the free pointer be
  3022. * corrupted if a user writes before the start
  3023. * of the object.
  3024. */
  3025. size += sizeof(void *);
  3026. s->red_left_pad = sizeof(void *);
  3027. s->red_left_pad = ALIGN(s->red_left_pad, s->align);
  3028. size += s->red_left_pad;
  3029. }
  3030. #endif
  3031. /*
  3032. * SLUB stores one object immediately after another beginning from
  3033. * offset 0. In order to align the objects we have to simply size
  3034. * each object to conform to the alignment.
  3035. */
  3036. size = ALIGN(size, s->align);
  3037. s->size = size;
  3038. if (forced_order >= 0)
  3039. order = forced_order;
  3040. else
  3041. order = calculate_order(size);
  3042. if ((int)order < 0)
  3043. return 0;
  3044. s->allocflags = 0;
  3045. if (order)
  3046. s->allocflags |= __GFP_COMP;
  3047. if (s->flags & SLAB_CACHE_DMA)
  3048. s->allocflags |= GFP_DMA;
  3049. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3050. s->allocflags |= __GFP_RECLAIMABLE;
  3051. /*
  3052. * Determine the number of objects per slab
  3053. */
  3054. s->oo = oo_make(order, size);
  3055. s->min = oo_make(get_order(size), size);
  3056. if (oo_objects(s->oo) > oo_objects(s->max))
  3057. s->max = s->oo;
  3058. return !!oo_objects(s->oo);
  3059. }
  3060. static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags)
  3061. {
  3062. s->flags = kmem_cache_flags(s->size, flags, s->name, s->ctor);
  3063. #ifdef CONFIG_SLAB_FREELIST_HARDENED
  3064. s->random = get_random_long();
  3065. #endif
  3066. if (!calculate_sizes(s, -1))
  3067. goto error;
  3068. if (disable_higher_order_debug) {
  3069. /*
  3070. * Disable debugging flags that store metadata if the min slab
  3071. * order increased.
  3072. */
  3073. if (get_order(s->size) > get_order(s->object_size)) {
  3074. s->flags &= ~DEBUG_METADATA_FLAGS;
  3075. s->offset = 0;
  3076. if (!calculate_sizes(s, -1))
  3077. goto error;
  3078. }
  3079. }
  3080. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
  3081. defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  3082. if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0)
  3083. /* Enable fast mode */
  3084. s->flags |= __CMPXCHG_DOUBLE;
  3085. #endif
  3086. /*
  3087. * The larger the object size is, the more pages we want on the partial
  3088. * list to avoid pounding the page allocator excessively.
  3089. */
  3090. set_min_partial(s, ilog2(s->size) / 2);
  3091. set_cpu_partial(s);
  3092. #ifdef CONFIG_NUMA
  3093. s->remote_node_defrag_ratio = 1000;
  3094. #endif
  3095. /* Initialize the pre-computed randomized freelist if slab is up */
  3096. if (slab_state >= UP) {
  3097. if (init_cache_random_seq(s))
  3098. goto error;
  3099. }
  3100. if (!init_kmem_cache_nodes(s))
  3101. goto error;
  3102. if (alloc_kmem_cache_cpus(s))
  3103. return 0;
  3104. free_kmem_cache_nodes(s);
  3105. error:
  3106. if (flags & SLAB_PANIC)
  3107. panic("Cannot create slab %s size=%u realsize=%u order=%u offset=%u flags=%lx\n",
  3108. s->name, s->size, s->size,
  3109. oo_order(s->oo), s->offset, (unsigned long)flags);
  3110. return -EINVAL;
  3111. }
  3112. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  3113. const char *text)
  3114. {
  3115. #ifdef CONFIG_SLUB_DEBUG
  3116. void *addr = page_address(page);
  3117. void *p;
  3118. unsigned long *map = bitmap_zalloc(page->objects, GFP_ATOMIC);
  3119. if (!map)
  3120. return;
  3121. slab_err(s, page, text, s->name);
  3122. slab_lock(page);
  3123. get_map(s, page, map);
  3124. for_each_object(p, s, addr, page->objects) {
  3125. if (!test_bit(slab_index(p, s, addr), map)) {
  3126. pr_err("INFO: Object 0x%p @offset=%tu\n", p, p - addr);
  3127. print_tracking(s, p);
  3128. }
  3129. }
  3130. slab_unlock(page);
  3131. bitmap_free(map);
  3132. #endif
  3133. }
  3134. /*
  3135. * Attempt to free all partial slabs on a node.
  3136. * This is called from __kmem_cache_shutdown(). We must take list_lock
  3137. * because sysfs file might still access partial list after the shutdowning.
  3138. */
  3139. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  3140. {
  3141. LIST_HEAD(discard);
  3142. struct page *page, *h;
  3143. BUG_ON(irqs_disabled());
  3144. spin_lock_irq(&n->list_lock);
  3145. list_for_each_entry_safe(page, h, &n->partial, lru) {
  3146. if (!page->inuse) {
  3147. remove_partial(n, page);
  3148. list_add(&page->lru, &discard);
  3149. } else {
  3150. list_slab_objects(s, page,
  3151. "Objects remaining in %s on __kmem_cache_shutdown()");
  3152. }
  3153. }
  3154. spin_unlock_irq(&n->list_lock);
  3155. list_for_each_entry_safe(page, h, &discard, lru)
  3156. discard_slab(s, page);
  3157. }
  3158. bool __kmem_cache_empty(struct kmem_cache *s)
  3159. {
  3160. int node;
  3161. struct kmem_cache_node *n;
  3162. for_each_kmem_cache_node(s, node, n)
  3163. if (n->nr_partial || slabs_node(s, node))
  3164. return false;
  3165. return true;
  3166. }
  3167. /*
  3168. * Release all resources used by a slab cache.
  3169. */
  3170. int __kmem_cache_shutdown(struct kmem_cache *s)
  3171. {
  3172. int node;
  3173. struct kmem_cache_node *n;
  3174. flush_all(s);
  3175. /* Attempt to free all objects */
  3176. for_each_kmem_cache_node(s, node, n) {
  3177. free_partial(s, n);
  3178. if (n->nr_partial || slabs_node(s, node))
  3179. return 1;
  3180. }
  3181. sysfs_slab_remove(s);
  3182. return 0;
  3183. }
  3184. /********************************************************************
  3185. * Kmalloc subsystem
  3186. *******************************************************************/
  3187. static int __init setup_slub_min_order(char *str)
  3188. {
  3189. get_option(&str, (int *)&slub_min_order);
  3190. return 1;
  3191. }
  3192. __setup("slub_min_order=", setup_slub_min_order);
  3193. static int __init setup_slub_max_order(char *str)
  3194. {
  3195. get_option(&str, (int *)&slub_max_order);
  3196. slub_max_order = min(slub_max_order, (unsigned int)MAX_ORDER - 1);
  3197. return 1;
  3198. }
  3199. __setup("slub_max_order=", setup_slub_max_order);
  3200. static int __init setup_slub_min_objects(char *str)
  3201. {
  3202. get_option(&str, (int *)&slub_min_objects);
  3203. return 1;
  3204. }
  3205. __setup("slub_min_objects=", setup_slub_min_objects);
  3206. void *__kmalloc(size_t size, gfp_t flags)
  3207. {
  3208. struct kmem_cache *s;
  3209. void *ret;
  3210. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
  3211. return kmalloc_large(size, flags);
  3212. s = kmalloc_slab(size, flags);
  3213. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3214. return s;
  3215. ret = slab_alloc(s, flags, _RET_IP_);
  3216. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  3217. kasan_kmalloc(s, ret, size, flags);
  3218. return ret;
  3219. }
  3220. EXPORT_SYMBOL(__kmalloc);
  3221. #ifdef CONFIG_NUMA
  3222. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  3223. {
  3224. struct page *page;
  3225. void *ptr = NULL;
  3226. flags |= __GFP_COMP;
  3227. page = alloc_pages_node(node, flags, get_order(size));
  3228. if (page)
  3229. ptr = page_address(page);
  3230. kmalloc_large_node_hook(ptr, size, flags);
  3231. return ptr;
  3232. }
  3233. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3234. {
  3235. struct kmem_cache *s;
  3236. void *ret;
  3237. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
  3238. ret = kmalloc_large_node(size, flags, node);
  3239. trace_kmalloc_node(_RET_IP_, ret,
  3240. size, PAGE_SIZE << get_order(size),
  3241. flags, node);
  3242. return ret;
  3243. }
  3244. s = kmalloc_slab(size, flags);
  3245. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3246. return s;
  3247. ret = slab_alloc_node(s, flags, node, _RET_IP_);
  3248. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  3249. kasan_kmalloc(s, ret, size, flags);
  3250. return ret;
  3251. }
  3252. EXPORT_SYMBOL(__kmalloc_node);
  3253. #endif
  3254. #ifdef CONFIG_HARDENED_USERCOPY
  3255. /*
  3256. * Rejects incorrectly sized objects and objects that are to be copied
  3257. * to/from userspace but do not fall entirely within the containing slab
  3258. * cache's usercopy region.
  3259. *
  3260. * Returns NULL if check passes, otherwise const char * to name of cache
  3261. * to indicate an error.
  3262. */
  3263. void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
  3264. bool to_user)
  3265. {
  3266. struct kmem_cache *s;
  3267. unsigned int offset;
  3268. size_t object_size;
  3269. /* Find object and usable object size. */
  3270. s = page->slab_cache;
  3271. /* Reject impossible pointers. */
  3272. if (ptr < page_address(page))
  3273. usercopy_abort("SLUB object not in SLUB page?!", NULL,
  3274. to_user, 0, n);
  3275. /* Find offset within object. */
  3276. offset = (ptr - page_address(page)) % s->size;
  3277. /* Adjust for redzone and reject if within the redzone. */
  3278. if (kmem_cache_debug(s) && s->flags & SLAB_RED_ZONE) {
  3279. if (offset < s->red_left_pad)
  3280. usercopy_abort("SLUB object in left red zone",
  3281. s->name, to_user, offset, n);
  3282. offset -= s->red_left_pad;
  3283. }
  3284. /* Allow address range falling entirely within usercopy region. */
  3285. if (offset >= s->useroffset &&
  3286. offset - s->useroffset <= s->usersize &&
  3287. n <= s->useroffset - offset + s->usersize)
  3288. return;
  3289. /*
  3290. * If the copy is still within the allocated object, produce
  3291. * a warning instead of rejecting the copy. This is intended
  3292. * to be a temporary method to find any missing usercopy
  3293. * whitelists.
  3294. */
  3295. object_size = slab_ksize(s);
  3296. if (usercopy_fallback &&
  3297. offset <= object_size && n <= object_size - offset) {
  3298. usercopy_warn("SLUB object", s->name, to_user, offset, n);
  3299. return;
  3300. }
  3301. usercopy_abort("SLUB object", s->name, to_user, offset, n);
  3302. }
  3303. #endif /* CONFIG_HARDENED_USERCOPY */
  3304. static size_t __ksize(const void *object)
  3305. {
  3306. struct page *page;
  3307. if (unlikely(object == ZERO_SIZE_PTR))
  3308. return 0;
  3309. page = virt_to_head_page(object);
  3310. if (unlikely(!PageSlab(page))) {
  3311. WARN_ON(!PageCompound(page));
  3312. return PAGE_SIZE << compound_order(page);
  3313. }
  3314. return slab_ksize(page->slab_cache);
  3315. }
  3316. size_t ksize(const void *object)
  3317. {
  3318. size_t size = __ksize(object);
  3319. /* We assume that ksize callers could use whole allocated area,
  3320. * so we need to unpoison this area.
  3321. */
  3322. kasan_unpoison_shadow(object, size);
  3323. return size;
  3324. }
  3325. EXPORT_SYMBOL(ksize);
  3326. void kfree(const void *x)
  3327. {
  3328. struct page *page;
  3329. void *object = (void *)x;
  3330. trace_kfree(_RET_IP_, x);
  3331. if (unlikely(ZERO_OR_NULL_PTR(x)))
  3332. return;
  3333. page = virt_to_head_page(x);
  3334. if (unlikely(!PageSlab(page))) {
  3335. BUG_ON(!PageCompound(page));
  3336. kfree_hook(object);
  3337. __free_pages(page, compound_order(page));
  3338. return;
  3339. }
  3340. slab_free(page->slab_cache, page, object, NULL, 1, _RET_IP_);
  3341. }
  3342. EXPORT_SYMBOL(kfree);
  3343. #define SHRINK_PROMOTE_MAX 32
  3344. /*
  3345. * kmem_cache_shrink discards empty slabs and promotes the slabs filled
  3346. * up most to the head of the partial lists. New allocations will then
  3347. * fill those up and thus they can be removed from the partial lists.
  3348. *
  3349. * The slabs with the least items are placed last. This results in them
  3350. * being allocated from last increasing the chance that the last objects
  3351. * are freed in them.
  3352. */
  3353. int __kmem_cache_shrink(struct kmem_cache *s)
  3354. {
  3355. int node;
  3356. int i;
  3357. struct kmem_cache_node *n;
  3358. struct page *page;
  3359. struct page *t;
  3360. struct list_head discard;
  3361. struct list_head promote[SHRINK_PROMOTE_MAX];
  3362. unsigned long flags;
  3363. int ret = 0;
  3364. flush_all(s);
  3365. for_each_kmem_cache_node(s, node, n) {
  3366. INIT_LIST_HEAD(&discard);
  3367. for (i = 0; i < SHRINK_PROMOTE_MAX; i++)
  3368. INIT_LIST_HEAD(promote + i);
  3369. spin_lock_irqsave(&n->list_lock, flags);
  3370. /*
  3371. * Build lists of slabs to discard or promote.
  3372. *
  3373. * Note that concurrent frees may occur while we hold the
  3374. * list_lock. page->inuse here is the upper limit.
  3375. */
  3376. list_for_each_entry_safe(page, t, &n->partial, lru) {
  3377. int free = page->objects - page->inuse;
  3378. /* Do not reread page->inuse */
  3379. barrier();
  3380. /* We do not keep full slabs on the list */
  3381. BUG_ON(free <= 0);
  3382. if (free == page->objects) {
  3383. list_move(&page->lru, &discard);
  3384. n->nr_partial--;
  3385. } else if (free <= SHRINK_PROMOTE_MAX)
  3386. list_move(&page->lru, promote + free - 1);
  3387. }
  3388. /*
  3389. * Promote the slabs filled up most to the head of the
  3390. * partial list.
  3391. */
  3392. for (i = SHRINK_PROMOTE_MAX - 1; i >= 0; i--)
  3393. list_splice(promote + i, &n->partial);
  3394. spin_unlock_irqrestore(&n->list_lock, flags);
  3395. /* Release empty slabs */
  3396. list_for_each_entry_safe(page, t, &discard, lru)
  3397. discard_slab(s, page);
  3398. if (slabs_node(s, node))
  3399. ret = 1;
  3400. }
  3401. return ret;
  3402. }
  3403. #ifdef CONFIG_MEMCG
  3404. static void kmemcg_cache_deact_after_rcu(struct kmem_cache *s)
  3405. {
  3406. /*
  3407. * Called with all the locks held after a sched RCU grace period.
  3408. * Even if @s becomes empty after shrinking, we can't know that @s
  3409. * doesn't have allocations already in-flight and thus can't
  3410. * destroy @s until the associated memcg is released.
  3411. *
  3412. * However, let's remove the sysfs files for empty caches here.
  3413. * Each cache has a lot of interface files which aren't
  3414. * particularly useful for empty draining caches; otherwise, we can
  3415. * easily end up with millions of unnecessary sysfs files on
  3416. * systems which have a lot of memory and transient cgroups.
  3417. */
  3418. if (!__kmem_cache_shrink(s))
  3419. sysfs_slab_remove(s);
  3420. }
  3421. void __kmemcg_cache_deactivate(struct kmem_cache *s)
  3422. {
  3423. /*
  3424. * Disable empty slabs caching. Used to avoid pinning offline
  3425. * memory cgroups by kmem pages that can be freed.
  3426. */
  3427. slub_set_cpu_partial(s, 0);
  3428. s->min_partial = 0;
  3429. /*
  3430. * s->cpu_partial is checked locklessly (see put_cpu_partial), so
  3431. * we have to make sure the change is visible before shrinking.
  3432. */
  3433. slab_deactivate_memcg_cache_rcu_sched(s, kmemcg_cache_deact_after_rcu);
  3434. }
  3435. #endif
  3436. static int slab_mem_going_offline_callback(void *arg)
  3437. {
  3438. struct kmem_cache *s;
  3439. mutex_lock(&slab_mutex);
  3440. list_for_each_entry(s, &slab_caches, list)
  3441. __kmem_cache_shrink(s);
  3442. mutex_unlock(&slab_mutex);
  3443. return 0;
  3444. }
  3445. static void slab_mem_offline_callback(void *arg)
  3446. {
  3447. struct kmem_cache_node *n;
  3448. struct kmem_cache *s;
  3449. struct memory_notify *marg = arg;
  3450. int offline_node;
  3451. offline_node = marg->status_change_nid_normal;
  3452. /*
  3453. * If the node still has available memory. we need kmem_cache_node
  3454. * for it yet.
  3455. */
  3456. if (offline_node < 0)
  3457. return;
  3458. mutex_lock(&slab_mutex);
  3459. list_for_each_entry(s, &slab_caches, list) {
  3460. n = get_node(s, offline_node);
  3461. if (n) {
  3462. /*
  3463. * if n->nr_slabs > 0, slabs still exist on the node
  3464. * that is going down. We were unable to free them,
  3465. * and offline_pages() function shouldn't call this
  3466. * callback. So, we must fail.
  3467. */
  3468. BUG_ON(slabs_node(s, offline_node));
  3469. s->node[offline_node] = NULL;
  3470. kmem_cache_free(kmem_cache_node, n);
  3471. }
  3472. }
  3473. mutex_unlock(&slab_mutex);
  3474. }
  3475. static int slab_mem_going_online_callback(void *arg)
  3476. {
  3477. struct kmem_cache_node *n;
  3478. struct kmem_cache *s;
  3479. struct memory_notify *marg = arg;
  3480. int nid = marg->status_change_nid_normal;
  3481. int ret = 0;
  3482. /*
  3483. * If the node's memory is already available, then kmem_cache_node is
  3484. * already created. Nothing to do.
  3485. */
  3486. if (nid < 0)
  3487. return 0;
  3488. /*
  3489. * We are bringing a node online. No memory is available yet. We must
  3490. * allocate a kmem_cache_node structure in order to bring the node
  3491. * online.
  3492. */
  3493. mutex_lock(&slab_mutex);
  3494. list_for_each_entry(s, &slab_caches, list) {
  3495. /*
  3496. * XXX: kmem_cache_alloc_node will fallback to other nodes
  3497. * since memory is not yet available from the node that
  3498. * is brought up.
  3499. */
  3500. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  3501. if (!n) {
  3502. ret = -ENOMEM;
  3503. goto out;
  3504. }
  3505. init_kmem_cache_node(n);
  3506. s->node[nid] = n;
  3507. }
  3508. out:
  3509. mutex_unlock(&slab_mutex);
  3510. return ret;
  3511. }
  3512. static int slab_memory_callback(struct notifier_block *self,
  3513. unsigned long action, void *arg)
  3514. {
  3515. int ret = 0;
  3516. switch (action) {
  3517. case MEM_GOING_ONLINE:
  3518. ret = slab_mem_going_online_callback(arg);
  3519. break;
  3520. case MEM_GOING_OFFLINE:
  3521. ret = slab_mem_going_offline_callback(arg);
  3522. break;
  3523. case MEM_OFFLINE:
  3524. case MEM_CANCEL_ONLINE:
  3525. slab_mem_offline_callback(arg);
  3526. break;
  3527. case MEM_ONLINE:
  3528. case MEM_CANCEL_OFFLINE:
  3529. break;
  3530. }
  3531. if (ret)
  3532. ret = notifier_from_errno(ret);
  3533. else
  3534. ret = NOTIFY_OK;
  3535. return ret;
  3536. }
  3537. static struct notifier_block slab_memory_callback_nb = {
  3538. .notifier_call = slab_memory_callback,
  3539. .priority = SLAB_CALLBACK_PRI,
  3540. };
  3541. /********************************************************************
  3542. * Basic setup of slabs
  3543. *******************************************************************/
  3544. /*
  3545. * Used for early kmem_cache structures that were allocated using
  3546. * the page allocator. Allocate them properly then fix up the pointers
  3547. * that may be pointing to the wrong kmem_cache structure.
  3548. */
  3549. static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
  3550. {
  3551. int node;
  3552. struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
  3553. struct kmem_cache_node *n;
  3554. memcpy(s, static_cache, kmem_cache->object_size);
  3555. /*
  3556. * This runs very early, and only the boot processor is supposed to be
  3557. * up. Even if it weren't true, IRQs are not up so we couldn't fire
  3558. * IPIs around.
  3559. */
  3560. __flush_cpu_slab(s, smp_processor_id());
  3561. for_each_kmem_cache_node(s, node, n) {
  3562. struct page *p;
  3563. list_for_each_entry(p, &n->partial, lru)
  3564. p->slab_cache = s;
  3565. #ifdef CONFIG_SLUB_DEBUG
  3566. list_for_each_entry(p, &n->full, lru)
  3567. p->slab_cache = s;
  3568. #endif
  3569. }
  3570. slab_init_memcg_params(s);
  3571. list_add(&s->list, &slab_caches);
  3572. memcg_link_cache(s);
  3573. return s;
  3574. }
  3575. void __init kmem_cache_init(void)
  3576. {
  3577. static __initdata struct kmem_cache boot_kmem_cache,
  3578. boot_kmem_cache_node;
  3579. if (debug_guardpage_minorder())
  3580. slub_max_order = 0;
  3581. kmem_cache_node = &boot_kmem_cache_node;
  3582. kmem_cache = &boot_kmem_cache;
  3583. create_boot_cache(kmem_cache_node, "kmem_cache_node",
  3584. sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0);
  3585. register_hotmemory_notifier(&slab_memory_callback_nb);
  3586. /* Able to allocate the per node structures */
  3587. slab_state = PARTIAL;
  3588. create_boot_cache(kmem_cache, "kmem_cache",
  3589. offsetof(struct kmem_cache, node) +
  3590. nr_node_ids * sizeof(struct kmem_cache_node *),
  3591. SLAB_HWCACHE_ALIGN, 0, 0);
  3592. kmem_cache = bootstrap(&boot_kmem_cache);
  3593. kmem_cache_node = bootstrap(&boot_kmem_cache_node);
  3594. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  3595. setup_kmalloc_cache_index_table();
  3596. create_kmalloc_caches(0);
  3597. /* Setup random freelists for each cache */
  3598. init_freelist_randomization();
  3599. cpuhp_setup_state_nocalls(CPUHP_SLUB_DEAD, "slub:dead", NULL,
  3600. slub_cpu_dead);
  3601. pr_info("SLUB: HWalign=%d, Order=%u-%u, MinObjects=%u, CPUs=%u, Nodes=%d\n",
  3602. cache_line_size(),
  3603. slub_min_order, slub_max_order, slub_min_objects,
  3604. nr_cpu_ids, nr_node_ids);
  3605. }
  3606. void __init kmem_cache_init_late(void)
  3607. {
  3608. }
  3609. struct kmem_cache *
  3610. __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
  3611. slab_flags_t flags, void (*ctor)(void *))
  3612. {
  3613. struct kmem_cache *s, *c;
  3614. s = find_mergeable(size, align, flags, name, ctor);
  3615. if (s) {
  3616. s->refcount++;
  3617. /*
  3618. * Adjust the object sizes so that we clear
  3619. * the complete object on kzalloc.
  3620. */
  3621. s->object_size = max(s->object_size, size);
  3622. s->inuse = max(s->inuse, ALIGN(size, sizeof(void *)));
  3623. for_each_memcg_cache(c, s) {
  3624. c->object_size = s->object_size;
  3625. c->inuse = max(c->inuse, ALIGN(size, sizeof(void *)));
  3626. }
  3627. if (sysfs_slab_alias(s, name)) {
  3628. s->refcount--;
  3629. s = NULL;
  3630. }
  3631. }
  3632. return s;
  3633. }
  3634. int __kmem_cache_create(struct kmem_cache *s, slab_flags_t flags)
  3635. {
  3636. int err;
  3637. err = kmem_cache_open(s, flags);
  3638. if (err)
  3639. return err;
  3640. /* Mutex is not taken during early boot */
  3641. if (slab_state <= UP)
  3642. return 0;
  3643. memcg_propagate_slab_attrs(s);
  3644. err = sysfs_slab_add(s);
  3645. if (err)
  3646. __kmem_cache_release(s);
  3647. return err;
  3648. }
  3649. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3650. {
  3651. struct kmem_cache *s;
  3652. void *ret;
  3653. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
  3654. return kmalloc_large(size, gfpflags);
  3655. s = kmalloc_slab(size, gfpflags);
  3656. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3657. return s;
  3658. ret = slab_alloc(s, gfpflags, caller);
  3659. /* Honor the call site pointer we received. */
  3660. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3661. return ret;
  3662. }
  3663. #ifdef CONFIG_NUMA
  3664. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3665. int node, unsigned long caller)
  3666. {
  3667. struct kmem_cache *s;
  3668. void *ret;
  3669. if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) {
  3670. ret = kmalloc_large_node(size, gfpflags, node);
  3671. trace_kmalloc_node(caller, ret,
  3672. size, PAGE_SIZE << get_order(size),
  3673. gfpflags, node);
  3674. return ret;
  3675. }
  3676. s = kmalloc_slab(size, gfpflags);
  3677. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3678. return s;
  3679. ret = slab_alloc_node(s, gfpflags, node, caller);
  3680. /* Honor the call site pointer we received. */
  3681. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3682. return ret;
  3683. }
  3684. #endif
  3685. #ifdef CONFIG_SYSFS
  3686. static int count_inuse(struct page *page)
  3687. {
  3688. return page->inuse;
  3689. }
  3690. static int count_total(struct page *page)
  3691. {
  3692. return page->objects;
  3693. }
  3694. #endif
  3695. #ifdef CONFIG_SLUB_DEBUG
  3696. static int validate_slab(struct kmem_cache *s, struct page *page,
  3697. unsigned long *map)
  3698. {
  3699. void *p;
  3700. void *addr = page_address(page);
  3701. if (!check_slab(s, page) ||
  3702. !on_freelist(s, page, NULL))
  3703. return 0;
  3704. /* Now we know that a valid freelist exists */
  3705. bitmap_zero(map, page->objects);
  3706. get_map(s, page, map);
  3707. for_each_object(p, s, addr, page->objects) {
  3708. if (test_bit(slab_index(p, s, addr), map))
  3709. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3710. return 0;
  3711. }
  3712. for_each_object(p, s, addr, page->objects)
  3713. if (!test_bit(slab_index(p, s, addr), map))
  3714. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3715. return 0;
  3716. return 1;
  3717. }
  3718. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3719. unsigned long *map)
  3720. {
  3721. slab_lock(page);
  3722. validate_slab(s, page, map);
  3723. slab_unlock(page);
  3724. }
  3725. static int validate_slab_node(struct kmem_cache *s,
  3726. struct kmem_cache_node *n, unsigned long *map)
  3727. {
  3728. unsigned long count = 0;
  3729. struct page *page;
  3730. unsigned long flags;
  3731. spin_lock_irqsave(&n->list_lock, flags);
  3732. list_for_each_entry(page, &n->partial, lru) {
  3733. validate_slab_slab(s, page, map);
  3734. count++;
  3735. }
  3736. if (count != n->nr_partial)
  3737. pr_err("SLUB %s: %ld partial slabs counted but counter=%ld\n",
  3738. s->name, count, n->nr_partial);
  3739. if (!(s->flags & SLAB_STORE_USER))
  3740. goto out;
  3741. list_for_each_entry(page, &n->full, lru) {
  3742. validate_slab_slab(s, page, map);
  3743. count++;
  3744. }
  3745. if (count != atomic_long_read(&n->nr_slabs))
  3746. pr_err("SLUB: %s %ld slabs counted but counter=%ld\n",
  3747. s->name, count, atomic_long_read(&n->nr_slabs));
  3748. out:
  3749. spin_unlock_irqrestore(&n->list_lock, flags);
  3750. return count;
  3751. }
  3752. static long validate_slab_cache(struct kmem_cache *s)
  3753. {
  3754. int node;
  3755. unsigned long count = 0;
  3756. struct kmem_cache_node *n;
  3757. unsigned long *map = bitmap_alloc(oo_objects(s->max), GFP_KERNEL);
  3758. if (!map)
  3759. return -ENOMEM;
  3760. flush_all(s);
  3761. for_each_kmem_cache_node(s, node, n)
  3762. count += validate_slab_node(s, n, map);
  3763. bitmap_free(map);
  3764. return count;
  3765. }
  3766. /*
  3767. * Generate lists of code addresses where slabcache objects are allocated
  3768. * and freed.
  3769. */
  3770. struct location {
  3771. unsigned long count;
  3772. unsigned long addr;
  3773. long long sum_time;
  3774. long min_time;
  3775. long max_time;
  3776. long min_pid;
  3777. long max_pid;
  3778. DECLARE_BITMAP(cpus, NR_CPUS);
  3779. nodemask_t nodes;
  3780. };
  3781. struct loc_track {
  3782. unsigned long max;
  3783. unsigned long count;
  3784. struct location *loc;
  3785. };
  3786. static void free_loc_track(struct loc_track *t)
  3787. {
  3788. if (t->max)
  3789. free_pages((unsigned long)t->loc,
  3790. get_order(sizeof(struct location) * t->max));
  3791. }
  3792. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3793. {
  3794. struct location *l;
  3795. int order;
  3796. order = get_order(sizeof(struct location) * max);
  3797. l = (void *)__get_free_pages(flags, order);
  3798. if (!l)
  3799. return 0;
  3800. if (t->count) {
  3801. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3802. free_loc_track(t);
  3803. }
  3804. t->max = max;
  3805. t->loc = l;
  3806. return 1;
  3807. }
  3808. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3809. const struct track *track)
  3810. {
  3811. long start, end, pos;
  3812. struct location *l;
  3813. unsigned long caddr;
  3814. unsigned long age = jiffies - track->when;
  3815. start = -1;
  3816. end = t->count;
  3817. for ( ; ; ) {
  3818. pos = start + (end - start + 1) / 2;
  3819. /*
  3820. * There is nothing at "end". If we end up there
  3821. * we need to add something to before end.
  3822. */
  3823. if (pos == end)
  3824. break;
  3825. caddr = t->loc[pos].addr;
  3826. if (track->addr == caddr) {
  3827. l = &t->loc[pos];
  3828. l->count++;
  3829. if (track->when) {
  3830. l->sum_time += age;
  3831. if (age < l->min_time)
  3832. l->min_time = age;
  3833. if (age > l->max_time)
  3834. l->max_time = age;
  3835. if (track->pid < l->min_pid)
  3836. l->min_pid = track->pid;
  3837. if (track->pid > l->max_pid)
  3838. l->max_pid = track->pid;
  3839. cpumask_set_cpu(track->cpu,
  3840. to_cpumask(l->cpus));
  3841. }
  3842. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3843. return 1;
  3844. }
  3845. if (track->addr < caddr)
  3846. end = pos;
  3847. else
  3848. start = pos;
  3849. }
  3850. /*
  3851. * Not found. Insert new tracking element.
  3852. */
  3853. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3854. return 0;
  3855. l = t->loc + pos;
  3856. if (pos < t->count)
  3857. memmove(l + 1, l,
  3858. (t->count - pos) * sizeof(struct location));
  3859. t->count++;
  3860. l->count = 1;
  3861. l->addr = track->addr;
  3862. l->sum_time = age;
  3863. l->min_time = age;
  3864. l->max_time = age;
  3865. l->min_pid = track->pid;
  3866. l->max_pid = track->pid;
  3867. cpumask_clear(to_cpumask(l->cpus));
  3868. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3869. nodes_clear(l->nodes);
  3870. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3871. return 1;
  3872. }
  3873. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3874. struct page *page, enum track_item alloc,
  3875. unsigned long *map)
  3876. {
  3877. void *addr = page_address(page);
  3878. void *p;
  3879. bitmap_zero(map, page->objects);
  3880. get_map(s, page, map);
  3881. for_each_object(p, s, addr, page->objects)
  3882. if (!test_bit(slab_index(p, s, addr), map))
  3883. add_location(t, s, get_track(s, p, alloc));
  3884. }
  3885. static int list_locations(struct kmem_cache *s, char *buf,
  3886. enum track_item alloc)
  3887. {
  3888. int len = 0;
  3889. unsigned long i;
  3890. struct loc_track t = { 0, 0, NULL };
  3891. int node;
  3892. struct kmem_cache_node *n;
  3893. unsigned long *map = bitmap_alloc(oo_objects(s->max), GFP_KERNEL);
  3894. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3895. GFP_KERNEL)) {
  3896. bitmap_free(map);
  3897. return sprintf(buf, "Out of memory\n");
  3898. }
  3899. /* Push back cpu slabs */
  3900. flush_all(s);
  3901. for_each_kmem_cache_node(s, node, n) {
  3902. unsigned long flags;
  3903. struct page *page;
  3904. if (!atomic_long_read(&n->nr_slabs))
  3905. continue;
  3906. spin_lock_irqsave(&n->list_lock, flags);
  3907. list_for_each_entry(page, &n->partial, lru)
  3908. process_slab(&t, s, page, alloc, map);
  3909. list_for_each_entry(page, &n->full, lru)
  3910. process_slab(&t, s, page, alloc, map);
  3911. spin_unlock_irqrestore(&n->list_lock, flags);
  3912. }
  3913. for (i = 0; i < t.count; i++) {
  3914. struct location *l = &t.loc[i];
  3915. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3916. break;
  3917. len += sprintf(buf + len, "%7ld ", l->count);
  3918. if (l->addr)
  3919. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3920. else
  3921. len += sprintf(buf + len, "<not-available>");
  3922. if (l->sum_time != l->min_time) {
  3923. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3924. l->min_time,
  3925. (long)div_u64(l->sum_time, l->count),
  3926. l->max_time);
  3927. } else
  3928. len += sprintf(buf + len, " age=%ld",
  3929. l->min_time);
  3930. if (l->min_pid != l->max_pid)
  3931. len += sprintf(buf + len, " pid=%ld-%ld",
  3932. l->min_pid, l->max_pid);
  3933. else
  3934. len += sprintf(buf + len, " pid=%ld",
  3935. l->min_pid);
  3936. if (num_online_cpus() > 1 &&
  3937. !cpumask_empty(to_cpumask(l->cpus)) &&
  3938. len < PAGE_SIZE - 60)
  3939. len += scnprintf(buf + len, PAGE_SIZE - len - 50,
  3940. " cpus=%*pbl",
  3941. cpumask_pr_args(to_cpumask(l->cpus)));
  3942. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3943. len < PAGE_SIZE - 60)
  3944. len += scnprintf(buf + len, PAGE_SIZE - len - 50,
  3945. " nodes=%*pbl",
  3946. nodemask_pr_args(&l->nodes));
  3947. len += sprintf(buf + len, "\n");
  3948. }
  3949. free_loc_track(&t);
  3950. bitmap_free(map);
  3951. if (!t.count)
  3952. len += sprintf(buf, "No data\n");
  3953. return len;
  3954. }
  3955. #endif
  3956. #ifdef SLUB_RESILIENCY_TEST
  3957. static void __init resiliency_test(void)
  3958. {
  3959. u8 *p;
  3960. int type = KMALLOC_NORMAL;
  3961. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || KMALLOC_SHIFT_HIGH < 10);
  3962. pr_err("SLUB resiliency testing\n");
  3963. pr_err("-----------------------\n");
  3964. pr_err("A. Corruption after allocation\n");
  3965. p = kzalloc(16, GFP_KERNEL);
  3966. p[16] = 0x12;
  3967. pr_err("\n1. kmalloc-16: Clobber Redzone/next pointer 0x12->0x%p\n\n",
  3968. p + 16);
  3969. validate_slab_cache(kmalloc_caches[type][4]);
  3970. /* Hmmm... The next two are dangerous */
  3971. p = kzalloc(32, GFP_KERNEL);
  3972. p[32 + sizeof(void *)] = 0x34;
  3973. pr_err("\n2. kmalloc-32: Clobber next pointer/next slab 0x34 -> -0x%p\n",
  3974. p);
  3975. pr_err("If allocated object is overwritten then not detectable\n\n");
  3976. validate_slab_cache(kmalloc_caches[type][5]);
  3977. p = kzalloc(64, GFP_KERNEL);
  3978. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3979. *p = 0x56;
  3980. pr_err("\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3981. p);
  3982. pr_err("If allocated object is overwritten then not detectable\n\n");
  3983. validate_slab_cache(kmalloc_caches[type][6]);
  3984. pr_err("\nB. Corruption after free\n");
  3985. p = kzalloc(128, GFP_KERNEL);
  3986. kfree(p);
  3987. *p = 0x78;
  3988. pr_err("1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3989. validate_slab_cache(kmalloc_caches[type][7]);
  3990. p = kzalloc(256, GFP_KERNEL);
  3991. kfree(p);
  3992. p[50] = 0x9a;
  3993. pr_err("\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
  3994. validate_slab_cache(kmalloc_caches[type][8]);
  3995. p = kzalloc(512, GFP_KERNEL);
  3996. kfree(p);
  3997. p[512] = 0xab;
  3998. pr_err("\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3999. validate_slab_cache(kmalloc_caches[type][9]);
  4000. }
  4001. #else
  4002. #ifdef CONFIG_SYSFS
  4003. static void resiliency_test(void) {};
  4004. #endif
  4005. #endif
  4006. #ifdef CONFIG_SYSFS
  4007. enum slab_stat_type {
  4008. SL_ALL, /* All slabs */
  4009. SL_PARTIAL, /* Only partially allocated slabs */
  4010. SL_CPU, /* Only slabs used for cpu caches */
  4011. SL_OBJECTS, /* Determine allocated objects not slabs */
  4012. SL_TOTAL /* Determine object capacity not slabs */
  4013. };
  4014. #define SO_ALL (1 << SL_ALL)
  4015. #define SO_PARTIAL (1 << SL_PARTIAL)
  4016. #define SO_CPU (1 << SL_CPU)
  4017. #define SO_OBJECTS (1 << SL_OBJECTS)
  4018. #define SO_TOTAL (1 << SL_TOTAL)
  4019. #ifdef CONFIG_MEMCG
  4020. static bool memcg_sysfs_enabled = IS_ENABLED(CONFIG_SLUB_MEMCG_SYSFS_ON);
  4021. static int __init setup_slub_memcg_sysfs(char *str)
  4022. {
  4023. int v;
  4024. if (get_option(&str, &v) > 0)
  4025. memcg_sysfs_enabled = v;
  4026. return 1;
  4027. }
  4028. __setup("slub_memcg_sysfs=", setup_slub_memcg_sysfs);
  4029. #endif
  4030. static ssize_t show_slab_objects(struct kmem_cache *s,
  4031. char *buf, unsigned long flags)
  4032. {
  4033. unsigned long total = 0;
  4034. int node;
  4035. int x;
  4036. unsigned long *nodes;
  4037. nodes = kcalloc(nr_node_ids, sizeof(unsigned long), GFP_KERNEL);
  4038. if (!nodes)
  4039. return -ENOMEM;
  4040. if (flags & SO_CPU) {
  4041. int cpu;
  4042. for_each_possible_cpu(cpu) {
  4043. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab,
  4044. cpu);
  4045. int node;
  4046. struct page *page;
  4047. page = READ_ONCE(c->page);
  4048. if (!page)
  4049. continue;
  4050. node = page_to_nid(page);
  4051. if (flags & SO_TOTAL)
  4052. x = page->objects;
  4053. else if (flags & SO_OBJECTS)
  4054. x = page->inuse;
  4055. else
  4056. x = 1;
  4057. total += x;
  4058. nodes[node] += x;
  4059. page = slub_percpu_partial_read_once(c);
  4060. if (page) {
  4061. node = page_to_nid(page);
  4062. if (flags & SO_TOTAL)
  4063. WARN_ON_ONCE(1);
  4064. else if (flags & SO_OBJECTS)
  4065. WARN_ON_ONCE(1);
  4066. else
  4067. x = page->pages;
  4068. total += x;
  4069. nodes[node] += x;
  4070. }
  4071. }
  4072. }
  4073. get_online_mems();
  4074. #ifdef CONFIG_SLUB_DEBUG
  4075. if (flags & SO_ALL) {
  4076. struct kmem_cache_node *n;
  4077. for_each_kmem_cache_node(s, node, n) {
  4078. if (flags & SO_TOTAL)
  4079. x = atomic_long_read(&n->total_objects);
  4080. else if (flags & SO_OBJECTS)
  4081. x = atomic_long_read(&n->total_objects) -
  4082. count_partial(n, count_free);
  4083. else
  4084. x = atomic_long_read(&n->nr_slabs);
  4085. total += x;
  4086. nodes[node] += x;
  4087. }
  4088. } else
  4089. #endif
  4090. if (flags & SO_PARTIAL) {
  4091. struct kmem_cache_node *n;
  4092. for_each_kmem_cache_node(s, node, n) {
  4093. if (flags & SO_TOTAL)
  4094. x = count_partial(n, count_total);
  4095. else if (flags & SO_OBJECTS)
  4096. x = count_partial(n, count_inuse);
  4097. else
  4098. x = n->nr_partial;
  4099. total += x;
  4100. nodes[node] += x;
  4101. }
  4102. }
  4103. x = sprintf(buf, "%lu", total);
  4104. #ifdef CONFIG_NUMA
  4105. for (node = 0; node < nr_node_ids; node++)
  4106. if (nodes[node])
  4107. x += sprintf(buf + x, " N%d=%lu",
  4108. node, nodes[node]);
  4109. #endif
  4110. put_online_mems();
  4111. kfree(nodes);
  4112. return x + sprintf(buf + x, "\n");
  4113. }
  4114. #ifdef CONFIG_SLUB_DEBUG
  4115. static int any_slab_objects(struct kmem_cache *s)
  4116. {
  4117. int node;
  4118. struct kmem_cache_node *n;
  4119. for_each_kmem_cache_node(s, node, n)
  4120. if (atomic_long_read(&n->total_objects))
  4121. return 1;
  4122. return 0;
  4123. }
  4124. #endif
  4125. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  4126. #define to_slab(n) container_of(n, struct kmem_cache, kobj)
  4127. struct slab_attribute {
  4128. struct attribute attr;
  4129. ssize_t (*show)(struct kmem_cache *s, char *buf);
  4130. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  4131. };
  4132. #define SLAB_ATTR_RO(_name) \
  4133. static struct slab_attribute _name##_attr = \
  4134. __ATTR(_name, 0400, _name##_show, NULL)
  4135. #define SLAB_ATTR(_name) \
  4136. static struct slab_attribute _name##_attr = \
  4137. __ATTR(_name, 0600, _name##_show, _name##_store)
  4138. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  4139. {
  4140. return sprintf(buf, "%u\n", s->size);
  4141. }
  4142. SLAB_ATTR_RO(slab_size);
  4143. static ssize_t align_show(struct kmem_cache *s, char *buf)
  4144. {
  4145. return sprintf(buf, "%u\n", s->align);
  4146. }
  4147. SLAB_ATTR_RO(align);
  4148. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  4149. {
  4150. return sprintf(buf, "%u\n", s->object_size);
  4151. }
  4152. SLAB_ATTR_RO(object_size);
  4153. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  4154. {
  4155. return sprintf(buf, "%u\n", oo_objects(s->oo));
  4156. }
  4157. SLAB_ATTR_RO(objs_per_slab);
  4158. static ssize_t order_store(struct kmem_cache *s,
  4159. const char *buf, size_t length)
  4160. {
  4161. unsigned int order;
  4162. int err;
  4163. err = kstrtouint(buf, 10, &order);
  4164. if (err)
  4165. return err;
  4166. if (order > slub_max_order || order < slub_min_order)
  4167. return -EINVAL;
  4168. calculate_sizes(s, order);
  4169. return length;
  4170. }
  4171. static ssize_t order_show(struct kmem_cache *s, char *buf)
  4172. {
  4173. return sprintf(buf, "%u\n", oo_order(s->oo));
  4174. }
  4175. SLAB_ATTR(order);
  4176. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  4177. {
  4178. return sprintf(buf, "%lu\n", s->min_partial);
  4179. }
  4180. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  4181. size_t length)
  4182. {
  4183. unsigned long min;
  4184. int err;
  4185. err = kstrtoul(buf, 10, &min);
  4186. if (err)
  4187. return err;
  4188. set_min_partial(s, min);
  4189. return length;
  4190. }
  4191. SLAB_ATTR(min_partial);
  4192. static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
  4193. {
  4194. return sprintf(buf, "%u\n", slub_cpu_partial(s));
  4195. }
  4196. static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
  4197. size_t length)
  4198. {
  4199. unsigned int objects;
  4200. int err;
  4201. err = kstrtouint(buf, 10, &objects);
  4202. if (err)
  4203. return err;
  4204. if (objects && !kmem_cache_has_cpu_partial(s))
  4205. return -EINVAL;
  4206. slub_set_cpu_partial(s, objects);
  4207. flush_all(s);
  4208. return length;
  4209. }
  4210. SLAB_ATTR(cpu_partial);
  4211. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  4212. {
  4213. if (!s->ctor)
  4214. return 0;
  4215. return sprintf(buf, "%pS\n", s->ctor);
  4216. }
  4217. SLAB_ATTR_RO(ctor);
  4218. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  4219. {
  4220. return sprintf(buf, "%d\n", s->refcount < 0 ? 0 : s->refcount - 1);
  4221. }
  4222. SLAB_ATTR_RO(aliases);
  4223. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  4224. {
  4225. return show_slab_objects(s, buf, SO_PARTIAL);
  4226. }
  4227. SLAB_ATTR_RO(partial);
  4228. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  4229. {
  4230. return show_slab_objects(s, buf, SO_CPU);
  4231. }
  4232. SLAB_ATTR_RO(cpu_slabs);
  4233. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  4234. {
  4235. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  4236. }
  4237. SLAB_ATTR_RO(objects);
  4238. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  4239. {
  4240. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  4241. }
  4242. SLAB_ATTR_RO(objects_partial);
  4243. static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
  4244. {
  4245. int objects = 0;
  4246. int pages = 0;
  4247. int cpu;
  4248. int len;
  4249. for_each_online_cpu(cpu) {
  4250. struct page *page;
  4251. page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
  4252. if (page) {
  4253. pages += page->pages;
  4254. objects += page->pobjects;
  4255. }
  4256. }
  4257. len = sprintf(buf, "%d(%d)", objects, pages);
  4258. #ifdef CONFIG_SMP
  4259. for_each_online_cpu(cpu) {
  4260. struct page *page;
  4261. page = slub_percpu_partial(per_cpu_ptr(s->cpu_slab, cpu));
  4262. if (page && len < PAGE_SIZE - 20)
  4263. len += sprintf(buf + len, " C%d=%d(%d)", cpu,
  4264. page->pobjects, page->pages);
  4265. }
  4266. #endif
  4267. return len + sprintf(buf + len, "\n");
  4268. }
  4269. SLAB_ATTR_RO(slabs_cpu_partial);
  4270. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  4271. {
  4272. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  4273. }
  4274. static ssize_t reclaim_account_store(struct kmem_cache *s,
  4275. const char *buf, size_t length)
  4276. {
  4277. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  4278. if (buf[0] == '1')
  4279. s->flags |= SLAB_RECLAIM_ACCOUNT;
  4280. return length;
  4281. }
  4282. SLAB_ATTR(reclaim_account);
  4283. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  4284. {
  4285. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  4286. }
  4287. SLAB_ATTR_RO(hwcache_align);
  4288. #ifdef CONFIG_ZONE_DMA
  4289. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  4290. {
  4291. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  4292. }
  4293. SLAB_ATTR_RO(cache_dma);
  4294. #endif
  4295. static ssize_t usersize_show(struct kmem_cache *s, char *buf)
  4296. {
  4297. return sprintf(buf, "%u\n", s->usersize);
  4298. }
  4299. SLAB_ATTR_RO(usersize);
  4300. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  4301. {
  4302. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TYPESAFE_BY_RCU));
  4303. }
  4304. SLAB_ATTR_RO(destroy_by_rcu);
  4305. #ifdef CONFIG_SLUB_DEBUG
  4306. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  4307. {
  4308. return show_slab_objects(s, buf, SO_ALL);
  4309. }
  4310. SLAB_ATTR_RO(slabs);
  4311. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  4312. {
  4313. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  4314. }
  4315. SLAB_ATTR_RO(total_objects);
  4316. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  4317. {
  4318. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CONSISTENCY_CHECKS));
  4319. }
  4320. static ssize_t sanity_checks_store(struct kmem_cache *s,
  4321. const char *buf, size_t length)
  4322. {
  4323. s->flags &= ~SLAB_CONSISTENCY_CHECKS;
  4324. if (buf[0] == '1') {
  4325. s->flags &= ~__CMPXCHG_DOUBLE;
  4326. s->flags |= SLAB_CONSISTENCY_CHECKS;
  4327. }
  4328. return length;
  4329. }
  4330. SLAB_ATTR(sanity_checks);
  4331. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  4332. {
  4333. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  4334. }
  4335. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  4336. size_t length)
  4337. {
  4338. /*
  4339. * Tracing a merged cache is going to give confusing results
  4340. * as well as cause other issues like converting a mergeable
  4341. * cache into an umergeable one.
  4342. */
  4343. if (s->refcount > 1)
  4344. return -EINVAL;
  4345. s->flags &= ~SLAB_TRACE;
  4346. if (buf[0] == '1') {
  4347. s->flags &= ~__CMPXCHG_DOUBLE;
  4348. s->flags |= SLAB_TRACE;
  4349. }
  4350. return length;
  4351. }
  4352. SLAB_ATTR(trace);
  4353. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  4354. {
  4355. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  4356. }
  4357. static ssize_t red_zone_store(struct kmem_cache *s,
  4358. const char *buf, size_t length)
  4359. {
  4360. if (any_slab_objects(s))
  4361. return -EBUSY;
  4362. s->flags &= ~SLAB_RED_ZONE;
  4363. if (buf[0] == '1') {
  4364. s->flags |= SLAB_RED_ZONE;
  4365. }
  4366. calculate_sizes(s, -1);
  4367. return length;
  4368. }
  4369. SLAB_ATTR(red_zone);
  4370. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  4371. {
  4372. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  4373. }
  4374. static ssize_t poison_store(struct kmem_cache *s,
  4375. const char *buf, size_t length)
  4376. {
  4377. if (any_slab_objects(s))
  4378. return -EBUSY;
  4379. s->flags &= ~SLAB_POISON;
  4380. if (buf[0] == '1') {
  4381. s->flags |= SLAB_POISON;
  4382. }
  4383. calculate_sizes(s, -1);
  4384. return length;
  4385. }
  4386. SLAB_ATTR(poison);
  4387. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  4388. {
  4389. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  4390. }
  4391. static ssize_t store_user_store(struct kmem_cache *s,
  4392. const char *buf, size_t length)
  4393. {
  4394. if (any_slab_objects(s))
  4395. return -EBUSY;
  4396. s->flags &= ~SLAB_STORE_USER;
  4397. if (buf[0] == '1') {
  4398. s->flags &= ~__CMPXCHG_DOUBLE;
  4399. s->flags |= SLAB_STORE_USER;
  4400. }
  4401. calculate_sizes(s, -1);
  4402. return length;
  4403. }
  4404. SLAB_ATTR(store_user);
  4405. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  4406. {
  4407. return 0;
  4408. }
  4409. static ssize_t validate_store(struct kmem_cache *s,
  4410. const char *buf, size_t length)
  4411. {
  4412. int ret = -EINVAL;
  4413. if (buf[0] == '1') {
  4414. ret = validate_slab_cache(s);
  4415. if (ret >= 0)
  4416. ret = length;
  4417. }
  4418. return ret;
  4419. }
  4420. SLAB_ATTR(validate);
  4421. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  4422. {
  4423. if (!(s->flags & SLAB_STORE_USER))
  4424. return -ENOSYS;
  4425. return list_locations(s, buf, TRACK_ALLOC);
  4426. }
  4427. SLAB_ATTR_RO(alloc_calls);
  4428. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  4429. {
  4430. if (!(s->flags & SLAB_STORE_USER))
  4431. return -ENOSYS;
  4432. return list_locations(s, buf, TRACK_FREE);
  4433. }
  4434. SLAB_ATTR_RO(free_calls);
  4435. #endif /* CONFIG_SLUB_DEBUG */
  4436. #ifdef CONFIG_FAILSLAB
  4437. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  4438. {
  4439. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  4440. }
  4441. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  4442. size_t length)
  4443. {
  4444. if (s->refcount > 1)
  4445. return -EINVAL;
  4446. s->flags &= ~SLAB_FAILSLAB;
  4447. if (buf[0] == '1')
  4448. s->flags |= SLAB_FAILSLAB;
  4449. return length;
  4450. }
  4451. SLAB_ATTR(failslab);
  4452. #endif
  4453. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  4454. {
  4455. return 0;
  4456. }
  4457. static ssize_t shrink_store(struct kmem_cache *s,
  4458. const char *buf, size_t length)
  4459. {
  4460. if (buf[0] == '1')
  4461. kmem_cache_shrink(s);
  4462. else
  4463. return -EINVAL;
  4464. return length;
  4465. }
  4466. SLAB_ATTR(shrink);
  4467. #ifdef CONFIG_NUMA
  4468. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  4469. {
  4470. return sprintf(buf, "%u\n", s->remote_node_defrag_ratio / 10);
  4471. }
  4472. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  4473. const char *buf, size_t length)
  4474. {
  4475. unsigned int ratio;
  4476. int err;
  4477. err = kstrtouint(buf, 10, &ratio);
  4478. if (err)
  4479. return err;
  4480. if (ratio > 100)
  4481. return -ERANGE;
  4482. s->remote_node_defrag_ratio = ratio * 10;
  4483. return length;
  4484. }
  4485. SLAB_ATTR(remote_node_defrag_ratio);
  4486. #endif
  4487. #ifdef CONFIG_SLUB_STATS
  4488. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  4489. {
  4490. unsigned long sum = 0;
  4491. int cpu;
  4492. int len;
  4493. int *data = kmalloc_array(nr_cpu_ids, sizeof(int), GFP_KERNEL);
  4494. if (!data)
  4495. return -ENOMEM;
  4496. for_each_online_cpu(cpu) {
  4497. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  4498. data[cpu] = x;
  4499. sum += x;
  4500. }
  4501. len = sprintf(buf, "%lu", sum);
  4502. #ifdef CONFIG_SMP
  4503. for_each_online_cpu(cpu) {
  4504. if (data[cpu] && len < PAGE_SIZE - 20)
  4505. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  4506. }
  4507. #endif
  4508. kfree(data);
  4509. return len + sprintf(buf + len, "\n");
  4510. }
  4511. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  4512. {
  4513. int cpu;
  4514. for_each_online_cpu(cpu)
  4515. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  4516. }
  4517. #define STAT_ATTR(si, text) \
  4518. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  4519. { \
  4520. return show_stat(s, buf, si); \
  4521. } \
  4522. static ssize_t text##_store(struct kmem_cache *s, \
  4523. const char *buf, size_t length) \
  4524. { \
  4525. if (buf[0] != '0') \
  4526. return -EINVAL; \
  4527. clear_stat(s, si); \
  4528. return length; \
  4529. } \
  4530. SLAB_ATTR(text); \
  4531. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  4532. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  4533. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  4534. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  4535. STAT_ATTR(FREE_FROZEN, free_frozen);
  4536. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  4537. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  4538. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  4539. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  4540. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  4541. STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
  4542. STAT_ATTR(FREE_SLAB, free_slab);
  4543. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  4544. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  4545. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  4546. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  4547. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  4548. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  4549. STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
  4550. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  4551. STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
  4552. STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
  4553. STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
  4554. STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
  4555. STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
  4556. STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
  4557. #endif
  4558. static struct attribute *slab_attrs[] = {
  4559. &slab_size_attr.attr,
  4560. &object_size_attr.attr,
  4561. &objs_per_slab_attr.attr,
  4562. &order_attr.attr,
  4563. &min_partial_attr.attr,
  4564. &cpu_partial_attr.attr,
  4565. &objects_attr.attr,
  4566. &objects_partial_attr.attr,
  4567. &partial_attr.attr,
  4568. &cpu_slabs_attr.attr,
  4569. &ctor_attr.attr,
  4570. &aliases_attr.attr,
  4571. &align_attr.attr,
  4572. &hwcache_align_attr.attr,
  4573. &reclaim_account_attr.attr,
  4574. &destroy_by_rcu_attr.attr,
  4575. &shrink_attr.attr,
  4576. &slabs_cpu_partial_attr.attr,
  4577. #ifdef CONFIG_SLUB_DEBUG
  4578. &total_objects_attr.attr,
  4579. &slabs_attr.attr,
  4580. &sanity_checks_attr.attr,
  4581. &trace_attr.attr,
  4582. &red_zone_attr.attr,
  4583. &poison_attr.attr,
  4584. &store_user_attr.attr,
  4585. &validate_attr.attr,
  4586. &alloc_calls_attr.attr,
  4587. &free_calls_attr.attr,
  4588. #endif
  4589. #ifdef CONFIG_ZONE_DMA
  4590. &cache_dma_attr.attr,
  4591. #endif
  4592. #ifdef CONFIG_NUMA
  4593. &remote_node_defrag_ratio_attr.attr,
  4594. #endif
  4595. #ifdef CONFIG_SLUB_STATS
  4596. &alloc_fastpath_attr.attr,
  4597. &alloc_slowpath_attr.attr,
  4598. &free_fastpath_attr.attr,
  4599. &free_slowpath_attr.attr,
  4600. &free_frozen_attr.attr,
  4601. &free_add_partial_attr.attr,
  4602. &free_remove_partial_attr.attr,
  4603. &alloc_from_partial_attr.attr,
  4604. &alloc_slab_attr.attr,
  4605. &alloc_refill_attr.attr,
  4606. &alloc_node_mismatch_attr.attr,
  4607. &free_slab_attr.attr,
  4608. &cpuslab_flush_attr.attr,
  4609. &deactivate_full_attr.attr,
  4610. &deactivate_empty_attr.attr,
  4611. &deactivate_to_head_attr.attr,
  4612. &deactivate_to_tail_attr.attr,
  4613. &deactivate_remote_frees_attr.attr,
  4614. &deactivate_bypass_attr.attr,
  4615. &order_fallback_attr.attr,
  4616. &cmpxchg_double_fail_attr.attr,
  4617. &cmpxchg_double_cpu_fail_attr.attr,
  4618. &cpu_partial_alloc_attr.attr,
  4619. &cpu_partial_free_attr.attr,
  4620. &cpu_partial_node_attr.attr,
  4621. &cpu_partial_drain_attr.attr,
  4622. #endif
  4623. #ifdef CONFIG_FAILSLAB
  4624. &failslab_attr.attr,
  4625. #endif
  4626. &usersize_attr.attr,
  4627. NULL
  4628. };
  4629. static const struct attribute_group slab_attr_group = {
  4630. .attrs = slab_attrs,
  4631. };
  4632. static ssize_t slab_attr_show(struct kobject *kobj,
  4633. struct attribute *attr,
  4634. char *buf)
  4635. {
  4636. struct slab_attribute *attribute;
  4637. struct kmem_cache *s;
  4638. int err;
  4639. attribute = to_slab_attr(attr);
  4640. s = to_slab(kobj);
  4641. if (!attribute->show)
  4642. return -EIO;
  4643. err = attribute->show(s, buf);
  4644. return err;
  4645. }
  4646. static ssize_t slab_attr_store(struct kobject *kobj,
  4647. struct attribute *attr,
  4648. const char *buf, size_t len)
  4649. {
  4650. struct slab_attribute *attribute;
  4651. struct kmem_cache *s;
  4652. int err;
  4653. attribute = to_slab_attr(attr);
  4654. s = to_slab(kobj);
  4655. if (!attribute->store)
  4656. return -EIO;
  4657. err = attribute->store(s, buf, len);
  4658. #ifdef CONFIG_MEMCG
  4659. if (slab_state >= FULL && err >= 0 && is_root_cache(s)) {
  4660. struct kmem_cache *c;
  4661. mutex_lock(&slab_mutex);
  4662. if (s->max_attr_size < len)
  4663. s->max_attr_size = len;
  4664. /*
  4665. * This is a best effort propagation, so this function's return
  4666. * value will be determined by the parent cache only. This is
  4667. * basically because not all attributes will have a well
  4668. * defined semantics for rollbacks - most of the actions will
  4669. * have permanent effects.
  4670. *
  4671. * Returning the error value of any of the children that fail
  4672. * is not 100 % defined, in the sense that users seeing the
  4673. * error code won't be able to know anything about the state of
  4674. * the cache.
  4675. *
  4676. * Only returning the error code for the parent cache at least
  4677. * has well defined semantics. The cache being written to
  4678. * directly either failed or succeeded, in which case we loop
  4679. * through the descendants with best-effort propagation.
  4680. */
  4681. for_each_memcg_cache(c, s)
  4682. attribute->store(c, buf, len);
  4683. mutex_unlock(&slab_mutex);
  4684. }
  4685. #endif
  4686. return err;
  4687. }
  4688. static void memcg_propagate_slab_attrs(struct kmem_cache *s)
  4689. {
  4690. #ifdef CONFIG_MEMCG
  4691. int i;
  4692. char *buffer = NULL;
  4693. struct kmem_cache *root_cache;
  4694. if (is_root_cache(s))
  4695. return;
  4696. root_cache = s->memcg_params.root_cache;
  4697. /*
  4698. * This mean this cache had no attribute written. Therefore, no point
  4699. * in copying default values around
  4700. */
  4701. if (!root_cache->max_attr_size)
  4702. return;
  4703. for (i = 0; i < ARRAY_SIZE(slab_attrs); i++) {
  4704. char mbuf[64];
  4705. char *buf;
  4706. struct slab_attribute *attr = to_slab_attr(slab_attrs[i]);
  4707. ssize_t len;
  4708. if (!attr || !attr->store || !attr->show)
  4709. continue;
  4710. /*
  4711. * It is really bad that we have to allocate here, so we will
  4712. * do it only as a fallback. If we actually allocate, though,
  4713. * we can just use the allocated buffer until the end.
  4714. *
  4715. * Most of the slub attributes will tend to be very small in
  4716. * size, but sysfs allows buffers up to a page, so they can
  4717. * theoretically happen.
  4718. */
  4719. if (buffer)
  4720. buf = buffer;
  4721. else if (root_cache->max_attr_size < ARRAY_SIZE(mbuf))
  4722. buf = mbuf;
  4723. else {
  4724. buffer = (char *) get_zeroed_page(GFP_KERNEL);
  4725. if (WARN_ON(!buffer))
  4726. continue;
  4727. buf = buffer;
  4728. }
  4729. len = attr->show(root_cache, buf);
  4730. if (len > 0)
  4731. attr->store(s, buf, len);
  4732. }
  4733. if (buffer)
  4734. free_page((unsigned long)buffer);
  4735. #endif
  4736. }
  4737. static void kmem_cache_release(struct kobject *k)
  4738. {
  4739. slab_kmem_cache_release(to_slab(k));
  4740. }
  4741. static const struct sysfs_ops slab_sysfs_ops = {
  4742. .show = slab_attr_show,
  4743. .store = slab_attr_store,
  4744. };
  4745. static struct kobj_type slab_ktype = {
  4746. .sysfs_ops = &slab_sysfs_ops,
  4747. .release = kmem_cache_release,
  4748. };
  4749. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  4750. {
  4751. struct kobj_type *ktype = get_ktype(kobj);
  4752. if (ktype == &slab_ktype)
  4753. return 1;
  4754. return 0;
  4755. }
  4756. static const struct kset_uevent_ops slab_uevent_ops = {
  4757. .filter = uevent_filter,
  4758. };
  4759. static struct kset *slab_kset;
  4760. static inline struct kset *cache_kset(struct kmem_cache *s)
  4761. {
  4762. #ifdef CONFIG_MEMCG
  4763. if (!is_root_cache(s))
  4764. return s->memcg_params.root_cache->memcg_kset;
  4765. #endif
  4766. return slab_kset;
  4767. }
  4768. #define ID_STR_LENGTH 64
  4769. /* Create a unique string id for a slab cache:
  4770. *
  4771. * Format :[flags-]size
  4772. */
  4773. static char *create_unique_id(struct kmem_cache *s)
  4774. {
  4775. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  4776. char *p = name;
  4777. BUG_ON(!name);
  4778. *p++ = ':';
  4779. /*
  4780. * First flags affecting slabcache operations. We will only
  4781. * get here for aliasable slabs so we do not need to support
  4782. * too many flags. The flags here must cover all flags that
  4783. * are matched during merging to guarantee that the id is
  4784. * unique.
  4785. */
  4786. if (s->flags & SLAB_CACHE_DMA)
  4787. *p++ = 'd';
  4788. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  4789. *p++ = 'a';
  4790. if (s->flags & SLAB_CONSISTENCY_CHECKS)
  4791. *p++ = 'F';
  4792. if (s->flags & SLAB_ACCOUNT)
  4793. *p++ = 'A';
  4794. if (p != name + 1)
  4795. *p++ = '-';
  4796. p += sprintf(p, "%07u", s->size);
  4797. BUG_ON(p > name + ID_STR_LENGTH - 1);
  4798. return name;
  4799. }
  4800. static void sysfs_slab_remove_workfn(struct work_struct *work)
  4801. {
  4802. struct kmem_cache *s =
  4803. container_of(work, struct kmem_cache, kobj_remove_work);
  4804. if (!s->kobj.state_in_sysfs)
  4805. /*
  4806. * For a memcg cache, this may be called during
  4807. * deactivation and again on shutdown. Remove only once.
  4808. * A cache is never shut down before deactivation is
  4809. * complete, so no need to worry about synchronization.
  4810. */
  4811. goto out;
  4812. #ifdef CONFIG_MEMCG
  4813. kset_unregister(s->memcg_kset);
  4814. #endif
  4815. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4816. out:
  4817. kobject_put(&s->kobj);
  4818. }
  4819. static int sysfs_slab_add(struct kmem_cache *s)
  4820. {
  4821. int err;
  4822. const char *name;
  4823. struct kset *kset = cache_kset(s);
  4824. int unmergeable = slab_unmergeable(s);
  4825. INIT_WORK(&s->kobj_remove_work, sysfs_slab_remove_workfn);
  4826. if (!kset) {
  4827. kobject_init(&s->kobj, &slab_ktype);
  4828. return 0;
  4829. }
  4830. if (!unmergeable && disable_higher_order_debug &&
  4831. (slub_debug & DEBUG_METADATA_FLAGS))
  4832. unmergeable = 1;
  4833. if (unmergeable) {
  4834. /*
  4835. * Slabcache can never be merged so we can use the name proper.
  4836. * This is typically the case for debug situations. In that
  4837. * case we can catch duplicate names easily.
  4838. */
  4839. sysfs_remove_link(&slab_kset->kobj, s->name);
  4840. name = s->name;
  4841. } else {
  4842. /*
  4843. * Create a unique name for the slab as a target
  4844. * for the symlinks.
  4845. */
  4846. name = create_unique_id(s);
  4847. }
  4848. s->kobj.kset = kset;
  4849. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, "%s", name);
  4850. if (err)
  4851. goto out;
  4852. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4853. if (err)
  4854. goto out_del_kobj;
  4855. #ifdef CONFIG_MEMCG
  4856. if (is_root_cache(s) && memcg_sysfs_enabled) {
  4857. s->memcg_kset = kset_create_and_add("cgroup", NULL, &s->kobj);
  4858. if (!s->memcg_kset) {
  4859. err = -ENOMEM;
  4860. goto out_del_kobj;
  4861. }
  4862. }
  4863. #endif
  4864. kobject_uevent(&s->kobj, KOBJ_ADD);
  4865. if (!unmergeable) {
  4866. /* Setup first alias */
  4867. sysfs_slab_alias(s, s->name);
  4868. }
  4869. out:
  4870. if (!unmergeable)
  4871. kfree(name);
  4872. return err;
  4873. out_del_kobj:
  4874. kobject_del(&s->kobj);
  4875. goto out;
  4876. }
  4877. static void sysfs_slab_remove(struct kmem_cache *s)
  4878. {
  4879. if (slab_state < FULL)
  4880. /*
  4881. * Sysfs has not been setup yet so no need to remove the
  4882. * cache from sysfs.
  4883. */
  4884. return;
  4885. kobject_get(&s->kobj);
  4886. schedule_work(&s->kobj_remove_work);
  4887. }
  4888. void sysfs_slab_unlink(struct kmem_cache *s)
  4889. {
  4890. if (slab_state >= FULL)
  4891. kobject_del(&s->kobj);
  4892. }
  4893. void sysfs_slab_release(struct kmem_cache *s)
  4894. {
  4895. if (slab_state >= FULL)
  4896. kobject_put(&s->kobj);
  4897. }
  4898. /*
  4899. * Need to buffer aliases during bootup until sysfs becomes
  4900. * available lest we lose that information.
  4901. */
  4902. struct saved_alias {
  4903. struct kmem_cache *s;
  4904. const char *name;
  4905. struct saved_alias *next;
  4906. };
  4907. static struct saved_alias *alias_list;
  4908. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4909. {
  4910. struct saved_alias *al;
  4911. if (slab_state == FULL) {
  4912. /*
  4913. * If we have a leftover link then remove it.
  4914. */
  4915. sysfs_remove_link(&slab_kset->kobj, name);
  4916. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4917. }
  4918. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4919. if (!al)
  4920. return -ENOMEM;
  4921. al->s = s;
  4922. al->name = name;
  4923. al->next = alias_list;
  4924. alias_list = al;
  4925. return 0;
  4926. }
  4927. static int __init slab_sysfs_init(void)
  4928. {
  4929. struct kmem_cache *s;
  4930. int err;
  4931. mutex_lock(&slab_mutex);
  4932. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4933. if (!slab_kset) {
  4934. mutex_unlock(&slab_mutex);
  4935. pr_err("Cannot register slab subsystem.\n");
  4936. return -ENOSYS;
  4937. }
  4938. slab_state = FULL;
  4939. list_for_each_entry(s, &slab_caches, list) {
  4940. err = sysfs_slab_add(s);
  4941. if (err)
  4942. pr_err("SLUB: Unable to add boot slab %s to sysfs\n",
  4943. s->name);
  4944. }
  4945. while (alias_list) {
  4946. struct saved_alias *al = alias_list;
  4947. alias_list = alias_list->next;
  4948. err = sysfs_slab_alias(al->s, al->name);
  4949. if (err)
  4950. pr_err("SLUB: Unable to add boot slab alias %s to sysfs\n",
  4951. al->name);
  4952. kfree(al);
  4953. }
  4954. mutex_unlock(&slab_mutex);
  4955. resiliency_test();
  4956. return 0;
  4957. }
  4958. __initcall(slab_sysfs_init);
  4959. #endif /* CONFIG_SYSFS */
  4960. /*
  4961. * The /proc/slabinfo ABI
  4962. */
  4963. #ifdef CONFIG_SLUB_DEBUG
  4964. void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo)
  4965. {
  4966. unsigned long nr_slabs = 0;
  4967. unsigned long nr_objs = 0;
  4968. unsigned long nr_free = 0;
  4969. int node;
  4970. struct kmem_cache_node *n;
  4971. for_each_kmem_cache_node(s, node, n) {
  4972. nr_slabs += node_nr_slabs(n);
  4973. nr_objs += node_nr_objs(n);
  4974. nr_free += count_partial(n, count_free);
  4975. }
  4976. sinfo->active_objs = nr_objs - nr_free;
  4977. sinfo->num_objs = nr_objs;
  4978. sinfo->active_slabs = nr_slabs;
  4979. sinfo->num_slabs = nr_slabs;
  4980. sinfo->objects_per_slab = oo_objects(s->oo);
  4981. sinfo->cache_order = oo_order(s->oo);
  4982. }
  4983. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s)
  4984. {
  4985. }
  4986. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  4987. size_t count, loff_t *ppos)
  4988. {
  4989. return -EIO;
  4990. }
  4991. #endif /* CONFIG_SLUB_DEBUG */