slub.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023
  1. /*
  2. * SLUB: A slab allocator that limits cache line use instead of queuing
  3. * objects in per cpu and per node lists.
  4. *
  5. * The allocator synchronizes using per slab locks and only
  6. * uses a centralized lock to manage a pool of partial slabs.
  7. *
  8. * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/bit_spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/bitops.h>
  15. #include <linux/slab.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpuset.h>
  19. #include <linux/mempolicy.h>
  20. #include <linux/ctype.h>
  21. #include <linux/kallsyms.h>
  22. /*
  23. * Lock order:
  24. * 1. slab_lock(page)
  25. * 2. slab->list_lock
  26. *
  27. * The slab_lock protects operations on the object of a particular
  28. * slab and its metadata in the page struct. If the slab lock
  29. * has been taken then no allocations nor frees can be performed
  30. * on the objects in the slab nor can the slab be added or removed
  31. * from the partial or full lists since this would mean modifying
  32. * the page_struct of the slab.
  33. *
  34. * The list_lock protects the partial and full list on each node and
  35. * the partial slab counter. If taken then no new slabs may be added or
  36. * removed from the lists nor make the number of partial slabs be modified.
  37. * (Note that the total number of slabs is an atomic value that may be
  38. * modified without taking the list lock).
  39. *
  40. * The list_lock is a centralized lock and thus we avoid taking it as
  41. * much as possible. As long as SLUB does not have to handle partial
  42. * slabs, operations can continue without any centralized lock. F.e.
  43. * allocating a long series of objects that fill up slabs does not require
  44. * the list lock.
  45. *
  46. * The lock order is sometimes inverted when we are trying to get a slab
  47. * off a list. We take the list_lock and then look for a page on the list
  48. * to use. While we do that objects in the slabs may be freed. We can
  49. * only operate on the slab if we have also taken the slab_lock. So we use
  50. * a slab_trylock() on the slab. If trylock was successful then no frees
  51. * can occur anymore and we can use the slab for allocations etc. If the
  52. * slab_trylock() does not succeed then frees are in progress in the slab and
  53. * we must stay away from it for a while since we may cause a bouncing
  54. * cacheline if we try to acquire the lock. So go onto the next slab.
  55. * If all pages are busy then we may allocate a new slab instead of reusing
  56. * a partial slab. A new slab has noone operating on it and thus there is
  57. * no danger of cacheline contention.
  58. *
  59. * Interrupts are disabled during allocation and deallocation in order to
  60. * make the slab allocator safe to use in the context of an irq. In addition
  61. * interrupts are disabled to ensure that the processor does not change
  62. * while handling per_cpu slabs, due to kernel preemption.
  63. *
  64. * SLUB assigns one slab for allocation to each processor.
  65. * Allocations only occur from these slabs called cpu slabs.
  66. *
  67. * Slabs with free elements are kept on a partial list and during regular
  68. * operations no list for full slabs is used. If an object in a full slab is
  69. * freed then the slab will show up again on the partial lists.
  70. * We track full slabs for debugging purposes though because otherwise we
  71. * cannot scan all objects.
  72. *
  73. * Slabs are freed when they become empty. Teardown and setup is
  74. * minimal so we rely on the page allocators per cpu caches for
  75. * fast frees and allocs.
  76. *
  77. * Overloading of page flags that are otherwise used for LRU management.
  78. *
  79. * PageActive The slab is frozen and exempt from list processing.
  80. * This means that the slab is dedicated to a purpose
  81. * such as satisfying allocations for a specific
  82. * processor. Objects may be freed in the slab while
  83. * it is frozen but slab_free will then skip the usual
  84. * list operations. It is up to the processor holding
  85. * the slab to integrate the slab into the slab lists
  86. * when the slab is no longer needed.
  87. *
  88. * One use of this flag is to mark slabs that are
  89. * used for allocations. Then such a slab becomes a cpu
  90. * slab. The cpu slab may be equipped with an additional
  91. * freelist that allows lockless access to
  92. * free objects in addition to the regular freelist
  93. * that requires the slab lock.
  94. *
  95. * PageError Slab requires special handling due to debug
  96. * options set. This moves slab handling out of
  97. * the fast path and disables lockless freelists.
  98. */
  99. #define FROZEN (1 << PG_active)
  100. #ifdef CONFIG_SLUB_DEBUG
  101. #define SLABDEBUG (1 << PG_error)
  102. #else
  103. #define SLABDEBUG 0
  104. #endif
  105. static inline int SlabFrozen(struct page *page)
  106. {
  107. return page->flags & FROZEN;
  108. }
  109. static inline void SetSlabFrozen(struct page *page)
  110. {
  111. page->flags |= FROZEN;
  112. }
  113. static inline void ClearSlabFrozen(struct page *page)
  114. {
  115. page->flags &= ~FROZEN;
  116. }
  117. static inline int SlabDebug(struct page *page)
  118. {
  119. return page->flags & SLABDEBUG;
  120. }
  121. static inline void SetSlabDebug(struct page *page)
  122. {
  123. page->flags |= SLABDEBUG;
  124. }
  125. static inline void ClearSlabDebug(struct page *page)
  126. {
  127. page->flags &= ~SLABDEBUG;
  128. }
  129. /*
  130. * Issues still to be resolved:
  131. *
  132. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  133. *
  134. * - Variable sizing of the per node arrays
  135. */
  136. /* Enable to test recovery from slab corruption on boot */
  137. #undef SLUB_RESILIENCY_TEST
  138. #if PAGE_SHIFT <= 12
  139. /*
  140. * Small page size. Make sure that we do not fragment memory
  141. */
  142. #define DEFAULT_MAX_ORDER 1
  143. #define DEFAULT_MIN_OBJECTS 4
  144. #else
  145. /*
  146. * Large page machines are customarily able to handle larger
  147. * page orders.
  148. */
  149. #define DEFAULT_MAX_ORDER 2
  150. #define DEFAULT_MIN_OBJECTS 8
  151. #endif
  152. /*
  153. * Mininum number of partial slabs. These will be left on the partial
  154. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  155. */
  156. #define MIN_PARTIAL 2
  157. /*
  158. * Maximum number of desirable partial slabs.
  159. * The existence of more partial slabs makes kmem_cache_shrink
  160. * sort the partial list by the number of objects in the.
  161. */
  162. #define MAX_PARTIAL 10
  163. #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
  164. SLAB_POISON | SLAB_STORE_USER)
  165. /*
  166. * Set of flags that will prevent slab merging
  167. */
  168. #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  169. SLAB_TRACE | SLAB_DESTROY_BY_RCU)
  170. #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  171. SLAB_CACHE_DMA)
  172. #ifndef ARCH_KMALLOC_MINALIGN
  173. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  174. #endif
  175. #ifndef ARCH_SLAB_MINALIGN
  176. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  177. #endif
  178. /* Internal SLUB flags */
  179. #define __OBJECT_POISON 0x80000000 /* Poison object */
  180. #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
  181. /* Not all arches define cache_line_size */
  182. #ifndef cache_line_size
  183. #define cache_line_size() L1_CACHE_BYTES
  184. #endif
  185. static int kmem_size = sizeof(struct kmem_cache);
  186. #ifdef CONFIG_SMP
  187. static struct notifier_block slab_notifier;
  188. #endif
  189. static enum {
  190. DOWN, /* No slab functionality available */
  191. PARTIAL, /* kmem_cache_open() works but kmalloc does not */
  192. UP, /* Everything works but does not show up in sysfs */
  193. SYSFS /* Sysfs up */
  194. } slab_state = DOWN;
  195. /* A list of all slab caches on the system */
  196. static DECLARE_RWSEM(slub_lock);
  197. static LIST_HEAD(slab_caches);
  198. /*
  199. * Tracking user of a slab.
  200. */
  201. struct track {
  202. void *addr; /* Called from address */
  203. int cpu; /* Was running on cpu */
  204. int pid; /* Pid context */
  205. unsigned long when; /* When did the operation occur */
  206. };
  207. enum track_item { TRACK_ALLOC, TRACK_FREE };
  208. #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
  209. static int sysfs_slab_add(struct kmem_cache *);
  210. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  211. static void sysfs_slab_remove(struct kmem_cache *);
  212. #else
  213. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  214. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  215. { return 0; }
  216. static inline void sysfs_slab_remove(struct kmem_cache *s) {}
  217. #endif
  218. /********************************************************************
  219. * Core slab cache functions
  220. *******************************************************************/
  221. int slab_is_available(void)
  222. {
  223. return slab_state >= UP;
  224. }
  225. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  226. {
  227. #ifdef CONFIG_NUMA
  228. return s->node[node];
  229. #else
  230. return &s->local_node;
  231. #endif
  232. }
  233. static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
  234. {
  235. #ifdef CONFIG_SMP
  236. return s->cpu_slab[cpu];
  237. #else
  238. return &s->cpu_slab;
  239. #endif
  240. }
  241. static inline int check_valid_pointer(struct kmem_cache *s,
  242. struct page *page, const void *object)
  243. {
  244. void *base;
  245. if (!object)
  246. return 1;
  247. base = page_address(page);
  248. if (object < base || object >= base + s->objects * s->size ||
  249. (object - base) % s->size) {
  250. return 0;
  251. }
  252. return 1;
  253. }
  254. /*
  255. * Slow version of get and set free pointer.
  256. *
  257. * This version requires touching the cache lines of kmem_cache which
  258. * we avoid to do in the fast alloc free paths. There we obtain the offset
  259. * from the page struct.
  260. */
  261. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  262. {
  263. return *(void **)(object + s->offset);
  264. }
  265. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  266. {
  267. *(void **)(object + s->offset) = fp;
  268. }
  269. /* Loop over all objects in a slab */
  270. #define for_each_object(__p, __s, __addr) \
  271. for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
  272. __p += (__s)->size)
  273. /* Scan freelist */
  274. #define for_each_free_object(__p, __s, __free) \
  275. for (__p = (__free); __p; __p = get_freepointer((__s), __p))
  276. /* Determine object index from a given position */
  277. static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
  278. {
  279. return (p - addr) / s->size;
  280. }
  281. #ifdef CONFIG_SLUB_DEBUG
  282. /*
  283. * Debug settings:
  284. */
  285. #ifdef CONFIG_SLUB_DEBUG_ON
  286. static int slub_debug = DEBUG_DEFAULT_FLAGS;
  287. #else
  288. static int slub_debug;
  289. #endif
  290. static char *slub_debug_slabs;
  291. /*
  292. * Object debugging
  293. */
  294. static void print_section(char *text, u8 *addr, unsigned int length)
  295. {
  296. int i, offset;
  297. int newline = 1;
  298. char ascii[17];
  299. ascii[16] = 0;
  300. for (i = 0; i < length; i++) {
  301. if (newline) {
  302. printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
  303. newline = 0;
  304. }
  305. printk(" %02x", addr[i]);
  306. offset = i % 16;
  307. ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
  308. if (offset == 15) {
  309. printk(" %s\n",ascii);
  310. newline = 1;
  311. }
  312. }
  313. if (!newline) {
  314. i %= 16;
  315. while (i < 16) {
  316. printk(" ");
  317. ascii[i] = ' ';
  318. i++;
  319. }
  320. printk(" %s\n", ascii);
  321. }
  322. }
  323. static struct track *get_track(struct kmem_cache *s, void *object,
  324. enum track_item alloc)
  325. {
  326. struct track *p;
  327. if (s->offset)
  328. p = object + s->offset + sizeof(void *);
  329. else
  330. p = object + s->inuse;
  331. return p + alloc;
  332. }
  333. static void set_track(struct kmem_cache *s, void *object,
  334. enum track_item alloc, void *addr)
  335. {
  336. struct track *p;
  337. if (s->offset)
  338. p = object + s->offset + sizeof(void *);
  339. else
  340. p = object + s->inuse;
  341. p += alloc;
  342. if (addr) {
  343. p->addr = addr;
  344. p->cpu = smp_processor_id();
  345. p->pid = current ? current->pid : -1;
  346. p->when = jiffies;
  347. } else
  348. memset(p, 0, sizeof(struct track));
  349. }
  350. static void init_tracking(struct kmem_cache *s, void *object)
  351. {
  352. if (!(s->flags & SLAB_STORE_USER))
  353. return;
  354. set_track(s, object, TRACK_FREE, NULL);
  355. set_track(s, object, TRACK_ALLOC, NULL);
  356. }
  357. static void print_track(const char *s, struct track *t)
  358. {
  359. if (!t->addr)
  360. return;
  361. printk(KERN_ERR "INFO: %s in ", s);
  362. __print_symbol("%s", (unsigned long)t->addr);
  363. printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
  364. }
  365. static void print_tracking(struct kmem_cache *s, void *object)
  366. {
  367. if (!(s->flags & SLAB_STORE_USER))
  368. return;
  369. print_track("Allocated", get_track(s, object, TRACK_ALLOC));
  370. print_track("Freed", get_track(s, object, TRACK_FREE));
  371. }
  372. static void print_page_info(struct page *page)
  373. {
  374. printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
  375. page, page->inuse, page->freelist, page->flags);
  376. }
  377. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  378. {
  379. va_list args;
  380. char buf[100];
  381. va_start(args, fmt);
  382. vsnprintf(buf, sizeof(buf), fmt, args);
  383. va_end(args);
  384. printk(KERN_ERR "========================================"
  385. "=====================================\n");
  386. printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
  387. printk(KERN_ERR "----------------------------------------"
  388. "-------------------------------------\n\n");
  389. }
  390. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  391. {
  392. va_list args;
  393. char buf[100];
  394. va_start(args, fmt);
  395. vsnprintf(buf, sizeof(buf), fmt, args);
  396. va_end(args);
  397. printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
  398. }
  399. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  400. {
  401. unsigned int off; /* Offset of last byte */
  402. u8 *addr = page_address(page);
  403. print_tracking(s, p);
  404. print_page_info(page);
  405. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  406. p, p - addr, get_freepointer(s, p));
  407. if (p > addr + 16)
  408. print_section("Bytes b4", p - 16, 16);
  409. print_section("Object", p, min(s->objsize, 128));
  410. if (s->flags & SLAB_RED_ZONE)
  411. print_section("Redzone", p + s->objsize,
  412. s->inuse - s->objsize);
  413. if (s->offset)
  414. off = s->offset + sizeof(void *);
  415. else
  416. off = s->inuse;
  417. if (s->flags & SLAB_STORE_USER)
  418. off += 2 * sizeof(struct track);
  419. if (off != s->size)
  420. /* Beginning of the filler is the free pointer */
  421. print_section("Padding", p + off, s->size - off);
  422. dump_stack();
  423. }
  424. static void object_err(struct kmem_cache *s, struct page *page,
  425. u8 *object, char *reason)
  426. {
  427. slab_bug(s, reason);
  428. print_trailer(s, page, object);
  429. }
  430. static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
  431. {
  432. va_list args;
  433. char buf[100];
  434. va_start(args, fmt);
  435. vsnprintf(buf, sizeof(buf), fmt, args);
  436. va_end(args);
  437. slab_bug(s, fmt);
  438. print_page_info(page);
  439. dump_stack();
  440. }
  441. static void init_object(struct kmem_cache *s, void *object, int active)
  442. {
  443. u8 *p = object;
  444. if (s->flags & __OBJECT_POISON) {
  445. memset(p, POISON_FREE, s->objsize - 1);
  446. p[s->objsize -1] = POISON_END;
  447. }
  448. if (s->flags & SLAB_RED_ZONE)
  449. memset(p + s->objsize,
  450. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
  451. s->inuse - s->objsize);
  452. }
  453. static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  454. {
  455. while (bytes) {
  456. if (*start != (u8)value)
  457. return start;
  458. start++;
  459. bytes--;
  460. }
  461. return NULL;
  462. }
  463. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  464. void *from, void *to)
  465. {
  466. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  467. memset(from, data, to - from);
  468. }
  469. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  470. u8 *object, char *what,
  471. u8* start, unsigned int value, unsigned int bytes)
  472. {
  473. u8 *fault;
  474. u8 *end;
  475. fault = check_bytes(start, value, bytes);
  476. if (!fault)
  477. return 1;
  478. end = start + bytes;
  479. while (end > fault && end[-1] == value)
  480. end--;
  481. slab_bug(s, "%s overwritten", what);
  482. printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  483. fault, end - 1, fault[0], value);
  484. print_trailer(s, page, object);
  485. restore_bytes(s, what, value, fault, end);
  486. return 0;
  487. }
  488. /*
  489. * Object layout:
  490. *
  491. * object address
  492. * Bytes of the object to be managed.
  493. * If the freepointer may overlay the object then the free
  494. * pointer is the first word of the object.
  495. *
  496. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  497. * 0xa5 (POISON_END)
  498. *
  499. * object + s->objsize
  500. * Padding to reach word boundary. This is also used for Redzoning.
  501. * Padding is extended by another word if Redzoning is enabled and
  502. * objsize == inuse.
  503. *
  504. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  505. * 0xcc (RED_ACTIVE) for objects in use.
  506. *
  507. * object + s->inuse
  508. * Meta data starts here.
  509. *
  510. * A. Free pointer (if we cannot overwrite object on free)
  511. * B. Tracking data for SLAB_STORE_USER
  512. * C. Padding to reach required alignment boundary or at mininum
  513. * one word if debuggin is on to be able to detect writes
  514. * before the word boundary.
  515. *
  516. * Padding is done using 0x5a (POISON_INUSE)
  517. *
  518. * object + s->size
  519. * Nothing is used beyond s->size.
  520. *
  521. * If slabcaches are merged then the objsize and inuse boundaries are mostly
  522. * ignored. And therefore no slab options that rely on these boundaries
  523. * may be used with merged slabcaches.
  524. */
  525. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  526. {
  527. unsigned long off = s->inuse; /* The end of info */
  528. if (s->offset)
  529. /* Freepointer is placed after the object. */
  530. off += sizeof(void *);
  531. if (s->flags & SLAB_STORE_USER)
  532. /* We also have user information there */
  533. off += 2 * sizeof(struct track);
  534. if (s->size == off)
  535. return 1;
  536. return check_bytes_and_report(s, page, p, "Object padding",
  537. p + off, POISON_INUSE, s->size - off);
  538. }
  539. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  540. {
  541. u8 *start;
  542. u8 *fault;
  543. u8 *end;
  544. int length;
  545. int remainder;
  546. if (!(s->flags & SLAB_POISON))
  547. return 1;
  548. start = page_address(page);
  549. end = start + (PAGE_SIZE << s->order);
  550. length = s->objects * s->size;
  551. remainder = end - (start + length);
  552. if (!remainder)
  553. return 1;
  554. fault = check_bytes(start + length, POISON_INUSE, remainder);
  555. if (!fault)
  556. return 1;
  557. while (end > fault && end[-1] == POISON_INUSE)
  558. end--;
  559. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  560. print_section("Padding", start, length);
  561. restore_bytes(s, "slab padding", POISON_INUSE, start, end);
  562. return 0;
  563. }
  564. static int check_object(struct kmem_cache *s, struct page *page,
  565. void *object, int active)
  566. {
  567. u8 *p = object;
  568. u8 *endobject = object + s->objsize;
  569. if (s->flags & SLAB_RED_ZONE) {
  570. unsigned int red =
  571. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
  572. if (!check_bytes_and_report(s, page, object, "Redzone",
  573. endobject, red, s->inuse - s->objsize))
  574. return 0;
  575. } else {
  576. if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
  577. check_bytes_and_report(s, page, p, "Alignment padding", endobject,
  578. POISON_INUSE, s->inuse - s->objsize);
  579. }
  580. if (s->flags & SLAB_POISON) {
  581. if (!active && (s->flags & __OBJECT_POISON) &&
  582. (!check_bytes_and_report(s, page, p, "Poison", p,
  583. POISON_FREE, s->objsize - 1) ||
  584. !check_bytes_and_report(s, page, p, "Poison",
  585. p + s->objsize -1, POISON_END, 1)))
  586. return 0;
  587. /*
  588. * check_pad_bytes cleans up on its own.
  589. */
  590. check_pad_bytes(s, page, p);
  591. }
  592. if (!s->offset && active)
  593. /*
  594. * Object and freepointer overlap. Cannot check
  595. * freepointer while object is allocated.
  596. */
  597. return 1;
  598. /* Check free pointer validity */
  599. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  600. object_err(s, page, p, "Freepointer corrupt");
  601. /*
  602. * No choice but to zap it and thus loose the remainder
  603. * of the free objects in this slab. May cause
  604. * another error because the object count is now wrong.
  605. */
  606. set_freepointer(s, p, NULL);
  607. return 0;
  608. }
  609. return 1;
  610. }
  611. static int check_slab(struct kmem_cache *s, struct page *page)
  612. {
  613. VM_BUG_ON(!irqs_disabled());
  614. if (!PageSlab(page)) {
  615. slab_err(s, page, "Not a valid slab page");
  616. return 0;
  617. }
  618. if (page->inuse > s->objects) {
  619. slab_err(s, page, "inuse %u > max %u",
  620. s->name, page->inuse, s->objects);
  621. return 0;
  622. }
  623. /* Slab_pad_check fixes things up after itself */
  624. slab_pad_check(s, page);
  625. return 1;
  626. }
  627. /*
  628. * Determine if a certain object on a page is on the freelist. Must hold the
  629. * slab lock to guarantee that the chains are in a consistent state.
  630. */
  631. static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
  632. {
  633. int nr = 0;
  634. void *fp = page->freelist;
  635. void *object = NULL;
  636. while (fp && nr <= s->objects) {
  637. if (fp == search)
  638. return 1;
  639. if (!check_valid_pointer(s, page, fp)) {
  640. if (object) {
  641. object_err(s, page, object,
  642. "Freechain corrupt");
  643. set_freepointer(s, object, NULL);
  644. break;
  645. } else {
  646. slab_err(s, page, "Freepointer corrupt");
  647. page->freelist = NULL;
  648. page->inuse = s->objects;
  649. slab_fix(s, "Freelist cleared");
  650. return 0;
  651. }
  652. break;
  653. }
  654. object = fp;
  655. fp = get_freepointer(s, object);
  656. nr++;
  657. }
  658. if (page->inuse != s->objects - nr) {
  659. slab_err(s, page, "Wrong object count. Counter is %d but "
  660. "counted were %d", page->inuse, s->objects - nr);
  661. page->inuse = s->objects - nr;
  662. slab_fix(s, "Object count adjusted.");
  663. }
  664. return search == NULL;
  665. }
  666. static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
  667. {
  668. if (s->flags & SLAB_TRACE) {
  669. printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  670. s->name,
  671. alloc ? "alloc" : "free",
  672. object, page->inuse,
  673. page->freelist);
  674. if (!alloc)
  675. print_section("Object", (void *)object, s->objsize);
  676. dump_stack();
  677. }
  678. }
  679. /*
  680. * Tracking of fully allocated slabs for debugging purposes.
  681. */
  682. static void add_full(struct kmem_cache_node *n, struct page *page)
  683. {
  684. spin_lock(&n->list_lock);
  685. list_add(&page->lru, &n->full);
  686. spin_unlock(&n->list_lock);
  687. }
  688. static void remove_full(struct kmem_cache *s, struct page *page)
  689. {
  690. struct kmem_cache_node *n;
  691. if (!(s->flags & SLAB_STORE_USER))
  692. return;
  693. n = get_node(s, page_to_nid(page));
  694. spin_lock(&n->list_lock);
  695. list_del(&page->lru);
  696. spin_unlock(&n->list_lock);
  697. }
  698. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  699. void *object)
  700. {
  701. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  702. return;
  703. init_object(s, object, 0);
  704. init_tracking(s, object);
  705. }
  706. static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
  707. void *object, void *addr)
  708. {
  709. if (!check_slab(s, page))
  710. goto bad;
  711. if (object && !on_freelist(s, page, object)) {
  712. object_err(s, page, object, "Object already allocated");
  713. goto bad;
  714. }
  715. if (!check_valid_pointer(s, page, object)) {
  716. object_err(s, page, object, "Freelist Pointer check fails");
  717. goto bad;
  718. }
  719. if (object && !check_object(s, page, object, 0))
  720. goto bad;
  721. /* Success perform special debug activities for allocs */
  722. if (s->flags & SLAB_STORE_USER)
  723. set_track(s, object, TRACK_ALLOC, addr);
  724. trace(s, page, object, 1);
  725. init_object(s, object, 1);
  726. return 1;
  727. bad:
  728. if (PageSlab(page)) {
  729. /*
  730. * If this is a slab page then lets do the best we can
  731. * to avoid issues in the future. Marking all objects
  732. * as used avoids touching the remaining objects.
  733. */
  734. slab_fix(s, "Marking all objects used");
  735. page->inuse = s->objects;
  736. page->freelist = NULL;
  737. }
  738. return 0;
  739. }
  740. static int free_debug_processing(struct kmem_cache *s, struct page *page,
  741. void *object, void *addr)
  742. {
  743. if (!check_slab(s, page))
  744. goto fail;
  745. if (!check_valid_pointer(s, page, object)) {
  746. slab_err(s, page, "Invalid object pointer 0x%p", object);
  747. goto fail;
  748. }
  749. if (on_freelist(s, page, object)) {
  750. object_err(s, page, object, "Object already free");
  751. goto fail;
  752. }
  753. if (!check_object(s, page, object, 1))
  754. return 0;
  755. if (unlikely(s != page->slab)) {
  756. if (!PageSlab(page))
  757. slab_err(s, page, "Attempt to free object(0x%p) "
  758. "outside of slab", object);
  759. else
  760. if (!page->slab) {
  761. printk(KERN_ERR
  762. "SLUB <none>: no slab for object 0x%p.\n",
  763. object);
  764. dump_stack();
  765. }
  766. else
  767. object_err(s, page, object,
  768. "page slab pointer corrupt.");
  769. goto fail;
  770. }
  771. /* Special debug activities for freeing objects */
  772. if (!SlabFrozen(page) && !page->freelist)
  773. remove_full(s, page);
  774. if (s->flags & SLAB_STORE_USER)
  775. set_track(s, object, TRACK_FREE, addr);
  776. trace(s, page, object, 0);
  777. init_object(s, object, 0);
  778. return 1;
  779. fail:
  780. slab_fix(s, "Object at 0x%p not freed", object);
  781. return 0;
  782. }
  783. static int __init setup_slub_debug(char *str)
  784. {
  785. slub_debug = DEBUG_DEFAULT_FLAGS;
  786. if (*str++ != '=' || !*str)
  787. /*
  788. * No options specified. Switch on full debugging.
  789. */
  790. goto out;
  791. if (*str == ',')
  792. /*
  793. * No options but restriction on slabs. This means full
  794. * debugging for slabs matching a pattern.
  795. */
  796. goto check_slabs;
  797. slub_debug = 0;
  798. if (*str == '-')
  799. /*
  800. * Switch off all debugging measures.
  801. */
  802. goto out;
  803. /*
  804. * Determine which debug features should be switched on
  805. */
  806. for ( ;*str && *str != ','; str++) {
  807. switch (tolower(*str)) {
  808. case 'f':
  809. slub_debug |= SLAB_DEBUG_FREE;
  810. break;
  811. case 'z':
  812. slub_debug |= SLAB_RED_ZONE;
  813. break;
  814. case 'p':
  815. slub_debug |= SLAB_POISON;
  816. break;
  817. case 'u':
  818. slub_debug |= SLAB_STORE_USER;
  819. break;
  820. case 't':
  821. slub_debug |= SLAB_TRACE;
  822. break;
  823. default:
  824. printk(KERN_ERR "slub_debug option '%c' "
  825. "unknown. skipped\n",*str);
  826. }
  827. }
  828. check_slabs:
  829. if (*str == ',')
  830. slub_debug_slabs = str + 1;
  831. out:
  832. return 1;
  833. }
  834. __setup("slub_debug", setup_slub_debug);
  835. static unsigned long kmem_cache_flags(unsigned long objsize,
  836. unsigned long flags, const char *name,
  837. void (*ctor)(struct kmem_cache *, void *))
  838. {
  839. /*
  840. * The page->offset field is only 16 bit wide. This is an offset
  841. * in units of words from the beginning of an object. If the slab
  842. * size is bigger then we cannot move the free pointer behind the
  843. * object anymore.
  844. *
  845. * On 32 bit platforms the limit is 256k. On 64bit platforms
  846. * the limit is 512k.
  847. *
  848. * Debugging or ctor may create a need to move the free
  849. * pointer. Fail if this happens.
  850. */
  851. if (objsize >= 65535 * sizeof(void *)) {
  852. BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
  853. SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
  854. BUG_ON(ctor);
  855. } else {
  856. /*
  857. * Enable debugging if selected on the kernel commandline.
  858. */
  859. if (slub_debug && (!slub_debug_slabs ||
  860. strncmp(slub_debug_slabs, name,
  861. strlen(slub_debug_slabs)) == 0))
  862. flags |= slub_debug;
  863. }
  864. return flags;
  865. }
  866. #else
  867. static inline void setup_object_debug(struct kmem_cache *s,
  868. struct page *page, void *object) {}
  869. static inline int alloc_debug_processing(struct kmem_cache *s,
  870. struct page *page, void *object, void *addr) { return 0; }
  871. static inline int free_debug_processing(struct kmem_cache *s,
  872. struct page *page, void *object, void *addr) { return 0; }
  873. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  874. { return 1; }
  875. static inline int check_object(struct kmem_cache *s, struct page *page,
  876. void *object, int active) { return 1; }
  877. static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
  878. static inline unsigned long kmem_cache_flags(unsigned long objsize,
  879. unsigned long flags, const char *name,
  880. void (*ctor)(struct kmem_cache *, void *))
  881. {
  882. return flags;
  883. }
  884. #define slub_debug 0
  885. #endif
  886. /*
  887. * Slab allocation and freeing
  888. */
  889. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  890. {
  891. struct page * page;
  892. int pages = 1 << s->order;
  893. if (s->order)
  894. flags |= __GFP_COMP;
  895. if (s->flags & SLAB_CACHE_DMA)
  896. flags |= SLUB_DMA;
  897. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  898. flags |= __GFP_RECLAIMABLE;
  899. if (node == -1)
  900. page = alloc_pages(flags, s->order);
  901. else
  902. page = alloc_pages_node(node, flags, s->order);
  903. if (!page)
  904. return NULL;
  905. mod_zone_page_state(page_zone(page),
  906. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  907. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  908. pages);
  909. return page;
  910. }
  911. static void setup_object(struct kmem_cache *s, struct page *page,
  912. void *object)
  913. {
  914. setup_object_debug(s, page, object);
  915. if (unlikely(s->ctor))
  916. s->ctor(s, object);
  917. }
  918. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  919. {
  920. struct page *page;
  921. struct kmem_cache_node *n;
  922. void *start;
  923. void *end;
  924. void *last;
  925. void *p;
  926. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  927. page = allocate_slab(s,
  928. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  929. if (!page)
  930. goto out;
  931. n = get_node(s, page_to_nid(page));
  932. if (n)
  933. atomic_long_inc(&n->nr_slabs);
  934. page->slab = s;
  935. page->flags |= 1 << PG_slab;
  936. if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
  937. SLAB_STORE_USER | SLAB_TRACE))
  938. SetSlabDebug(page);
  939. start = page_address(page);
  940. end = start + s->objects * s->size;
  941. if (unlikely(s->flags & SLAB_POISON))
  942. memset(start, POISON_INUSE, PAGE_SIZE << s->order);
  943. last = start;
  944. for_each_object(p, s, start) {
  945. setup_object(s, page, last);
  946. set_freepointer(s, last, p);
  947. last = p;
  948. }
  949. setup_object(s, page, last);
  950. set_freepointer(s, last, NULL);
  951. page->freelist = start;
  952. page->inuse = 0;
  953. out:
  954. return page;
  955. }
  956. static void __free_slab(struct kmem_cache *s, struct page *page)
  957. {
  958. int pages = 1 << s->order;
  959. if (unlikely(SlabDebug(page))) {
  960. void *p;
  961. slab_pad_check(s, page);
  962. for_each_object(p, s, page_address(page))
  963. check_object(s, page, p, 0);
  964. ClearSlabDebug(page);
  965. }
  966. mod_zone_page_state(page_zone(page),
  967. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  968. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  969. - pages);
  970. __free_pages(page, s->order);
  971. }
  972. static void rcu_free_slab(struct rcu_head *h)
  973. {
  974. struct page *page;
  975. page = container_of((struct list_head *)h, struct page, lru);
  976. __free_slab(page->slab, page);
  977. }
  978. static void free_slab(struct kmem_cache *s, struct page *page)
  979. {
  980. if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
  981. /*
  982. * RCU free overloads the RCU head over the LRU
  983. */
  984. struct rcu_head *head = (void *)&page->lru;
  985. call_rcu(head, rcu_free_slab);
  986. } else
  987. __free_slab(s, page);
  988. }
  989. static void discard_slab(struct kmem_cache *s, struct page *page)
  990. {
  991. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  992. atomic_long_dec(&n->nr_slabs);
  993. reset_page_mapcount(page);
  994. __ClearPageSlab(page);
  995. free_slab(s, page);
  996. }
  997. /*
  998. * Per slab locking using the pagelock
  999. */
  1000. static __always_inline void slab_lock(struct page *page)
  1001. {
  1002. bit_spin_lock(PG_locked, &page->flags);
  1003. }
  1004. static __always_inline void slab_unlock(struct page *page)
  1005. {
  1006. bit_spin_unlock(PG_locked, &page->flags);
  1007. }
  1008. static __always_inline int slab_trylock(struct page *page)
  1009. {
  1010. int rc = 1;
  1011. rc = bit_spin_trylock(PG_locked, &page->flags);
  1012. return rc;
  1013. }
  1014. /*
  1015. * Management of partially allocated slabs
  1016. */
  1017. static void add_partial_tail(struct kmem_cache_node *n, struct page *page)
  1018. {
  1019. spin_lock(&n->list_lock);
  1020. n->nr_partial++;
  1021. list_add_tail(&page->lru, &n->partial);
  1022. spin_unlock(&n->list_lock);
  1023. }
  1024. static void add_partial(struct kmem_cache_node *n, struct page *page)
  1025. {
  1026. spin_lock(&n->list_lock);
  1027. n->nr_partial++;
  1028. list_add(&page->lru, &n->partial);
  1029. spin_unlock(&n->list_lock);
  1030. }
  1031. static void remove_partial(struct kmem_cache *s,
  1032. struct page *page)
  1033. {
  1034. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1035. spin_lock(&n->list_lock);
  1036. list_del(&page->lru);
  1037. n->nr_partial--;
  1038. spin_unlock(&n->list_lock);
  1039. }
  1040. /*
  1041. * Lock slab and remove from the partial list.
  1042. *
  1043. * Must hold list_lock.
  1044. */
  1045. static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
  1046. {
  1047. if (slab_trylock(page)) {
  1048. list_del(&page->lru);
  1049. n->nr_partial--;
  1050. SetSlabFrozen(page);
  1051. return 1;
  1052. }
  1053. return 0;
  1054. }
  1055. /*
  1056. * Try to allocate a partial slab from a specific node.
  1057. */
  1058. static struct page *get_partial_node(struct kmem_cache_node *n)
  1059. {
  1060. struct page *page;
  1061. /*
  1062. * Racy check. If we mistakenly see no partial slabs then we
  1063. * just allocate an empty slab. If we mistakenly try to get a
  1064. * partial slab and there is none available then get_partials()
  1065. * will return NULL.
  1066. */
  1067. if (!n || !n->nr_partial)
  1068. return NULL;
  1069. spin_lock(&n->list_lock);
  1070. list_for_each_entry(page, &n->partial, lru)
  1071. if (lock_and_freeze_slab(n, page))
  1072. goto out;
  1073. page = NULL;
  1074. out:
  1075. spin_unlock(&n->list_lock);
  1076. return page;
  1077. }
  1078. /*
  1079. * Get a page from somewhere. Search in increasing NUMA distances.
  1080. */
  1081. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
  1082. {
  1083. #ifdef CONFIG_NUMA
  1084. struct zonelist *zonelist;
  1085. struct zone **z;
  1086. struct page *page;
  1087. /*
  1088. * The defrag ratio allows a configuration of the tradeoffs between
  1089. * inter node defragmentation and node local allocations. A lower
  1090. * defrag_ratio increases the tendency to do local allocations
  1091. * instead of attempting to obtain partial slabs from other nodes.
  1092. *
  1093. * If the defrag_ratio is set to 0 then kmalloc() always
  1094. * returns node local objects. If the ratio is higher then kmalloc()
  1095. * may return off node objects because partial slabs are obtained
  1096. * from other nodes and filled up.
  1097. *
  1098. * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
  1099. * defrag_ratio = 1000) then every (well almost) allocation will
  1100. * first attempt to defrag slab caches on other nodes. This means
  1101. * scanning over all nodes to look for partial slabs which may be
  1102. * expensive if we do it every time we are trying to find a slab
  1103. * with available objects.
  1104. */
  1105. if (!s->defrag_ratio || get_cycles() % 1024 > s->defrag_ratio)
  1106. return NULL;
  1107. zonelist = &NODE_DATA(slab_node(current->mempolicy))
  1108. ->node_zonelists[gfp_zone(flags)];
  1109. for (z = zonelist->zones; *z; z++) {
  1110. struct kmem_cache_node *n;
  1111. n = get_node(s, zone_to_nid(*z));
  1112. if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
  1113. n->nr_partial > MIN_PARTIAL) {
  1114. page = get_partial_node(n);
  1115. if (page)
  1116. return page;
  1117. }
  1118. }
  1119. #endif
  1120. return NULL;
  1121. }
  1122. /*
  1123. * Get a partial page, lock it and return it.
  1124. */
  1125. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  1126. {
  1127. struct page *page;
  1128. int searchnode = (node == -1) ? numa_node_id() : node;
  1129. page = get_partial_node(get_node(s, searchnode));
  1130. if (page || (flags & __GFP_THISNODE))
  1131. return page;
  1132. return get_any_partial(s, flags);
  1133. }
  1134. /*
  1135. * Move a page back to the lists.
  1136. *
  1137. * Must be called with the slab lock held.
  1138. *
  1139. * On exit the slab lock will have been dropped.
  1140. */
  1141. static void unfreeze_slab(struct kmem_cache *s, struct page *page)
  1142. {
  1143. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1144. ClearSlabFrozen(page);
  1145. if (page->inuse) {
  1146. if (page->freelist)
  1147. add_partial(n, page);
  1148. else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
  1149. add_full(n, page);
  1150. slab_unlock(page);
  1151. } else {
  1152. if (n->nr_partial < MIN_PARTIAL) {
  1153. /*
  1154. * Adding an empty slab to the partial slabs in order
  1155. * to avoid page allocator overhead. This slab needs
  1156. * to come after the other slabs with objects in
  1157. * order to fill them up. That way the size of the
  1158. * partial list stays small. kmem_cache_shrink can
  1159. * reclaim empty slabs from the partial list.
  1160. */
  1161. add_partial_tail(n, page);
  1162. slab_unlock(page);
  1163. } else {
  1164. slab_unlock(page);
  1165. discard_slab(s, page);
  1166. }
  1167. }
  1168. }
  1169. /*
  1170. * Remove the cpu slab
  1171. */
  1172. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1173. {
  1174. struct page *page = c->page;
  1175. /*
  1176. * Merge cpu freelist into freelist. Typically we get here
  1177. * because both freelists are empty. So this is unlikely
  1178. * to occur.
  1179. */
  1180. while (unlikely(c->freelist)) {
  1181. void **object;
  1182. /* Retrieve object from cpu_freelist */
  1183. object = c->freelist;
  1184. c->freelist = c->freelist[c->offset];
  1185. /* And put onto the regular freelist */
  1186. object[c->offset] = page->freelist;
  1187. page->freelist = object;
  1188. page->inuse--;
  1189. }
  1190. c->page = NULL;
  1191. unfreeze_slab(s, page);
  1192. }
  1193. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1194. {
  1195. slab_lock(c->page);
  1196. deactivate_slab(s, c);
  1197. }
  1198. /*
  1199. * Flush cpu slab.
  1200. * Called from IPI handler with interrupts disabled.
  1201. */
  1202. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1203. {
  1204. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1205. if (likely(c && c->page))
  1206. flush_slab(s, c);
  1207. }
  1208. static void flush_cpu_slab(void *d)
  1209. {
  1210. struct kmem_cache *s = d;
  1211. __flush_cpu_slab(s, smp_processor_id());
  1212. }
  1213. static void flush_all(struct kmem_cache *s)
  1214. {
  1215. #ifdef CONFIG_SMP
  1216. on_each_cpu(flush_cpu_slab, s, 1, 1);
  1217. #else
  1218. unsigned long flags;
  1219. local_irq_save(flags);
  1220. flush_cpu_slab(s);
  1221. local_irq_restore(flags);
  1222. #endif
  1223. }
  1224. /*
  1225. * Check if the objects in a per cpu structure fit numa
  1226. * locality expectations.
  1227. */
  1228. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1229. {
  1230. #ifdef CONFIG_NUMA
  1231. if (node != -1 && c->node != node)
  1232. return 0;
  1233. #endif
  1234. return 1;
  1235. }
  1236. /*
  1237. * Slow path. The lockless freelist is empty or we need to perform
  1238. * debugging duties.
  1239. *
  1240. * Interrupts are disabled.
  1241. *
  1242. * Processing is still very fast if new objects have been freed to the
  1243. * regular freelist. In that case we simply take over the regular freelist
  1244. * as the lockless freelist and zap the regular freelist.
  1245. *
  1246. * If that is not working then we fall back to the partial lists. We take the
  1247. * first element of the freelist as the object to allocate now and move the
  1248. * rest of the freelist to the lockless freelist.
  1249. *
  1250. * And if we were unable to get a new slab from the partial slab lists then
  1251. * we need to allocate a new slab. This is slowest path since we may sleep.
  1252. */
  1253. static void *__slab_alloc(struct kmem_cache *s,
  1254. gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
  1255. {
  1256. void **object;
  1257. struct page *new;
  1258. if (!c->page)
  1259. goto new_slab;
  1260. slab_lock(c->page);
  1261. if (unlikely(!node_match(c, node)))
  1262. goto another_slab;
  1263. load_freelist:
  1264. object = c->page->freelist;
  1265. if (unlikely(!object))
  1266. goto another_slab;
  1267. if (unlikely(SlabDebug(c->page)))
  1268. goto debug;
  1269. object = c->page->freelist;
  1270. c->freelist = object[c->offset];
  1271. c->page->inuse = s->objects;
  1272. c->page->freelist = NULL;
  1273. c->node = page_to_nid(c->page);
  1274. slab_unlock(c->page);
  1275. return object;
  1276. another_slab:
  1277. deactivate_slab(s, c);
  1278. new_slab:
  1279. new = get_partial(s, gfpflags, node);
  1280. if (new) {
  1281. c->page = new;
  1282. goto load_freelist;
  1283. }
  1284. if (gfpflags & __GFP_WAIT)
  1285. local_irq_enable();
  1286. new = new_slab(s, gfpflags, node);
  1287. if (gfpflags & __GFP_WAIT)
  1288. local_irq_disable();
  1289. if (new) {
  1290. c = get_cpu_slab(s, smp_processor_id());
  1291. if (c->page) {
  1292. /*
  1293. * Someone else populated the cpu_slab while we
  1294. * enabled interrupts, or we have gotten scheduled
  1295. * on another cpu. The page may not be on the
  1296. * requested node even if __GFP_THISNODE was
  1297. * specified. So we need to recheck.
  1298. */
  1299. if (node_match(c, node)) {
  1300. /*
  1301. * Current cpuslab is acceptable and we
  1302. * want the current one since its cache hot
  1303. */
  1304. discard_slab(s, new);
  1305. slab_lock(c->page);
  1306. goto load_freelist;
  1307. }
  1308. /* New slab does not fit our expectations */
  1309. flush_slab(s, c);
  1310. }
  1311. slab_lock(new);
  1312. SetSlabFrozen(new);
  1313. c->page = new;
  1314. goto load_freelist;
  1315. }
  1316. return NULL;
  1317. debug:
  1318. object = c->page->freelist;
  1319. if (!alloc_debug_processing(s, c->page, object, addr))
  1320. goto another_slab;
  1321. c->page->inuse++;
  1322. c->page->freelist = object[c->offset];
  1323. c->node = -1;
  1324. slab_unlock(c->page);
  1325. return object;
  1326. }
  1327. /*
  1328. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1329. * have the fastpath folded into their functions. So no function call
  1330. * overhead for requests that can be satisfied on the fastpath.
  1331. *
  1332. * The fastpath works by first checking if the lockless freelist can be used.
  1333. * If not then __slab_alloc is called for slow processing.
  1334. *
  1335. * Otherwise we can simply pick the next object from the lockless free list.
  1336. */
  1337. static void __always_inline *slab_alloc(struct kmem_cache *s,
  1338. gfp_t gfpflags, int node, void *addr)
  1339. {
  1340. void **object;
  1341. unsigned long flags;
  1342. struct kmem_cache_cpu *c;
  1343. local_irq_save(flags);
  1344. c = get_cpu_slab(s, smp_processor_id());
  1345. if (unlikely(!c->freelist || !node_match(c, node)))
  1346. object = __slab_alloc(s, gfpflags, node, addr, c);
  1347. else {
  1348. object = c->freelist;
  1349. c->freelist = object[c->offset];
  1350. }
  1351. local_irq_restore(flags);
  1352. if (unlikely((gfpflags & __GFP_ZERO) && object))
  1353. memset(object, 0, c->objsize);
  1354. return object;
  1355. }
  1356. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1357. {
  1358. return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
  1359. }
  1360. EXPORT_SYMBOL(kmem_cache_alloc);
  1361. #ifdef CONFIG_NUMA
  1362. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1363. {
  1364. return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
  1365. }
  1366. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1367. #endif
  1368. /*
  1369. * Slow patch handling. This may still be called frequently since objects
  1370. * have a longer lifetime than the cpu slabs in most processing loads.
  1371. *
  1372. * So we still attempt to reduce cache line usage. Just take the slab
  1373. * lock and free the item. If there is no additional partial page
  1374. * handling required then we can return immediately.
  1375. */
  1376. static void __slab_free(struct kmem_cache *s, struct page *page,
  1377. void *x, void *addr, unsigned int offset)
  1378. {
  1379. void *prior;
  1380. void **object = (void *)x;
  1381. slab_lock(page);
  1382. if (unlikely(SlabDebug(page)))
  1383. goto debug;
  1384. checks_ok:
  1385. prior = object[offset] = page->freelist;
  1386. page->freelist = object;
  1387. page->inuse--;
  1388. if (unlikely(SlabFrozen(page)))
  1389. goto out_unlock;
  1390. if (unlikely(!page->inuse))
  1391. goto slab_empty;
  1392. /*
  1393. * Objects left in the slab. If it
  1394. * was not on the partial list before
  1395. * then add it.
  1396. */
  1397. if (unlikely(!prior))
  1398. add_partial(get_node(s, page_to_nid(page)), page);
  1399. out_unlock:
  1400. slab_unlock(page);
  1401. return;
  1402. slab_empty:
  1403. if (prior)
  1404. /*
  1405. * Slab still on the partial list.
  1406. */
  1407. remove_partial(s, page);
  1408. slab_unlock(page);
  1409. discard_slab(s, page);
  1410. return;
  1411. debug:
  1412. if (!free_debug_processing(s, page, x, addr))
  1413. goto out_unlock;
  1414. goto checks_ok;
  1415. }
  1416. /*
  1417. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1418. * can perform fastpath freeing without additional function calls.
  1419. *
  1420. * The fastpath is only possible if we are freeing to the current cpu slab
  1421. * of this processor. This typically the case if we have just allocated
  1422. * the item before.
  1423. *
  1424. * If fastpath is not possible then fall back to __slab_free where we deal
  1425. * with all sorts of special processing.
  1426. */
  1427. static void __always_inline slab_free(struct kmem_cache *s,
  1428. struct page *page, void *x, void *addr)
  1429. {
  1430. void **object = (void *)x;
  1431. unsigned long flags;
  1432. struct kmem_cache_cpu *c;
  1433. local_irq_save(flags);
  1434. debug_check_no_locks_freed(object, s->objsize);
  1435. c = get_cpu_slab(s, smp_processor_id());
  1436. if (likely(page == c->page && c->node >= 0)) {
  1437. object[c->offset] = c->freelist;
  1438. c->freelist = object;
  1439. } else
  1440. __slab_free(s, page, x, addr, c->offset);
  1441. local_irq_restore(flags);
  1442. }
  1443. void kmem_cache_free(struct kmem_cache *s, void *x)
  1444. {
  1445. struct page *page;
  1446. page = virt_to_head_page(x);
  1447. slab_free(s, page, x, __builtin_return_address(0));
  1448. }
  1449. EXPORT_SYMBOL(kmem_cache_free);
  1450. /* Figure out on which slab object the object resides */
  1451. static struct page *get_object_page(const void *x)
  1452. {
  1453. struct page *page = virt_to_head_page(x);
  1454. if (!PageSlab(page))
  1455. return NULL;
  1456. return page;
  1457. }
  1458. /*
  1459. * Object placement in a slab is made very easy because we always start at
  1460. * offset 0. If we tune the size of the object to the alignment then we can
  1461. * get the required alignment by putting one properly sized object after
  1462. * another.
  1463. *
  1464. * Notice that the allocation order determines the sizes of the per cpu
  1465. * caches. Each processor has always one slab available for allocations.
  1466. * Increasing the allocation order reduces the number of times that slabs
  1467. * must be moved on and off the partial lists and is therefore a factor in
  1468. * locking overhead.
  1469. */
  1470. /*
  1471. * Mininum / Maximum order of slab pages. This influences locking overhead
  1472. * and slab fragmentation. A higher order reduces the number of partial slabs
  1473. * and increases the number of allocations possible without having to
  1474. * take the list_lock.
  1475. */
  1476. static int slub_min_order;
  1477. static int slub_max_order = DEFAULT_MAX_ORDER;
  1478. static int slub_min_objects = DEFAULT_MIN_OBJECTS;
  1479. /*
  1480. * Merge control. If this is set then no merging of slab caches will occur.
  1481. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1482. */
  1483. static int slub_nomerge;
  1484. /*
  1485. * Calculate the order of allocation given an slab object size.
  1486. *
  1487. * The order of allocation has significant impact on performance and other
  1488. * system components. Generally order 0 allocations should be preferred since
  1489. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1490. * be problematic to put into order 0 slabs because there may be too much
  1491. * unused space left. We go to a higher order if more than 1/8th of the slab
  1492. * would be wasted.
  1493. *
  1494. * In order to reach satisfactory performance we must ensure that a minimum
  1495. * number of objects is in one slab. Otherwise we may generate too much
  1496. * activity on the partial lists which requires taking the list_lock. This is
  1497. * less a concern for large slabs though which are rarely used.
  1498. *
  1499. * slub_max_order specifies the order where we begin to stop considering the
  1500. * number of objects in a slab as critical. If we reach slub_max_order then
  1501. * we try to keep the page order as low as possible. So we accept more waste
  1502. * of space in favor of a small page order.
  1503. *
  1504. * Higher order allocations also allow the placement of more objects in a
  1505. * slab and thereby reduce object handling overhead. If the user has
  1506. * requested a higher mininum order then we start with that one instead of
  1507. * the smallest order which will fit the object.
  1508. */
  1509. static inline int slab_order(int size, int min_objects,
  1510. int max_order, int fract_leftover)
  1511. {
  1512. int order;
  1513. int rem;
  1514. int min_order = slub_min_order;
  1515. for (order = max(min_order,
  1516. fls(min_objects * size - 1) - PAGE_SHIFT);
  1517. order <= max_order; order++) {
  1518. unsigned long slab_size = PAGE_SIZE << order;
  1519. if (slab_size < min_objects * size)
  1520. continue;
  1521. rem = slab_size % size;
  1522. if (rem <= slab_size / fract_leftover)
  1523. break;
  1524. }
  1525. return order;
  1526. }
  1527. static inline int calculate_order(int size)
  1528. {
  1529. int order;
  1530. int min_objects;
  1531. int fraction;
  1532. /*
  1533. * Attempt to find best configuration for a slab. This
  1534. * works by first attempting to generate a layout with
  1535. * the best configuration and backing off gradually.
  1536. *
  1537. * First we reduce the acceptable waste in a slab. Then
  1538. * we reduce the minimum objects required in a slab.
  1539. */
  1540. min_objects = slub_min_objects;
  1541. while (min_objects > 1) {
  1542. fraction = 8;
  1543. while (fraction >= 4) {
  1544. order = slab_order(size, min_objects,
  1545. slub_max_order, fraction);
  1546. if (order <= slub_max_order)
  1547. return order;
  1548. fraction /= 2;
  1549. }
  1550. min_objects /= 2;
  1551. }
  1552. /*
  1553. * We were unable to place multiple objects in a slab. Now
  1554. * lets see if we can place a single object there.
  1555. */
  1556. order = slab_order(size, 1, slub_max_order, 1);
  1557. if (order <= slub_max_order)
  1558. return order;
  1559. /*
  1560. * Doh this slab cannot be placed using slub_max_order.
  1561. */
  1562. order = slab_order(size, 1, MAX_ORDER, 1);
  1563. if (order <= MAX_ORDER)
  1564. return order;
  1565. return -ENOSYS;
  1566. }
  1567. /*
  1568. * Figure out what the alignment of the objects will be.
  1569. */
  1570. static unsigned long calculate_alignment(unsigned long flags,
  1571. unsigned long align, unsigned long size)
  1572. {
  1573. /*
  1574. * If the user wants hardware cache aligned objects then
  1575. * follow that suggestion if the object is sufficiently
  1576. * large.
  1577. *
  1578. * The hardware cache alignment cannot override the
  1579. * specified alignment though. If that is greater
  1580. * then use it.
  1581. */
  1582. if ((flags & SLAB_HWCACHE_ALIGN) &&
  1583. size > cache_line_size() / 2)
  1584. return max_t(unsigned long, align, cache_line_size());
  1585. if (align < ARCH_SLAB_MINALIGN)
  1586. return ARCH_SLAB_MINALIGN;
  1587. return ALIGN(align, sizeof(void *));
  1588. }
  1589. static void init_kmem_cache_cpu(struct kmem_cache *s,
  1590. struct kmem_cache_cpu *c)
  1591. {
  1592. c->page = NULL;
  1593. c->freelist = NULL;
  1594. c->node = 0;
  1595. c->offset = s->offset / sizeof(void *);
  1596. c->objsize = s->objsize;
  1597. }
  1598. static void init_kmem_cache_node(struct kmem_cache_node *n)
  1599. {
  1600. n->nr_partial = 0;
  1601. atomic_long_set(&n->nr_slabs, 0);
  1602. spin_lock_init(&n->list_lock);
  1603. INIT_LIST_HEAD(&n->partial);
  1604. #ifdef CONFIG_SLUB_DEBUG
  1605. INIT_LIST_HEAD(&n->full);
  1606. #endif
  1607. }
  1608. #ifdef CONFIG_SMP
  1609. /*
  1610. * Per cpu array for per cpu structures.
  1611. *
  1612. * The per cpu array places all kmem_cache_cpu structures from one processor
  1613. * close together meaning that it becomes possible that multiple per cpu
  1614. * structures are contained in one cacheline. This may be particularly
  1615. * beneficial for the kmalloc caches.
  1616. *
  1617. * A desktop system typically has around 60-80 slabs. With 100 here we are
  1618. * likely able to get per cpu structures for all caches from the array defined
  1619. * here. We must be able to cover all kmalloc caches during bootstrap.
  1620. *
  1621. * If the per cpu array is exhausted then fall back to kmalloc
  1622. * of individual cachelines. No sharing is possible then.
  1623. */
  1624. #define NR_KMEM_CACHE_CPU 100
  1625. static DEFINE_PER_CPU(struct kmem_cache_cpu,
  1626. kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
  1627. static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
  1628. static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
  1629. static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
  1630. int cpu, gfp_t flags)
  1631. {
  1632. struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
  1633. if (c)
  1634. per_cpu(kmem_cache_cpu_free, cpu) =
  1635. (void *)c->freelist;
  1636. else {
  1637. /* Table overflow: So allocate ourselves */
  1638. c = kmalloc_node(
  1639. ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
  1640. flags, cpu_to_node(cpu));
  1641. if (!c)
  1642. return NULL;
  1643. }
  1644. init_kmem_cache_cpu(s, c);
  1645. return c;
  1646. }
  1647. static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
  1648. {
  1649. if (c < per_cpu(kmem_cache_cpu, cpu) ||
  1650. c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
  1651. kfree(c);
  1652. return;
  1653. }
  1654. c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
  1655. per_cpu(kmem_cache_cpu_free, cpu) = c;
  1656. }
  1657. static void free_kmem_cache_cpus(struct kmem_cache *s)
  1658. {
  1659. int cpu;
  1660. for_each_online_cpu(cpu) {
  1661. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1662. if (c) {
  1663. s->cpu_slab[cpu] = NULL;
  1664. free_kmem_cache_cpu(c, cpu);
  1665. }
  1666. }
  1667. }
  1668. static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1669. {
  1670. int cpu;
  1671. for_each_online_cpu(cpu) {
  1672. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1673. if (c)
  1674. continue;
  1675. c = alloc_kmem_cache_cpu(s, cpu, flags);
  1676. if (!c) {
  1677. free_kmem_cache_cpus(s);
  1678. return 0;
  1679. }
  1680. s->cpu_slab[cpu] = c;
  1681. }
  1682. return 1;
  1683. }
  1684. /*
  1685. * Initialize the per cpu array.
  1686. */
  1687. static void init_alloc_cpu_cpu(int cpu)
  1688. {
  1689. int i;
  1690. if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
  1691. return;
  1692. for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
  1693. free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
  1694. cpu_set(cpu, kmem_cach_cpu_free_init_once);
  1695. }
  1696. static void __init init_alloc_cpu(void)
  1697. {
  1698. int cpu;
  1699. for_each_online_cpu(cpu)
  1700. init_alloc_cpu_cpu(cpu);
  1701. }
  1702. #else
  1703. static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
  1704. static inline void init_alloc_cpu(void) {}
  1705. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1706. {
  1707. init_kmem_cache_cpu(s, &s->cpu_slab);
  1708. return 1;
  1709. }
  1710. #endif
  1711. #ifdef CONFIG_NUMA
  1712. /*
  1713. * No kmalloc_node yet so do it by hand. We know that this is the first
  1714. * slab on the node for this slabcache. There are no concurrent accesses
  1715. * possible.
  1716. *
  1717. * Note that this function only works on the kmalloc_node_cache
  1718. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  1719. * memory on a fresh node that has no slab structures yet.
  1720. */
  1721. static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
  1722. int node)
  1723. {
  1724. struct page *page;
  1725. struct kmem_cache_node *n;
  1726. BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
  1727. page = new_slab(kmalloc_caches, gfpflags, node);
  1728. BUG_ON(!page);
  1729. if (page_to_nid(page) != node) {
  1730. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  1731. "node %d\n", node);
  1732. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  1733. "in order to be able to continue\n");
  1734. }
  1735. n = page->freelist;
  1736. BUG_ON(!n);
  1737. page->freelist = get_freepointer(kmalloc_caches, n);
  1738. page->inuse++;
  1739. kmalloc_caches->node[node] = n;
  1740. #ifdef CONFIG_SLUB_DEBUG
  1741. init_object(kmalloc_caches, n, 1);
  1742. init_tracking(kmalloc_caches, n);
  1743. #endif
  1744. init_kmem_cache_node(n);
  1745. atomic_long_inc(&n->nr_slabs);
  1746. add_partial(n, page);
  1747. return n;
  1748. }
  1749. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1750. {
  1751. int node;
  1752. for_each_node_state(node, N_NORMAL_MEMORY) {
  1753. struct kmem_cache_node *n = s->node[node];
  1754. if (n && n != &s->local_node)
  1755. kmem_cache_free(kmalloc_caches, n);
  1756. s->node[node] = NULL;
  1757. }
  1758. }
  1759. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1760. {
  1761. int node;
  1762. int local_node;
  1763. if (slab_state >= UP)
  1764. local_node = page_to_nid(virt_to_page(s));
  1765. else
  1766. local_node = 0;
  1767. for_each_node_state(node, N_NORMAL_MEMORY) {
  1768. struct kmem_cache_node *n;
  1769. if (local_node == node)
  1770. n = &s->local_node;
  1771. else {
  1772. if (slab_state == DOWN) {
  1773. n = early_kmem_cache_node_alloc(gfpflags,
  1774. node);
  1775. continue;
  1776. }
  1777. n = kmem_cache_alloc_node(kmalloc_caches,
  1778. gfpflags, node);
  1779. if (!n) {
  1780. free_kmem_cache_nodes(s);
  1781. return 0;
  1782. }
  1783. }
  1784. s->node[node] = n;
  1785. init_kmem_cache_node(n);
  1786. }
  1787. return 1;
  1788. }
  1789. #else
  1790. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1791. {
  1792. }
  1793. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1794. {
  1795. init_kmem_cache_node(&s->local_node);
  1796. return 1;
  1797. }
  1798. #endif
  1799. /*
  1800. * calculate_sizes() determines the order and the distribution of data within
  1801. * a slab object.
  1802. */
  1803. static int calculate_sizes(struct kmem_cache *s)
  1804. {
  1805. unsigned long flags = s->flags;
  1806. unsigned long size = s->objsize;
  1807. unsigned long align = s->align;
  1808. /*
  1809. * Determine if we can poison the object itself. If the user of
  1810. * the slab may touch the object after free or before allocation
  1811. * then we should never poison the object itself.
  1812. */
  1813. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  1814. !s->ctor)
  1815. s->flags |= __OBJECT_POISON;
  1816. else
  1817. s->flags &= ~__OBJECT_POISON;
  1818. /*
  1819. * Round up object size to the next word boundary. We can only
  1820. * place the free pointer at word boundaries and this determines
  1821. * the possible location of the free pointer.
  1822. */
  1823. size = ALIGN(size, sizeof(void *));
  1824. #ifdef CONFIG_SLUB_DEBUG
  1825. /*
  1826. * If we are Redzoning then check if there is some space between the
  1827. * end of the object and the free pointer. If not then add an
  1828. * additional word to have some bytes to store Redzone information.
  1829. */
  1830. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  1831. size += sizeof(void *);
  1832. #endif
  1833. /*
  1834. * With that we have determined the number of bytes in actual use
  1835. * by the object. This is the potential offset to the free pointer.
  1836. */
  1837. s->inuse = size;
  1838. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  1839. s->ctor)) {
  1840. /*
  1841. * Relocate free pointer after the object if it is not
  1842. * permitted to overwrite the first word of the object on
  1843. * kmem_cache_free.
  1844. *
  1845. * This is the case if we do RCU, have a constructor or
  1846. * destructor or are poisoning the objects.
  1847. */
  1848. s->offset = size;
  1849. size += sizeof(void *);
  1850. }
  1851. #ifdef CONFIG_SLUB_DEBUG
  1852. if (flags & SLAB_STORE_USER)
  1853. /*
  1854. * Need to store information about allocs and frees after
  1855. * the object.
  1856. */
  1857. size += 2 * sizeof(struct track);
  1858. if (flags & SLAB_RED_ZONE)
  1859. /*
  1860. * Add some empty padding so that we can catch
  1861. * overwrites from earlier objects rather than let
  1862. * tracking information or the free pointer be
  1863. * corrupted if an user writes before the start
  1864. * of the object.
  1865. */
  1866. size += sizeof(void *);
  1867. #endif
  1868. /*
  1869. * Determine the alignment based on various parameters that the
  1870. * user specified and the dynamic determination of cache line size
  1871. * on bootup.
  1872. */
  1873. align = calculate_alignment(flags, align, s->objsize);
  1874. /*
  1875. * SLUB stores one object immediately after another beginning from
  1876. * offset 0. In order to align the objects we have to simply size
  1877. * each object to conform to the alignment.
  1878. */
  1879. size = ALIGN(size, align);
  1880. s->size = size;
  1881. s->order = calculate_order(size);
  1882. if (s->order < 0)
  1883. return 0;
  1884. /*
  1885. * Determine the number of objects per slab
  1886. */
  1887. s->objects = (PAGE_SIZE << s->order) / size;
  1888. return !!s->objects;
  1889. }
  1890. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
  1891. const char *name, size_t size,
  1892. size_t align, unsigned long flags,
  1893. void (*ctor)(struct kmem_cache *, void *))
  1894. {
  1895. memset(s, 0, kmem_size);
  1896. s->name = name;
  1897. s->ctor = ctor;
  1898. s->objsize = size;
  1899. s->align = align;
  1900. s->flags = kmem_cache_flags(size, flags, name, ctor);
  1901. if (!calculate_sizes(s))
  1902. goto error;
  1903. s->refcount = 1;
  1904. #ifdef CONFIG_NUMA
  1905. s->defrag_ratio = 100;
  1906. #endif
  1907. if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
  1908. goto error;
  1909. if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
  1910. return 1;
  1911. free_kmem_cache_nodes(s);
  1912. error:
  1913. if (flags & SLAB_PANIC)
  1914. panic("Cannot create slab %s size=%lu realsize=%u "
  1915. "order=%u offset=%u flags=%lx\n",
  1916. s->name, (unsigned long)size, s->size, s->order,
  1917. s->offset, flags);
  1918. return 0;
  1919. }
  1920. /*
  1921. * Check if a given pointer is valid
  1922. */
  1923. int kmem_ptr_validate(struct kmem_cache *s, const void *object)
  1924. {
  1925. struct page * page;
  1926. page = get_object_page(object);
  1927. if (!page || s != page->slab)
  1928. /* No slab or wrong slab */
  1929. return 0;
  1930. if (!check_valid_pointer(s, page, object))
  1931. return 0;
  1932. /*
  1933. * We could also check if the object is on the slabs freelist.
  1934. * But this would be too expensive and it seems that the main
  1935. * purpose of kmem_ptr_valid is to check if the object belongs
  1936. * to a certain slab.
  1937. */
  1938. return 1;
  1939. }
  1940. EXPORT_SYMBOL(kmem_ptr_validate);
  1941. /*
  1942. * Determine the size of a slab object
  1943. */
  1944. unsigned int kmem_cache_size(struct kmem_cache *s)
  1945. {
  1946. return s->objsize;
  1947. }
  1948. EXPORT_SYMBOL(kmem_cache_size);
  1949. const char *kmem_cache_name(struct kmem_cache *s)
  1950. {
  1951. return s->name;
  1952. }
  1953. EXPORT_SYMBOL(kmem_cache_name);
  1954. /*
  1955. * Attempt to free all slabs on a node. Return the number of slabs we
  1956. * were unable to free.
  1957. */
  1958. static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
  1959. struct list_head *list)
  1960. {
  1961. int slabs_inuse = 0;
  1962. unsigned long flags;
  1963. struct page *page, *h;
  1964. spin_lock_irqsave(&n->list_lock, flags);
  1965. list_for_each_entry_safe(page, h, list, lru)
  1966. if (!page->inuse) {
  1967. list_del(&page->lru);
  1968. discard_slab(s, page);
  1969. } else
  1970. slabs_inuse++;
  1971. spin_unlock_irqrestore(&n->list_lock, flags);
  1972. return slabs_inuse;
  1973. }
  1974. /*
  1975. * Release all resources used by a slab cache.
  1976. */
  1977. static inline int kmem_cache_close(struct kmem_cache *s)
  1978. {
  1979. int node;
  1980. flush_all(s);
  1981. /* Attempt to free all objects */
  1982. free_kmem_cache_cpus(s);
  1983. for_each_node_state(node, N_NORMAL_MEMORY) {
  1984. struct kmem_cache_node *n = get_node(s, node);
  1985. n->nr_partial -= free_list(s, n, &n->partial);
  1986. if (atomic_long_read(&n->nr_slabs))
  1987. return 1;
  1988. }
  1989. free_kmem_cache_nodes(s);
  1990. return 0;
  1991. }
  1992. /*
  1993. * Close a cache and release the kmem_cache structure
  1994. * (must be used for caches created using kmem_cache_create)
  1995. */
  1996. void kmem_cache_destroy(struct kmem_cache *s)
  1997. {
  1998. down_write(&slub_lock);
  1999. s->refcount--;
  2000. if (!s->refcount) {
  2001. list_del(&s->list);
  2002. up_write(&slub_lock);
  2003. if (kmem_cache_close(s))
  2004. WARN_ON(1);
  2005. sysfs_slab_remove(s);
  2006. kfree(s);
  2007. } else
  2008. up_write(&slub_lock);
  2009. }
  2010. EXPORT_SYMBOL(kmem_cache_destroy);
  2011. /********************************************************************
  2012. * Kmalloc subsystem
  2013. *******************************************************************/
  2014. struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
  2015. EXPORT_SYMBOL(kmalloc_caches);
  2016. #ifdef CONFIG_ZONE_DMA
  2017. static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
  2018. #endif
  2019. static int __init setup_slub_min_order(char *str)
  2020. {
  2021. get_option (&str, &slub_min_order);
  2022. return 1;
  2023. }
  2024. __setup("slub_min_order=", setup_slub_min_order);
  2025. static int __init setup_slub_max_order(char *str)
  2026. {
  2027. get_option (&str, &slub_max_order);
  2028. return 1;
  2029. }
  2030. __setup("slub_max_order=", setup_slub_max_order);
  2031. static int __init setup_slub_min_objects(char *str)
  2032. {
  2033. get_option (&str, &slub_min_objects);
  2034. return 1;
  2035. }
  2036. __setup("slub_min_objects=", setup_slub_min_objects);
  2037. static int __init setup_slub_nomerge(char *str)
  2038. {
  2039. slub_nomerge = 1;
  2040. return 1;
  2041. }
  2042. __setup("slub_nomerge", setup_slub_nomerge);
  2043. static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
  2044. const char *name, int size, gfp_t gfp_flags)
  2045. {
  2046. unsigned int flags = 0;
  2047. if (gfp_flags & SLUB_DMA)
  2048. flags = SLAB_CACHE_DMA;
  2049. down_write(&slub_lock);
  2050. if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
  2051. flags, NULL))
  2052. goto panic;
  2053. list_add(&s->list, &slab_caches);
  2054. up_write(&slub_lock);
  2055. if (sysfs_slab_add(s))
  2056. goto panic;
  2057. return s;
  2058. panic:
  2059. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2060. }
  2061. #ifdef CONFIG_ZONE_DMA
  2062. static void sysfs_add_func(struct work_struct *w)
  2063. {
  2064. struct kmem_cache *s;
  2065. down_write(&slub_lock);
  2066. list_for_each_entry(s, &slab_caches, list) {
  2067. if (s->flags & __SYSFS_ADD_DEFERRED) {
  2068. s->flags &= ~__SYSFS_ADD_DEFERRED;
  2069. sysfs_slab_add(s);
  2070. }
  2071. }
  2072. up_write(&slub_lock);
  2073. }
  2074. static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
  2075. static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
  2076. {
  2077. struct kmem_cache *s;
  2078. char *text;
  2079. size_t realsize;
  2080. s = kmalloc_caches_dma[index];
  2081. if (s)
  2082. return s;
  2083. /* Dynamically create dma cache */
  2084. if (flags & __GFP_WAIT)
  2085. down_write(&slub_lock);
  2086. else {
  2087. if (!down_write_trylock(&slub_lock))
  2088. goto out;
  2089. }
  2090. if (kmalloc_caches_dma[index])
  2091. goto unlock_out;
  2092. realsize = kmalloc_caches[index].objsize;
  2093. text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
  2094. s = kmalloc(kmem_size, flags & ~SLUB_DMA);
  2095. if (!s || !text || !kmem_cache_open(s, flags, text,
  2096. realsize, ARCH_KMALLOC_MINALIGN,
  2097. SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
  2098. kfree(s);
  2099. kfree(text);
  2100. goto unlock_out;
  2101. }
  2102. list_add(&s->list, &slab_caches);
  2103. kmalloc_caches_dma[index] = s;
  2104. schedule_work(&sysfs_add_work);
  2105. unlock_out:
  2106. up_write(&slub_lock);
  2107. out:
  2108. return kmalloc_caches_dma[index];
  2109. }
  2110. #endif
  2111. /*
  2112. * Conversion table for small slabs sizes / 8 to the index in the
  2113. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2114. * of two cache sizes there. The size of larger slabs can be determined using
  2115. * fls.
  2116. */
  2117. static s8 size_index[24] = {
  2118. 3, /* 8 */
  2119. 4, /* 16 */
  2120. 5, /* 24 */
  2121. 5, /* 32 */
  2122. 6, /* 40 */
  2123. 6, /* 48 */
  2124. 6, /* 56 */
  2125. 6, /* 64 */
  2126. 1, /* 72 */
  2127. 1, /* 80 */
  2128. 1, /* 88 */
  2129. 1, /* 96 */
  2130. 7, /* 104 */
  2131. 7, /* 112 */
  2132. 7, /* 120 */
  2133. 7, /* 128 */
  2134. 2, /* 136 */
  2135. 2, /* 144 */
  2136. 2, /* 152 */
  2137. 2, /* 160 */
  2138. 2, /* 168 */
  2139. 2, /* 176 */
  2140. 2, /* 184 */
  2141. 2 /* 192 */
  2142. };
  2143. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2144. {
  2145. int index;
  2146. if (size <= 192) {
  2147. if (!size)
  2148. return ZERO_SIZE_PTR;
  2149. index = size_index[(size - 1) / 8];
  2150. } else
  2151. index = fls(size - 1);
  2152. #ifdef CONFIG_ZONE_DMA
  2153. if (unlikely((flags & SLUB_DMA)))
  2154. return dma_kmalloc_cache(index, flags);
  2155. #endif
  2156. return &kmalloc_caches[index];
  2157. }
  2158. void *__kmalloc(size_t size, gfp_t flags)
  2159. {
  2160. struct kmem_cache *s;
  2161. if (unlikely(size > PAGE_SIZE / 2))
  2162. return (void *)__get_free_pages(flags | __GFP_COMP,
  2163. get_order(size));
  2164. s = get_slab(size, flags);
  2165. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2166. return s;
  2167. return slab_alloc(s, flags, -1, __builtin_return_address(0));
  2168. }
  2169. EXPORT_SYMBOL(__kmalloc);
  2170. #ifdef CONFIG_NUMA
  2171. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2172. {
  2173. struct kmem_cache *s;
  2174. if (unlikely(size > PAGE_SIZE / 2))
  2175. return (void *)__get_free_pages(flags | __GFP_COMP,
  2176. get_order(size));
  2177. s = get_slab(size, flags);
  2178. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2179. return s;
  2180. return slab_alloc(s, flags, node, __builtin_return_address(0));
  2181. }
  2182. EXPORT_SYMBOL(__kmalloc_node);
  2183. #endif
  2184. size_t ksize(const void *object)
  2185. {
  2186. struct page *page;
  2187. struct kmem_cache *s;
  2188. BUG_ON(!object);
  2189. if (unlikely(object == ZERO_SIZE_PTR))
  2190. return 0;
  2191. page = get_object_page(object);
  2192. BUG_ON(!page);
  2193. s = page->slab;
  2194. BUG_ON(!s);
  2195. /*
  2196. * Debugging requires use of the padding between object
  2197. * and whatever may come after it.
  2198. */
  2199. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  2200. return s->objsize;
  2201. /*
  2202. * If we have the need to store the freelist pointer
  2203. * back there or track user information then we can
  2204. * only use the space before that information.
  2205. */
  2206. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  2207. return s->inuse;
  2208. /*
  2209. * Else we can use all the padding etc for the allocation
  2210. */
  2211. return s->size;
  2212. }
  2213. EXPORT_SYMBOL(ksize);
  2214. void kfree(const void *x)
  2215. {
  2216. struct page *page;
  2217. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2218. return;
  2219. page = virt_to_head_page(x);
  2220. if (unlikely(!PageSlab(page))) {
  2221. put_page(page);
  2222. return;
  2223. }
  2224. slab_free(page->slab, page, (void *)x, __builtin_return_address(0));
  2225. }
  2226. EXPORT_SYMBOL(kfree);
  2227. /*
  2228. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2229. * the remaining slabs by the number of items in use. The slabs with the
  2230. * most items in use come first. New allocations will then fill those up
  2231. * and thus they can be removed from the partial lists.
  2232. *
  2233. * The slabs with the least items are placed last. This results in them
  2234. * being allocated from last increasing the chance that the last objects
  2235. * are freed in them.
  2236. */
  2237. int kmem_cache_shrink(struct kmem_cache *s)
  2238. {
  2239. int node;
  2240. int i;
  2241. struct kmem_cache_node *n;
  2242. struct page *page;
  2243. struct page *t;
  2244. struct list_head *slabs_by_inuse =
  2245. kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
  2246. unsigned long flags;
  2247. if (!slabs_by_inuse)
  2248. return -ENOMEM;
  2249. flush_all(s);
  2250. for_each_node_state(node, N_NORMAL_MEMORY) {
  2251. n = get_node(s, node);
  2252. if (!n->nr_partial)
  2253. continue;
  2254. for (i = 0; i < s->objects; i++)
  2255. INIT_LIST_HEAD(slabs_by_inuse + i);
  2256. spin_lock_irqsave(&n->list_lock, flags);
  2257. /*
  2258. * Build lists indexed by the items in use in each slab.
  2259. *
  2260. * Note that concurrent frees may occur while we hold the
  2261. * list_lock. page->inuse here is the upper limit.
  2262. */
  2263. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2264. if (!page->inuse && slab_trylock(page)) {
  2265. /*
  2266. * Must hold slab lock here because slab_free
  2267. * may have freed the last object and be
  2268. * waiting to release the slab.
  2269. */
  2270. list_del(&page->lru);
  2271. n->nr_partial--;
  2272. slab_unlock(page);
  2273. discard_slab(s, page);
  2274. } else {
  2275. list_move(&page->lru,
  2276. slabs_by_inuse + page->inuse);
  2277. }
  2278. }
  2279. /*
  2280. * Rebuild the partial list with the slabs filled up most
  2281. * first and the least used slabs at the end.
  2282. */
  2283. for (i = s->objects - 1; i >= 0; i--)
  2284. list_splice(slabs_by_inuse + i, n->partial.prev);
  2285. spin_unlock_irqrestore(&n->list_lock, flags);
  2286. }
  2287. kfree(slabs_by_inuse);
  2288. return 0;
  2289. }
  2290. EXPORT_SYMBOL(kmem_cache_shrink);
  2291. /********************************************************************
  2292. * Basic setup of slabs
  2293. *******************************************************************/
  2294. void __init kmem_cache_init(void)
  2295. {
  2296. int i;
  2297. int caches = 0;
  2298. init_alloc_cpu();
  2299. #ifdef CONFIG_NUMA
  2300. /*
  2301. * Must first have the slab cache available for the allocations of the
  2302. * struct kmem_cache_node's. There is special bootstrap code in
  2303. * kmem_cache_open for slab_state == DOWN.
  2304. */
  2305. create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
  2306. sizeof(struct kmem_cache_node), GFP_KERNEL);
  2307. kmalloc_caches[0].refcount = -1;
  2308. caches++;
  2309. #endif
  2310. /* Able to allocate the per node structures */
  2311. slab_state = PARTIAL;
  2312. /* Caches that are not of the two-to-the-power-of size */
  2313. if (KMALLOC_MIN_SIZE <= 64) {
  2314. create_kmalloc_cache(&kmalloc_caches[1],
  2315. "kmalloc-96", 96, GFP_KERNEL);
  2316. caches++;
  2317. }
  2318. if (KMALLOC_MIN_SIZE <= 128) {
  2319. create_kmalloc_cache(&kmalloc_caches[2],
  2320. "kmalloc-192", 192, GFP_KERNEL);
  2321. caches++;
  2322. }
  2323. for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
  2324. create_kmalloc_cache(&kmalloc_caches[i],
  2325. "kmalloc", 1 << i, GFP_KERNEL);
  2326. caches++;
  2327. }
  2328. /*
  2329. * Patch up the size_index table if we have strange large alignment
  2330. * requirements for the kmalloc array. This is only the case for
  2331. * mips it seems. The standard arches will not generate any code here.
  2332. *
  2333. * Largest permitted alignment is 256 bytes due to the way we
  2334. * handle the index determination for the smaller caches.
  2335. *
  2336. * Make sure that nothing crazy happens if someone starts tinkering
  2337. * around with ARCH_KMALLOC_MINALIGN
  2338. */
  2339. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2340. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2341. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
  2342. size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
  2343. slab_state = UP;
  2344. /* Provide the correct kmalloc names now that the caches are up */
  2345. for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
  2346. kmalloc_caches[i]. name =
  2347. kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
  2348. #ifdef CONFIG_SMP
  2349. register_cpu_notifier(&slab_notifier);
  2350. kmem_size = offsetof(struct kmem_cache, cpu_slab) +
  2351. nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
  2352. #else
  2353. kmem_size = sizeof(struct kmem_cache);
  2354. #endif
  2355. printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2356. " CPUs=%d, Nodes=%d\n",
  2357. caches, cache_line_size(),
  2358. slub_min_order, slub_max_order, slub_min_objects,
  2359. nr_cpu_ids, nr_node_ids);
  2360. }
  2361. /*
  2362. * Find a mergeable slab cache
  2363. */
  2364. static int slab_unmergeable(struct kmem_cache *s)
  2365. {
  2366. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2367. return 1;
  2368. if (s->ctor)
  2369. return 1;
  2370. /*
  2371. * We may have set a slab to be unmergeable during bootstrap.
  2372. */
  2373. if (s->refcount < 0)
  2374. return 1;
  2375. return 0;
  2376. }
  2377. static struct kmem_cache *find_mergeable(size_t size,
  2378. size_t align, unsigned long flags, const char *name,
  2379. void (*ctor)(struct kmem_cache *, void *))
  2380. {
  2381. struct kmem_cache *s;
  2382. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2383. return NULL;
  2384. if (ctor)
  2385. return NULL;
  2386. size = ALIGN(size, sizeof(void *));
  2387. align = calculate_alignment(flags, align, size);
  2388. size = ALIGN(size, align);
  2389. flags = kmem_cache_flags(size, flags, name, NULL);
  2390. list_for_each_entry(s, &slab_caches, list) {
  2391. if (slab_unmergeable(s))
  2392. continue;
  2393. if (size > s->size)
  2394. continue;
  2395. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2396. continue;
  2397. /*
  2398. * Check if alignment is compatible.
  2399. * Courtesy of Adrian Drzewiecki
  2400. */
  2401. if ((s->size & ~(align -1)) != s->size)
  2402. continue;
  2403. if (s->size - size >= sizeof(void *))
  2404. continue;
  2405. return s;
  2406. }
  2407. return NULL;
  2408. }
  2409. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2410. size_t align, unsigned long flags,
  2411. void (*ctor)(struct kmem_cache *, void *))
  2412. {
  2413. struct kmem_cache *s;
  2414. down_write(&slub_lock);
  2415. s = find_mergeable(size, align, flags, name, ctor);
  2416. if (s) {
  2417. int cpu;
  2418. s->refcount++;
  2419. /*
  2420. * Adjust the object sizes so that we clear
  2421. * the complete object on kzalloc.
  2422. */
  2423. s->objsize = max(s->objsize, (int)size);
  2424. /*
  2425. * And then we need to update the object size in the
  2426. * per cpu structures
  2427. */
  2428. for_each_online_cpu(cpu)
  2429. get_cpu_slab(s, cpu)->objsize = s->objsize;
  2430. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2431. up_write(&slub_lock);
  2432. if (sysfs_slab_alias(s, name))
  2433. goto err;
  2434. return s;
  2435. }
  2436. s = kmalloc(kmem_size, GFP_KERNEL);
  2437. if (s) {
  2438. if (kmem_cache_open(s, GFP_KERNEL, name,
  2439. size, align, flags, ctor)) {
  2440. list_add(&s->list, &slab_caches);
  2441. up_write(&slub_lock);
  2442. if (sysfs_slab_add(s))
  2443. goto err;
  2444. return s;
  2445. }
  2446. kfree(s);
  2447. }
  2448. up_write(&slub_lock);
  2449. err:
  2450. if (flags & SLAB_PANIC)
  2451. panic("Cannot create slabcache %s\n", name);
  2452. else
  2453. s = NULL;
  2454. return s;
  2455. }
  2456. EXPORT_SYMBOL(kmem_cache_create);
  2457. #ifdef CONFIG_SMP
  2458. /*
  2459. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2460. * necessary.
  2461. */
  2462. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2463. unsigned long action, void *hcpu)
  2464. {
  2465. long cpu = (long)hcpu;
  2466. struct kmem_cache *s;
  2467. unsigned long flags;
  2468. switch (action) {
  2469. case CPU_UP_PREPARE:
  2470. case CPU_UP_PREPARE_FROZEN:
  2471. init_alloc_cpu_cpu(cpu);
  2472. down_read(&slub_lock);
  2473. list_for_each_entry(s, &slab_caches, list)
  2474. s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
  2475. GFP_KERNEL);
  2476. up_read(&slub_lock);
  2477. break;
  2478. case CPU_UP_CANCELED:
  2479. case CPU_UP_CANCELED_FROZEN:
  2480. case CPU_DEAD:
  2481. case CPU_DEAD_FROZEN:
  2482. down_read(&slub_lock);
  2483. list_for_each_entry(s, &slab_caches, list) {
  2484. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2485. local_irq_save(flags);
  2486. __flush_cpu_slab(s, cpu);
  2487. local_irq_restore(flags);
  2488. free_kmem_cache_cpu(c, cpu);
  2489. s->cpu_slab[cpu] = NULL;
  2490. }
  2491. up_read(&slub_lock);
  2492. break;
  2493. default:
  2494. break;
  2495. }
  2496. return NOTIFY_OK;
  2497. }
  2498. static struct notifier_block __cpuinitdata slab_notifier =
  2499. { &slab_cpuup_callback, NULL, 0 };
  2500. #endif
  2501. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
  2502. {
  2503. struct kmem_cache *s;
  2504. if (unlikely(size > PAGE_SIZE / 2))
  2505. return (void *)__get_free_pages(gfpflags | __GFP_COMP,
  2506. get_order(size));
  2507. s = get_slab(size, gfpflags);
  2508. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2509. return s;
  2510. return slab_alloc(s, gfpflags, -1, caller);
  2511. }
  2512. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2513. int node, void *caller)
  2514. {
  2515. struct kmem_cache *s;
  2516. if (unlikely(size > PAGE_SIZE / 2))
  2517. return (void *)__get_free_pages(gfpflags | __GFP_COMP,
  2518. get_order(size));
  2519. s = get_slab(size, gfpflags);
  2520. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2521. return s;
  2522. return slab_alloc(s, gfpflags, node, caller);
  2523. }
  2524. #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
  2525. static int validate_slab(struct kmem_cache *s, struct page *page,
  2526. unsigned long *map)
  2527. {
  2528. void *p;
  2529. void *addr = page_address(page);
  2530. if (!check_slab(s, page) ||
  2531. !on_freelist(s, page, NULL))
  2532. return 0;
  2533. /* Now we know that a valid freelist exists */
  2534. bitmap_zero(map, s->objects);
  2535. for_each_free_object(p, s, page->freelist) {
  2536. set_bit(slab_index(p, s, addr), map);
  2537. if (!check_object(s, page, p, 0))
  2538. return 0;
  2539. }
  2540. for_each_object(p, s, addr)
  2541. if (!test_bit(slab_index(p, s, addr), map))
  2542. if (!check_object(s, page, p, 1))
  2543. return 0;
  2544. return 1;
  2545. }
  2546. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  2547. unsigned long *map)
  2548. {
  2549. if (slab_trylock(page)) {
  2550. validate_slab(s, page, map);
  2551. slab_unlock(page);
  2552. } else
  2553. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  2554. s->name, page);
  2555. if (s->flags & DEBUG_DEFAULT_FLAGS) {
  2556. if (!SlabDebug(page))
  2557. printk(KERN_ERR "SLUB %s: SlabDebug not set "
  2558. "on slab 0x%p\n", s->name, page);
  2559. } else {
  2560. if (SlabDebug(page))
  2561. printk(KERN_ERR "SLUB %s: SlabDebug set on "
  2562. "slab 0x%p\n", s->name, page);
  2563. }
  2564. }
  2565. static int validate_slab_node(struct kmem_cache *s,
  2566. struct kmem_cache_node *n, unsigned long *map)
  2567. {
  2568. unsigned long count = 0;
  2569. struct page *page;
  2570. unsigned long flags;
  2571. spin_lock_irqsave(&n->list_lock, flags);
  2572. list_for_each_entry(page, &n->partial, lru) {
  2573. validate_slab_slab(s, page, map);
  2574. count++;
  2575. }
  2576. if (count != n->nr_partial)
  2577. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  2578. "counter=%ld\n", s->name, count, n->nr_partial);
  2579. if (!(s->flags & SLAB_STORE_USER))
  2580. goto out;
  2581. list_for_each_entry(page, &n->full, lru) {
  2582. validate_slab_slab(s, page, map);
  2583. count++;
  2584. }
  2585. if (count != atomic_long_read(&n->nr_slabs))
  2586. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  2587. "counter=%ld\n", s->name, count,
  2588. atomic_long_read(&n->nr_slabs));
  2589. out:
  2590. spin_unlock_irqrestore(&n->list_lock, flags);
  2591. return count;
  2592. }
  2593. static long validate_slab_cache(struct kmem_cache *s)
  2594. {
  2595. int node;
  2596. unsigned long count = 0;
  2597. unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
  2598. sizeof(unsigned long), GFP_KERNEL);
  2599. if (!map)
  2600. return -ENOMEM;
  2601. flush_all(s);
  2602. for_each_node_state(node, N_NORMAL_MEMORY) {
  2603. struct kmem_cache_node *n = get_node(s, node);
  2604. count += validate_slab_node(s, n, map);
  2605. }
  2606. kfree(map);
  2607. return count;
  2608. }
  2609. #ifdef SLUB_RESILIENCY_TEST
  2610. static void resiliency_test(void)
  2611. {
  2612. u8 *p;
  2613. printk(KERN_ERR "SLUB resiliency testing\n");
  2614. printk(KERN_ERR "-----------------------\n");
  2615. printk(KERN_ERR "A. Corruption after allocation\n");
  2616. p = kzalloc(16, GFP_KERNEL);
  2617. p[16] = 0x12;
  2618. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  2619. " 0x12->0x%p\n\n", p + 16);
  2620. validate_slab_cache(kmalloc_caches + 4);
  2621. /* Hmmm... The next two are dangerous */
  2622. p = kzalloc(32, GFP_KERNEL);
  2623. p[32 + sizeof(void *)] = 0x34;
  2624. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  2625. " 0x34 -> -0x%p\n", p);
  2626. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2627. validate_slab_cache(kmalloc_caches + 5);
  2628. p = kzalloc(64, GFP_KERNEL);
  2629. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  2630. *p = 0x56;
  2631. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  2632. p);
  2633. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2634. validate_slab_cache(kmalloc_caches + 6);
  2635. printk(KERN_ERR "\nB. Corruption after free\n");
  2636. p = kzalloc(128, GFP_KERNEL);
  2637. kfree(p);
  2638. *p = 0x78;
  2639. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  2640. validate_slab_cache(kmalloc_caches + 7);
  2641. p = kzalloc(256, GFP_KERNEL);
  2642. kfree(p);
  2643. p[50] = 0x9a;
  2644. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
  2645. validate_slab_cache(kmalloc_caches + 8);
  2646. p = kzalloc(512, GFP_KERNEL);
  2647. kfree(p);
  2648. p[512] = 0xab;
  2649. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  2650. validate_slab_cache(kmalloc_caches + 9);
  2651. }
  2652. #else
  2653. static void resiliency_test(void) {};
  2654. #endif
  2655. /*
  2656. * Generate lists of code addresses where slabcache objects are allocated
  2657. * and freed.
  2658. */
  2659. struct location {
  2660. unsigned long count;
  2661. void *addr;
  2662. long long sum_time;
  2663. long min_time;
  2664. long max_time;
  2665. long min_pid;
  2666. long max_pid;
  2667. cpumask_t cpus;
  2668. nodemask_t nodes;
  2669. };
  2670. struct loc_track {
  2671. unsigned long max;
  2672. unsigned long count;
  2673. struct location *loc;
  2674. };
  2675. static void free_loc_track(struct loc_track *t)
  2676. {
  2677. if (t->max)
  2678. free_pages((unsigned long)t->loc,
  2679. get_order(sizeof(struct location) * t->max));
  2680. }
  2681. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  2682. {
  2683. struct location *l;
  2684. int order;
  2685. order = get_order(sizeof(struct location) * max);
  2686. l = (void *)__get_free_pages(flags, order);
  2687. if (!l)
  2688. return 0;
  2689. if (t->count) {
  2690. memcpy(l, t->loc, sizeof(struct location) * t->count);
  2691. free_loc_track(t);
  2692. }
  2693. t->max = max;
  2694. t->loc = l;
  2695. return 1;
  2696. }
  2697. static int add_location(struct loc_track *t, struct kmem_cache *s,
  2698. const struct track *track)
  2699. {
  2700. long start, end, pos;
  2701. struct location *l;
  2702. void *caddr;
  2703. unsigned long age = jiffies - track->when;
  2704. start = -1;
  2705. end = t->count;
  2706. for ( ; ; ) {
  2707. pos = start + (end - start + 1) / 2;
  2708. /*
  2709. * There is nothing at "end". If we end up there
  2710. * we need to add something to before end.
  2711. */
  2712. if (pos == end)
  2713. break;
  2714. caddr = t->loc[pos].addr;
  2715. if (track->addr == caddr) {
  2716. l = &t->loc[pos];
  2717. l->count++;
  2718. if (track->when) {
  2719. l->sum_time += age;
  2720. if (age < l->min_time)
  2721. l->min_time = age;
  2722. if (age > l->max_time)
  2723. l->max_time = age;
  2724. if (track->pid < l->min_pid)
  2725. l->min_pid = track->pid;
  2726. if (track->pid > l->max_pid)
  2727. l->max_pid = track->pid;
  2728. cpu_set(track->cpu, l->cpus);
  2729. }
  2730. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2731. return 1;
  2732. }
  2733. if (track->addr < caddr)
  2734. end = pos;
  2735. else
  2736. start = pos;
  2737. }
  2738. /*
  2739. * Not found. Insert new tracking element.
  2740. */
  2741. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  2742. return 0;
  2743. l = t->loc + pos;
  2744. if (pos < t->count)
  2745. memmove(l + 1, l,
  2746. (t->count - pos) * sizeof(struct location));
  2747. t->count++;
  2748. l->count = 1;
  2749. l->addr = track->addr;
  2750. l->sum_time = age;
  2751. l->min_time = age;
  2752. l->max_time = age;
  2753. l->min_pid = track->pid;
  2754. l->max_pid = track->pid;
  2755. cpus_clear(l->cpus);
  2756. cpu_set(track->cpu, l->cpus);
  2757. nodes_clear(l->nodes);
  2758. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2759. return 1;
  2760. }
  2761. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  2762. struct page *page, enum track_item alloc)
  2763. {
  2764. void *addr = page_address(page);
  2765. DECLARE_BITMAP(map, s->objects);
  2766. void *p;
  2767. bitmap_zero(map, s->objects);
  2768. for_each_free_object(p, s, page->freelist)
  2769. set_bit(slab_index(p, s, addr), map);
  2770. for_each_object(p, s, addr)
  2771. if (!test_bit(slab_index(p, s, addr), map))
  2772. add_location(t, s, get_track(s, p, alloc));
  2773. }
  2774. static int list_locations(struct kmem_cache *s, char *buf,
  2775. enum track_item alloc)
  2776. {
  2777. int n = 0;
  2778. unsigned long i;
  2779. struct loc_track t = { 0, 0, NULL };
  2780. int node;
  2781. if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  2782. GFP_TEMPORARY))
  2783. return sprintf(buf, "Out of memory\n");
  2784. /* Push back cpu slabs */
  2785. flush_all(s);
  2786. for_each_node_state(node, N_NORMAL_MEMORY) {
  2787. struct kmem_cache_node *n = get_node(s, node);
  2788. unsigned long flags;
  2789. struct page *page;
  2790. if (!atomic_long_read(&n->nr_slabs))
  2791. continue;
  2792. spin_lock_irqsave(&n->list_lock, flags);
  2793. list_for_each_entry(page, &n->partial, lru)
  2794. process_slab(&t, s, page, alloc);
  2795. list_for_each_entry(page, &n->full, lru)
  2796. process_slab(&t, s, page, alloc);
  2797. spin_unlock_irqrestore(&n->list_lock, flags);
  2798. }
  2799. for (i = 0; i < t.count; i++) {
  2800. struct location *l = &t.loc[i];
  2801. if (n > PAGE_SIZE - 100)
  2802. break;
  2803. n += sprintf(buf + n, "%7ld ", l->count);
  2804. if (l->addr)
  2805. n += sprint_symbol(buf + n, (unsigned long)l->addr);
  2806. else
  2807. n += sprintf(buf + n, "<not-available>");
  2808. if (l->sum_time != l->min_time) {
  2809. unsigned long remainder;
  2810. n += sprintf(buf + n, " age=%ld/%ld/%ld",
  2811. l->min_time,
  2812. div_long_long_rem(l->sum_time, l->count, &remainder),
  2813. l->max_time);
  2814. } else
  2815. n += sprintf(buf + n, " age=%ld",
  2816. l->min_time);
  2817. if (l->min_pid != l->max_pid)
  2818. n += sprintf(buf + n, " pid=%ld-%ld",
  2819. l->min_pid, l->max_pid);
  2820. else
  2821. n += sprintf(buf + n, " pid=%ld",
  2822. l->min_pid);
  2823. if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
  2824. n < PAGE_SIZE - 60) {
  2825. n += sprintf(buf + n, " cpus=");
  2826. n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
  2827. l->cpus);
  2828. }
  2829. if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
  2830. n < PAGE_SIZE - 60) {
  2831. n += sprintf(buf + n, " nodes=");
  2832. n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
  2833. l->nodes);
  2834. }
  2835. n += sprintf(buf + n, "\n");
  2836. }
  2837. free_loc_track(&t);
  2838. if (!t.count)
  2839. n += sprintf(buf, "No data\n");
  2840. return n;
  2841. }
  2842. static unsigned long count_partial(struct kmem_cache_node *n)
  2843. {
  2844. unsigned long flags;
  2845. unsigned long x = 0;
  2846. struct page *page;
  2847. spin_lock_irqsave(&n->list_lock, flags);
  2848. list_for_each_entry(page, &n->partial, lru)
  2849. x += page->inuse;
  2850. spin_unlock_irqrestore(&n->list_lock, flags);
  2851. return x;
  2852. }
  2853. enum slab_stat_type {
  2854. SL_FULL,
  2855. SL_PARTIAL,
  2856. SL_CPU,
  2857. SL_OBJECTS
  2858. };
  2859. #define SO_FULL (1 << SL_FULL)
  2860. #define SO_PARTIAL (1 << SL_PARTIAL)
  2861. #define SO_CPU (1 << SL_CPU)
  2862. #define SO_OBJECTS (1 << SL_OBJECTS)
  2863. static unsigned long slab_objects(struct kmem_cache *s,
  2864. char *buf, unsigned long flags)
  2865. {
  2866. unsigned long total = 0;
  2867. int cpu;
  2868. int node;
  2869. int x;
  2870. unsigned long *nodes;
  2871. unsigned long *per_cpu;
  2872. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  2873. per_cpu = nodes + nr_node_ids;
  2874. for_each_possible_cpu(cpu) {
  2875. struct page *page;
  2876. int node;
  2877. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2878. if (!c)
  2879. continue;
  2880. page = c->page;
  2881. node = c->node;
  2882. if (node < 0)
  2883. continue;
  2884. if (page) {
  2885. if (flags & SO_CPU) {
  2886. int x = 0;
  2887. if (flags & SO_OBJECTS)
  2888. x = page->inuse;
  2889. else
  2890. x = 1;
  2891. total += x;
  2892. nodes[node] += x;
  2893. }
  2894. per_cpu[node]++;
  2895. }
  2896. }
  2897. for_each_node_state(node, N_NORMAL_MEMORY) {
  2898. struct kmem_cache_node *n = get_node(s, node);
  2899. if (flags & SO_PARTIAL) {
  2900. if (flags & SO_OBJECTS)
  2901. x = count_partial(n);
  2902. else
  2903. x = n->nr_partial;
  2904. total += x;
  2905. nodes[node] += x;
  2906. }
  2907. if (flags & SO_FULL) {
  2908. int full_slabs = atomic_long_read(&n->nr_slabs)
  2909. - per_cpu[node]
  2910. - n->nr_partial;
  2911. if (flags & SO_OBJECTS)
  2912. x = full_slabs * s->objects;
  2913. else
  2914. x = full_slabs;
  2915. total += x;
  2916. nodes[node] += x;
  2917. }
  2918. }
  2919. x = sprintf(buf, "%lu", total);
  2920. #ifdef CONFIG_NUMA
  2921. for_each_node_state(node, N_NORMAL_MEMORY)
  2922. if (nodes[node])
  2923. x += sprintf(buf + x, " N%d=%lu",
  2924. node, nodes[node]);
  2925. #endif
  2926. kfree(nodes);
  2927. return x + sprintf(buf + x, "\n");
  2928. }
  2929. static int any_slab_objects(struct kmem_cache *s)
  2930. {
  2931. int node;
  2932. int cpu;
  2933. for_each_possible_cpu(cpu) {
  2934. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2935. if (c && c->page)
  2936. return 1;
  2937. }
  2938. for_each_online_node(node) {
  2939. struct kmem_cache_node *n = get_node(s, node);
  2940. if (!n)
  2941. continue;
  2942. if (n->nr_partial || atomic_long_read(&n->nr_slabs))
  2943. return 1;
  2944. }
  2945. return 0;
  2946. }
  2947. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  2948. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  2949. struct slab_attribute {
  2950. struct attribute attr;
  2951. ssize_t (*show)(struct kmem_cache *s, char *buf);
  2952. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  2953. };
  2954. #define SLAB_ATTR_RO(_name) \
  2955. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  2956. #define SLAB_ATTR(_name) \
  2957. static struct slab_attribute _name##_attr = \
  2958. __ATTR(_name, 0644, _name##_show, _name##_store)
  2959. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  2960. {
  2961. return sprintf(buf, "%d\n", s->size);
  2962. }
  2963. SLAB_ATTR_RO(slab_size);
  2964. static ssize_t align_show(struct kmem_cache *s, char *buf)
  2965. {
  2966. return sprintf(buf, "%d\n", s->align);
  2967. }
  2968. SLAB_ATTR_RO(align);
  2969. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  2970. {
  2971. return sprintf(buf, "%d\n", s->objsize);
  2972. }
  2973. SLAB_ATTR_RO(object_size);
  2974. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  2975. {
  2976. return sprintf(buf, "%d\n", s->objects);
  2977. }
  2978. SLAB_ATTR_RO(objs_per_slab);
  2979. static ssize_t order_show(struct kmem_cache *s, char *buf)
  2980. {
  2981. return sprintf(buf, "%d\n", s->order);
  2982. }
  2983. SLAB_ATTR_RO(order);
  2984. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  2985. {
  2986. if (s->ctor) {
  2987. int n = sprint_symbol(buf, (unsigned long)s->ctor);
  2988. return n + sprintf(buf + n, "\n");
  2989. }
  2990. return 0;
  2991. }
  2992. SLAB_ATTR_RO(ctor);
  2993. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  2994. {
  2995. return sprintf(buf, "%d\n", s->refcount - 1);
  2996. }
  2997. SLAB_ATTR_RO(aliases);
  2998. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  2999. {
  3000. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
  3001. }
  3002. SLAB_ATTR_RO(slabs);
  3003. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3004. {
  3005. return slab_objects(s, buf, SO_PARTIAL);
  3006. }
  3007. SLAB_ATTR_RO(partial);
  3008. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3009. {
  3010. return slab_objects(s, buf, SO_CPU);
  3011. }
  3012. SLAB_ATTR_RO(cpu_slabs);
  3013. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3014. {
  3015. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
  3016. }
  3017. SLAB_ATTR_RO(objects);
  3018. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3019. {
  3020. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3021. }
  3022. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3023. const char *buf, size_t length)
  3024. {
  3025. s->flags &= ~SLAB_DEBUG_FREE;
  3026. if (buf[0] == '1')
  3027. s->flags |= SLAB_DEBUG_FREE;
  3028. return length;
  3029. }
  3030. SLAB_ATTR(sanity_checks);
  3031. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3032. {
  3033. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3034. }
  3035. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3036. size_t length)
  3037. {
  3038. s->flags &= ~SLAB_TRACE;
  3039. if (buf[0] == '1')
  3040. s->flags |= SLAB_TRACE;
  3041. return length;
  3042. }
  3043. SLAB_ATTR(trace);
  3044. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3045. {
  3046. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3047. }
  3048. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3049. const char *buf, size_t length)
  3050. {
  3051. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3052. if (buf[0] == '1')
  3053. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3054. return length;
  3055. }
  3056. SLAB_ATTR(reclaim_account);
  3057. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3058. {
  3059. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3060. }
  3061. SLAB_ATTR_RO(hwcache_align);
  3062. #ifdef CONFIG_ZONE_DMA
  3063. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3064. {
  3065. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3066. }
  3067. SLAB_ATTR_RO(cache_dma);
  3068. #endif
  3069. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3070. {
  3071. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3072. }
  3073. SLAB_ATTR_RO(destroy_by_rcu);
  3074. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3075. {
  3076. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3077. }
  3078. static ssize_t red_zone_store(struct kmem_cache *s,
  3079. const char *buf, size_t length)
  3080. {
  3081. if (any_slab_objects(s))
  3082. return -EBUSY;
  3083. s->flags &= ~SLAB_RED_ZONE;
  3084. if (buf[0] == '1')
  3085. s->flags |= SLAB_RED_ZONE;
  3086. calculate_sizes(s);
  3087. return length;
  3088. }
  3089. SLAB_ATTR(red_zone);
  3090. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3091. {
  3092. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3093. }
  3094. static ssize_t poison_store(struct kmem_cache *s,
  3095. const char *buf, size_t length)
  3096. {
  3097. if (any_slab_objects(s))
  3098. return -EBUSY;
  3099. s->flags &= ~SLAB_POISON;
  3100. if (buf[0] == '1')
  3101. s->flags |= SLAB_POISON;
  3102. calculate_sizes(s);
  3103. return length;
  3104. }
  3105. SLAB_ATTR(poison);
  3106. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3107. {
  3108. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3109. }
  3110. static ssize_t store_user_store(struct kmem_cache *s,
  3111. const char *buf, size_t length)
  3112. {
  3113. if (any_slab_objects(s))
  3114. return -EBUSY;
  3115. s->flags &= ~SLAB_STORE_USER;
  3116. if (buf[0] == '1')
  3117. s->flags |= SLAB_STORE_USER;
  3118. calculate_sizes(s);
  3119. return length;
  3120. }
  3121. SLAB_ATTR(store_user);
  3122. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3123. {
  3124. return 0;
  3125. }
  3126. static ssize_t validate_store(struct kmem_cache *s,
  3127. const char *buf, size_t length)
  3128. {
  3129. int ret = -EINVAL;
  3130. if (buf[0] == '1') {
  3131. ret = validate_slab_cache(s);
  3132. if (ret >= 0)
  3133. ret = length;
  3134. }
  3135. return ret;
  3136. }
  3137. SLAB_ATTR(validate);
  3138. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3139. {
  3140. return 0;
  3141. }
  3142. static ssize_t shrink_store(struct kmem_cache *s,
  3143. const char *buf, size_t length)
  3144. {
  3145. if (buf[0] == '1') {
  3146. int rc = kmem_cache_shrink(s);
  3147. if (rc)
  3148. return rc;
  3149. } else
  3150. return -EINVAL;
  3151. return length;
  3152. }
  3153. SLAB_ATTR(shrink);
  3154. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3155. {
  3156. if (!(s->flags & SLAB_STORE_USER))
  3157. return -ENOSYS;
  3158. return list_locations(s, buf, TRACK_ALLOC);
  3159. }
  3160. SLAB_ATTR_RO(alloc_calls);
  3161. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3162. {
  3163. if (!(s->flags & SLAB_STORE_USER))
  3164. return -ENOSYS;
  3165. return list_locations(s, buf, TRACK_FREE);
  3166. }
  3167. SLAB_ATTR_RO(free_calls);
  3168. #ifdef CONFIG_NUMA
  3169. static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
  3170. {
  3171. return sprintf(buf, "%d\n", s->defrag_ratio / 10);
  3172. }
  3173. static ssize_t defrag_ratio_store(struct kmem_cache *s,
  3174. const char *buf, size_t length)
  3175. {
  3176. int n = simple_strtoul(buf, NULL, 10);
  3177. if (n < 100)
  3178. s->defrag_ratio = n * 10;
  3179. return length;
  3180. }
  3181. SLAB_ATTR(defrag_ratio);
  3182. #endif
  3183. static struct attribute * slab_attrs[] = {
  3184. &slab_size_attr.attr,
  3185. &object_size_attr.attr,
  3186. &objs_per_slab_attr.attr,
  3187. &order_attr.attr,
  3188. &objects_attr.attr,
  3189. &slabs_attr.attr,
  3190. &partial_attr.attr,
  3191. &cpu_slabs_attr.attr,
  3192. &ctor_attr.attr,
  3193. &aliases_attr.attr,
  3194. &align_attr.attr,
  3195. &sanity_checks_attr.attr,
  3196. &trace_attr.attr,
  3197. &hwcache_align_attr.attr,
  3198. &reclaim_account_attr.attr,
  3199. &destroy_by_rcu_attr.attr,
  3200. &red_zone_attr.attr,
  3201. &poison_attr.attr,
  3202. &store_user_attr.attr,
  3203. &validate_attr.attr,
  3204. &shrink_attr.attr,
  3205. &alloc_calls_attr.attr,
  3206. &free_calls_attr.attr,
  3207. #ifdef CONFIG_ZONE_DMA
  3208. &cache_dma_attr.attr,
  3209. #endif
  3210. #ifdef CONFIG_NUMA
  3211. &defrag_ratio_attr.attr,
  3212. #endif
  3213. NULL
  3214. };
  3215. static struct attribute_group slab_attr_group = {
  3216. .attrs = slab_attrs,
  3217. };
  3218. static ssize_t slab_attr_show(struct kobject *kobj,
  3219. struct attribute *attr,
  3220. char *buf)
  3221. {
  3222. struct slab_attribute *attribute;
  3223. struct kmem_cache *s;
  3224. int err;
  3225. attribute = to_slab_attr(attr);
  3226. s = to_slab(kobj);
  3227. if (!attribute->show)
  3228. return -EIO;
  3229. err = attribute->show(s, buf);
  3230. return err;
  3231. }
  3232. static ssize_t slab_attr_store(struct kobject *kobj,
  3233. struct attribute *attr,
  3234. const char *buf, size_t len)
  3235. {
  3236. struct slab_attribute *attribute;
  3237. struct kmem_cache *s;
  3238. int err;
  3239. attribute = to_slab_attr(attr);
  3240. s = to_slab(kobj);
  3241. if (!attribute->store)
  3242. return -EIO;
  3243. err = attribute->store(s, buf, len);
  3244. return err;
  3245. }
  3246. static struct sysfs_ops slab_sysfs_ops = {
  3247. .show = slab_attr_show,
  3248. .store = slab_attr_store,
  3249. };
  3250. static struct kobj_type slab_ktype = {
  3251. .sysfs_ops = &slab_sysfs_ops,
  3252. };
  3253. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3254. {
  3255. struct kobj_type *ktype = get_ktype(kobj);
  3256. if (ktype == &slab_ktype)
  3257. return 1;
  3258. return 0;
  3259. }
  3260. static struct kset_uevent_ops slab_uevent_ops = {
  3261. .filter = uevent_filter,
  3262. };
  3263. static decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
  3264. #define ID_STR_LENGTH 64
  3265. /* Create a unique string id for a slab cache:
  3266. * format
  3267. * :[flags-]size:[memory address of kmemcache]
  3268. */
  3269. static char *create_unique_id(struct kmem_cache *s)
  3270. {
  3271. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3272. char *p = name;
  3273. BUG_ON(!name);
  3274. *p++ = ':';
  3275. /*
  3276. * First flags affecting slabcache operations. We will only
  3277. * get here for aliasable slabs so we do not need to support
  3278. * too many flags. The flags here must cover all flags that
  3279. * are matched during merging to guarantee that the id is
  3280. * unique.
  3281. */
  3282. if (s->flags & SLAB_CACHE_DMA)
  3283. *p++ = 'd';
  3284. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3285. *p++ = 'a';
  3286. if (s->flags & SLAB_DEBUG_FREE)
  3287. *p++ = 'F';
  3288. if (p != name + 1)
  3289. *p++ = '-';
  3290. p += sprintf(p, "%07d", s->size);
  3291. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3292. return name;
  3293. }
  3294. static int sysfs_slab_add(struct kmem_cache *s)
  3295. {
  3296. int err;
  3297. const char *name;
  3298. int unmergeable;
  3299. if (slab_state < SYSFS)
  3300. /* Defer until later */
  3301. return 0;
  3302. unmergeable = slab_unmergeable(s);
  3303. if (unmergeable) {
  3304. /*
  3305. * Slabcache can never be merged so we can use the name proper.
  3306. * This is typically the case for debug situations. In that
  3307. * case we can catch duplicate names easily.
  3308. */
  3309. sysfs_remove_link(&slab_subsys.kobj, s->name);
  3310. name = s->name;
  3311. } else {
  3312. /*
  3313. * Create a unique name for the slab as a target
  3314. * for the symlinks.
  3315. */
  3316. name = create_unique_id(s);
  3317. }
  3318. kobj_set_kset_s(s, slab_subsys);
  3319. kobject_set_name(&s->kobj, name);
  3320. kobject_init(&s->kobj);
  3321. err = kobject_add(&s->kobj);
  3322. if (err)
  3323. return err;
  3324. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3325. if (err)
  3326. return err;
  3327. kobject_uevent(&s->kobj, KOBJ_ADD);
  3328. if (!unmergeable) {
  3329. /* Setup first alias */
  3330. sysfs_slab_alias(s, s->name);
  3331. kfree(name);
  3332. }
  3333. return 0;
  3334. }
  3335. static void sysfs_slab_remove(struct kmem_cache *s)
  3336. {
  3337. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3338. kobject_del(&s->kobj);
  3339. }
  3340. /*
  3341. * Need to buffer aliases during bootup until sysfs becomes
  3342. * available lest we loose that information.
  3343. */
  3344. struct saved_alias {
  3345. struct kmem_cache *s;
  3346. const char *name;
  3347. struct saved_alias *next;
  3348. };
  3349. static struct saved_alias *alias_list;
  3350. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  3351. {
  3352. struct saved_alias *al;
  3353. if (slab_state == SYSFS) {
  3354. /*
  3355. * If we have a leftover link then remove it.
  3356. */
  3357. sysfs_remove_link(&slab_subsys.kobj, name);
  3358. return sysfs_create_link(&slab_subsys.kobj,
  3359. &s->kobj, name);
  3360. }
  3361. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  3362. if (!al)
  3363. return -ENOMEM;
  3364. al->s = s;
  3365. al->name = name;
  3366. al->next = alias_list;
  3367. alias_list = al;
  3368. return 0;
  3369. }
  3370. static int __init slab_sysfs_init(void)
  3371. {
  3372. struct kmem_cache *s;
  3373. int err;
  3374. err = subsystem_register(&slab_subsys);
  3375. if (err) {
  3376. printk(KERN_ERR "Cannot register slab subsystem.\n");
  3377. return -ENOSYS;
  3378. }
  3379. slab_state = SYSFS;
  3380. list_for_each_entry(s, &slab_caches, list) {
  3381. err = sysfs_slab_add(s);
  3382. if (err)
  3383. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  3384. " to sysfs\n", s->name);
  3385. }
  3386. while (alias_list) {
  3387. struct saved_alias *al = alias_list;
  3388. alias_list = alias_list->next;
  3389. err = sysfs_slab_alias(al->s, al->name);
  3390. if (err)
  3391. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  3392. " %s to sysfs\n", s->name);
  3393. kfree(al);
  3394. }
  3395. resiliency_test();
  3396. return 0;
  3397. }
  3398. __initcall(slab_sysfs_init);
  3399. #endif