slab.c 111 KB

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