slab.c 111 KB

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