slab.c 111 KB

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