slub.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117
  1. /*
  2. * SLUB: A slab allocator that limits cache line use instead of queuing
  3. * objects in per cpu and per node lists.
  4. *
  5. * The allocator synchronizes using per slab locks or atomic operatios
  6. * and only uses a centralized lock to manage a pool of partial slabs.
  7. *
  8. * (C) 2007 SGI, Christoph Lameter
  9. * (C) 2011 Linux Foundation, Christoph Lameter
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/swap.h> /* struct reclaim_state */
  13. #include <linux/module.h>
  14. #include <linux/bit_spinlock.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/bitops.h>
  17. #include <linux/slab.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/kmemcheck.h>
  21. #include <linux/cpu.h>
  22. #include <linux/cpuset.h>
  23. #include <linux/mempolicy.h>
  24. #include <linux/ctype.h>
  25. #include <linux/debugobjects.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/memory.h>
  28. #include <linux/math64.h>
  29. #include <linux/fault-inject.h>
  30. #include <trace/events/kmem.h>
  31. /*
  32. * Lock order:
  33. * 1. slub_lock (Global Semaphore)
  34. * 2. node->list_lock
  35. * 3. slab_lock(page) (Only on some arches and for debugging)
  36. *
  37. * slub_lock
  38. *
  39. * The role of the slub_lock is to protect the list of all the slabs
  40. * and to synchronize major metadata changes to slab cache structures.
  41. *
  42. * The slab_lock is only used for debugging and on arches that do not
  43. * have the ability to do a cmpxchg_double. It only protects the second
  44. * double word in the page struct. Meaning
  45. * A. page->freelist -> List of object free in a page
  46. * B. page->counters -> Counters of objects
  47. * C. page->frozen -> frozen state
  48. *
  49. * If a slab is frozen then it is exempt from list management. It is not
  50. * on any list. The processor that froze the slab is the one who can
  51. * perform list operations on the page. Other processors may put objects
  52. * onto the freelist but the processor that froze the slab is the only
  53. * one that can retrieve the objects from the page's freelist.
  54. *
  55. * The list_lock protects the partial and full list on each node and
  56. * the partial slab counter. If taken then no new slabs may be added or
  57. * removed from the lists nor make the number of partial slabs be modified.
  58. * (Note that the total number of slabs is an atomic value that may be
  59. * modified without taking the list lock).
  60. *
  61. * The list_lock is a centralized lock and thus we avoid taking it as
  62. * much as possible. As long as SLUB does not have to handle partial
  63. * slabs, operations can continue without any centralized lock. F.e.
  64. * allocating a long series of objects that fill up slabs does not require
  65. * the list lock.
  66. * Interrupts are disabled during allocation and deallocation in order to
  67. * make the slab allocator safe to use in the context of an irq. In addition
  68. * interrupts are disabled to ensure that the processor does not change
  69. * while handling per_cpu slabs, due to kernel preemption.
  70. *
  71. * SLUB assigns one slab for allocation to each processor.
  72. * Allocations only occur from these slabs called cpu slabs.
  73. *
  74. * Slabs with free elements are kept on a partial list and during regular
  75. * operations no list for full slabs is used. If an object in a full slab is
  76. * freed then the slab will show up again on the partial lists.
  77. * We track full slabs for debugging purposes though because otherwise we
  78. * cannot scan all objects.
  79. *
  80. * Slabs are freed when they become empty. Teardown and setup is
  81. * minimal so we rely on the page allocators per cpu caches for
  82. * fast frees and allocs.
  83. *
  84. * Overloading of page flags that are otherwise used for LRU management.
  85. *
  86. * PageActive The slab is frozen and exempt from list processing.
  87. * This means that the slab is dedicated to a purpose
  88. * such as satisfying allocations for a specific
  89. * processor. Objects may be freed in the slab while
  90. * it is frozen but slab_free will then skip the usual
  91. * list operations. It is up to the processor holding
  92. * the slab to integrate the slab into the slab lists
  93. * when the slab is no longer needed.
  94. *
  95. * One use of this flag is to mark slabs that are
  96. * used for allocations. Then such a slab becomes a cpu
  97. * slab. The cpu slab may be equipped with an additional
  98. * freelist that allows lockless access to
  99. * free objects in addition to the regular freelist
  100. * that requires the slab lock.
  101. *
  102. * PageError Slab requires special handling due to debug
  103. * options set. This moves slab handling out of
  104. * the fast path and disables lockless freelists.
  105. */
  106. #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  107. SLAB_TRACE | SLAB_DEBUG_FREE)
  108. static inline int kmem_cache_debug(struct kmem_cache *s)
  109. {
  110. #ifdef CONFIG_SLUB_DEBUG
  111. return unlikely(s->flags & SLAB_DEBUG_FLAGS);
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. /*
  117. * Issues still to be resolved:
  118. *
  119. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  120. *
  121. * - Variable sizing of the per node arrays
  122. */
  123. /* Enable to test recovery from slab corruption on boot */
  124. #undef SLUB_RESILIENCY_TEST
  125. /* Enable to log cmpxchg failures */
  126. #undef SLUB_DEBUG_CMPXCHG
  127. /*
  128. * Mininum number of partial slabs. These will be left on the partial
  129. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  130. */
  131. #define MIN_PARTIAL 5
  132. /*
  133. * Maximum number of desirable partial slabs.
  134. * The existence of more partial slabs makes kmem_cache_shrink
  135. * sort the partial list by the number of objects in the.
  136. */
  137. #define MAX_PARTIAL 10
  138. #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
  139. SLAB_POISON | SLAB_STORE_USER)
  140. /*
  141. * Debugging flags that require metadata to be stored in the slab. These get
  142. * disabled when slub_debug=O is used and a cache's min order increases with
  143. * metadata.
  144. */
  145. #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  146. /*
  147. * Set of flags that will prevent slab merging
  148. */
  149. #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  150. SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
  151. SLAB_FAILSLAB)
  152. #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  153. SLAB_CACHE_DMA | SLAB_NOTRACK)
  154. #define OO_SHIFT 16
  155. #define OO_MASK ((1 << OO_SHIFT) - 1)
  156. #define MAX_OBJS_PER_PAGE 32767 /* since page.objects is u15 */
  157. /* Internal SLUB flags */
  158. #define __OBJECT_POISON 0x80000000UL /* Poison object */
  159. #define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
  160. static int kmem_size = sizeof(struct kmem_cache);
  161. #ifdef CONFIG_SMP
  162. static struct notifier_block slab_notifier;
  163. #endif
  164. static enum {
  165. DOWN, /* No slab functionality available */
  166. PARTIAL, /* Kmem_cache_node works */
  167. UP, /* Everything works but does not show up in sysfs */
  168. SYSFS /* Sysfs up */
  169. } slab_state = DOWN;
  170. /* A list of all slab caches on the system */
  171. static DECLARE_RWSEM(slub_lock);
  172. static LIST_HEAD(slab_caches);
  173. /*
  174. * Tracking user of a slab.
  175. */
  176. struct track {
  177. unsigned long addr; /* Called from address */
  178. int cpu; /* Was running on cpu */
  179. int pid; /* Pid context */
  180. unsigned long when; /* When did the operation occur */
  181. };
  182. enum track_item { TRACK_ALLOC, TRACK_FREE };
  183. #ifdef CONFIG_SYSFS
  184. static int sysfs_slab_add(struct kmem_cache *);
  185. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  186. static void sysfs_slab_remove(struct kmem_cache *);
  187. #else
  188. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  189. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  190. { return 0; }
  191. static inline void sysfs_slab_remove(struct kmem_cache *s)
  192. {
  193. kfree(s->name);
  194. kfree(s);
  195. }
  196. #endif
  197. static inline void stat(const struct kmem_cache *s, enum stat_item si)
  198. {
  199. #ifdef CONFIG_SLUB_STATS
  200. __this_cpu_inc(s->cpu_slab->stat[si]);
  201. #endif
  202. }
  203. /********************************************************************
  204. * Core slab cache functions
  205. *******************************************************************/
  206. int slab_is_available(void)
  207. {
  208. return slab_state >= UP;
  209. }
  210. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  211. {
  212. return s->node[node];
  213. }
  214. /* Verify that a pointer has an address that is valid within a slab page */
  215. static inline int check_valid_pointer(struct kmem_cache *s,
  216. struct page *page, const void *object)
  217. {
  218. void *base;
  219. if (!object)
  220. return 1;
  221. base = page_address(page);
  222. if (object < base || object >= base + page->objects * s->size ||
  223. (object - base) % s->size) {
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  229. {
  230. return *(void **)(object + s->offset);
  231. }
  232. static inline void *get_freepointer_safe(struct kmem_cache *s, void *object)
  233. {
  234. void *p;
  235. #ifdef CONFIG_DEBUG_PAGEALLOC
  236. probe_kernel_read(&p, (void **)(object + s->offset), sizeof(p));
  237. #else
  238. p = get_freepointer(s, object);
  239. #endif
  240. return p;
  241. }
  242. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  243. {
  244. *(void **)(object + s->offset) = fp;
  245. }
  246. /* Loop over all objects in a slab */
  247. #define for_each_object(__p, __s, __addr, __objects) \
  248. for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
  249. __p += (__s)->size)
  250. /* Determine object index from a given position */
  251. static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
  252. {
  253. return (p - addr) / s->size;
  254. }
  255. static inline size_t slab_ksize(const struct kmem_cache *s)
  256. {
  257. #ifdef CONFIG_SLUB_DEBUG
  258. /*
  259. * Debugging requires use of the padding between object
  260. * and whatever may come after it.
  261. */
  262. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  263. return s->objsize;
  264. #endif
  265. /*
  266. * If we have the need to store the freelist pointer
  267. * back there or track user information then we can
  268. * only use the space before that information.
  269. */
  270. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  271. return s->inuse;
  272. /*
  273. * Else we can use all the padding etc for the allocation
  274. */
  275. return s->size;
  276. }
  277. static inline int order_objects(int order, unsigned long size, int reserved)
  278. {
  279. return ((PAGE_SIZE << order) - reserved) / size;
  280. }
  281. static inline struct kmem_cache_order_objects oo_make(int order,
  282. unsigned long size, int reserved)
  283. {
  284. struct kmem_cache_order_objects x = {
  285. (order << OO_SHIFT) + order_objects(order, size, reserved)
  286. };
  287. return x;
  288. }
  289. static inline int oo_order(struct kmem_cache_order_objects x)
  290. {
  291. return x.x >> OO_SHIFT;
  292. }
  293. static inline int oo_objects(struct kmem_cache_order_objects x)
  294. {
  295. return x.x & OO_MASK;
  296. }
  297. /*
  298. * Per slab locking using the pagelock
  299. */
  300. static __always_inline void slab_lock(struct page *page)
  301. {
  302. bit_spin_lock(PG_locked, &page->flags);
  303. }
  304. static __always_inline void slab_unlock(struct page *page)
  305. {
  306. __bit_spin_unlock(PG_locked, &page->flags);
  307. }
  308. static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
  309. void *freelist_old, unsigned long counters_old,
  310. void *freelist_new, unsigned long counters_new,
  311. const char *n)
  312. {
  313. #ifdef CONFIG_CMPXCHG_DOUBLE
  314. if (s->flags & __CMPXCHG_DOUBLE) {
  315. if (cmpxchg_double(&page->freelist,
  316. freelist_old, counters_old,
  317. freelist_new, counters_new))
  318. return 1;
  319. } else
  320. #endif
  321. {
  322. slab_lock(page);
  323. if (page->freelist == freelist_old && page->counters == counters_old) {
  324. page->freelist = freelist_new;
  325. page->counters = counters_new;
  326. slab_unlock(page);
  327. return 1;
  328. }
  329. slab_unlock(page);
  330. }
  331. cpu_relax();
  332. stat(s, CMPXCHG_DOUBLE_FAIL);
  333. #ifdef SLUB_DEBUG_CMPXCHG
  334. printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
  335. #endif
  336. return 0;
  337. }
  338. #ifdef CONFIG_SLUB_DEBUG
  339. /*
  340. * Determine a map of object in use on a page.
  341. *
  342. * Node listlock must be held to guarantee that the page does
  343. * not vanish from under us.
  344. */
  345. static void get_map(struct kmem_cache *s, struct page *page, unsigned long *map)
  346. {
  347. void *p;
  348. void *addr = page_address(page);
  349. for (p = page->freelist; p; p = get_freepointer(s, p))
  350. set_bit(slab_index(p, s, addr), map);
  351. }
  352. /*
  353. * Debug settings:
  354. */
  355. #ifdef CONFIG_SLUB_DEBUG_ON
  356. static int slub_debug = DEBUG_DEFAULT_FLAGS;
  357. #else
  358. static int slub_debug;
  359. #endif
  360. static char *slub_debug_slabs;
  361. static int disable_higher_order_debug;
  362. /*
  363. * Object debugging
  364. */
  365. static void print_section(char *text, u8 *addr, unsigned int length)
  366. {
  367. int i, offset;
  368. int newline = 1;
  369. char ascii[17];
  370. ascii[16] = 0;
  371. for (i = 0; i < length; i++) {
  372. if (newline) {
  373. printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
  374. newline = 0;
  375. }
  376. printk(KERN_CONT " %02x", addr[i]);
  377. offset = i % 16;
  378. ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
  379. if (offset == 15) {
  380. printk(KERN_CONT " %s\n", ascii);
  381. newline = 1;
  382. }
  383. }
  384. if (!newline) {
  385. i %= 16;
  386. while (i < 16) {
  387. printk(KERN_CONT " ");
  388. ascii[i] = ' ';
  389. i++;
  390. }
  391. printk(KERN_CONT " %s\n", ascii);
  392. }
  393. }
  394. static struct track *get_track(struct kmem_cache *s, void *object,
  395. enum track_item alloc)
  396. {
  397. struct track *p;
  398. if (s->offset)
  399. p = object + s->offset + sizeof(void *);
  400. else
  401. p = object + s->inuse;
  402. return p + alloc;
  403. }
  404. static void set_track(struct kmem_cache *s, void *object,
  405. enum track_item alloc, unsigned long addr)
  406. {
  407. struct track *p = get_track(s, object, alloc);
  408. if (addr) {
  409. p->addr = addr;
  410. p->cpu = smp_processor_id();
  411. p->pid = current->pid;
  412. p->when = jiffies;
  413. } else
  414. memset(p, 0, sizeof(struct track));
  415. }
  416. static void init_tracking(struct kmem_cache *s, void *object)
  417. {
  418. if (!(s->flags & SLAB_STORE_USER))
  419. return;
  420. set_track(s, object, TRACK_FREE, 0UL);
  421. set_track(s, object, TRACK_ALLOC, 0UL);
  422. }
  423. static void print_track(const char *s, struct track *t)
  424. {
  425. if (!t->addr)
  426. return;
  427. printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
  428. s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
  429. }
  430. static void print_tracking(struct kmem_cache *s, void *object)
  431. {
  432. if (!(s->flags & SLAB_STORE_USER))
  433. return;
  434. print_track("Allocated", get_track(s, object, TRACK_ALLOC));
  435. print_track("Freed", get_track(s, object, TRACK_FREE));
  436. }
  437. static void print_page_info(struct page *page)
  438. {
  439. printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
  440. page, page->objects, page->inuse, page->freelist, page->flags);
  441. }
  442. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  443. {
  444. va_list args;
  445. char buf[100];
  446. va_start(args, fmt);
  447. vsnprintf(buf, sizeof(buf), fmt, args);
  448. va_end(args);
  449. printk(KERN_ERR "========================================"
  450. "=====================================\n");
  451. printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
  452. printk(KERN_ERR "----------------------------------------"
  453. "-------------------------------------\n\n");
  454. }
  455. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  456. {
  457. va_list args;
  458. char buf[100];
  459. va_start(args, fmt);
  460. vsnprintf(buf, sizeof(buf), fmt, args);
  461. va_end(args);
  462. printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
  463. }
  464. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  465. {
  466. unsigned int off; /* Offset of last byte */
  467. u8 *addr = page_address(page);
  468. print_tracking(s, p);
  469. print_page_info(page);
  470. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  471. p, p - addr, get_freepointer(s, p));
  472. if (p > addr + 16)
  473. print_section("Bytes b4", p - 16, 16);
  474. print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
  475. if (s->flags & SLAB_RED_ZONE)
  476. print_section("Redzone", p + s->objsize,
  477. s->inuse - s->objsize);
  478. if (s->offset)
  479. off = s->offset + sizeof(void *);
  480. else
  481. off = s->inuse;
  482. if (s->flags & SLAB_STORE_USER)
  483. off += 2 * sizeof(struct track);
  484. if (off != s->size)
  485. /* Beginning of the filler is the free pointer */
  486. print_section("Padding", p + off, s->size - off);
  487. dump_stack();
  488. }
  489. static void object_err(struct kmem_cache *s, struct page *page,
  490. u8 *object, char *reason)
  491. {
  492. slab_bug(s, "%s", reason);
  493. print_trailer(s, page, object);
  494. }
  495. static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
  496. {
  497. va_list args;
  498. char buf[100];
  499. va_start(args, fmt);
  500. vsnprintf(buf, sizeof(buf), fmt, args);
  501. va_end(args);
  502. slab_bug(s, "%s", buf);
  503. print_page_info(page);
  504. dump_stack();
  505. }
  506. static void init_object(struct kmem_cache *s, void *object, u8 val)
  507. {
  508. u8 *p = object;
  509. if (s->flags & __OBJECT_POISON) {
  510. memset(p, POISON_FREE, s->objsize - 1);
  511. p[s->objsize - 1] = POISON_END;
  512. }
  513. if (s->flags & SLAB_RED_ZONE)
  514. memset(p + s->objsize, val, s->inuse - s->objsize);
  515. }
  516. static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  517. {
  518. while (bytes) {
  519. if (*start != (u8)value)
  520. return start;
  521. start++;
  522. bytes--;
  523. }
  524. return NULL;
  525. }
  526. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  527. void *from, void *to)
  528. {
  529. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  530. memset(from, data, to - from);
  531. }
  532. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  533. u8 *object, char *what,
  534. u8 *start, unsigned int value, unsigned int bytes)
  535. {
  536. u8 *fault;
  537. u8 *end;
  538. fault = check_bytes(start, value, bytes);
  539. if (!fault)
  540. return 1;
  541. end = start + bytes;
  542. while (end > fault && end[-1] == value)
  543. end--;
  544. slab_bug(s, "%s overwritten", what);
  545. printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  546. fault, end - 1, fault[0], value);
  547. print_trailer(s, page, object);
  548. restore_bytes(s, what, value, fault, end);
  549. return 0;
  550. }
  551. /*
  552. * Object layout:
  553. *
  554. * object address
  555. * Bytes of the object to be managed.
  556. * If the freepointer may overlay the object then the free
  557. * pointer is the first word of the object.
  558. *
  559. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  560. * 0xa5 (POISON_END)
  561. *
  562. * object + s->objsize
  563. * Padding to reach word boundary. This is also used for Redzoning.
  564. * Padding is extended by another word if Redzoning is enabled and
  565. * objsize == inuse.
  566. *
  567. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  568. * 0xcc (RED_ACTIVE) for objects in use.
  569. *
  570. * object + s->inuse
  571. * Meta data starts here.
  572. *
  573. * A. Free pointer (if we cannot overwrite object on free)
  574. * B. Tracking data for SLAB_STORE_USER
  575. * C. Padding to reach required alignment boundary or at mininum
  576. * one word if debugging is on to be able to detect writes
  577. * before the word boundary.
  578. *
  579. * Padding is done using 0x5a (POISON_INUSE)
  580. *
  581. * object + s->size
  582. * Nothing is used beyond s->size.
  583. *
  584. * If slabcaches are merged then the objsize and inuse boundaries are mostly
  585. * ignored. And therefore no slab options that rely on these boundaries
  586. * may be used with merged slabcaches.
  587. */
  588. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  589. {
  590. unsigned long off = s->inuse; /* The end of info */
  591. if (s->offset)
  592. /* Freepointer is placed after the object. */
  593. off += sizeof(void *);
  594. if (s->flags & SLAB_STORE_USER)
  595. /* We also have user information there */
  596. off += 2 * sizeof(struct track);
  597. if (s->size == off)
  598. return 1;
  599. return check_bytes_and_report(s, page, p, "Object padding",
  600. p + off, POISON_INUSE, s->size - off);
  601. }
  602. /* Check the pad bytes at the end of a slab page */
  603. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  604. {
  605. u8 *start;
  606. u8 *fault;
  607. u8 *end;
  608. int length;
  609. int remainder;
  610. if (!(s->flags & SLAB_POISON))
  611. return 1;
  612. start = page_address(page);
  613. length = (PAGE_SIZE << compound_order(page)) - s->reserved;
  614. end = start + length;
  615. remainder = length % s->size;
  616. if (!remainder)
  617. return 1;
  618. fault = check_bytes(end - remainder, POISON_INUSE, remainder);
  619. if (!fault)
  620. return 1;
  621. while (end > fault && end[-1] == POISON_INUSE)
  622. end--;
  623. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  624. print_section("Padding", end - remainder, remainder);
  625. restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
  626. return 0;
  627. }
  628. static int check_object(struct kmem_cache *s, struct page *page,
  629. void *object, u8 val)
  630. {
  631. u8 *p = object;
  632. u8 *endobject = object + s->objsize;
  633. if (s->flags & SLAB_RED_ZONE) {
  634. if (!check_bytes_and_report(s, page, object, "Redzone",
  635. endobject, val, s->inuse - s->objsize))
  636. return 0;
  637. } else {
  638. if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
  639. check_bytes_and_report(s, page, p, "Alignment padding",
  640. endobject, POISON_INUSE, s->inuse - s->objsize);
  641. }
  642. }
  643. if (s->flags & SLAB_POISON) {
  644. if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) &&
  645. (!check_bytes_and_report(s, page, p, "Poison", p,
  646. POISON_FREE, s->objsize - 1) ||
  647. !check_bytes_and_report(s, page, p, "Poison",
  648. p + s->objsize - 1, POISON_END, 1)))
  649. return 0;
  650. /*
  651. * check_pad_bytes cleans up on its own.
  652. */
  653. check_pad_bytes(s, page, p);
  654. }
  655. if (!s->offset && val == SLUB_RED_ACTIVE)
  656. /*
  657. * Object and freepointer overlap. Cannot check
  658. * freepointer while object is allocated.
  659. */
  660. return 1;
  661. /* Check free pointer validity */
  662. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  663. object_err(s, page, p, "Freepointer corrupt");
  664. /*
  665. * No choice but to zap it and thus lose the remainder
  666. * of the free objects in this slab. May cause
  667. * another error because the object count is now wrong.
  668. */
  669. set_freepointer(s, p, NULL);
  670. return 0;
  671. }
  672. return 1;
  673. }
  674. static int check_slab(struct kmem_cache *s, struct page *page)
  675. {
  676. int maxobj;
  677. VM_BUG_ON(!irqs_disabled());
  678. if (!PageSlab(page)) {
  679. slab_err(s, page, "Not a valid slab page");
  680. return 0;
  681. }
  682. maxobj = order_objects(compound_order(page), s->size, s->reserved);
  683. if (page->objects > maxobj) {
  684. slab_err(s, page, "objects %u > max %u",
  685. s->name, page->objects, maxobj);
  686. return 0;
  687. }
  688. if (page->inuse > page->objects) {
  689. slab_err(s, page, "inuse %u > max %u",
  690. s->name, page->inuse, page->objects);
  691. return 0;
  692. }
  693. /* Slab_pad_check fixes things up after itself */
  694. slab_pad_check(s, page);
  695. return 1;
  696. }
  697. /*
  698. * Determine if a certain object on a page is on the freelist. Must hold the
  699. * slab lock to guarantee that the chains are in a consistent state.
  700. */
  701. static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
  702. {
  703. int nr = 0;
  704. void *fp;
  705. void *object = NULL;
  706. unsigned long max_objects;
  707. fp = page->freelist;
  708. while (fp && nr <= page->objects) {
  709. if (fp == search)
  710. return 1;
  711. if (!check_valid_pointer(s, page, fp)) {
  712. if (object) {
  713. object_err(s, page, object,
  714. "Freechain corrupt");
  715. set_freepointer(s, object, NULL);
  716. break;
  717. } else {
  718. slab_err(s, page, "Freepointer corrupt");
  719. page->freelist = NULL;
  720. page->inuse = page->objects;
  721. slab_fix(s, "Freelist cleared");
  722. return 0;
  723. }
  724. break;
  725. }
  726. object = fp;
  727. fp = get_freepointer(s, object);
  728. nr++;
  729. }
  730. max_objects = order_objects(compound_order(page), s->size, s->reserved);
  731. if (max_objects > MAX_OBJS_PER_PAGE)
  732. max_objects = MAX_OBJS_PER_PAGE;
  733. if (page->objects != max_objects) {
  734. slab_err(s, page, "Wrong number of objects. Found %d but "
  735. "should be %d", page->objects, max_objects);
  736. page->objects = max_objects;
  737. slab_fix(s, "Number of objects adjusted.");
  738. }
  739. if (page->inuse != page->objects - nr) {
  740. slab_err(s, page, "Wrong object count. Counter is %d but "
  741. "counted were %d", page->inuse, page->objects - nr);
  742. page->inuse = page->objects - nr;
  743. slab_fix(s, "Object count adjusted.");
  744. }
  745. return search == NULL;
  746. }
  747. static void trace(struct kmem_cache *s, struct page *page, void *object,
  748. int alloc)
  749. {
  750. if (s->flags & SLAB_TRACE) {
  751. printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  752. s->name,
  753. alloc ? "alloc" : "free",
  754. object, page->inuse,
  755. page->freelist);
  756. if (!alloc)
  757. print_section("Object", (void *)object, s->objsize);
  758. dump_stack();
  759. }
  760. }
  761. /*
  762. * Hooks for other subsystems that check memory allocations. In a typical
  763. * production configuration these hooks all should produce no code at all.
  764. */
  765. static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
  766. {
  767. flags &= gfp_allowed_mask;
  768. lockdep_trace_alloc(flags);
  769. might_sleep_if(flags & __GFP_WAIT);
  770. return should_failslab(s->objsize, flags, s->flags);
  771. }
  772. static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags, void *object)
  773. {
  774. flags &= gfp_allowed_mask;
  775. kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
  776. kmemleak_alloc_recursive(object, s->objsize, 1, s->flags, flags);
  777. }
  778. static inline void slab_free_hook(struct kmem_cache *s, void *x)
  779. {
  780. kmemleak_free_recursive(x, s->flags);
  781. /*
  782. * Trouble is that we may no longer disable interupts in the fast path
  783. * So in order to make the debug calls that expect irqs to be
  784. * disabled we need to disable interrupts temporarily.
  785. */
  786. #if defined(CONFIG_KMEMCHECK) || defined(CONFIG_LOCKDEP)
  787. {
  788. unsigned long flags;
  789. local_irq_save(flags);
  790. kmemcheck_slab_free(s, x, s->objsize);
  791. debug_check_no_locks_freed(x, s->objsize);
  792. local_irq_restore(flags);
  793. }
  794. #endif
  795. if (!(s->flags & SLAB_DEBUG_OBJECTS))
  796. debug_check_no_obj_freed(x, s->objsize);
  797. }
  798. /*
  799. * Tracking of fully allocated slabs for debugging purposes.
  800. *
  801. * list_lock must be held.
  802. */
  803. static void add_full(struct kmem_cache *s,
  804. struct kmem_cache_node *n, struct page *page)
  805. {
  806. if (!(s->flags & SLAB_STORE_USER))
  807. return;
  808. list_add(&page->lru, &n->full);
  809. }
  810. /*
  811. * list_lock must be held.
  812. */
  813. static void remove_full(struct kmem_cache *s, struct page *page)
  814. {
  815. if (!(s->flags & SLAB_STORE_USER))
  816. return;
  817. list_del(&page->lru);
  818. }
  819. /* Tracking of the number of slabs for debugging purposes */
  820. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  821. {
  822. struct kmem_cache_node *n = get_node(s, node);
  823. return atomic_long_read(&n->nr_slabs);
  824. }
  825. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  826. {
  827. return atomic_long_read(&n->nr_slabs);
  828. }
  829. static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
  830. {
  831. struct kmem_cache_node *n = get_node(s, node);
  832. /*
  833. * May be called early in order to allocate a slab for the
  834. * kmem_cache_node structure. Solve the chicken-egg
  835. * dilemma by deferring the increment of the count during
  836. * bootstrap (see early_kmem_cache_node_alloc).
  837. */
  838. if (n) {
  839. atomic_long_inc(&n->nr_slabs);
  840. atomic_long_add(objects, &n->total_objects);
  841. }
  842. }
  843. static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
  844. {
  845. struct kmem_cache_node *n = get_node(s, node);
  846. atomic_long_dec(&n->nr_slabs);
  847. atomic_long_sub(objects, &n->total_objects);
  848. }
  849. /* Object debug checks for alloc/free paths */
  850. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  851. void *object)
  852. {
  853. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  854. return;
  855. init_object(s, object, SLUB_RED_INACTIVE);
  856. init_tracking(s, object);
  857. }
  858. static noinline int alloc_debug_processing(struct kmem_cache *s, struct page *page,
  859. void *object, unsigned long addr)
  860. {
  861. if (!check_slab(s, page))
  862. goto bad;
  863. if (!check_valid_pointer(s, page, object)) {
  864. object_err(s, page, object, "Freelist Pointer check fails");
  865. goto bad;
  866. }
  867. if (!check_object(s, page, object, SLUB_RED_INACTIVE))
  868. goto bad;
  869. /* Success perform special debug activities for allocs */
  870. if (s->flags & SLAB_STORE_USER)
  871. set_track(s, object, TRACK_ALLOC, addr);
  872. trace(s, page, object, 1);
  873. init_object(s, object, SLUB_RED_ACTIVE);
  874. return 1;
  875. bad:
  876. if (PageSlab(page)) {
  877. /*
  878. * If this is a slab page then lets do the best we can
  879. * to avoid issues in the future. Marking all objects
  880. * as used avoids touching the remaining objects.
  881. */
  882. slab_fix(s, "Marking all objects used");
  883. page->inuse = page->objects;
  884. page->freelist = NULL;
  885. }
  886. return 0;
  887. }
  888. static noinline int free_debug_processing(struct kmem_cache *s,
  889. struct page *page, void *object, unsigned long addr)
  890. {
  891. unsigned long flags;
  892. int rc = 0;
  893. local_irq_save(flags);
  894. slab_lock(page);
  895. if (!check_slab(s, page))
  896. goto fail;
  897. if (!check_valid_pointer(s, page, object)) {
  898. slab_err(s, page, "Invalid object pointer 0x%p", object);
  899. goto fail;
  900. }
  901. if (on_freelist(s, page, object)) {
  902. object_err(s, page, object, "Object already free");
  903. goto fail;
  904. }
  905. if (!check_object(s, page, object, SLUB_RED_ACTIVE))
  906. goto out;
  907. if (unlikely(s != page->slab)) {
  908. if (!PageSlab(page)) {
  909. slab_err(s, page, "Attempt to free object(0x%p) "
  910. "outside of slab", object);
  911. } else if (!page->slab) {
  912. printk(KERN_ERR
  913. "SLUB <none>: no slab for object 0x%p.\n",
  914. object);
  915. dump_stack();
  916. } else
  917. object_err(s, page, object,
  918. "page slab pointer corrupt.");
  919. goto fail;
  920. }
  921. if (s->flags & SLAB_STORE_USER)
  922. set_track(s, object, TRACK_FREE, addr);
  923. trace(s, page, object, 0);
  924. init_object(s, object, SLUB_RED_INACTIVE);
  925. rc = 1;
  926. out:
  927. slab_unlock(page);
  928. local_irq_restore(flags);
  929. return rc;
  930. fail:
  931. slab_fix(s, "Object at 0x%p not freed", object);
  932. goto out;
  933. }
  934. static int __init setup_slub_debug(char *str)
  935. {
  936. slub_debug = DEBUG_DEFAULT_FLAGS;
  937. if (*str++ != '=' || !*str)
  938. /*
  939. * No options specified. Switch on full debugging.
  940. */
  941. goto out;
  942. if (*str == ',')
  943. /*
  944. * No options but restriction on slabs. This means full
  945. * debugging for slabs matching a pattern.
  946. */
  947. goto check_slabs;
  948. if (tolower(*str) == 'o') {
  949. /*
  950. * Avoid enabling debugging on caches if its minimum order
  951. * would increase as a result.
  952. */
  953. disable_higher_order_debug = 1;
  954. goto out;
  955. }
  956. slub_debug = 0;
  957. if (*str == '-')
  958. /*
  959. * Switch off all debugging measures.
  960. */
  961. goto out;
  962. /*
  963. * Determine which debug features should be switched on
  964. */
  965. for (; *str && *str != ','; str++) {
  966. switch (tolower(*str)) {
  967. case 'f':
  968. slub_debug |= SLAB_DEBUG_FREE;
  969. break;
  970. case 'z':
  971. slub_debug |= SLAB_RED_ZONE;
  972. break;
  973. case 'p':
  974. slub_debug |= SLAB_POISON;
  975. break;
  976. case 'u':
  977. slub_debug |= SLAB_STORE_USER;
  978. break;
  979. case 't':
  980. slub_debug |= SLAB_TRACE;
  981. break;
  982. case 'a':
  983. slub_debug |= SLAB_FAILSLAB;
  984. break;
  985. default:
  986. printk(KERN_ERR "slub_debug option '%c' "
  987. "unknown. skipped\n", *str);
  988. }
  989. }
  990. check_slabs:
  991. if (*str == ',')
  992. slub_debug_slabs = str + 1;
  993. out:
  994. return 1;
  995. }
  996. __setup("slub_debug", setup_slub_debug);
  997. static unsigned long kmem_cache_flags(unsigned long objsize,
  998. unsigned long flags, const char *name,
  999. void (*ctor)(void *))
  1000. {
  1001. /*
  1002. * Enable debugging if selected on the kernel commandline.
  1003. */
  1004. if (slub_debug && (!slub_debug_slabs ||
  1005. !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
  1006. flags |= slub_debug;
  1007. return flags;
  1008. }
  1009. #else
  1010. static inline void setup_object_debug(struct kmem_cache *s,
  1011. struct page *page, void *object) {}
  1012. static inline int alloc_debug_processing(struct kmem_cache *s,
  1013. struct page *page, void *object, unsigned long addr) { return 0; }
  1014. static inline int free_debug_processing(struct kmem_cache *s,
  1015. struct page *page, void *object, unsigned long addr) { return 0; }
  1016. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  1017. { return 1; }
  1018. static inline int check_object(struct kmem_cache *s, struct page *page,
  1019. void *object, u8 val) { return 1; }
  1020. static inline void add_full(struct kmem_cache *s, struct kmem_cache_node *n,
  1021. struct page *page) {}
  1022. static inline void remove_full(struct kmem_cache *s, struct page *page) {}
  1023. static inline unsigned long kmem_cache_flags(unsigned long objsize,
  1024. unsigned long flags, const char *name,
  1025. void (*ctor)(void *))
  1026. {
  1027. return flags;
  1028. }
  1029. #define slub_debug 0
  1030. #define disable_higher_order_debug 0
  1031. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  1032. { return 0; }
  1033. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  1034. { return 0; }
  1035. static inline void inc_slabs_node(struct kmem_cache *s, int node,
  1036. int objects) {}
  1037. static inline void dec_slabs_node(struct kmem_cache *s, int node,
  1038. int objects) {}
  1039. static inline int slab_pre_alloc_hook(struct kmem_cache *s, gfp_t flags)
  1040. { return 0; }
  1041. static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
  1042. void *object) {}
  1043. static inline void slab_free_hook(struct kmem_cache *s, void *x) {}
  1044. #endif /* CONFIG_SLUB_DEBUG */
  1045. /*
  1046. * Slab allocation and freeing
  1047. */
  1048. static inline struct page *alloc_slab_page(gfp_t flags, int node,
  1049. struct kmem_cache_order_objects oo)
  1050. {
  1051. int order = oo_order(oo);
  1052. flags |= __GFP_NOTRACK;
  1053. if (node == NUMA_NO_NODE)
  1054. return alloc_pages(flags, order);
  1055. else
  1056. return alloc_pages_exact_node(node, flags, order);
  1057. }
  1058. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  1059. {
  1060. struct page *page;
  1061. struct kmem_cache_order_objects oo = s->oo;
  1062. gfp_t alloc_gfp;
  1063. flags &= gfp_allowed_mask;
  1064. if (flags & __GFP_WAIT)
  1065. local_irq_enable();
  1066. flags |= s->allocflags;
  1067. /*
  1068. * Let the initial higher-order allocation fail under memory pressure
  1069. * so we fall-back to the minimum order allocation.
  1070. */
  1071. alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
  1072. page = alloc_slab_page(alloc_gfp, node, oo);
  1073. if (unlikely(!page)) {
  1074. oo = s->min;
  1075. /*
  1076. * Allocation may have failed due to fragmentation.
  1077. * Try a lower order alloc if possible
  1078. */
  1079. page = alloc_slab_page(flags, node, oo);
  1080. if (page)
  1081. stat(s, ORDER_FALLBACK);
  1082. }
  1083. if (flags & __GFP_WAIT)
  1084. local_irq_disable();
  1085. if (!page)
  1086. return NULL;
  1087. if (kmemcheck_enabled
  1088. && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  1089. int pages = 1 << oo_order(oo);
  1090. kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
  1091. /*
  1092. * Objects from caches that have a constructor don't get
  1093. * cleared when they're allocated, so we need to do it here.
  1094. */
  1095. if (s->ctor)
  1096. kmemcheck_mark_uninitialized_pages(page, pages);
  1097. else
  1098. kmemcheck_mark_unallocated_pages(page, pages);
  1099. }
  1100. page->objects = oo_objects(oo);
  1101. mod_zone_page_state(page_zone(page),
  1102. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  1103. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  1104. 1 << oo_order(oo));
  1105. return page;
  1106. }
  1107. static void setup_object(struct kmem_cache *s, struct page *page,
  1108. void *object)
  1109. {
  1110. setup_object_debug(s, page, object);
  1111. if (unlikely(s->ctor))
  1112. s->ctor(object);
  1113. }
  1114. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  1115. {
  1116. struct page *page;
  1117. void *start;
  1118. void *last;
  1119. void *p;
  1120. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  1121. page = allocate_slab(s,
  1122. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  1123. if (!page)
  1124. goto out;
  1125. inc_slabs_node(s, page_to_nid(page), page->objects);
  1126. page->slab = s;
  1127. page->flags |= 1 << PG_slab;
  1128. start = page_address(page);
  1129. if (unlikely(s->flags & SLAB_POISON))
  1130. memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
  1131. last = start;
  1132. for_each_object(p, s, start, page->objects) {
  1133. setup_object(s, page, last);
  1134. set_freepointer(s, last, p);
  1135. last = p;
  1136. }
  1137. setup_object(s, page, last);
  1138. set_freepointer(s, last, NULL);
  1139. page->freelist = start;
  1140. page->inuse = 0;
  1141. page->frozen = 1;
  1142. out:
  1143. return page;
  1144. }
  1145. static void __free_slab(struct kmem_cache *s, struct page *page)
  1146. {
  1147. int order = compound_order(page);
  1148. int pages = 1 << order;
  1149. if (kmem_cache_debug(s)) {
  1150. void *p;
  1151. slab_pad_check(s, page);
  1152. for_each_object(p, s, page_address(page),
  1153. page->objects)
  1154. check_object(s, page, p, SLUB_RED_INACTIVE);
  1155. }
  1156. kmemcheck_free_shadow(page, compound_order(page));
  1157. mod_zone_page_state(page_zone(page),
  1158. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  1159. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  1160. -pages);
  1161. __ClearPageSlab(page);
  1162. reset_page_mapcount(page);
  1163. if (current->reclaim_state)
  1164. current->reclaim_state->reclaimed_slab += pages;
  1165. __free_pages(page, order);
  1166. }
  1167. #define need_reserve_slab_rcu \
  1168. (sizeof(((struct page *)NULL)->lru) < sizeof(struct rcu_head))
  1169. static void rcu_free_slab(struct rcu_head *h)
  1170. {
  1171. struct page *page;
  1172. if (need_reserve_slab_rcu)
  1173. page = virt_to_head_page(h);
  1174. else
  1175. page = container_of((struct list_head *)h, struct page, lru);
  1176. __free_slab(page->slab, page);
  1177. }
  1178. static void free_slab(struct kmem_cache *s, struct page *page)
  1179. {
  1180. if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
  1181. struct rcu_head *head;
  1182. if (need_reserve_slab_rcu) {
  1183. int order = compound_order(page);
  1184. int offset = (PAGE_SIZE << order) - s->reserved;
  1185. VM_BUG_ON(s->reserved != sizeof(*head));
  1186. head = page_address(page) + offset;
  1187. } else {
  1188. /*
  1189. * RCU free overloads the RCU head over the LRU
  1190. */
  1191. head = (void *)&page->lru;
  1192. }
  1193. call_rcu(head, rcu_free_slab);
  1194. } else
  1195. __free_slab(s, page);
  1196. }
  1197. static void discard_slab(struct kmem_cache *s, struct page *page)
  1198. {
  1199. dec_slabs_node(s, page_to_nid(page), page->objects);
  1200. free_slab(s, page);
  1201. }
  1202. /*
  1203. * Management of partially allocated slabs.
  1204. *
  1205. * list_lock must be held.
  1206. */
  1207. static inline void add_partial(struct kmem_cache_node *n,
  1208. struct page *page, int tail)
  1209. {
  1210. n->nr_partial++;
  1211. if (tail)
  1212. list_add_tail(&page->lru, &n->partial);
  1213. else
  1214. list_add(&page->lru, &n->partial);
  1215. }
  1216. /*
  1217. * list_lock must be held.
  1218. */
  1219. static inline void remove_partial(struct kmem_cache_node *n,
  1220. struct page *page)
  1221. {
  1222. list_del(&page->lru);
  1223. n->nr_partial--;
  1224. }
  1225. /*
  1226. * Lock slab, remove from the partial list and put the object into the
  1227. * per cpu freelist.
  1228. *
  1229. * Must hold list_lock.
  1230. */
  1231. static inline int acquire_slab(struct kmem_cache *s,
  1232. struct kmem_cache_node *n, struct page *page)
  1233. {
  1234. void *freelist;
  1235. unsigned long counters;
  1236. struct page new;
  1237. /*
  1238. * Zap the freelist and set the frozen bit.
  1239. * The old freelist is the list of objects for the
  1240. * per cpu allocation list.
  1241. */
  1242. do {
  1243. freelist = page->freelist;
  1244. counters = page->counters;
  1245. new.counters = counters;
  1246. new.inuse = page->objects;
  1247. VM_BUG_ON(new.frozen);
  1248. new.frozen = 1;
  1249. } while (!cmpxchg_double_slab(s, page,
  1250. freelist, counters,
  1251. NULL, new.counters,
  1252. "lock and freeze"));
  1253. remove_partial(n, page);
  1254. if (freelist) {
  1255. /* Populate the per cpu freelist */
  1256. this_cpu_write(s->cpu_slab->freelist, freelist);
  1257. this_cpu_write(s->cpu_slab->page, page);
  1258. this_cpu_write(s->cpu_slab->node, page_to_nid(page));
  1259. return 1;
  1260. } else {
  1261. /*
  1262. * Slab page came from the wrong list. No object to allocate
  1263. * from. Put it onto the correct list and continue partial
  1264. * scan.
  1265. */
  1266. printk(KERN_ERR "SLUB: %s : Page without available objects on"
  1267. " partial list\n", s->name);
  1268. return 0;
  1269. }
  1270. }
  1271. /*
  1272. * Try to allocate a partial slab from a specific node.
  1273. */
  1274. static struct page *get_partial_node(struct kmem_cache *s,
  1275. struct kmem_cache_node *n)
  1276. {
  1277. struct page *page;
  1278. /*
  1279. * Racy check. If we mistakenly see no partial slabs then we
  1280. * just allocate an empty slab. If we mistakenly try to get a
  1281. * partial slab and there is none available then get_partials()
  1282. * will return NULL.
  1283. */
  1284. if (!n || !n->nr_partial)
  1285. return NULL;
  1286. spin_lock(&n->list_lock);
  1287. list_for_each_entry(page, &n->partial, lru)
  1288. if (acquire_slab(s, n, page))
  1289. goto out;
  1290. page = NULL;
  1291. out:
  1292. spin_unlock(&n->list_lock);
  1293. return page;
  1294. }
  1295. /*
  1296. * Get a page from somewhere. Search in increasing NUMA distances.
  1297. */
  1298. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
  1299. {
  1300. #ifdef CONFIG_NUMA
  1301. struct zonelist *zonelist;
  1302. struct zoneref *z;
  1303. struct zone *zone;
  1304. enum zone_type high_zoneidx = gfp_zone(flags);
  1305. struct page *page;
  1306. /*
  1307. * The defrag ratio allows a configuration of the tradeoffs between
  1308. * inter node defragmentation and node local allocations. A lower
  1309. * defrag_ratio increases the tendency to do local allocations
  1310. * instead of attempting to obtain partial slabs from other nodes.
  1311. *
  1312. * If the defrag_ratio is set to 0 then kmalloc() always
  1313. * returns node local objects. If the ratio is higher then kmalloc()
  1314. * may return off node objects because partial slabs are obtained
  1315. * from other nodes and filled up.
  1316. *
  1317. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1318. * defrag_ratio = 1000) then every (well almost) allocation will
  1319. * first attempt to defrag slab caches on other nodes. This means
  1320. * scanning over all nodes to look for partial slabs which may be
  1321. * expensive if we do it every time we are trying to find a slab
  1322. * with available objects.
  1323. */
  1324. if (!s->remote_node_defrag_ratio ||
  1325. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1326. return NULL;
  1327. get_mems_allowed();
  1328. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1329. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1330. struct kmem_cache_node *n;
  1331. n = get_node(s, zone_to_nid(zone));
  1332. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1333. n->nr_partial > s->min_partial) {
  1334. page = get_partial_node(s, n);
  1335. if (page) {
  1336. put_mems_allowed();
  1337. return page;
  1338. }
  1339. }
  1340. }
  1341. put_mems_allowed();
  1342. #endif
  1343. return NULL;
  1344. }
  1345. /*
  1346. * Get a partial page, lock it and return it.
  1347. */
  1348. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  1349. {
  1350. struct page *page;
  1351. int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
  1352. page = get_partial_node(s, get_node(s, searchnode));
  1353. if (page || node != NUMA_NO_NODE)
  1354. return page;
  1355. return get_any_partial(s, flags);
  1356. }
  1357. #ifdef CONFIG_PREEMPT
  1358. /*
  1359. * Calculate the next globally unique transaction for disambiguiation
  1360. * during cmpxchg. The transactions start with the cpu number and are then
  1361. * incremented by CONFIG_NR_CPUS.
  1362. */
  1363. #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
  1364. #else
  1365. /*
  1366. * No preemption supported therefore also no need to check for
  1367. * different cpus.
  1368. */
  1369. #define TID_STEP 1
  1370. #endif
  1371. static inline unsigned long next_tid(unsigned long tid)
  1372. {
  1373. return tid + TID_STEP;
  1374. }
  1375. static inline unsigned int tid_to_cpu(unsigned long tid)
  1376. {
  1377. return tid % TID_STEP;
  1378. }
  1379. static inline unsigned long tid_to_event(unsigned long tid)
  1380. {
  1381. return tid / TID_STEP;
  1382. }
  1383. static inline unsigned int init_tid(int cpu)
  1384. {
  1385. return cpu;
  1386. }
  1387. static inline void note_cmpxchg_failure(const char *n,
  1388. const struct kmem_cache *s, unsigned long tid)
  1389. {
  1390. #ifdef SLUB_DEBUG_CMPXCHG
  1391. unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
  1392. printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
  1393. #ifdef CONFIG_PREEMPT
  1394. if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
  1395. printk("due to cpu change %d -> %d\n",
  1396. tid_to_cpu(tid), tid_to_cpu(actual_tid));
  1397. else
  1398. #endif
  1399. if (tid_to_event(tid) != tid_to_event(actual_tid))
  1400. printk("due to cpu running other code. Event %ld->%ld\n",
  1401. tid_to_event(tid), tid_to_event(actual_tid));
  1402. else
  1403. printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
  1404. actual_tid, tid, next_tid(tid));
  1405. #endif
  1406. stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
  1407. }
  1408. void init_kmem_cache_cpus(struct kmem_cache *s)
  1409. {
  1410. int cpu;
  1411. for_each_possible_cpu(cpu)
  1412. per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
  1413. }
  1414. /*
  1415. * Remove the cpu slab
  1416. */
  1417. /*
  1418. * Remove the cpu slab
  1419. */
  1420. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1421. {
  1422. enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
  1423. struct page *page = c->page;
  1424. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1425. int lock = 0;
  1426. enum slab_modes l = M_NONE, m = M_NONE;
  1427. void *freelist;
  1428. void *nextfree;
  1429. int tail = 0;
  1430. struct page new;
  1431. struct page old;
  1432. if (page->freelist) {
  1433. stat(s, DEACTIVATE_REMOTE_FREES);
  1434. tail = 1;
  1435. }
  1436. c->tid = next_tid(c->tid);
  1437. c->page = NULL;
  1438. freelist = c->freelist;
  1439. c->freelist = NULL;
  1440. /*
  1441. * Stage one: Free all available per cpu objects back
  1442. * to the page freelist while it is still frozen. Leave the
  1443. * last one.
  1444. *
  1445. * There is no need to take the list->lock because the page
  1446. * is still frozen.
  1447. */
  1448. while (freelist && (nextfree = get_freepointer(s, freelist))) {
  1449. void *prior;
  1450. unsigned long counters;
  1451. do {
  1452. prior = page->freelist;
  1453. counters = page->counters;
  1454. set_freepointer(s, freelist, prior);
  1455. new.counters = counters;
  1456. new.inuse--;
  1457. VM_BUG_ON(!new.frozen);
  1458. } while (!cmpxchg_double_slab(s, page,
  1459. prior, counters,
  1460. freelist, new.counters,
  1461. "drain percpu freelist"));
  1462. freelist = nextfree;
  1463. }
  1464. /*
  1465. * Stage two: Ensure that the page is unfrozen while the
  1466. * list presence reflects the actual number of objects
  1467. * during unfreeze.
  1468. *
  1469. * We setup the list membership and then perform a cmpxchg
  1470. * with the count. If there is a mismatch then the page
  1471. * is not unfrozen but the page is on the wrong list.
  1472. *
  1473. * Then we restart the process which may have to remove
  1474. * the page from the list that we just put it on again
  1475. * because the number of objects in the slab may have
  1476. * changed.
  1477. */
  1478. redo:
  1479. old.freelist = page->freelist;
  1480. old.counters = page->counters;
  1481. VM_BUG_ON(!old.frozen);
  1482. /* Determine target state of the slab */
  1483. new.counters = old.counters;
  1484. if (freelist) {
  1485. new.inuse--;
  1486. set_freepointer(s, freelist, old.freelist);
  1487. new.freelist = freelist;
  1488. } else
  1489. new.freelist = old.freelist;
  1490. new.frozen = 0;
  1491. if (!new.inuse && n->nr_partial < s->min_partial)
  1492. m = M_FREE;
  1493. else if (new.freelist) {
  1494. m = M_PARTIAL;
  1495. if (!lock) {
  1496. lock = 1;
  1497. /*
  1498. * Taking the spinlock removes the possiblity
  1499. * that acquire_slab() will see a slab page that
  1500. * is frozen
  1501. */
  1502. spin_lock(&n->list_lock);
  1503. }
  1504. } else {
  1505. m = M_FULL;
  1506. if (kmem_cache_debug(s) && !lock) {
  1507. lock = 1;
  1508. /*
  1509. * This also ensures that the scanning of full
  1510. * slabs from diagnostic functions will not see
  1511. * any frozen slabs.
  1512. */
  1513. spin_lock(&n->list_lock);
  1514. }
  1515. }
  1516. if (l != m) {
  1517. if (l == M_PARTIAL)
  1518. remove_partial(n, page);
  1519. else if (l == M_FULL)
  1520. remove_full(s, page);
  1521. if (m == M_PARTIAL) {
  1522. add_partial(n, page, tail);
  1523. stat(s, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
  1524. } else if (m == M_FULL) {
  1525. stat(s, DEACTIVATE_FULL);
  1526. add_full(s, n, page);
  1527. }
  1528. }
  1529. l = m;
  1530. if (!cmpxchg_double_slab(s, page,
  1531. old.freelist, old.counters,
  1532. new.freelist, new.counters,
  1533. "unfreezing slab"))
  1534. goto redo;
  1535. if (lock)
  1536. spin_unlock(&n->list_lock);
  1537. if (m == M_FREE) {
  1538. stat(s, DEACTIVATE_EMPTY);
  1539. discard_slab(s, page);
  1540. stat(s, FREE_SLAB);
  1541. }
  1542. }
  1543. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1544. {
  1545. stat(s, CPUSLAB_FLUSH);
  1546. deactivate_slab(s, c);
  1547. }
  1548. /*
  1549. * Flush cpu slab.
  1550. *
  1551. * Called from IPI handler with interrupts disabled.
  1552. */
  1553. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1554. {
  1555. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1556. if (likely(c && c->page))
  1557. flush_slab(s, c);
  1558. }
  1559. static void flush_cpu_slab(void *d)
  1560. {
  1561. struct kmem_cache *s = d;
  1562. __flush_cpu_slab(s, smp_processor_id());
  1563. }
  1564. static void flush_all(struct kmem_cache *s)
  1565. {
  1566. on_each_cpu(flush_cpu_slab, s, 1);
  1567. }
  1568. /*
  1569. * Check if the objects in a per cpu structure fit numa
  1570. * locality expectations.
  1571. */
  1572. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1573. {
  1574. #ifdef CONFIG_NUMA
  1575. if (node != NUMA_NO_NODE && c->node != node)
  1576. return 0;
  1577. #endif
  1578. return 1;
  1579. }
  1580. static int count_free(struct page *page)
  1581. {
  1582. return page->objects - page->inuse;
  1583. }
  1584. static unsigned long count_partial(struct kmem_cache_node *n,
  1585. int (*get_count)(struct page *))
  1586. {
  1587. unsigned long flags;
  1588. unsigned long x = 0;
  1589. struct page *page;
  1590. spin_lock_irqsave(&n->list_lock, flags);
  1591. list_for_each_entry(page, &n->partial, lru)
  1592. x += get_count(page);
  1593. spin_unlock_irqrestore(&n->list_lock, flags);
  1594. return x;
  1595. }
  1596. static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
  1597. {
  1598. #ifdef CONFIG_SLUB_DEBUG
  1599. return atomic_long_read(&n->total_objects);
  1600. #else
  1601. return 0;
  1602. #endif
  1603. }
  1604. static noinline void
  1605. slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
  1606. {
  1607. int node;
  1608. printk(KERN_WARNING
  1609. "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1610. nid, gfpflags);
  1611. printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
  1612. "default order: %d, min order: %d\n", s->name, s->objsize,
  1613. s->size, oo_order(s->oo), oo_order(s->min));
  1614. if (oo_order(s->min) > get_order(s->objsize))
  1615. printk(KERN_WARNING " %s debugging increased min order, use "
  1616. "slub_debug=O to disable.\n", s->name);
  1617. for_each_online_node(node) {
  1618. struct kmem_cache_node *n = get_node(s, node);
  1619. unsigned long nr_slabs;
  1620. unsigned long nr_objs;
  1621. unsigned long nr_free;
  1622. if (!n)
  1623. continue;
  1624. nr_free = count_partial(n, count_free);
  1625. nr_slabs = node_nr_slabs(n);
  1626. nr_objs = node_nr_objs(n);
  1627. printk(KERN_WARNING
  1628. " node %d: slabs: %ld, objs: %ld, free: %ld\n",
  1629. node, nr_slabs, nr_objs, nr_free);
  1630. }
  1631. }
  1632. /*
  1633. * Slow path. The lockless freelist is empty or we need to perform
  1634. * debugging duties.
  1635. *
  1636. * Interrupts are disabled.
  1637. *
  1638. * Processing is still very fast if new objects have been freed to the
  1639. * regular freelist. In that case we simply take over the regular freelist
  1640. * as the lockless freelist and zap the regular freelist.
  1641. *
  1642. * If that is not working then we fall back to the partial lists. We take the
  1643. * first element of the freelist as the object to allocate now and move the
  1644. * rest of the freelist to the lockless freelist.
  1645. *
  1646. * And if we were unable to get a new slab from the partial slab lists then
  1647. * we need to allocate a new slab. This is the slowest path since it involves
  1648. * a call to the page allocator and the setup of a new slab.
  1649. */
  1650. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  1651. unsigned long addr, struct kmem_cache_cpu *c)
  1652. {
  1653. void **object;
  1654. struct page *page;
  1655. unsigned long flags;
  1656. struct page new;
  1657. unsigned long counters;
  1658. local_irq_save(flags);
  1659. #ifdef CONFIG_PREEMPT
  1660. /*
  1661. * We may have been preempted and rescheduled on a different
  1662. * cpu before disabling interrupts. Need to reload cpu area
  1663. * pointer.
  1664. */
  1665. c = this_cpu_ptr(s->cpu_slab);
  1666. #endif
  1667. /* We handle __GFP_ZERO in the caller */
  1668. gfpflags &= ~__GFP_ZERO;
  1669. page = c->page;
  1670. if (!page)
  1671. goto new_slab;
  1672. if (unlikely(!node_match(c, node))) {
  1673. stat(s, ALLOC_NODE_MISMATCH);
  1674. deactivate_slab(s, c);
  1675. goto new_slab;
  1676. }
  1677. stat(s, ALLOC_SLOWPATH);
  1678. do {
  1679. object = page->freelist;
  1680. counters = page->counters;
  1681. new.counters = counters;
  1682. VM_BUG_ON(!new.frozen);
  1683. /*
  1684. * If there is no object left then we use this loop to
  1685. * deactivate the slab which is simple since no objects
  1686. * are left in the slab and therefore we do not need to
  1687. * put the page back onto the partial list.
  1688. *
  1689. * If there are objects left then we retrieve them
  1690. * and use them to refill the per cpu queue.
  1691. */
  1692. new.inuse = page->objects;
  1693. new.frozen = object != NULL;
  1694. } while (!cmpxchg_double_slab(s, page,
  1695. object, counters,
  1696. NULL, new.counters,
  1697. "__slab_alloc"));
  1698. if (unlikely(!object)) {
  1699. c->page = NULL;
  1700. stat(s, DEACTIVATE_BYPASS);
  1701. goto new_slab;
  1702. }
  1703. stat(s, ALLOC_REFILL);
  1704. load_freelist:
  1705. VM_BUG_ON(!page->frozen);
  1706. c->freelist = get_freepointer(s, object);
  1707. c->tid = next_tid(c->tid);
  1708. local_irq_restore(flags);
  1709. return object;
  1710. new_slab:
  1711. page = get_partial(s, gfpflags, node);
  1712. if (page) {
  1713. stat(s, ALLOC_FROM_PARTIAL);
  1714. object = c->freelist;
  1715. if (kmem_cache_debug(s))
  1716. goto debug;
  1717. goto load_freelist;
  1718. }
  1719. page = new_slab(s, gfpflags, node);
  1720. if (page) {
  1721. c = __this_cpu_ptr(s->cpu_slab);
  1722. if (c->page)
  1723. flush_slab(s, c);
  1724. /*
  1725. * No other reference to the page yet so we can
  1726. * muck around with it freely without cmpxchg
  1727. */
  1728. object = page->freelist;
  1729. page->freelist = NULL;
  1730. page->inuse = page->objects;
  1731. stat(s, ALLOC_SLAB);
  1732. c->node = page_to_nid(page);
  1733. c->page = page;
  1734. goto load_freelist;
  1735. }
  1736. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1737. slab_out_of_memory(s, gfpflags, node);
  1738. local_irq_restore(flags);
  1739. return NULL;
  1740. debug:
  1741. if (!object || !alloc_debug_processing(s, page, object, addr))
  1742. goto new_slab;
  1743. c->freelist = get_freepointer(s, object);
  1744. deactivate_slab(s, c);
  1745. c->page = NULL;
  1746. c->node = NUMA_NO_NODE;
  1747. local_irq_restore(flags);
  1748. return object;
  1749. }
  1750. /*
  1751. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1752. * have the fastpath folded into their functions. So no function call
  1753. * overhead for requests that can be satisfied on the fastpath.
  1754. *
  1755. * The fastpath works by first checking if the lockless freelist can be used.
  1756. * If not then __slab_alloc is called for slow processing.
  1757. *
  1758. * Otherwise we can simply pick the next object from the lockless free list.
  1759. */
  1760. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1761. gfp_t gfpflags, int node, unsigned long addr)
  1762. {
  1763. void **object;
  1764. struct kmem_cache_cpu *c;
  1765. unsigned long tid;
  1766. if (slab_pre_alloc_hook(s, gfpflags))
  1767. return NULL;
  1768. redo:
  1769. /*
  1770. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1771. * enabled. We may switch back and forth between cpus while
  1772. * reading from one cpu area. That does not matter as long
  1773. * as we end up on the original cpu again when doing the cmpxchg.
  1774. */
  1775. c = __this_cpu_ptr(s->cpu_slab);
  1776. /*
  1777. * The transaction ids are globally unique per cpu and per operation on
  1778. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1779. * occurs on the right processor and that there was no operation on the
  1780. * linked list in between.
  1781. */
  1782. tid = c->tid;
  1783. barrier();
  1784. object = c->freelist;
  1785. if (unlikely(!object || !node_match(c, node)))
  1786. object = __slab_alloc(s, gfpflags, node, addr, c);
  1787. else {
  1788. /*
  1789. * The cmpxchg will only match if there was no additional
  1790. * operation and if we are on the right processor.
  1791. *
  1792. * The cmpxchg does the following atomically (without lock semantics!)
  1793. * 1. Relocate first pointer to the current per cpu area.
  1794. * 2. Verify that tid and freelist have not been changed
  1795. * 3. If they were not changed replace tid and freelist
  1796. *
  1797. * Since this is without lock semantics the protection is only against
  1798. * code executing on this cpu *not* from access by other cpus.
  1799. */
  1800. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  1801. s->cpu_slab->freelist, s->cpu_slab->tid,
  1802. object, tid,
  1803. get_freepointer_safe(s, object), next_tid(tid)))) {
  1804. note_cmpxchg_failure("slab_alloc", s, tid);
  1805. goto redo;
  1806. }
  1807. stat(s, ALLOC_FASTPATH);
  1808. }
  1809. if (unlikely(gfpflags & __GFP_ZERO) && object)
  1810. memset(object, 0, s->objsize);
  1811. slab_post_alloc_hook(s, gfpflags, object);
  1812. return object;
  1813. }
  1814. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1815. {
  1816. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1817. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  1818. return ret;
  1819. }
  1820. EXPORT_SYMBOL(kmem_cache_alloc);
  1821. #ifdef CONFIG_TRACING
  1822. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  1823. {
  1824. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1825. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  1826. return ret;
  1827. }
  1828. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  1829. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1830. {
  1831. void *ret = kmalloc_order(size, flags, order);
  1832. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1833. return ret;
  1834. }
  1835. EXPORT_SYMBOL(kmalloc_order_trace);
  1836. #endif
  1837. #ifdef CONFIG_NUMA
  1838. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1839. {
  1840. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1841. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  1842. s->objsize, s->size, gfpflags, node);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1846. #ifdef CONFIG_TRACING
  1847. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  1848. gfp_t gfpflags,
  1849. int node, size_t size)
  1850. {
  1851. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1852. trace_kmalloc_node(_RET_IP_, ret,
  1853. size, s->size, gfpflags, node);
  1854. return ret;
  1855. }
  1856. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  1857. #endif
  1858. #endif
  1859. /*
  1860. * Slow patch handling. This may still be called frequently since objects
  1861. * have a longer lifetime than the cpu slabs in most processing loads.
  1862. *
  1863. * So we still attempt to reduce cache line usage. Just take the slab
  1864. * lock and free the item. If there is no additional partial page
  1865. * handling required then we can return immediately.
  1866. */
  1867. static void __slab_free(struct kmem_cache *s, struct page *page,
  1868. void *x, unsigned long addr)
  1869. {
  1870. void *prior;
  1871. void **object = (void *)x;
  1872. int was_frozen;
  1873. int inuse;
  1874. struct page new;
  1875. unsigned long counters;
  1876. struct kmem_cache_node *n = NULL;
  1877. unsigned long uninitialized_var(flags);
  1878. stat(s, FREE_SLOWPATH);
  1879. if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
  1880. return;
  1881. do {
  1882. prior = page->freelist;
  1883. counters = page->counters;
  1884. set_freepointer(s, object, prior);
  1885. new.counters = counters;
  1886. was_frozen = new.frozen;
  1887. new.inuse--;
  1888. if ((!new.inuse || !prior) && !was_frozen && !n) {
  1889. n = get_node(s, page_to_nid(page));
  1890. /*
  1891. * Speculatively acquire the list_lock.
  1892. * If the cmpxchg does not succeed then we may
  1893. * drop the list_lock without any processing.
  1894. *
  1895. * Otherwise the list_lock will synchronize with
  1896. * other processors updating the list of slabs.
  1897. */
  1898. spin_lock_irqsave(&n->list_lock, flags);
  1899. }
  1900. inuse = new.inuse;
  1901. } while (!cmpxchg_double_slab(s, page,
  1902. prior, counters,
  1903. object, new.counters,
  1904. "__slab_free"));
  1905. if (likely(!n)) {
  1906. /*
  1907. * The list lock was not taken therefore no list
  1908. * activity can be necessary.
  1909. */
  1910. if (was_frozen)
  1911. stat(s, FREE_FROZEN);
  1912. return;
  1913. }
  1914. /*
  1915. * was_frozen may have been set after we acquired the list_lock in
  1916. * an earlier loop. So we need to check it here again.
  1917. */
  1918. if (was_frozen)
  1919. stat(s, FREE_FROZEN);
  1920. else {
  1921. if (unlikely(!inuse && n->nr_partial > s->min_partial))
  1922. goto slab_empty;
  1923. /*
  1924. * Objects left in the slab. If it was not on the partial list before
  1925. * then add it.
  1926. */
  1927. if (unlikely(!prior)) {
  1928. remove_full(s, page);
  1929. add_partial(n, page, 0);
  1930. stat(s, FREE_ADD_PARTIAL);
  1931. }
  1932. }
  1933. spin_unlock_irqrestore(&n->list_lock, flags);
  1934. return;
  1935. slab_empty:
  1936. if (prior) {
  1937. /*
  1938. * Slab still on the partial list.
  1939. */
  1940. remove_partial(n, page);
  1941. stat(s, FREE_REMOVE_PARTIAL);
  1942. }
  1943. spin_unlock_irqrestore(&n->list_lock, flags);
  1944. stat(s, FREE_SLAB);
  1945. discard_slab(s, page);
  1946. }
  1947. /*
  1948. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1949. * can perform fastpath freeing without additional function calls.
  1950. *
  1951. * The fastpath is only possible if we are freeing to the current cpu slab
  1952. * of this processor. This typically the case if we have just allocated
  1953. * the item before.
  1954. *
  1955. * If fastpath is not possible then fall back to __slab_free where we deal
  1956. * with all sorts of special processing.
  1957. */
  1958. static __always_inline void slab_free(struct kmem_cache *s,
  1959. struct page *page, void *x, unsigned long addr)
  1960. {
  1961. void **object = (void *)x;
  1962. struct kmem_cache_cpu *c;
  1963. unsigned long tid;
  1964. slab_free_hook(s, x);
  1965. redo:
  1966. /*
  1967. * Determine the currently cpus per cpu slab.
  1968. * The cpu may change afterward. However that does not matter since
  1969. * data is retrieved via this pointer. If we are on the same cpu
  1970. * during the cmpxchg then the free will succedd.
  1971. */
  1972. c = __this_cpu_ptr(s->cpu_slab);
  1973. tid = c->tid;
  1974. barrier();
  1975. if (likely(page == c->page)) {
  1976. set_freepointer(s, object, c->freelist);
  1977. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  1978. s->cpu_slab->freelist, s->cpu_slab->tid,
  1979. c->freelist, tid,
  1980. object, next_tid(tid)))) {
  1981. note_cmpxchg_failure("slab_free", s, tid);
  1982. goto redo;
  1983. }
  1984. stat(s, FREE_FASTPATH);
  1985. } else
  1986. __slab_free(s, page, x, addr);
  1987. }
  1988. void kmem_cache_free(struct kmem_cache *s, void *x)
  1989. {
  1990. struct page *page;
  1991. page = virt_to_head_page(x);
  1992. slab_free(s, page, x, _RET_IP_);
  1993. trace_kmem_cache_free(_RET_IP_, x);
  1994. }
  1995. EXPORT_SYMBOL(kmem_cache_free);
  1996. /*
  1997. * Object placement in a slab is made very easy because we always start at
  1998. * offset 0. If we tune the size of the object to the alignment then we can
  1999. * get the required alignment by putting one properly sized object after
  2000. * another.
  2001. *
  2002. * Notice that the allocation order determines the sizes of the per cpu
  2003. * caches. Each processor has always one slab available for allocations.
  2004. * Increasing the allocation order reduces the number of times that slabs
  2005. * must be moved on and off the partial lists and is therefore a factor in
  2006. * locking overhead.
  2007. */
  2008. /*
  2009. * Mininum / Maximum order of slab pages. This influences locking overhead
  2010. * and slab fragmentation. A higher order reduces the number of partial slabs
  2011. * and increases the number of allocations possible without having to
  2012. * take the list_lock.
  2013. */
  2014. static int slub_min_order;
  2015. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  2016. static int slub_min_objects;
  2017. /*
  2018. * Merge control. If this is set then no merging of slab caches will occur.
  2019. * (Could be removed. This was introduced to pacify the merge skeptics.)
  2020. */
  2021. static int slub_nomerge;
  2022. /*
  2023. * Calculate the order of allocation given an slab object size.
  2024. *
  2025. * The order of allocation has significant impact on performance and other
  2026. * system components. Generally order 0 allocations should be preferred since
  2027. * order 0 does not cause fragmentation in the page allocator. Larger objects
  2028. * be problematic to put into order 0 slabs because there may be too much
  2029. * unused space left. We go to a higher order if more than 1/16th of the slab
  2030. * would be wasted.
  2031. *
  2032. * In order to reach satisfactory performance we must ensure that a minimum
  2033. * number of objects is in one slab. Otherwise we may generate too much
  2034. * activity on the partial lists which requires taking the list_lock. This is
  2035. * less a concern for large slabs though which are rarely used.
  2036. *
  2037. * slub_max_order specifies the order where we begin to stop considering the
  2038. * number of objects in a slab as critical. If we reach slub_max_order then
  2039. * we try to keep the page order as low as possible. So we accept more waste
  2040. * of space in favor of a small page order.
  2041. *
  2042. * Higher order allocations also allow the placement of more objects in a
  2043. * slab and thereby reduce object handling overhead. If the user has
  2044. * requested a higher mininum order then we start with that one instead of
  2045. * the smallest order which will fit the object.
  2046. */
  2047. static inline int slab_order(int size, int min_objects,
  2048. int max_order, int fract_leftover, int reserved)
  2049. {
  2050. int order;
  2051. int rem;
  2052. int min_order = slub_min_order;
  2053. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  2054. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  2055. for (order = max(min_order,
  2056. fls(min_objects * size - 1) - PAGE_SHIFT);
  2057. order <= max_order; order++) {
  2058. unsigned long slab_size = PAGE_SIZE << order;
  2059. if (slab_size < min_objects * size + reserved)
  2060. continue;
  2061. rem = (slab_size - reserved) % size;
  2062. if (rem <= slab_size / fract_leftover)
  2063. break;
  2064. }
  2065. return order;
  2066. }
  2067. static inline int calculate_order(int size, int reserved)
  2068. {
  2069. int order;
  2070. int min_objects;
  2071. int fraction;
  2072. int max_objects;
  2073. /*
  2074. * Attempt to find best configuration for a slab. This
  2075. * works by first attempting to generate a layout with
  2076. * the best configuration and backing off gradually.
  2077. *
  2078. * First we reduce the acceptable waste in a slab. Then
  2079. * we reduce the minimum objects required in a slab.
  2080. */
  2081. min_objects = slub_min_objects;
  2082. if (!min_objects)
  2083. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  2084. max_objects = order_objects(slub_max_order, size, reserved);
  2085. min_objects = min(min_objects, max_objects);
  2086. while (min_objects > 1) {
  2087. fraction = 16;
  2088. while (fraction >= 4) {
  2089. order = slab_order(size, min_objects,
  2090. slub_max_order, fraction, reserved);
  2091. if (order <= slub_max_order)
  2092. return order;
  2093. fraction /= 2;
  2094. }
  2095. min_objects--;
  2096. }
  2097. /*
  2098. * We were unable to place multiple objects in a slab. Now
  2099. * lets see if we can place a single object there.
  2100. */
  2101. order = slab_order(size, 1, slub_max_order, 1, reserved);
  2102. if (order <= slub_max_order)
  2103. return order;
  2104. /*
  2105. * Doh this slab cannot be placed using slub_max_order.
  2106. */
  2107. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  2108. if (order < MAX_ORDER)
  2109. return order;
  2110. return -ENOSYS;
  2111. }
  2112. /*
  2113. * Figure out what the alignment of the objects will be.
  2114. */
  2115. static unsigned long calculate_alignment(unsigned long flags,
  2116. unsigned long align, unsigned long size)
  2117. {
  2118. /*
  2119. * If the user wants hardware cache aligned objects then follow that
  2120. * suggestion if the object is sufficiently large.
  2121. *
  2122. * The hardware cache alignment cannot override the specified
  2123. * alignment though. If that is greater then use it.
  2124. */
  2125. if (flags & SLAB_HWCACHE_ALIGN) {
  2126. unsigned long ralign = cache_line_size();
  2127. while (size <= ralign / 2)
  2128. ralign /= 2;
  2129. align = max(align, ralign);
  2130. }
  2131. if (align < ARCH_SLAB_MINALIGN)
  2132. align = ARCH_SLAB_MINALIGN;
  2133. return ALIGN(align, sizeof(void *));
  2134. }
  2135. static void
  2136. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  2137. {
  2138. n->nr_partial = 0;
  2139. spin_lock_init(&n->list_lock);
  2140. INIT_LIST_HEAD(&n->partial);
  2141. #ifdef CONFIG_SLUB_DEBUG
  2142. atomic_long_set(&n->nr_slabs, 0);
  2143. atomic_long_set(&n->total_objects, 0);
  2144. INIT_LIST_HEAD(&n->full);
  2145. #endif
  2146. }
  2147. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2148. {
  2149. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2150. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  2151. /*
  2152. * Must align to double word boundary for the double cmpxchg
  2153. * instructions to work; see __pcpu_double_call_return_bool().
  2154. */
  2155. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  2156. 2 * sizeof(void *));
  2157. if (!s->cpu_slab)
  2158. return 0;
  2159. init_kmem_cache_cpus(s);
  2160. return 1;
  2161. }
  2162. static struct kmem_cache *kmem_cache_node;
  2163. /*
  2164. * No kmalloc_node yet so do it by hand. We know that this is the first
  2165. * slab on the node for this slabcache. There are no concurrent accesses
  2166. * possible.
  2167. *
  2168. * Note that this function only works on the kmalloc_node_cache
  2169. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2170. * memory on a fresh node that has no slab structures yet.
  2171. */
  2172. static void early_kmem_cache_node_alloc(int node)
  2173. {
  2174. struct page *page;
  2175. struct kmem_cache_node *n;
  2176. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2177. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2178. BUG_ON(!page);
  2179. if (page_to_nid(page) != node) {
  2180. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2181. "node %d\n", node);
  2182. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2183. "in order to be able to continue\n");
  2184. }
  2185. n = page->freelist;
  2186. BUG_ON(!n);
  2187. page->freelist = get_freepointer(kmem_cache_node, n);
  2188. page->inuse++;
  2189. page->frozen = 0;
  2190. kmem_cache_node->node[node] = n;
  2191. #ifdef CONFIG_SLUB_DEBUG
  2192. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2193. init_tracking(kmem_cache_node, n);
  2194. #endif
  2195. init_kmem_cache_node(n, kmem_cache_node);
  2196. inc_slabs_node(kmem_cache_node, node, page->objects);
  2197. add_partial(n, page, 0);
  2198. }
  2199. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2200. {
  2201. int node;
  2202. for_each_node_state(node, N_NORMAL_MEMORY) {
  2203. struct kmem_cache_node *n = s->node[node];
  2204. if (n)
  2205. kmem_cache_free(kmem_cache_node, n);
  2206. s->node[node] = NULL;
  2207. }
  2208. }
  2209. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2210. {
  2211. int node;
  2212. for_each_node_state(node, N_NORMAL_MEMORY) {
  2213. struct kmem_cache_node *n;
  2214. if (slab_state == DOWN) {
  2215. early_kmem_cache_node_alloc(node);
  2216. continue;
  2217. }
  2218. n = kmem_cache_alloc_node(kmem_cache_node,
  2219. GFP_KERNEL, node);
  2220. if (!n) {
  2221. free_kmem_cache_nodes(s);
  2222. return 0;
  2223. }
  2224. s->node[node] = n;
  2225. init_kmem_cache_node(n, s);
  2226. }
  2227. return 1;
  2228. }
  2229. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2230. {
  2231. if (min < MIN_PARTIAL)
  2232. min = MIN_PARTIAL;
  2233. else if (min > MAX_PARTIAL)
  2234. min = MAX_PARTIAL;
  2235. s->min_partial = min;
  2236. }
  2237. /*
  2238. * calculate_sizes() determines the order and the distribution of data within
  2239. * a slab object.
  2240. */
  2241. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2242. {
  2243. unsigned long flags = s->flags;
  2244. unsigned long size = s->objsize;
  2245. unsigned long align = s->align;
  2246. int order;
  2247. /*
  2248. * Round up object size to the next word boundary. We can only
  2249. * place the free pointer at word boundaries and this determines
  2250. * the possible location of the free pointer.
  2251. */
  2252. size = ALIGN(size, sizeof(void *));
  2253. #ifdef CONFIG_SLUB_DEBUG
  2254. /*
  2255. * Determine if we can poison the object itself. If the user of
  2256. * the slab may touch the object after free or before allocation
  2257. * then we should never poison the object itself.
  2258. */
  2259. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2260. !s->ctor)
  2261. s->flags |= __OBJECT_POISON;
  2262. else
  2263. s->flags &= ~__OBJECT_POISON;
  2264. /*
  2265. * If we are Redzoning then check if there is some space between the
  2266. * end of the object and the free pointer. If not then add an
  2267. * additional word to have some bytes to store Redzone information.
  2268. */
  2269. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2270. size += sizeof(void *);
  2271. #endif
  2272. /*
  2273. * With that we have determined the number of bytes in actual use
  2274. * by the object. This is the potential offset to the free pointer.
  2275. */
  2276. s->inuse = size;
  2277. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2278. s->ctor)) {
  2279. /*
  2280. * Relocate free pointer after the object if it is not
  2281. * permitted to overwrite the first word of the object on
  2282. * kmem_cache_free.
  2283. *
  2284. * This is the case if we do RCU, have a constructor or
  2285. * destructor or are poisoning the objects.
  2286. */
  2287. s->offset = size;
  2288. size += sizeof(void *);
  2289. }
  2290. #ifdef CONFIG_SLUB_DEBUG
  2291. if (flags & SLAB_STORE_USER)
  2292. /*
  2293. * Need to store information about allocs and frees after
  2294. * the object.
  2295. */
  2296. size += 2 * sizeof(struct track);
  2297. if (flags & SLAB_RED_ZONE)
  2298. /*
  2299. * Add some empty padding so that we can catch
  2300. * overwrites from earlier objects rather than let
  2301. * tracking information or the free pointer be
  2302. * corrupted if a user writes before the start
  2303. * of the object.
  2304. */
  2305. size += sizeof(void *);
  2306. #endif
  2307. /*
  2308. * Determine the alignment based on various parameters that the
  2309. * user specified and the dynamic determination of cache line size
  2310. * on bootup.
  2311. */
  2312. align = calculate_alignment(flags, align, s->objsize);
  2313. s->align = align;
  2314. /*
  2315. * SLUB stores one object immediately after another beginning from
  2316. * offset 0. In order to align the objects we have to simply size
  2317. * each object to conform to the alignment.
  2318. */
  2319. size = ALIGN(size, align);
  2320. s->size = size;
  2321. if (forced_order >= 0)
  2322. order = forced_order;
  2323. else
  2324. order = calculate_order(size, s->reserved);
  2325. if (order < 0)
  2326. return 0;
  2327. s->allocflags = 0;
  2328. if (order)
  2329. s->allocflags |= __GFP_COMP;
  2330. if (s->flags & SLAB_CACHE_DMA)
  2331. s->allocflags |= SLUB_DMA;
  2332. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2333. s->allocflags |= __GFP_RECLAIMABLE;
  2334. /*
  2335. * Determine the number of objects per slab
  2336. */
  2337. s->oo = oo_make(order, size, s->reserved);
  2338. s->min = oo_make(get_order(size), size, s->reserved);
  2339. if (oo_objects(s->oo) > oo_objects(s->max))
  2340. s->max = s->oo;
  2341. return !!oo_objects(s->oo);
  2342. }
  2343. static int kmem_cache_open(struct kmem_cache *s,
  2344. const char *name, size_t size,
  2345. size_t align, unsigned long flags,
  2346. void (*ctor)(void *))
  2347. {
  2348. memset(s, 0, kmem_size);
  2349. s->name = name;
  2350. s->ctor = ctor;
  2351. s->objsize = size;
  2352. s->align = align;
  2353. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2354. s->reserved = 0;
  2355. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2356. s->reserved = sizeof(struct rcu_head);
  2357. if (!calculate_sizes(s, -1))
  2358. goto error;
  2359. if (disable_higher_order_debug) {
  2360. /*
  2361. * Disable debugging flags that store metadata if the min slab
  2362. * order increased.
  2363. */
  2364. if (get_order(s->size) > get_order(s->objsize)) {
  2365. s->flags &= ~DEBUG_METADATA_FLAGS;
  2366. s->offset = 0;
  2367. if (!calculate_sizes(s, -1))
  2368. goto error;
  2369. }
  2370. }
  2371. #ifdef CONFIG_CMPXCHG_DOUBLE
  2372. if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
  2373. /* Enable fast mode */
  2374. s->flags |= __CMPXCHG_DOUBLE;
  2375. #endif
  2376. /*
  2377. * The larger the object size is, the more pages we want on the partial
  2378. * list to avoid pounding the page allocator excessively.
  2379. */
  2380. set_min_partial(s, ilog2(s->size));
  2381. s->refcount = 1;
  2382. #ifdef CONFIG_NUMA
  2383. s->remote_node_defrag_ratio = 1000;
  2384. #endif
  2385. if (!init_kmem_cache_nodes(s))
  2386. goto error;
  2387. if (alloc_kmem_cache_cpus(s))
  2388. return 1;
  2389. free_kmem_cache_nodes(s);
  2390. error:
  2391. if (flags & SLAB_PANIC)
  2392. panic("Cannot create slab %s size=%lu realsize=%u "
  2393. "order=%u offset=%u flags=%lx\n",
  2394. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2395. s->offset, flags);
  2396. return 0;
  2397. }
  2398. /*
  2399. * Determine the size of a slab object
  2400. */
  2401. unsigned int kmem_cache_size(struct kmem_cache *s)
  2402. {
  2403. return s->objsize;
  2404. }
  2405. EXPORT_SYMBOL(kmem_cache_size);
  2406. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2407. const char *text)
  2408. {
  2409. #ifdef CONFIG_SLUB_DEBUG
  2410. void *addr = page_address(page);
  2411. void *p;
  2412. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2413. sizeof(long), GFP_ATOMIC);
  2414. if (!map)
  2415. return;
  2416. slab_err(s, page, "%s", text);
  2417. slab_lock(page);
  2418. get_map(s, page, map);
  2419. for_each_object(p, s, addr, page->objects) {
  2420. if (!test_bit(slab_index(p, s, addr), map)) {
  2421. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2422. p, p - addr);
  2423. print_tracking(s, p);
  2424. }
  2425. }
  2426. slab_unlock(page);
  2427. kfree(map);
  2428. #endif
  2429. }
  2430. /*
  2431. * Attempt to free all partial slabs on a node.
  2432. */
  2433. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2434. {
  2435. unsigned long flags;
  2436. struct page *page, *h;
  2437. spin_lock_irqsave(&n->list_lock, flags);
  2438. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2439. if (!page->inuse) {
  2440. remove_partial(n, page);
  2441. discard_slab(s, page);
  2442. } else {
  2443. list_slab_objects(s, page,
  2444. "Objects remaining on kmem_cache_close()");
  2445. }
  2446. }
  2447. spin_unlock_irqrestore(&n->list_lock, flags);
  2448. }
  2449. /*
  2450. * Release all resources used by a slab cache.
  2451. */
  2452. static inline int kmem_cache_close(struct kmem_cache *s)
  2453. {
  2454. int node;
  2455. flush_all(s);
  2456. free_percpu(s->cpu_slab);
  2457. /* Attempt to free all objects */
  2458. for_each_node_state(node, N_NORMAL_MEMORY) {
  2459. struct kmem_cache_node *n = get_node(s, node);
  2460. free_partial(s, n);
  2461. if (n->nr_partial || slabs_node(s, node))
  2462. return 1;
  2463. }
  2464. free_kmem_cache_nodes(s);
  2465. return 0;
  2466. }
  2467. /*
  2468. * Close a cache and release the kmem_cache structure
  2469. * (must be used for caches created using kmem_cache_create)
  2470. */
  2471. void kmem_cache_destroy(struct kmem_cache *s)
  2472. {
  2473. down_write(&slub_lock);
  2474. s->refcount--;
  2475. if (!s->refcount) {
  2476. list_del(&s->list);
  2477. if (kmem_cache_close(s)) {
  2478. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2479. "still has objects.\n", s->name, __func__);
  2480. dump_stack();
  2481. }
  2482. if (s->flags & SLAB_DESTROY_BY_RCU)
  2483. rcu_barrier();
  2484. sysfs_slab_remove(s);
  2485. }
  2486. up_write(&slub_lock);
  2487. }
  2488. EXPORT_SYMBOL(kmem_cache_destroy);
  2489. /********************************************************************
  2490. * Kmalloc subsystem
  2491. *******************************************************************/
  2492. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2493. EXPORT_SYMBOL(kmalloc_caches);
  2494. static struct kmem_cache *kmem_cache;
  2495. #ifdef CONFIG_ZONE_DMA
  2496. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2497. #endif
  2498. static int __init setup_slub_min_order(char *str)
  2499. {
  2500. get_option(&str, &slub_min_order);
  2501. return 1;
  2502. }
  2503. __setup("slub_min_order=", setup_slub_min_order);
  2504. static int __init setup_slub_max_order(char *str)
  2505. {
  2506. get_option(&str, &slub_max_order);
  2507. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2508. return 1;
  2509. }
  2510. __setup("slub_max_order=", setup_slub_max_order);
  2511. static int __init setup_slub_min_objects(char *str)
  2512. {
  2513. get_option(&str, &slub_min_objects);
  2514. return 1;
  2515. }
  2516. __setup("slub_min_objects=", setup_slub_min_objects);
  2517. static int __init setup_slub_nomerge(char *str)
  2518. {
  2519. slub_nomerge = 1;
  2520. return 1;
  2521. }
  2522. __setup("slub_nomerge", setup_slub_nomerge);
  2523. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2524. int size, unsigned int flags)
  2525. {
  2526. struct kmem_cache *s;
  2527. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2528. /*
  2529. * This function is called with IRQs disabled during early-boot on
  2530. * single CPU so there's no need to take slub_lock here.
  2531. */
  2532. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2533. flags, NULL))
  2534. goto panic;
  2535. list_add(&s->list, &slab_caches);
  2536. return s;
  2537. panic:
  2538. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2539. return NULL;
  2540. }
  2541. /*
  2542. * Conversion table for small slabs sizes / 8 to the index in the
  2543. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2544. * of two cache sizes there. The size of larger slabs can be determined using
  2545. * fls.
  2546. */
  2547. static s8 size_index[24] = {
  2548. 3, /* 8 */
  2549. 4, /* 16 */
  2550. 5, /* 24 */
  2551. 5, /* 32 */
  2552. 6, /* 40 */
  2553. 6, /* 48 */
  2554. 6, /* 56 */
  2555. 6, /* 64 */
  2556. 1, /* 72 */
  2557. 1, /* 80 */
  2558. 1, /* 88 */
  2559. 1, /* 96 */
  2560. 7, /* 104 */
  2561. 7, /* 112 */
  2562. 7, /* 120 */
  2563. 7, /* 128 */
  2564. 2, /* 136 */
  2565. 2, /* 144 */
  2566. 2, /* 152 */
  2567. 2, /* 160 */
  2568. 2, /* 168 */
  2569. 2, /* 176 */
  2570. 2, /* 184 */
  2571. 2 /* 192 */
  2572. };
  2573. static inline int size_index_elem(size_t bytes)
  2574. {
  2575. return (bytes - 1) / 8;
  2576. }
  2577. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2578. {
  2579. int index;
  2580. if (size <= 192) {
  2581. if (!size)
  2582. return ZERO_SIZE_PTR;
  2583. index = size_index[size_index_elem(size)];
  2584. } else
  2585. index = fls(size - 1);
  2586. #ifdef CONFIG_ZONE_DMA
  2587. if (unlikely((flags & SLUB_DMA)))
  2588. return kmalloc_dma_caches[index];
  2589. #endif
  2590. return kmalloc_caches[index];
  2591. }
  2592. void *__kmalloc(size_t size, gfp_t flags)
  2593. {
  2594. struct kmem_cache *s;
  2595. void *ret;
  2596. if (unlikely(size > SLUB_MAX_SIZE))
  2597. return kmalloc_large(size, flags);
  2598. s = get_slab(size, flags);
  2599. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2600. return s;
  2601. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2602. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2603. return ret;
  2604. }
  2605. EXPORT_SYMBOL(__kmalloc);
  2606. #ifdef CONFIG_NUMA
  2607. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2608. {
  2609. struct page *page;
  2610. void *ptr = NULL;
  2611. flags |= __GFP_COMP | __GFP_NOTRACK;
  2612. page = alloc_pages_node(node, flags, get_order(size));
  2613. if (page)
  2614. ptr = page_address(page);
  2615. kmemleak_alloc(ptr, size, 1, flags);
  2616. return ptr;
  2617. }
  2618. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2619. {
  2620. struct kmem_cache *s;
  2621. void *ret;
  2622. if (unlikely(size > SLUB_MAX_SIZE)) {
  2623. ret = kmalloc_large_node(size, flags, node);
  2624. trace_kmalloc_node(_RET_IP_, ret,
  2625. size, PAGE_SIZE << get_order(size),
  2626. flags, node);
  2627. return ret;
  2628. }
  2629. s = get_slab(size, flags);
  2630. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2631. return s;
  2632. ret = slab_alloc(s, flags, node, _RET_IP_);
  2633. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2634. return ret;
  2635. }
  2636. EXPORT_SYMBOL(__kmalloc_node);
  2637. #endif
  2638. size_t ksize(const void *object)
  2639. {
  2640. struct page *page;
  2641. if (unlikely(object == ZERO_SIZE_PTR))
  2642. return 0;
  2643. page = virt_to_head_page(object);
  2644. if (unlikely(!PageSlab(page))) {
  2645. WARN_ON(!PageCompound(page));
  2646. return PAGE_SIZE << compound_order(page);
  2647. }
  2648. return slab_ksize(page->slab);
  2649. }
  2650. EXPORT_SYMBOL(ksize);
  2651. void kfree(const void *x)
  2652. {
  2653. struct page *page;
  2654. void *object = (void *)x;
  2655. trace_kfree(_RET_IP_, x);
  2656. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2657. return;
  2658. page = virt_to_head_page(x);
  2659. if (unlikely(!PageSlab(page))) {
  2660. BUG_ON(!PageCompound(page));
  2661. kmemleak_free(x);
  2662. put_page(page);
  2663. return;
  2664. }
  2665. slab_free(page->slab, page, object, _RET_IP_);
  2666. }
  2667. EXPORT_SYMBOL(kfree);
  2668. /*
  2669. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2670. * the remaining slabs by the number of items in use. The slabs with the
  2671. * most items in use come first. New allocations will then fill those up
  2672. * and thus they can be removed from the partial lists.
  2673. *
  2674. * The slabs with the least items are placed last. This results in them
  2675. * being allocated from last increasing the chance that the last objects
  2676. * are freed in them.
  2677. */
  2678. int kmem_cache_shrink(struct kmem_cache *s)
  2679. {
  2680. int node;
  2681. int i;
  2682. struct kmem_cache_node *n;
  2683. struct page *page;
  2684. struct page *t;
  2685. int objects = oo_objects(s->max);
  2686. struct list_head *slabs_by_inuse =
  2687. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2688. unsigned long flags;
  2689. if (!slabs_by_inuse)
  2690. return -ENOMEM;
  2691. flush_all(s);
  2692. for_each_node_state(node, N_NORMAL_MEMORY) {
  2693. n = get_node(s, node);
  2694. if (!n->nr_partial)
  2695. continue;
  2696. for (i = 0; i < objects; i++)
  2697. INIT_LIST_HEAD(slabs_by_inuse + i);
  2698. spin_lock_irqsave(&n->list_lock, flags);
  2699. /*
  2700. * Build lists indexed by the items in use in each slab.
  2701. *
  2702. * Note that concurrent frees may occur while we hold the
  2703. * list_lock. page->inuse here is the upper limit.
  2704. */
  2705. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2706. if (!page->inuse) {
  2707. remove_partial(n, page);
  2708. discard_slab(s, page);
  2709. } else {
  2710. list_move(&page->lru,
  2711. slabs_by_inuse + page->inuse);
  2712. }
  2713. }
  2714. /*
  2715. * Rebuild the partial list with the slabs filled up most
  2716. * first and the least used slabs at the end.
  2717. */
  2718. for (i = objects - 1; i >= 0; i--)
  2719. list_splice(slabs_by_inuse + i, n->partial.prev);
  2720. spin_unlock_irqrestore(&n->list_lock, flags);
  2721. }
  2722. kfree(slabs_by_inuse);
  2723. return 0;
  2724. }
  2725. EXPORT_SYMBOL(kmem_cache_shrink);
  2726. #if defined(CONFIG_MEMORY_HOTPLUG)
  2727. static int slab_mem_going_offline_callback(void *arg)
  2728. {
  2729. struct kmem_cache *s;
  2730. down_read(&slub_lock);
  2731. list_for_each_entry(s, &slab_caches, list)
  2732. kmem_cache_shrink(s);
  2733. up_read(&slub_lock);
  2734. return 0;
  2735. }
  2736. static void slab_mem_offline_callback(void *arg)
  2737. {
  2738. struct kmem_cache_node *n;
  2739. struct kmem_cache *s;
  2740. struct memory_notify *marg = arg;
  2741. int offline_node;
  2742. offline_node = marg->status_change_nid;
  2743. /*
  2744. * If the node still has available memory. we need kmem_cache_node
  2745. * for it yet.
  2746. */
  2747. if (offline_node < 0)
  2748. return;
  2749. down_read(&slub_lock);
  2750. list_for_each_entry(s, &slab_caches, list) {
  2751. n = get_node(s, offline_node);
  2752. if (n) {
  2753. /*
  2754. * if n->nr_slabs > 0, slabs still exist on the node
  2755. * that is going down. We were unable to free them,
  2756. * and offline_pages() function shouldn't call this
  2757. * callback. So, we must fail.
  2758. */
  2759. BUG_ON(slabs_node(s, offline_node));
  2760. s->node[offline_node] = NULL;
  2761. kmem_cache_free(kmem_cache_node, n);
  2762. }
  2763. }
  2764. up_read(&slub_lock);
  2765. }
  2766. static int slab_mem_going_online_callback(void *arg)
  2767. {
  2768. struct kmem_cache_node *n;
  2769. struct kmem_cache *s;
  2770. struct memory_notify *marg = arg;
  2771. int nid = marg->status_change_nid;
  2772. int ret = 0;
  2773. /*
  2774. * If the node's memory is already available, then kmem_cache_node is
  2775. * already created. Nothing to do.
  2776. */
  2777. if (nid < 0)
  2778. return 0;
  2779. /*
  2780. * We are bringing a node online. No memory is available yet. We must
  2781. * allocate a kmem_cache_node structure in order to bring the node
  2782. * online.
  2783. */
  2784. down_read(&slub_lock);
  2785. list_for_each_entry(s, &slab_caches, list) {
  2786. /*
  2787. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2788. * since memory is not yet available from the node that
  2789. * is brought up.
  2790. */
  2791. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  2792. if (!n) {
  2793. ret = -ENOMEM;
  2794. goto out;
  2795. }
  2796. init_kmem_cache_node(n, s);
  2797. s->node[nid] = n;
  2798. }
  2799. out:
  2800. up_read(&slub_lock);
  2801. return ret;
  2802. }
  2803. static int slab_memory_callback(struct notifier_block *self,
  2804. unsigned long action, void *arg)
  2805. {
  2806. int ret = 0;
  2807. switch (action) {
  2808. case MEM_GOING_ONLINE:
  2809. ret = slab_mem_going_online_callback(arg);
  2810. break;
  2811. case MEM_GOING_OFFLINE:
  2812. ret = slab_mem_going_offline_callback(arg);
  2813. break;
  2814. case MEM_OFFLINE:
  2815. case MEM_CANCEL_ONLINE:
  2816. slab_mem_offline_callback(arg);
  2817. break;
  2818. case MEM_ONLINE:
  2819. case MEM_CANCEL_OFFLINE:
  2820. break;
  2821. }
  2822. if (ret)
  2823. ret = notifier_from_errno(ret);
  2824. else
  2825. ret = NOTIFY_OK;
  2826. return ret;
  2827. }
  2828. #endif /* CONFIG_MEMORY_HOTPLUG */
  2829. /********************************************************************
  2830. * Basic setup of slabs
  2831. *******************************************************************/
  2832. /*
  2833. * Used for early kmem_cache structures that were allocated using
  2834. * the page allocator
  2835. */
  2836. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  2837. {
  2838. int node;
  2839. list_add(&s->list, &slab_caches);
  2840. s->refcount = -1;
  2841. for_each_node_state(node, N_NORMAL_MEMORY) {
  2842. struct kmem_cache_node *n = get_node(s, node);
  2843. struct page *p;
  2844. if (n) {
  2845. list_for_each_entry(p, &n->partial, lru)
  2846. p->slab = s;
  2847. #ifdef CONFIG_SLUB_DEBUG
  2848. list_for_each_entry(p, &n->full, lru)
  2849. p->slab = s;
  2850. #endif
  2851. }
  2852. }
  2853. }
  2854. void __init kmem_cache_init(void)
  2855. {
  2856. int i;
  2857. int caches = 0;
  2858. struct kmem_cache *temp_kmem_cache;
  2859. int order;
  2860. struct kmem_cache *temp_kmem_cache_node;
  2861. unsigned long kmalloc_size;
  2862. kmem_size = offsetof(struct kmem_cache, node) +
  2863. nr_node_ids * sizeof(struct kmem_cache_node *);
  2864. /* Allocate two kmem_caches from the page allocator */
  2865. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  2866. order = get_order(2 * kmalloc_size);
  2867. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  2868. /*
  2869. * Must first have the slab cache available for the allocations of the
  2870. * struct kmem_cache_node's. There is special bootstrap code in
  2871. * kmem_cache_open for slab_state == DOWN.
  2872. */
  2873. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  2874. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  2875. sizeof(struct kmem_cache_node),
  2876. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2877. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2878. /* Able to allocate the per node structures */
  2879. slab_state = PARTIAL;
  2880. temp_kmem_cache = kmem_cache;
  2881. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  2882. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2883. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2884. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  2885. /*
  2886. * Allocate kmem_cache_node properly from the kmem_cache slab.
  2887. * kmem_cache_node is separately allocated so no need to
  2888. * update any list pointers.
  2889. */
  2890. temp_kmem_cache_node = kmem_cache_node;
  2891. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2892. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  2893. kmem_cache_bootstrap_fixup(kmem_cache_node);
  2894. caches++;
  2895. kmem_cache_bootstrap_fixup(kmem_cache);
  2896. caches++;
  2897. /* Free temporary boot structure */
  2898. free_pages((unsigned long)temp_kmem_cache, order);
  2899. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  2900. /*
  2901. * Patch up the size_index table if we have strange large alignment
  2902. * requirements for the kmalloc array. This is only the case for
  2903. * MIPS it seems. The standard arches will not generate any code here.
  2904. *
  2905. * Largest permitted alignment is 256 bytes due to the way we
  2906. * handle the index determination for the smaller caches.
  2907. *
  2908. * Make sure that nothing crazy happens if someone starts tinkering
  2909. * around with ARCH_KMALLOC_MINALIGN
  2910. */
  2911. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2912. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2913. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  2914. int elem = size_index_elem(i);
  2915. if (elem >= ARRAY_SIZE(size_index))
  2916. break;
  2917. size_index[elem] = KMALLOC_SHIFT_LOW;
  2918. }
  2919. if (KMALLOC_MIN_SIZE == 64) {
  2920. /*
  2921. * The 96 byte size cache is not used if the alignment
  2922. * is 64 byte.
  2923. */
  2924. for (i = 64 + 8; i <= 96; i += 8)
  2925. size_index[size_index_elem(i)] = 7;
  2926. } else if (KMALLOC_MIN_SIZE == 128) {
  2927. /*
  2928. * The 192 byte sized cache is not used if the alignment
  2929. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  2930. * instead.
  2931. */
  2932. for (i = 128 + 8; i <= 192; i += 8)
  2933. size_index[size_index_elem(i)] = 8;
  2934. }
  2935. /* Caches that are not of the two-to-the-power-of size */
  2936. if (KMALLOC_MIN_SIZE <= 32) {
  2937. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  2938. caches++;
  2939. }
  2940. if (KMALLOC_MIN_SIZE <= 64) {
  2941. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  2942. caches++;
  2943. }
  2944. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2945. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  2946. caches++;
  2947. }
  2948. slab_state = UP;
  2949. /* Provide the correct kmalloc names now that the caches are up */
  2950. if (KMALLOC_MIN_SIZE <= 32) {
  2951. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  2952. BUG_ON(!kmalloc_caches[1]->name);
  2953. }
  2954. if (KMALLOC_MIN_SIZE <= 64) {
  2955. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  2956. BUG_ON(!kmalloc_caches[2]->name);
  2957. }
  2958. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2959. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  2960. BUG_ON(!s);
  2961. kmalloc_caches[i]->name = s;
  2962. }
  2963. #ifdef CONFIG_SMP
  2964. register_cpu_notifier(&slab_notifier);
  2965. #endif
  2966. #ifdef CONFIG_ZONE_DMA
  2967. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  2968. struct kmem_cache *s = kmalloc_caches[i];
  2969. if (s && s->size) {
  2970. char *name = kasprintf(GFP_NOWAIT,
  2971. "dma-kmalloc-%d", s->objsize);
  2972. BUG_ON(!name);
  2973. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  2974. s->objsize, SLAB_CACHE_DMA);
  2975. }
  2976. }
  2977. #endif
  2978. printk(KERN_INFO
  2979. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2980. " CPUs=%d, Nodes=%d\n",
  2981. caches, cache_line_size(),
  2982. slub_min_order, slub_max_order, slub_min_objects,
  2983. nr_cpu_ids, nr_node_ids);
  2984. }
  2985. void __init kmem_cache_init_late(void)
  2986. {
  2987. }
  2988. /*
  2989. * Find a mergeable slab cache
  2990. */
  2991. static int slab_unmergeable(struct kmem_cache *s)
  2992. {
  2993. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2994. return 1;
  2995. if (s->ctor)
  2996. return 1;
  2997. /*
  2998. * We may have set a slab to be unmergeable during bootstrap.
  2999. */
  3000. if (s->refcount < 0)
  3001. return 1;
  3002. return 0;
  3003. }
  3004. static struct kmem_cache *find_mergeable(size_t size,
  3005. size_t align, unsigned long flags, const char *name,
  3006. void (*ctor)(void *))
  3007. {
  3008. struct kmem_cache *s;
  3009. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  3010. return NULL;
  3011. if (ctor)
  3012. return NULL;
  3013. size = ALIGN(size, sizeof(void *));
  3014. align = calculate_alignment(flags, align, size);
  3015. size = ALIGN(size, align);
  3016. flags = kmem_cache_flags(size, flags, name, NULL);
  3017. list_for_each_entry(s, &slab_caches, list) {
  3018. if (slab_unmergeable(s))
  3019. continue;
  3020. if (size > s->size)
  3021. continue;
  3022. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  3023. continue;
  3024. /*
  3025. * Check if alignment is compatible.
  3026. * Courtesy of Adrian Drzewiecki
  3027. */
  3028. if ((s->size & ~(align - 1)) != s->size)
  3029. continue;
  3030. if (s->size - size >= sizeof(void *))
  3031. continue;
  3032. return s;
  3033. }
  3034. return NULL;
  3035. }
  3036. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  3037. size_t align, unsigned long flags, void (*ctor)(void *))
  3038. {
  3039. struct kmem_cache *s;
  3040. char *n;
  3041. if (WARN_ON(!name))
  3042. return NULL;
  3043. down_write(&slub_lock);
  3044. s = find_mergeable(size, align, flags, name, ctor);
  3045. if (s) {
  3046. s->refcount++;
  3047. /*
  3048. * Adjust the object sizes so that we clear
  3049. * the complete object on kzalloc.
  3050. */
  3051. s->objsize = max(s->objsize, (int)size);
  3052. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  3053. if (sysfs_slab_alias(s, name)) {
  3054. s->refcount--;
  3055. goto err;
  3056. }
  3057. up_write(&slub_lock);
  3058. return s;
  3059. }
  3060. n = kstrdup(name, GFP_KERNEL);
  3061. if (!n)
  3062. goto err;
  3063. s = kmalloc(kmem_size, GFP_KERNEL);
  3064. if (s) {
  3065. if (kmem_cache_open(s, n,
  3066. size, align, flags, ctor)) {
  3067. list_add(&s->list, &slab_caches);
  3068. if (sysfs_slab_add(s)) {
  3069. list_del(&s->list);
  3070. kfree(n);
  3071. kfree(s);
  3072. goto err;
  3073. }
  3074. up_write(&slub_lock);
  3075. return s;
  3076. }
  3077. kfree(n);
  3078. kfree(s);
  3079. }
  3080. err:
  3081. up_write(&slub_lock);
  3082. if (flags & SLAB_PANIC)
  3083. panic("Cannot create slabcache %s\n", name);
  3084. else
  3085. s = NULL;
  3086. return s;
  3087. }
  3088. EXPORT_SYMBOL(kmem_cache_create);
  3089. #ifdef CONFIG_SMP
  3090. /*
  3091. * Use the cpu notifier to insure that the cpu slabs are flushed when
  3092. * necessary.
  3093. */
  3094. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  3095. unsigned long action, void *hcpu)
  3096. {
  3097. long cpu = (long)hcpu;
  3098. struct kmem_cache *s;
  3099. unsigned long flags;
  3100. switch (action) {
  3101. case CPU_UP_CANCELED:
  3102. case CPU_UP_CANCELED_FROZEN:
  3103. case CPU_DEAD:
  3104. case CPU_DEAD_FROZEN:
  3105. down_read(&slub_lock);
  3106. list_for_each_entry(s, &slab_caches, list) {
  3107. local_irq_save(flags);
  3108. __flush_cpu_slab(s, cpu);
  3109. local_irq_restore(flags);
  3110. }
  3111. up_read(&slub_lock);
  3112. break;
  3113. default:
  3114. break;
  3115. }
  3116. return NOTIFY_OK;
  3117. }
  3118. static struct notifier_block __cpuinitdata slab_notifier = {
  3119. .notifier_call = slab_cpuup_callback
  3120. };
  3121. #endif
  3122. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3123. {
  3124. struct kmem_cache *s;
  3125. void *ret;
  3126. if (unlikely(size > SLUB_MAX_SIZE))
  3127. return kmalloc_large(size, gfpflags);
  3128. s = get_slab(size, gfpflags);
  3129. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3130. return s;
  3131. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  3132. /* Honor the call site pointer we received. */
  3133. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3134. return ret;
  3135. }
  3136. #ifdef CONFIG_NUMA
  3137. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3138. int node, unsigned long caller)
  3139. {
  3140. struct kmem_cache *s;
  3141. void *ret;
  3142. if (unlikely(size > SLUB_MAX_SIZE)) {
  3143. ret = kmalloc_large_node(size, gfpflags, node);
  3144. trace_kmalloc_node(caller, ret,
  3145. size, PAGE_SIZE << get_order(size),
  3146. gfpflags, node);
  3147. return ret;
  3148. }
  3149. s = get_slab(size, gfpflags);
  3150. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3151. return s;
  3152. ret = slab_alloc(s, gfpflags, node, caller);
  3153. /* Honor the call site pointer we received. */
  3154. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3155. return ret;
  3156. }
  3157. #endif
  3158. #ifdef CONFIG_SYSFS
  3159. static int count_inuse(struct page *page)
  3160. {
  3161. return page->inuse;
  3162. }
  3163. static int count_total(struct page *page)
  3164. {
  3165. return page->objects;
  3166. }
  3167. #endif
  3168. #ifdef CONFIG_SLUB_DEBUG
  3169. static int validate_slab(struct kmem_cache *s, struct page *page,
  3170. unsigned long *map)
  3171. {
  3172. void *p;
  3173. void *addr = page_address(page);
  3174. if (!check_slab(s, page) ||
  3175. !on_freelist(s, page, NULL))
  3176. return 0;
  3177. /* Now we know that a valid freelist exists */
  3178. bitmap_zero(map, page->objects);
  3179. get_map(s, page, map);
  3180. for_each_object(p, s, addr, page->objects) {
  3181. if (test_bit(slab_index(p, s, addr), map))
  3182. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3183. return 0;
  3184. }
  3185. for_each_object(p, s, addr, page->objects)
  3186. if (!test_bit(slab_index(p, s, addr), map))
  3187. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3188. return 0;
  3189. return 1;
  3190. }
  3191. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3192. unsigned long *map)
  3193. {
  3194. slab_lock(page);
  3195. validate_slab(s, page, map);
  3196. slab_unlock(page);
  3197. }
  3198. static int validate_slab_node(struct kmem_cache *s,
  3199. struct kmem_cache_node *n, unsigned long *map)
  3200. {
  3201. unsigned long count = 0;
  3202. struct page *page;
  3203. unsigned long flags;
  3204. spin_lock_irqsave(&n->list_lock, flags);
  3205. list_for_each_entry(page, &n->partial, lru) {
  3206. validate_slab_slab(s, page, map);
  3207. count++;
  3208. }
  3209. if (count != n->nr_partial)
  3210. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3211. "counter=%ld\n", s->name, count, n->nr_partial);
  3212. if (!(s->flags & SLAB_STORE_USER))
  3213. goto out;
  3214. list_for_each_entry(page, &n->full, lru) {
  3215. validate_slab_slab(s, page, map);
  3216. count++;
  3217. }
  3218. if (count != atomic_long_read(&n->nr_slabs))
  3219. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3220. "counter=%ld\n", s->name, count,
  3221. atomic_long_read(&n->nr_slabs));
  3222. out:
  3223. spin_unlock_irqrestore(&n->list_lock, flags);
  3224. return count;
  3225. }
  3226. static long validate_slab_cache(struct kmem_cache *s)
  3227. {
  3228. int node;
  3229. unsigned long count = 0;
  3230. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3231. sizeof(unsigned long), GFP_KERNEL);
  3232. if (!map)
  3233. return -ENOMEM;
  3234. flush_all(s);
  3235. for_each_node_state(node, N_NORMAL_MEMORY) {
  3236. struct kmem_cache_node *n = get_node(s, node);
  3237. count += validate_slab_node(s, n, map);
  3238. }
  3239. kfree(map);
  3240. return count;
  3241. }
  3242. /*
  3243. * Generate lists of code addresses where slabcache objects are allocated
  3244. * and freed.
  3245. */
  3246. struct location {
  3247. unsigned long count;
  3248. unsigned long addr;
  3249. long long sum_time;
  3250. long min_time;
  3251. long max_time;
  3252. long min_pid;
  3253. long max_pid;
  3254. DECLARE_BITMAP(cpus, NR_CPUS);
  3255. nodemask_t nodes;
  3256. };
  3257. struct loc_track {
  3258. unsigned long max;
  3259. unsigned long count;
  3260. struct location *loc;
  3261. };
  3262. static void free_loc_track(struct loc_track *t)
  3263. {
  3264. if (t->max)
  3265. free_pages((unsigned long)t->loc,
  3266. get_order(sizeof(struct location) * t->max));
  3267. }
  3268. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3269. {
  3270. struct location *l;
  3271. int order;
  3272. order = get_order(sizeof(struct location) * max);
  3273. l = (void *)__get_free_pages(flags, order);
  3274. if (!l)
  3275. return 0;
  3276. if (t->count) {
  3277. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3278. free_loc_track(t);
  3279. }
  3280. t->max = max;
  3281. t->loc = l;
  3282. return 1;
  3283. }
  3284. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3285. const struct track *track)
  3286. {
  3287. long start, end, pos;
  3288. struct location *l;
  3289. unsigned long caddr;
  3290. unsigned long age = jiffies - track->when;
  3291. start = -1;
  3292. end = t->count;
  3293. for ( ; ; ) {
  3294. pos = start + (end - start + 1) / 2;
  3295. /*
  3296. * There is nothing at "end". If we end up there
  3297. * we need to add something to before end.
  3298. */
  3299. if (pos == end)
  3300. break;
  3301. caddr = t->loc[pos].addr;
  3302. if (track->addr == caddr) {
  3303. l = &t->loc[pos];
  3304. l->count++;
  3305. if (track->when) {
  3306. l->sum_time += age;
  3307. if (age < l->min_time)
  3308. l->min_time = age;
  3309. if (age > l->max_time)
  3310. l->max_time = age;
  3311. if (track->pid < l->min_pid)
  3312. l->min_pid = track->pid;
  3313. if (track->pid > l->max_pid)
  3314. l->max_pid = track->pid;
  3315. cpumask_set_cpu(track->cpu,
  3316. to_cpumask(l->cpus));
  3317. }
  3318. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3319. return 1;
  3320. }
  3321. if (track->addr < caddr)
  3322. end = pos;
  3323. else
  3324. start = pos;
  3325. }
  3326. /*
  3327. * Not found. Insert new tracking element.
  3328. */
  3329. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3330. return 0;
  3331. l = t->loc + pos;
  3332. if (pos < t->count)
  3333. memmove(l + 1, l,
  3334. (t->count - pos) * sizeof(struct location));
  3335. t->count++;
  3336. l->count = 1;
  3337. l->addr = track->addr;
  3338. l->sum_time = age;
  3339. l->min_time = age;
  3340. l->max_time = age;
  3341. l->min_pid = track->pid;
  3342. l->max_pid = track->pid;
  3343. cpumask_clear(to_cpumask(l->cpus));
  3344. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3345. nodes_clear(l->nodes);
  3346. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3347. return 1;
  3348. }
  3349. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3350. struct page *page, enum track_item alloc,
  3351. unsigned long *map)
  3352. {
  3353. void *addr = page_address(page);
  3354. void *p;
  3355. bitmap_zero(map, page->objects);
  3356. get_map(s, page, map);
  3357. for_each_object(p, s, addr, page->objects)
  3358. if (!test_bit(slab_index(p, s, addr), map))
  3359. add_location(t, s, get_track(s, p, alloc));
  3360. }
  3361. static int list_locations(struct kmem_cache *s, char *buf,
  3362. enum track_item alloc)
  3363. {
  3364. int len = 0;
  3365. unsigned long i;
  3366. struct loc_track t = { 0, 0, NULL };
  3367. int node;
  3368. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3369. sizeof(unsigned long), GFP_KERNEL);
  3370. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3371. GFP_TEMPORARY)) {
  3372. kfree(map);
  3373. return sprintf(buf, "Out of memory\n");
  3374. }
  3375. /* Push back cpu slabs */
  3376. flush_all(s);
  3377. for_each_node_state(node, N_NORMAL_MEMORY) {
  3378. struct kmem_cache_node *n = get_node(s, node);
  3379. unsigned long flags;
  3380. struct page *page;
  3381. if (!atomic_long_read(&n->nr_slabs))
  3382. continue;
  3383. spin_lock_irqsave(&n->list_lock, flags);
  3384. list_for_each_entry(page, &n->partial, lru)
  3385. process_slab(&t, s, page, alloc, map);
  3386. list_for_each_entry(page, &n->full, lru)
  3387. process_slab(&t, s, page, alloc, map);
  3388. spin_unlock_irqrestore(&n->list_lock, flags);
  3389. }
  3390. for (i = 0; i < t.count; i++) {
  3391. struct location *l = &t.loc[i];
  3392. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3393. break;
  3394. len += sprintf(buf + len, "%7ld ", l->count);
  3395. if (l->addr)
  3396. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3397. else
  3398. len += sprintf(buf + len, "<not-available>");
  3399. if (l->sum_time != l->min_time) {
  3400. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3401. l->min_time,
  3402. (long)div_u64(l->sum_time, l->count),
  3403. l->max_time);
  3404. } else
  3405. len += sprintf(buf + len, " age=%ld",
  3406. l->min_time);
  3407. if (l->min_pid != l->max_pid)
  3408. len += sprintf(buf + len, " pid=%ld-%ld",
  3409. l->min_pid, l->max_pid);
  3410. else
  3411. len += sprintf(buf + len, " pid=%ld",
  3412. l->min_pid);
  3413. if (num_online_cpus() > 1 &&
  3414. !cpumask_empty(to_cpumask(l->cpus)) &&
  3415. len < PAGE_SIZE - 60) {
  3416. len += sprintf(buf + len, " cpus=");
  3417. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3418. to_cpumask(l->cpus));
  3419. }
  3420. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3421. len < PAGE_SIZE - 60) {
  3422. len += sprintf(buf + len, " nodes=");
  3423. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3424. l->nodes);
  3425. }
  3426. len += sprintf(buf + len, "\n");
  3427. }
  3428. free_loc_track(&t);
  3429. kfree(map);
  3430. if (!t.count)
  3431. len += sprintf(buf, "No data\n");
  3432. return len;
  3433. }
  3434. #endif
  3435. #ifdef SLUB_RESILIENCY_TEST
  3436. static void resiliency_test(void)
  3437. {
  3438. u8 *p;
  3439. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3440. printk(KERN_ERR "SLUB resiliency testing\n");
  3441. printk(KERN_ERR "-----------------------\n");
  3442. printk(KERN_ERR "A. Corruption after allocation\n");
  3443. p = kzalloc(16, GFP_KERNEL);
  3444. p[16] = 0x12;
  3445. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3446. " 0x12->0x%p\n\n", p + 16);
  3447. validate_slab_cache(kmalloc_caches[4]);
  3448. /* Hmmm... The next two are dangerous */
  3449. p = kzalloc(32, GFP_KERNEL);
  3450. p[32 + sizeof(void *)] = 0x34;
  3451. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3452. " 0x34 -> -0x%p\n", p);
  3453. printk(KERN_ERR
  3454. "If allocated object is overwritten then not detectable\n\n");
  3455. validate_slab_cache(kmalloc_caches[5]);
  3456. p = kzalloc(64, GFP_KERNEL);
  3457. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3458. *p = 0x56;
  3459. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3460. p);
  3461. printk(KERN_ERR
  3462. "If allocated object is overwritten then not detectable\n\n");
  3463. validate_slab_cache(kmalloc_caches[6]);
  3464. printk(KERN_ERR "\nB. Corruption after free\n");
  3465. p = kzalloc(128, GFP_KERNEL);
  3466. kfree(p);
  3467. *p = 0x78;
  3468. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3469. validate_slab_cache(kmalloc_caches[7]);
  3470. p = kzalloc(256, GFP_KERNEL);
  3471. kfree(p);
  3472. p[50] = 0x9a;
  3473. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3474. p);
  3475. validate_slab_cache(kmalloc_caches[8]);
  3476. p = kzalloc(512, GFP_KERNEL);
  3477. kfree(p);
  3478. p[512] = 0xab;
  3479. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3480. validate_slab_cache(kmalloc_caches[9]);
  3481. }
  3482. #else
  3483. #ifdef CONFIG_SYSFS
  3484. static void resiliency_test(void) {};
  3485. #endif
  3486. #endif
  3487. #ifdef CONFIG_SYSFS
  3488. enum slab_stat_type {
  3489. SL_ALL, /* All slabs */
  3490. SL_PARTIAL, /* Only partially allocated slabs */
  3491. SL_CPU, /* Only slabs used for cpu caches */
  3492. SL_OBJECTS, /* Determine allocated objects not slabs */
  3493. SL_TOTAL /* Determine object capacity not slabs */
  3494. };
  3495. #define SO_ALL (1 << SL_ALL)
  3496. #define SO_PARTIAL (1 << SL_PARTIAL)
  3497. #define SO_CPU (1 << SL_CPU)
  3498. #define SO_OBJECTS (1 << SL_OBJECTS)
  3499. #define SO_TOTAL (1 << SL_TOTAL)
  3500. static ssize_t show_slab_objects(struct kmem_cache *s,
  3501. char *buf, unsigned long flags)
  3502. {
  3503. unsigned long total = 0;
  3504. int node;
  3505. int x;
  3506. unsigned long *nodes;
  3507. unsigned long *per_cpu;
  3508. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3509. if (!nodes)
  3510. return -ENOMEM;
  3511. per_cpu = nodes + nr_node_ids;
  3512. if (flags & SO_CPU) {
  3513. int cpu;
  3514. for_each_possible_cpu(cpu) {
  3515. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3516. if (!c || c->node < 0)
  3517. continue;
  3518. if (c->page) {
  3519. if (flags & SO_TOTAL)
  3520. x = c->page->objects;
  3521. else if (flags & SO_OBJECTS)
  3522. x = c->page->inuse;
  3523. else
  3524. x = 1;
  3525. total += x;
  3526. nodes[c->node] += x;
  3527. }
  3528. per_cpu[c->node]++;
  3529. }
  3530. }
  3531. lock_memory_hotplug();
  3532. #ifdef CONFIG_SLUB_DEBUG
  3533. if (flags & SO_ALL) {
  3534. for_each_node_state(node, N_NORMAL_MEMORY) {
  3535. struct kmem_cache_node *n = get_node(s, node);
  3536. if (flags & SO_TOTAL)
  3537. x = atomic_long_read(&n->total_objects);
  3538. else if (flags & SO_OBJECTS)
  3539. x = atomic_long_read(&n->total_objects) -
  3540. count_partial(n, count_free);
  3541. else
  3542. x = atomic_long_read(&n->nr_slabs);
  3543. total += x;
  3544. nodes[node] += x;
  3545. }
  3546. } else
  3547. #endif
  3548. if (flags & SO_PARTIAL) {
  3549. for_each_node_state(node, N_NORMAL_MEMORY) {
  3550. struct kmem_cache_node *n = get_node(s, node);
  3551. if (flags & SO_TOTAL)
  3552. x = count_partial(n, count_total);
  3553. else if (flags & SO_OBJECTS)
  3554. x = count_partial(n, count_inuse);
  3555. else
  3556. x = n->nr_partial;
  3557. total += x;
  3558. nodes[node] += x;
  3559. }
  3560. }
  3561. x = sprintf(buf, "%lu", total);
  3562. #ifdef CONFIG_NUMA
  3563. for_each_node_state(node, N_NORMAL_MEMORY)
  3564. if (nodes[node])
  3565. x += sprintf(buf + x, " N%d=%lu",
  3566. node, nodes[node]);
  3567. #endif
  3568. unlock_memory_hotplug();
  3569. kfree(nodes);
  3570. return x + sprintf(buf + x, "\n");
  3571. }
  3572. #ifdef CONFIG_SLUB_DEBUG
  3573. static int any_slab_objects(struct kmem_cache *s)
  3574. {
  3575. int node;
  3576. for_each_online_node(node) {
  3577. struct kmem_cache_node *n = get_node(s, node);
  3578. if (!n)
  3579. continue;
  3580. if (atomic_long_read(&n->total_objects))
  3581. return 1;
  3582. }
  3583. return 0;
  3584. }
  3585. #endif
  3586. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3587. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3588. struct slab_attribute {
  3589. struct attribute attr;
  3590. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3591. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3592. };
  3593. #define SLAB_ATTR_RO(_name) \
  3594. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3595. #define SLAB_ATTR(_name) \
  3596. static struct slab_attribute _name##_attr = \
  3597. __ATTR(_name, 0644, _name##_show, _name##_store)
  3598. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3599. {
  3600. return sprintf(buf, "%d\n", s->size);
  3601. }
  3602. SLAB_ATTR_RO(slab_size);
  3603. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3604. {
  3605. return sprintf(buf, "%d\n", s->align);
  3606. }
  3607. SLAB_ATTR_RO(align);
  3608. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3609. {
  3610. return sprintf(buf, "%d\n", s->objsize);
  3611. }
  3612. SLAB_ATTR_RO(object_size);
  3613. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3614. {
  3615. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3616. }
  3617. SLAB_ATTR_RO(objs_per_slab);
  3618. static ssize_t order_store(struct kmem_cache *s,
  3619. const char *buf, size_t length)
  3620. {
  3621. unsigned long order;
  3622. int err;
  3623. err = strict_strtoul(buf, 10, &order);
  3624. if (err)
  3625. return err;
  3626. if (order > slub_max_order || order < slub_min_order)
  3627. return -EINVAL;
  3628. calculate_sizes(s, order);
  3629. return length;
  3630. }
  3631. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3632. {
  3633. return sprintf(buf, "%d\n", oo_order(s->oo));
  3634. }
  3635. SLAB_ATTR(order);
  3636. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3637. {
  3638. return sprintf(buf, "%lu\n", s->min_partial);
  3639. }
  3640. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3641. size_t length)
  3642. {
  3643. unsigned long min;
  3644. int err;
  3645. err = strict_strtoul(buf, 10, &min);
  3646. if (err)
  3647. return err;
  3648. set_min_partial(s, min);
  3649. return length;
  3650. }
  3651. SLAB_ATTR(min_partial);
  3652. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3653. {
  3654. if (!s->ctor)
  3655. return 0;
  3656. return sprintf(buf, "%pS\n", s->ctor);
  3657. }
  3658. SLAB_ATTR_RO(ctor);
  3659. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3660. {
  3661. return sprintf(buf, "%d\n", s->refcount - 1);
  3662. }
  3663. SLAB_ATTR_RO(aliases);
  3664. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3665. {
  3666. return show_slab_objects(s, buf, SO_PARTIAL);
  3667. }
  3668. SLAB_ATTR_RO(partial);
  3669. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3670. {
  3671. return show_slab_objects(s, buf, SO_CPU);
  3672. }
  3673. SLAB_ATTR_RO(cpu_slabs);
  3674. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3675. {
  3676. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3677. }
  3678. SLAB_ATTR_RO(objects);
  3679. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3680. {
  3681. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3682. }
  3683. SLAB_ATTR_RO(objects_partial);
  3684. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3685. {
  3686. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3687. }
  3688. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3689. const char *buf, size_t length)
  3690. {
  3691. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3692. if (buf[0] == '1')
  3693. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3694. return length;
  3695. }
  3696. SLAB_ATTR(reclaim_account);
  3697. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3698. {
  3699. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3700. }
  3701. SLAB_ATTR_RO(hwcache_align);
  3702. #ifdef CONFIG_ZONE_DMA
  3703. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3704. {
  3705. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3706. }
  3707. SLAB_ATTR_RO(cache_dma);
  3708. #endif
  3709. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3710. {
  3711. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3712. }
  3713. SLAB_ATTR_RO(destroy_by_rcu);
  3714. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  3715. {
  3716. return sprintf(buf, "%d\n", s->reserved);
  3717. }
  3718. SLAB_ATTR_RO(reserved);
  3719. #ifdef CONFIG_SLUB_DEBUG
  3720. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3721. {
  3722. return show_slab_objects(s, buf, SO_ALL);
  3723. }
  3724. SLAB_ATTR_RO(slabs);
  3725. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3726. {
  3727. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3728. }
  3729. SLAB_ATTR_RO(total_objects);
  3730. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3731. {
  3732. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3733. }
  3734. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3735. const char *buf, size_t length)
  3736. {
  3737. s->flags &= ~SLAB_DEBUG_FREE;
  3738. if (buf[0] == '1') {
  3739. s->flags &= ~__CMPXCHG_DOUBLE;
  3740. s->flags |= SLAB_DEBUG_FREE;
  3741. }
  3742. return length;
  3743. }
  3744. SLAB_ATTR(sanity_checks);
  3745. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3746. {
  3747. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3748. }
  3749. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3750. size_t length)
  3751. {
  3752. s->flags &= ~SLAB_TRACE;
  3753. if (buf[0] == '1') {
  3754. s->flags &= ~__CMPXCHG_DOUBLE;
  3755. s->flags |= SLAB_TRACE;
  3756. }
  3757. return length;
  3758. }
  3759. SLAB_ATTR(trace);
  3760. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3761. {
  3762. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3763. }
  3764. static ssize_t red_zone_store(struct kmem_cache *s,
  3765. const char *buf, size_t length)
  3766. {
  3767. if (any_slab_objects(s))
  3768. return -EBUSY;
  3769. s->flags &= ~SLAB_RED_ZONE;
  3770. if (buf[0] == '1') {
  3771. s->flags &= ~__CMPXCHG_DOUBLE;
  3772. s->flags |= SLAB_RED_ZONE;
  3773. }
  3774. calculate_sizes(s, -1);
  3775. return length;
  3776. }
  3777. SLAB_ATTR(red_zone);
  3778. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3779. {
  3780. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3781. }
  3782. static ssize_t poison_store(struct kmem_cache *s,
  3783. const char *buf, size_t length)
  3784. {
  3785. if (any_slab_objects(s))
  3786. return -EBUSY;
  3787. s->flags &= ~SLAB_POISON;
  3788. if (buf[0] == '1') {
  3789. s->flags &= ~__CMPXCHG_DOUBLE;
  3790. s->flags |= SLAB_POISON;
  3791. }
  3792. calculate_sizes(s, -1);
  3793. return length;
  3794. }
  3795. SLAB_ATTR(poison);
  3796. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3797. {
  3798. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3799. }
  3800. static ssize_t store_user_store(struct kmem_cache *s,
  3801. const char *buf, size_t length)
  3802. {
  3803. if (any_slab_objects(s))
  3804. return -EBUSY;
  3805. s->flags &= ~SLAB_STORE_USER;
  3806. if (buf[0] == '1') {
  3807. s->flags &= ~__CMPXCHG_DOUBLE;
  3808. s->flags |= SLAB_STORE_USER;
  3809. }
  3810. calculate_sizes(s, -1);
  3811. return length;
  3812. }
  3813. SLAB_ATTR(store_user);
  3814. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3815. {
  3816. return 0;
  3817. }
  3818. static ssize_t validate_store(struct kmem_cache *s,
  3819. const char *buf, size_t length)
  3820. {
  3821. int ret = -EINVAL;
  3822. if (buf[0] == '1') {
  3823. ret = validate_slab_cache(s);
  3824. if (ret >= 0)
  3825. ret = length;
  3826. }
  3827. return ret;
  3828. }
  3829. SLAB_ATTR(validate);
  3830. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3831. {
  3832. if (!(s->flags & SLAB_STORE_USER))
  3833. return -ENOSYS;
  3834. return list_locations(s, buf, TRACK_ALLOC);
  3835. }
  3836. SLAB_ATTR_RO(alloc_calls);
  3837. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3838. {
  3839. if (!(s->flags & SLAB_STORE_USER))
  3840. return -ENOSYS;
  3841. return list_locations(s, buf, TRACK_FREE);
  3842. }
  3843. SLAB_ATTR_RO(free_calls);
  3844. #endif /* CONFIG_SLUB_DEBUG */
  3845. #ifdef CONFIG_FAILSLAB
  3846. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  3847. {
  3848. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  3849. }
  3850. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  3851. size_t length)
  3852. {
  3853. s->flags &= ~SLAB_FAILSLAB;
  3854. if (buf[0] == '1')
  3855. s->flags |= SLAB_FAILSLAB;
  3856. return length;
  3857. }
  3858. SLAB_ATTR(failslab);
  3859. #endif
  3860. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3861. {
  3862. return 0;
  3863. }
  3864. static ssize_t shrink_store(struct kmem_cache *s,
  3865. const char *buf, size_t length)
  3866. {
  3867. if (buf[0] == '1') {
  3868. int rc = kmem_cache_shrink(s);
  3869. if (rc)
  3870. return rc;
  3871. } else
  3872. return -EINVAL;
  3873. return length;
  3874. }
  3875. SLAB_ATTR(shrink);
  3876. #ifdef CONFIG_NUMA
  3877. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3878. {
  3879. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3880. }
  3881. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3882. const char *buf, size_t length)
  3883. {
  3884. unsigned long ratio;
  3885. int err;
  3886. err = strict_strtoul(buf, 10, &ratio);
  3887. if (err)
  3888. return err;
  3889. if (ratio <= 100)
  3890. s->remote_node_defrag_ratio = ratio * 10;
  3891. return length;
  3892. }
  3893. SLAB_ATTR(remote_node_defrag_ratio);
  3894. #endif
  3895. #ifdef CONFIG_SLUB_STATS
  3896. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  3897. {
  3898. unsigned long sum = 0;
  3899. int cpu;
  3900. int len;
  3901. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  3902. if (!data)
  3903. return -ENOMEM;
  3904. for_each_online_cpu(cpu) {
  3905. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  3906. data[cpu] = x;
  3907. sum += x;
  3908. }
  3909. len = sprintf(buf, "%lu", sum);
  3910. #ifdef CONFIG_SMP
  3911. for_each_online_cpu(cpu) {
  3912. if (data[cpu] && len < PAGE_SIZE - 20)
  3913. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  3914. }
  3915. #endif
  3916. kfree(data);
  3917. return len + sprintf(buf + len, "\n");
  3918. }
  3919. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  3920. {
  3921. int cpu;
  3922. for_each_online_cpu(cpu)
  3923. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  3924. }
  3925. #define STAT_ATTR(si, text) \
  3926. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  3927. { \
  3928. return show_stat(s, buf, si); \
  3929. } \
  3930. static ssize_t text##_store(struct kmem_cache *s, \
  3931. const char *buf, size_t length) \
  3932. { \
  3933. if (buf[0] != '0') \
  3934. return -EINVAL; \
  3935. clear_stat(s, si); \
  3936. return length; \
  3937. } \
  3938. SLAB_ATTR(text); \
  3939. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  3940. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  3941. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  3942. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  3943. STAT_ATTR(FREE_FROZEN, free_frozen);
  3944. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  3945. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  3946. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  3947. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  3948. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  3949. STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
  3950. STAT_ATTR(FREE_SLAB, free_slab);
  3951. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  3952. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  3953. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  3954. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  3955. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  3956. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  3957. STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
  3958. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  3959. STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
  3960. STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
  3961. #endif
  3962. static struct attribute *slab_attrs[] = {
  3963. &slab_size_attr.attr,
  3964. &object_size_attr.attr,
  3965. &objs_per_slab_attr.attr,
  3966. &order_attr.attr,
  3967. &min_partial_attr.attr,
  3968. &objects_attr.attr,
  3969. &objects_partial_attr.attr,
  3970. &partial_attr.attr,
  3971. &cpu_slabs_attr.attr,
  3972. &ctor_attr.attr,
  3973. &aliases_attr.attr,
  3974. &align_attr.attr,
  3975. &hwcache_align_attr.attr,
  3976. &reclaim_account_attr.attr,
  3977. &destroy_by_rcu_attr.attr,
  3978. &shrink_attr.attr,
  3979. &reserved_attr.attr,
  3980. #ifdef CONFIG_SLUB_DEBUG
  3981. &total_objects_attr.attr,
  3982. &slabs_attr.attr,
  3983. &sanity_checks_attr.attr,
  3984. &trace_attr.attr,
  3985. &red_zone_attr.attr,
  3986. &poison_attr.attr,
  3987. &store_user_attr.attr,
  3988. &validate_attr.attr,
  3989. &alloc_calls_attr.attr,
  3990. &free_calls_attr.attr,
  3991. #endif
  3992. #ifdef CONFIG_ZONE_DMA
  3993. &cache_dma_attr.attr,
  3994. #endif
  3995. #ifdef CONFIG_NUMA
  3996. &remote_node_defrag_ratio_attr.attr,
  3997. #endif
  3998. #ifdef CONFIG_SLUB_STATS
  3999. &alloc_fastpath_attr.attr,
  4000. &alloc_slowpath_attr.attr,
  4001. &free_fastpath_attr.attr,
  4002. &free_slowpath_attr.attr,
  4003. &free_frozen_attr.attr,
  4004. &free_add_partial_attr.attr,
  4005. &free_remove_partial_attr.attr,
  4006. &alloc_from_partial_attr.attr,
  4007. &alloc_slab_attr.attr,
  4008. &alloc_refill_attr.attr,
  4009. &alloc_node_mismatch_attr.attr,
  4010. &free_slab_attr.attr,
  4011. &cpuslab_flush_attr.attr,
  4012. &deactivate_full_attr.attr,
  4013. &deactivate_empty_attr.attr,
  4014. &deactivate_to_head_attr.attr,
  4015. &deactivate_to_tail_attr.attr,
  4016. &deactivate_remote_frees_attr.attr,
  4017. &deactivate_bypass_attr.attr,
  4018. &order_fallback_attr.attr,
  4019. &cmpxchg_double_fail_attr.attr,
  4020. &cmpxchg_double_cpu_fail_attr.attr,
  4021. #endif
  4022. #ifdef CONFIG_FAILSLAB
  4023. &failslab_attr.attr,
  4024. #endif
  4025. NULL
  4026. };
  4027. static struct attribute_group slab_attr_group = {
  4028. .attrs = slab_attrs,
  4029. };
  4030. static ssize_t slab_attr_show(struct kobject *kobj,
  4031. struct attribute *attr,
  4032. char *buf)
  4033. {
  4034. struct slab_attribute *attribute;
  4035. struct kmem_cache *s;
  4036. int err;
  4037. attribute = to_slab_attr(attr);
  4038. s = to_slab(kobj);
  4039. if (!attribute->show)
  4040. return -EIO;
  4041. err = attribute->show(s, buf);
  4042. return err;
  4043. }
  4044. static ssize_t slab_attr_store(struct kobject *kobj,
  4045. struct attribute *attr,
  4046. const char *buf, size_t len)
  4047. {
  4048. struct slab_attribute *attribute;
  4049. struct kmem_cache *s;
  4050. int err;
  4051. attribute = to_slab_attr(attr);
  4052. s = to_slab(kobj);
  4053. if (!attribute->store)
  4054. return -EIO;
  4055. err = attribute->store(s, buf, len);
  4056. return err;
  4057. }
  4058. static void kmem_cache_release(struct kobject *kobj)
  4059. {
  4060. struct kmem_cache *s = to_slab(kobj);
  4061. kfree(s->name);
  4062. kfree(s);
  4063. }
  4064. static const struct sysfs_ops slab_sysfs_ops = {
  4065. .show = slab_attr_show,
  4066. .store = slab_attr_store,
  4067. };
  4068. static struct kobj_type slab_ktype = {
  4069. .sysfs_ops = &slab_sysfs_ops,
  4070. .release = kmem_cache_release
  4071. };
  4072. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  4073. {
  4074. struct kobj_type *ktype = get_ktype(kobj);
  4075. if (ktype == &slab_ktype)
  4076. return 1;
  4077. return 0;
  4078. }
  4079. static const struct kset_uevent_ops slab_uevent_ops = {
  4080. .filter = uevent_filter,
  4081. };
  4082. static struct kset *slab_kset;
  4083. #define ID_STR_LENGTH 64
  4084. /* Create a unique string id for a slab cache:
  4085. *
  4086. * Format :[flags-]size
  4087. */
  4088. static char *create_unique_id(struct kmem_cache *s)
  4089. {
  4090. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  4091. char *p = name;
  4092. BUG_ON(!name);
  4093. *p++ = ':';
  4094. /*
  4095. * First flags affecting slabcache operations. We will only
  4096. * get here for aliasable slabs so we do not need to support
  4097. * too many flags. The flags here must cover all flags that
  4098. * are matched during merging to guarantee that the id is
  4099. * unique.
  4100. */
  4101. if (s->flags & SLAB_CACHE_DMA)
  4102. *p++ = 'd';
  4103. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  4104. *p++ = 'a';
  4105. if (s->flags & SLAB_DEBUG_FREE)
  4106. *p++ = 'F';
  4107. if (!(s->flags & SLAB_NOTRACK))
  4108. *p++ = 't';
  4109. if (p != name + 1)
  4110. *p++ = '-';
  4111. p += sprintf(p, "%07d", s->size);
  4112. BUG_ON(p > name + ID_STR_LENGTH - 1);
  4113. return name;
  4114. }
  4115. static int sysfs_slab_add(struct kmem_cache *s)
  4116. {
  4117. int err;
  4118. const char *name;
  4119. int unmergeable;
  4120. if (slab_state < SYSFS)
  4121. /* Defer until later */
  4122. return 0;
  4123. unmergeable = slab_unmergeable(s);
  4124. if (unmergeable) {
  4125. /*
  4126. * Slabcache can never be merged so we can use the name proper.
  4127. * This is typically the case for debug situations. In that
  4128. * case we can catch duplicate names easily.
  4129. */
  4130. sysfs_remove_link(&slab_kset->kobj, s->name);
  4131. name = s->name;
  4132. } else {
  4133. /*
  4134. * Create a unique name for the slab as a target
  4135. * for the symlinks.
  4136. */
  4137. name = create_unique_id(s);
  4138. }
  4139. s->kobj.kset = slab_kset;
  4140. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  4141. if (err) {
  4142. kobject_put(&s->kobj);
  4143. return err;
  4144. }
  4145. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4146. if (err) {
  4147. kobject_del(&s->kobj);
  4148. kobject_put(&s->kobj);
  4149. return err;
  4150. }
  4151. kobject_uevent(&s->kobj, KOBJ_ADD);
  4152. if (!unmergeable) {
  4153. /* Setup first alias */
  4154. sysfs_slab_alias(s, s->name);
  4155. kfree(name);
  4156. }
  4157. return 0;
  4158. }
  4159. static void sysfs_slab_remove(struct kmem_cache *s)
  4160. {
  4161. if (slab_state < SYSFS)
  4162. /*
  4163. * Sysfs has not been setup yet so no need to remove the
  4164. * cache from sysfs.
  4165. */
  4166. return;
  4167. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4168. kobject_del(&s->kobj);
  4169. kobject_put(&s->kobj);
  4170. }
  4171. /*
  4172. * Need to buffer aliases during bootup until sysfs becomes
  4173. * available lest we lose that information.
  4174. */
  4175. struct saved_alias {
  4176. struct kmem_cache *s;
  4177. const char *name;
  4178. struct saved_alias *next;
  4179. };
  4180. static struct saved_alias *alias_list;
  4181. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4182. {
  4183. struct saved_alias *al;
  4184. if (slab_state == SYSFS) {
  4185. /*
  4186. * If we have a leftover link then remove it.
  4187. */
  4188. sysfs_remove_link(&slab_kset->kobj, name);
  4189. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4190. }
  4191. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4192. if (!al)
  4193. return -ENOMEM;
  4194. al->s = s;
  4195. al->name = name;
  4196. al->next = alias_list;
  4197. alias_list = al;
  4198. return 0;
  4199. }
  4200. static int __init slab_sysfs_init(void)
  4201. {
  4202. struct kmem_cache *s;
  4203. int err;
  4204. down_write(&slub_lock);
  4205. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4206. if (!slab_kset) {
  4207. up_write(&slub_lock);
  4208. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4209. return -ENOSYS;
  4210. }
  4211. slab_state = SYSFS;
  4212. list_for_each_entry(s, &slab_caches, list) {
  4213. err = sysfs_slab_add(s);
  4214. if (err)
  4215. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4216. " to sysfs\n", s->name);
  4217. }
  4218. while (alias_list) {
  4219. struct saved_alias *al = alias_list;
  4220. alias_list = alias_list->next;
  4221. err = sysfs_slab_alias(al->s, al->name);
  4222. if (err)
  4223. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4224. " %s to sysfs\n", s->name);
  4225. kfree(al);
  4226. }
  4227. up_write(&slub_lock);
  4228. resiliency_test();
  4229. return 0;
  4230. }
  4231. __initcall(slab_sysfs_init);
  4232. #endif /* CONFIG_SYSFS */
  4233. /*
  4234. * The /proc/slabinfo ABI
  4235. */
  4236. #ifdef CONFIG_SLABINFO
  4237. static void print_slabinfo_header(struct seq_file *m)
  4238. {
  4239. seq_puts(m, "slabinfo - version: 2.1\n");
  4240. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4241. "<objperslab> <pagesperslab>");
  4242. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4243. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4244. seq_putc(m, '\n');
  4245. }
  4246. static void *s_start(struct seq_file *m, loff_t *pos)
  4247. {
  4248. loff_t n = *pos;
  4249. down_read(&slub_lock);
  4250. if (!n)
  4251. print_slabinfo_header(m);
  4252. return seq_list_start(&slab_caches, *pos);
  4253. }
  4254. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4255. {
  4256. return seq_list_next(p, &slab_caches, pos);
  4257. }
  4258. static void s_stop(struct seq_file *m, void *p)
  4259. {
  4260. up_read(&slub_lock);
  4261. }
  4262. static int s_show(struct seq_file *m, void *p)
  4263. {
  4264. unsigned long nr_partials = 0;
  4265. unsigned long nr_slabs = 0;
  4266. unsigned long nr_inuse = 0;
  4267. unsigned long nr_objs = 0;
  4268. unsigned long nr_free = 0;
  4269. struct kmem_cache *s;
  4270. int node;
  4271. s = list_entry(p, struct kmem_cache, list);
  4272. for_each_online_node(node) {
  4273. struct kmem_cache_node *n = get_node(s, node);
  4274. if (!n)
  4275. continue;
  4276. nr_partials += n->nr_partial;
  4277. nr_slabs += atomic_long_read(&n->nr_slabs);
  4278. nr_objs += atomic_long_read(&n->total_objects);
  4279. nr_free += count_partial(n, count_free);
  4280. }
  4281. nr_inuse = nr_objs - nr_free;
  4282. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4283. nr_objs, s->size, oo_objects(s->oo),
  4284. (1 << oo_order(s->oo)));
  4285. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4286. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4287. 0UL);
  4288. seq_putc(m, '\n');
  4289. return 0;
  4290. }
  4291. static const struct seq_operations slabinfo_op = {
  4292. .start = s_start,
  4293. .next = s_next,
  4294. .stop = s_stop,
  4295. .show = s_show,
  4296. };
  4297. static int slabinfo_open(struct inode *inode, struct file *file)
  4298. {
  4299. return seq_open(file, &slabinfo_op);
  4300. }
  4301. static const struct file_operations proc_slabinfo_operations = {
  4302. .open = slabinfo_open,
  4303. .read = seq_read,
  4304. .llseek = seq_lseek,
  4305. .release = seq_release,
  4306. };
  4307. static int __init slab_proc_init(void)
  4308. {
  4309. proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
  4310. return 0;
  4311. }
  4312. module_init(slab_proc_init);
  4313. #endif /* CONFIG_SLABINFO */