slab.c 118 KB

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