slub.c 72 KB

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