slub.c 75 KB

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