slub.c 87 KB

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