slub.c 72 KB

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