slab.c 107 KB

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