slab.c 110 KB

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