slab.c 118 KB

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