slub.c 105 KB

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