slab.c 108 KB

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