slub.c 73 KB

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