slab.c 112 KB

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