slab.c 121 KB

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