slab.c 110 KB

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