slab.c 117 KB

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