slub.c 90 KB

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