slub.c 80 KB

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