slab.c 114 KB

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