slub.c 102 KB

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