slab.c 111 KB

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