slub.c 81 KB

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