slab.c 122 KB

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