slab.c 107 KB

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