slub.c 86 KB

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