slab.c 116 KB

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