slab.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373
  1. /*
  2. * linux/mm/slab.c
  3. * Written by Mark Hemment, 1996/97.
  4. * (markhe@nextd.demon.co.uk)
  5. *
  6. * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
  7. *
  8. * Major cleanup, different bufctl logic, per-cpu arrays
  9. * (c) 2000 Manfred Spraul
  10. *
  11. * Cleanup, make the head arrays unconditional, preparation for NUMA
  12. * (c) 2002 Manfred Spraul
  13. *
  14. * An implementation of the Slab Allocator as described in outline in;
  15. * UNIX Internals: The New Frontiers by Uresh Vahalia
  16. * Pub: Prentice Hall ISBN 0-13-101908-2
  17. * or with a little more detail in;
  18. * The Slab Allocator: An Object-Caching Kernel Memory Allocator
  19. * Jeff Bonwick (Sun Microsystems).
  20. * Presented at: USENIX Summer 1994 Technical Conference
  21. *
  22. * The memory is organized in caches, one cache for each object type.
  23. * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
  24. * Each cache consists out of many slabs (they are small (usually one
  25. * page long) and always contiguous), and each slab contains multiple
  26. * initialized objects.
  27. *
  28. * This means, that your constructor is used only for newly allocated
  29. * slabs and you must pass objects with the same intializations to
  30. * kmem_cache_free.
  31. *
  32. * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
  33. * normal). If you need a special memory type, then must create a new
  34. * cache for that memory type.
  35. *
  36. * In order to reduce fragmentation, the slabs are sorted in 3 groups:
  37. * full slabs with 0 free objects
  38. * partial slabs
  39. * empty slabs with no allocated objects
  40. *
  41. * If partial slabs exist, then new allocations come from these slabs,
  42. * otherwise from empty slabs or new slabs are allocated.
  43. *
  44. * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
  45. * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
  46. *
  47. * Each cache has a short per-cpu head array, most allocs
  48. * and frees go into that array, and if that array overflows, then 1/2
  49. * of the entries in the array are given back into the global cache.
  50. * The head array is strictly LIFO and should improve the cache hit rates.
  51. * On SMP, it additionally reduces the spinlock operations.
  52. *
  53. * The c_cpuarray may not be read with enabled local interrupts -
  54. * it's changed with a smp_call_function().
  55. *
  56. * SMP synchronization:
  57. * constructors and destructors are called without any locking.
  58. * Several members in struct kmem_cache and struct slab never change, they
  59. * are accessed without any locking.
  60. * The per-cpu arrays are never accessed from the wrong cpu, no locking,
  61. * and local interrupts are disabled so slab code is preempt-safe.
  62. * The non-constant members are protected with a per-cache irq spinlock.
  63. *
  64. * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
  65. * in 2000 - many ideas in the current implementation are derived from
  66. * his patch.
  67. *
  68. * Further notes from the original documentation:
  69. *
  70. * 11 April '97. Started multi-threading - markhe
  71. * The global cache-chain is protected by the mutex 'cache_chain_mutex'.
  72. * The sem is only needed when accessing/extending the cache-chain, which
  73. * can never happen inside an interrupt (kmem_cache_create(),
  74. * kmem_cache_shrink() and kmem_cache_reap()).
  75. *
  76. * At present, each engine can be growing a cache. This should be blocked.
  77. *
  78. * 15 March 2005. NUMA slab allocator.
  79. * Shai Fultheim <shai@scalex86.org>.
  80. * Shobhit Dayal <shobhit@calsoftinc.com>
  81. * Alok N Kataria <alokk@calsoftinc.com>
  82. * Christoph Lameter <christoph@lameter.com>
  83. *
  84. * Modified the slab allocator to be node aware on NUMA systems.
  85. * Each node has its own list of partial, free and full slabs.
  86. * All object allocations for a node occur from node specific slab lists.
  87. */
  88. #include <linux/slab.h>
  89. #include <linux/mm.h>
  90. #include <linux/poison.h>
  91. #include <linux/swap.h>
  92. #include <linux/cache.h>
  93. #include <linux/interrupt.h>
  94. #include <linux/init.h>
  95. #include <linux/compiler.h>
  96. #include <linux/cpuset.h>
  97. #include <linux/seq_file.h>
  98. #include <linux/notifier.h>
  99. #include <linux/kallsyms.h>
  100. #include <linux/cpu.h>
  101. #include <linux/sysctl.h>
  102. #include <linux/module.h>
  103. #include <linux/rcupdate.h>
  104. #include <linux/string.h>
  105. #include <linux/nodemask.h>
  106. #include <linux/mempolicy.h>
  107. #include <linux/mutex.h>
  108. #include <linux/rtmutex.h>
  109. #include <asm/uaccess.h>
  110. #include <asm/cacheflush.h>
  111. #include <asm/tlbflush.h>
  112. #include <asm/page.h>
  113. /*
  114. * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
  115. * SLAB_RED_ZONE & SLAB_POISON.
  116. * 0 for faster, smaller code (especially in the critical paths).
  117. *
  118. * STATS - 1 to collect stats for /proc/slabinfo.
  119. * 0 for faster, smaller code (especially in the critical paths).
  120. *
  121. * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
  122. */
  123. #ifdef CONFIG_DEBUG_SLAB
  124. #define DEBUG 1
  125. #define STATS 1
  126. #define FORCED_DEBUG 1
  127. #else
  128. #define DEBUG 0
  129. #define STATS 0
  130. #define FORCED_DEBUG 0
  131. #endif
  132. /* Shouldn't this be in a header file somewhere? */
  133. #define BYTES_PER_WORD sizeof(void *)
  134. #ifndef cache_line_size
  135. #define cache_line_size() L1_CACHE_BYTES
  136. #endif
  137. #ifndef ARCH_KMALLOC_MINALIGN
  138. /*
  139. * Enforce a minimum alignment for the kmalloc caches.
  140. * Usually, the kmalloc caches are cache_line_size() aligned, except when
  141. * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
  142. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  143. * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
  144. * Note that this flag disables some debug features.
  145. */
  146. #define ARCH_KMALLOC_MINALIGN 0
  147. #endif
  148. #ifndef ARCH_SLAB_MINALIGN
  149. /*
  150. * Enforce a minimum alignment for all caches.
  151. * Intended for archs that get misalignment faults even for BYTES_PER_WORD
  152. * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
  153. * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
  154. * some debug features.
  155. */
  156. #define ARCH_SLAB_MINALIGN 0
  157. #endif
  158. #ifndef ARCH_KMALLOC_FLAGS
  159. #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  160. #endif
  161. /* Legal flag mask for kmem_cache_create(). */
  162. #if DEBUG
  163. # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
  164. SLAB_POISON | SLAB_HWCACHE_ALIGN | \
  165. SLAB_CACHE_DMA | \
  166. SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
  167. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  168. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
  169. #else
  170. # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
  171. SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
  172. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  173. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
  174. #endif
  175. /*
  176. * kmem_bufctl_t:
  177. *
  178. * Bufctl's are used for linking objs within a slab
  179. * linked offsets.
  180. *
  181. * This implementation relies on "struct page" for locating the cache &
  182. * slab an object belongs to.
  183. * This allows the bufctl structure to be small (one int), but limits
  184. * the number of objects a slab (not a cache) can contain when off-slab
  185. * bufctls are used. The limit is the size of the largest general cache
  186. * that does not use off-slab slabs.
  187. * For 32bit archs with 4 kB pages, is this 56.
  188. * This is not serious, as it is only for large objects, when it is unwise
  189. * to have too many per slab.
  190. * Note: This limit can be raised by introducing a general cache whose size
  191. * is less than 512 (PAGE_SIZE<<3), but greater than 256.
  192. */
  193. typedef unsigned int kmem_bufctl_t;
  194. #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
  195. #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
  196. #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
  197. #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
  198. /*
  199. * struct slab
  200. *
  201. * Manages the objs in a slab. Placed either at the beginning of mem allocated
  202. * for a slab, or allocated from an general cache.
  203. * Slabs are chained into three list: fully used, partial, fully free slabs.
  204. */
  205. struct slab {
  206. struct list_head list;
  207. unsigned long colouroff;
  208. void *s_mem; /* including colour offset */
  209. unsigned int inuse; /* num of objs active in slab */
  210. kmem_bufctl_t free;
  211. unsigned short nodeid;
  212. };
  213. /*
  214. * struct slab_rcu
  215. *
  216. * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
  217. * arrange for kmem_freepages to be called via RCU. This is useful if
  218. * we need to approach a kernel structure obliquely, from its address
  219. * obtained without the usual locking. We can lock the structure to
  220. * stabilize it and check it's still at the given address, only if we
  221. * can be sure that the memory has not been meanwhile reused for some
  222. * other kind of object (which our subsystem's lock might corrupt).
  223. *
  224. * rcu_read_lock before reading the address, then rcu_read_unlock after
  225. * taking the spinlock within the structure expected at that address.
  226. *
  227. * We assume struct slab_rcu can overlay struct slab when destroying.
  228. */
  229. struct slab_rcu {
  230. struct rcu_head head;
  231. struct kmem_cache *cachep;
  232. void *addr;
  233. };
  234. /*
  235. * struct array_cache
  236. *
  237. * Purpose:
  238. * - LIFO ordering, to hand out cache-warm objects from _alloc
  239. * - reduce the number of linked list operations
  240. * - reduce spinlock operations
  241. *
  242. * The limit is stored in the per-cpu structure to reduce the data cache
  243. * footprint.
  244. *
  245. */
  246. struct array_cache {
  247. unsigned int avail;
  248. unsigned int limit;
  249. unsigned int batchcount;
  250. unsigned int touched;
  251. spinlock_t lock;
  252. void *entry[0]; /*
  253. * Must have this definition in here for the proper
  254. * alignment of array_cache. Also simplifies accessing
  255. * the entries.
  256. * [0] is for gcc 2.95. It should really be [].
  257. */
  258. };
  259. /*
  260. * bootstrap: The caches do not work without cpuarrays anymore, but the
  261. * cpuarrays are allocated from the generic caches...
  262. */
  263. #define BOOT_CPUCACHE_ENTRIES 1
  264. struct arraycache_init {
  265. struct array_cache cache;
  266. void *entries[BOOT_CPUCACHE_ENTRIES];
  267. };
  268. /*
  269. * The slab lists for all objects.
  270. */
  271. struct kmem_list3 {
  272. struct list_head slabs_partial; /* partial list first, better asm code */
  273. struct list_head slabs_full;
  274. struct list_head slabs_free;
  275. unsigned long free_objects;
  276. unsigned int free_limit;
  277. unsigned int colour_next; /* Per-node cache coloring */
  278. spinlock_t list_lock;
  279. struct array_cache *shared; /* shared per node */
  280. struct array_cache **alien; /* on other nodes */
  281. unsigned long next_reap; /* updated without locking */
  282. int free_touched; /* updated without locking */
  283. };
  284. /*
  285. * Need this for bootstrapping a per node allocator.
  286. */
  287. #define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
  288. struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
  289. #define CACHE_CACHE 0
  290. #define SIZE_AC 1
  291. #define SIZE_L3 (1 + MAX_NUMNODES)
  292. static int drain_freelist(struct kmem_cache *cache,
  293. struct kmem_list3 *l3, int tofree);
  294. static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  295. int node);
  296. static int enable_cpucache(struct kmem_cache *cachep);
  297. static void cache_reap(struct work_struct *unused);
  298. /*
  299. * This function must be completely optimized away if a constant is passed to
  300. * it. Mostly the same as what is in linux/slab.h except it returns an index.
  301. */
  302. static __always_inline int index_of(const size_t size)
  303. {
  304. extern void __bad_size(void);
  305. if (__builtin_constant_p(size)) {
  306. int i = 0;
  307. #define CACHE(x) \
  308. if (size <=x) \
  309. return i; \
  310. else \
  311. i++;
  312. #include "linux/kmalloc_sizes.h"
  313. #undef CACHE
  314. __bad_size();
  315. } else
  316. __bad_size();
  317. return 0;
  318. }
  319. static int slab_early_init = 1;
  320. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  321. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  322. static void kmem_list3_init(struct kmem_list3 *parent)
  323. {
  324. INIT_LIST_HEAD(&parent->slabs_full);
  325. INIT_LIST_HEAD(&parent->slabs_partial);
  326. INIT_LIST_HEAD(&parent->slabs_free);
  327. parent->shared = NULL;
  328. parent->alien = NULL;
  329. parent->colour_next = 0;
  330. spin_lock_init(&parent->list_lock);
  331. parent->free_objects = 0;
  332. parent->free_touched = 0;
  333. }
  334. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  335. do { \
  336. INIT_LIST_HEAD(listp); \
  337. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  338. } while (0)
  339. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  340. do { \
  341. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  342. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  343. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  344. } while (0)
  345. /*
  346. * struct kmem_cache
  347. *
  348. * manages a cache.
  349. */
  350. struct kmem_cache {
  351. /* 1) per-cpu data, touched during every alloc/free */
  352. struct array_cache *array[NR_CPUS];
  353. /* 2) Cache tunables. Protected by cache_chain_mutex */
  354. unsigned int batchcount;
  355. unsigned int limit;
  356. unsigned int shared;
  357. unsigned int buffer_size;
  358. /* 3) touched by every alloc & free from the backend */
  359. struct kmem_list3 *nodelists[MAX_NUMNODES];
  360. unsigned int flags; /* constant flags */
  361. unsigned int num; /* # of objs per slab */
  362. /* 4) cache_grow/shrink */
  363. /* order of pgs per slab (2^n) */
  364. unsigned int gfporder;
  365. /* force GFP flags, e.g. GFP_DMA */
  366. gfp_t gfpflags;
  367. size_t colour; /* cache colouring range */
  368. unsigned int colour_off; /* colour offset */
  369. struct kmem_cache *slabp_cache;
  370. unsigned int slab_size;
  371. unsigned int dflags; /* dynamic flags */
  372. /* constructor func */
  373. void (*ctor) (void *, struct kmem_cache *, unsigned long);
  374. /* de-constructor func */
  375. void (*dtor) (void *, struct kmem_cache *, unsigned long);
  376. /* 5) cache creation/removal */
  377. const char *name;
  378. struct list_head next;
  379. /* 6) statistics */
  380. #if STATS
  381. unsigned long num_active;
  382. unsigned long num_allocations;
  383. unsigned long high_mark;
  384. unsigned long grown;
  385. unsigned long reaped;
  386. unsigned long errors;
  387. unsigned long max_freeable;
  388. unsigned long node_allocs;
  389. unsigned long node_frees;
  390. unsigned long node_overflow;
  391. atomic_t allochit;
  392. atomic_t allocmiss;
  393. atomic_t freehit;
  394. atomic_t freemiss;
  395. #endif
  396. #if DEBUG
  397. /*
  398. * If debugging is enabled, then the allocator can add additional
  399. * fields and/or padding to every object. buffer_size contains the total
  400. * object size including these internal fields, the following two
  401. * variables contain the offset to the user object and its size.
  402. */
  403. int obj_offset;
  404. int obj_size;
  405. #endif
  406. };
  407. #define CFLGS_OFF_SLAB (0x80000000UL)
  408. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  409. #define BATCHREFILL_LIMIT 16
  410. /*
  411. * Optimization question: fewer reaps means less probability for unnessary
  412. * cpucache drain/refill cycles.
  413. *
  414. * OTOH the cpuarrays can contain lots of objects,
  415. * which could lock up otherwise freeable slabs.
  416. */
  417. #define REAPTIMEOUT_CPUC (2*HZ)
  418. #define REAPTIMEOUT_LIST3 (4*HZ)
  419. #if STATS
  420. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  421. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  422. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  423. #define STATS_INC_GROWN(x) ((x)->grown++)
  424. #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
  425. #define STATS_SET_HIGH(x) \
  426. do { \
  427. if ((x)->num_active > (x)->high_mark) \
  428. (x)->high_mark = (x)->num_active; \
  429. } while (0)
  430. #define STATS_INC_ERR(x) ((x)->errors++)
  431. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  432. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  433. #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
  434. #define STATS_SET_FREEABLE(x, i) \
  435. do { \
  436. if ((x)->max_freeable < i) \
  437. (x)->max_freeable = i; \
  438. } while (0)
  439. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  440. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  441. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  442. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  443. #else
  444. #define STATS_INC_ACTIVE(x) do { } while (0)
  445. #define STATS_DEC_ACTIVE(x) do { } while (0)
  446. #define STATS_INC_ALLOCED(x) do { } while (0)
  447. #define STATS_INC_GROWN(x) do { } while (0)
  448. #define STATS_ADD_REAPED(x,y) do { } while (0)
  449. #define STATS_SET_HIGH(x) do { } while (0)
  450. #define STATS_INC_ERR(x) do { } while (0)
  451. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  452. #define STATS_INC_NODEFREES(x) do { } while (0)
  453. #define STATS_INC_ACOVERFLOW(x) do { } while (0)
  454. #define STATS_SET_FREEABLE(x, i) do { } while (0)
  455. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  456. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  457. #define STATS_INC_FREEHIT(x) do { } while (0)
  458. #define STATS_INC_FREEMISS(x) do { } while (0)
  459. #endif
  460. #if DEBUG
  461. /*
  462. * memory layout of objects:
  463. * 0 : objp
  464. * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
  465. * the end of an object is aligned with the end of the real
  466. * allocation. Catches writes behind the end of the allocation.
  467. * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
  468. * redzone word.
  469. * cachep->obj_offset: The real object.
  470. * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
  471. * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
  472. * [BYTES_PER_WORD long]
  473. */
  474. static int obj_offset(struct kmem_cache *cachep)
  475. {
  476. return cachep->obj_offset;
  477. }
  478. static int obj_size(struct kmem_cache *cachep)
  479. {
  480. return cachep->obj_size;
  481. }
  482. static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
  483. {
  484. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  485. return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
  486. }
  487. static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
  488. {
  489. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  490. if (cachep->flags & SLAB_STORE_USER)
  491. return (unsigned long *)(objp + cachep->buffer_size -
  492. 2 * BYTES_PER_WORD);
  493. return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
  494. }
  495. static void **dbg_userword(struct kmem_cache *cachep, void *objp)
  496. {
  497. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  498. return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
  499. }
  500. #else
  501. #define obj_offset(x) 0
  502. #define obj_size(cachep) (cachep->buffer_size)
  503. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  504. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  505. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  506. #endif
  507. /*
  508. * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
  509. * order.
  510. */
  511. #if defined(CONFIG_LARGE_ALLOCS)
  512. #define MAX_OBJ_ORDER 13 /* up to 32Mb */
  513. #define MAX_GFP_ORDER 13 /* up to 32Mb */
  514. #elif defined(CONFIG_MMU)
  515. #define MAX_OBJ_ORDER 5 /* 32 pages */
  516. #define MAX_GFP_ORDER 5 /* 32 pages */
  517. #else
  518. #define MAX_OBJ_ORDER 8 /* up to 1Mb */
  519. #define MAX_GFP_ORDER 8 /* up to 1Mb */
  520. #endif
  521. /*
  522. * Do not go above this order unless 0 objects fit into the slab.
  523. */
  524. #define BREAK_GFP_ORDER_HI 1
  525. #define BREAK_GFP_ORDER_LO 0
  526. static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
  527. /*
  528. * Functions for storing/retrieving the cachep and or slab from the page
  529. * allocator. These are used to find the slab an obj belongs to. With kfree(),
  530. * these are used to find the cache which an obj belongs to.
  531. */
  532. static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
  533. {
  534. page->lru.next = (struct list_head *)cache;
  535. }
  536. static inline struct kmem_cache *page_get_cache(struct page *page)
  537. {
  538. if (unlikely(PageCompound(page)))
  539. page = (struct page *)page_private(page);
  540. BUG_ON(!PageSlab(page));
  541. return (struct kmem_cache *)page->lru.next;
  542. }
  543. static inline void page_set_slab(struct page *page, struct slab *slab)
  544. {
  545. page->lru.prev = (struct list_head *)slab;
  546. }
  547. static inline struct slab *page_get_slab(struct page *page)
  548. {
  549. if (unlikely(PageCompound(page)))
  550. page = (struct page *)page_private(page);
  551. BUG_ON(!PageSlab(page));
  552. return (struct slab *)page->lru.prev;
  553. }
  554. static inline struct kmem_cache *virt_to_cache(const void *obj)
  555. {
  556. struct page *page = virt_to_page(obj);
  557. return page_get_cache(page);
  558. }
  559. static inline struct slab *virt_to_slab(const void *obj)
  560. {
  561. struct page *page = virt_to_page(obj);
  562. return page_get_slab(page);
  563. }
  564. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  565. unsigned int idx)
  566. {
  567. return slab->s_mem + cache->buffer_size * idx;
  568. }
  569. static inline unsigned int obj_to_index(struct kmem_cache *cache,
  570. struct slab *slab, void *obj)
  571. {
  572. return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
  573. }
  574. /*
  575. * These are the default caches for kmalloc. Custom caches can have other sizes.
  576. */
  577. struct cache_sizes malloc_sizes[] = {
  578. #define CACHE(x) { .cs_size = (x) },
  579. #include <linux/kmalloc_sizes.h>
  580. CACHE(ULONG_MAX)
  581. #undef CACHE
  582. };
  583. EXPORT_SYMBOL(malloc_sizes);
  584. /* Must match cache_sizes above. Out of line to keep cache footprint low. */
  585. struct cache_names {
  586. char *name;
  587. char *name_dma;
  588. };
  589. static struct cache_names __initdata cache_names[] = {
  590. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  591. #include <linux/kmalloc_sizes.h>
  592. {NULL,}
  593. #undef CACHE
  594. };
  595. static struct arraycache_init initarray_cache __initdata =
  596. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  597. static struct arraycache_init initarray_generic =
  598. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  599. /* internal cache of cache description objs */
  600. static struct kmem_cache cache_cache = {
  601. .batchcount = 1,
  602. .limit = BOOT_CPUCACHE_ENTRIES,
  603. .shared = 1,
  604. .buffer_size = sizeof(struct kmem_cache),
  605. .name = "kmem_cache",
  606. #if DEBUG
  607. .obj_size = sizeof(struct kmem_cache),
  608. #endif
  609. };
  610. #define BAD_ALIEN_MAGIC 0x01020304ul
  611. #ifdef CONFIG_LOCKDEP
  612. /*
  613. * Slab sometimes uses the kmalloc slabs to store the slab headers
  614. * for other slabs "off slab".
  615. * The locking for this is tricky in that it nests within the locks
  616. * of all other slabs in a few places; to deal with this special
  617. * locking we put on-slab caches into a separate lock-class.
  618. *
  619. * We set lock class for alien array caches which are up during init.
  620. * The lock annotation will be lost if all cpus of a node goes down and
  621. * then comes back up during hotplug
  622. */
  623. static struct lock_class_key on_slab_l3_key;
  624. static struct lock_class_key on_slab_alc_key;
  625. static inline void init_lock_keys(void)
  626. {
  627. int q;
  628. struct cache_sizes *s = malloc_sizes;
  629. while (s->cs_size != ULONG_MAX) {
  630. for_each_node(q) {
  631. struct array_cache **alc;
  632. int r;
  633. struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
  634. if (!l3 || OFF_SLAB(s->cs_cachep))
  635. continue;
  636. lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
  637. alc = l3->alien;
  638. /*
  639. * FIXME: This check for BAD_ALIEN_MAGIC
  640. * should go away when common slab code is taught to
  641. * work even without alien caches.
  642. * Currently, non NUMA code returns BAD_ALIEN_MAGIC
  643. * for alloc_alien_cache,
  644. */
  645. if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
  646. continue;
  647. for_each_node(r) {
  648. if (alc[r])
  649. lockdep_set_class(&alc[r]->lock,
  650. &on_slab_alc_key);
  651. }
  652. }
  653. s++;
  654. }
  655. }
  656. #else
  657. static inline void init_lock_keys(void)
  658. {
  659. }
  660. #endif
  661. /*
  662. * 1. Guard access to the cache-chain.
  663. * 2. Protect sanity of cpu_online_map against cpu hotplug events
  664. */
  665. static DEFINE_MUTEX(cache_chain_mutex);
  666. static struct list_head cache_chain;
  667. /*
  668. * chicken and egg problem: delay the per-cpu array allocation
  669. * until the general caches are up.
  670. */
  671. static enum {
  672. NONE,
  673. PARTIAL_AC,
  674. PARTIAL_L3,
  675. FULL
  676. } g_cpucache_up;
  677. /*
  678. * used by boot code to determine if it can use slab based allocator
  679. */
  680. int slab_is_available(void)
  681. {
  682. return g_cpucache_up == FULL;
  683. }
  684. static DEFINE_PER_CPU(struct delayed_work, reap_work);
  685. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
  686. {
  687. return cachep->array[smp_processor_id()];
  688. }
  689. static inline struct kmem_cache *__find_general_cachep(size_t size,
  690. gfp_t gfpflags)
  691. {
  692. struct cache_sizes *csizep = malloc_sizes;
  693. #if DEBUG
  694. /* This happens if someone tries to call
  695. * kmem_cache_create(), or __kmalloc(), before
  696. * the generic caches are initialized.
  697. */
  698. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  699. #endif
  700. while (size > csizep->cs_size)
  701. csizep++;
  702. /*
  703. * Really subtle: The last entry with cs->cs_size==ULONG_MAX
  704. * has cs_{dma,}cachep==NULL. Thus no special case
  705. * for large kmalloc calls required.
  706. */
  707. if (unlikely(gfpflags & GFP_DMA))
  708. return csizep->cs_dmacachep;
  709. return csizep->cs_cachep;
  710. }
  711. static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
  712. {
  713. return __find_general_cachep(size, gfpflags);
  714. }
  715. static size_t slab_mgmt_size(size_t nr_objs, size_t align)
  716. {
  717. return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  718. }
  719. /*
  720. * Calculate the number of objects and left-over bytes for a given buffer size.
  721. */
  722. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  723. size_t align, int flags, size_t *left_over,
  724. unsigned int *num)
  725. {
  726. int nr_objs;
  727. size_t mgmt_size;
  728. size_t slab_size = PAGE_SIZE << gfporder;
  729. /*
  730. * The slab management structure can be either off the slab or
  731. * on it. For the latter case, the memory allocated for a
  732. * slab is used for:
  733. *
  734. * - The struct slab
  735. * - One kmem_bufctl_t for each object
  736. * - Padding to respect alignment of @align
  737. * - @buffer_size bytes for each object
  738. *
  739. * If the slab management structure is off the slab, then the
  740. * alignment will already be calculated into the size. Because
  741. * the slabs are all pages aligned, the objects will be at the
  742. * correct alignment when allocated.
  743. */
  744. if (flags & CFLGS_OFF_SLAB) {
  745. mgmt_size = 0;
  746. nr_objs = slab_size / buffer_size;
  747. if (nr_objs > SLAB_LIMIT)
  748. nr_objs = SLAB_LIMIT;
  749. } else {
  750. /*
  751. * Ignore padding for the initial guess. The padding
  752. * is at most @align-1 bytes, and @buffer_size is at
  753. * least @align. In the worst case, this result will
  754. * be one greater than the number of objects that fit
  755. * into the memory allocation when taking the padding
  756. * into account.
  757. */
  758. nr_objs = (slab_size - sizeof(struct slab)) /
  759. (buffer_size + sizeof(kmem_bufctl_t));
  760. /*
  761. * This calculated number will be either the right
  762. * amount, or one greater than what we want.
  763. */
  764. if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  765. > slab_size)
  766. nr_objs--;
  767. if (nr_objs > SLAB_LIMIT)
  768. nr_objs = SLAB_LIMIT;
  769. mgmt_size = slab_mgmt_size(nr_objs, align);
  770. }
  771. *num = nr_objs;
  772. *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  773. }
  774. #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
  775. static void __slab_error(const char *function, struct kmem_cache *cachep,
  776. char *msg)
  777. {
  778. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  779. function, cachep->name, msg);
  780. dump_stack();
  781. }
  782. /*
  783. * By default on NUMA we use alien caches to stage the freeing of
  784. * objects allocated from other nodes. This causes massive memory
  785. * inefficiencies when using fake NUMA setup to split memory into a
  786. * large number of small nodes, so it can be disabled on the command
  787. * line
  788. */
  789. static int use_alien_caches __read_mostly = 1;
  790. static int __init noaliencache_setup(char *s)
  791. {
  792. use_alien_caches = 0;
  793. return 1;
  794. }
  795. __setup("noaliencache", noaliencache_setup);
  796. #ifdef CONFIG_NUMA
  797. /*
  798. * Special reaping functions for NUMA systems called from cache_reap().
  799. * These take care of doing round robin flushing of alien caches (containing
  800. * objects freed on different nodes from which they were allocated) and the
  801. * flushing of remote pcps by calling drain_node_pages.
  802. */
  803. static DEFINE_PER_CPU(unsigned long, reap_node);
  804. static void init_reap_node(int cpu)
  805. {
  806. int node;
  807. node = next_node(cpu_to_node(cpu), node_online_map);
  808. if (node == MAX_NUMNODES)
  809. node = first_node(node_online_map);
  810. per_cpu(reap_node, cpu) = node;
  811. }
  812. static void next_reap_node(void)
  813. {
  814. int node = __get_cpu_var(reap_node);
  815. /*
  816. * Also drain per cpu pages on remote zones
  817. */
  818. if (node != numa_node_id())
  819. drain_node_pages(node);
  820. node = next_node(node, node_online_map);
  821. if (unlikely(node >= MAX_NUMNODES))
  822. node = first_node(node_online_map);
  823. __get_cpu_var(reap_node) = node;
  824. }
  825. #else
  826. #define init_reap_node(cpu) do { } while (0)
  827. #define next_reap_node(void) do { } while (0)
  828. #endif
  829. /*
  830. * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
  831. * via the workqueue/eventd.
  832. * Add the CPU number into the expiration time to minimize the possibility of
  833. * the CPUs getting into lockstep and contending for the global cache chain
  834. * lock.
  835. */
  836. static void __devinit start_cpu_timer(int cpu)
  837. {
  838. struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
  839. /*
  840. * When this gets called from do_initcalls via cpucache_init(),
  841. * init_workqueues() has already run, so keventd will be setup
  842. * at that time.
  843. */
  844. if (keventd_up() && reap_work->work.func == NULL) {
  845. init_reap_node(cpu);
  846. INIT_DELAYED_WORK(reap_work, cache_reap);
  847. schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
  848. }
  849. }
  850. static struct array_cache *alloc_arraycache(int node, int entries,
  851. int batchcount)
  852. {
  853. int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
  854. struct array_cache *nc = NULL;
  855. nc = kmalloc_node(memsize, GFP_KERNEL, node);
  856. if (nc) {
  857. nc->avail = 0;
  858. nc->limit = entries;
  859. nc->batchcount = batchcount;
  860. nc->touched = 0;
  861. spin_lock_init(&nc->lock);
  862. }
  863. return nc;
  864. }
  865. /*
  866. * Transfer objects in one arraycache to another.
  867. * Locking must be handled by the caller.
  868. *
  869. * Return the number of entries transferred.
  870. */
  871. static int transfer_objects(struct array_cache *to,
  872. struct array_cache *from, unsigned int max)
  873. {
  874. /* Figure out how many entries to transfer */
  875. int nr = min(min(from->avail, max), to->limit - to->avail);
  876. if (!nr)
  877. return 0;
  878. memcpy(to->entry + to->avail, from->entry + from->avail -nr,
  879. sizeof(void *) *nr);
  880. from->avail -= nr;
  881. to->avail += nr;
  882. to->touched = 1;
  883. return nr;
  884. }
  885. #ifndef CONFIG_NUMA
  886. #define drain_alien_cache(cachep, alien) do { } while (0)
  887. #define reap_alien(cachep, l3) do { } while (0)
  888. static inline struct array_cache **alloc_alien_cache(int node, int limit)
  889. {
  890. return (struct array_cache **)BAD_ALIEN_MAGIC;
  891. }
  892. static inline void free_alien_cache(struct array_cache **ac_ptr)
  893. {
  894. }
  895. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  896. {
  897. return 0;
  898. }
  899. static inline void *alternate_node_alloc(struct kmem_cache *cachep,
  900. gfp_t flags)
  901. {
  902. return NULL;
  903. }
  904. static inline void *____cache_alloc_node(struct kmem_cache *cachep,
  905. gfp_t flags, int nodeid)
  906. {
  907. return NULL;
  908. }
  909. #else /* CONFIG_NUMA */
  910. static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
  911. static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
  912. static struct array_cache **alloc_alien_cache(int node, int limit)
  913. {
  914. struct array_cache **ac_ptr;
  915. int memsize = sizeof(void *) * MAX_NUMNODES;
  916. int i;
  917. if (limit > 1)
  918. limit = 12;
  919. ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
  920. if (ac_ptr) {
  921. for_each_node(i) {
  922. if (i == node || !node_online(i)) {
  923. ac_ptr[i] = NULL;
  924. continue;
  925. }
  926. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
  927. if (!ac_ptr[i]) {
  928. for (i--; i <= 0; i--)
  929. kfree(ac_ptr[i]);
  930. kfree(ac_ptr);
  931. return NULL;
  932. }
  933. }
  934. }
  935. return ac_ptr;
  936. }
  937. static void free_alien_cache(struct array_cache **ac_ptr)
  938. {
  939. int i;
  940. if (!ac_ptr)
  941. return;
  942. for_each_node(i)
  943. kfree(ac_ptr[i]);
  944. kfree(ac_ptr);
  945. }
  946. static void __drain_alien_cache(struct kmem_cache *cachep,
  947. struct array_cache *ac, int node)
  948. {
  949. struct kmem_list3 *rl3 = cachep->nodelists[node];
  950. if (ac->avail) {
  951. spin_lock(&rl3->list_lock);
  952. /*
  953. * Stuff objects into the remote nodes shared array first.
  954. * That way we could avoid the overhead of putting the objects
  955. * into the free lists and getting them back later.
  956. */
  957. if (rl3->shared)
  958. transfer_objects(rl3->shared, ac, ac->limit);
  959. free_block(cachep, ac->entry, ac->avail, node);
  960. ac->avail = 0;
  961. spin_unlock(&rl3->list_lock);
  962. }
  963. }
  964. /*
  965. * Called from cache_reap() to regularly drain alien caches round robin.
  966. */
  967. static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
  968. {
  969. int node = __get_cpu_var(reap_node);
  970. if (l3->alien) {
  971. struct array_cache *ac = l3->alien[node];
  972. if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
  973. __drain_alien_cache(cachep, ac, node);
  974. spin_unlock_irq(&ac->lock);
  975. }
  976. }
  977. }
  978. static void drain_alien_cache(struct kmem_cache *cachep,
  979. struct array_cache **alien)
  980. {
  981. int i = 0;
  982. struct array_cache *ac;
  983. unsigned long flags;
  984. for_each_online_node(i) {
  985. ac = alien[i];
  986. if (ac) {
  987. spin_lock_irqsave(&ac->lock, flags);
  988. __drain_alien_cache(cachep, ac, i);
  989. spin_unlock_irqrestore(&ac->lock, flags);
  990. }
  991. }
  992. }
  993. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  994. {
  995. struct slab *slabp = virt_to_slab(objp);
  996. int nodeid = slabp->nodeid;
  997. struct kmem_list3 *l3;
  998. struct array_cache *alien = NULL;
  999. int node;
  1000. node = numa_node_id();
  1001. /*
  1002. * Make sure we are not freeing a object from another node to the array
  1003. * cache on this cpu.
  1004. */
  1005. if (likely(slabp->nodeid == node) || unlikely(!use_alien_caches))
  1006. return 0;
  1007. l3 = cachep->nodelists[node];
  1008. STATS_INC_NODEFREES(cachep);
  1009. if (l3->alien && l3->alien[nodeid]) {
  1010. alien = l3->alien[nodeid];
  1011. spin_lock(&alien->lock);
  1012. if (unlikely(alien->avail == alien->limit)) {
  1013. STATS_INC_ACOVERFLOW(cachep);
  1014. __drain_alien_cache(cachep, alien, nodeid);
  1015. }
  1016. alien->entry[alien->avail++] = objp;
  1017. spin_unlock(&alien->lock);
  1018. } else {
  1019. spin_lock(&(cachep->nodelists[nodeid])->list_lock);
  1020. free_block(cachep, &objp, 1, nodeid);
  1021. spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
  1022. }
  1023. return 1;
  1024. }
  1025. #endif
  1026. static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  1027. unsigned long action, void *hcpu)
  1028. {
  1029. long cpu = (long)hcpu;
  1030. struct kmem_cache *cachep;
  1031. struct kmem_list3 *l3 = NULL;
  1032. int node = cpu_to_node(cpu);
  1033. int memsize = sizeof(struct kmem_list3);
  1034. switch (action) {
  1035. case CPU_UP_PREPARE:
  1036. mutex_lock(&cache_chain_mutex);
  1037. /*
  1038. * We need to do this right in the beginning since
  1039. * alloc_arraycache's are going to use this list.
  1040. * kmalloc_node allows us to add the slab to the right
  1041. * kmem_list3 and not this cpu's kmem_list3
  1042. */
  1043. list_for_each_entry(cachep, &cache_chain, next) {
  1044. /*
  1045. * Set up the size64 kmemlist for cpu before we can
  1046. * begin anything. Make sure some other cpu on this
  1047. * node has not already allocated this
  1048. */
  1049. if (!cachep->nodelists[node]) {
  1050. l3 = kmalloc_node(memsize, GFP_KERNEL, node);
  1051. if (!l3)
  1052. goto bad;
  1053. kmem_list3_init(l3);
  1054. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  1055. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1056. /*
  1057. * The l3s don't come and go as CPUs come and
  1058. * go. cache_chain_mutex is sufficient
  1059. * protection here.
  1060. */
  1061. cachep->nodelists[node] = l3;
  1062. }
  1063. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  1064. cachep->nodelists[node]->free_limit =
  1065. (1 + nr_cpus_node(node)) *
  1066. cachep->batchcount + cachep->num;
  1067. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  1068. }
  1069. /*
  1070. * Now we can go ahead with allocating the shared arrays and
  1071. * array caches
  1072. */
  1073. list_for_each_entry(cachep, &cache_chain, next) {
  1074. struct array_cache *nc;
  1075. struct array_cache *shared;
  1076. struct array_cache **alien = NULL;
  1077. nc = alloc_arraycache(node, cachep->limit,
  1078. cachep->batchcount);
  1079. if (!nc)
  1080. goto bad;
  1081. shared = alloc_arraycache(node,
  1082. cachep->shared * cachep->batchcount,
  1083. 0xbaadf00d);
  1084. if (!shared)
  1085. goto bad;
  1086. if (use_alien_caches) {
  1087. alien = alloc_alien_cache(node, cachep->limit);
  1088. if (!alien)
  1089. goto bad;
  1090. }
  1091. cachep->array[cpu] = nc;
  1092. l3 = cachep->nodelists[node];
  1093. BUG_ON(!l3);
  1094. spin_lock_irq(&l3->list_lock);
  1095. if (!l3->shared) {
  1096. /*
  1097. * We are serialised from CPU_DEAD or
  1098. * CPU_UP_CANCELLED by the cpucontrol lock
  1099. */
  1100. l3->shared = shared;
  1101. shared = NULL;
  1102. }
  1103. #ifdef CONFIG_NUMA
  1104. if (!l3->alien) {
  1105. l3->alien = alien;
  1106. alien = NULL;
  1107. }
  1108. #endif
  1109. spin_unlock_irq(&l3->list_lock);
  1110. kfree(shared);
  1111. free_alien_cache(alien);
  1112. }
  1113. break;
  1114. case CPU_ONLINE:
  1115. mutex_unlock(&cache_chain_mutex);
  1116. start_cpu_timer(cpu);
  1117. break;
  1118. #ifdef CONFIG_HOTPLUG_CPU
  1119. case CPU_DOWN_PREPARE:
  1120. mutex_lock(&cache_chain_mutex);
  1121. break;
  1122. case CPU_DOWN_FAILED:
  1123. mutex_unlock(&cache_chain_mutex);
  1124. break;
  1125. case CPU_DEAD:
  1126. /*
  1127. * Even if all the cpus of a node are down, we don't free the
  1128. * kmem_list3 of any cache. This to avoid a race between
  1129. * cpu_down, and a kmalloc allocation from another cpu for
  1130. * memory from the node of the cpu going down. The list3
  1131. * structure is usually allocated from kmem_cache_create() and
  1132. * gets destroyed at kmem_cache_destroy().
  1133. */
  1134. /* fall thru */
  1135. #endif
  1136. case CPU_UP_CANCELED:
  1137. list_for_each_entry(cachep, &cache_chain, next) {
  1138. struct array_cache *nc;
  1139. struct array_cache *shared;
  1140. struct array_cache **alien;
  1141. cpumask_t mask;
  1142. mask = node_to_cpumask(node);
  1143. /* cpu is dead; no one can alloc from it. */
  1144. nc = cachep->array[cpu];
  1145. cachep->array[cpu] = NULL;
  1146. l3 = cachep->nodelists[node];
  1147. if (!l3)
  1148. goto free_array_cache;
  1149. spin_lock_irq(&l3->list_lock);
  1150. /* Free limit for this kmem_list3 */
  1151. l3->free_limit -= cachep->batchcount;
  1152. if (nc)
  1153. free_block(cachep, nc->entry, nc->avail, node);
  1154. if (!cpus_empty(mask)) {
  1155. spin_unlock_irq(&l3->list_lock);
  1156. goto free_array_cache;
  1157. }
  1158. shared = l3->shared;
  1159. if (shared) {
  1160. free_block(cachep, l3->shared->entry,
  1161. l3->shared->avail, node);
  1162. l3->shared = NULL;
  1163. }
  1164. alien = l3->alien;
  1165. l3->alien = NULL;
  1166. spin_unlock_irq(&l3->list_lock);
  1167. kfree(shared);
  1168. if (alien) {
  1169. drain_alien_cache(cachep, alien);
  1170. free_alien_cache(alien);
  1171. }
  1172. free_array_cache:
  1173. kfree(nc);
  1174. }
  1175. /*
  1176. * In the previous loop, all the objects were freed to
  1177. * the respective cache's slabs, now we can go ahead and
  1178. * shrink each nodelist to its limit.
  1179. */
  1180. list_for_each_entry(cachep, &cache_chain, next) {
  1181. l3 = cachep->nodelists[node];
  1182. if (!l3)
  1183. continue;
  1184. drain_freelist(cachep, l3, l3->free_objects);
  1185. }
  1186. mutex_unlock(&cache_chain_mutex);
  1187. break;
  1188. }
  1189. return NOTIFY_OK;
  1190. bad:
  1191. return NOTIFY_BAD;
  1192. }
  1193. static struct notifier_block __cpuinitdata cpucache_notifier = {
  1194. &cpuup_callback, NULL, 0
  1195. };
  1196. /*
  1197. * swap the static kmem_list3 with kmalloced memory
  1198. */
  1199. static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
  1200. int nodeid)
  1201. {
  1202. struct kmem_list3 *ptr;
  1203. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
  1204. BUG_ON(!ptr);
  1205. local_irq_disable();
  1206. memcpy(ptr, list, sizeof(struct kmem_list3));
  1207. /*
  1208. * Do not assume that spinlocks can be initialized via memcpy:
  1209. */
  1210. spin_lock_init(&ptr->list_lock);
  1211. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1212. cachep->nodelists[nodeid] = ptr;
  1213. local_irq_enable();
  1214. }
  1215. /*
  1216. * Initialisation. Called after the page allocator have been initialised and
  1217. * before smp_init().
  1218. */
  1219. void __init kmem_cache_init(void)
  1220. {
  1221. size_t left_over;
  1222. struct cache_sizes *sizes;
  1223. struct cache_names *names;
  1224. int i;
  1225. int order;
  1226. int node;
  1227. for (i = 0; i < NUM_INIT_LISTS; i++) {
  1228. kmem_list3_init(&initkmem_list3[i]);
  1229. if (i < MAX_NUMNODES)
  1230. cache_cache.nodelists[i] = NULL;
  1231. }
  1232. /*
  1233. * Fragmentation resistance on low memory - only use bigger
  1234. * page orders on machines with more than 32MB of memory.
  1235. */
  1236. if (num_physpages > (32 << 20) >> PAGE_SHIFT)
  1237. slab_break_gfp_order = BREAK_GFP_ORDER_HI;
  1238. /* Bootstrap is tricky, because several objects are allocated
  1239. * from caches that do not exist yet:
  1240. * 1) initialize the cache_cache cache: it contains the struct
  1241. * kmem_cache structures of all caches, except cache_cache itself:
  1242. * cache_cache is statically allocated.
  1243. * Initially an __init data area is used for the head array and the
  1244. * kmem_list3 structures, it's replaced with a kmalloc allocated
  1245. * array at the end of the bootstrap.
  1246. * 2) Create the first kmalloc cache.
  1247. * The struct kmem_cache for the new cache is allocated normally.
  1248. * An __init data area is used for the head array.
  1249. * 3) Create the remaining kmalloc caches, with minimally sized
  1250. * head arrays.
  1251. * 4) Replace the __init data head arrays for cache_cache and the first
  1252. * kmalloc cache with kmalloc allocated arrays.
  1253. * 5) Replace the __init data for kmem_list3 for cache_cache and
  1254. * the other cache's with kmalloc allocated memory.
  1255. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1256. */
  1257. node = numa_node_id();
  1258. /* 1) create the cache_cache */
  1259. INIT_LIST_HEAD(&cache_chain);
  1260. list_add(&cache_cache.next, &cache_chain);
  1261. cache_cache.colour_off = cache_line_size();
  1262. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  1263. cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
  1264. cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
  1265. cache_line_size());
  1266. for (order = 0; order < MAX_ORDER; order++) {
  1267. cache_estimate(order, cache_cache.buffer_size,
  1268. cache_line_size(), 0, &left_over, &cache_cache.num);
  1269. if (cache_cache.num)
  1270. break;
  1271. }
  1272. BUG_ON(!cache_cache.num);
  1273. cache_cache.gfporder = order;
  1274. cache_cache.colour = left_over / cache_cache.colour_off;
  1275. cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
  1276. sizeof(struct slab), cache_line_size());
  1277. /* 2+3) create the kmalloc caches */
  1278. sizes = malloc_sizes;
  1279. names = cache_names;
  1280. /*
  1281. * Initialize the caches that provide memory for the array cache and the
  1282. * kmem_list3 structures first. Without this, further allocations will
  1283. * bug.
  1284. */
  1285. sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
  1286. sizes[INDEX_AC].cs_size,
  1287. ARCH_KMALLOC_MINALIGN,
  1288. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1289. NULL, NULL);
  1290. if (INDEX_AC != INDEX_L3) {
  1291. sizes[INDEX_L3].cs_cachep =
  1292. kmem_cache_create(names[INDEX_L3].name,
  1293. sizes[INDEX_L3].cs_size,
  1294. ARCH_KMALLOC_MINALIGN,
  1295. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1296. NULL, NULL);
  1297. }
  1298. slab_early_init = 0;
  1299. while (sizes->cs_size != ULONG_MAX) {
  1300. /*
  1301. * For performance, all the general caches are L1 aligned.
  1302. * This should be particularly beneficial on SMP boxes, as it
  1303. * eliminates "false sharing".
  1304. * Note for systems short on memory removing the alignment will
  1305. * allow tighter packing of the smaller caches.
  1306. */
  1307. if (!sizes->cs_cachep) {
  1308. sizes->cs_cachep = kmem_cache_create(names->name,
  1309. sizes->cs_size,
  1310. ARCH_KMALLOC_MINALIGN,
  1311. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1312. NULL, NULL);
  1313. }
  1314. sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
  1315. sizes->cs_size,
  1316. ARCH_KMALLOC_MINALIGN,
  1317. ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
  1318. SLAB_PANIC,
  1319. NULL, NULL);
  1320. sizes++;
  1321. names++;
  1322. }
  1323. /* 4) Replace the bootstrap head arrays */
  1324. {
  1325. struct array_cache *ptr;
  1326. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1327. local_irq_disable();
  1328. BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
  1329. memcpy(ptr, cpu_cache_get(&cache_cache),
  1330. sizeof(struct arraycache_init));
  1331. /*
  1332. * Do not assume that spinlocks can be initialized via memcpy:
  1333. */
  1334. spin_lock_init(&ptr->lock);
  1335. cache_cache.array[smp_processor_id()] = ptr;
  1336. local_irq_enable();
  1337. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1338. local_irq_disable();
  1339. BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
  1340. != &initarray_generic.cache);
  1341. memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
  1342. sizeof(struct arraycache_init));
  1343. /*
  1344. * Do not assume that spinlocks can be initialized via memcpy:
  1345. */
  1346. spin_lock_init(&ptr->lock);
  1347. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  1348. ptr;
  1349. local_irq_enable();
  1350. }
  1351. /* 5) Replace the bootstrap kmem_list3's */
  1352. {
  1353. int nid;
  1354. /* Replace the static kmem_list3 structures for the boot cpu */
  1355. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
  1356. for_each_online_node(nid) {
  1357. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1358. &initkmem_list3[SIZE_AC + nid], nid);
  1359. if (INDEX_AC != INDEX_L3) {
  1360. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1361. &initkmem_list3[SIZE_L3 + nid], nid);
  1362. }
  1363. }
  1364. }
  1365. /* 6) resize the head arrays to their final sizes */
  1366. {
  1367. struct kmem_cache *cachep;
  1368. mutex_lock(&cache_chain_mutex);
  1369. list_for_each_entry(cachep, &cache_chain, next)
  1370. if (enable_cpucache(cachep))
  1371. BUG();
  1372. mutex_unlock(&cache_chain_mutex);
  1373. }
  1374. /* Annotate slab for lockdep -- annotate the malloc caches */
  1375. init_lock_keys();
  1376. /* Done! */
  1377. g_cpucache_up = FULL;
  1378. /*
  1379. * Register a cpu startup notifier callback that initializes
  1380. * cpu_cache_get for all new cpus
  1381. */
  1382. register_cpu_notifier(&cpucache_notifier);
  1383. /*
  1384. * The reap timers are started later, with a module init call: That part
  1385. * of the kernel is not yet operational.
  1386. */
  1387. }
  1388. static int __init cpucache_init(void)
  1389. {
  1390. int cpu;
  1391. /*
  1392. * Register the timers that return unneeded pages to the page allocator
  1393. */
  1394. for_each_online_cpu(cpu)
  1395. start_cpu_timer(cpu);
  1396. return 0;
  1397. }
  1398. __initcall(cpucache_init);
  1399. /*
  1400. * Interface to system's page allocator. No need to hold the cache-lock.
  1401. *
  1402. * If we requested dmaable memory, we will get it. Even if we
  1403. * did not request dmaable memory, we might get it, but that
  1404. * would be relatively rare and ignorable.
  1405. */
  1406. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1407. {
  1408. struct page *page;
  1409. int nr_pages;
  1410. int i;
  1411. #ifndef CONFIG_MMU
  1412. /*
  1413. * Nommu uses slab's for process anonymous memory allocations, and thus
  1414. * requires __GFP_COMP to properly refcount higher order allocations
  1415. */
  1416. flags |= __GFP_COMP;
  1417. #endif
  1418. flags |= cachep->gfpflags;
  1419. page = alloc_pages_node(nodeid, flags, cachep->gfporder);
  1420. if (!page)
  1421. return NULL;
  1422. nr_pages = (1 << cachep->gfporder);
  1423. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1424. add_zone_page_state(page_zone(page),
  1425. NR_SLAB_RECLAIMABLE, nr_pages);
  1426. else
  1427. add_zone_page_state(page_zone(page),
  1428. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1429. for (i = 0; i < nr_pages; i++)
  1430. __SetPageSlab(page + i);
  1431. return page_address(page);
  1432. }
  1433. /*
  1434. * Interface to system's page release.
  1435. */
  1436. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1437. {
  1438. unsigned long i = (1 << cachep->gfporder);
  1439. struct page *page = virt_to_page(addr);
  1440. const unsigned long nr_freed = i;
  1441. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1442. sub_zone_page_state(page_zone(page),
  1443. NR_SLAB_RECLAIMABLE, nr_freed);
  1444. else
  1445. sub_zone_page_state(page_zone(page),
  1446. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1447. while (i--) {
  1448. BUG_ON(!PageSlab(page));
  1449. __ClearPageSlab(page);
  1450. page++;
  1451. }
  1452. if (current->reclaim_state)
  1453. current->reclaim_state->reclaimed_slab += nr_freed;
  1454. free_pages((unsigned long)addr, cachep->gfporder);
  1455. }
  1456. static void kmem_rcu_free(struct rcu_head *head)
  1457. {
  1458. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1459. struct kmem_cache *cachep = slab_rcu->cachep;
  1460. kmem_freepages(cachep, slab_rcu->addr);
  1461. if (OFF_SLAB(cachep))
  1462. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1463. }
  1464. #if DEBUG
  1465. #ifdef CONFIG_DEBUG_PAGEALLOC
  1466. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1467. unsigned long caller)
  1468. {
  1469. int size = obj_size(cachep);
  1470. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1471. if (size < 5 * sizeof(unsigned long))
  1472. return;
  1473. *addr++ = 0x12345678;
  1474. *addr++ = caller;
  1475. *addr++ = smp_processor_id();
  1476. size -= 3 * sizeof(unsigned long);
  1477. {
  1478. unsigned long *sptr = &caller;
  1479. unsigned long svalue;
  1480. while (!kstack_end(sptr)) {
  1481. svalue = *sptr++;
  1482. if (kernel_text_address(svalue)) {
  1483. *addr++ = svalue;
  1484. size -= sizeof(unsigned long);
  1485. if (size <= sizeof(unsigned long))
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. *addr++ = 0x87654321;
  1491. }
  1492. #endif
  1493. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1494. {
  1495. int size = obj_size(cachep);
  1496. addr = &((char *)addr)[obj_offset(cachep)];
  1497. memset(addr, val, size);
  1498. *(unsigned char *)(addr + size - 1) = POISON_END;
  1499. }
  1500. static void dump_line(char *data, int offset, int limit)
  1501. {
  1502. int i;
  1503. unsigned char error = 0;
  1504. int bad_count = 0;
  1505. printk(KERN_ERR "%03x:", offset);
  1506. for (i = 0; i < limit; i++) {
  1507. if (data[offset + i] != POISON_FREE) {
  1508. error = data[offset + i];
  1509. bad_count++;
  1510. }
  1511. printk(" %02x", (unsigned char)data[offset + i]);
  1512. }
  1513. printk("\n");
  1514. if (bad_count == 1) {
  1515. error ^= POISON_FREE;
  1516. if (!(error & (error - 1))) {
  1517. printk(KERN_ERR "Single bit error detected. Probably "
  1518. "bad RAM.\n");
  1519. #ifdef CONFIG_X86
  1520. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1521. "test tool.\n");
  1522. #else
  1523. printk(KERN_ERR "Run a memory test tool.\n");
  1524. #endif
  1525. }
  1526. }
  1527. }
  1528. #endif
  1529. #if DEBUG
  1530. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1531. {
  1532. int i, size;
  1533. char *realobj;
  1534. if (cachep->flags & SLAB_RED_ZONE) {
  1535. printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
  1536. *dbg_redzone1(cachep, objp),
  1537. *dbg_redzone2(cachep, objp));
  1538. }
  1539. if (cachep->flags & SLAB_STORE_USER) {
  1540. printk(KERN_ERR "Last user: [<%p>]",
  1541. *dbg_userword(cachep, objp));
  1542. print_symbol("(%s)",
  1543. (unsigned long)*dbg_userword(cachep, objp));
  1544. printk("\n");
  1545. }
  1546. realobj = (char *)objp + obj_offset(cachep);
  1547. size = obj_size(cachep);
  1548. for (i = 0; i < size && lines; i += 16, lines--) {
  1549. int limit;
  1550. limit = 16;
  1551. if (i + limit > size)
  1552. limit = size - i;
  1553. dump_line(realobj, i, limit);
  1554. }
  1555. }
  1556. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1557. {
  1558. char *realobj;
  1559. int size, i;
  1560. int lines = 0;
  1561. realobj = (char *)objp + obj_offset(cachep);
  1562. size = obj_size(cachep);
  1563. for (i = 0; i < size; i++) {
  1564. char exp = POISON_FREE;
  1565. if (i == size - 1)
  1566. exp = POISON_END;
  1567. if (realobj[i] != exp) {
  1568. int limit;
  1569. /* Mismatch ! */
  1570. /* Print header */
  1571. if (lines == 0) {
  1572. printk(KERN_ERR
  1573. "Slab corruption: start=%p, len=%d\n",
  1574. realobj, size);
  1575. print_objinfo(cachep, objp, 0);
  1576. }
  1577. /* Hexdump the affected line */
  1578. i = (i / 16) * 16;
  1579. limit = 16;
  1580. if (i + limit > size)
  1581. limit = size - i;
  1582. dump_line(realobj, i, limit);
  1583. i += 16;
  1584. lines++;
  1585. /* Limit to 5 lines */
  1586. if (lines > 5)
  1587. break;
  1588. }
  1589. }
  1590. if (lines != 0) {
  1591. /* Print some data about the neighboring objects, if they
  1592. * exist:
  1593. */
  1594. struct slab *slabp = virt_to_slab(objp);
  1595. unsigned int objnr;
  1596. objnr = obj_to_index(cachep, slabp, objp);
  1597. if (objnr) {
  1598. objp = index_to_obj(cachep, slabp, objnr - 1);
  1599. realobj = (char *)objp + obj_offset(cachep);
  1600. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1601. realobj, size);
  1602. print_objinfo(cachep, objp, 2);
  1603. }
  1604. if (objnr + 1 < cachep->num) {
  1605. objp = index_to_obj(cachep, slabp, objnr + 1);
  1606. realobj = (char *)objp + obj_offset(cachep);
  1607. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1608. realobj, size);
  1609. print_objinfo(cachep, objp, 2);
  1610. }
  1611. }
  1612. }
  1613. #endif
  1614. #if DEBUG
  1615. /**
  1616. * slab_destroy_objs - destroy a slab and its objects
  1617. * @cachep: cache pointer being destroyed
  1618. * @slabp: slab pointer being destroyed
  1619. *
  1620. * Call the registered destructor for each object in a slab that is being
  1621. * destroyed.
  1622. */
  1623. static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
  1624. {
  1625. int i;
  1626. for (i = 0; i < cachep->num; i++) {
  1627. void *objp = index_to_obj(cachep, slabp, i);
  1628. if (cachep->flags & SLAB_POISON) {
  1629. #ifdef CONFIG_DEBUG_PAGEALLOC
  1630. if (cachep->buffer_size % PAGE_SIZE == 0 &&
  1631. OFF_SLAB(cachep))
  1632. kernel_map_pages(virt_to_page(objp),
  1633. cachep->buffer_size / PAGE_SIZE, 1);
  1634. else
  1635. check_poison_obj(cachep, objp);
  1636. #else
  1637. check_poison_obj(cachep, objp);
  1638. #endif
  1639. }
  1640. if (cachep->flags & SLAB_RED_ZONE) {
  1641. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1642. slab_error(cachep, "start of a freed object "
  1643. "was overwritten");
  1644. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1645. slab_error(cachep, "end of a freed object "
  1646. "was overwritten");
  1647. }
  1648. if (cachep->dtor && !(cachep->flags & SLAB_POISON))
  1649. (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
  1650. }
  1651. }
  1652. #else
  1653. static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
  1654. {
  1655. if (cachep->dtor) {
  1656. int i;
  1657. for (i = 0; i < cachep->num; i++) {
  1658. void *objp = index_to_obj(cachep, slabp, i);
  1659. (cachep->dtor) (objp, cachep, 0);
  1660. }
  1661. }
  1662. }
  1663. #endif
  1664. /**
  1665. * slab_destroy - destroy and release all objects in a slab
  1666. * @cachep: cache pointer being destroyed
  1667. * @slabp: slab pointer being destroyed
  1668. *
  1669. * Destroy all the objs in a slab, and release the mem back to the system.
  1670. * Before calling the slab must have been unlinked from the cache. The
  1671. * cache-lock is not held/needed.
  1672. */
  1673. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1674. {
  1675. void *addr = slabp->s_mem - slabp->colouroff;
  1676. slab_destroy_objs(cachep, slabp);
  1677. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1678. struct slab_rcu *slab_rcu;
  1679. slab_rcu = (struct slab_rcu *)slabp;
  1680. slab_rcu->cachep = cachep;
  1681. slab_rcu->addr = addr;
  1682. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1683. } else {
  1684. kmem_freepages(cachep, addr);
  1685. if (OFF_SLAB(cachep))
  1686. kmem_cache_free(cachep->slabp_cache, slabp);
  1687. }
  1688. }
  1689. /*
  1690. * For setting up all the kmem_list3s for cache whose buffer_size is same as
  1691. * size of kmem_list3.
  1692. */
  1693. static void set_up_list3s(struct kmem_cache *cachep, int index)
  1694. {
  1695. int node;
  1696. for_each_online_node(node) {
  1697. cachep->nodelists[node] = &initkmem_list3[index + node];
  1698. cachep->nodelists[node]->next_reap = jiffies +
  1699. REAPTIMEOUT_LIST3 +
  1700. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1701. }
  1702. }
  1703. static void __kmem_cache_destroy(struct kmem_cache *cachep)
  1704. {
  1705. int i;
  1706. struct kmem_list3 *l3;
  1707. for_each_online_cpu(i)
  1708. kfree(cachep->array[i]);
  1709. /* NUMA: free the list3 structures */
  1710. for_each_online_node(i) {
  1711. l3 = cachep->nodelists[i];
  1712. if (l3) {
  1713. kfree(l3->shared);
  1714. free_alien_cache(l3->alien);
  1715. kfree(l3);
  1716. }
  1717. }
  1718. kmem_cache_free(&cache_cache, cachep);
  1719. }
  1720. /**
  1721. * calculate_slab_order - calculate size (page order) of slabs
  1722. * @cachep: pointer to the cache that is being created
  1723. * @size: size of objects to be created in this cache.
  1724. * @align: required alignment for the objects.
  1725. * @flags: slab allocation flags
  1726. *
  1727. * Also calculates the number of objects per slab.
  1728. *
  1729. * This could be made much more intelligent. For now, try to avoid using
  1730. * high order pages for slabs. When the gfp() functions are more friendly
  1731. * towards high-order requests, this should be changed.
  1732. */
  1733. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1734. size_t size, size_t align, unsigned long flags)
  1735. {
  1736. unsigned long offslab_limit;
  1737. size_t left_over = 0;
  1738. int gfporder;
  1739. for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
  1740. unsigned int num;
  1741. size_t remainder;
  1742. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1743. if (!num)
  1744. continue;
  1745. if (flags & CFLGS_OFF_SLAB) {
  1746. /*
  1747. * Max number of objs-per-slab for caches which
  1748. * use off-slab slabs. Needed to avoid a possible
  1749. * looping condition in cache_grow().
  1750. */
  1751. offslab_limit = size - sizeof(struct slab);
  1752. offslab_limit /= sizeof(kmem_bufctl_t);
  1753. if (num > offslab_limit)
  1754. break;
  1755. }
  1756. /* Found something acceptable - save it away */
  1757. cachep->num = num;
  1758. cachep->gfporder = gfporder;
  1759. left_over = remainder;
  1760. /*
  1761. * A VFS-reclaimable slab tends to have most allocations
  1762. * as GFP_NOFS and we really don't want to have to be allocating
  1763. * higher-order pages when we are unable to shrink dcache.
  1764. */
  1765. if (flags & SLAB_RECLAIM_ACCOUNT)
  1766. break;
  1767. /*
  1768. * Large number of objects is good, but very large slabs are
  1769. * currently bad for the gfp()s.
  1770. */
  1771. if (gfporder >= slab_break_gfp_order)
  1772. break;
  1773. /*
  1774. * Acceptable internal fragmentation?
  1775. */
  1776. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1777. break;
  1778. }
  1779. return left_over;
  1780. }
  1781. static int setup_cpu_cache(struct kmem_cache *cachep)
  1782. {
  1783. if (g_cpucache_up == FULL)
  1784. return enable_cpucache(cachep);
  1785. if (g_cpucache_up == NONE) {
  1786. /*
  1787. * Note: the first kmem_cache_create must create the cache
  1788. * that's used by kmalloc(24), otherwise the creation of
  1789. * further caches will BUG().
  1790. */
  1791. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1792. /*
  1793. * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
  1794. * the first cache, then we need to set up all its list3s,
  1795. * otherwise the creation of further caches will BUG().
  1796. */
  1797. set_up_list3s(cachep, SIZE_AC);
  1798. if (INDEX_AC == INDEX_L3)
  1799. g_cpucache_up = PARTIAL_L3;
  1800. else
  1801. g_cpucache_up = PARTIAL_AC;
  1802. } else {
  1803. cachep->array[smp_processor_id()] =
  1804. kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1805. if (g_cpucache_up == PARTIAL_AC) {
  1806. set_up_list3s(cachep, SIZE_L3);
  1807. g_cpucache_up = PARTIAL_L3;
  1808. } else {
  1809. int node;
  1810. for_each_online_node(node) {
  1811. cachep->nodelists[node] =
  1812. kmalloc_node(sizeof(struct kmem_list3),
  1813. GFP_KERNEL, node);
  1814. BUG_ON(!cachep->nodelists[node]);
  1815. kmem_list3_init(cachep->nodelists[node]);
  1816. }
  1817. }
  1818. }
  1819. cachep->nodelists[numa_node_id()]->next_reap =
  1820. jiffies + REAPTIMEOUT_LIST3 +
  1821. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1822. cpu_cache_get(cachep)->avail = 0;
  1823. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1824. cpu_cache_get(cachep)->batchcount = 1;
  1825. cpu_cache_get(cachep)->touched = 0;
  1826. cachep->batchcount = 1;
  1827. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1828. return 0;
  1829. }
  1830. /**
  1831. * kmem_cache_create - Create a cache.
  1832. * @name: A string which is used in /proc/slabinfo to identify this cache.
  1833. * @size: The size of objects to be created in this cache.
  1834. * @align: The required alignment for the objects.
  1835. * @flags: SLAB flags
  1836. * @ctor: A constructor for the objects.
  1837. * @dtor: A destructor for the objects.
  1838. *
  1839. * Returns a ptr to the cache on success, NULL on failure.
  1840. * Cannot be called within a int, but can be interrupted.
  1841. * The @ctor is run when new pages are allocated by the cache
  1842. * and the @dtor is run before the pages are handed back.
  1843. *
  1844. * @name must be valid until the cache is destroyed. This implies that
  1845. * the module calling this has to destroy the cache before getting unloaded.
  1846. *
  1847. * The flags are
  1848. *
  1849. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1850. * to catch references to uninitialised memory.
  1851. *
  1852. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1853. * for buffer overruns.
  1854. *
  1855. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1856. * cacheline. This can be beneficial if you're counting cycles as closely
  1857. * as davem.
  1858. */
  1859. struct kmem_cache *
  1860. kmem_cache_create (const char *name, size_t size, size_t align,
  1861. unsigned long flags,
  1862. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  1863. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  1864. {
  1865. size_t left_over, slab_size, ralign;
  1866. struct kmem_cache *cachep = NULL, *pc;
  1867. /*
  1868. * Sanity checks... these are all serious usage bugs.
  1869. */
  1870. if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
  1871. (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
  1872. printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
  1873. name);
  1874. BUG();
  1875. }
  1876. /*
  1877. * We use cache_chain_mutex to ensure a consistent view of
  1878. * cpu_online_map as well. Please see cpuup_callback
  1879. */
  1880. mutex_lock(&cache_chain_mutex);
  1881. list_for_each_entry(pc, &cache_chain, next) {
  1882. mm_segment_t old_fs = get_fs();
  1883. char tmp;
  1884. int res;
  1885. /*
  1886. * This happens when the module gets unloaded and doesn't
  1887. * destroy its slab cache and no-one else reuses the vmalloc
  1888. * area of the module. Print a warning.
  1889. */
  1890. set_fs(KERNEL_DS);
  1891. res = __get_user(tmp, pc->name);
  1892. set_fs(old_fs);
  1893. if (res) {
  1894. printk("SLAB: cache with size %d has lost its name\n",
  1895. pc->buffer_size);
  1896. continue;
  1897. }
  1898. if (!strcmp(pc->name, name)) {
  1899. printk("kmem_cache_create: duplicate cache %s\n", name);
  1900. dump_stack();
  1901. goto oops;
  1902. }
  1903. }
  1904. #if DEBUG
  1905. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  1906. if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
  1907. /* No constructor, but inital state check requested */
  1908. printk(KERN_ERR "%s: No con, but init state check "
  1909. "requested - %s\n", __FUNCTION__, name);
  1910. flags &= ~SLAB_DEBUG_INITIAL;
  1911. }
  1912. #if FORCED_DEBUG
  1913. /*
  1914. * Enable redzoning and last user accounting, except for caches with
  1915. * large objects, if the increased size would increase the object size
  1916. * above the next power of two: caches with object sizes just above a
  1917. * power of two have a significant amount of internal fragmentation.
  1918. */
  1919. if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
  1920. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1921. if (!(flags & SLAB_DESTROY_BY_RCU))
  1922. flags |= SLAB_POISON;
  1923. #endif
  1924. if (flags & SLAB_DESTROY_BY_RCU)
  1925. BUG_ON(flags & SLAB_POISON);
  1926. #endif
  1927. if (flags & SLAB_DESTROY_BY_RCU)
  1928. BUG_ON(dtor);
  1929. /*
  1930. * Always checks flags, a caller might be expecting debug support which
  1931. * isn't available.
  1932. */
  1933. BUG_ON(flags & ~CREATE_MASK);
  1934. /*
  1935. * Check that size is in terms of words. This is needed to avoid
  1936. * unaligned accesses for some archs when redzoning is used, and makes
  1937. * sure any on-slab bufctl's are also correctly aligned.
  1938. */
  1939. if (size & (BYTES_PER_WORD - 1)) {
  1940. size += (BYTES_PER_WORD - 1);
  1941. size &= ~(BYTES_PER_WORD - 1);
  1942. }
  1943. /* calculate the final buffer alignment: */
  1944. /* 1) arch recommendation: can be overridden for debug */
  1945. if (flags & SLAB_HWCACHE_ALIGN) {
  1946. /*
  1947. * Default alignment: as specified by the arch code. Except if
  1948. * an object is really small, then squeeze multiple objects into
  1949. * one cacheline.
  1950. */
  1951. ralign = cache_line_size();
  1952. while (size <= ralign / 2)
  1953. ralign /= 2;
  1954. } else {
  1955. ralign = BYTES_PER_WORD;
  1956. }
  1957. /*
  1958. * Redzoning and user store require word alignment. Note this will be
  1959. * overridden by architecture or caller mandated alignment if either
  1960. * is greater than BYTES_PER_WORD.
  1961. */
  1962. if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
  1963. ralign = BYTES_PER_WORD;
  1964. /* 2) arch mandated alignment */
  1965. if (ralign < ARCH_SLAB_MINALIGN) {
  1966. ralign = ARCH_SLAB_MINALIGN;
  1967. }
  1968. /* 3) caller mandated alignment */
  1969. if (ralign < align) {
  1970. ralign = align;
  1971. }
  1972. /* disable debug if necessary */
  1973. if (ralign > BYTES_PER_WORD)
  1974. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1975. /*
  1976. * 4) Store it.
  1977. */
  1978. align = ralign;
  1979. /* Get cache's description obj. */
  1980. cachep = kmem_cache_zalloc(&cache_cache, GFP_KERNEL);
  1981. if (!cachep)
  1982. goto oops;
  1983. #if DEBUG
  1984. cachep->obj_size = size;
  1985. /*
  1986. * Both debugging options require word-alignment which is calculated
  1987. * into align above.
  1988. */
  1989. if (flags & SLAB_RED_ZONE) {
  1990. /* add space for red zone words */
  1991. cachep->obj_offset += BYTES_PER_WORD;
  1992. size += 2 * BYTES_PER_WORD;
  1993. }
  1994. if (flags & SLAB_STORE_USER) {
  1995. /* user store requires one word storage behind the end of
  1996. * the real object.
  1997. */
  1998. size += BYTES_PER_WORD;
  1999. }
  2000. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  2001. if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
  2002. && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
  2003. cachep->obj_offset += PAGE_SIZE - size;
  2004. size = PAGE_SIZE;
  2005. }
  2006. #endif
  2007. #endif
  2008. /*
  2009. * Determine if the slab management is 'on' or 'off' slab.
  2010. * (bootstrapping cannot cope with offslab caches so don't do
  2011. * it too early on.)
  2012. */
  2013. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
  2014. /*
  2015. * Size is large, assume best to place the slab management obj
  2016. * off-slab (should allow better packing of objs).
  2017. */
  2018. flags |= CFLGS_OFF_SLAB;
  2019. size = ALIGN(size, align);
  2020. left_over = calculate_slab_order(cachep, size, align, flags);
  2021. if (!cachep->num) {
  2022. printk("kmem_cache_create: couldn't create cache %s.\n", name);
  2023. kmem_cache_free(&cache_cache, cachep);
  2024. cachep = NULL;
  2025. goto oops;
  2026. }
  2027. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  2028. + sizeof(struct slab), align);
  2029. /*
  2030. * If the slab has been placed off-slab, and we have enough space then
  2031. * move it on-slab. This is at the expense of any extra colouring.
  2032. */
  2033. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  2034. flags &= ~CFLGS_OFF_SLAB;
  2035. left_over -= slab_size;
  2036. }
  2037. if (flags & CFLGS_OFF_SLAB) {
  2038. /* really off slab. No need for manual alignment */
  2039. slab_size =
  2040. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  2041. }
  2042. cachep->colour_off = cache_line_size();
  2043. /* Offset must be a multiple of the alignment. */
  2044. if (cachep->colour_off < align)
  2045. cachep->colour_off = align;
  2046. cachep->colour = left_over / cachep->colour_off;
  2047. cachep->slab_size = slab_size;
  2048. cachep->flags = flags;
  2049. cachep->gfpflags = 0;
  2050. if (flags & SLAB_CACHE_DMA)
  2051. cachep->gfpflags |= GFP_DMA;
  2052. cachep->buffer_size = size;
  2053. if (flags & CFLGS_OFF_SLAB) {
  2054. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  2055. /*
  2056. * This is a possibility for one of the malloc_sizes caches.
  2057. * But since we go off slab only for object size greater than
  2058. * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  2059. * this should not happen at all.
  2060. * But leave a BUG_ON for some lucky dude.
  2061. */
  2062. BUG_ON(!cachep->slabp_cache);
  2063. }
  2064. cachep->ctor = ctor;
  2065. cachep->dtor = dtor;
  2066. cachep->name = name;
  2067. if (setup_cpu_cache(cachep)) {
  2068. __kmem_cache_destroy(cachep);
  2069. cachep = NULL;
  2070. goto oops;
  2071. }
  2072. /* cache setup completed, link it into the list */
  2073. list_add(&cachep->next, &cache_chain);
  2074. oops:
  2075. if (!cachep && (flags & SLAB_PANIC))
  2076. panic("kmem_cache_create(): failed to create slab `%s'\n",
  2077. name);
  2078. mutex_unlock(&cache_chain_mutex);
  2079. return cachep;
  2080. }
  2081. EXPORT_SYMBOL(kmem_cache_create);
  2082. #if DEBUG
  2083. static void check_irq_off(void)
  2084. {
  2085. BUG_ON(!irqs_disabled());
  2086. }
  2087. static void check_irq_on(void)
  2088. {
  2089. BUG_ON(irqs_disabled());
  2090. }
  2091. static void check_spinlock_acquired(struct kmem_cache *cachep)
  2092. {
  2093. #ifdef CONFIG_SMP
  2094. check_irq_off();
  2095. assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
  2096. #endif
  2097. }
  2098. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  2099. {
  2100. #ifdef CONFIG_SMP
  2101. check_irq_off();
  2102. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  2103. #endif
  2104. }
  2105. #else
  2106. #define check_irq_off() do { } while(0)
  2107. #define check_irq_on() do { } while(0)
  2108. #define check_spinlock_acquired(x) do { } while(0)
  2109. #define check_spinlock_acquired_node(x, y) do { } while(0)
  2110. #endif
  2111. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  2112. struct array_cache *ac,
  2113. int force, int node);
  2114. static void do_drain(void *arg)
  2115. {
  2116. struct kmem_cache *cachep = arg;
  2117. struct array_cache *ac;
  2118. int node = numa_node_id();
  2119. check_irq_off();
  2120. ac = cpu_cache_get(cachep);
  2121. spin_lock(&cachep->nodelists[node]->list_lock);
  2122. free_block(cachep, ac->entry, ac->avail, node);
  2123. spin_unlock(&cachep->nodelists[node]->list_lock);
  2124. ac->avail = 0;
  2125. }
  2126. static void drain_cpu_caches(struct kmem_cache *cachep)
  2127. {
  2128. struct kmem_list3 *l3;
  2129. int node;
  2130. on_each_cpu(do_drain, cachep, 1, 1);
  2131. check_irq_on();
  2132. for_each_online_node(node) {
  2133. l3 = cachep->nodelists[node];
  2134. if (l3 && l3->alien)
  2135. drain_alien_cache(cachep, l3->alien);
  2136. }
  2137. for_each_online_node(node) {
  2138. l3 = cachep->nodelists[node];
  2139. if (l3)
  2140. drain_array(cachep, l3, l3->shared, 1, node);
  2141. }
  2142. }
  2143. /*
  2144. * Remove slabs from the list of free slabs.
  2145. * Specify the number of slabs to drain in tofree.
  2146. *
  2147. * Returns the actual number of slabs released.
  2148. */
  2149. static int drain_freelist(struct kmem_cache *cache,
  2150. struct kmem_list3 *l3, int tofree)
  2151. {
  2152. struct list_head *p;
  2153. int nr_freed;
  2154. struct slab *slabp;
  2155. nr_freed = 0;
  2156. while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
  2157. spin_lock_irq(&l3->list_lock);
  2158. p = l3->slabs_free.prev;
  2159. if (p == &l3->slabs_free) {
  2160. spin_unlock_irq(&l3->list_lock);
  2161. goto out;
  2162. }
  2163. slabp = list_entry(p, struct slab, list);
  2164. #if DEBUG
  2165. BUG_ON(slabp->inuse);
  2166. #endif
  2167. list_del(&slabp->list);
  2168. /*
  2169. * Safe to drop the lock. The slab is no longer linked
  2170. * to the cache.
  2171. */
  2172. l3->free_objects -= cache->num;
  2173. spin_unlock_irq(&l3->list_lock);
  2174. slab_destroy(cache, slabp);
  2175. nr_freed++;
  2176. }
  2177. out:
  2178. return nr_freed;
  2179. }
  2180. /* Called with cache_chain_mutex held to protect against cpu hotplug */
  2181. static int __cache_shrink(struct kmem_cache *cachep)
  2182. {
  2183. int ret = 0, i = 0;
  2184. struct kmem_list3 *l3;
  2185. drain_cpu_caches(cachep);
  2186. check_irq_on();
  2187. for_each_online_node(i) {
  2188. l3 = cachep->nodelists[i];
  2189. if (!l3)
  2190. continue;
  2191. drain_freelist(cachep, l3, l3->free_objects);
  2192. ret += !list_empty(&l3->slabs_full) ||
  2193. !list_empty(&l3->slabs_partial);
  2194. }
  2195. return (ret ? 1 : 0);
  2196. }
  2197. /**
  2198. * kmem_cache_shrink - Shrink a cache.
  2199. * @cachep: The cache to shrink.
  2200. *
  2201. * Releases as many slabs as possible for a cache.
  2202. * To help debugging, a zero exit status indicates all slabs were released.
  2203. */
  2204. int kmem_cache_shrink(struct kmem_cache *cachep)
  2205. {
  2206. int ret;
  2207. BUG_ON(!cachep || in_interrupt());
  2208. mutex_lock(&cache_chain_mutex);
  2209. ret = __cache_shrink(cachep);
  2210. mutex_unlock(&cache_chain_mutex);
  2211. return ret;
  2212. }
  2213. EXPORT_SYMBOL(kmem_cache_shrink);
  2214. /**
  2215. * kmem_cache_destroy - delete a cache
  2216. * @cachep: the cache to destroy
  2217. *
  2218. * Remove a struct kmem_cache object from the slab cache.
  2219. *
  2220. * It is expected this function will be called by a module when it is
  2221. * unloaded. This will remove the cache completely, and avoid a duplicate
  2222. * cache being allocated each time a module is loaded and unloaded, if the
  2223. * module doesn't have persistent in-kernel storage across loads and unloads.
  2224. *
  2225. * The cache must be empty before calling this function.
  2226. *
  2227. * The caller must guarantee that noone will allocate memory from the cache
  2228. * during the kmem_cache_destroy().
  2229. */
  2230. void kmem_cache_destroy(struct kmem_cache *cachep)
  2231. {
  2232. BUG_ON(!cachep || in_interrupt());
  2233. /* Find the cache in the chain of caches. */
  2234. mutex_lock(&cache_chain_mutex);
  2235. /*
  2236. * the chain is never empty, cache_cache is never destroyed
  2237. */
  2238. list_del(&cachep->next);
  2239. if (__cache_shrink(cachep)) {
  2240. slab_error(cachep, "Can't free all objects");
  2241. list_add(&cachep->next, &cache_chain);
  2242. mutex_unlock(&cache_chain_mutex);
  2243. return;
  2244. }
  2245. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  2246. synchronize_rcu();
  2247. __kmem_cache_destroy(cachep);
  2248. mutex_unlock(&cache_chain_mutex);
  2249. }
  2250. EXPORT_SYMBOL(kmem_cache_destroy);
  2251. /*
  2252. * Get the memory for a slab management obj.
  2253. * For a slab cache when the slab descriptor is off-slab, slab descriptors
  2254. * always come from malloc_sizes caches. The slab descriptor cannot
  2255. * come from the same cache which is getting created because,
  2256. * when we are searching for an appropriate cache for these
  2257. * descriptors in kmem_cache_create, we search through the malloc_sizes array.
  2258. * If we are creating a malloc_sizes cache here it would not be visible to
  2259. * kmem_find_general_cachep till the initialization is complete.
  2260. * Hence we cannot have slabp_cache same as the original cache.
  2261. */
  2262. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2263. int colour_off, gfp_t local_flags,
  2264. int nodeid)
  2265. {
  2266. struct slab *slabp;
  2267. if (OFF_SLAB(cachep)) {
  2268. /* Slab management obj is off-slab. */
  2269. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  2270. local_flags & ~GFP_THISNODE, nodeid);
  2271. if (!slabp)
  2272. return NULL;
  2273. } else {
  2274. slabp = objp + colour_off;
  2275. colour_off += cachep->slab_size;
  2276. }
  2277. slabp->inuse = 0;
  2278. slabp->colouroff = colour_off;
  2279. slabp->s_mem = objp + colour_off;
  2280. slabp->nodeid = nodeid;
  2281. return slabp;
  2282. }
  2283. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2284. {
  2285. return (kmem_bufctl_t *) (slabp + 1);
  2286. }
  2287. static void cache_init_objs(struct kmem_cache *cachep,
  2288. struct slab *slabp, unsigned long ctor_flags)
  2289. {
  2290. int i;
  2291. for (i = 0; i < cachep->num; i++) {
  2292. void *objp = index_to_obj(cachep, slabp, i);
  2293. #if DEBUG
  2294. /* need to poison the objs? */
  2295. if (cachep->flags & SLAB_POISON)
  2296. poison_obj(cachep, objp, POISON_FREE);
  2297. if (cachep->flags & SLAB_STORE_USER)
  2298. *dbg_userword(cachep, objp) = NULL;
  2299. if (cachep->flags & SLAB_RED_ZONE) {
  2300. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2301. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2302. }
  2303. /*
  2304. * Constructors are not allowed to allocate memory from the same
  2305. * cache which they are a constructor for. Otherwise, deadlock.
  2306. * They must also be threaded.
  2307. */
  2308. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2309. cachep->ctor(objp + obj_offset(cachep), cachep,
  2310. ctor_flags);
  2311. if (cachep->flags & SLAB_RED_ZONE) {
  2312. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2313. slab_error(cachep, "constructor overwrote the"
  2314. " end of an object");
  2315. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2316. slab_error(cachep, "constructor overwrote the"
  2317. " start of an object");
  2318. }
  2319. if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
  2320. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2321. kernel_map_pages(virt_to_page(objp),
  2322. cachep->buffer_size / PAGE_SIZE, 0);
  2323. #else
  2324. if (cachep->ctor)
  2325. cachep->ctor(objp, cachep, ctor_flags);
  2326. #endif
  2327. slab_bufctl(slabp)[i] = i + 1;
  2328. }
  2329. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2330. slabp->free = 0;
  2331. }
  2332. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2333. {
  2334. if (flags & GFP_DMA)
  2335. BUG_ON(!(cachep->gfpflags & GFP_DMA));
  2336. else
  2337. BUG_ON(cachep->gfpflags & GFP_DMA);
  2338. }
  2339. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2340. int nodeid)
  2341. {
  2342. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2343. kmem_bufctl_t next;
  2344. slabp->inuse++;
  2345. next = slab_bufctl(slabp)[slabp->free];
  2346. #if DEBUG
  2347. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2348. WARN_ON(slabp->nodeid != nodeid);
  2349. #endif
  2350. slabp->free = next;
  2351. return objp;
  2352. }
  2353. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2354. void *objp, int nodeid)
  2355. {
  2356. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2357. #if DEBUG
  2358. /* Verify that the slab belongs to the intended node */
  2359. WARN_ON(slabp->nodeid != nodeid);
  2360. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  2361. printk(KERN_ERR "slab: double free detected in cache "
  2362. "'%s', objp %p\n", cachep->name, objp);
  2363. BUG();
  2364. }
  2365. #endif
  2366. slab_bufctl(slabp)[objnr] = slabp->free;
  2367. slabp->free = objnr;
  2368. slabp->inuse--;
  2369. }
  2370. /*
  2371. * Map pages beginning at addr to the given cache and slab. This is required
  2372. * for the slab allocator to be able to lookup the cache and slab of a
  2373. * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
  2374. */
  2375. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  2376. void *addr)
  2377. {
  2378. int nr_pages;
  2379. struct page *page;
  2380. page = virt_to_page(addr);
  2381. nr_pages = 1;
  2382. if (likely(!PageCompound(page)))
  2383. nr_pages <<= cache->gfporder;
  2384. do {
  2385. page_set_cache(page, cache);
  2386. page_set_slab(page, slab);
  2387. page++;
  2388. } while (--nr_pages);
  2389. }
  2390. /*
  2391. * Grow (by 1) the number of slabs within a cache. This is called by
  2392. * kmem_cache_alloc() when there are no active objs left in a cache.
  2393. */
  2394. static int cache_grow(struct kmem_cache *cachep,
  2395. gfp_t flags, int nodeid, void *objp)
  2396. {
  2397. struct slab *slabp;
  2398. size_t offset;
  2399. gfp_t local_flags;
  2400. unsigned long ctor_flags;
  2401. struct kmem_list3 *l3;
  2402. /*
  2403. * Be lazy and only check for valid flags here, keeping it out of the
  2404. * critical path in kmem_cache_alloc().
  2405. */
  2406. BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK | __GFP_NO_GROW));
  2407. if (flags & __GFP_NO_GROW)
  2408. return 0;
  2409. ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  2410. local_flags = (flags & GFP_LEVEL_MASK);
  2411. if (!(local_flags & __GFP_WAIT))
  2412. /*
  2413. * Not allowed to sleep. Need to tell a constructor about
  2414. * this - it might need to know...
  2415. */
  2416. ctor_flags |= SLAB_CTOR_ATOMIC;
  2417. /* Take the l3 list lock to change the colour_next on this node */
  2418. check_irq_off();
  2419. l3 = cachep->nodelists[nodeid];
  2420. spin_lock(&l3->list_lock);
  2421. /* Get colour for the slab, and cal the next value. */
  2422. offset = l3->colour_next;
  2423. l3->colour_next++;
  2424. if (l3->colour_next >= cachep->colour)
  2425. l3->colour_next = 0;
  2426. spin_unlock(&l3->list_lock);
  2427. offset *= cachep->colour_off;
  2428. if (local_flags & __GFP_WAIT)
  2429. local_irq_enable();
  2430. /*
  2431. * The test for missing atomic flag is performed here, rather than
  2432. * the more obvious place, simply to reduce the critical path length
  2433. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2434. * will eventually be caught here (where it matters).
  2435. */
  2436. kmem_flagcheck(cachep, flags);
  2437. /*
  2438. * Get mem for the objs. Attempt to allocate a physical page from
  2439. * 'nodeid'.
  2440. */
  2441. if (!objp)
  2442. objp = kmem_getpages(cachep, flags, nodeid);
  2443. if (!objp)
  2444. goto failed;
  2445. /* Get slab management. */
  2446. slabp = alloc_slabmgmt(cachep, objp, offset,
  2447. local_flags & ~GFP_THISNODE, nodeid);
  2448. if (!slabp)
  2449. goto opps1;
  2450. slabp->nodeid = nodeid;
  2451. slab_map_pages(cachep, slabp, objp);
  2452. cache_init_objs(cachep, slabp, ctor_flags);
  2453. if (local_flags & __GFP_WAIT)
  2454. local_irq_disable();
  2455. check_irq_off();
  2456. spin_lock(&l3->list_lock);
  2457. /* Make slab active. */
  2458. list_add_tail(&slabp->list, &(l3->slabs_free));
  2459. STATS_INC_GROWN(cachep);
  2460. l3->free_objects += cachep->num;
  2461. spin_unlock(&l3->list_lock);
  2462. return 1;
  2463. opps1:
  2464. kmem_freepages(cachep, objp);
  2465. failed:
  2466. if (local_flags & __GFP_WAIT)
  2467. local_irq_disable();
  2468. return 0;
  2469. }
  2470. #if DEBUG
  2471. /*
  2472. * Perform extra freeing checks:
  2473. * - detect bad pointers.
  2474. * - POISON/RED_ZONE checking
  2475. * - destructor calls, for caches with POISON+dtor
  2476. */
  2477. static void kfree_debugcheck(const void *objp)
  2478. {
  2479. struct page *page;
  2480. if (!virt_addr_valid(objp)) {
  2481. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2482. (unsigned long)objp);
  2483. BUG();
  2484. }
  2485. page = virt_to_page(objp);
  2486. if (!PageSlab(page)) {
  2487. printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
  2488. (unsigned long)objp);
  2489. BUG();
  2490. }
  2491. }
  2492. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2493. {
  2494. unsigned long redzone1, redzone2;
  2495. redzone1 = *dbg_redzone1(cache, obj);
  2496. redzone2 = *dbg_redzone2(cache, obj);
  2497. /*
  2498. * Redzone is ok.
  2499. */
  2500. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2501. return;
  2502. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2503. slab_error(cache, "double free detected");
  2504. else
  2505. slab_error(cache, "memory outside object was overwritten");
  2506. printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
  2507. obj, redzone1, redzone2);
  2508. }
  2509. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2510. void *caller)
  2511. {
  2512. struct page *page;
  2513. unsigned int objnr;
  2514. struct slab *slabp;
  2515. objp -= obj_offset(cachep);
  2516. kfree_debugcheck(objp);
  2517. page = virt_to_page(objp);
  2518. slabp = page_get_slab(page);
  2519. if (cachep->flags & SLAB_RED_ZONE) {
  2520. verify_redzone_free(cachep, objp);
  2521. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2522. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2523. }
  2524. if (cachep->flags & SLAB_STORE_USER)
  2525. *dbg_userword(cachep, objp) = caller;
  2526. objnr = obj_to_index(cachep, slabp, objp);
  2527. BUG_ON(objnr >= cachep->num);
  2528. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2529. if (cachep->flags & SLAB_DEBUG_INITIAL) {
  2530. /*
  2531. * Need to call the slab's constructor so the caller can
  2532. * perform a verify of its state (debugging). Called without
  2533. * the cache-lock held.
  2534. */
  2535. cachep->ctor(objp + obj_offset(cachep),
  2536. cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
  2537. }
  2538. if (cachep->flags & SLAB_POISON && cachep->dtor) {
  2539. /* we want to cache poison the object,
  2540. * call the destruction callback
  2541. */
  2542. cachep->dtor(objp + obj_offset(cachep), cachep, 0);
  2543. }
  2544. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2545. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2546. #endif
  2547. if (cachep->flags & SLAB_POISON) {
  2548. #ifdef CONFIG_DEBUG_PAGEALLOC
  2549. if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2550. store_stackinfo(cachep, objp, (unsigned long)caller);
  2551. kernel_map_pages(virt_to_page(objp),
  2552. cachep->buffer_size / PAGE_SIZE, 0);
  2553. } else {
  2554. poison_obj(cachep, objp, POISON_FREE);
  2555. }
  2556. #else
  2557. poison_obj(cachep, objp, POISON_FREE);
  2558. #endif
  2559. }
  2560. return objp;
  2561. }
  2562. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2563. {
  2564. kmem_bufctl_t i;
  2565. int entries = 0;
  2566. /* Check slab's freelist to see if this obj is there. */
  2567. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2568. entries++;
  2569. if (entries > cachep->num || i >= cachep->num)
  2570. goto bad;
  2571. }
  2572. if (entries != cachep->num - slabp->inuse) {
  2573. bad:
  2574. printk(KERN_ERR "slab: Internal list corruption detected in "
  2575. "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
  2576. cachep->name, cachep->num, slabp, slabp->inuse);
  2577. for (i = 0;
  2578. i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
  2579. i++) {
  2580. if (i % 16 == 0)
  2581. printk("\n%03x:", i);
  2582. printk(" %02x", ((unsigned char *)slabp)[i]);
  2583. }
  2584. printk("\n");
  2585. BUG();
  2586. }
  2587. }
  2588. #else
  2589. #define kfree_debugcheck(x) do { } while(0)
  2590. #define cache_free_debugcheck(x,objp,z) (objp)
  2591. #define check_slabp(x,y) do { } while(0)
  2592. #endif
  2593. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2594. {
  2595. int batchcount;
  2596. struct kmem_list3 *l3;
  2597. struct array_cache *ac;
  2598. int node;
  2599. node = numa_node_id();
  2600. check_irq_off();
  2601. ac = cpu_cache_get(cachep);
  2602. retry:
  2603. batchcount = ac->batchcount;
  2604. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2605. /*
  2606. * If there was little recent activity on this cache, then
  2607. * perform only a partial refill. Otherwise we could generate
  2608. * refill bouncing.
  2609. */
  2610. batchcount = BATCHREFILL_LIMIT;
  2611. }
  2612. l3 = cachep->nodelists[node];
  2613. BUG_ON(ac->avail > 0 || !l3);
  2614. spin_lock(&l3->list_lock);
  2615. /* See if we can refill from the shared array */
  2616. if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
  2617. goto alloc_done;
  2618. while (batchcount > 0) {
  2619. struct list_head *entry;
  2620. struct slab *slabp;
  2621. /* Get slab alloc is to come from. */
  2622. entry = l3->slabs_partial.next;
  2623. if (entry == &l3->slabs_partial) {
  2624. l3->free_touched = 1;
  2625. entry = l3->slabs_free.next;
  2626. if (entry == &l3->slabs_free)
  2627. goto must_grow;
  2628. }
  2629. slabp = list_entry(entry, struct slab, list);
  2630. check_slabp(cachep, slabp);
  2631. check_spinlock_acquired(cachep);
  2632. while (slabp->inuse < cachep->num && batchcount--) {
  2633. STATS_INC_ALLOCED(cachep);
  2634. STATS_INC_ACTIVE(cachep);
  2635. STATS_SET_HIGH(cachep);
  2636. ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
  2637. node);
  2638. }
  2639. check_slabp(cachep, slabp);
  2640. /* move slabp to correct slabp list: */
  2641. list_del(&slabp->list);
  2642. if (slabp->free == BUFCTL_END)
  2643. list_add(&slabp->list, &l3->slabs_full);
  2644. else
  2645. list_add(&slabp->list, &l3->slabs_partial);
  2646. }
  2647. must_grow:
  2648. l3->free_objects -= ac->avail;
  2649. alloc_done:
  2650. spin_unlock(&l3->list_lock);
  2651. if (unlikely(!ac->avail)) {
  2652. int x;
  2653. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2654. /* cache_grow can reenable interrupts, then ac could change. */
  2655. ac = cpu_cache_get(cachep);
  2656. if (!x && ac->avail == 0) /* no objects in sight? abort */
  2657. return NULL;
  2658. if (!ac->avail) /* objects refilled by interrupt? */
  2659. goto retry;
  2660. }
  2661. ac->touched = 1;
  2662. return ac->entry[--ac->avail];
  2663. }
  2664. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2665. gfp_t flags)
  2666. {
  2667. might_sleep_if(flags & __GFP_WAIT);
  2668. #if DEBUG
  2669. kmem_flagcheck(cachep, flags);
  2670. #endif
  2671. }
  2672. #if DEBUG
  2673. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2674. gfp_t flags, void *objp, void *caller)
  2675. {
  2676. if (!objp)
  2677. return objp;
  2678. if (cachep->flags & SLAB_POISON) {
  2679. #ifdef CONFIG_DEBUG_PAGEALLOC
  2680. if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2681. kernel_map_pages(virt_to_page(objp),
  2682. cachep->buffer_size / PAGE_SIZE, 1);
  2683. else
  2684. check_poison_obj(cachep, objp);
  2685. #else
  2686. check_poison_obj(cachep, objp);
  2687. #endif
  2688. poison_obj(cachep, objp, POISON_INUSE);
  2689. }
  2690. if (cachep->flags & SLAB_STORE_USER)
  2691. *dbg_userword(cachep, objp) = caller;
  2692. if (cachep->flags & SLAB_RED_ZONE) {
  2693. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2694. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2695. slab_error(cachep, "double free, or memory outside"
  2696. " object was overwritten");
  2697. printk(KERN_ERR
  2698. "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
  2699. objp, *dbg_redzone1(cachep, objp),
  2700. *dbg_redzone2(cachep, objp));
  2701. }
  2702. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2703. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2704. }
  2705. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2706. {
  2707. struct slab *slabp;
  2708. unsigned objnr;
  2709. slabp = page_get_slab(virt_to_page(objp));
  2710. objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
  2711. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2712. }
  2713. #endif
  2714. objp += obj_offset(cachep);
  2715. if (cachep->ctor && cachep->flags & SLAB_POISON) {
  2716. unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  2717. if (!(flags & __GFP_WAIT))
  2718. ctor_flags |= SLAB_CTOR_ATOMIC;
  2719. cachep->ctor(objp, cachep, ctor_flags);
  2720. }
  2721. #if ARCH_SLAB_MINALIGN
  2722. if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
  2723. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2724. objp, ARCH_SLAB_MINALIGN);
  2725. }
  2726. #endif
  2727. return objp;
  2728. }
  2729. #else
  2730. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2731. #endif
  2732. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2733. {
  2734. void *objp;
  2735. struct array_cache *ac;
  2736. check_irq_off();
  2737. ac = cpu_cache_get(cachep);
  2738. if (likely(ac->avail)) {
  2739. STATS_INC_ALLOCHIT(cachep);
  2740. ac->touched = 1;
  2741. objp = ac->entry[--ac->avail];
  2742. } else {
  2743. STATS_INC_ALLOCMISS(cachep);
  2744. objp = cache_alloc_refill(cachep, flags);
  2745. }
  2746. return objp;
  2747. }
  2748. static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
  2749. gfp_t flags, void *caller)
  2750. {
  2751. unsigned long save_flags;
  2752. void *objp = NULL;
  2753. cache_alloc_debugcheck_before(cachep, flags);
  2754. local_irq_save(save_flags);
  2755. if (unlikely(NUMA_BUILD &&
  2756. current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY)))
  2757. objp = alternate_node_alloc(cachep, flags);
  2758. if (!objp)
  2759. objp = ____cache_alloc(cachep, flags);
  2760. /*
  2761. * We may just have run out of memory on the local node.
  2762. * ____cache_alloc_node() knows how to locate memory on other nodes
  2763. */
  2764. if (NUMA_BUILD && !objp)
  2765. objp = ____cache_alloc_node(cachep, flags, numa_node_id());
  2766. local_irq_restore(save_flags);
  2767. objp = cache_alloc_debugcheck_after(cachep, flags, objp,
  2768. caller);
  2769. prefetchw(objp);
  2770. return objp;
  2771. }
  2772. #ifdef CONFIG_NUMA
  2773. /*
  2774. * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
  2775. *
  2776. * If we are in_interrupt, then process context, including cpusets and
  2777. * mempolicy, may not apply and should not be used for allocation policy.
  2778. */
  2779. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2780. {
  2781. int nid_alloc, nid_here;
  2782. if (in_interrupt() || (flags & __GFP_THISNODE))
  2783. return NULL;
  2784. nid_alloc = nid_here = numa_node_id();
  2785. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2786. nid_alloc = cpuset_mem_spread_node();
  2787. else if (current->mempolicy)
  2788. nid_alloc = slab_node(current->mempolicy);
  2789. if (nid_alloc != nid_here)
  2790. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2791. return NULL;
  2792. }
  2793. /*
  2794. * Fallback function if there was no memory available and no objects on a
  2795. * certain node and fall back is permitted. First we scan all the
  2796. * available nodelists for available objects. If that fails then we
  2797. * perform an allocation without specifying a node. This allows the page
  2798. * allocator to do its reclaim / fallback magic. We then insert the
  2799. * slab into the proper nodelist and then allocate from it.
  2800. */
  2801. void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2802. {
  2803. struct zonelist *zonelist = &NODE_DATA(slab_node(current->mempolicy))
  2804. ->node_zonelists[gfp_zone(flags)];
  2805. struct zone **z;
  2806. void *obj = NULL;
  2807. int nid;
  2808. retry:
  2809. /*
  2810. * Look through allowed nodes for objects available
  2811. * from existing per node queues.
  2812. */
  2813. for (z = zonelist->zones; *z && !obj; z++) {
  2814. nid = zone_to_nid(*z);
  2815. if (cpuset_zone_allowed(*z, flags) &&
  2816. cache->nodelists[nid] &&
  2817. cache->nodelists[nid]->free_objects)
  2818. obj = ____cache_alloc_node(cache,
  2819. flags | GFP_THISNODE, nid);
  2820. }
  2821. if (!obj) {
  2822. /*
  2823. * This allocation will be performed within the constraints
  2824. * of the current cpuset / memory policy requirements.
  2825. * We may trigger various forms of reclaim on the allowed
  2826. * set and go into memory reserves if necessary.
  2827. */
  2828. obj = kmem_getpages(cache, flags, -1);
  2829. if (obj) {
  2830. /*
  2831. * Insert into the appropriate per node queues
  2832. */
  2833. nid = page_to_nid(virt_to_page(obj));
  2834. if (cache_grow(cache, flags, nid, obj)) {
  2835. obj = ____cache_alloc_node(cache,
  2836. flags | GFP_THISNODE, nid);
  2837. if (!obj)
  2838. /*
  2839. * Another processor may allocate the
  2840. * objects in the slab since we are
  2841. * not holding any locks.
  2842. */
  2843. goto retry;
  2844. } else {
  2845. kmem_freepages(cache, obj);
  2846. obj = NULL;
  2847. }
  2848. }
  2849. }
  2850. return obj;
  2851. }
  2852. /*
  2853. * A interface to enable slab creation on nodeid
  2854. */
  2855. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2856. int nodeid)
  2857. {
  2858. struct list_head *entry;
  2859. struct slab *slabp;
  2860. struct kmem_list3 *l3;
  2861. void *obj;
  2862. int x;
  2863. l3 = cachep->nodelists[nodeid];
  2864. BUG_ON(!l3);
  2865. retry:
  2866. check_irq_off();
  2867. spin_lock(&l3->list_lock);
  2868. entry = l3->slabs_partial.next;
  2869. if (entry == &l3->slabs_partial) {
  2870. l3->free_touched = 1;
  2871. entry = l3->slabs_free.next;
  2872. if (entry == &l3->slabs_free)
  2873. goto must_grow;
  2874. }
  2875. slabp = list_entry(entry, struct slab, list);
  2876. check_spinlock_acquired_node(cachep, nodeid);
  2877. check_slabp(cachep, slabp);
  2878. STATS_INC_NODEALLOCS(cachep);
  2879. STATS_INC_ACTIVE(cachep);
  2880. STATS_SET_HIGH(cachep);
  2881. BUG_ON(slabp->inuse == cachep->num);
  2882. obj = slab_get_obj(cachep, slabp, nodeid);
  2883. check_slabp(cachep, slabp);
  2884. l3->free_objects--;
  2885. /* move slabp to correct slabp list: */
  2886. list_del(&slabp->list);
  2887. if (slabp->free == BUFCTL_END)
  2888. list_add(&slabp->list, &l3->slabs_full);
  2889. else
  2890. list_add(&slabp->list, &l3->slabs_partial);
  2891. spin_unlock(&l3->list_lock);
  2892. goto done;
  2893. must_grow:
  2894. spin_unlock(&l3->list_lock);
  2895. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  2896. if (x)
  2897. goto retry;
  2898. if (!(flags & __GFP_THISNODE))
  2899. /* Unable to grow the cache. Fall back to other nodes. */
  2900. return fallback_alloc(cachep, flags);
  2901. return NULL;
  2902. done:
  2903. return obj;
  2904. }
  2905. #endif
  2906. /*
  2907. * Caller needs to acquire correct kmem_list's list_lock
  2908. */
  2909. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  2910. int node)
  2911. {
  2912. int i;
  2913. struct kmem_list3 *l3;
  2914. for (i = 0; i < nr_objects; i++) {
  2915. void *objp = objpp[i];
  2916. struct slab *slabp;
  2917. slabp = virt_to_slab(objp);
  2918. l3 = cachep->nodelists[node];
  2919. list_del(&slabp->list);
  2920. check_spinlock_acquired_node(cachep, node);
  2921. check_slabp(cachep, slabp);
  2922. slab_put_obj(cachep, slabp, objp, node);
  2923. STATS_DEC_ACTIVE(cachep);
  2924. l3->free_objects++;
  2925. check_slabp(cachep, slabp);
  2926. /* fixup slab chains */
  2927. if (slabp->inuse == 0) {
  2928. if (l3->free_objects > l3->free_limit) {
  2929. l3->free_objects -= cachep->num;
  2930. /* No need to drop any previously held
  2931. * lock here, even if we have a off-slab slab
  2932. * descriptor it is guaranteed to come from
  2933. * a different cache, refer to comments before
  2934. * alloc_slabmgmt.
  2935. */
  2936. slab_destroy(cachep, slabp);
  2937. } else {
  2938. list_add(&slabp->list, &l3->slabs_free);
  2939. }
  2940. } else {
  2941. /* Unconditionally move a slab to the end of the
  2942. * partial list on free - maximum time for the
  2943. * other objects to be freed, too.
  2944. */
  2945. list_add_tail(&slabp->list, &l3->slabs_partial);
  2946. }
  2947. }
  2948. }
  2949. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  2950. {
  2951. int batchcount;
  2952. struct kmem_list3 *l3;
  2953. int node = numa_node_id();
  2954. batchcount = ac->batchcount;
  2955. #if DEBUG
  2956. BUG_ON(!batchcount || batchcount > ac->avail);
  2957. #endif
  2958. check_irq_off();
  2959. l3 = cachep->nodelists[node];
  2960. spin_lock(&l3->list_lock);
  2961. if (l3->shared) {
  2962. struct array_cache *shared_array = l3->shared;
  2963. int max = shared_array->limit - shared_array->avail;
  2964. if (max) {
  2965. if (batchcount > max)
  2966. batchcount = max;
  2967. memcpy(&(shared_array->entry[shared_array->avail]),
  2968. ac->entry, sizeof(void *) * batchcount);
  2969. shared_array->avail += batchcount;
  2970. goto free_done;
  2971. }
  2972. }
  2973. free_block(cachep, ac->entry, batchcount, node);
  2974. free_done:
  2975. #if STATS
  2976. {
  2977. int i = 0;
  2978. struct list_head *p;
  2979. p = l3->slabs_free.next;
  2980. while (p != &(l3->slabs_free)) {
  2981. struct slab *slabp;
  2982. slabp = list_entry(p, struct slab, list);
  2983. BUG_ON(slabp->inuse);
  2984. i++;
  2985. p = p->next;
  2986. }
  2987. STATS_SET_FREEABLE(cachep, i);
  2988. }
  2989. #endif
  2990. spin_unlock(&l3->list_lock);
  2991. ac->avail -= batchcount;
  2992. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  2993. }
  2994. /*
  2995. * Release an obj back to its cache. If the obj has a constructed state, it must
  2996. * be in this state _before_ it is released. Called with disabled ints.
  2997. */
  2998. static inline void __cache_free(struct kmem_cache *cachep, void *objp)
  2999. {
  3000. struct array_cache *ac = cpu_cache_get(cachep);
  3001. check_irq_off();
  3002. objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
  3003. if (cache_free_alien(cachep, objp))
  3004. return;
  3005. if (likely(ac->avail < ac->limit)) {
  3006. STATS_INC_FREEHIT(cachep);
  3007. ac->entry[ac->avail++] = objp;
  3008. return;
  3009. } else {
  3010. STATS_INC_FREEMISS(cachep);
  3011. cache_flusharray(cachep, ac);
  3012. ac->entry[ac->avail++] = objp;
  3013. }
  3014. }
  3015. /**
  3016. * kmem_cache_alloc - Allocate an object
  3017. * @cachep: The cache to allocate from.
  3018. * @flags: See kmalloc().
  3019. *
  3020. * Allocate an object from this cache. The flags are only relevant
  3021. * if the cache has no available objects.
  3022. */
  3023. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3024. {
  3025. return __cache_alloc(cachep, flags, __builtin_return_address(0));
  3026. }
  3027. EXPORT_SYMBOL(kmem_cache_alloc);
  3028. /**
  3029. * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
  3030. * @cache: The cache to allocate from.
  3031. * @flags: See kmalloc().
  3032. *
  3033. * Allocate an object from this cache and set the allocated memory to zero.
  3034. * The flags are only relevant if the cache has no available objects.
  3035. */
  3036. void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
  3037. {
  3038. void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
  3039. if (ret)
  3040. memset(ret, 0, obj_size(cache));
  3041. return ret;
  3042. }
  3043. EXPORT_SYMBOL(kmem_cache_zalloc);
  3044. /**
  3045. * kmem_ptr_validate - check if an untrusted pointer might
  3046. * be a slab entry.
  3047. * @cachep: the cache we're checking against
  3048. * @ptr: pointer to validate
  3049. *
  3050. * This verifies that the untrusted pointer looks sane:
  3051. * it is _not_ a guarantee that the pointer is actually
  3052. * part of the slab cache in question, but it at least
  3053. * validates that the pointer can be dereferenced and
  3054. * looks half-way sane.
  3055. *
  3056. * Currently only used for dentry validation.
  3057. */
  3058. int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
  3059. {
  3060. unsigned long addr = (unsigned long)ptr;
  3061. unsigned long min_addr = PAGE_OFFSET;
  3062. unsigned long align_mask = BYTES_PER_WORD - 1;
  3063. unsigned long size = cachep->buffer_size;
  3064. struct page *page;
  3065. if (unlikely(addr < min_addr))
  3066. goto out;
  3067. if (unlikely(addr > (unsigned long)high_memory - size))
  3068. goto out;
  3069. if (unlikely(addr & align_mask))
  3070. goto out;
  3071. if (unlikely(!kern_addr_valid(addr)))
  3072. goto out;
  3073. if (unlikely(!kern_addr_valid(addr + size - 1)))
  3074. goto out;
  3075. page = virt_to_page(ptr);
  3076. if (unlikely(!PageSlab(page)))
  3077. goto out;
  3078. if (unlikely(page_get_cache(page) != cachep))
  3079. goto out;
  3080. return 1;
  3081. out:
  3082. return 0;
  3083. }
  3084. #ifdef CONFIG_NUMA
  3085. /**
  3086. * kmem_cache_alloc_node - Allocate an object on the specified node
  3087. * @cachep: The cache to allocate from.
  3088. * @flags: See kmalloc().
  3089. * @nodeid: node number of the target node.
  3090. *
  3091. * Identical to kmem_cache_alloc but it will allocate memory on the given
  3092. * node, which can improve the performance for cpu bound structures.
  3093. *
  3094. * Fallback to other node is possible if __GFP_THISNODE is not set.
  3095. */
  3096. static __always_inline void *
  3097. __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  3098. int nodeid, void *caller)
  3099. {
  3100. unsigned long save_flags;
  3101. void *ptr = NULL;
  3102. cache_alloc_debugcheck_before(cachep, flags);
  3103. local_irq_save(save_flags);
  3104. if (unlikely(nodeid == -1))
  3105. nodeid = numa_node_id();
  3106. if (likely(cachep->nodelists[nodeid])) {
  3107. if (nodeid == numa_node_id()) {
  3108. /*
  3109. * Use the locally cached objects if possible.
  3110. * However ____cache_alloc does not allow fallback
  3111. * to other nodes. It may fail while we still have
  3112. * objects on other nodes available.
  3113. */
  3114. ptr = ____cache_alloc(cachep, flags);
  3115. }
  3116. if (!ptr) {
  3117. /* ___cache_alloc_node can fall back to other nodes */
  3118. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  3119. }
  3120. } else {
  3121. /* Node not bootstrapped yet */
  3122. if (!(flags & __GFP_THISNODE))
  3123. ptr = fallback_alloc(cachep, flags);
  3124. }
  3125. local_irq_restore(save_flags);
  3126. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  3127. return ptr;
  3128. }
  3129. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  3130. {
  3131. return __cache_alloc_node(cachep, flags, nodeid,
  3132. __builtin_return_address(0));
  3133. }
  3134. EXPORT_SYMBOL(kmem_cache_alloc_node);
  3135. static __always_inline void *
  3136. __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
  3137. {
  3138. struct kmem_cache *cachep;
  3139. cachep = kmem_find_general_cachep(size, flags);
  3140. if (unlikely(cachep == NULL))
  3141. return NULL;
  3142. return kmem_cache_alloc_node(cachep, flags, node);
  3143. }
  3144. #ifdef CONFIG_DEBUG_SLAB
  3145. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3146. {
  3147. return __do_kmalloc_node(size, flags, node,
  3148. __builtin_return_address(0));
  3149. }
  3150. EXPORT_SYMBOL(__kmalloc_node);
  3151. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3152. int node, void *caller)
  3153. {
  3154. return __do_kmalloc_node(size, flags, node, caller);
  3155. }
  3156. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3157. #else
  3158. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3159. {
  3160. return __do_kmalloc_node(size, flags, node, NULL);
  3161. }
  3162. EXPORT_SYMBOL(__kmalloc_node);
  3163. #endif /* CONFIG_DEBUG_SLAB */
  3164. #endif /* CONFIG_NUMA */
  3165. /**
  3166. * __do_kmalloc - allocate memory
  3167. * @size: how many bytes of memory are required.
  3168. * @flags: the type of memory to allocate (see kmalloc).
  3169. * @caller: function caller for debug tracking of the caller
  3170. */
  3171. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3172. void *caller)
  3173. {
  3174. struct kmem_cache *cachep;
  3175. /* If you want to save a few bytes .text space: replace
  3176. * __ with kmem_.
  3177. * Then kmalloc uses the uninlined functions instead of the inline
  3178. * functions.
  3179. */
  3180. cachep = __find_general_cachep(size, flags);
  3181. if (unlikely(cachep == NULL))
  3182. return NULL;
  3183. return __cache_alloc(cachep, flags, caller);
  3184. }
  3185. #ifdef CONFIG_DEBUG_SLAB
  3186. void *__kmalloc(size_t size, gfp_t flags)
  3187. {
  3188. return __do_kmalloc(size, flags, __builtin_return_address(0));
  3189. }
  3190. EXPORT_SYMBOL(__kmalloc);
  3191. void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
  3192. {
  3193. return __do_kmalloc(size, flags, caller);
  3194. }
  3195. EXPORT_SYMBOL(__kmalloc_track_caller);
  3196. #else
  3197. void *__kmalloc(size_t size, gfp_t flags)
  3198. {
  3199. return __do_kmalloc(size, flags, NULL);
  3200. }
  3201. EXPORT_SYMBOL(__kmalloc);
  3202. #endif
  3203. /**
  3204. * kmem_cache_free - Deallocate an object
  3205. * @cachep: The cache the allocation was from.
  3206. * @objp: The previously allocated object.
  3207. *
  3208. * Free an object which was previously allocated from this
  3209. * cache.
  3210. */
  3211. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3212. {
  3213. unsigned long flags;
  3214. BUG_ON(virt_to_cache(objp) != cachep);
  3215. local_irq_save(flags);
  3216. __cache_free(cachep, objp);
  3217. local_irq_restore(flags);
  3218. }
  3219. EXPORT_SYMBOL(kmem_cache_free);
  3220. /**
  3221. * kfree - free previously allocated memory
  3222. * @objp: pointer returned by kmalloc.
  3223. *
  3224. * If @objp is NULL, no operation is performed.
  3225. *
  3226. * Don't free memory not originally allocated by kmalloc()
  3227. * or you will run into trouble.
  3228. */
  3229. void kfree(const void *objp)
  3230. {
  3231. struct kmem_cache *c;
  3232. unsigned long flags;
  3233. if (unlikely(!objp))
  3234. return;
  3235. local_irq_save(flags);
  3236. kfree_debugcheck(objp);
  3237. c = virt_to_cache(objp);
  3238. debug_check_no_locks_freed(objp, obj_size(c));
  3239. __cache_free(c, (void *)objp);
  3240. local_irq_restore(flags);
  3241. }
  3242. EXPORT_SYMBOL(kfree);
  3243. unsigned int kmem_cache_size(struct kmem_cache *cachep)
  3244. {
  3245. return obj_size(cachep);
  3246. }
  3247. EXPORT_SYMBOL(kmem_cache_size);
  3248. const char *kmem_cache_name(struct kmem_cache *cachep)
  3249. {
  3250. return cachep->name;
  3251. }
  3252. EXPORT_SYMBOL_GPL(kmem_cache_name);
  3253. /*
  3254. * This initializes kmem_list3 or resizes varioius caches for all nodes.
  3255. */
  3256. static int alloc_kmemlist(struct kmem_cache *cachep)
  3257. {
  3258. int node;
  3259. struct kmem_list3 *l3;
  3260. struct array_cache *new_shared;
  3261. struct array_cache **new_alien = NULL;
  3262. for_each_online_node(node) {
  3263. if (use_alien_caches) {
  3264. new_alien = alloc_alien_cache(node, cachep->limit);
  3265. if (!new_alien)
  3266. goto fail;
  3267. }
  3268. new_shared = alloc_arraycache(node,
  3269. cachep->shared*cachep->batchcount,
  3270. 0xbaadf00d);
  3271. if (!new_shared) {
  3272. free_alien_cache(new_alien);
  3273. goto fail;
  3274. }
  3275. l3 = cachep->nodelists[node];
  3276. if (l3) {
  3277. struct array_cache *shared = l3->shared;
  3278. spin_lock_irq(&l3->list_lock);
  3279. if (shared)
  3280. free_block(cachep, shared->entry,
  3281. shared->avail, node);
  3282. l3->shared = new_shared;
  3283. if (!l3->alien) {
  3284. l3->alien = new_alien;
  3285. new_alien = NULL;
  3286. }
  3287. l3->free_limit = (1 + nr_cpus_node(node)) *
  3288. cachep->batchcount + cachep->num;
  3289. spin_unlock_irq(&l3->list_lock);
  3290. kfree(shared);
  3291. free_alien_cache(new_alien);
  3292. continue;
  3293. }
  3294. l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
  3295. if (!l3) {
  3296. free_alien_cache(new_alien);
  3297. kfree(new_shared);
  3298. goto fail;
  3299. }
  3300. kmem_list3_init(l3);
  3301. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3302. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3303. l3->shared = new_shared;
  3304. l3->alien = new_alien;
  3305. l3->free_limit = (1 + nr_cpus_node(node)) *
  3306. cachep->batchcount + cachep->num;
  3307. cachep->nodelists[node] = l3;
  3308. }
  3309. return 0;
  3310. fail:
  3311. if (!cachep->next.next) {
  3312. /* Cache is not active yet. Roll back what we did */
  3313. node--;
  3314. while (node >= 0) {
  3315. if (cachep->nodelists[node]) {
  3316. l3 = cachep->nodelists[node];
  3317. kfree(l3->shared);
  3318. free_alien_cache(l3->alien);
  3319. kfree(l3);
  3320. cachep->nodelists[node] = NULL;
  3321. }
  3322. node--;
  3323. }
  3324. }
  3325. return -ENOMEM;
  3326. }
  3327. struct ccupdate_struct {
  3328. struct kmem_cache *cachep;
  3329. struct array_cache *new[NR_CPUS];
  3330. };
  3331. static void do_ccupdate_local(void *info)
  3332. {
  3333. struct ccupdate_struct *new = info;
  3334. struct array_cache *old;
  3335. check_irq_off();
  3336. old = cpu_cache_get(new->cachep);
  3337. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3338. new->new[smp_processor_id()] = old;
  3339. }
  3340. /* Always called with the cache_chain_mutex held */
  3341. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3342. int batchcount, int shared)
  3343. {
  3344. struct ccupdate_struct *new;
  3345. int i;
  3346. new = kzalloc(sizeof(*new), GFP_KERNEL);
  3347. if (!new)
  3348. return -ENOMEM;
  3349. for_each_online_cpu(i) {
  3350. new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
  3351. batchcount);
  3352. if (!new->new[i]) {
  3353. for (i--; i >= 0; i--)
  3354. kfree(new->new[i]);
  3355. kfree(new);
  3356. return -ENOMEM;
  3357. }
  3358. }
  3359. new->cachep = cachep;
  3360. on_each_cpu(do_ccupdate_local, (void *)new, 1, 1);
  3361. check_irq_on();
  3362. cachep->batchcount = batchcount;
  3363. cachep->limit = limit;
  3364. cachep->shared = shared;
  3365. for_each_online_cpu(i) {
  3366. struct array_cache *ccold = new->new[i];
  3367. if (!ccold)
  3368. continue;
  3369. spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
  3370. free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
  3371. spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
  3372. kfree(ccold);
  3373. }
  3374. kfree(new);
  3375. return alloc_kmemlist(cachep);
  3376. }
  3377. /* Called with cache_chain_mutex held always */
  3378. static int enable_cpucache(struct kmem_cache *cachep)
  3379. {
  3380. int err;
  3381. int limit, shared;
  3382. /*
  3383. * The head array serves three purposes:
  3384. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3385. * - reduce the number of spinlock operations.
  3386. * - reduce the number of linked list operations on the slab and
  3387. * bufctl chains: array operations are cheaper.
  3388. * The numbers are guessed, we should auto-tune as described by
  3389. * Bonwick.
  3390. */
  3391. if (cachep->buffer_size > 131072)
  3392. limit = 1;
  3393. else if (cachep->buffer_size > PAGE_SIZE)
  3394. limit = 8;
  3395. else if (cachep->buffer_size > 1024)
  3396. limit = 24;
  3397. else if (cachep->buffer_size > 256)
  3398. limit = 54;
  3399. else
  3400. limit = 120;
  3401. /*
  3402. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3403. * allocation behaviour: Most allocs on one cpu, most free operations
  3404. * on another cpu. For these cases, an efficient object passing between
  3405. * cpus is necessary. This is provided by a shared array. The array
  3406. * replaces Bonwick's magazine layer.
  3407. * On uniprocessor, it's functionally equivalent (but less efficient)
  3408. * to a larger limit. Thus disabled by default.
  3409. */
  3410. shared = 0;
  3411. #ifdef CONFIG_SMP
  3412. if (cachep->buffer_size <= PAGE_SIZE)
  3413. shared = 8;
  3414. #endif
  3415. #if DEBUG
  3416. /*
  3417. * With debugging enabled, large batchcount lead to excessively long
  3418. * periods with disabled local interrupts. Limit the batchcount
  3419. */
  3420. if (limit > 32)
  3421. limit = 32;
  3422. #endif
  3423. err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
  3424. if (err)
  3425. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3426. cachep->name, -err);
  3427. return err;
  3428. }
  3429. /*
  3430. * Drain an array if it contains any elements taking the l3 lock only if
  3431. * necessary. Note that the l3 listlock also protects the array_cache
  3432. * if drain_array() is used on the shared array.
  3433. */
  3434. void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  3435. struct array_cache *ac, int force, int node)
  3436. {
  3437. int tofree;
  3438. if (!ac || !ac->avail)
  3439. return;
  3440. if (ac->touched && !force) {
  3441. ac->touched = 0;
  3442. } else {
  3443. spin_lock_irq(&l3->list_lock);
  3444. if (ac->avail) {
  3445. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3446. if (tofree > ac->avail)
  3447. tofree = (ac->avail + 1) / 2;
  3448. free_block(cachep, ac->entry, tofree, node);
  3449. ac->avail -= tofree;
  3450. memmove(ac->entry, &(ac->entry[tofree]),
  3451. sizeof(void *) * ac->avail);
  3452. }
  3453. spin_unlock_irq(&l3->list_lock);
  3454. }
  3455. }
  3456. /**
  3457. * cache_reap - Reclaim memory from caches.
  3458. * @unused: unused parameter
  3459. *
  3460. * Called from workqueue/eventd every few seconds.
  3461. * Purpose:
  3462. * - clear the per-cpu caches for this CPU.
  3463. * - return freeable pages to the main free memory pool.
  3464. *
  3465. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3466. * again on the next iteration.
  3467. */
  3468. static void cache_reap(struct work_struct *unused)
  3469. {
  3470. struct kmem_cache *searchp;
  3471. struct kmem_list3 *l3;
  3472. int node = numa_node_id();
  3473. if (!mutex_trylock(&cache_chain_mutex)) {
  3474. /* Give up. Setup the next iteration. */
  3475. schedule_delayed_work(&__get_cpu_var(reap_work),
  3476. REAPTIMEOUT_CPUC);
  3477. return;
  3478. }
  3479. list_for_each_entry(searchp, &cache_chain, next) {
  3480. check_irq_on();
  3481. /*
  3482. * We only take the l3 lock if absolutely necessary and we
  3483. * have established with reasonable certainty that
  3484. * we can do some work if the lock was obtained.
  3485. */
  3486. l3 = searchp->nodelists[node];
  3487. reap_alien(searchp, l3);
  3488. drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
  3489. /*
  3490. * These are racy checks but it does not matter
  3491. * if we skip one check or scan twice.
  3492. */
  3493. if (time_after(l3->next_reap, jiffies))
  3494. goto next;
  3495. l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3496. drain_array(searchp, l3, l3->shared, 0, node);
  3497. if (l3->free_touched)
  3498. l3->free_touched = 0;
  3499. else {
  3500. int freed;
  3501. freed = drain_freelist(searchp, l3, (l3->free_limit +
  3502. 5 * searchp->num - 1) / (5 * searchp->num));
  3503. STATS_ADD_REAPED(searchp, freed);
  3504. }
  3505. next:
  3506. cond_resched();
  3507. }
  3508. check_irq_on();
  3509. mutex_unlock(&cache_chain_mutex);
  3510. next_reap_node();
  3511. refresh_cpu_vm_stats(smp_processor_id());
  3512. /* Set up the next iteration */
  3513. schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC);
  3514. }
  3515. #ifdef CONFIG_PROC_FS
  3516. static void print_slabinfo_header(struct seq_file *m)
  3517. {
  3518. /*
  3519. * Output format version, so at least we can change it
  3520. * without _too_ many complaints.
  3521. */
  3522. #if STATS
  3523. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  3524. #else
  3525. seq_puts(m, "slabinfo - version: 2.1\n");
  3526. #endif
  3527. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3528. "<objperslab> <pagesperslab>");
  3529. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3530. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3531. #if STATS
  3532. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  3533. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  3534. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  3535. #endif
  3536. seq_putc(m, '\n');
  3537. }
  3538. static void *s_start(struct seq_file *m, loff_t *pos)
  3539. {
  3540. loff_t n = *pos;
  3541. struct list_head *p;
  3542. mutex_lock(&cache_chain_mutex);
  3543. if (!n)
  3544. print_slabinfo_header(m);
  3545. p = cache_chain.next;
  3546. while (n--) {
  3547. p = p->next;
  3548. if (p == &cache_chain)
  3549. return NULL;
  3550. }
  3551. return list_entry(p, struct kmem_cache, next);
  3552. }
  3553. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3554. {
  3555. struct kmem_cache *cachep = p;
  3556. ++*pos;
  3557. return cachep->next.next == &cache_chain ?
  3558. NULL : list_entry(cachep->next.next, struct kmem_cache, next);
  3559. }
  3560. static void s_stop(struct seq_file *m, void *p)
  3561. {
  3562. mutex_unlock(&cache_chain_mutex);
  3563. }
  3564. static int s_show(struct seq_file *m, void *p)
  3565. {
  3566. struct kmem_cache *cachep = p;
  3567. struct slab *slabp;
  3568. unsigned long active_objs;
  3569. unsigned long num_objs;
  3570. unsigned long active_slabs = 0;
  3571. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3572. const char *name;
  3573. char *error = NULL;
  3574. int node;
  3575. struct kmem_list3 *l3;
  3576. active_objs = 0;
  3577. num_slabs = 0;
  3578. for_each_online_node(node) {
  3579. l3 = cachep->nodelists[node];
  3580. if (!l3)
  3581. continue;
  3582. check_irq_on();
  3583. spin_lock_irq(&l3->list_lock);
  3584. list_for_each_entry(slabp, &l3->slabs_full, list) {
  3585. if (slabp->inuse != cachep->num && !error)
  3586. error = "slabs_full accounting error";
  3587. active_objs += cachep->num;
  3588. active_slabs++;
  3589. }
  3590. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  3591. if (slabp->inuse == cachep->num && !error)
  3592. error = "slabs_partial inuse accounting error";
  3593. if (!slabp->inuse && !error)
  3594. error = "slabs_partial/inuse accounting error";
  3595. active_objs += slabp->inuse;
  3596. active_slabs++;
  3597. }
  3598. list_for_each_entry(slabp, &l3->slabs_free, list) {
  3599. if (slabp->inuse && !error)
  3600. error = "slabs_free/inuse accounting error";
  3601. num_slabs++;
  3602. }
  3603. free_objects += l3->free_objects;
  3604. if (l3->shared)
  3605. shared_avail += l3->shared->avail;
  3606. spin_unlock_irq(&l3->list_lock);
  3607. }
  3608. num_slabs += active_slabs;
  3609. num_objs = num_slabs * cachep->num;
  3610. if (num_objs - active_objs != free_objects && !error)
  3611. error = "free_objects accounting error";
  3612. name = cachep->name;
  3613. if (error)
  3614. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3615. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  3616. name, active_objs, num_objs, cachep->buffer_size,
  3617. cachep->num, (1 << cachep->gfporder));
  3618. seq_printf(m, " : tunables %4u %4u %4u",
  3619. cachep->limit, cachep->batchcount, cachep->shared);
  3620. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  3621. active_slabs, num_slabs, shared_avail);
  3622. #if STATS
  3623. { /* list3 stats */
  3624. unsigned long high = cachep->high_mark;
  3625. unsigned long allocs = cachep->num_allocations;
  3626. unsigned long grown = cachep->grown;
  3627. unsigned long reaped = cachep->reaped;
  3628. unsigned long errors = cachep->errors;
  3629. unsigned long max_freeable = cachep->max_freeable;
  3630. unsigned long node_allocs = cachep->node_allocs;
  3631. unsigned long node_frees = cachep->node_frees;
  3632. unsigned long overflows = cachep->node_overflow;
  3633. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
  3634. %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
  3635. reaped, errors, max_freeable, node_allocs,
  3636. node_frees, overflows);
  3637. }
  3638. /* cpu stats */
  3639. {
  3640. unsigned long allochit = atomic_read(&cachep->allochit);
  3641. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3642. unsigned long freehit = atomic_read(&cachep->freehit);
  3643. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3644. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3645. allochit, allocmiss, freehit, freemiss);
  3646. }
  3647. #endif
  3648. seq_putc(m, '\n');
  3649. return 0;
  3650. }
  3651. /*
  3652. * slabinfo_op - iterator that generates /proc/slabinfo
  3653. *
  3654. * Output layout:
  3655. * cache-name
  3656. * num-active-objs
  3657. * total-objs
  3658. * object size
  3659. * num-active-slabs
  3660. * total-slabs
  3661. * num-pages-per-slab
  3662. * + further values on SMP and with statistics enabled
  3663. */
  3664. struct seq_operations slabinfo_op = {
  3665. .start = s_start,
  3666. .next = s_next,
  3667. .stop = s_stop,
  3668. .show = s_show,
  3669. };
  3670. #define MAX_SLABINFO_WRITE 128
  3671. /**
  3672. * slabinfo_write - Tuning for the slab allocator
  3673. * @file: unused
  3674. * @buffer: user buffer
  3675. * @count: data length
  3676. * @ppos: unused
  3677. */
  3678. ssize_t slabinfo_write(struct file *file, const char __user * buffer,
  3679. size_t count, loff_t *ppos)
  3680. {
  3681. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3682. int limit, batchcount, shared, res;
  3683. struct kmem_cache *cachep;
  3684. if (count > MAX_SLABINFO_WRITE)
  3685. return -EINVAL;
  3686. if (copy_from_user(&kbuf, buffer, count))
  3687. return -EFAULT;
  3688. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3689. tmp = strchr(kbuf, ' ');
  3690. if (!tmp)
  3691. return -EINVAL;
  3692. *tmp = '\0';
  3693. tmp++;
  3694. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3695. return -EINVAL;
  3696. /* Find the cache in the chain of caches. */
  3697. mutex_lock(&cache_chain_mutex);
  3698. res = -EINVAL;
  3699. list_for_each_entry(cachep, &cache_chain, next) {
  3700. if (!strcmp(cachep->name, kbuf)) {
  3701. if (limit < 1 || batchcount < 1 ||
  3702. batchcount > limit || shared < 0) {
  3703. res = 0;
  3704. } else {
  3705. res = do_tune_cpucache(cachep, limit,
  3706. batchcount, shared);
  3707. }
  3708. break;
  3709. }
  3710. }
  3711. mutex_unlock(&cache_chain_mutex);
  3712. if (res >= 0)
  3713. res = count;
  3714. return res;
  3715. }
  3716. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3717. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3718. {
  3719. loff_t n = *pos;
  3720. struct list_head *p;
  3721. mutex_lock(&cache_chain_mutex);
  3722. p = cache_chain.next;
  3723. while (n--) {
  3724. p = p->next;
  3725. if (p == &cache_chain)
  3726. return NULL;
  3727. }
  3728. return list_entry(p, struct kmem_cache, next);
  3729. }
  3730. static inline int add_caller(unsigned long *n, unsigned long v)
  3731. {
  3732. unsigned long *p;
  3733. int l;
  3734. if (!v)
  3735. return 1;
  3736. l = n[1];
  3737. p = n + 2;
  3738. while (l) {
  3739. int i = l/2;
  3740. unsigned long *q = p + 2 * i;
  3741. if (*q == v) {
  3742. q[1]++;
  3743. return 1;
  3744. }
  3745. if (*q > v) {
  3746. l = i;
  3747. } else {
  3748. p = q + 2;
  3749. l -= i + 1;
  3750. }
  3751. }
  3752. if (++n[1] == n[0])
  3753. return 0;
  3754. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3755. p[0] = v;
  3756. p[1] = 1;
  3757. return 1;
  3758. }
  3759. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  3760. {
  3761. void *p;
  3762. int i;
  3763. if (n[0] == n[1])
  3764. return;
  3765. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
  3766. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  3767. continue;
  3768. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  3769. return;
  3770. }
  3771. }
  3772. static void show_symbol(struct seq_file *m, unsigned long address)
  3773. {
  3774. #ifdef CONFIG_KALLSYMS
  3775. char *modname;
  3776. const char *name;
  3777. unsigned long offset, size;
  3778. char namebuf[KSYM_NAME_LEN+1];
  3779. name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
  3780. if (name) {
  3781. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3782. if (modname)
  3783. seq_printf(m, " [%s]", modname);
  3784. return;
  3785. }
  3786. #endif
  3787. seq_printf(m, "%p", (void *)address);
  3788. }
  3789. static int leaks_show(struct seq_file *m, void *p)
  3790. {
  3791. struct kmem_cache *cachep = p;
  3792. struct slab *slabp;
  3793. struct kmem_list3 *l3;
  3794. const char *name;
  3795. unsigned long *n = m->private;
  3796. int node;
  3797. int i;
  3798. if (!(cachep->flags & SLAB_STORE_USER))
  3799. return 0;
  3800. if (!(cachep->flags & SLAB_RED_ZONE))
  3801. return 0;
  3802. /* OK, we can do it */
  3803. n[1] = 0;
  3804. for_each_online_node(node) {
  3805. l3 = cachep->nodelists[node];
  3806. if (!l3)
  3807. continue;
  3808. check_irq_on();
  3809. spin_lock_irq(&l3->list_lock);
  3810. list_for_each_entry(slabp, &l3->slabs_full, list)
  3811. handle_slab(n, cachep, slabp);
  3812. list_for_each_entry(slabp, &l3->slabs_partial, list)
  3813. handle_slab(n, cachep, slabp);
  3814. spin_unlock_irq(&l3->list_lock);
  3815. }
  3816. name = cachep->name;
  3817. if (n[0] == n[1]) {
  3818. /* Increase the buffer size */
  3819. mutex_unlock(&cache_chain_mutex);
  3820. m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  3821. if (!m->private) {
  3822. /* Too bad, we are really out */
  3823. m->private = n;
  3824. mutex_lock(&cache_chain_mutex);
  3825. return -ENOMEM;
  3826. }
  3827. *(unsigned long *)m->private = n[0] * 2;
  3828. kfree(n);
  3829. mutex_lock(&cache_chain_mutex);
  3830. /* Now make sure this entry will be retried */
  3831. m->count = m->size;
  3832. return 0;
  3833. }
  3834. for (i = 0; i < n[1]; i++) {
  3835. seq_printf(m, "%s: %lu ", name, n[2*i+3]);
  3836. show_symbol(m, n[2*i+2]);
  3837. seq_putc(m, '\n');
  3838. }
  3839. return 0;
  3840. }
  3841. struct seq_operations slabstats_op = {
  3842. .start = leaks_start,
  3843. .next = s_next,
  3844. .stop = s_stop,
  3845. .show = leaks_show,
  3846. };
  3847. #endif
  3848. #endif
  3849. /**
  3850. * ksize - get the actual amount of memory allocated for a given object
  3851. * @objp: Pointer to the object
  3852. *
  3853. * kmalloc may internally round up allocations and return more memory
  3854. * than requested. ksize() can be used to determine the actual amount of
  3855. * memory allocated. The caller may use this additional memory, even though
  3856. * a smaller amount of memory was initially specified with the kmalloc call.
  3857. * The caller must guarantee that objp points to a valid object previously
  3858. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  3859. * must not be freed during the duration of the call.
  3860. */
  3861. unsigned int ksize(const void *objp)
  3862. {
  3863. if (unlikely(objp == NULL))
  3864. return 0;
  3865. return obj_size(virt_to_cache(objp));
  3866. }