slab.c 121 KB

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