slab.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501
  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 <= KMALLOC_SHIFT_HIGH; 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, LOCKDEP_NOW_UNRELIABLE);
  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 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 inline int slabs_tofree(struct kmem_cache *cachep,
  1029. struct kmem_cache_node *n)
  1030. {
  1031. return (n->free_objects + cachep->num - 1) / cachep->num;
  1032. }
  1033. static void cpuup_canceled(long cpu)
  1034. {
  1035. struct kmem_cache *cachep;
  1036. struct kmem_cache_node *n = NULL;
  1037. int node = cpu_to_mem(cpu);
  1038. const struct cpumask *mask = cpumask_of_node(node);
  1039. list_for_each_entry(cachep, &slab_caches, list) {
  1040. struct array_cache *nc;
  1041. struct array_cache *shared;
  1042. struct array_cache **alien;
  1043. /* cpu is dead; no one can alloc from it. */
  1044. nc = cachep->array[cpu];
  1045. cachep->array[cpu] = NULL;
  1046. n = cachep->node[node];
  1047. if (!n)
  1048. goto free_array_cache;
  1049. spin_lock_irq(&n->list_lock);
  1050. /* Free limit for this kmem_cache_node */
  1051. n->free_limit -= cachep->batchcount;
  1052. if (nc)
  1053. free_block(cachep, nc->entry, nc->avail, node);
  1054. if (!cpumask_empty(mask)) {
  1055. spin_unlock_irq(&n->list_lock);
  1056. goto free_array_cache;
  1057. }
  1058. shared = n->shared;
  1059. if (shared) {
  1060. free_block(cachep, shared->entry,
  1061. shared->avail, node);
  1062. n->shared = NULL;
  1063. }
  1064. alien = n->alien;
  1065. n->alien = NULL;
  1066. spin_unlock_irq(&n->list_lock);
  1067. kfree(shared);
  1068. if (alien) {
  1069. drain_alien_cache(cachep, alien);
  1070. free_alien_cache(alien);
  1071. }
  1072. free_array_cache:
  1073. kfree(nc);
  1074. }
  1075. /*
  1076. * In the previous loop, all the objects were freed to
  1077. * the respective cache's slabs, now we can go ahead and
  1078. * shrink each nodelist to its limit.
  1079. */
  1080. list_for_each_entry(cachep, &slab_caches, list) {
  1081. n = cachep->node[node];
  1082. if (!n)
  1083. continue;
  1084. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  1085. }
  1086. }
  1087. static int cpuup_prepare(long cpu)
  1088. {
  1089. struct kmem_cache *cachep;
  1090. struct kmem_cache_node *n = NULL;
  1091. int node = cpu_to_mem(cpu);
  1092. int err;
  1093. /*
  1094. * We need to do this right in the beginning since
  1095. * alloc_arraycache's are going to use this list.
  1096. * kmalloc_node allows us to add the slab to the right
  1097. * kmem_cache_node and not this cpu's kmem_cache_node
  1098. */
  1099. err = init_cache_node_node(node);
  1100. if (err < 0)
  1101. goto bad;
  1102. /*
  1103. * Now we can go ahead with allocating the shared arrays and
  1104. * array caches
  1105. */
  1106. list_for_each_entry(cachep, &slab_caches, list) {
  1107. struct array_cache *nc;
  1108. struct array_cache *shared = NULL;
  1109. struct array_cache **alien = NULL;
  1110. nc = alloc_arraycache(node, cachep->limit,
  1111. cachep->batchcount, GFP_KERNEL);
  1112. if (!nc)
  1113. goto bad;
  1114. if (cachep->shared) {
  1115. shared = alloc_arraycache(node,
  1116. cachep->shared * cachep->batchcount,
  1117. 0xbaadf00d, GFP_KERNEL);
  1118. if (!shared) {
  1119. kfree(nc);
  1120. goto bad;
  1121. }
  1122. }
  1123. if (use_alien_caches) {
  1124. alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
  1125. if (!alien) {
  1126. kfree(shared);
  1127. kfree(nc);
  1128. goto bad;
  1129. }
  1130. }
  1131. cachep->array[cpu] = nc;
  1132. n = cachep->node[node];
  1133. BUG_ON(!n);
  1134. spin_lock_irq(&n->list_lock);
  1135. if (!n->shared) {
  1136. /*
  1137. * We are serialised from CPU_DEAD or
  1138. * CPU_UP_CANCELLED by the cpucontrol lock
  1139. */
  1140. n->shared = shared;
  1141. shared = NULL;
  1142. }
  1143. #ifdef CONFIG_NUMA
  1144. if (!n->alien) {
  1145. n->alien = alien;
  1146. alien = NULL;
  1147. }
  1148. #endif
  1149. spin_unlock_irq(&n->list_lock);
  1150. kfree(shared);
  1151. free_alien_cache(alien);
  1152. if (cachep->flags & SLAB_DEBUG_OBJECTS)
  1153. slab_set_debugobj_lock_classes_node(cachep, node);
  1154. else if (!OFF_SLAB(cachep) &&
  1155. !(cachep->flags & SLAB_DESTROY_BY_RCU))
  1156. on_slab_lock_classes_node(cachep, node);
  1157. }
  1158. init_node_lock_keys(node);
  1159. return 0;
  1160. bad:
  1161. cpuup_canceled(cpu);
  1162. return -ENOMEM;
  1163. }
  1164. static int cpuup_callback(struct notifier_block *nfb,
  1165. unsigned long action, void *hcpu)
  1166. {
  1167. long cpu = (long)hcpu;
  1168. int err = 0;
  1169. switch (action) {
  1170. case CPU_UP_PREPARE:
  1171. case CPU_UP_PREPARE_FROZEN:
  1172. mutex_lock(&slab_mutex);
  1173. err = cpuup_prepare(cpu);
  1174. mutex_unlock(&slab_mutex);
  1175. break;
  1176. case CPU_ONLINE:
  1177. case CPU_ONLINE_FROZEN:
  1178. start_cpu_timer(cpu);
  1179. break;
  1180. #ifdef CONFIG_HOTPLUG_CPU
  1181. case CPU_DOWN_PREPARE:
  1182. case CPU_DOWN_PREPARE_FROZEN:
  1183. /*
  1184. * Shutdown cache reaper. Note that the slab_mutex is
  1185. * held so that if cache_reap() is invoked it cannot do
  1186. * anything expensive but will only modify reap_work
  1187. * and reschedule the timer.
  1188. */
  1189. cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
  1190. /* Now the cache_reaper is guaranteed to be not running. */
  1191. per_cpu(slab_reap_work, cpu).work.func = NULL;
  1192. break;
  1193. case CPU_DOWN_FAILED:
  1194. case CPU_DOWN_FAILED_FROZEN:
  1195. start_cpu_timer(cpu);
  1196. break;
  1197. case CPU_DEAD:
  1198. case CPU_DEAD_FROZEN:
  1199. /*
  1200. * Even if all the cpus of a node are down, we don't free the
  1201. * kmem_cache_node of any cache. This to avoid a race between
  1202. * cpu_down, and a kmalloc allocation from another cpu for
  1203. * memory from the node of the cpu going down. The node
  1204. * structure is usually allocated from kmem_cache_create() and
  1205. * gets destroyed at kmem_cache_destroy().
  1206. */
  1207. /* fall through */
  1208. #endif
  1209. case CPU_UP_CANCELED:
  1210. case CPU_UP_CANCELED_FROZEN:
  1211. mutex_lock(&slab_mutex);
  1212. cpuup_canceled(cpu);
  1213. mutex_unlock(&slab_mutex);
  1214. break;
  1215. }
  1216. return notifier_from_errno(err);
  1217. }
  1218. static struct notifier_block cpucache_notifier = {
  1219. &cpuup_callback, NULL, 0
  1220. };
  1221. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  1222. /*
  1223. * Drains freelist for a node on each slab cache, used for memory hot-remove.
  1224. * Returns -EBUSY if all objects cannot be drained so that the node is not
  1225. * removed.
  1226. *
  1227. * Must hold slab_mutex.
  1228. */
  1229. static int __meminit drain_cache_node_node(int node)
  1230. {
  1231. struct kmem_cache *cachep;
  1232. int ret = 0;
  1233. list_for_each_entry(cachep, &slab_caches, list) {
  1234. struct kmem_cache_node *n;
  1235. n = cachep->node[node];
  1236. if (!n)
  1237. continue;
  1238. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  1239. if (!list_empty(&n->slabs_full) ||
  1240. !list_empty(&n->slabs_partial)) {
  1241. ret = -EBUSY;
  1242. break;
  1243. }
  1244. }
  1245. return ret;
  1246. }
  1247. static int __meminit slab_memory_callback(struct notifier_block *self,
  1248. unsigned long action, void *arg)
  1249. {
  1250. struct memory_notify *mnb = arg;
  1251. int ret = 0;
  1252. int nid;
  1253. nid = mnb->status_change_nid;
  1254. if (nid < 0)
  1255. goto out;
  1256. switch (action) {
  1257. case MEM_GOING_ONLINE:
  1258. mutex_lock(&slab_mutex);
  1259. ret = init_cache_node_node(nid);
  1260. mutex_unlock(&slab_mutex);
  1261. break;
  1262. case MEM_GOING_OFFLINE:
  1263. mutex_lock(&slab_mutex);
  1264. ret = drain_cache_node_node(nid);
  1265. mutex_unlock(&slab_mutex);
  1266. break;
  1267. case MEM_ONLINE:
  1268. case MEM_OFFLINE:
  1269. case MEM_CANCEL_ONLINE:
  1270. case MEM_CANCEL_OFFLINE:
  1271. break;
  1272. }
  1273. out:
  1274. return notifier_from_errno(ret);
  1275. }
  1276. #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
  1277. /*
  1278. * swap the static kmem_cache_node with kmalloced memory
  1279. */
  1280. static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
  1281. int nodeid)
  1282. {
  1283. struct kmem_cache_node *ptr;
  1284. ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
  1285. BUG_ON(!ptr);
  1286. memcpy(ptr, list, sizeof(struct kmem_cache_node));
  1287. /*
  1288. * Do not assume that spinlocks can be initialized via memcpy:
  1289. */
  1290. spin_lock_init(&ptr->list_lock);
  1291. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1292. cachep->node[nodeid] = ptr;
  1293. }
  1294. /*
  1295. * For setting up all the kmem_cache_node for cache whose buffer_size is same as
  1296. * size of kmem_cache_node.
  1297. */
  1298. static void __init set_up_node(struct kmem_cache *cachep, int index)
  1299. {
  1300. int node;
  1301. for_each_online_node(node) {
  1302. cachep->node[node] = &init_kmem_cache_node[index + node];
  1303. cachep->node[node]->next_reap = jiffies +
  1304. REAPTIMEOUT_LIST3 +
  1305. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1306. }
  1307. }
  1308. /*
  1309. * The memory after the last cpu cache pointer is used for the
  1310. * the node pointer.
  1311. */
  1312. static void setup_node_pointer(struct kmem_cache *cachep)
  1313. {
  1314. cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
  1315. }
  1316. /*
  1317. * Initialisation. Called after the page allocator have been initialised and
  1318. * before smp_init().
  1319. */
  1320. void __init kmem_cache_init(void)
  1321. {
  1322. int i;
  1323. kmem_cache = &kmem_cache_boot;
  1324. setup_node_pointer(kmem_cache);
  1325. if (num_possible_nodes() == 1)
  1326. use_alien_caches = 0;
  1327. for (i = 0; i < NUM_INIT_LISTS; i++)
  1328. kmem_cache_node_init(&init_kmem_cache_node[i]);
  1329. set_up_node(kmem_cache, CACHE_CACHE);
  1330. /*
  1331. * Fragmentation resistance on low memory - only use bigger
  1332. * page orders on machines with more than 32MB of memory if
  1333. * not overridden on the command line.
  1334. */
  1335. if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
  1336. slab_max_order = SLAB_MAX_ORDER_HI;
  1337. /* Bootstrap is tricky, because several objects are allocated
  1338. * from caches that do not exist yet:
  1339. * 1) initialize the kmem_cache cache: it contains the struct
  1340. * kmem_cache structures of all caches, except kmem_cache itself:
  1341. * kmem_cache is statically allocated.
  1342. * Initially an __init data area is used for the head array and the
  1343. * kmem_cache_node structures, it's replaced with a kmalloc allocated
  1344. * array at the end of the bootstrap.
  1345. * 2) Create the first kmalloc cache.
  1346. * The struct kmem_cache for the new cache is allocated normally.
  1347. * An __init data area is used for the head array.
  1348. * 3) Create the remaining kmalloc caches, with minimally sized
  1349. * head arrays.
  1350. * 4) Replace the __init data head arrays for kmem_cache and the first
  1351. * kmalloc cache with kmalloc allocated arrays.
  1352. * 5) Replace the __init data for kmem_cache_node for kmem_cache and
  1353. * the other cache's with kmalloc allocated memory.
  1354. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1355. */
  1356. /* 1) create the kmem_cache */
  1357. /*
  1358. * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
  1359. */
  1360. create_boot_cache(kmem_cache, "kmem_cache",
  1361. offsetof(struct kmem_cache, array[nr_cpu_ids]) +
  1362. nr_node_ids * sizeof(struct kmem_cache_node *),
  1363. SLAB_HWCACHE_ALIGN);
  1364. list_add(&kmem_cache->list, &slab_caches);
  1365. /* 2+3) create the kmalloc caches */
  1366. /*
  1367. * Initialize the caches that provide memory for the array cache and the
  1368. * kmem_cache_node structures first. Without this, further allocations will
  1369. * bug.
  1370. */
  1371. kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
  1372. kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
  1373. if (INDEX_AC != INDEX_NODE)
  1374. kmalloc_caches[INDEX_NODE] =
  1375. create_kmalloc_cache("kmalloc-node",
  1376. kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
  1377. slab_early_init = 0;
  1378. /* 4) Replace the bootstrap head arrays */
  1379. {
  1380. struct array_cache *ptr;
  1381. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1382. memcpy(ptr, cpu_cache_get(kmem_cache),
  1383. sizeof(struct arraycache_init));
  1384. /*
  1385. * Do not assume that spinlocks can be initialized via memcpy:
  1386. */
  1387. spin_lock_init(&ptr->lock);
  1388. kmem_cache->array[smp_processor_id()] = ptr;
  1389. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1390. BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
  1391. != &initarray_generic.cache);
  1392. memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
  1393. sizeof(struct arraycache_init));
  1394. /*
  1395. * Do not assume that spinlocks can be initialized via memcpy:
  1396. */
  1397. spin_lock_init(&ptr->lock);
  1398. kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
  1399. }
  1400. /* 5) Replace the bootstrap kmem_cache_node */
  1401. {
  1402. int nid;
  1403. for_each_online_node(nid) {
  1404. init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
  1405. init_list(kmalloc_caches[INDEX_AC],
  1406. &init_kmem_cache_node[SIZE_AC + nid], nid);
  1407. if (INDEX_AC != INDEX_NODE) {
  1408. init_list(kmalloc_caches[INDEX_NODE],
  1409. &init_kmem_cache_node[SIZE_NODE + nid], nid);
  1410. }
  1411. }
  1412. }
  1413. create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
  1414. }
  1415. void __init kmem_cache_init_late(void)
  1416. {
  1417. struct kmem_cache *cachep;
  1418. slab_state = UP;
  1419. /* 6) resize the head arrays to their final sizes */
  1420. mutex_lock(&slab_mutex);
  1421. list_for_each_entry(cachep, &slab_caches, list)
  1422. if (enable_cpucache(cachep, GFP_NOWAIT))
  1423. BUG();
  1424. mutex_unlock(&slab_mutex);
  1425. /* Annotate slab for lockdep -- annotate the malloc caches */
  1426. init_lock_keys();
  1427. /* Done! */
  1428. slab_state = FULL;
  1429. /*
  1430. * Register a cpu startup notifier callback that initializes
  1431. * cpu_cache_get for all new cpus
  1432. */
  1433. register_cpu_notifier(&cpucache_notifier);
  1434. #ifdef CONFIG_NUMA
  1435. /*
  1436. * Register a memory hotplug callback that initializes and frees
  1437. * node.
  1438. */
  1439. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  1440. #endif
  1441. /*
  1442. * The reap timers are started later, with a module init call: That part
  1443. * of the kernel is not yet operational.
  1444. */
  1445. }
  1446. static int __init cpucache_init(void)
  1447. {
  1448. int cpu;
  1449. /*
  1450. * Register the timers that return unneeded pages to the page allocator
  1451. */
  1452. for_each_online_cpu(cpu)
  1453. start_cpu_timer(cpu);
  1454. /* Done! */
  1455. slab_state = FULL;
  1456. return 0;
  1457. }
  1458. __initcall(cpucache_init);
  1459. static noinline void
  1460. slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  1461. {
  1462. struct kmem_cache_node *n;
  1463. struct slab *slabp;
  1464. unsigned long flags;
  1465. int node;
  1466. printk(KERN_WARNING
  1467. "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1468. nodeid, gfpflags);
  1469. printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
  1470. cachep->name, cachep->size, cachep->gfporder);
  1471. for_each_online_node(node) {
  1472. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1473. unsigned long active_slabs = 0, num_slabs = 0;
  1474. n = cachep->node[node];
  1475. if (!n)
  1476. continue;
  1477. spin_lock_irqsave(&n->list_lock, flags);
  1478. list_for_each_entry(slabp, &n->slabs_full, list) {
  1479. active_objs += cachep->num;
  1480. active_slabs++;
  1481. }
  1482. list_for_each_entry(slabp, &n->slabs_partial, list) {
  1483. active_objs += slabp->inuse;
  1484. active_slabs++;
  1485. }
  1486. list_for_each_entry(slabp, &n->slabs_free, list)
  1487. num_slabs++;
  1488. free_objects += n->free_objects;
  1489. spin_unlock_irqrestore(&n->list_lock, flags);
  1490. num_slabs += active_slabs;
  1491. num_objs = num_slabs * cachep->num;
  1492. printk(KERN_WARNING
  1493. " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1494. node, active_slabs, num_slabs, active_objs, num_objs,
  1495. free_objects);
  1496. }
  1497. }
  1498. /*
  1499. * Interface to system's page allocator. No need to hold the cache-lock.
  1500. *
  1501. * If we requested dmaable memory, we will get it. Even if we
  1502. * did not request dmaable memory, we might get it, but that
  1503. * would be relatively rare and ignorable.
  1504. */
  1505. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1506. {
  1507. struct page *page;
  1508. int nr_pages;
  1509. int i;
  1510. #ifndef CONFIG_MMU
  1511. /*
  1512. * Nommu uses slab's for process anonymous memory allocations, and thus
  1513. * requires __GFP_COMP to properly refcount higher order allocations
  1514. */
  1515. flags |= __GFP_COMP;
  1516. #endif
  1517. flags |= cachep->allocflags;
  1518. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1519. flags |= __GFP_RECLAIMABLE;
  1520. page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1521. if (!page) {
  1522. if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  1523. slab_out_of_memory(cachep, flags, nodeid);
  1524. return NULL;
  1525. }
  1526. /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
  1527. if (unlikely(page->pfmemalloc))
  1528. pfmemalloc_active = true;
  1529. nr_pages = (1 << cachep->gfporder);
  1530. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1531. add_zone_page_state(page_zone(page),
  1532. NR_SLAB_RECLAIMABLE, nr_pages);
  1533. else
  1534. add_zone_page_state(page_zone(page),
  1535. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1536. for (i = 0; i < nr_pages; i++) {
  1537. __SetPageSlab(page + i);
  1538. if (page->pfmemalloc)
  1539. SetPageSlabPfmemalloc(page + i);
  1540. }
  1541. memcg_bind_pages(cachep, cachep->gfporder);
  1542. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1543. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1544. if (cachep->ctor)
  1545. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1546. else
  1547. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1548. }
  1549. return page_address(page);
  1550. }
  1551. /*
  1552. * Interface to system's page release.
  1553. */
  1554. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1555. {
  1556. unsigned long i = (1 << cachep->gfporder);
  1557. struct page *page = virt_to_page(addr);
  1558. const unsigned long nr_freed = i;
  1559. kmemcheck_free_shadow(page, cachep->gfporder);
  1560. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1561. sub_zone_page_state(page_zone(page),
  1562. NR_SLAB_RECLAIMABLE, nr_freed);
  1563. else
  1564. sub_zone_page_state(page_zone(page),
  1565. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1566. while (i--) {
  1567. BUG_ON(!PageSlab(page));
  1568. __ClearPageSlabPfmemalloc(page);
  1569. __ClearPageSlab(page);
  1570. page++;
  1571. }
  1572. memcg_release_pages(cachep, cachep->gfporder);
  1573. if (current->reclaim_state)
  1574. current->reclaim_state->reclaimed_slab += nr_freed;
  1575. free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
  1576. }
  1577. static void kmem_rcu_free(struct rcu_head *head)
  1578. {
  1579. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1580. struct kmem_cache *cachep = slab_rcu->cachep;
  1581. kmem_freepages(cachep, slab_rcu->addr);
  1582. if (OFF_SLAB(cachep))
  1583. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1584. }
  1585. #if DEBUG
  1586. #ifdef CONFIG_DEBUG_PAGEALLOC
  1587. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1588. unsigned long caller)
  1589. {
  1590. int size = cachep->object_size;
  1591. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1592. if (size < 5 * sizeof(unsigned long))
  1593. return;
  1594. *addr++ = 0x12345678;
  1595. *addr++ = caller;
  1596. *addr++ = smp_processor_id();
  1597. size -= 3 * sizeof(unsigned long);
  1598. {
  1599. unsigned long *sptr = &caller;
  1600. unsigned long svalue;
  1601. while (!kstack_end(sptr)) {
  1602. svalue = *sptr++;
  1603. if (kernel_text_address(svalue)) {
  1604. *addr++ = svalue;
  1605. size -= sizeof(unsigned long);
  1606. if (size <= sizeof(unsigned long))
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. *addr++ = 0x87654321;
  1612. }
  1613. #endif
  1614. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1615. {
  1616. int size = cachep->object_size;
  1617. addr = &((char *)addr)[obj_offset(cachep)];
  1618. memset(addr, val, size);
  1619. *(unsigned char *)(addr + size - 1) = POISON_END;
  1620. }
  1621. static void dump_line(char *data, int offset, int limit)
  1622. {
  1623. int i;
  1624. unsigned char error = 0;
  1625. int bad_count = 0;
  1626. printk(KERN_ERR "%03x: ", offset);
  1627. for (i = 0; i < limit; i++) {
  1628. if (data[offset + i] != POISON_FREE) {
  1629. error = data[offset + i];
  1630. bad_count++;
  1631. }
  1632. }
  1633. print_hex_dump(KERN_CONT, "", 0, 16, 1,
  1634. &data[offset], limit, 1);
  1635. if (bad_count == 1) {
  1636. error ^= POISON_FREE;
  1637. if (!(error & (error - 1))) {
  1638. printk(KERN_ERR "Single bit error detected. Probably "
  1639. "bad RAM.\n");
  1640. #ifdef CONFIG_X86
  1641. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1642. "test tool.\n");
  1643. #else
  1644. printk(KERN_ERR "Run a memory test tool.\n");
  1645. #endif
  1646. }
  1647. }
  1648. }
  1649. #endif
  1650. #if DEBUG
  1651. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1652. {
  1653. int i, size;
  1654. char *realobj;
  1655. if (cachep->flags & SLAB_RED_ZONE) {
  1656. printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
  1657. *dbg_redzone1(cachep, objp),
  1658. *dbg_redzone2(cachep, objp));
  1659. }
  1660. if (cachep->flags & SLAB_STORE_USER) {
  1661. printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
  1662. *dbg_userword(cachep, objp),
  1663. *dbg_userword(cachep, objp));
  1664. }
  1665. realobj = (char *)objp + obj_offset(cachep);
  1666. size = cachep->object_size;
  1667. for (i = 0; i < size && lines; i += 16, lines--) {
  1668. int limit;
  1669. limit = 16;
  1670. if (i + limit > size)
  1671. limit = size - i;
  1672. dump_line(realobj, i, limit);
  1673. }
  1674. }
  1675. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1676. {
  1677. char *realobj;
  1678. int size, i;
  1679. int lines = 0;
  1680. realobj = (char *)objp + obj_offset(cachep);
  1681. size = cachep->object_size;
  1682. for (i = 0; i < size; i++) {
  1683. char exp = POISON_FREE;
  1684. if (i == size - 1)
  1685. exp = POISON_END;
  1686. if (realobj[i] != exp) {
  1687. int limit;
  1688. /* Mismatch ! */
  1689. /* Print header */
  1690. if (lines == 0) {
  1691. printk(KERN_ERR
  1692. "Slab corruption (%s): %s start=%p, len=%d\n",
  1693. print_tainted(), cachep->name, realobj, size);
  1694. print_objinfo(cachep, objp, 0);
  1695. }
  1696. /* Hexdump the affected line */
  1697. i = (i / 16) * 16;
  1698. limit = 16;
  1699. if (i + limit > size)
  1700. limit = size - i;
  1701. dump_line(realobj, i, limit);
  1702. i += 16;
  1703. lines++;
  1704. /* Limit to 5 lines */
  1705. if (lines > 5)
  1706. break;
  1707. }
  1708. }
  1709. if (lines != 0) {
  1710. /* Print some data about the neighboring objects, if they
  1711. * exist:
  1712. */
  1713. struct slab *slabp = virt_to_slab(objp);
  1714. unsigned int objnr;
  1715. objnr = obj_to_index(cachep, slabp, objp);
  1716. if (objnr) {
  1717. objp = index_to_obj(cachep, slabp, objnr - 1);
  1718. realobj = (char *)objp + obj_offset(cachep);
  1719. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1720. realobj, size);
  1721. print_objinfo(cachep, objp, 2);
  1722. }
  1723. if (objnr + 1 < cachep->num) {
  1724. objp = index_to_obj(cachep, slabp, objnr + 1);
  1725. realobj = (char *)objp + obj_offset(cachep);
  1726. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1727. realobj, size);
  1728. print_objinfo(cachep, objp, 2);
  1729. }
  1730. }
  1731. }
  1732. #endif
  1733. #if DEBUG
  1734. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1735. {
  1736. int i;
  1737. for (i = 0; i < cachep->num; i++) {
  1738. void *objp = index_to_obj(cachep, slabp, i);
  1739. if (cachep->flags & SLAB_POISON) {
  1740. #ifdef CONFIG_DEBUG_PAGEALLOC
  1741. if (cachep->size % PAGE_SIZE == 0 &&
  1742. OFF_SLAB(cachep))
  1743. kernel_map_pages(virt_to_page(objp),
  1744. cachep->size / PAGE_SIZE, 1);
  1745. else
  1746. check_poison_obj(cachep, objp);
  1747. #else
  1748. check_poison_obj(cachep, objp);
  1749. #endif
  1750. }
  1751. if (cachep->flags & SLAB_RED_ZONE) {
  1752. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1753. slab_error(cachep, "start of a freed object "
  1754. "was overwritten");
  1755. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1756. slab_error(cachep, "end of a freed object "
  1757. "was overwritten");
  1758. }
  1759. }
  1760. }
  1761. #else
  1762. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1763. {
  1764. }
  1765. #endif
  1766. /**
  1767. * slab_destroy - destroy and release all objects in a slab
  1768. * @cachep: cache pointer being destroyed
  1769. * @slabp: slab pointer being destroyed
  1770. *
  1771. * Destroy all the objs in a slab, and release the mem back to the system.
  1772. * Before calling the slab must have been unlinked from the cache. The
  1773. * cache-lock is not held/needed.
  1774. */
  1775. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1776. {
  1777. void *addr = slabp->s_mem - slabp->colouroff;
  1778. slab_destroy_debugcheck(cachep, slabp);
  1779. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1780. struct slab_rcu *slab_rcu;
  1781. slab_rcu = (struct slab_rcu *)slabp;
  1782. slab_rcu->cachep = cachep;
  1783. slab_rcu->addr = addr;
  1784. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1785. } else {
  1786. kmem_freepages(cachep, addr);
  1787. if (OFF_SLAB(cachep))
  1788. kmem_cache_free(cachep->slabp_cache, slabp);
  1789. }
  1790. }
  1791. /**
  1792. * calculate_slab_order - calculate size (page order) of slabs
  1793. * @cachep: pointer to the cache that is being created
  1794. * @size: size of objects to be created in this cache.
  1795. * @align: required alignment for the objects.
  1796. * @flags: slab allocation flags
  1797. *
  1798. * Also calculates the number of objects per slab.
  1799. *
  1800. * This could be made much more intelligent. For now, try to avoid using
  1801. * high order pages for slabs. When the gfp() functions are more friendly
  1802. * towards high-order requests, this should be changed.
  1803. */
  1804. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1805. size_t size, size_t align, unsigned long flags)
  1806. {
  1807. unsigned long offslab_limit;
  1808. size_t left_over = 0;
  1809. int gfporder;
  1810. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1811. unsigned int num;
  1812. size_t remainder;
  1813. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1814. if (!num)
  1815. continue;
  1816. if (flags & CFLGS_OFF_SLAB) {
  1817. /*
  1818. * Max number of objs-per-slab for caches which
  1819. * use off-slab slabs. Needed to avoid a possible
  1820. * looping condition in cache_grow().
  1821. */
  1822. offslab_limit = size - sizeof(struct slab);
  1823. offslab_limit /= sizeof(kmem_bufctl_t);
  1824. if (num > offslab_limit)
  1825. break;
  1826. }
  1827. /* Found something acceptable - save it away */
  1828. cachep->num = num;
  1829. cachep->gfporder = gfporder;
  1830. left_over = remainder;
  1831. /*
  1832. * A VFS-reclaimable slab tends to have most allocations
  1833. * as GFP_NOFS and we really don't want to have to be allocating
  1834. * higher-order pages when we are unable to shrink dcache.
  1835. */
  1836. if (flags & SLAB_RECLAIM_ACCOUNT)
  1837. break;
  1838. /*
  1839. * Large number of objects is good, but very large slabs are
  1840. * currently bad for the gfp()s.
  1841. */
  1842. if (gfporder >= slab_max_order)
  1843. break;
  1844. /*
  1845. * Acceptable internal fragmentation?
  1846. */
  1847. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1848. break;
  1849. }
  1850. return left_over;
  1851. }
  1852. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  1853. {
  1854. if (slab_state >= FULL)
  1855. return enable_cpucache(cachep, gfp);
  1856. if (slab_state == DOWN) {
  1857. /*
  1858. * Note: Creation of first cache (kmem_cache).
  1859. * The setup_node is taken care
  1860. * of by the caller of __kmem_cache_create
  1861. */
  1862. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1863. slab_state = PARTIAL;
  1864. } else if (slab_state == PARTIAL) {
  1865. /*
  1866. * Note: the second kmem_cache_create must create the cache
  1867. * that's used by kmalloc(24), otherwise the creation of
  1868. * further caches will BUG().
  1869. */
  1870. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1871. /*
  1872. * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
  1873. * the second cache, then we need to set up all its node/,
  1874. * otherwise the creation of further caches will BUG().
  1875. */
  1876. set_up_node(cachep, SIZE_AC);
  1877. if (INDEX_AC == INDEX_NODE)
  1878. slab_state = PARTIAL_NODE;
  1879. else
  1880. slab_state = PARTIAL_ARRAYCACHE;
  1881. } else {
  1882. /* Remaining boot caches */
  1883. cachep->array[smp_processor_id()] =
  1884. kmalloc(sizeof(struct arraycache_init), gfp);
  1885. if (slab_state == PARTIAL_ARRAYCACHE) {
  1886. set_up_node(cachep, SIZE_NODE);
  1887. slab_state = PARTIAL_NODE;
  1888. } else {
  1889. int node;
  1890. for_each_online_node(node) {
  1891. cachep->node[node] =
  1892. kmalloc_node(sizeof(struct kmem_cache_node),
  1893. gfp, node);
  1894. BUG_ON(!cachep->node[node]);
  1895. kmem_cache_node_init(cachep->node[node]);
  1896. }
  1897. }
  1898. }
  1899. cachep->node[numa_mem_id()]->next_reap =
  1900. jiffies + REAPTIMEOUT_LIST3 +
  1901. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1902. cpu_cache_get(cachep)->avail = 0;
  1903. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1904. cpu_cache_get(cachep)->batchcount = 1;
  1905. cpu_cache_get(cachep)->touched = 0;
  1906. cachep->batchcount = 1;
  1907. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1908. return 0;
  1909. }
  1910. /**
  1911. * __kmem_cache_create - Create a cache.
  1912. * @cachep: cache management descriptor
  1913. * @flags: SLAB flags
  1914. *
  1915. * Returns a ptr to the cache on success, NULL on failure.
  1916. * Cannot be called within a int, but can be interrupted.
  1917. * The @ctor is run when new pages are allocated by the cache.
  1918. *
  1919. * The flags are
  1920. *
  1921. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1922. * to catch references to uninitialised memory.
  1923. *
  1924. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1925. * for buffer overruns.
  1926. *
  1927. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1928. * cacheline. This can be beneficial if you're counting cycles as closely
  1929. * as davem.
  1930. */
  1931. int
  1932. __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
  1933. {
  1934. size_t left_over, slab_size, ralign;
  1935. gfp_t gfp;
  1936. int err;
  1937. size_t size = cachep->size;
  1938. #if DEBUG
  1939. #if FORCED_DEBUG
  1940. /*
  1941. * Enable redzoning and last user accounting, except for caches with
  1942. * large objects, if the increased size would increase the object size
  1943. * above the next power of two: caches with object sizes just above a
  1944. * power of two have a significant amount of internal fragmentation.
  1945. */
  1946. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  1947. 2 * sizeof(unsigned long long)))
  1948. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1949. if (!(flags & SLAB_DESTROY_BY_RCU))
  1950. flags |= SLAB_POISON;
  1951. #endif
  1952. if (flags & SLAB_DESTROY_BY_RCU)
  1953. BUG_ON(flags & SLAB_POISON);
  1954. #endif
  1955. /*
  1956. * Check that size is in terms of words. This is needed to avoid
  1957. * unaligned accesses for some archs when redzoning is used, and makes
  1958. * sure any on-slab bufctl's are also correctly aligned.
  1959. */
  1960. if (size & (BYTES_PER_WORD - 1)) {
  1961. size += (BYTES_PER_WORD - 1);
  1962. size &= ~(BYTES_PER_WORD - 1);
  1963. }
  1964. /*
  1965. * Redzoning and user store require word alignment or possibly larger.
  1966. * Note this will be overridden by architecture or caller mandated
  1967. * alignment if either is greater than BYTES_PER_WORD.
  1968. */
  1969. if (flags & SLAB_STORE_USER)
  1970. ralign = BYTES_PER_WORD;
  1971. if (flags & SLAB_RED_ZONE) {
  1972. ralign = REDZONE_ALIGN;
  1973. /* If redzoning, ensure that the second redzone is suitably
  1974. * aligned, by adjusting the object size accordingly. */
  1975. size += REDZONE_ALIGN - 1;
  1976. size &= ~(REDZONE_ALIGN - 1);
  1977. }
  1978. /* 3) caller mandated alignment */
  1979. if (ralign < cachep->align) {
  1980. ralign = cachep->align;
  1981. }
  1982. /* disable debug if necessary */
  1983. if (ralign > __alignof__(unsigned long long))
  1984. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1985. /*
  1986. * 4) Store it.
  1987. */
  1988. cachep->align = ralign;
  1989. if (slab_is_available())
  1990. gfp = GFP_KERNEL;
  1991. else
  1992. gfp = GFP_NOWAIT;
  1993. setup_node_pointer(cachep);
  1994. #if DEBUG
  1995. /*
  1996. * Both debugging options require word-alignment which is calculated
  1997. * into align above.
  1998. */
  1999. if (flags & SLAB_RED_ZONE) {
  2000. /* add space for red zone words */
  2001. cachep->obj_offset += sizeof(unsigned long long);
  2002. size += 2 * sizeof(unsigned long long);
  2003. }
  2004. if (flags & SLAB_STORE_USER) {
  2005. /* user store requires one word storage behind the end of
  2006. * the real object. But if the second red zone needs to be
  2007. * aligned to 64 bits, we must allow that much space.
  2008. */
  2009. if (flags & SLAB_RED_ZONE)
  2010. size += REDZONE_ALIGN;
  2011. else
  2012. size += BYTES_PER_WORD;
  2013. }
  2014. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  2015. if (size >= kmalloc_size(INDEX_NODE + 1)
  2016. && cachep->object_size > cache_line_size()
  2017. && ALIGN(size, cachep->align) < PAGE_SIZE) {
  2018. cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
  2019. size = PAGE_SIZE;
  2020. }
  2021. #endif
  2022. #endif
  2023. /*
  2024. * Determine if the slab management is 'on' or 'off' slab.
  2025. * (bootstrapping cannot cope with offslab caches so don't do
  2026. * it too early on. Always use on-slab management when
  2027. * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
  2028. */
  2029. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  2030. !(flags & SLAB_NOLEAKTRACE))
  2031. /*
  2032. * Size is large, assume best to place the slab management obj
  2033. * off-slab (should allow better packing of objs).
  2034. */
  2035. flags |= CFLGS_OFF_SLAB;
  2036. size = ALIGN(size, cachep->align);
  2037. left_over = calculate_slab_order(cachep, size, cachep->align, flags);
  2038. if (!cachep->num)
  2039. return -E2BIG;
  2040. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  2041. + sizeof(struct slab), cachep->align);
  2042. /*
  2043. * If the slab has been placed off-slab, and we have enough space then
  2044. * move it on-slab. This is at the expense of any extra colouring.
  2045. */
  2046. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  2047. flags &= ~CFLGS_OFF_SLAB;
  2048. left_over -= slab_size;
  2049. }
  2050. if (flags & CFLGS_OFF_SLAB) {
  2051. /* really off slab. No need for manual alignment */
  2052. slab_size =
  2053. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  2054. #ifdef CONFIG_PAGE_POISONING
  2055. /* If we're going to use the generic kernel_map_pages()
  2056. * poisoning, then it's going to smash the contents of
  2057. * the redzone and userword anyhow, so switch them off.
  2058. */
  2059. if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  2060. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2061. #endif
  2062. }
  2063. cachep->colour_off = cache_line_size();
  2064. /* Offset must be a multiple of the alignment. */
  2065. if (cachep->colour_off < cachep->align)
  2066. cachep->colour_off = cachep->align;
  2067. cachep->colour = left_over / cachep->colour_off;
  2068. cachep->slab_size = slab_size;
  2069. cachep->flags = flags;
  2070. cachep->allocflags = 0;
  2071. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  2072. cachep->allocflags |= GFP_DMA;
  2073. cachep->size = size;
  2074. cachep->reciprocal_buffer_size = reciprocal_value(size);
  2075. if (flags & CFLGS_OFF_SLAB) {
  2076. cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
  2077. /*
  2078. * This is a possibility for one of the malloc_sizes caches.
  2079. * But since we go off slab only for object size greater than
  2080. * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  2081. * this should not happen at all.
  2082. * But leave a BUG_ON for some lucky dude.
  2083. */
  2084. BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
  2085. }
  2086. err = setup_cpu_cache(cachep, gfp);
  2087. if (err) {
  2088. __kmem_cache_shutdown(cachep);
  2089. return err;
  2090. }
  2091. if (flags & SLAB_DEBUG_OBJECTS) {
  2092. /*
  2093. * Would deadlock through slab_destroy()->call_rcu()->
  2094. * debug_object_activate()->kmem_cache_alloc().
  2095. */
  2096. WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  2097. slab_set_debugobj_lock_classes(cachep);
  2098. } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
  2099. on_slab_lock_classes(cachep);
  2100. return 0;
  2101. }
  2102. #if DEBUG
  2103. static void check_irq_off(void)
  2104. {
  2105. BUG_ON(!irqs_disabled());
  2106. }
  2107. static void check_irq_on(void)
  2108. {
  2109. BUG_ON(irqs_disabled());
  2110. }
  2111. static void check_spinlock_acquired(struct kmem_cache *cachep)
  2112. {
  2113. #ifdef CONFIG_SMP
  2114. check_irq_off();
  2115. assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
  2116. #endif
  2117. }
  2118. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  2119. {
  2120. #ifdef CONFIG_SMP
  2121. check_irq_off();
  2122. assert_spin_locked(&cachep->node[node]->list_lock);
  2123. #endif
  2124. }
  2125. #else
  2126. #define check_irq_off() do { } while(0)
  2127. #define check_irq_on() do { } while(0)
  2128. #define check_spinlock_acquired(x) do { } while(0)
  2129. #define check_spinlock_acquired_node(x, y) do { } while(0)
  2130. #endif
  2131. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  2132. struct array_cache *ac,
  2133. int force, int node);
  2134. static void do_drain(void *arg)
  2135. {
  2136. struct kmem_cache *cachep = arg;
  2137. struct array_cache *ac;
  2138. int node = numa_mem_id();
  2139. check_irq_off();
  2140. ac = cpu_cache_get(cachep);
  2141. spin_lock(&cachep->node[node]->list_lock);
  2142. free_block(cachep, ac->entry, ac->avail, node);
  2143. spin_unlock(&cachep->node[node]->list_lock);
  2144. ac->avail = 0;
  2145. }
  2146. static void drain_cpu_caches(struct kmem_cache *cachep)
  2147. {
  2148. struct kmem_cache_node *n;
  2149. int node;
  2150. on_each_cpu(do_drain, cachep, 1);
  2151. check_irq_on();
  2152. for_each_online_node(node) {
  2153. n = cachep->node[node];
  2154. if (n && n->alien)
  2155. drain_alien_cache(cachep, n->alien);
  2156. }
  2157. for_each_online_node(node) {
  2158. n = cachep->node[node];
  2159. if (n)
  2160. drain_array(cachep, n, n->shared, 1, node);
  2161. }
  2162. }
  2163. /*
  2164. * Remove slabs from the list of free slabs.
  2165. * Specify the number of slabs to drain in tofree.
  2166. *
  2167. * Returns the actual number of slabs released.
  2168. */
  2169. static int drain_freelist(struct kmem_cache *cache,
  2170. struct kmem_cache_node *n, int tofree)
  2171. {
  2172. struct list_head *p;
  2173. int nr_freed;
  2174. struct slab *slabp;
  2175. nr_freed = 0;
  2176. while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
  2177. spin_lock_irq(&n->list_lock);
  2178. p = n->slabs_free.prev;
  2179. if (p == &n->slabs_free) {
  2180. spin_unlock_irq(&n->list_lock);
  2181. goto out;
  2182. }
  2183. slabp = list_entry(p, struct slab, list);
  2184. #if DEBUG
  2185. BUG_ON(slabp->inuse);
  2186. #endif
  2187. list_del(&slabp->list);
  2188. /*
  2189. * Safe to drop the lock. The slab is no longer linked
  2190. * to the cache.
  2191. */
  2192. n->free_objects -= cache->num;
  2193. spin_unlock_irq(&n->list_lock);
  2194. slab_destroy(cache, slabp);
  2195. nr_freed++;
  2196. }
  2197. out:
  2198. return nr_freed;
  2199. }
  2200. /* Called with slab_mutex held to protect against cpu hotplug */
  2201. static int __cache_shrink(struct kmem_cache *cachep)
  2202. {
  2203. int ret = 0, i = 0;
  2204. struct kmem_cache_node *n;
  2205. drain_cpu_caches(cachep);
  2206. check_irq_on();
  2207. for_each_online_node(i) {
  2208. n = cachep->node[i];
  2209. if (!n)
  2210. continue;
  2211. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  2212. ret += !list_empty(&n->slabs_full) ||
  2213. !list_empty(&n->slabs_partial);
  2214. }
  2215. return (ret ? 1 : 0);
  2216. }
  2217. /**
  2218. * kmem_cache_shrink - Shrink a cache.
  2219. * @cachep: The cache to shrink.
  2220. *
  2221. * Releases as many slabs as possible for a cache.
  2222. * To help debugging, a zero exit status indicates all slabs were released.
  2223. */
  2224. int kmem_cache_shrink(struct kmem_cache *cachep)
  2225. {
  2226. int ret;
  2227. BUG_ON(!cachep || in_interrupt());
  2228. get_online_cpus();
  2229. mutex_lock(&slab_mutex);
  2230. ret = __cache_shrink(cachep);
  2231. mutex_unlock(&slab_mutex);
  2232. put_online_cpus();
  2233. return ret;
  2234. }
  2235. EXPORT_SYMBOL(kmem_cache_shrink);
  2236. int __kmem_cache_shutdown(struct kmem_cache *cachep)
  2237. {
  2238. int i;
  2239. struct kmem_cache_node *n;
  2240. int rc = __cache_shrink(cachep);
  2241. if (rc)
  2242. return rc;
  2243. for_each_online_cpu(i)
  2244. kfree(cachep->array[i]);
  2245. /* NUMA: free the node structures */
  2246. for_each_online_node(i) {
  2247. n = cachep->node[i];
  2248. if (n) {
  2249. kfree(n->shared);
  2250. free_alien_cache(n->alien);
  2251. kfree(n);
  2252. }
  2253. }
  2254. return 0;
  2255. }
  2256. /*
  2257. * Get the memory for a slab management obj.
  2258. * For a slab cache when the slab descriptor is off-slab, slab descriptors
  2259. * always come from malloc_sizes caches. The slab descriptor cannot
  2260. * come from the same cache which is getting created because,
  2261. * when we are searching for an appropriate cache for these
  2262. * descriptors in kmem_cache_create, we search through the malloc_sizes array.
  2263. * If we are creating a malloc_sizes cache here it would not be visible to
  2264. * kmem_find_general_cachep till the initialization is complete.
  2265. * Hence we cannot have slabp_cache same as the original cache.
  2266. */
  2267. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2268. int colour_off, gfp_t local_flags,
  2269. int nodeid)
  2270. {
  2271. struct slab *slabp;
  2272. if (OFF_SLAB(cachep)) {
  2273. /* Slab management obj is off-slab. */
  2274. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  2275. local_flags, nodeid);
  2276. /*
  2277. * If the first object in the slab is leaked (it's allocated
  2278. * but no one has a reference to it), we want to make sure
  2279. * kmemleak does not treat the ->s_mem pointer as a reference
  2280. * to the object. Otherwise we will not report the leak.
  2281. */
  2282. kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  2283. local_flags);
  2284. if (!slabp)
  2285. return NULL;
  2286. } else {
  2287. slabp = objp + colour_off;
  2288. colour_off += cachep->slab_size;
  2289. }
  2290. slabp->inuse = 0;
  2291. slabp->colouroff = colour_off;
  2292. slabp->s_mem = objp + colour_off;
  2293. slabp->nodeid = nodeid;
  2294. slabp->free = 0;
  2295. return slabp;
  2296. }
  2297. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2298. {
  2299. return (kmem_bufctl_t *) (slabp + 1);
  2300. }
  2301. static void cache_init_objs(struct kmem_cache *cachep,
  2302. struct slab *slabp)
  2303. {
  2304. int i;
  2305. for (i = 0; i < cachep->num; i++) {
  2306. void *objp = index_to_obj(cachep, slabp, i);
  2307. #if DEBUG
  2308. /* need to poison the objs? */
  2309. if (cachep->flags & SLAB_POISON)
  2310. poison_obj(cachep, objp, POISON_FREE);
  2311. if (cachep->flags & SLAB_STORE_USER)
  2312. *dbg_userword(cachep, objp) = NULL;
  2313. if (cachep->flags & SLAB_RED_ZONE) {
  2314. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2315. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2316. }
  2317. /*
  2318. * Constructors are not allowed to allocate memory from the same
  2319. * cache which they are a constructor for. Otherwise, deadlock.
  2320. * They must also be threaded.
  2321. */
  2322. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2323. cachep->ctor(objp + obj_offset(cachep));
  2324. if (cachep->flags & SLAB_RED_ZONE) {
  2325. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2326. slab_error(cachep, "constructor overwrote the"
  2327. " end of an object");
  2328. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2329. slab_error(cachep, "constructor overwrote the"
  2330. " start of an object");
  2331. }
  2332. if ((cachep->size % PAGE_SIZE) == 0 &&
  2333. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2334. kernel_map_pages(virt_to_page(objp),
  2335. cachep->size / PAGE_SIZE, 0);
  2336. #else
  2337. if (cachep->ctor)
  2338. cachep->ctor(objp);
  2339. #endif
  2340. slab_bufctl(slabp)[i] = i + 1;
  2341. }
  2342. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2343. }
  2344. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2345. {
  2346. if (CONFIG_ZONE_DMA_FLAG) {
  2347. if (flags & GFP_DMA)
  2348. BUG_ON(!(cachep->allocflags & GFP_DMA));
  2349. else
  2350. BUG_ON(cachep->allocflags & GFP_DMA);
  2351. }
  2352. }
  2353. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2354. int nodeid)
  2355. {
  2356. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2357. kmem_bufctl_t next;
  2358. slabp->inuse++;
  2359. next = slab_bufctl(slabp)[slabp->free];
  2360. #if DEBUG
  2361. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2362. WARN_ON(slabp->nodeid != nodeid);
  2363. #endif
  2364. slabp->free = next;
  2365. return objp;
  2366. }
  2367. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2368. void *objp, int nodeid)
  2369. {
  2370. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2371. #if DEBUG
  2372. /* Verify that the slab belongs to the intended node */
  2373. WARN_ON(slabp->nodeid != nodeid);
  2374. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  2375. printk(KERN_ERR "slab: double free detected in cache "
  2376. "'%s', objp %p\n", cachep->name, objp);
  2377. BUG();
  2378. }
  2379. #endif
  2380. slab_bufctl(slabp)[objnr] = slabp->free;
  2381. slabp->free = objnr;
  2382. slabp->inuse--;
  2383. }
  2384. /*
  2385. * Map pages beginning at addr to the given cache and slab. This is required
  2386. * for the slab allocator to be able to lookup the cache and slab of a
  2387. * virtual address for kfree, ksize, and slab debugging.
  2388. */
  2389. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  2390. void *addr)
  2391. {
  2392. int nr_pages;
  2393. struct page *page;
  2394. page = virt_to_page(addr);
  2395. nr_pages = 1;
  2396. if (likely(!PageCompound(page)))
  2397. nr_pages <<= cache->gfporder;
  2398. do {
  2399. page->slab_cache = cache;
  2400. page->slab_page = slab;
  2401. page++;
  2402. } while (--nr_pages);
  2403. }
  2404. /*
  2405. * Grow (by 1) the number of slabs within a cache. This is called by
  2406. * kmem_cache_alloc() when there are no active objs left in a cache.
  2407. */
  2408. static int cache_grow(struct kmem_cache *cachep,
  2409. gfp_t flags, int nodeid, void *objp)
  2410. {
  2411. struct slab *slabp;
  2412. size_t offset;
  2413. gfp_t local_flags;
  2414. struct kmem_cache_node *n;
  2415. /*
  2416. * Be lazy and only check for valid flags here, keeping it out of the
  2417. * critical path in kmem_cache_alloc().
  2418. */
  2419. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  2420. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2421. /* Take the node list lock to change the colour_next on this node */
  2422. check_irq_off();
  2423. n = cachep->node[nodeid];
  2424. spin_lock(&n->list_lock);
  2425. /* Get colour for the slab, and cal the next value. */
  2426. offset = n->colour_next;
  2427. n->colour_next++;
  2428. if (n->colour_next >= cachep->colour)
  2429. n->colour_next = 0;
  2430. spin_unlock(&n->list_lock);
  2431. offset *= cachep->colour_off;
  2432. if (local_flags & __GFP_WAIT)
  2433. local_irq_enable();
  2434. /*
  2435. * The test for missing atomic flag is performed here, rather than
  2436. * the more obvious place, simply to reduce the critical path length
  2437. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2438. * will eventually be caught here (where it matters).
  2439. */
  2440. kmem_flagcheck(cachep, flags);
  2441. /*
  2442. * Get mem for the objs. Attempt to allocate a physical page from
  2443. * 'nodeid'.
  2444. */
  2445. if (!objp)
  2446. objp = kmem_getpages(cachep, local_flags, nodeid);
  2447. if (!objp)
  2448. goto failed;
  2449. /* Get slab management. */
  2450. slabp = alloc_slabmgmt(cachep, objp, offset,
  2451. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  2452. if (!slabp)
  2453. goto opps1;
  2454. slab_map_pages(cachep, slabp, objp);
  2455. cache_init_objs(cachep, slabp);
  2456. if (local_flags & __GFP_WAIT)
  2457. local_irq_disable();
  2458. check_irq_off();
  2459. spin_lock(&n->list_lock);
  2460. /* Make slab active. */
  2461. list_add_tail(&slabp->list, &(n->slabs_free));
  2462. STATS_INC_GROWN(cachep);
  2463. n->free_objects += cachep->num;
  2464. spin_unlock(&n->list_lock);
  2465. return 1;
  2466. opps1:
  2467. kmem_freepages(cachep, objp);
  2468. failed:
  2469. if (local_flags & __GFP_WAIT)
  2470. local_irq_disable();
  2471. return 0;
  2472. }
  2473. #if DEBUG
  2474. /*
  2475. * Perform extra freeing checks:
  2476. * - detect bad pointers.
  2477. * - POISON/RED_ZONE checking
  2478. */
  2479. static void kfree_debugcheck(const void *objp)
  2480. {
  2481. if (!virt_addr_valid(objp)) {
  2482. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2483. (unsigned long)objp);
  2484. BUG();
  2485. }
  2486. }
  2487. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2488. {
  2489. unsigned long long redzone1, redzone2;
  2490. redzone1 = *dbg_redzone1(cache, obj);
  2491. redzone2 = *dbg_redzone2(cache, obj);
  2492. /*
  2493. * Redzone is ok.
  2494. */
  2495. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2496. return;
  2497. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2498. slab_error(cache, "double free detected");
  2499. else
  2500. slab_error(cache, "memory outside object was overwritten");
  2501. printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
  2502. obj, redzone1, redzone2);
  2503. }
  2504. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2505. unsigned long caller)
  2506. {
  2507. struct page *page;
  2508. unsigned int objnr;
  2509. struct slab *slabp;
  2510. BUG_ON(virt_to_cache(objp) != cachep);
  2511. objp -= obj_offset(cachep);
  2512. kfree_debugcheck(objp);
  2513. page = virt_to_head_page(objp);
  2514. slabp = page->slab_page;
  2515. if (cachep->flags & SLAB_RED_ZONE) {
  2516. verify_redzone_free(cachep, objp);
  2517. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2518. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2519. }
  2520. if (cachep->flags & SLAB_STORE_USER)
  2521. *dbg_userword(cachep, objp) = (void *)caller;
  2522. objnr = obj_to_index(cachep, slabp, objp);
  2523. BUG_ON(objnr >= cachep->num);
  2524. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2525. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2526. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2527. #endif
  2528. if (cachep->flags & SLAB_POISON) {
  2529. #ifdef CONFIG_DEBUG_PAGEALLOC
  2530. if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2531. store_stackinfo(cachep, objp, caller);
  2532. kernel_map_pages(virt_to_page(objp),
  2533. cachep->size / PAGE_SIZE, 0);
  2534. } else {
  2535. poison_obj(cachep, objp, POISON_FREE);
  2536. }
  2537. #else
  2538. poison_obj(cachep, objp, POISON_FREE);
  2539. #endif
  2540. }
  2541. return objp;
  2542. }
  2543. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2544. {
  2545. kmem_bufctl_t i;
  2546. int entries = 0;
  2547. /* Check slab's freelist to see if this obj is there. */
  2548. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2549. entries++;
  2550. if (entries > cachep->num || i >= cachep->num)
  2551. goto bad;
  2552. }
  2553. if (entries != cachep->num - slabp->inuse) {
  2554. bad:
  2555. printk(KERN_ERR "slab: Internal list corruption detected in "
  2556. "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
  2557. cachep->name, cachep->num, slabp, slabp->inuse,
  2558. print_tainted());
  2559. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  2560. sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  2561. 1);
  2562. BUG();
  2563. }
  2564. }
  2565. #else
  2566. #define kfree_debugcheck(x) do { } while(0)
  2567. #define cache_free_debugcheck(x,objp,z) (objp)
  2568. #define check_slabp(x,y) do { } while(0)
  2569. #endif
  2570. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
  2571. bool force_refill)
  2572. {
  2573. int batchcount;
  2574. struct kmem_cache_node *n;
  2575. struct array_cache *ac;
  2576. int node;
  2577. check_irq_off();
  2578. node = numa_mem_id();
  2579. if (unlikely(force_refill))
  2580. goto force_grow;
  2581. retry:
  2582. ac = cpu_cache_get(cachep);
  2583. batchcount = ac->batchcount;
  2584. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2585. /*
  2586. * If there was little recent activity on this cache, then
  2587. * perform only a partial refill. Otherwise we could generate
  2588. * refill bouncing.
  2589. */
  2590. batchcount = BATCHREFILL_LIMIT;
  2591. }
  2592. n = cachep->node[node];
  2593. BUG_ON(ac->avail > 0 || !n);
  2594. spin_lock(&n->list_lock);
  2595. /* See if we can refill from the shared array */
  2596. if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
  2597. n->shared->touched = 1;
  2598. goto alloc_done;
  2599. }
  2600. while (batchcount > 0) {
  2601. struct list_head *entry;
  2602. struct slab *slabp;
  2603. /* Get slab alloc is to come from. */
  2604. entry = n->slabs_partial.next;
  2605. if (entry == &n->slabs_partial) {
  2606. n->free_touched = 1;
  2607. entry = n->slabs_free.next;
  2608. if (entry == &n->slabs_free)
  2609. goto must_grow;
  2610. }
  2611. slabp = list_entry(entry, struct slab, list);
  2612. check_slabp(cachep, slabp);
  2613. check_spinlock_acquired(cachep);
  2614. /*
  2615. * The slab was either on partial or free list so
  2616. * there must be at least one object available for
  2617. * allocation.
  2618. */
  2619. BUG_ON(slabp->inuse >= cachep->num);
  2620. while (slabp->inuse < cachep->num && batchcount--) {
  2621. STATS_INC_ALLOCED(cachep);
  2622. STATS_INC_ACTIVE(cachep);
  2623. STATS_SET_HIGH(cachep);
  2624. ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
  2625. node));
  2626. }
  2627. check_slabp(cachep, slabp);
  2628. /* move slabp to correct slabp list: */
  2629. list_del(&slabp->list);
  2630. if (slabp->free == BUFCTL_END)
  2631. list_add(&slabp->list, &n->slabs_full);
  2632. else
  2633. list_add(&slabp->list, &n->slabs_partial);
  2634. }
  2635. must_grow:
  2636. n->free_objects -= ac->avail;
  2637. alloc_done:
  2638. spin_unlock(&n->list_lock);
  2639. if (unlikely(!ac->avail)) {
  2640. int x;
  2641. force_grow:
  2642. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2643. /* cache_grow can reenable interrupts, then ac could change. */
  2644. ac = cpu_cache_get(cachep);
  2645. node = numa_mem_id();
  2646. /* no objects in sight? abort */
  2647. if (!x && (ac->avail == 0 || force_refill))
  2648. return NULL;
  2649. if (!ac->avail) /* objects refilled by interrupt? */
  2650. goto retry;
  2651. }
  2652. ac->touched = 1;
  2653. return ac_get_obj(cachep, ac, flags, force_refill);
  2654. }
  2655. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2656. gfp_t flags)
  2657. {
  2658. might_sleep_if(flags & __GFP_WAIT);
  2659. #if DEBUG
  2660. kmem_flagcheck(cachep, flags);
  2661. #endif
  2662. }
  2663. #if DEBUG
  2664. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2665. gfp_t flags, void *objp, unsigned long caller)
  2666. {
  2667. if (!objp)
  2668. return objp;
  2669. if (cachep->flags & SLAB_POISON) {
  2670. #ifdef CONFIG_DEBUG_PAGEALLOC
  2671. if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2672. kernel_map_pages(virt_to_page(objp),
  2673. cachep->size / PAGE_SIZE, 1);
  2674. else
  2675. check_poison_obj(cachep, objp);
  2676. #else
  2677. check_poison_obj(cachep, objp);
  2678. #endif
  2679. poison_obj(cachep, objp, POISON_INUSE);
  2680. }
  2681. if (cachep->flags & SLAB_STORE_USER)
  2682. *dbg_userword(cachep, objp) = (void *)caller;
  2683. if (cachep->flags & SLAB_RED_ZONE) {
  2684. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2685. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2686. slab_error(cachep, "double free, or memory outside"
  2687. " object was overwritten");
  2688. printk(KERN_ERR
  2689. "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2690. objp, *dbg_redzone1(cachep, objp),
  2691. *dbg_redzone2(cachep, objp));
  2692. }
  2693. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2694. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2695. }
  2696. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2697. {
  2698. struct slab *slabp;
  2699. unsigned objnr;
  2700. slabp = virt_to_head_page(objp)->slab_page;
  2701. objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
  2702. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2703. }
  2704. #endif
  2705. objp += obj_offset(cachep);
  2706. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2707. cachep->ctor(objp);
  2708. if (ARCH_SLAB_MINALIGN &&
  2709. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2710. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2711. objp, (int)ARCH_SLAB_MINALIGN);
  2712. }
  2713. return objp;
  2714. }
  2715. #else
  2716. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2717. #endif
  2718. static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
  2719. {
  2720. if (cachep == kmem_cache)
  2721. return false;
  2722. return should_failslab(cachep->object_size, flags, cachep->flags);
  2723. }
  2724. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2725. {
  2726. void *objp;
  2727. struct array_cache *ac;
  2728. bool force_refill = false;
  2729. check_irq_off();
  2730. ac = cpu_cache_get(cachep);
  2731. if (likely(ac->avail)) {
  2732. ac->touched = 1;
  2733. objp = ac_get_obj(cachep, ac, flags, false);
  2734. /*
  2735. * Allow for the possibility all avail objects are not allowed
  2736. * by the current flags
  2737. */
  2738. if (objp) {
  2739. STATS_INC_ALLOCHIT(cachep);
  2740. goto out;
  2741. }
  2742. force_refill = true;
  2743. }
  2744. STATS_INC_ALLOCMISS(cachep);
  2745. objp = cache_alloc_refill(cachep, flags, force_refill);
  2746. /*
  2747. * the 'ac' may be updated by cache_alloc_refill(),
  2748. * and kmemleak_erase() requires its correct value.
  2749. */
  2750. ac = cpu_cache_get(cachep);
  2751. out:
  2752. /*
  2753. * To avoid a false negative, if an object that is in one of the
  2754. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  2755. * treat the array pointers as a reference to the object.
  2756. */
  2757. if (objp)
  2758. kmemleak_erase(&ac->entry[ac->avail]);
  2759. return objp;
  2760. }
  2761. #ifdef CONFIG_NUMA
  2762. /*
  2763. * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
  2764. *
  2765. * If we are in_interrupt, then process context, including cpusets and
  2766. * mempolicy, may not apply and should not be used for allocation policy.
  2767. */
  2768. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2769. {
  2770. int nid_alloc, nid_here;
  2771. if (in_interrupt() || (flags & __GFP_THISNODE))
  2772. return NULL;
  2773. nid_alloc = nid_here = numa_mem_id();
  2774. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2775. nid_alloc = cpuset_slab_spread_node();
  2776. else if (current->mempolicy)
  2777. nid_alloc = slab_node();
  2778. if (nid_alloc != nid_here)
  2779. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2780. return NULL;
  2781. }
  2782. /*
  2783. * Fallback function if there was no memory available and no objects on a
  2784. * certain node and fall back is permitted. First we scan all the
  2785. * available node for available objects. If that fails then we
  2786. * perform an allocation without specifying a node. This allows the page
  2787. * allocator to do its reclaim / fallback magic. We then insert the
  2788. * slab into the proper nodelist and then allocate from it.
  2789. */
  2790. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2791. {
  2792. struct zonelist *zonelist;
  2793. gfp_t local_flags;
  2794. struct zoneref *z;
  2795. struct zone *zone;
  2796. enum zone_type high_zoneidx = gfp_zone(flags);
  2797. void *obj = NULL;
  2798. int nid;
  2799. unsigned int cpuset_mems_cookie;
  2800. if (flags & __GFP_THISNODE)
  2801. return NULL;
  2802. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2803. retry_cpuset:
  2804. cpuset_mems_cookie = get_mems_allowed();
  2805. zonelist = node_zonelist(slab_node(), flags);
  2806. retry:
  2807. /*
  2808. * Look through allowed nodes for objects available
  2809. * from existing per node queues.
  2810. */
  2811. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  2812. nid = zone_to_nid(zone);
  2813. if (cpuset_zone_allowed_hardwall(zone, flags) &&
  2814. cache->node[nid] &&
  2815. cache->node[nid]->free_objects) {
  2816. obj = ____cache_alloc_node(cache,
  2817. flags | GFP_THISNODE, nid);
  2818. if (obj)
  2819. break;
  2820. }
  2821. }
  2822. if (!obj) {
  2823. /*
  2824. * This allocation will be performed within the constraints
  2825. * of the current cpuset / memory policy requirements.
  2826. * We may trigger various forms of reclaim on the allowed
  2827. * set and go into memory reserves if necessary.
  2828. */
  2829. if (local_flags & __GFP_WAIT)
  2830. local_irq_enable();
  2831. kmem_flagcheck(cache, flags);
  2832. obj = kmem_getpages(cache, local_flags, numa_mem_id());
  2833. if (local_flags & __GFP_WAIT)
  2834. local_irq_disable();
  2835. if (obj) {
  2836. /*
  2837. * Insert into the appropriate per node queues
  2838. */
  2839. nid = page_to_nid(virt_to_page(obj));
  2840. if (cache_grow(cache, flags, nid, obj)) {
  2841. obj = ____cache_alloc_node(cache,
  2842. flags | GFP_THISNODE, nid);
  2843. if (!obj)
  2844. /*
  2845. * Another processor may allocate the
  2846. * objects in the slab since we are
  2847. * not holding any locks.
  2848. */
  2849. goto retry;
  2850. } else {
  2851. /* cache_grow already freed obj */
  2852. obj = NULL;
  2853. }
  2854. }
  2855. }
  2856. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  2857. goto retry_cpuset;
  2858. return obj;
  2859. }
  2860. /*
  2861. * A interface to enable slab creation on nodeid
  2862. */
  2863. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2864. int nodeid)
  2865. {
  2866. struct list_head *entry;
  2867. struct slab *slabp;
  2868. struct kmem_cache_node *n;
  2869. void *obj;
  2870. int x;
  2871. VM_BUG_ON(nodeid > num_online_nodes());
  2872. n = cachep->node[nodeid];
  2873. BUG_ON(!n);
  2874. retry:
  2875. check_irq_off();
  2876. spin_lock(&n->list_lock);
  2877. entry = n->slabs_partial.next;
  2878. if (entry == &n->slabs_partial) {
  2879. n->free_touched = 1;
  2880. entry = n->slabs_free.next;
  2881. if (entry == &n->slabs_free)
  2882. goto must_grow;
  2883. }
  2884. slabp = list_entry(entry, struct slab, list);
  2885. check_spinlock_acquired_node(cachep, nodeid);
  2886. check_slabp(cachep, slabp);
  2887. STATS_INC_NODEALLOCS(cachep);
  2888. STATS_INC_ACTIVE(cachep);
  2889. STATS_SET_HIGH(cachep);
  2890. BUG_ON(slabp->inuse == cachep->num);
  2891. obj = slab_get_obj(cachep, slabp, nodeid);
  2892. check_slabp(cachep, slabp);
  2893. n->free_objects--;
  2894. /* move slabp to correct slabp list: */
  2895. list_del(&slabp->list);
  2896. if (slabp->free == BUFCTL_END)
  2897. list_add(&slabp->list, &n->slabs_full);
  2898. else
  2899. list_add(&slabp->list, &n->slabs_partial);
  2900. spin_unlock(&n->list_lock);
  2901. goto done;
  2902. must_grow:
  2903. spin_unlock(&n->list_lock);
  2904. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  2905. if (x)
  2906. goto retry;
  2907. return fallback_alloc(cachep, flags);
  2908. done:
  2909. return obj;
  2910. }
  2911. static __always_inline void *
  2912. slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  2913. unsigned long caller)
  2914. {
  2915. unsigned long save_flags;
  2916. void *ptr;
  2917. int slab_node = numa_mem_id();
  2918. flags &= gfp_allowed_mask;
  2919. lockdep_trace_alloc(flags);
  2920. if (slab_should_failslab(cachep, flags))
  2921. return NULL;
  2922. cachep = memcg_kmem_get_cache(cachep, flags);
  2923. cache_alloc_debugcheck_before(cachep, flags);
  2924. local_irq_save(save_flags);
  2925. if (nodeid == NUMA_NO_NODE)
  2926. nodeid = slab_node;
  2927. if (unlikely(!cachep->node[nodeid])) {
  2928. /* Node not bootstrapped yet */
  2929. ptr = fallback_alloc(cachep, flags);
  2930. goto out;
  2931. }
  2932. if (nodeid == slab_node) {
  2933. /*
  2934. * Use the locally cached objects if possible.
  2935. * However ____cache_alloc does not allow fallback
  2936. * to other nodes. It may fail while we still have
  2937. * objects on other nodes available.
  2938. */
  2939. ptr = ____cache_alloc(cachep, flags);
  2940. if (ptr)
  2941. goto out;
  2942. }
  2943. /* ___cache_alloc_node can fall back to other nodes */
  2944. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  2945. out:
  2946. local_irq_restore(save_flags);
  2947. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  2948. kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
  2949. flags);
  2950. if (likely(ptr))
  2951. kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
  2952. if (unlikely((flags & __GFP_ZERO) && ptr))
  2953. memset(ptr, 0, cachep->object_size);
  2954. return ptr;
  2955. }
  2956. static __always_inline void *
  2957. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  2958. {
  2959. void *objp;
  2960. if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
  2961. objp = alternate_node_alloc(cache, flags);
  2962. if (objp)
  2963. goto out;
  2964. }
  2965. objp = ____cache_alloc(cache, flags);
  2966. /*
  2967. * We may just have run out of memory on the local node.
  2968. * ____cache_alloc_node() knows how to locate memory on other nodes
  2969. */
  2970. if (!objp)
  2971. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  2972. out:
  2973. return objp;
  2974. }
  2975. #else
  2976. static __always_inline void *
  2977. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2978. {
  2979. return ____cache_alloc(cachep, flags);
  2980. }
  2981. #endif /* CONFIG_NUMA */
  2982. static __always_inline void *
  2983. slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
  2984. {
  2985. unsigned long save_flags;
  2986. void *objp;
  2987. flags &= gfp_allowed_mask;
  2988. lockdep_trace_alloc(flags);
  2989. if (slab_should_failslab(cachep, flags))
  2990. return NULL;
  2991. cachep = memcg_kmem_get_cache(cachep, flags);
  2992. cache_alloc_debugcheck_before(cachep, flags);
  2993. local_irq_save(save_flags);
  2994. objp = __do_cache_alloc(cachep, flags);
  2995. local_irq_restore(save_flags);
  2996. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  2997. kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
  2998. flags);
  2999. prefetchw(objp);
  3000. if (likely(objp))
  3001. kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
  3002. if (unlikely((flags & __GFP_ZERO) && objp))
  3003. memset(objp, 0, cachep->object_size);
  3004. return objp;
  3005. }
  3006. /*
  3007. * Caller needs to acquire correct kmem_list's list_lock
  3008. */
  3009. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  3010. int node)
  3011. {
  3012. int i;
  3013. struct kmem_cache_node *n;
  3014. for (i = 0; i < nr_objects; i++) {
  3015. void *objp;
  3016. struct slab *slabp;
  3017. clear_obj_pfmemalloc(&objpp[i]);
  3018. objp = objpp[i];
  3019. slabp = virt_to_slab(objp);
  3020. n = cachep->node[node];
  3021. list_del(&slabp->list);
  3022. check_spinlock_acquired_node(cachep, node);
  3023. check_slabp(cachep, slabp);
  3024. slab_put_obj(cachep, slabp, objp, node);
  3025. STATS_DEC_ACTIVE(cachep);
  3026. n->free_objects++;
  3027. check_slabp(cachep, slabp);
  3028. /* fixup slab chains */
  3029. if (slabp->inuse == 0) {
  3030. if (n->free_objects > n->free_limit) {
  3031. n->free_objects -= cachep->num;
  3032. /* No need to drop any previously held
  3033. * lock here, even if we have a off-slab slab
  3034. * descriptor it is guaranteed to come from
  3035. * a different cache, refer to comments before
  3036. * alloc_slabmgmt.
  3037. */
  3038. slab_destroy(cachep, slabp);
  3039. } else {
  3040. list_add(&slabp->list, &n->slabs_free);
  3041. }
  3042. } else {
  3043. /* Unconditionally move a slab to the end of the
  3044. * partial list on free - maximum time for the
  3045. * other objects to be freed, too.
  3046. */
  3047. list_add_tail(&slabp->list, &n->slabs_partial);
  3048. }
  3049. }
  3050. }
  3051. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  3052. {
  3053. int batchcount;
  3054. struct kmem_cache_node *n;
  3055. int node = numa_mem_id();
  3056. batchcount = ac->batchcount;
  3057. #if DEBUG
  3058. BUG_ON(!batchcount || batchcount > ac->avail);
  3059. #endif
  3060. check_irq_off();
  3061. n = cachep->node[node];
  3062. spin_lock(&n->list_lock);
  3063. if (n->shared) {
  3064. struct array_cache *shared_array = n->shared;
  3065. int max = shared_array->limit - shared_array->avail;
  3066. if (max) {
  3067. if (batchcount > max)
  3068. batchcount = max;
  3069. memcpy(&(shared_array->entry[shared_array->avail]),
  3070. ac->entry, sizeof(void *) * batchcount);
  3071. shared_array->avail += batchcount;
  3072. goto free_done;
  3073. }
  3074. }
  3075. free_block(cachep, ac->entry, batchcount, node);
  3076. free_done:
  3077. #if STATS
  3078. {
  3079. int i = 0;
  3080. struct list_head *p;
  3081. p = n->slabs_free.next;
  3082. while (p != &(n->slabs_free)) {
  3083. struct slab *slabp;
  3084. slabp = list_entry(p, struct slab, list);
  3085. BUG_ON(slabp->inuse);
  3086. i++;
  3087. p = p->next;
  3088. }
  3089. STATS_SET_FREEABLE(cachep, i);
  3090. }
  3091. #endif
  3092. spin_unlock(&n->list_lock);
  3093. ac->avail -= batchcount;
  3094. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  3095. }
  3096. /*
  3097. * Release an obj back to its cache. If the obj has a constructed state, it must
  3098. * be in this state _before_ it is released. Called with disabled ints.
  3099. */
  3100. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  3101. unsigned long caller)
  3102. {
  3103. struct array_cache *ac = cpu_cache_get(cachep);
  3104. check_irq_off();
  3105. kmemleak_free_recursive(objp, cachep->flags);
  3106. objp = cache_free_debugcheck(cachep, objp, caller);
  3107. kmemcheck_slab_free(cachep, objp, cachep->object_size);
  3108. /*
  3109. * Skip calling cache_free_alien() when the platform is not numa.
  3110. * This will avoid cache misses that happen while accessing slabp (which
  3111. * is per page memory reference) to get nodeid. Instead use a global
  3112. * variable to skip the call, which is mostly likely to be present in
  3113. * the cache.
  3114. */
  3115. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  3116. return;
  3117. if (likely(ac->avail < ac->limit)) {
  3118. STATS_INC_FREEHIT(cachep);
  3119. } else {
  3120. STATS_INC_FREEMISS(cachep);
  3121. cache_flusharray(cachep, ac);
  3122. }
  3123. ac_put_obj(cachep, ac, objp);
  3124. }
  3125. /**
  3126. * kmem_cache_alloc - Allocate an object
  3127. * @cachep: The cache to allocate from.
  3128. * @flags: See kmalloc().
  3129. *
  3130. * Allocate an object from this cache. The flags are only relevant
  3131. * if the cache has no available objects.
  3132. */
  3133. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3134. {
  3135. void *ret = slab_alloc(cachep, flags, _RET_IP_);
  3136. trace_kmem_cache_alloc(_RET_IP_, ret,
  3137. cachep->object_size, cachep->size, flags);
  3138. return ret;
  3139. }
  3140. EXPORT_SYMBOL(kmem_cache_alloc);
  3141. #ifdef CONFIG_TRACING
  3142. void *
  3143. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  3144. {
  3145. void *ret;
  3146. ret = slab_alloc(cachep, flags, _RET_IP_);
  3147. trace_kmalloc(_RET_IP_, ret,
  3148. size, cachep->size, flags);
  3149. return ret;
  3150. }
  3151. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  3152. #endif
  3153. #ifdef CONFIG_NUMA
  3154. /**
  3155. * kmem_cache_alloc_node - Allocate an object on the specified node
  3156. * @cachep: The cache to allocate from.
  3157. * @flags: See kmalloc().
  3158. * @nodeid: node number of the target node.
  3159. *
  3160. * Identical to kmem_cache_alloc but it will allocate memory on the given
  3161. * node, which can improve the performance for cpu bound structures.
  3162. *
  3163. * Fallback to other node is possible if __GFP_THISNODE is not set.
  3164. */
  3165. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  3166. {
  3167. void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  3168. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  3169. cachep->object_size, cachep->size,
  3170. flags, nodeid);
  3171. return ret;
  3172. }
  3173. EXPORT_SYMBOL(kmem_cache_alloc_node);
  3174. #ifdef CONFIG_TRACING
  3175. void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  3176. gfp_t flags,
  3177. int nodeid,
  3178. size_t size)
  3179. {
  3180. void *ret;
  3181. ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  3182. trace_kmalloc_node(_RET_IP_, ret,
  3183. size, cachep->size,
  3184. flags, nodeid);
  3185. return ret;
  3186. }
  3187. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  3188. #endif
  3189. static __always_inline void *
  3190. __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
  3191. {
  3192. struct kmem_cache *cachep;
  3193. cachep = kmalloc_slab(size, flags);
  3194. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3195. return cachep;
  3196. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  3197. }
  3198. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3199. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3200. {
  3201. return __do_kmalloc_node(size, flags, node, _RET_IP_);
  3202. }
  3203. EXPORT_SYMBOL(__kmalloc_node);
  3204. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3205. int node, unsigned long caller)
  3206. {
  3207. return __do_kmalloc_node(size, flags, node, caller);
  3208. }
  3209. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3210. #else
  3211. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3212. {
  3213. return __do_kmalloc_node(size, flags, node, 0);
  3214. }
  3215. EXPORT_SYMBOL(__kmalloc_node);
  3216. #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
  3217. #endif /* CONFIG_NUMA */
  3218. /**
  3219. * __do_kmalloc - allocate memory
  3220. * @size: how many bytes of memory are required.
  3221. * @flags: the type of memory to allocate (see kmalloc).
  3222. * @caller: function caller for debug tracking of the caller
  3223. */
  3224. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3225. unsigned long caller)
  3226. {
  3227. struct kmem_cache *cachep;
  3228. void *ret;
  3229. /* If you want to save a few bytes .text space: replace
  3230. * __ with kmem_.
  3231. * Then kmalloc uses the uninlined functions instead of the inline
  3232. * functions.
  3233. */
  3234. cachep = kmalloc_slab(size, flags);
  3235. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3236. return cachep;
  3237. ret = slab_alloc(cachep, flags, caller);
  3238. trace_kmalloc(caller, ret,
  3239. size, cachep->size, flags);
  3240. return ret;
  3241. }
  3242. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3243. void *__kmalloc(size_t size, gfp_t flags)
  3244. {
  3245. return __do_kmalloc(size, flags, _RET_IP_);
  3246. }
  3247. EXPORT_SYMBOL(__kmalloc);
  3248. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  3249. {
  3250. return __do_kmalloc(size, flags, caller);
  3251. }
  3252. EXPORT_SYMBOL(__kmalloc_track_caller);
  3253. #else
  3254. void *__kmalloc(size_t size, gfp_t flags)
  3255. {
  3256. return __do_kmalloc(size, flags, 0);
  3257. }
  3258. EXPORT_SYMBOL(__kmalloc);
  3259. #endif
  3260. /**
  3261. * kmem_cache_free - Deallocate an object
  3262. * @cachep: The cache the allocation was from.
  3263. * @objp: The previously allocated object.
  3264. *
  3265. * Free an object which was previously allocated from this
  3266. * cache.
  3267. */
  3268. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3269. {
  3270. unsigned long flags;
  3271. cachep = cache_from_obj(cachep, objp);
  3272. if (!cachep)
  3273. return;
  3274. local_irq_save(flags);
  3275. debug_check_no_locks_freed(objp, cachep->object_size);
  3276. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  3277. debug_check_no_obj_freed(objp, cachep->object_size);
  3278. __cache_free(cachep, objp, _RET_IP_);
  3279. local_irq_restore(flags);
  3280. trace_kmem_cache_free(_RET_IP_, objp);
  3281. }
  3282. EXPORT_SYMBOL(kmem_cache_free);
  3283. /**
  3284. * kfree - free previously allocated memory
  3285. * @objp: pointer returned by kmalloc.
  3286. *
  3287. * If @objp is NULL, no operation is performed.
  3288. *
  3289. * Don't free memory not originally allocated by kmalloc()
  3290. * or you will run into trouble.
  3291. */
  3292. void kfree(const void *objp)
  3293. {
  3294. struct kmem_cache *c;
  3295. unsigned long flags;
  3296. trace_kfree(_RET_IP_, objp);
  3297. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  3298. return;
  3299. local_irq_save(flags);
  3300. kfree_debugcheck(objp);
  3301. c = virt_to_cache(objp);
  3302. debug_check_no_locks_freed(objp, c->object_size);
  3303. debug_check_no_obj_freed(objp, c->object_size);
  3304. __cache_free(c, (void *)objp, _RET_IP_);
  3305. local_irq_restore(flags);
  3306. }
  3307. EXPORT_SYMBOL(kfree);
  3308. /*
  3309. * This initializes kmem_cache_node or resizes various caches for all nodes.
  3310. */
  3311. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
  3312. {
  3313. int node;
  3314. struct kmem_cache_node *n;
  3315. struct array_cache *new_shared;
  3316. struct array_cache **new_alien = NULL;
  3317. for_each_online_node(node) {
  3318. if (use_alien_caches) {
  3319. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  3320. if (!new_alien)
  3321. goto fail;
  3322. }
  3323. new_shared = NULL;
  3324. if (cachep->shared) {
  3325. new_shared = alloc_arraycache(node,
  3326. cachep->shared*cachep->batchcount,
  3327. 0xbaadf00d, gfp);
  3328. if (!new_shared) {
  3329. free_alien_cache(new_alien);
  3330. goto fail;
  3331. }
  3332. }
  3333. n = cachep->node[node];
  3334. if (n) {
  3335. struct array_cache *shared = n->shared;
  3336. spin_lock_irq(&n->list_lock);
  3337. if (shared)
  3338. free_block(cachep, shared->entry,
  3339. shared->avail, node);
  3340. n->shared = new_shared;
  3341. if (!n->alien) {
  3342. n->alien = new_alien;
  3343. new_alien = NULL;
  3344. }
  3345. n->free_limit = (1 + nr_cpus_node(node)) *
  3346. cachep->batchcount + cachep->num;
  3347. spin_unlock_irq(&n->list_lock);
  3348. kfree(shared);
  3349. free_alien_cache(new_alien);
  3350. continue;
  3351. }
  3352. n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
  3353. if (!n) {
  3354. free_alien_cache(new_alien);
  3355. kfree(new_shared);
  3356. goto fail;
  3357. }
  3358. kmem_cache_node_init(n);
  3359. n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3360. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3361. n->shared = new_shared;
  3362. n->alien = new_alien;
  3363. n->free_limit = (1 + nr_cpus_node(node)) *
  3364. cachep->batchcount + cachep->num;
  3365. cachep->node[node] = n;
  3366. }
  3367. return 0;
  3368. fail:
  3369. if (!cachep->list.next) {
  3370. /* Cache is not active yet. Roll back what we did */
  3371. node--;
  3372. while (node >= 0) {
  3373. if (cachep->node[node]) {
  3374. n = cachep->node[node];
  3375. kfree(n->shared);
  3376. free_alien_cache(n->alien);
  3377. kfree(n);
  3378. cachep->node[node] = NULL;
  3379. }
  3380. node--;
  3381. }
  3382. }
  3383. return -ENOMEM;
  3384. }
  3385. struct ccupdate_struct {
  3386. struct kmem_cache *cachep;
  3387. struct array_cache *new[0];
  3388. };
  3389. static void do_ccupdate_local(void *info)
  3390. {
  3391. struct ccupdate_struct *new = info;
  3392. struct array_cache *old;
  3393. check_irq_off();
  3394. old = cpu_cache_get(new->cachep);
  3395. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3396. new->new[smp_processor_id()] = old;
  3397. }
  3398. /* Always called with the slab_mutex held */
  3399. static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3400. int batchcount, int shared, gfp_t gfp)
  3401. {
  3402. struct ccupdate_struct *new;
  3403. int i;
  3404. new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  3405. gfp);
  3406. if (!new)
  3407. return -ENOMEM;
  3408. for_each_online_cpu(i) {
  3409. new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
  3410. batchcount, gfp);
  3411. if (!new->new[i]) {
  3412. for (i--; i >= 0; i--)
  3413. kfree(new->new[i]);
  3414. kfree(new);
  3415. return -ENOMEM;
  3416. }
  3417. }
  3418. new->cachep = cachep;
  3419. on_each_cpu(do_ccupdate_local, (void *)new, 1);
  3420. check_irq_on();
  3421. cachep->batchcount = batchcount;
  3422. cachep->limit = limit;
  3423. cachep->shared = shared;
  3424. for_each_online_cpu(i) {
  3425. struct array_cache *ccold = new->new[i];
  3426. if (!ccold)
  3427. continue;
  3428. spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
  3429. free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
  3430. spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
  3431. kfree(ccold);
  3432. }
  3433. kfree(new);
  3434. return alloc_kmemlist(cachep, gfp);
  3435. }
  3436. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3437. int batchcount, int shared, gfp_t gfp)
  3438. {
  3439. int ret;
  3440. struct kmem_cache *c = NULL;
  3441. int i = 0;
  3442. ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3443. if (slab_state < FULL)
  3444. return ret;
  3445. if ((ret < 0) || !is_root_cache(cachep))
  3446. return ret;
  3447. VM_BUG_ON(!mutex_is_locked(&slab_mutex));
  3448. for_each_memcg_cache_index(i) {
  3449. c = cache_from_memcg(cachep, i);
  3450. if (c)
  3451. /* return value determined by the parent cache only */
  3452. __do_tune_cpucache(c, limit, batchcount, shared, gfp);
  3453. }
  3454. return ret;
  3455. }
  3456. /* Called with slab_mutex held always */
  3457. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  3458. {
  3459. int err;
  3460. int limit = 0;
  3461. int shared = 0;
  3462. int batchcount = 0;
  3463. if (!is_root_cache(cachep)) {
  3464. struct kmem_cache *root = memcg_root_cache(cachep);
  3465. limit = root->limit;
  3466. shared = root->shared;
  3467. batchcount = root->batchcount;
  3468. }
  3469. if (limit && shared && batchcount)
  3470. goto skip_setup;
  3471. /*
  3472. * The head array serves three purposes:
  3473. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3474. * - reduce the number of spinlock operations.
  3475. * - reduce the number of linked list operations on the slab and
  3476. * bufctl chains: array operations are cheaper.
  3477. * The numbers are guessed, we should auto-tune as described by
  3478. * Bonwick.
  3479. */
  3480. if (cachep->size > 131072)
  3481. limit = 1;
  3482. else if (cachep->size > PAGE_SIZE)
  3483. limit = 8;
  3484. else if (cachep->size > 1024)
  3485. limit = 24;
  3486. else if (cachep->size > 256)
  3487. limit = 54;
  3488. else
  3489. limit = 120;
  3490. /*
  3491. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3492. * allocation behaviour: Most allocs on one cpu, most free operations
  3493. * on another cpu. For these cases, an efficient object passing between
  3494. * cpus is necessary. This is provided by a shared array. The array
  3495. * replaces Bonwick's magazine layer.
  3496. * On uniprocessor, it's functionally equivalent (but less efficient)
  3497. * to a larger limit. Thus disabled by default.
  3498. */
  3499. shared = 0;
  3500. if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
  3501. shared = 8;
  3502. #if DEBUG
  3503. /*
  3504. * With debugging enabled, large batchcount lead to excessively long
  3505. * periods with disabled local interrupts. Limit the batchcount
  3506. */
  3507. if (limit > 32)
  3508. limit = 32;
  3509. #endif
  3510. batchcount = (limit + 1) / 2;
  3511. skip_setup:
  3512. err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3513. if (err)
  3514. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3515. cachep->name, -err);
  3516. return err;
  3517. }
  3518. /*
  3519. * Drain an array if it contains any elements taking the node lock only if
  3520. * necessary. Note that the node listlock also protects the array_cache
  3521. * if drain_array() is used on the shared array.
  3522. */
  3523. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  3524. struct array_cache *ac, int force, int node)
  3525. {
  3526. int tofree;
  3527. if (!ac || !ac->avail)
  3528. return;
  3529. if (ac->touched && !force) {
  3530. ac->touched = 0;
  3531. } else {
  3532. spin_lock_irq(&n->list_lock);
  3533. if (ac->avail) {
  3534. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3535. if (tofree > ac->avail)
  3536. tofree = (ac->avail + 1) / 2;
  3537. free_block(cachep, ac->entry, tofree, node);
  3538. ac->avail -= tofree;
  3539. memmove(ac->entry, &(ac->entry[tofree]),
  3540. sizeof(void *) * ac->avail);
  3541. }
  3542. spin_unlock_irq(&n->list_lock);
  3543. }
  3544. }
  3545. /**
  3546. * cache_reap - Reclaim memory from caches.
  3547. * @w: work descriptor
  3548. *
  3549. * Called from workqueue/eventd every few seconds.
  3550. * Purpose:
  3551. * - clear the per-cpu caches for this CPU.
  3552. * - return freeable pages to the main free memory pool.
  3553. *
  3554. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3555. * again on the next iteration.
  3556. */
  3557. static void cache_reap(struct work_struct *w)
  3558. {
  3559. struct kmem_cache *searchp;
  3560. struct kmem_cache_node *n;
  3561. int node = numa_mem_id();
  3562. struct delayed_work *work = to_delayed_work(w);
  3563. if (!mutex_trylock(&slab_mutex))
  3564. /* Give up. Setup the next iteration. */
  3565. goto out;
  3566. list_for_each_entry(searchp, &slab_caches, list) {
  3567. check_irq_on();
  3568. /*
  3569. * We only take the node lock if absolutely necessary and we
  3570. * have established with reasonable certainty that
  3571. * we can do some work if the lock was obtained.
  3572. */
  3573. n = searchp->node[node];
  3574. reap_alien(searchp, n);
  3575. drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
  3576. /*
  3577. * These are racy checks but it does not matter
  3578. * if we skip one check or scan twice.
  3579. */
  3580. if (time_after(n->next_reap, jiffies))
  3581. goto next;
  3582. n->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3583. drain_array(searchp, n, n->shared, 0, node);
  3584. if (n->free_touched)
  3585. n->free_touched = 0;
  3586. else {
  3587. int freed;
  3588. freed = drain_freelist(searchp, n, (n->free_limit +
  3589. 5 * searchp->num - 1) / (5 * searchp->num));
  3590. STATS_ADD_REAPED(searchp, freed);
  3591. }
  3592. next:
  3593. cond_resched();
  3594. }
  3595. check_irq_on();
  3596. mutex_unlock(&slab_mutex);
  3597. next_reap_node();
  3598. out:
  3599. /* Set up the next iteration */
  3600. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
  3601. }
  3602. #ifdef CONFIG_SLABINFO
  3603. void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
  3604. {
  3605. struct slab *slabp;
  3606. unsigned long active_objs;
  3607. unsigned long num_objs;
  3608. unsigned long active_slabs = 0;
  3609. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3610. const char *name;
  3611. char *error = NULL;
  3612. int node;
  3613. struct kmem_cache_node *n;
  3614. active_objs = 0;
  3615. num_slabs = 0;
  3616. for_each_online_node(node) {
  3617. n = cachep->node[node];
  3618. if (!n)
  3619. continue;
  3620. check_irq_on();
  3621. spin_lock_irq(&n->list_lock);
  3622. list_for_each_entry(slabp, &n->slabs_full, list) {
  3623. if (slabp->inuse != cachep->num && !error)
  3624. error = "slabs_full accounting error";
  3625. active_objs += cachep->num;
  3626. active_slabs++;
  3627. }
  3628. list_for_each_entry(slabp, &n->slabs_partial, list) {
  3629. if (slabp->inuse == cachep->num && !error)
  3630. error = "slabs_partial inuse accounting error";
  3631. if (!slabp->inuse && !error)
  3632. error = "slabs_partial/inuse accounting error";
  3633. active_objs += slabp->inuse;
  3634. active_slabs++;
  3635. }
  3636. list_for_each_entry(slabp, &n->slabs_free, list) {
  3637. if (slabp->inuse && !error)
  3638. error = "slabs_free/inuse accounting error";
  3639. num_slabs++;
  3640. }
  3641. free_objects += n->free_objects;
  3642. if (n->shared)
  3643. shared_avail += n->shared->avail;
  3644. spin_unlock_irq(&n->list_lock);
  3645. }
  3646. num_slabs += active_slabs;
  3647. num_objs = num_slabs * cachep->num;
  3648. if (num_objs - active_objs != free_objects && !error)
  3649. error = "free_objects accounting error";
  3650. name = cachep->name;
  3651. if (error)
  3652. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3653. sinfo->active_objs = active_objs;
  3654. sinfo->num_objs = num_objs;
  3655. sinfo->active_slabs = active_slabs;
  3656. sinfo->num_slabs = num_slabs;
  3657. sinfo->shared_avail = shared_avail;
  3658. sinfo->limit = cachep->limit;
  3659. sinfo->batchcount = cachep->batchcount;
  3660. sinfo->shared = cachep->shared;
  3661. sinfo->objects_per_slab = cachep->num;
  3662. sinfo->cache_order = cachep->gfporder;
  3663. }
  3664. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
  3665. {
  3666. #if STATS
  3667. { /* node stats */
  3668. unsigned long high = cachep->high_mark;
  3669. unsigned long allocs = cachep->num_allocations;
  3670. unsigned long grown = cachep->grown;
  3671. unsigned long reaped = cachep->reaped;
  3672. unsigned long errors = cachep->errors;
  3673. unsigned long max_freeable = cachep->max_freeable;
  3674. unsigned long node_allocs = cachep->node_allocs;
  3675. unsigned long node_frees = cachep->node_frees;
  3676. unsigned long overflows = cachep->node_overflow;
  3677. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  3678. "%4lu %4lu %4lu %4lu %4lu",
  3679. allocs, high, grown,
  3680. reaped, errors, max_freeable, node_allocs,
  3681. node_frees, overflows);
  3682. }
  3683. /* cpu stats */
  3684. {
  3685. unsigned long allochit = atomic_read(&cachep->allochit);
  3686. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3687. unsigned long freehit = atomic_read(&cachep->freehit);
  3688. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3689. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3690. allochit, allocmiss, freehit, freemiss);
  3691. }
  3692. #endif
  3693. }
  3694. #define MAX_SLABINFO_WRITE 128
  3695. /**
  3696. * slabinfo_write - Tuning for the slab allocator
  3697. * @file: unused
  3698. * @buffer: user buffer
  3699. * @count: data length
  3700. * @ppos: unused
  3701. */
  3702. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  3703. size_t count, loff_t *ppos)
  3704. {
  3705. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3706. int limit, batchcount, shared, res;
  3707. struct kmem_cache *cachep;
  3708. if (count > MAX_SLABINFO_WRITE)
  3709. return -EINVAL;
  3710. if (copy_from_user(&kbuf, buffer, count))
  3711. return -EFAULT;
  3712. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3713. tmp = strchr(kbuf, ' ');
  3714. if (!tmp)
  3715. return -EINVAL;
  3716. *tmp = '\0';
  3717. tmp++;
  3718. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3719. return -EINVAL;
  3720. /* Find the cache in the chain of caches. */
  3721. mutex_lock(&slab_mutex);
  3722. res = -EINVAL;
  3723. list_for_each_entry(cachep, &slab_caches, list) {
  3724. if (!strcmp(cachep->name, kbuf)) {
  3725. if (limit < 1 || batchcount < 1 ||
  3726. batchcount > limit || shared < 0) {
  3727. res = 0;
  3728. } else {
  3729. res = do_tune_cpucache(cachep, limit,
  3730. batchcount, shared,
  3731. GFP_KERNEL);
  3732. }
  3733. break;
  3734. }
  3735. }
  3736. mutex_unlock(&slab_mutex);
  3737. if (res >= 0)
  3738. res = count;
  3739. return res;
  3740. }
  3741. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3742. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3743. {
  3744. mutex_lock(&slab_mutex);
  3745. return seq_list_start(&slab_caches, *pos);
  3746. }
  3747. static inline int add_caller(unsigned long *n, unsigned long v)
  3748. {
  3749. unsigned long *p;
  3750. int l;
  3751. if (!v)
  3752. return 1;
  3753. l = n[1];
  3754. p = n + 2;
  3755. while (l) {
  3756. int i = l/2;
  3757. unsigned long *q = p + 2 * i;
  3758. if (*q == v) {
  3759. q[1]++;
  3760. return 1;
  3761. }
  3762. if (*q > v) {
  3763. l = i;
  3764. } else {
  3765. p = q + 2;
  3766. l -= i + 1;
  3767. }
  3768. }
  3769. if (++n[1] == n[0])
  3770. return 0;
  3771. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3772. p[0] = v;
  3773. p[1] = 1;
  3774. return 1;
  3775. }
  3776. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  3777. {
  3778. void *p;
  3779. int i;
  3780. if (n[0] == n[1])
  3781. return;
  3782. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
  3783. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  3784. continue;
  3785. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  3786. return;
  3787. }
  3788. }
  3789. static void show_symbol(struct seq_file *m, unsigned long address)
  3790. {
  3791. #ifdef CONFIG_KALLSYMS
  3792. unsigned long offset, size;
  3793. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  3794. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  3795. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3796. if (modname[0])
  3797. seq_printf(m, " [%s]", modname);
  3798. return;
  3799. }
  3800. #endif
  3801. seq_printf(m, "%p", (void *)address);
  3802. }
  3803. static int leaks_show(struct seq_file *m, void *p)
  3804. {
  3805. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
  3806. struct slab *slabp;
  3807. struct kmem_cache_node *n;
  3808. const char *name;
  3809. unsigned long *x = m->private;
  3810. int node;
  3811. int i;
  3812. if (!(cachep->flags & SLAB_STORE_USER))
  3813. return 0;
  3814. if (!(cachep->flags & SLAB_RED_ZONE))
  3815. return 0;
  3816. /* OK, we can do it */
  3817. x[1] = 0;
  3818. for_each_online_node(node) {
  3819. n = cachep->node[node];
  3820. if (!n)
  3821. continue;
  3822. check_irq_on();
  3823. spin_lock_irq(&n->list_lock);
  3824. list_for_each_entry(slabp, &n->slabs_full, list)
  3825. handle_slab(x, cachep, slabp);
  3826. list_for_each_entry(slabp, &n->slabs_partial, list)
  3827. handle_slab(x, cachep, slabp);
  3828. spin_unlock_irq(&n->list_lock);
  3829. }
  3830. name = cachep->name;
  3831. if (x[0] == x[1]) {
  3832. /* Increase the buffer size */
  3833. mutex_unlock(&slab_mutex);
  3834. m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  3835. if (!m->private) {
  3836. /* Too bad, we are really out */
  3837. m->private = x;
  3838. mutex_lock(&slab_mutex);
  3839. return -ENOMEM;
  3840. }
  3841. *(unsigned long *)m->private = x[0] * 2;
  3842. kfree(x);
  3843. mutex_lock(&slab_mutex);
  3844. /* Now make sure this entry will be retried */
  3845. m->count = m->size;
  3846. return 0;
  3847. }
  3848. for (i = 0; i < x[1]; i++) {
  3849. seq_printf(m, "%s: %lu ", name, x[2*i+3]);
  3850. show_symbol(m, x[2*i+2]);
  3851. seq_putc(m, '\n');
  3852. }
  3853. return 0;
  3854. }
  3855. static const struct seq_operations slabstats_op = {
  3856. .start = leaks_start,
  3857. .next = slab_next,
  3858. .stop = slab_stop,
  3859. .show = leaks_show,
  3860. };
  3861. static int slabstats_open(struct inode *inode, struct file *file)
  3862. {
  3863. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  3864. int ret = -ENOMEM;
  3865. if (n) {
  3866. ret = seq_open(file, &slabstats_op);
  3867. if (!ret) {
  3868. struct seq_file *m = file->private_data;
  3869. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  3870. m->private = n;
  3871. n = NULL;
  3872. }
  3873. kfree(n);
  3874. }
  3875. return ret;
  3876. }
  3877. static const struct file_operations proc_slabstats_operations = {
  3878. .open = slabstats_open,
  3879. .read = seq_read,
  3880. .llseek = seq_lseek,
  3881. .release = seq_release_private,
  3882. };
  3883. #endif
  3884. static int __init slab_proc_init(void)
  3885. {
  3886. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3887. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  3888. #endif
  3889. return 0;
  3890. }
  3891. module_init(slab_proc_init);
  3892. #endif
  3893. /**
  3894. * ksize - get the actual amount of memory allocated for a given object
  3895. * @objp: Pointer to the object
  3896. *
  3897. * kmalloc may internally round up allocations and return more memory
  3898. * than requested. ksize() can be used to determine the actual amount of
  3899. * memory allocated. The caller may use this additional memory, even though
  3900. * a smaller amount of memory was initially specified with the kmalloc call.
  3901. * The caller must guarantee that objp points to a valid object previously
  3902. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  3903. * must not be freed during the duration of the call.
  3904. */
  3905. size_t ksize(const void *objp)
  3906. {
  3907. BUG_ON(!objp);
  3908. if (unlikely(objp == ZERO_SIZE_PTR))
  3909. return 0;
  3910. return virt_to_cache(objp)->object_size;
  3911. }
  3912. EXPORT_SYMBOL(ksize);