slub.c 86 KB

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