slab.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331
  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. pr_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 & ~__GFP_NOFAIL;
  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 reclaim or
  737. * warn about failures.
  738. */
  739. static inline gfp_t gfp_exact_node(gfp_t flags)
  740. {
  741. return (flags | __GFP_THISNODE | __GFP_NOWARN) & ~(__GFP_RECLAIM|__GFP_NOFAIL);
  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. pr_warn("SLAB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
  1179. nodeid, gfpflags, &gfpflags);
  1180. pr_warn(" cache: %s, object size: %d, order: %d\n",
  1181. cachep->name, cachep->size, cachep->gfporder);
  1182. for_each_kmem_cache_node(cachep, node, n) {
  1183. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1184. unsigned long active_slabs = 0, num_slabs = 0;
  1185. spin_lock_irqsave(&n->list_lock, flags);
  1186. list_for_each_entry(page, &n->slabs_full, lru) {
  1187. active_objs += cachep->num;
  1188. active_slabs++;
  1189. }
  1190. list_for_each_entry(page, &n->slabs_partial, lru) {
  1191. active_objs += page->active;
  1192. active_slabs++;
  1193. }
  1194. list_for_each_entry(page, &n->slabs_free, lru)
  1195. num_slabs++;
  1196. free_objects += n->free_objects;
  1197. spin_unlock_irqrestore(&n->list_lock, flags);
  1198. num_slabs += active_slabs;
  1199. num_objs = num_slabs * cachep->num;
  1200. pr_warn(" node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1201. node, active_slabs, num_slabs, active_objs, num_objs,
  1202. free_objects);
  1203. }
  1204. #endif
  1205. }
  1206. /*
  1207. * Interface to system's page allocator. No need to hold the
  1208. * kmem_cache_node ->list_lock.
  1209. *
  1210. * If we requested dmaable memory, we will get it. Even if we
  1211. * did not request dmaable memory, we might get it, but that
  1212. * would be relatively rare and ignorable.
  1213. */
  1214. static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
  1215. int nodeid)
  1216. {
  1217. struct page *page;
  1218. int nr_pages;
  1219. flags |= cachep->allocflags;
  1220. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1221. flags |= __GFP_RECLAIMABLE;
  1222. page = __alloc_pages_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1223. if (!page) {
  1224. slab_out_of_memory(cachep, flags, nodeid);
  1225. return NULL;
  1226. }
  1227. if (memcg_charge_slab(page, flags, cachep->gfporder, cachep)) {
  1228. __free_pages(page, cachep->gfporder);
  1229. return NULL;
  1230. }
  1231. nr_pages = (1 << cachep->gfporder);
  1232. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1233. add_zone_page_state(page_zone(page),
  1234. NR_SLAB_RECLAIMABLE, nr_pages);
  1235. else
  1236. add_zone_page_state(page_zone(page),
  1237. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1238. __SetPageSlab(page);
  1239. /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
  1240. if (sk_memalloc_socks() && page_is_pfmemalloc(page))
  1241. SetPageSlabPfmemalloc(page);
  1242. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1243. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1244. if (cachep->ctor)
  1245. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1246. else
  1247. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1248. }
  1249. return page;
  1250. }
  1251. /*
  1252. * Interface to system's page release.
  1253. */
  1254. static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
  1255. {
  1256. int order = cachep->gfporder;
  1257. unsigned long nr_freed = (1 << order);
  1258. kmemcheck_free_shadow(page, order);
  1259. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1260. sub_zone_page_state(page_zone(page),
  1261. NR_SLAB_RECLAIMABLE, nr_freed);
  1262. else
  1263. sub_zone_page_state(page_zone(page),
  1264. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1265. BUG_ON(!PageSlab(page));
  1266. __ClearPageSlabPfmemalloc(page);
  1267. __ClearPageSlab(page);
  1268. page_mapcount_reset(page);
  1269. page->mapping = NULL;
  1270. if (current->reclaim_state)
  1271. current->reclaim_state->reclaimed_slab += nr_freed;
  1272. memcg_uncharge_slab(page, order, cachep);
  1273. __free_pages(page, order);
  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. pr_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. pr_err("Single bit error detected. Probably bad RAM.\n");
  1356. #ifdef CONFIG_X86
  1357. pr_err("Run memtest86+ or a similar memory test tool.\n");
  1358. #else
  1359. pr_err("Run a memory test tool.\n");
  1360. #endif
  1361. }
  1362. }
  1363. }
  1364. #endif
  1365. #if DEBUG
  1366. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1367. {
  1368. int i, size;
  1369. char *realobj;
  1370. if (cachep->flags & SLAB_RED_ZONE) {
  1371. pr_err("Redzone: 0x%llx/0x%llx\n",
  1372. *dbg_redzone1(cachep, objp),
  1373. *dbg_redzone2(cachep, objp));
  1374. }
  1375. if (cachep->flags & SLAB_STORE_USER) {
  1376. pr_err("Last user: [<%p>](%pSR)\n",
  1377. *dbg_userword(cachep, objp),
  1378. *dbg_userword(cachep, objp));
  1379. }
  1380. realobj = (char *)objp + obj_offset(cachep);
  1381. size = cachep->object_size;
  1382. for (i = 0; i < size && lines; i += 16, lines--) {
  1383. int limit;
  1384. limit = 16;
  1385. if (i + limit > size)
  1386. limit = size - i;
  1387. dump_line(realobj, i, limit);
  1388. }
  1389. }
  1390. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1391. {
  1392. char *realobj;
  1393. int size, i;
  1394. int lines = 0;
  1395. if (is_debug_pagealloc_cache(cachep))
  1396. return;
  1397. realobj = (char *)objp + obj_offset(cachep);
  1398. size = cachep->object_size;
  1399. for (i = 0; i < size; i++) {
  1400. char exp = POISON_FREE;
  1401. if (i == size - 1)
  1402. exp = POISON_END;
  1403. if (realobj[i] != exp) {
  1404. int limit;
  1405. /* Mismatch ! */
  1406. /* Print header */
  1407. if (lines == 0) {
  1408. pr_err("Slab corruption (%s): %s start=%p, len=%d\n",
  1409. print_tainted(), cachep->name,
  1410. realobj, size);
  1411. print_objinfo(cachep, objp, 0);
  1412. }
  1413. /* Hexdump the affected line */
  1414. i = (i / 16) * 16;
  1415. limit = 16;
  1416. if (i + limit > size)
  1417. limit = size - i;
  1418. dump_line(realobj, i, limit);
  1419. i += 16;
  1420. lines++;
  1421. /* Limit to 5 lines */
  1422. if (lines > 5)
  1423. break;
  1424. }
  1425. }
  1426. if (lines != 0) {
  1427. /* Print some data about the neighboring objects, if they
  1428. * exist:
  1429. */
  1430. struct page *page = virt_to_head_page(objp);
  1431. unsigned int objnr;
  1432. objnr = obj_to_index(cachep, page, objp);
  1433. if (objnr) {
  1434. objp = index_to_obj(cachep, page, objnr - 1);
  1435. realobj = (char *)objp + obj_offset(cachep);
  1436. pr_err("Prev obj: start=%p, len=%d\n", realobj, size);
  1437. print_objinfo(cachep, objp, 2);
  1438. }
  1439. if (objnr + 1 < cachep->num) {
  1440. objp = index_to_obj(cachep, page, objnr + 1);
  1441. realobj = (char *)objp + obj_offset(cachep);
  1442. pr_err("Next obj: start=%p, len=%d\n", realobj, size);
  1443. print_objinfo(cachep, objp, 2);
  1444. }
  1445. }
  1446. }
  1447. #endif
  1448. #if DEBUG
  1449. static void slab_destroy_debugcheck(struct kmem_cache *cachep,
  1450. struct page *page)
  1451. {
  1452. int i;
  1453. if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) {
  1454. poison_obj(cachep, page->freelist - obj_offset(cachep),
  1455. POISON_FREE);
  1456. }
  1457. for (i = 0; i < cachep->num; i++) {
  1458. void *objp = index_to_obj(cachep, page, i);
  1459. if (cachep->flags & SLAB_POISON) {
  1460. check_poison_obj(cachep, objp);
  1461. slab_kernel_map(cachep, objp, 1, 0);
  1462. }
  1463. if (cachep->flags & SLAB_RED_ZONE) {
  1464. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1465. slab_error(cachep, "start of a freed object was overwritten");
  1466. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1467. slab_error(cachep, "end of a freed object was overwritten");
  1468. }
  1469. }
  1470. }
  1471. #else
  1472. static void slab_destroy_debugcheck(struct kmem_cache *cachep,
  1473. struct page *page)
  1474. {
  1475. }
  1476. #endif
  1477. /**
  1478. * slab_destroy - destroy and release all objects in a slab
  1479. * @cachep: cache pointer being destroyed
  1480. * @page: page pointer being destroyed
  1481. *
  1482. * Destroy all the objs in a slab page, and release the mem back to the system.
  1483. * Before calling the slab page must have been unlinked from the cache. The
  1484. * kmem_cache_node ->list_lock is not held/needed.
  1485. */
  1486. static void slab_destroy(struct kmem_cache *cachep, struct page *page)
  1487. {
  1488. void *freelist;
  1489. freelist = page->freelist;
  1490. slab_destroy_debugcheck(cachep, page);
  1491. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  1492. call_rcu(&page->rcu_head, kmem_rcu_free);
  1493. else
  1494. kmem_freepages(cachep, page);
  1495. /*
  1496. * From now on, we don't use freelist
  1497. * although actual page can be freed in rcu context
  1498. */
  1499. if (OFF_SLAB(cachep))
  1500. kmem_cache_free(cachep->freelist_cache, freelist);
  1501. }
  1502. static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
  1503. {
  1504. struct page *page, *n;
  1505. list_for_each_entry_safe(page, n, list, lru) {
  1506. list_del(&page->lru);
  1507. slab_destroy(cachep, page);
  1508. }
  1509. }
  1510. /**
  1511. * calculate_slab_order - calculate size (page order) of slabs
  1512. * @cachep: pointer to the cache that is being created
  1513. * @size: size of objects to be created in this cache.
  1514. * @flags: slab allocation flags
  1515. *
  1516. * Also calculates the number of objects per slab.
  1517. *
  1518. * This could be made much more intelligent. For now, try to avoid using
  1519. * high order pages for slabs. When the gfp() functions are more friendly
  1520. * towards high-order requests, this should be changed.
  1521. */
  1522. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1523. size_t size, unsigned long flags)
  1524. {
  1525. size_t left_over = 0;
  1526. int gfporder;
  1527. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1528. unsigned int num;
  1529. size_t remainder;
  1530. num = cache_estimate(gfporder, size, flags, &remainder);
  1531. if (!num)
  1532. continue;
  1533. /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
  1534. if (num > SLAB_OBJ_MAX_NUM)
  1535. break;
  1536. if (flags & CFLGS_OFF_SLAB) {
  1537. struct kmem_cache *freelist_cache;
  1538. size_t freelist_size;
  1539. freelist_size = num * sizeof(freelist_idx_t);
  1540. freelist_cache = kmalloc_slab(freelist_size, 0u);
  1541. if (!freelist_cache)
  1542. continue;
  1543. /*
  1544. * Needed to avoid possible looping condition
  1545. * in cache_grow()
  1546. */
  1547. if (OFF_SLAB(freelist_cache))
  1548. continue;
  1549. /* check if off slab has enough benefit */
  1550. if (freelist_cache->size > cachep->size / 2)
  1551. continue;
  1552. }
  1553. /* Found something acceptable - save it away */
  1554. cachep->num = num;
  1555. cachep->gfporder = gfporder;
  1556. left_over = remainder;
  1557. /*
  1558. * A VFS-reclaimable slab tends to have most allocations
  1559. * as GFP_NOFS and we really don't want to have to be allocating
  1560. * higher-order pages when we are unable to shrink dcache.
  1561. */
  1562. if (flags & SLAB_RECLAIM_ACCOUNT)
  1563. break;
  1564. /*
  1565. * Large number of objects is good, but very large slabs are
  1566. * currently bad for the gfp()s.
  1567. */
  1568. if (gfporder >= slab_max_order)
  1569. break;
  1570. /*
  1571. * Acceptable internal fragmentation?
  1572. */
  1573. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1574. break;
  1575. }
  1576. return left_over;
  1577. }
  1578. static struct array_cache __percpu *alloc_kmem_cache_cpus(
  1579. struct kmem_cache *cachep, int entries, int batchcount)
  1580. {
  1581. int cpu;
  1582. size_t size;
  1583. struct array_cache __percpu *cpu_cache;
  1584. size = sizeof(void *) * entries + sizeof(struct array_cache);
  1585. cpu_cache = __alloc_percpu(size, sizeof(void *));
  1586. if (!cpu_cache)
  1587. return NULL;
  1588. for_each_possible_cpu(cpu) {
  1589. init_arraycache(per_cpu_ptr(cpu_cache, cpu),
  1590. entries, batchcount);
  1591. }
  1592. return cpu_cache;
  1593. }
  1594. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  1595. {
  1596. if (slab_state >= FULL)
  1597. return enable_cpucache(cachep, gfp);
  1598. cachep->cpu_cache = alloc_kmem_cache_cpus(cachep, 1, 1);
  1599. if (!cachep->cpu_cache)
  1600. return 1;
  1601. if (slab_state == DOWN) {
  1602. /* Creation of first cache (kmem_cache). */
  1603. set_up_node(kmem_cache, CACHE_CACHE);
  1604. } else if (slab_state == PARTIAL) {
  1605. /* For kmem_cache_node */
  1606. set_up_node(cachep, SIZE_NODE);
  1607. } else {
  1608. int node;
  1609. for_each_online_node(node) {
  1610. cachep->node[node] = kmalloc_node(
  1611. sizeof(struct kmem_cache_node), gfp, node);
  1612. BUG_ON(!cachep->node[node]);
  1613. kmem_cache_node_init(cachep->node[node]);
  1614. }
  1615. }
  1616. cachep->node[numa_mem_id()]->next_reap =
  1617. jiffies + REAPTIMEOUT_NODE +
  1618. ((unsigned long)cachep) % REAPTIMEOUT_NODE;
  1619. cpu_cache_get(cachep)->avail = 0;
  1620. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1621. cpu_cache_get(cachep)->batchcount = 1;
  1622. cpu_cache_get(cachep)->touched = 0;
  1623. cachep->batchcount = 1;
  1624. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1625. return 0;
  1626. }
  1627. unsigned long kmem_cache_flags(unsigned long object_size,
  1628. unsigned long flags, const char *name,
  1629. void (*ctor)(void *))
  1630. {
  1631. return flags;
  1632. }
  1633. struct kmem_cache *
  1634. __kmem_cache_alias(const char *name, size_t size, size_t align,
  1635. unsigned long flags, void (*ctor)(void *))
  1636. {
  1637. struct kmem_cache *cachep;
  1638. cachep = find_mergeable(size, align, flags, name, ctor);
  1639. if (cachep) {
  1640. cachep->refcount++;
  1641. /*
  1642. * Adjust the object sizes so that we clear
  1643. * the complete object on kzalloc.
  1644. */
  1645. cachep->object_size = max_t(int, cachep->object_size, size);
  1646. }
  1647. return cachep;
  1648. }
  1649. static bool set_objfreelist_slab_cache(struct kmem_cache *cachep,
  1650. size_t size, unsigned long flags)
  1651. {
  1652. size_t left;
  1653. cachep->num = 0;
  1654. if (cachep->ctor || flags & SLAB_DESTROY_BY_RCU)
  1655. return false;
  1656. left = calculate_slab_order(cachep, size,
  1657. flags | CFLGS_OBJFREELIST_SLAB);
  1658. if (!cachep->num)
  1659. return false;
  1660. if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size)
  1661. return false;
  1662. cachep->colour = left / cachep->colour_off;
  1663. return true;
  1664. }
  1665. static bool set_off_slab_cache(struct kmem_cache *cachep,
  1666. size_t size, unsigned long flags)
  1667. {
  1668. size_t left;
  1669. cachep->num = 0;
  1670. /*
  1671. * Always use on-slab management when SLAB_NOLEAKTRACE
  1672. * to avoid recursive calls into kmemleak.
  1673. */
  1674. if (flags & SLAB_NOLEAKTRACE)
  1675. return false;
  1676. /*
  1677. * Size is large, assume best to place the slab management obj
  1678. * off-slab (should allow better packing of objs).
  1679. */
  1680. left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB);
  1681. if (!cachep->num)
  1682. return false;
  1683. /*
  1684. * If the slab has been placed off-slab, and we have enough space then
  1685. * move it on-slab. This is at the expense of any extra colouring.
  1686. */
  1687. if (left >= cachep->num * sizeof(freelist_idx_t))
  1688. return false;
  1689. cachep->colour = left / cachep->colour_off;
  1690. return true;
  1691. }
  1692. static bool set_on_slab_cache(struct kmem_cache *cachep,
  1693. size_t size, unsigned long flags)
  1694. {
  1695. size_t left;
  1696. cachep->num = 0;
  1697. left = calculate_slab_order(cachep, size, flags);
  1698. if (!cachep->num)
  1699. return false;
  1700. cachep->colour = left / cachep->colour_off;
  1701. return true;
  1702. }
  1703. /**
  1704. * __kmem_cache_create - Create a cache.
  1705. * @cachep: cache management descriptor
  1706. * @flags: SLAB flags
  1707. *
  1708. * Returns a ptr to the cache on success, NULL on failure.
  1709. * Cannot be called within a int, but can be interrupted.
  1710. * The @ctor is run when new pages are allocated by the cache.
  1711. *
  1712. * The flags are
  1713. *
  1714. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1715. * to catch references to uninitialised memory.
  1716. *
  1717. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1718. * for buffer overruns.
  1719. *
  1720. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1721. * cacheline. This can be beneficial if you're counting cycles as closely
  1722. * as davem.
  1723. */
  1724. int
  1725. __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
  1726. {
  1727. size_t ralign = BYTES_PER_WORD;
  1728. gfp_t gfp;
  1729. int err;
  1730. size_t size = cachep->size;
  1731. #if DEBUG
  1732. #if FORCED_DEBUG
  1733. /*
  1734. * Enable redzoning and last user accounting, except for caches with
  1735. * large objects, if the increased size would increase the object size
  1736. * above the next power of two: caches with object sizes just above a
  1737. * power of two have a significant amount of internal fragmentation.
  1738. */
  1739. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  1740. 2 * sizeof(unsigned long long)))
  1741. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1742. if (!(flags & SLAB_DESTROY_BY_RCU))
  1743. flags |= SLAB_POISON;
  1744. #endif
  1745. #endif
  1746. /*
  1747. * Check that size is in terms of words. This is needed to avoid
  1748. * unaligned accesses for some archs when redzoning is used, and makes
  1749. * sure any on-slab bufctl's are also correctly aligned.
  1750. */
  1751. if (size & (BYTES_PER_WORD - 1)) {
  1752. size += (BYTES_PER_WORD - 1);
  1753. size &= ~(BYTES_PER_WORD - 1);
  1754. }
  1755. if (flags & SLAB_RED_ZONE) {
  1756. ralign = REDZONE_ALIGN;
  1757. /* If redzoning, ensure that the second redzone is suitably
  1758. * aligned, by adjusting the object size accordingly. */
  1759. size += REDZONE_ALIGN - 1;
  1760. size &= ~(REDZONE_ALIGN - 1);
  1761. }
  1762. /* 3) caller mandated alignment */
  1763. if (ralign < cachep->align) {
  1764. ralign = cachep->align;
  1765. }
  1766. /* disable debug if necessary */
  1767. if (ralign > __alignof__(unsigned long long))
  1768. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1769. /*
  1770. * 4) Store it.
  1771. */
  1772. cachep->align = ralign;
  1773. cachep->colour_off = cache_line_size();
  1774. /* Offset must be a multiple of the alignment. */
  1775. if (cachep->colour_off < cachep->align)
  1776. cachep->colour_off = cachep->align;
  1777. if (slab_is_available())
  1778. gfp = GFP_KERNEL;
  1779. else
  1780. gfp = GFP_NOWAIT;
  1781. #if DEBUG
  1782. /*
  1783. * Both debugging options require word-alignment which is calculated
  1784. * into align above.
  1785. */
  1786. if (flags & SLAB_RED_ZONE) {
  1787. /* add space for red zone words */
  1788. cachep->obj_offset += sizeof(unsigned long long);
  1789. size += 2 * sizeof(unsigned long long);
  1790. }
  1791. if (flags & SLAB_STORE_USER) {
  1792. /* user store requires one word storage behind the end of
  1793. * the real object. But if the second red zone needs to be
  1794. * aligned to 64 bits, we must allow that much space.
  1795. */
  1796. if (flags & SLAB_RED_ZONE)
  1797. size += REDZONE_ALIGN;
  1798. else
  1799. size += BYTES_PER_WORD;
  1800. }
  1801. #endif
  1802. kasan_cache_create(cachep, &size, &flags);
  1803. size = ALIGN(size, cachep->align);
  1804. /*
  1805. * We should restrict the number of objects in a slab to implement
  1806. * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
  1807. */
  1808. if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
  1809. size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
  1810. #if DEBUG
  1811. /*
  1812. * To activate debug pagealloc, off-slab management is necessary
  1813. * requirement. In early phase of initialization, small sized slab
  1814. * doesn't get initialized so it would not be possible. So, we need
  1815. * to check size >= 256. It guarantees that all necessary small
  1816. * sized slab is initialized in current slab initialization sequence.
  1817. */
  1818. if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
  1819. size >= 256 && cachep->object_size > cache_line_size()) {
  1820. if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
  1821. size_t tmp_size = ALIGN(size, PAGE_SIZE);
  1822. if (set_off_slab_cache(cachep, tmp_size, flags)) {
  1823. flags |= CFLGS_OFF_SLAB;
  1824. cachep->obj_offset += tmp_size - size;
  1825. size = tmp_size;
  1826. goto done;
  1827. }
  1828. }
  1829. }
  1830. #endif
  1831. if (set_objfreelist_slab_cache(cachep, size, flags)) {
  1832. flags |= CFLGS_OBJFREELIST_SLAB;
  1833. goto done;
  1834. }
  1835. if (set_off_slab_cache(cachep, size, flags)) {
  1836. flags |= CFLGS_OFF_SLAB;
  1837. goto done;
  1838. }
  1839. if (set_on_slab_cache(cachep, size, flags))
  1840. goto done;
  1841. return -E2BIG;
  1842. done:
  1843. cachep->freelist_size = cachep->num * sizeof(freelist_idx_t);
  1844. cachep->flags = flags;
  1845. cachep->allocflags = __GFP_COMP;
  1846. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  1847. cachep->allocflags |= GFP_DMA;
  1848. cachep->size = size;
  1849. cachep->reciprocal_buffer_size = reciprocal_value(size);
  1850. #if DEBUG
  1851. /*
  1852. * If we're going to use the generic kernel_map_pages()
  1853. * poisoning, then it's going to smash the contents of
  1854. * the redzone and userword anyhow, so switch them off.
  1855. */
  1856. if (IS_ENABLED(CONFIG_PAGE_POISONING) &&
  1857. (cachep->flags & SLAB_POISON) &&
  1858. is_debug_pagealloc_cache(cachep))
  1859. cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1860. #endif
  1861. if (OFF_SLAB(cachep)) {
  1862. cachep->freelist_cache =
  1863. kmalloc_slab(cachep->freelist_size, 0u);
  1864. }
  1865. err = setup_cpu_cache(cachep, gfp);
  1866. if (err) {
  1867. __kmem_cache_release(cachep);
  1868. return err;
  1869. }
  1870. return 0;
  1871. }
  1872. #if DEBUG
  1873. static void check_irq_off(void)
  1874. {
  1875. BUG_ON(!irqs_disabled());
  1876. }
  1877. static void check_irq_on(void)
  1878. {
  1879. BUG_ON(irqs_disabled());
  1880. }
  1881. static void check_spinlock_acquired(struct kmem_cache *cachep)
  1882. {
  1883. #ifdef CONFIG_SMP
  1884. check_irq_off();
  1885. assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
  1886. #endif
  1887. }
  1888. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  1889. {
  1890. #ifdef CONFIG_SMP
  1891. check_irq_off();
  1892. assert_spin_locked(&get_node(cachep, node)->list_lock);
  1893. #endif
  1894. }
  1895. #else
  1896. #define check_irq_off() do { } while(0)
  1897. #define check_irq_on() do { } while(0)
  1898. #define check_spinlock_acquired(x) do { } while(0)
  1899. #define check_spinlock_acquired_node(x, y) do { } while(0)
  1900. #endif
  1901. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  1902. struct array_cache *ac,
  1903. int force, int node);
  1904. static void do_drain(void *arg)
  1905. {
  1906. struct kmem_cache *cachep = arg;
  1907. struct array_cache *ac;
  1908. int node = numa_mem_id();
  1909. struct kmem_cache_node *n;
  1910. LIST_HEAD(list);
  1911. check_irq_off();
  1912. ac = cpu_cache_get(cachep);
  1913. n = get_node(cachep, node);
  1914. spin_lock(&n->list_lock);
  1915. free_block(cachep, ac->entry, ac->avail, node, &list);
  1916. spin_unlock(&n->list_lock);
  1917. slabs_destroy(cachep, &list);
  1918. ac->avail = 0;
  1919. }
  1920. static void drain_cpu_caches(struct kmem_cache *cachep)
  1921. {
  1922. struct kmem_cache_node *n;
  1923. int node;
  1924. on_each_cpu(do_drain, cachep, 1);
  1925. check_irq_on();
  1926. for_each_kmem_cache_node(cachep, node, n)
  1927. if (n->alien)
  1928. drain_alien_cache(cachep, n->alien);
  1929. for_each_kmem_cache_node(cachep, node, n)
  1930. drain_array(cachep, n, n->shared, 1, node);
  1931. }
  1932. /*
  1933. * Remove slabs from the list of free slabs.
  1934. * Specify the number of slabs to drain in tofree.
  1935. *
  1936. * Returns the actual number of slabs released.
  1937. */
  1938. static int drain_freelist(struct kmem_cache *cache,
  1939. struct kmem_cache_node *n, int tofree)
  1940. {
  1941. struct list_head *p;
  1942. int nr_freed;
  1943. struct page *page;
  1944. nr_freed = 0;
  1945. while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
  1946. spin_lock_irq(&n->list_lock);
  1947. p = n->slabs_free.prev;
  1948. if (p == &n->slabs_free) {
  1949. spin_unlock_irq(&n->list_lock);
  1950. goto out;
  1951. }
  1952. page = list_entry(p, struct page, lru);
  1953. list_del(&page->lru);
  1954. /*
  1955. * Safe to drop the lock. The slab is no longer linked
  1956. * to the cache.
  1957. */
  1958. n->free_objects -= cache->num;
  1959. spin_unlock_irq(&n->list_lock);
  1960. slab_destroy(cache, page);
  1961. nr_freed++;
  1962. }
  1963. out:
  1964. return nr_freed;
  1965. }
  1966. int __kmem_cache_shrink(struct kmem_cache *cachep, bool deactivate)
  1967. {
  1968. int ret = 0;
  1969. int node;
  1970. struct kmem_cache_node *n;
  1971. drain_cpu_caches(cachep);
  1972. check_irq_on();
  1973. for_each_kmem_cache_node(cachep, node, n) {
  1974. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  1975. ret += !list_empty(&n->slabs_full) ||
  1976. !list_empty(&n->slabs_partial);
  1977. }
  1978. return (ret ? 1 : 0);
  1979. }
  1980. int __kmem_cache_shutdown(struct kmem_cache *cachep)
  1981. {
  1982. return __kmem_cache_shrink(cachep, false);
  1983. }
  1984. void __kmem_cache_release(struct kmem_cache *cachep)
  1985. {
  1986. int i;
  1987. struct kmem_cache_node *n;
  1988. free_percpu(cachep->cpu_cache);
  1989. /* NUMA: free the node structures */
  1990. for_each_kmem_cache_node(cachep, i, n) {
  1991. kfree(n->shared);
  1992. free_alien_cache(n->alien);
  1993. kfree(n);
  1994. cachep->node[i] = NULL;
  1995. }
  1996. }
  1997. /*
  1998. * Get the memory for a slab management obj.
  1999. *
  2000. * For a slab cache when the slab descriptor is off-slab, the
  2001. * slab descriptor can't come from the same cache which is being created,
  2002. * Because if it is the case, that means we defer the creation of
  2003. * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
  2004. * And we eventually call down to __kmem_cache_create(), which
  2005. * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
  2006. * This is a "chicken-and-egg" problem.
  2007. *
  2008. * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
  2009. * which are all initialized during kmem_cache_init().
  2010. */
  2011. static void *alloc_slabmgmt(struct kmem_cache *cachep,
  2012. struct page *page, int colour_off,
  2013. gfp_t local_flags, int nodeid)
  2014. {
  2015. void *freelist;
  2016. void *addr = page_address(page);
  2017. page->s_mem = addr + colour_off;
  2018. page->active = 0;
  2019. if (OBJFREELIST_SLAB(cachep))
  2020. freelist = NULL;
  2021. else if (OFF_SLAB(cachep)) {
  2022. /* Slab management obj is off-slab. */
  2023. freelist = kmem_cache_alloc_node(cachep->freelist_cache,
  2024. local_flags, nodeid);
  2025. if (!freelist)
  2026. return NULL;
  2027. } else {
  2028. /* We will use last bytes at the slab for freelist */
  2029. freelist = addr + (PAGE_SIZE << cachep->gfporder) -
  2030. cachep->freelist_size;
  2031. }
  2032. return freelist;
  2033. }
  2034. static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
  2035. {
  2036. return ((freelist_idx_t *)page->freelist)[idx];
  2037. }
  2038. static inline void set_free_obj(struct page *page,
  2039. unsigned int idx, freelist_idx_t val)
  2040. {
  2041. ((freelist_idx_t *)(page->freelist))[idx] = val;
  2042. }
  2043. static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page)
  2044. {
  2045. #if DEBUG
  2046. int i;
  2047. for (i = 0; i < cachep->num; i++) {
  2048. void *objp = index_to_obj(cachep, page, i);
  2049. if (cachep->flags & SLAB_STORE_USER)
  2050. *dbg_userword(cachep, objp) = NULL;
  2051. if (cachep->flags & SLAB_RED_ZONE) {
  2052. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2053. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2054. }
  2055. /*
  2056. * Constructors are not allowed to allocate memory from the same
  2057. * cache which they are a constructor for. Otherwise, deadlock.
  2058. * They must also be threaded.
  2059. */
  2060. if (cachep->ctor && !(cachep->flags & SLAB_POISON)) {
  2061. kasan_unpoison_object_data(cachep,
  2062. objp + obj_offset(cachep));
  2063. cachep->ctor(objp + obj_offset(cachep));
  2064. kasan_poison_object_data(
  2065. cachep, objp + obj_offset(cachep));
  2066. }
  2067. if (cachep->flags & SLAB_RED_ZONE) {
  2068. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2069. slab_error(cachep, "constructor overwrote the end of an object");
  2070. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2071. slab_error(cachep, "constructor overwrote the start of an object");
  2072. }
  2073. /* need to poison the objs? */
  2074. if (cachep->flags & SLAB_POISON) {
  2075. poison_obj(cachep, objp, POISON_FREE);
  2076. slab_kernel_map(cachep, objp, 0, 0);
  2077. }
  2078. }
  2079. #endif
  2080. }
  2081. static void cache_init_objs(struct kmem_cache *cachep,
  2082. struct page *page)
  2083. {
  2084. int i;
  2085. void *objp;
  2086. cache_init_objs_debug(cachep, page);
  2087. if (OBJFREELIST_SLAB(cachep)) {
  2088. page->freelist = index_to_obj(cachep, page, cachep->num - 1) +
  2089. obj_offset(cachep);
  2090. }
  2091. for (i = 0; i < cachep->num; i++) {
  2092. /* constructor could break poison info */
  2093. if (DEBUG == 0 && cachep->ctor) {
  2094. objp = index_to_obj(cachep, page, i);
  2095. kasan_unpoison_object_data(cachep, objp);
  2096. cachep->ctor(objp);
  2097. kasan_poison_object_data(cachep, objp);
  2098. }
  2099. set_free_obj(page, i, i);
  2100. }
  2101. }
  2102. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2103. {
  2104. if (CONFIG_ZONE_DMA_FLAG) {
  2105. if (flags & GFP_DMA)
  2106. BUG_ON(!(cachep->allocflags & GFP_DMA));
  2107. else
  2108. BUG_ON(cachep->allocflags & GFP_DMA);
  2109. }
  2110. }
  2111. static void *slab_get_obj(struct kmem_cache *cachep, struct page *page)
  2112. {
  2113. void *objp;
  2114. objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
  2115. page->active++;
  2116. #if DEBUG
  2117. if (cachep->flags & SLAB_STORE_USER)
  2118. set_store_user_dirty(cachep);
  2119. #endif
  2120. return objp;
  2121. }
  2122. static void slab_put_obj(struct kmem_cache *cachep,
  2123. struct page *page, void *objp)
  2124. {
  2125. unsigned int objnr = obj_to_index(cachep, page, objp);
  2126. #if DEBUG
  2127. unsigned int i;
  2128. /* Verify double free bug */
  2129. for (i = page->active; i < cachep->num; i++) {
  2130. if (get_free_obj(page, i) == objnr) {
  2131. pr_err("slab: double free detected in cache '%s', objp %p\n",
  2132. cachep->name, objp);
  2133. BUG();
  2134. }
  2135. }
  2136. #endif
  2137. page->active--;
  2138. if (!page->freelist)
  2139. page->freelist = objp + obj_offset(cachep);
  2140. set_free_obj(page, page->active, objnr);
  2141. }
  2142. /*
  2143. * Map pages beginning at addr to the given cache and slab. This is required
  2144. * for the slab allocator to be able to lookup the cache and slab of a
  2145. * virtual address for kfree, ksize, and slab debugging.
  2146. */
  2147. static void slab_map_pages(struct kmem_cache *cache, struct page *page,
  2148. void *freelist)
  2149. {
  2150. page->slab_cache = cache;
  2151. page->freelist = freelist;
  2152. }
  2153. /*
  2154. * Grow (by 1) the number of slabs within a cache. This is called by
  2155. * kmem_cache_alloc() when there are no active objs left in a cache.
  2156. */
  2157. static int cache_grow(struct kmem_cache *cachep,
  2158. gfp_t flags, int nodeid, struct page *page)
  2159. {
  2160. void *freelist;
  2161. size_t offset;
  2162. gfp_t local_flags;
  2163. struct kmem_cache_node *n;
  2164. /*
  2165. * Be lazy and only check for valid flags here, keeping it out of the
  2166. * critical path in kmem_cache_alloc().
  2167. */
  2168. if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
  2169. pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
  2170. BUG();
  2171. }
  2172. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2173. /* Take the node list lock to change the colour_next on this node */
  2174. check_irq_off();
  2175. n = get_node(cachep, nodeid);
  2176. spin_lock(&n->list_lock);
  2177. /* Get colour for the slab, and cal the next value. */
  2178. offset = n->colour_next;
  2179. n->colour_next++;
  2180. if (n->colour_next >= cachep->colour)
  2181. n->colour_next = 0;
  2182. spin_unlock(&n->list_lock);
  2183. offset *= cachep->colour_off;
  2184. if (gfpflags_allow_blocking(local_flags))
  2185. local_irq_enable();
  2186. /*
  2187. * The test for missing atomic flag is performed here, rather than
  2188. * the more obvious place, simply to reduce the critical path length
  2189. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2190. * will eventually be caught here (where it matters).
  2191. */
  2192. kmem_flagcheck(cachep, flags);
  2193. /*
  2194. * Get mem for the objs. Attempt to allocate a physical page from
  2195. * 'nodeid'.
  2196. */
  2197. if (!page)
  2198. page = kmem_getpages(cachep, local_flags, nodeid);
  2199. if (!page)
  2200. goto failed;
  2201. /* Get slab management. */
  2202. freelist = alloc_slabmgmt(cachep, page, offset,
  2203. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  2204. if (OFF_SLAB(cachep) && !freelist)
  2205. goto opps1;
  2206. slab_map_pages(cachep, page, freelist);
  2207. kasan_poison_slab(page);
  2208. cache_init_objs(cachep, page);
  2209. if (gfpflags_allow_blocking(local_flags))
  2210. local_irq_disable();
  2211. check_irq_off();
  2212. spin_lock(&n->list_lock);
  2213. /* Make slab active. */
  2214. list_add_tail(&page->lru, &(n->slabs_free));
  2215. STATS_INC_GROWN(cachep);
  2216. n->free_objects += cachep->num;
  2217. spin_unlock(&n->list_lock);
  2218. return 1;
  2219. opps1:
  2220. kmem_freepages(cachep, page);
  2221. failed:
  2222. if (gfpflags_allow_blocking(local_flags))
  2223. local_irq_disable();
  2224. return 0;
  2225. }
  2226. #if DEBUG
  2227. /*
  2228. * Perform extra freeing checks:
  2229. * - detect bad pointers.
  2230. * - POISON/RED_ZONE checking
  2231. */
  2232. static void kfree_debugcheck(const void *objp)
  2233. {
  2234. if (!virt_addr_valid(objp)) {
  2235. pr_err("kfree_debugcheck: out of range ptr %lxh\n",
  2236. (unsigned long)objp);
  2237. BUG();
  2238. }
  2239. }
  2240. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2241. {
  2242. unsigned long long redzone1, redzone2;
  2243. redzone1 = *dbg_redzone1(cache, obj);
  2244. redzone2 = *dbg_redzone2(cache, obj);
  2245. /*
  2246. * Redzone is ok.
  2247. */
  2248. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2249. return;
  2250. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2251. slab_error(cache, "double free detected");
  2252. else
  2253. slab_error(cache, "memory outside object was overwritten");
  2254. pr_err("%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2255. obj, redzone1, redzone2);
  2256. }
  2257. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2258. unsigned long caller)
  2259. {
  2260. unsigned int objnr;
  2261. struct page *page;
  2262. BUG_ON(virt_to_cache(objp) != cachep);
  2263. objp -= obj_offset(cachep);
  2264. kfree_debugcheck(objp);
  2265. page = virt_to_head_page(objp);
  2266. if (cachep->flags & SLAB_RED_ZONE) {
  2267. verify_redzone_free(cachep, objp);
  2268. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2269. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2270. }
  2271. if (cachep->flags & SLAB_STORE_USER) {
  2272. set_store_user_dirty(cachep);
  2273. *dbg_userword(cachep, objp) = (void *)caller;
  2274. }
  2275. objnr = obj_to_index(cachep, page, objp);
  2276. BUG_ON(objnr >= cachep->num);
  2277. BUG_ON(objp != index_to_obj(cachep, page, objnr));
  2278. if (cachep->flags & SLAB_POISON) {
  2279. poison_obj(cachep, objp, POISON_FREE);
  2280. slab_kernel_map(cachep, objp, 0, caller);
  2281. }
  2282. return objp;
  2283. }
  2284. #else
  2285. #define kfree_debugcheck(x) do { } while(0)
  2286. #define cache_free_debugcheck(x,objp,z) (objp)
  2287. #endif
  2288. static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
  2289. void **list)
  2290. {
  2291. #if DEBUG
  2292. void *next = *list;
  2293. void *objp;
  2294. while (next) {
  2295. objp = next - obj_offset(cachep);
  2296. next = *(void **)next;
  2297. poison_obj(cachep, objp, POISON_FREE);
  2298. }
  2299. #endif
  2300. }
  2301. static inline void fixup_slab_list(struct kmem_cache *cachep,
  2302. struct kmem_cache_node *n, struct page *page,
  2303. void **list)
  2304. {
  2305. /* move slabp to correct slabp list: */
  2306. list_del(&page->lru);
  2307. if (page->active == cachep->num) {
  2308. list_add(&page->lru, &n->slabs_full);
  2309. if (OBJFREELIST_SLAB(cachep)) {
  2310. #if DEBUG
  2311. /* Poisoning will be done without holding the lock */
  2312. if (cachep->flags & SLAB_POISON) {
  2313. void **objp = page->freelist;
  2314. *objp = *list;
  2315. *list = objp;
  2316. }
  2317. #endif
  2318. page->freelist = NULL;
  2319. }
  2320. } else
  2321. list_add(&page->lru, &n->slabs_partial);
  2322. }
  2323. /* Try to find non-pfmemalloc slab if needed */
  2324. static noinline struct page *get_valid_first_slab(struct kmem_cache_node *n,
  2325. struct page *page, bool pfmemalloc)
  2326. {
  2327. if (!page)
  2328. return NULL;
  2329. if (pfmemalloc)
  2330. return page;
  2331. if (!PageSlabPfmemalloc(page))
  2332. return page;
  2333. /* No need to keep pfmemalloc slab if we have enough free objects */
  2334. if (n->free_objects > n->free_limit) {
  2335. ClearPageSlabPfmemalloc(page);
  2336. return page;
  2337. }
  2338. /* Move pfmemalloc slab to the end of list to speed up next search */
  2339. list_del(&page->lru);
  2340. if (!page->active)
  2341. list_add_tail(&page->lru, &n->slabs_free);
  2342. else
  2343. list_add_tail(&page->lru, &n->slabs_partial);
  2344. list_for_each_entry(page, &n->slabs_partial, lru) {
  2345. if (!PageSlabPfmemalloc(page))
  2346. return page;
  2347. }
  2348. list_for_each_entry(page, &n->slabs_free, lru) {
  2349. if (!PageSlabPfmemalloc(page))
  2350. return page;
  2351. }
  2352. return NULL;
  2353. }
  2354. static struct page *get_first_slab(struct kmem_cache_node *n, bool pfmemalloc)
  2355. {
  2356. struct page *page;
  2357. page = list_first_entry_or_null(&n->slabs_partial,
  2358. struct page, lru);
  2359. if (!page) {
  2360. n->free_touched = 1;
  2361. page = list_first_entry_or_null(&n->slabs_free,
  2362. struct page, lru);
  2363. }
  2364. if (sk_memalloc_socks())
  2365. return get_valid_first_slab(n, page, pfmemalloc);
  2366. return page;
  2367. }
  2368. static noinline void *cache_alloc_pfmemalloc(struct kmem_cache *cachep,
  2369. struct kmem_cache_node *n, gfp_t flags)
  2370. {
  2371. struct page *page;
  2372. void *obj;
  2373. void *list = NULL;
  2374. if (!gfp_pfmemalloc_allowed(flags))
  2375. return NULL;
  2376. spin_lock(&n->list_lock);
  2377. page = get_first_slab(n, true);
  2378. if (!page) {
  2379. spin_unlock(&n->list_lock);
  2380. return NULL;
  2381. }
  2382. obj = slab_get_obj(cachep, page);
  2383. n->free_objects--;
  2384. fixup_slab_list(cachep, n, page, &list);
  2385. spin_unlock(&n->list_lock);
  2386. fixup_objfreelist_debug(cachep, &list);
  2387. return obj;
  2388. }
  2389. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2390. {
  2391. int batchcount;
  2392. struct kmem_cache_node *n;
  2393. struct array_cache *ac;
  2394. int node;
  2395. void *list = NULL;
  2396. check_irq_off();
  2397. node = numa_mem_id();
  2398. retry:
  2399. ac = cpu_cache_get(cachep);
  2400. batchcount = ac->batchcount;
  2401. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2402. /*
  2403. * If there was little recent activity on this cache, then
  2404. * perform only a partial refill. Otherwise we could generate
  2405. * refill bouncing.
  2406. */
  2407. batchcount = BATCHREFILL_LIMIT;
  2408. }
  2409. n = get_node(cachep, node);
  2410. BUG_ON(ac->avail > 0 || !n);
  2411. spin_lock(&n->list_lock);
  2412. /* See if we can refill from the shared array */
  2413. if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
  2414. n->shared->touched = 1;
  2415. goto alloc_done;
  2416. }
  2417. while (batchcount > 0) {
  2418. struct page *page;
  2419. /* Get slab alloc is to come from. */
  2420. page = get_first_slab(n, false);
  2421. if (!page)
  2422. goto must_grow;
  2423. check_spinlock_acquired(cachep);
  2424. /*
  2425. * The slab was either on partial or free list so
  2426. * there must be at least one object available for
  2427. * allocation.
  2428. */
  2429. BUG_ON(page->active >= cachep->num);
  2430. while (page->active < cachep->num && batchcount--) {
  2431. STATS_INC_ALLOCED(cachep);
  2432. STATS_INC_ACTIVE(cachep);
  2433. STATS_SET_HIGH(cachep);
  2434. ac->entry[ac->avail++] = slab_get_obj(cachep, page);
  2435. }
  2436. fixup_slab_list(cachep, n, page, &list);
  2437. }
  2438. must_grow:
  2439. n->free_objects -= ac->avail;
  2440. alloc_done:
  2441. spin_unlock(&n->list_lock);
  2442. fixup_objfreelist_debug(cachep, &list);
  2443. if (unlikely(!ac->avail)) {
  2444. int x;
  2445. /* Check if we can use obj in pfmemalloc slab */
  2446. if (sk_memalloc_socks()) {
  2447. void *obj = cache_alloc_pfmemalloc(cachep, n, flags);
  2448. if (obj)
  2449. return obj;
  2450. }
  2451. x = cache_grow(cachep, gfp_exact_node(flags), node, NULL);
  2452. /* cache_grow can reenable interrupts, then ac could change. */
  2453. ac = cpu_cache_get(cachep);
  2454. node = numa_mem_id();
  2455. /* no objects in sight? abort */
  2456. if (!x && ac->avail == 0)
  2457. return NULL;
  2458. if (!ac->avail) /* objects refilled by interrupt? */
  2459. goto retry;
  2460. }
  2461. ac->touched = 1;
  2462. return ac->entry[--ac->avail];
  2463. }
  2464. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2465. gfp_t flags)
  2466. {
  2467. might_sleep_if(gfpflags_allow_blocking(flags));
  2468. #if DEBUG
  2469. kmem_flagcheck(cachep, flags);
  2470. #endif
  2471. }
  2472. #if DEBUG
  2473. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2474. gfp_t flags, void *objp, unsigned long caller)
  2475. {
  2476. if (!objp)
  2477. return objp;
  2478. if (cachep->flags & SLAB_POISON) {
  2479. check_poison_obj(cachep, objp);
  2480. slab_kernel_map(cachep, objp, 1, 0);
  2481. poison_obj(cachep, objp, POISON_INUSE);
  2482. }
  2483. if (cachep->flags & SLAB_STORE_USER)
  2484. *dbg_userword(cachep, objp) = (void *)caller;
  2485. if (cachep->flags & SLAB_RED_ZONE) {
  2486. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2487. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2488. slab_error(cachep, "double free, or memory outside object was overwritten");
  2489. pr_err("%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2490. objp, *dbg_redzone1(cachep, objp),
  2491. *dbg_redzone2(cachep, objp));
  2492. }
  2493. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2494. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2495. }
  2496. objp += obj_offset(cachep);
  2497. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2498. cachep->ctor(objp);
  2499. if (ARCH_SLAB_MINALIGN &&
  2500. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2501. pr_err("0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2502. objp, (int)ARCH_SLAB_MINALIGN);
  2503. }
  2504. return objp;
  2505. }
  2506. #else
  2507. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2508. #endif
  2509. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2510. {
  2511. void *objp;
  2512. struct array_cache *ac;
  2513. check_irq_off();
  2514. ac = cpu_cache_get(cachep);
  2515. if (likely(ac->avail)) {
  2516. ac->touched = 1;
  2517. objp = ac->entry[--ac->avail];
  2518. STATS_INC_ALLOCHIT(cachep);
  2519. goto out;
  2520. }
  2521. STATS_INC_ALLOCMISS(cachep);
  2522. objp = cache_alloc_refill(cachep, flags);
  2523. /*
  2524. * the 'ac' may be updated by cache_alloc_refill(),
  2525. * and kmemleak_erase() requires its correct value.
  2526. */
  2527. ac = cpu_cache_get(cachep);
  2528. out:
  2529. /*
  2530. * To avoid a false negative, if an object that is in one of the
  2531. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  2532. * treat the array pointers as a reference to the object.
  2533. */
  2534. if (objp)
  2535. kmemleak_erase(&ac->entry[ac->avail]);
  2536. return objp;
  2537. }
  2538. #ifdef CONFIG_NUMA
  2539. /*
  2540. * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set.
  2541. *
  2542. * If we are in_interrupt, then process context, including cpusets and
  2543. * mempolicy, may not apply and should not be used for allocation policy.
  2544. */
  2545. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2546. {
  2547. int nid_alloc, nid_here;
  2548. if (in_interrupt() || (flags & __GFP_THISNODE))
  2549. return NULL;
  2550. nid_alloc = nid_here = numa_mem_id();
  2551. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2552. nid_alloc = cpuset_slab_spread_node();
  2553. else if (current->mempolicy)
  2554. nid_alloc = mempolicy_slab_node();
  2555. if (nid_alloc != nid_here)
  2556. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2557. return NULL;
  2558. }
  2559. /*
  2560. * Fallback function if there was no memory available and no objects on a
  2561. * certain node and fall back is permitted. First we scan all the
  2562. * available node for available objects. If that fails then we
  2563. * perform an allocation without specifying a node. This allows the page
  2564. * allocator to do its reclaim / fallback magic. We then insert the
  2565. * slab into the proper nodelist and then allocate from it.
  2566. */
  2567. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2568. {
  2569. struct zonelist *zonelist;
  2570. gfp_t local_flags;
  2571. struct zoneref *z;
  2572. struct zone *zone;
  2573. enum zone_type high_zoneidx = gfp_zone(flags);
  2574. void *obj = NULL;
  2575. int nid;
  2576. unsigned int cpuset_mems_cookie;
  2577. if (flags & __GFP_THISNODE)
  2578. return NULL;
  2579. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2580. retry_cpuset:
  2581. cpuset_mems_cookie = read_mems_allowed_begin();
  2582. zonelist = node_zonelist(mempolicy_slab_node(), flags);
  2583. retry:
  2584. /*
  2585. * Look through allowed nodes for objects available
  2586. * from existing per node queues.
  2587. */
  2588. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  2589. nid = zone_to_nid(zone);
  2590. if (cpuset_zone_allowed(zone, flags) &&
  2591. get_node(cache, nid) &&
  2592. get_node(cache, nid)->free_objects) {
  2593. obj = ____cache_alloc_node(cache,
  2594. gfp_exact_node(flags), nid);
  2595. if (obj)
  2596. break;
  2597. }
  2598. }
  2599. if (!obj) {
  2600. /*
  2601. * This allocation will be performed within the constraints
  2602. * of the current cpuset / memory policy requirements.
  2603. * We may trigger various forms of reclaim on the allowed
  2604. * set and go into memory reserves if necessary.
  2605. */
  2606. struct page *page;
  2607. if (gfpflags_allow_blocking(local_flags))
  2608. local_irq_enable();
  2609. kmem_flagcheck(cache, flags);
  2610. page = kmem_getpages(cache, local_flags, numa_mem_id());
  2611. if (gfpflags_allow_blocking(local_flags))
  2612. local_irq_disable();
  2613. if (page) {
  2614. /*
  2615. * Insert into the appropriate per node queues
  2616. */
  2617. nid = page_to_nid(page);
  2618. if (cache_grow(cache, flags, nid, page)) {
  2619. obj = ____cache_alloc_node(cache,
  2620. gfp_exact_node(flags), nid);
  2621. if (!obj)
  2622. /*
  2623. * Another processor may allocate the
  2624. * objects in the slab since we are
  2625. * not holding any locks.
  2626. */
  2627. goto retry;
  2628. } else {
  2629. /* cache_grow already freed obj */
  2630. obj = NULL;
  2631. }
  2632. }
  2633. }
  2634. if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
  2635. goto retry_cpuset;
  2636. return obj;
  2637. }
  2638. /*
  2639. * A interface to enable slab creation on nodeid
  2640. */
  2641. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2642. int nodeid)
  2643. {
  2644. struct page *page;
  2645. struct kmem_cache_node *n;
  2646. void *obj;
  2647. void *list = NULL;
  2648. int x;
  2649. VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES);
  2650. n = get_node(cachep, nodeid);
  2651. BUG_ON(!n);
  2652. retry:
  2653. check_irq_off();
  2654. spin_lock(&n->list_lock);
  2655. page = get_first_slab(n, false);
  2656. if (!page)
  2657. goto must_grow;
  2658. check_spinlock_acquired_node(cachep, nodeid);
  2659. STATS_INC_NODEALLOCS(cachep);
  2660. STATS_INC_ACTIVE(cachep);
  2661. STATS_SET_HIGH(cachep);
  2662. BUG_ON(page->active == cachep->num);
  2663. obj = slab_get_obj(cachep, page);
  2664. n->free_objects--;
  2665. fixup_slab_list(cachep, n, page, &list);
  2666. spin_unlock(&n->list_lock);
  2667. fixup_objfreelist_debug(cachep, &list);
  2668. goto done;
  2669. must_grow:
  2670. spin_unlock(&n->list_lock);
  2671. x = cache_grow(cachep, gfp_exact_node(flags), nodeid, NULL);
  2672. if (x)
  2673. goto retry;
  2674. return fallback_alloc(cachep, flags);
  2675. done:
  2676. return obj;
  2677. }
  2678. static __always_inline void *
  2679. slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  2680. unsigned long caller)
  2681. {
  2682. unsigned long save_flags;
  2683. void *ptr;
  2684. int slab_node = numa_mem_id();
  2685. flags &= gfp_allowed_mask;
  2686. cachep = slab_pre_alloc_hook(cachep, flags);
  2687. if (unlikely(!cachep))
  2688. return NULL;
  2689. cache_alloc_debugcheck_before(cachep, flags);
  2690. local_irq_save(save_flags);
  2691. if (nodeid == NUMA_NO_NODE)
  2692. nodeid = slab_node;
  2693. if (unlikely(!get_node(cachep, nodeid))) {
  2694. /* Node not bootstrapped yet */
  2695. ptr = fallback_alloc(cachep, flags);
  2696. goto out;
  2697. }
  2698. if (nodeid == slab_node) {
  2699. /*
  2700. * Use the locally cached objects if possible.
  2701. * However ____cache_alloc does not allow fallback
  2702. * to other nodes. It may fail while we still have
  2703. * objects on other nodes available.
  2704. */
  2705. ptr = ____cache_alloc(cachep, flags);
  2706. if (ptr)
  2707. goto out;
  2708. }
  2709. /* ___cache_alloc_node can fall back to other nodes */
  2710. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  2711. out:
  2712. local_irq_restore(save_flags);
  2713. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  2714. if (unlikely(flags & __GFP_ZERO) && ptr)
  2715. memset(ptr, 0, cachep->object_size);
  2716. slab_post_alloc_hook(cachep, flags, 1, &ptr);
  2717. return ptr;
  2718. }
  2719. static __always_inline void *
  2720. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  2721. {
  2722. void *objp;
  2723. if (current->mempolicy || cpuset_do_slab_mem_spread()) {
  2724. objp = alternate_node_alloc(cache, flags);
  2725. if (objp)
  2726. goto out;
  2727. }
  2728. objp = ____cache_alloc(cache, flags);
  2729. /*
  2730. * We may just have run out of memory on the local node.
  2731. * ____cache_alloc_node() knows how to locate memory on other nodes
  2732. */
  2733. if (!objp)
  2734. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  2735. out:
  2736. return objp;
  2737. }
  2738. #else
  2739. static __always_inline void *
  2740. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2741. {
  2742. return ____cache_alloc(cachep, flags);
  2743. }
  2744. #endif /* CONFIG_NUMA */
  2745. static __always_inline void *
  2746. slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
  2747. {
  2748. unsigned long save_flags;
  2749. void *objp;
  2750. flags &= gfp_allowed_mask;
  2751. cachep = slab_pre_alloc_hook(cachep, flags);
  2752. if (unlikely(!cachep))
  2753. return NULL;
  2754. cache_alloc_debugcheck_before(cachep, flags);
  2755. local_irq_save(save_flags);
  2756. objp = __do_cache_alloc(cachep, flags);
  2757. local_irq_restore(save_flags);
  2758. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  2759. prefetchw(objp);
  2760. if (unlikely(flags & __GFP_ZERO) && objp)
  2761. memset(objp, 0, cachep->object_size);
  2762. slab_post_alloc_hook(cachep, flags, 1, &objp);
  2763. return objp;
  2764. }
  2765. /*
  2766. * Caller needs to acquire correct kmem_cache_node's list_lock
  2767. * @list: List of detached free slabs should be freed by caller
  2768. */
  2769. static void free_block(struct kmem_cache *cachep, void **objpp,
  2770. int nr_objects, int node, struct list_head *list)
  2771. {
  2772. int i;
  2773. struct kmem_cache_node *n = get_node(cachep, node);
  2774. for (i = 0; i < nr_objects; i++) {
  2775. void *objp;
  2776. struct page *page;
  2777. objp = objpp[i];
  2778. page = virt_to_head_page(objp);
  2779. list_del(&page->lru);
  2780. check_spinlock_acquired_node(cachep, node);
  2781. slab_put_obj(cachep, page, objp);
  2782. STATS_DEC_ACTIVE(cachep);
  2783. n->free_objects++;
  2784. /* fixup slab chains */
  2785. if (page->active == 0) {
  2786. if (n->free_objects > n->free_limit) {
  2787. n->free_objects -= cachep->num;
  2788. list_add_tail(&page->lru, list);
  2789. } else {
  2790. list_add(&page->lru, &n->slabs_free);
  2791. }
  2792. } else {
  2793. /* Unconditionally move a slab to the end of the
  2794. * partial list on free - maximum time for the
  2795. * other objects to be freed, too.
  2796. */
  2797. list_add_tail(&page->lru, &n->slabs_partial);
  2798. }
  2799. }
  2800. }
  2801. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  2802. {
  2803. int batchcount;
  2804. struct kmem_cache_node *n;
  2805. int node = numa_mem_id();
  2806. LIST_HEAD(list);
  2807. batchcount = ac->batchcount;
  2808. check_irq_off();
  2809. n = get_node(cachep, node);
  2810. spin_lock(&n->list_lock);
  2811. if (n->shared) {
  2812. struct array_cache *shared_array = n->shared;
  2813. int max = shared_array->limit - shared_array->avail;
  2814. if (max) {
  2815. if (batchcount > max)
  2816. batchcount = max;
  2817. memcpy(&(shared_array->entry[shared_array->avail]),
  2818. ac->entry, sizeof(void *) * batchcount);
  2819. shared_array->avail += batchcount;
  2820. goto free_done;
  2821. }
  2822. }
  2823. free_block(cachep, ac->entry, batchcount, node, &list);
  2824. free_done:
  2825. #if STATS
  2826. {
  2827. int i = 0;
  2828. struct page *page;
  2829. list_for_each_entry(page, &n->slabs_free, lru) {
  2830. BUG_ON(page->active);
  2831. i++;
  2832. }
  2833. STATS_SET_FREEABLE(cachep, i);
  2834. }
  2835. #endif
  2836. spin_unlock(&n->list_lock);
  2837. slabs_destroy(cachep, &list);
  2838. ac->avail -= batchcount;
  2839. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  2840. }
  2841. /*
  2842. * Release an obj back to its cache. If the obj has a constructed state, it must
  2843. * be in this state _before_ it is released. Called with disabled ints.
  2844. */
  2845. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  2846. unsigned long caller)
  2847. {
  2848. struct array_cache *ac = cpu_cache_get(cachep);
  2849. kasan_slab_free(cachep, objp);
  2850. check_irq_off();
  2851. kmemleak_free_recursive(objp, cachep->flags);
  2852. objp = cache_free_debugcheck(cachep, objp, caller);
  2853. kmemcheck_slab_free(cachep, objp, cachep->object_size);
  2854. /*
  2855. * Skip calling cache_free_alien() when the platform is not numa.
  2856. * This will avoid cache misses that happen while accessing slabp (which
  2857. * is per page memory reference) to get nodeid. Instead use a global
  2858. * variable to skip the call, which is mostly likely to be present in
  2859. * the cache.
  2860. */
  2861. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  2862. return;
  2863. if (ac->avail < ac->limit) {
  2864. STATS_INC_FREEHIT(cachep);
  2865. } else {
  2866. STATS_INC_FREEMISS(cachep);
  2867. cache_flusharray(cachep, ac);
  2868. }
  2869. if (sk_memalloc_socks()) {
  2870. struct page *page = virt_to_head_page(objp);
  2871. if (unlikely(PageSlabPfmemalloc(page))) {
  2872. cache_free_pfmemalloc(cachep, page, objp);
  2873. return;
  2874. }
  2875. }
  2876. ac->entry[ac->avail++] = objp;
  2877. }
  2878. /**
  2879. * kmem_cache_alloc - Allocate an object
  2880. * @cachep: The cache to allocate from.
  2881. * @flags: See kmalloc().
  2882. *
  2883. * Allocate an object from this cache. The flags are only relevant
  2884. * if the cache has no available objects.
  2885. */
  2886. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2887. {
  2888. void *ret = slab_alloc(cachep, flags, _RET_IP_);
  2889. kasan_slab_alloc(cachep, ret, flags);
  2890. trace_kmem_cache_alloc(_RET_IP_, ret,
  2891. cachep->object_size, cachep->size, flags);
  2892. return ret;
  2893. }
  2894. EXPORT_SYMBOL(kmem_cache_alloc);
  2895. static __always_inline void
  2896. cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
  2897. size_t size, void **p, unsigned long caller)
  2898. {
  2899. size_t i;
  2900. for (i = 0; i < size; i++)
  2901. p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
  2902. }
  2903. int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
  2904. void **p)
  2905. {
  2906. size_t i;
  2907. s = slab_pre_alloc_hook(s, flags);
  2908. if (!s)
  2909. return 0;
  2910. cache_alloc_debugcheck_before(s, flags);
  2911. local_irq_disable();
  2912. for (i = 0; i < size; i++) {
  2913. void *objp = __do_cache_alloc(s, flags);
  2914. if (unlikely(!objp))
  2915. goto error;
  2916. p[i] = objp;
  2917. }
  2918. local_irq_enable();
  2919. cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_);
  2920. /* Clear memory outside IRQ disabled section */
  2921. if (unlikely(flags & __GFP_ZERO))
  2922. for (i = 0; i < size; i++)
  2923. memset(p[i], 0, s->object_size);
  2924. slab_post_alloc_hook(s, flags, size, p);
  2925. /* FIXME: Trace call missing. Christoph would like a bulk variant */
  2926. return size;
  2927. error:
  2928. local_irq_enable();
  2929. cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
  2930. slab_post_alloc_hook(s, flags, i, p);
  2931. __kmem_cache_free_bulk(s, i, p);
  2932. return 0;
  2933. }
  2934. EXPORT_SYMBOL(kmem_cache_alloc_bulk);
  2935. #ifdef CONFIG_TRACING
  2936. void *
  2937. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  2938. {
  2939. void *ret;
  2940. ret = slab_alloc(cachep, flags, _RET_IP_);
  2941. kasan_kmalloc(cachep, ret, size, flags);
  2942. trace_kmalloc(_RET_IP_, ret,
  2943. size, cachep->size, flags);
  2944. return ret;
  2945. }
  2946. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  2947. #endif
  2948. #ifdef CONFIG_NUMA
  2949. /**
  2950. * kmem_cache_alloc_node - Allocate an object on the specified node
  2951. * @cachep: The cache to allocate from.
  2952. * @flags: See kmalloc().
  2953. * @nodeid: node number of the target node.
  2954. *
  2955. * Identical to kmem_cache_alloc but it will allocate memory on the given
  2956. * node, which can improve the performance for cpu bound structures.
  2957. *
  2958. * Fallback to other node is possible if __GFP_THISNODE is not set.
  2959. */
  2960. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  2961. {
  2962. void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  2963. kasan_slab_alloc(cachep, ret, flags);
  2964. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  2965. cachep->object_size, cachep->size,
  2966. flags, nodeid);
  2967. return ret;
  2968. }
  2969. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2970. #ifdef CONFIG_TRACING
  2971. void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  2972. gfp_t flags,
  2973. int nodeid,
  2974. size_t size)
  2975. {
  2976. void *ret;
  2977. ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  2978. kasan_kmalloc(cachep, ret, size, flags);
  2979. trace_kmalloc_node(_RET_IP_, ret,
  2980. size, cachep->size,
  2981. flags, nodeid);
  2982. return ret;
  2983. }
  2984. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  2985. #endif
  2986. static __always_inline void *
  2987. __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
  2988. {
  2989. struct kmem_cache *cachep;
  2990. void *ret;
  2991. cachep = kmalloc_slab(size, flags);
  2992. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  2993. return cachep;
  2994. ret = kmem_cache_alloc_node_trace(cachep, flags, node, size);
  2995. kasan_kmalloc(cachep, ret, size, flags);
  2996. return ret;
  2997. }
  2998. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2999. {
  3000. return __do_kmalloc_node(size, flags, node, _RET_IP_);
  3001. }
  3002. EXPORT_SYMBOL(__kmalloc_node);
  3003. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3004. int node, unsigned long caller)
  3005. {
  3006. return __do_kmalloc_node(size, flags, node, caller);
  3007. }
  3008. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3009. #endif /* CONFIG_NUMA */
  3010. /**
  3011. * __do_kmalloc - allocate memory
  3012. * @size: how many bytes of memory are required.
  3013. * @flags: the type of memory to allocate (see kmalloc).
  3014. * @caller: function caller for debug tracking of the caller
  3015. */
  3016. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3017. unsigned long caller)
  3018. {
  3019. struct kmem_cache *cachep;
  3020. void *ret;
  3021. cachep = kmalloc_slab(size, flags);
  3022. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3023. return cachep;
  3024. ret = slab_alloc(cachep, flags, caller);
  3025. kasan_kmalloc(cachep, ret, size, flags);
  3026. trace_kmalloc(caller, ret,
  3027. size, cachep->size, flags);
  3028. return ret;
  3029. }
  3030. void *__kmalloc(size_t size, gfp_t flags)
  3031. {
  3032. return __do_kmalloc(size, flags, _RET_IP_);
  3033. }
  3034. EXPORT_SYMBOL(__kmalloc);
  3035. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  3036. {
  3037. return __do_kmalloc(size, flags, caller);
  3038. }
  3039. EXPORT_SYMBOL(__kmalloc_track_caller);
  3040. /**
  3041. * kmem_cache_free - Deallocate an object
  3042. * @cachep: The cache the allocation was from.
  3043. * @objp: The previously allocated object.
  3044. *
  3045. * Free an object which was previously allocated from this
  3046. * cache.
  3047. */
  3048. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3049. {
  3050. unsigned long flags;
  3051. cachep = cache_from_obj(cachep, objp);
  3052. if (!cachep)
  3053. return;
  3054. local_irq_save(flags);
  3055. debug_check_no_locks_freed(objp, cachep->object_size);
  3056. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  3057. debug_check_no_obj_freed(objp, cachep->object_size);
  3058. __cache_free(cachep, objp, _RET_IP_);
  3059. local_irq_restore(flags);
  3060. trace_kmem_cache_free(_RET_IP_, objp);
  3061. }
  3062. EXPORT_SYMBOL(kmem_cache_free);
  3063. void kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p)
  3064. {
  3065. struct kmem_cache *s;
  3066. size_t i;
  3067. local_irq_disable();
  3068. for (i = 0; i < size; i++) {
  3069. void *objp = p[i];
  3070. if (!orig_s) /* called via kfree_bulk */
  3071. s = virt_to_cache(objp);
  3072. else
  3073. s = cache_from_obj(orig_s, objp);
  3074. debug_check_no_locks_freed(objp, s->object_size);
  3075. if (!(s->flags & SLAB_DEBUG_OBJECTS))
  3076. debug_check_no_obj_freed(objp, s->object_size);
  3077. __cache_free(s, objp, _RET_IP_);
  3078. }
  3079. local_irq_enable();
  3080. /* FIXME: add tracing */
  3081. }
  3082. EXPORT_SYMBOL(kmem_cache_free_bulk);
  3083. /**
  3084. * kfree - free previously allocated memory
  3085. * @objp: pointer returned by kmalloc.
  3086. *
  3087. * If @objp is NULL, no operation is performed.
  3088. *
  3089. * Don't free memory not originally allocated by kmalloc()
  3090. * or you will run into trouble.
  3091. */
  3092. void kfree(const void *objp)
  3093. {
  3094. struct kmem_cache *c;
  3095. unsigned long flags;
  3096. trace_kfree(_RET_IP_, objp);
  3097. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  3098. return;
  3099. local_irq_save(flags);
  3100. kfree_debugcheck(objp);
  3101. c = virt_to_cache(objp);
  3102. debug_check_no_locks_freed(objp, c->object_size);
  3103. debug_check_no_obj_freed(objp, c->object_size);
  3104. __cache_free(c, (void *)objp, _RET_IP_);
  3105. local_irq_restore(flags);
  3106. }
  3107. EXPORT_SYMBOL(kfree);
  3108. /*
  3109. * This initializes kmem_cache_node or resizes various caches for all nodes.
  3110. */
  3111. static int alloc_kmem_cache_node(struct kmem_cache *cachep, gfp_t gfp)
  3112. {
  3113. int node;
  3114. struct kmem_cache_node *n;
  3115. struct array_cache *new_shared;
  3116. struct alien_cache **new_alien = NULL;
  3117. for_each_online_node(node) {
  3118. if (use_alien_caches) {
  3119. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  3120. if (!new_alien)
  3121. goto fail;
  3122. }
  3123. new_shared = NULL;
  3124. if (cachep->shared) {
  3125. new_shared = alloc_arraycache(node,
  3126. cachep->shared*cachep->batchcount,
  3127. 0xbaadf00d, gfp);
  3128. if (!new_shared) {
  3129. free_alien_cache(new_alien);
  3130. goto fail;
  3131. }
  3132. }
  3133. n = get_node(cachep, node);
  3134. if (n) {
  3135. struct array_cache *shared = n->shared;
  3136. LIST_HEAD(list);
  3137. spin_lock_irq(&n->list_lock);
  3138. if (shared)
  3139. free_block(cachep, shared->entry,
  3140. shared->avail, node, &list);
  3141. n->shared = new_shared;
  3142. if (!n->alien) {
  3143. n->alien = new_alien;
  3144. new_alien = NULL;
  3145. }
  3146. n->free_limit = (1 + nr_cpus_node(node)) *
  3147. cachep->batchcount + cachep->num;
  3148. spin_unlock_irq(&n->list_lock);
  3149. slabs_destroy(cachep, &list);
  3150. kfree(shared);
  3151. free_alien_cache(new_alien);
  3152. continue;
  3153. }
  3154. n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
  3155. if (!n) {
  3156. free_alien_cache(new_alien);
  3157. kfree(new_shared);
  3158. goto fail;
  3159. }
  3160. kmem_cache_node_init(n);
  3161. n->next_reap = jiffies + REAPTIMEOUT_NODE +
  3162. ((unsigned long)cachep) % REAPTIMEOUT_NODE;
  3163. n->shared = new_shared;
  3164. n->alien = new_alien;
  3165. n->free_limit = (1 + nr_cpus_node(node)) *
  3166. cachep->batchcount + cachep->num;
  3167. cachep->node[node] = n;
  3168. }
  3169. return 0;
  3170. fail:
  3171. if (!cachep->list.next) {
  3172. /* Cache is not active yet. Roll back what we did */
  3173. node--;
  3174. while (node >= 0) {
  3175. n = get_node(cachep, node);
  3176. if (n) {
  3177. kfree(n->shared);
  3178. free_alien_cache(n->alien);
  3179. kfree(n);
  3180. cachep->node[node] = NULL;
  3181. }
  3182. node--;
  3183. }
  3184. }
  3185. return -ENOMEM;
  3186. }
  3187. /* Always called with the slab_mutex held */
  3188. static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3189. int batchcount, int shared, gfp_t gfp)
  3190. {
  3191. struct array_cache __percpu *cpu_cache, *prev;
  3192. int cpu;
  3193. cpu_cache = alloc_kmem_cache_cpus(cachep, limit, batchcount);
  3194. if (!cpu_cache)
  3195. return -ENOMEM;
  3196. prev = cachep->cpu_cache;
  3197. cachep->cpu_cache = cpu_cache;
  3198. kick_all_cpus_sync();
  3199. check_irq_on();
  3200. cachep->batchcount = batchcount;
  3201. cachep->limit = limit;
  3202. cachep->shared = shared;
  3203. if (!prev)
  3204. goto alloc_node;
  3205. for_each_online_cpu(cpu) {
  3206. LIST_HEAD(list);
  3207. int node;
  3208. struct kmem_cache_node *n;
  3209. struct array_cache *ac = per_cpu_ptr(prev, cpu);
  3210. node = cpu_to_mem(cpu);
  3211. n = get_node(cachep, node);
  3212. spin_lock_irq(&n->list_lock);
  3213. free_block(cachep, ac->entry, ac->avail, node, &list);
  3214. spin_unlock_irq(&n->list_lock);
  3215. slabs_destroy(cachep, &list);
  3216. }
  3217. free_percpu(prev);
  3218. alloc_node:
  3219. return alloc_kmem_cache_node(cachep, gfp);
  3220. }
  3221. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3222. int batchcount, int shared, gfp_t gfp)
  3223. {
  3224. int ret;
  3225. struct kmem_cache *c;
  3226. ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3227. if (slab_state < FULL)
  3228. return ret;
  3229. if ((ret < 0) || !is_root_cache(cachep))
  3230. return ret;
  3231. lockdep_assert_held(&slab_mutex);
  3232. for_each_memcg_cache(c, cachep) {
  3233. /* return value determined by the root cache only */
  3234. __do_tune_cpucache(c, limit, batchcount, shared, gfp);
  3235. }
  3236. return ret;
  3237. }
  3238. /* Called with slab_mutex held always */
  3239. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  3240. {
  3241. int err;
  3242. int limit = 0;
  3243. int shared = 0;
  3244. int batchcount = 0;
  3245. if (!is_root_cache(cachep)) {
  3246. struct kmem_cache *root = memcg_root_cache(cachep);
  3247. limit = root->limit;
  3248. shared = root->shared;
  3249. batchcount = root->batchcount;
  3250. }
  3251. if (limit && shared && batchcount)
  3252. goto skip_setup;
  3253. /*
  3254. * The head array serves three purposes:
  3255. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3256. * - reduce the number of spinlock operations.
  3257. * - reduce the number of linked list operations on the slab and
  3258. * bufctl chains: array operations are cheaper.
  3259. * The numbers are guessed, we should auto-tune as described by
  3260. * Bonwick.
  3261. */
  3262. if (cachep->size > 131072)
  3263. limit = 1;
  3264. else if (cachep->size > PAGE_SIZE)
  3265. limit = 8;
  3266. else if (cachep->size > 1024)
  3267. limit = 24;
  3268. else if (cachep->size > 256)
  3269. limit = 54;
  3270. else
  3271. limit = 120;
  3272. /*
  3273. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3274. * allocation behaviour: Most allocs on one cpu, most free operations
  3275. * on another cpu. For these cases, an efficient object passing between
  3276. * cpus is necessary. This is provided by a shared array. The array
  3277. * replaces Bonwick's magazine layer.
  3278. * On uniprocessor, it's functionally equivalent (but less efficient)
  3279. * to a larger limit. Thus disabled by default.
  3280. */
  3281. shared = 0;
  3282. if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
  3283. shared = 8;
  3284. #if DEBUG
  3285. /*
  3286. * With debugging enabled, large batchcount lead to excessively long
  3287. * periods with disabled local interrupts. Limit the batchcount
  3288. */
  3289. if (limit > 32)
  3290. limit = 32;
  3291. #endif
  3292. batchcount = (limit + 1) / 2;
  3293. skip_setup:
  3294. err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3295. if (err)
  3296. pr_err("enable_cpucache failed for %s, error %d\n",
  3297. cachep->name, -err);
  3298. return err;
  3299. }
  3300. /*
  3301. * Drain an array if it contains any elements taking the node lock only if
  3302. * necessary. Note that the node listlock also protects the array_cache
  3303. * if drain_array() is used on the shared array.
  3304. */
  3305. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  3306. struct array_cache *ac, int force, int node)
  3307. {
  3308. LIST_HEAD(list);
  3309. int tofree;
  3310. if (!ac || !ac->avail)
  3311. return;
  3312. if (ac->touched && !force) {
  3313. ac->touched = 0;
  3314. } else {
  3315. spin_lock_irq(&n->list_lock);
  3316. if (ac->avail) {
  3317. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3318. if (tofree > ac->avail)
  3319. tofree = (ac->avail + 1) / 2;
  3320. free_block(cachep, ac->entry, tofree, node, &list);
  3321. ac->avail -= tofree;
  3322. memmove(ac->entry, &(ac->entry[tofree]),
  3323. sizeof(void *) * ac->avail);
  3324. }
  3325. spin_unlock_irq(&n->list_lock);
  3326. slabs_destroy(cachep, &list);
  3327. }
  3328. }
  3329. /**
  3330. * cache_reap - Reclaim memory from caches.
  3331. * @w: work descriptor
  3332. *
  3333. * Called from workqueue/eventd every few seconds.
  3334. * Purpose:
  3335. * - clear the per-cpu caches for this CPU.
  3336. * - return freeable pages to the main free memory pool.
  3337. *
  3338. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3339. * again on the next iteration.
  3340. */
  3341. static void cache_reap(struct work_struct *w)
  3342. {
  3343. struct kmem_cache *searchp;
  3344. struct kmem_cache_node *n;
  3345. int node = numa_mem_id();
  3346. struct delayed_work *work = to_delayed_work(w);
  3347. if (!mutex_trylock(&slab_mutex))
  3348. /* Give up. Setup the next iteration. */
  3349. goto out;
  3350. list_for_each_entry(searchp, &slab_caches, list) {
  3351. check_irq_on();
  3352. /*
  3353. * We only take the node lock if absolutely necessary and we
  3354. * have established with reasonable certainty that
  3355. * we can do some work if the lock was obtained.
  3356. */
  3357. n = get_node(searchp, node);
  3358. reap_alien(searchp, n);
  3359. drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
  3360. /*
  3361. * These are racy checks but it does not matter
  3362. * if we skip one check or scan twice.
  3363. */
  3364. if (time_after(n->next_reap, jiffies))
  3365. goto next;
  3366. n->next_reap = jiffies + REAPTIMEOUT_NODE;
  3367. drain_array(searchp, n, n->shared, 0, node);
  3368. if (n->free_touched)
  3369. n->free_touched = 0;
  3370. else {
  3371. int freed;
  3372. freed = drain_freelist(searchp, n, (n->free_limit +
  3373. 5 * searchp->num - 1) / (5 * searchp->num));
  3374. STATS_ADD_REAPED(searchp, freed);
  3375. }
  3376. next:
  3377. cond_resched();
  3378. }
  3379. check_irq_on();
  3380. mutex_unlock(&slab_mutex);
  3381. next_reap_node();
  3382. out:
  3383. /* Set up the next iteration */
  3384. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC));
  3385. }
  3386. #ifdef CONFIG_SLABINFO
  3387. void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
  3388. {
  3389. struct page *page;
  3390. unsigned long active_objs;
  3391. unsigned long num_objs;
  3392. unsigned long active_slabs = 0;
  3393. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3394. const char *name;
  3395. char *error = NULL;
  3396. int node;
  3397. struct kmem_cache_node *n;
  3398. active_objs = 0;
  3399. num_slabs = 0;
  3400. for_each_kmem_cache_node(cachep, node, n) {
  3401. check_irq_on();
  3402. spin_lock_irq(&n->list_lock);
  3403. list_for_each_entry(page, &n->slabs_full, lru) {
  3404. if (page->active != cachep->num && !error)
  3405. error = "slabs_full accounting error";
  3406. active_objs += cachep->num;
  3407. active_slabs++;
  3408. }
  3409. list_for_each_entry(page, &n->slabs_partial, lru) {
  3410. if (page->active == cachep->num && !error)
  3411. error = "slabs_partial accounting error";
  3412. if (!page->active && !error)
  3413. error = "slabs_partial accounting error";
  3414. active_objs += page->active;
  3415. active_slabs++;
  3416. }
  3417. list_for_each_entry(page, &n->slabs_free, lru) {
  3418. if (page->active && !error)
  3419. error = "slabs_free accounting error";
  3420. num_slabs++;
  3421. }
  3422. free_objects += n->free_objects;
  3423. if (n->shared)
  3424. shared_avail += n->shared->avail;
  3425. spin_unlock_irq(&n->list_lock);
  3426. }
  3427. num_slabs += active_slabs;
  3428. num_objs = num_slabs * cachep->num;
  3429. if (num_objs - active_objs != free_objects && !error)
  3430. error = "free_objects accounting error";
  3431. name = cachep->name;
  3432. if (error)
  3433. pr_err("slab: cache %s error: %s\n", name, error);
  3434. sinfo->active_objs = active_objs;
  3435. sinfo->num_objs = num_objs;
  3436. sinfo->active_slabs = active_slabs;
  3437. sinfo->num_slabs = num_slabs;
  3438. sinfo->shared_avail = shared_avail;
  3439. sinfo->limit = cachep->limit;
  3440. sinfo->batchcount = cachep->batchcount;
  3441. sinfo->shared = cachep->shared;
  3442. sinfo->objects_per_slab = cachep->num;
  3443. sinfo->cache_order = cachep->gfporder;
  3444. }
  3445. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
  3446. {
  3447. #if STATS
  3448. { /* node stats */
  3449. unsigned long high = cachep->high_mark;
  3450. unsigned long allocs = cachep->num_allocations;
  3451. unsigned long grown = cachep->grown;
  3452. unsigned long reaped = cachep->reaped;
  3453. unsigned long errors = cachep->errors;
  3454. unsigned long max_freeable = cachep->max_freeable;
  3455. unsigned long node_allocs = cachep->node_allocs;
  3456. unsigned long node_frees = cachep->node_frees;
  3457. unsigned long overflows = cachep->node_overflow;
  3458. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu %4lu",
  3459. allocs, high, grown,
  3460. reaped, errors, max_freeable, node_allocs,
  3461. node_frees, overflows);
  3462. }
  3463. /* cpu stats */
  3464. {
  3465. unsigned long allochit = atomic_read(&cachep->allochit);
  3466. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3467. unsigned long freehit = atomic_read(&cachep->freehit);
  3468. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3469. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3470. allochit, allocmiss, freehit, freemiss);
  3471. }
  3472. #endif
  3473. }
  3474. #define MAX_SLABINFO_WRITE 128
  3475. /**
  3476. * slabinfo_write - Tuning for the slab allocator
  3477. * @file: unused
  3478. * @buffer: user buffer
  3479. * @count: data length
  3480. * @ppos: unused
  3481. */
  3482. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  3483. size_t count, loff_t *ppos)
  3484. {
  3485. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3486. int limit, batchcount, shared, res;
  3487. struct kmem_cache *cachep;
  3488. if (count > MAX_SLABINFO_WRITE)
  3489. return -EINVAL;
  3490. if (copy_from_user(&kbuf, buffer, count))
  3491. return -EFAULT;
  3492. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3493. tmp = strchr(kbuf, ' ');
  3494. if (!tmp)
  3495. return -EINVAL;
  3496. *tmp = '\0';
  3497. tmp++;
  3498. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3499. return -EINVAL;
  3500. /* Find the cache in the chain of caches. */
  3501. mutex_lock(&slab_mutex);
  3502. res = -EINVAL;
  3503. list_for_each_entry(cachep, &slab_caches, list) {
  3504. if (!strcmp(cachep->name, kbuf)) {
  3505. if (limit < 1 || batchcount < 1 ||
  3506. batchcount > limit || shared < 0) {
  3507. res = 0;
  3508. } else {
  3509. res = do_tune_cpucache(cachep, limit,
  3510. batchcount, shared,
  3511. GFP_KERNEL);
  3512. }
  3513. break;
  3514. }
  3515. }
  3516. mutex_unlock(&slab_mutex);
  3517. if (res >= 0)
  3518. res = count;
  3519. return res;
  3520. }
  3521. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3522. static inline int add_caller(unsigned long *n, unsigned long v)
  3523. {
  3524. unsigned long *p;
  3525. int l;
  3526. if (!v)
  3527. return 1;
  3528. l = n[1];
  3529. p = n + 2;
  3530. while (l) {
  3531. int i = l/2;
  3532. unsigned long *q = p + 2 * i;
  3533. if (*q == v) {
  3534. q[1]++;
  3535. return 1;
  3536. }
  3537. if (*q > v) {
  3538. l = i;
  3539. } else {
  3540. p = q + 2;
  3541. l -= i + 1;
  3542. }
  3543. }
  3544. if (++n[1] == n[0])
  3545. return 0;
  3546. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3547. p[0] = v;
  3548. p[1] = 1;
  3549. return 1;
  3550. }
  3551. static void handle_slab(unsigned long *n, struct kmem_cache *c,
  3552. struct page *page)
  3553. {
  3554. void *p;
  3555. int i, j;
  3556. unsigned long v;
  3557. if (n[0] == n[1])
  3558. return;
  3559. for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
  3560. bool active = true;
  3561. for (j = page->active; j < c->num; j++) {
  3562. if (get_free_obj(page, j) == i) {
  3563. active = false;
  3564. break;
  3565. }
  3566. }
  3567. if (!active)
  3568. continue;
  3569. /*
  3570. * probe_kernel_read() is used for DEBUG_PAGEALLOC. page table
  3571. * mapping is established when actual object allocation and
  3572. * we could mistakenly access the unmapped object in the cpu
  3573. * cache.
  3574. */
  3575. if (probe_kernel_read(&v, dbg_userword(c, p), sizeof(v)))
  3576. continue;
  3577. if (!add_caller(n, v))
  3578. return;
  3579. }
  3580. }
  3581. static void show_symbol(struct seq_file *m, unsigned long address)
  3582. {
  3583. #ifdef CONFIG_KALLSYMS
  3584. unsigned long offset, size;
  3585. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  3586. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  3587. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3588. if (modname[0])
  3589. seq_printf(m, " [%s]", modname);
  3590. return;
  3591. }
  3592. #endif
  3593. seq_printf(m, "%p", (void *)address);
  3594. }
  3595. static int leaks_show(struct seq_file *m, void *p)
  3596. {
  3597. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
  3598. struct page *page;
  3599. struct kmem_cache_node *n;
  3600. const char *name;
  3601. unsigned long *x = m->private;
  3602. int node;
  3603. int i;
  3604. if (!(cachep->flags & SLAB_STORE_USER))
  3605. return 0;
  3606. if (!(cachep->flags & SLAB_RED_ZONE))
  3607. return 0;
  3608. /*
  3609. * Set store_user_clean and start to grab stored user information
  3610. * for all objects on this cache. If some alloc/free requests comes
  3611. * during the processing, information would be wrong so restart
  3612. * whole processing.
  3613. */
  3614. do {
  3615. set_store_user_clean(cachep);
  3616. drain_cpu_caches(cachep);
  3617. x[1] = 0;
  3618. for_each_kmem_cache_node(cachep, node, n) {
  3619. check_irq_on();
  3620. spin_lock_irq(&n->list_lock);
  3621. list_for_each_entry(page, &n->slabs_full, lru)
  3622. handle_slab(x, cachep, page);
  3623. list_for_each_entry(page, &n->slabs_partial, lru)
  3624. handle_slab(x, cachep, page);
  3625. spin_unlock_irq(&n->list_lock);
  3626. }
  3627. } while (!is_store_user_clean(cachep));
  3628. name = cachep->name;
  3629. if (x[0] == x[1]) {
  3630. /* Increase the buffer size */
  3631. mutex_unlock(&slab_mutex);
  3632. m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  3633. if (!m->private) {
  3634. /* Too bad, we are really out */
  3635. m->private = x;
  3636. mutex_lock(&slab_mutex);
  3637. return -ENOMEM;
  3638. }
  3639. *(unsigned long *)m->private = x[0] * 2;
  3640. kfree(x);
  3641. mutex_lock(&slab_mutex);
  3642. /* Now make sure this entry will be retried */
  3643. m->count = m->size;
  3644. return 0;
  3645. }
  3646. for (i = 0; i < x[1]; i++) {
  3647. seq_printf(m, "%s: %lu ", name, x[2*i+3]);
  3648. show_symbol(m, x[2*i+2]);
  3649. seq_putc(m, '\n');
  3650. }
  3651. return 0;
  3652. }
  3653. static const struct seq_operations slabstats_op = {
  3654. .start = slab_start,
  3655. .next = slab_next,
  3656. .stop = slab_stop,
  3657. .show = leaks_show,
  3658. };
  3659. static int slabstats_open(struct inode *inode, struct file *file)
  3660. {
  3661. unsigned long *n;
  3662. n = __seq_open_private(file, &slabstats_op, PAGE_SIZE);
  3663. if (!n)
  3664. return -ENOMEM;
  3665. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  3666. return 0;
  3667. }
  3668. static const struct file_operations proc_slabstats_operations = {
  3669. .open = slabstats_open,
  3670. .read = seq_read,
  3671. .llseek = seq_lseek,
  3672. .release = seq_release_private,
  3673. };
  3674. #endif
  3675. static int __init slab_proc_init(void)
  3676. {
  3677. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3678. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  3679. #endif
  3680. return 0;
  3681. }
  3682. module_init(slab_proc_init);
  3683. #endif
  3684. /**
  3685. * ksize - get the actual amount of memory allocated for a given object
  3686. * @objp: Pointer to the object
  3687. *
  3688. * kmalloc may internally round up allocations and return more memory
  3689. * than requested. ksize() can be used to determine the actual amount of
  3690. * memory allocated. The caller may use this additional memory, even though
  3691. * a smaller amount of memory was initially specified with the kmalloc call.
  3692. * The caller must guarantee that objp points to a valid object previously
  3693. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  3694. * must not be freed during the duration of the call.
  3695. */
  3696. size_t ksize(const void *objp)
  3697. {
  3698. size_t size;
  3699. BUG_ON(!objp);
  3700. if (unlikely(objp == ZERO_SIZE_PTR))
  3701. return 0;
  3702. size = virt_to_cache(objp)->object_size;
  3703. /* We assume that ksize callers could use the whole allocated area,
  3704. * so we need to unpoison this area.
  3705. */
  3706. kasan_krealloc(objp, size, GFP_NOWAIT);
  3707. return size;
  3708. }
  3709. EXPORT_SYMBOL(ksize);