slab.c 114 KB

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