slub.c 90 KB

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