slub.c 103 KB

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