slub.c 104 KB

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