slub.c 106 KB

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