slub.c 86 KB

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