slub.c 81 KB

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