slab.c 120 KB

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