slub.c 105 KB

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