slub.c 79 KB

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