slab.c 108 KB

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