slub.c 81 KB

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