slub.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580
  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
  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/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/cpu.h>
  19. #include <linux/cpuset.h>
  20. #include <linux/mempolicy.h>
  21. #include <linux/ctype.h>
  22. #include <linux/debugobjects.h>
  23. #include <linux/kallsyms.h>
  24. #include <linux/memory.h>
  25. #include <linux/math64.h>
  26. #include <linux/kmemtrace.h>
  27. /*
  28. * Lock order:
  29. * 1. slab_lock(page)
  30. * 2. slab->list_lock
  31. *
  32. * The slab_lock protects operations on the object of a particular
  33. * slab and its metadata in the page struct. If the slab lock
  34. * has been taken then no allocations nor frees can be performed
  35. * on the objects in the slab nor can the slab be added or removed
  36. * from the partial or full lists since this would mean modifying
  37. * the page_struct of the slab.
  38. *
  39. * The list_lock protects the partial and full list on each node and
  40. * the partial slab counter. If taken then no new slabs may be added or
  41. * removed from the lists nor make the number of partial slabs be modified.
  42. * (Note that the total number of slabs is an atomic value that may be
  43. * modified without taking the list lock).
  44. *
  45. * The list_lock is a centralized lock and thus we avoid taking it as
  46. * much as possible. As long as SLUB does not have to handle partial
  47. * slabs, operations can continue without any centralized lock. F.e.
  48. * allocating a long series of objects that fill up slabs does not require
  49. * the list lock.
  50. *
  51. * The lock order is sometimes inverted when we are trying to get a slab
  52. * off a list. We take the list_lock and then look for a page on the list
  53. * to use. While we do that objects in the slabs may be freed. We can
  54. * only operate on the slab if we have also taken the slab_lock. So we use
  55. * a slab_trylock() on the slab. If trylock was successful then no frees
  56. * can occur anymore and we can use the slab for allocations etc. If the
  57. * slab_trylock() does not succeed then frees are in progress in the slab and
  58. * we must stay away from it for a while since we may cause a bouncing
  59. * cacheline if we try to acquire the lock. So go onto the next slab.
  60. * If all pages are busy then we may allocate a new slab instead of reusing
  61. * a partial slab. A new slab has noone operating on it and thus there is
  62. * no danger of cacheline contention.
  63. *
  64. * Interrupts are disabled during allocation and deallocation in order to
  65. * make the slab allocator safe to use in the context of an irq. In addition
  66. * interrupts are disabled to ensure that the processor does not change
  67. * while handling per_cpu slabs, due to kernel preemption.
  68. *
  69. * SLUB assigns one slab for allocation to each processor.
  70. * Allocations only occur from these slabs called cpu slabs.
  71. *
  72. * Slabs with free elements are kept on a partial list and during regular
  73. * operations no list for full slabs is used. If an object in a full slab is
  74. * freed then the slab will show up again on the partial lists.
  75. * We track full slabs for debugging purposes though because otherwise we
  76. * cannot scan all objects.
  77. *
  78. * Slabs are freed when they become empty. Teardown and setup is
  79. * minimal so we rely on the page allocators per cpu caches for
  80. * fast frees and allocs.
  81. *
  82. * Overloading of page flags that are otherwise used for LRU management.
  83. *
  84. * PageActive The slab is frozen and exempt from list processing.
  85. * This means that the slab is dedicated to a purpose
  86. * such as satisfying allocations for a specific
  87. * processor. Objects may be freed in the slab while
  88. * it is frozen but slab_free will then skip the usual
  89. * list operations. It is up to the processor holding
  90. * the slab to integrate the slab into the slab lists
  91. * when the slab is no longer needed.
  92. *
  93. * One use of this flag is to mark slabs that are
  94. * used for allocations. Then such a slab becomes a cpu
  95. * slab. The cpu slab may be equipped with an additional
  96. * freelist that allows lockless access to
  97. * free objects in addition to the regular freelist
  98. * that requires the slab lock.
  99. *
  100. * PageError Slab requires special handling due to debug
  101. * options set. This moves slab handling out of
  102. * the fast path and disables lockless freelists.
  103. */
  104. #ifdef CONFIG_SLUB_DEBUG
  105. #define SLABDEBUG 1
  106. #else
  107. #define SLABDEBUG 0
  108. #endif
  109. /*
  110. * Issues still to be resolved:
  111. *
  112. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  113. *
  114. * - Variable sizing of the per node arrays
  115. */
  116. /* Enable to test recovery from slab corruption on boot */
  117. #undef SLUB_RESILIENCY_TEST
  118. /*
  119. * Mininum number of partial slabs. These will be left on the partial
  120. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  121. */
  122. #define MIN_PARTIAL 5
  123. /*
  124. * Maximum number of desirable partial slabs.
  125. * The existence of more partial slabs makes kmem_cache_shrink
  126. * sort the partial list by the number of objects in the.
  127. */
  128. #define MAX_PARTIAL 10
  129. #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
  130. SLAB_POISON | SLAB_STORE_USER)
  131. /*
  132. * Set of flags that will prevent slab merging
  133. */
  134. #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  135. SLAB_TRACE | SLAB_DESTROY_BY_RCU)
  136. #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  137. SLAB_CACHE_DMA)
  138. #ifndef ARCH_KMALLOC_MINALIGN
  139. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  140. #endif
  141. #ifndef ARCH_SLAB_MINALIGN
  142. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  143. #endif
  144. /* Internal SLUB flags */
  145. #define __OBJECT_POISON 0x80000000 /* Poison object */
  146. #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
  147. static int kmem_size = sizeof(struct kmem_cache);
  148. #ifdef CONFIG_SMP
  149. static struct notifier_block slab_notifier;
  150. #endif
  151. static enum {
  152. DOWN, /* No slab functionality available */
  153. PARTIAL, /* kmem_cache_open() works but kmalloc does not */
  154. UP, /* Everything works but does not show up in sysfs */
  155. SYSFS /* Sysfs up */
  156. } slab_state = DOWN;
  157. /* A list of all slab caches on the system */
  158. static DECLARE_RWSEM(slub_lock);
  159. static LIST_HEAD(slab_caches);
  160. /*
  161. * Tracking user of a slab.
  162. */
  163. struct track {
  164. unsigned long addr; /* Called from address */
  165. int cpu; /* Was running on cpu */
  166. int pid; /* Pid context */
  167. unsigned long when; /* When did the operation occur */
  168. };
  169. enum track_item { TRACK_ALLOC, TRACK_FREE };
  170. #ifdef CONFIG_SLUB_DEBUG
  171. static int sysfs_slab_add(struct kmem_cache *);
  172. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  173. static void sysfs_slab_remove(struct kmem_cache *);
  174. #else
  175. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  176. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  177. { return 0; }
  178. static inline void sysfs_slab_remove(struct kmem_cache *s)
  179. {
  180. kfree(s);
  181. }
  182. #endif
  183. static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
  184. {
  185. #ifdef CONFIG_SLUB_STATS
  186. c->stat[si]++;
  187. #endif
  188. }
  189. /********************************************************************
  190. * Core slab cache functions
  191. *******************************************************************/
  192. int slab_is_available(void)
  193. {
  194. return slab_state >= UP;
  195. }
  196. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  197. {
  198. #ifdef CONFIG_NUMA
  199. return s->node[node];
  200. #else
  201. return &s->local_node;
  202. #endif
  203. }
  204. static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
  205. {
  206. #ifdef CONFIG_SMP
  207. return s->cpu_slab[cpu];
  208. #else
  209. return &s->cpu_slab;
  210. #endif
  211. }
  212. /* Verify that a pointer has an address that is valid within a slab page */
  213. static inline int check_valid_pointer(struct kmem_cache *s,
  214. struct page *page, const void *object)
  215. {
  216. void *base;
  217. if (!object)
  218. return 1;
  219. base = page_address(page);
  220. if (object < base || object >= base + page->objects * s->size ||
  221. (object - base) % s->size) {
  222. return 0;
  223. }
  224. return 1;
  225. }
  226. /*
  227. * Slow version of get and set free pointer.
  228. *
  229. * This version requires touching the cache lines of kmem_cache which
  230. * we avoid to do in the fast alloc free paths. There we obtain the offset
  231. * from the page struct.
  232. */
  233. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  234. {
  235. return *(void **)(object + s->offset);
  236. }
  237. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  238. {
  239. *(void **)(object + s->offset) = fp;
  240. }
  241. /* Loop over all objects in a slab */
  242. #define for_each_object(__p, __s, __addr, __objects) \
  243. for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
  244. __p += (__s)->size)
  245. /* Scan freelist */
  246. #define for_each_free_object(__p, __s, __free) \
  247. for (__p = (__free); __p; __p = get_freepointer((__s), __p))
  248. /* Determine object index from a given position */
  249. static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
  250. {
  251. return (p - addr) / s->size;
  252. }
  253. static inline struct kmem_cache_order_objects oo_make(int order,
  254. unsigned long size)
  255. {
  256. struct kmem_cache_order_objects x = {
  257. (order << 16) + (PAGE_SIZE << order) / size
  258. };
  259. return x;
  260. }
  261. static inline int oo_order(struct kmem_cache_order_objects x)
  262. {
  263. return x.x >> 16;
  264. }
  265. static inline int oo_objects(struct kmem_cache_order_objects x)
  266. {
  267. return x.x & ((1 << 16) - 1);
  268. }
  269. #ifdef CONFIG_SLUB_DEBUG
  270. /*
  271. * Debug settings:
  272. */
  273. #ifdef CONFIG_SLUB_DEBUG_ON
  274. static int slub_debug = DEBUG_DEFAULT_FLAGS;
  275. #else
  276. static int slub_debug;
  277. #endif
  278. static char *slub_debug_slabs;
  279. /*
  280. * Object debugging
  281. */
  282. static void print_section(char *text, u8 *addr, unsigned int length)
  283. {
  284. int i, offset;
  285. int newline = 1;
  286. char ascii[17];
  287. ascii[16] = 0;
  288. for (i = 0; i < length; i++) {
  289. if (newline) {
  290. printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
  291. newline = 0;
  292. }
  293. printk(KERN_CONT " %02x", addr[i]);
  294. offset = i % 16;
  295. ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
  296. if (offset == 15) {
  297. printk(KERN_CONT " %s\n", ascii);
  298. newline = 1;
  299. }
  300. }
  301. if (!newline) {
  302. i %= 16;
  303. while (i < 16) {
  304. printk(KERN_CONT " ");
  305. ascii[i] = ' ';
  306. i++;
  307. }
  308. printk(KERN_CONT " %s\n", ascii);
  309. }
  310. }
  311. static struct track *get_track(struct kmem_cache *s, void *object,
  312. enum track_item alloc)
  313. {
  314. struct track *p;
  315. if (s->offset)
  316. p = object + s->offset + sizeof(void *);
  317. else
  318. p = object + s->inuse;
  319. return p + alloc;
  320. }
  321. static void set_track(struct kmem_cache *s, void *object,
  322. enum track_item alloc, unsigned long addr)
  323. {
  324. struct track *p;
  325. if (s->offset)
  326. p = object + s->offset + sizeof(void *);
  327. else
  328. p = object + s->inuse;
  329. p += alloc;
  330. if (addr) {
  331. p->addr = addr;
  332. p->cpu = smp_processor_id();
  333. p->pid = current->pid;
  334. p->when = jiffies;
  335. } else
  336. memset(p, 0, sizeof(struct track));
  337. }
  338. static void init_tracking(struct kmem_cache *s, void *object)
  339. {
  340. if (!(s->flags & SLAB_STORE_USER))
  341. return;
  342. set_track(s, object, TRACK_FREE, 0UL);
  343. set_track(s, object, TRACK_ALLOC, 0UL);
  344. }
  345. static void print_track(const char *s, struct track *t)
  346. {
  347. if (!t->addr)
  348. return;
  349. printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
  350. s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
  351. }
  352. static void print_tracking(struct kmem_cache *s, void *object)
  353. {
  354. if (!(s->flags & SLAB_STORE_USER))
  355. return;
  356. print_track("Allocated", get_track(s, object, TRACK_ALLOC));
  357. print_track("Freed", get_track(s, object, TRACK_FREE));
  358. }
  359. static void print_page_info(struct page *page)
  360. {
  361. printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
  362. page, page->objects, page->inuse, page->freelist, page->flags);
  363. }
  364. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  365. {
  366. va_list args;
  367. char buf[100];
  368. va_start(args, fmt);
  369. vsnprintf(buf, sizeof(buf), fmt, args);
  370. va_end(args);
  371. printk(KERN_ERR "========================================"
  372. "=====================================\n");
  373. printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
  374. printk(KERN_ERR "----------------------------------------"
  375. "-------------------------------------\n\n");
  376. }
  377. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  378. {
  379. va_list args;
  380. char buf[100];
  381. va_start(args, fmt);
  382. vsnprintf(buf, sizeof(buf), fmt, args);
  383. va_end(args);
  384. printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
  385. }
  386. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  387. {
  388. unsigned int off; /* Offset of last byte */
  389. u8 *addr = page_address(page);
  390. print_tracking(s, p);
  391. print_page_info(page);
  392. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  393. p, p - addr, get_freepointer(s, p));
  394. if (p > addr + 16)
  395. print_section("Bytes b4", p - 16, 16);
  396. print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
  397. if (s->flags & SLAB_RED_ZONE)
  398. print_section("Redzone", p + s->objsize,
  399. s->inuse - s->objsize);
  400. if (s->offset)
  401. off = s->offset + sizeof(void *);
  402. else
  403. off = s->inuse;
  404. if (s->flags & SLAB_STORE_USER)
  405. off += 2 * sizeof(struct track);
  406. if (off != s->size)
  407. /* Beginning of the filler is the free pointer */
  408. print_section("Padding", p + off, s->size - off);
  409. dump_stack();
  410. }
  411. static void object_err(struct kmem_cache *s, struct page *page,
  412. u8 *object, char *reason)
  413. {
  414. slab_bug(s, "%s", reason);
  415. print_trailer(s, page, object);
  416. }
  417. static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
  418. {
  419. va_list args;
  420. char buf[100];
  421. va_start(args, fmt);
  422. vsnprintf(buf, sizeof(buf), fmt, args);
  423. va_end(args);
  424. slab_bug(s, "%s", buf);
  425. print_page_info(page);
  426. dump_stack();
  427. }
  428. static void init_object(struct kmem_cache *s, void *object, int active)
  429. {
  430. u8 *p = object;
  431. if (s->flags & __OBJECT_POISON) {
  432. memset(p, POISON_FREE, s->objsize - 1);
  433. p[s->objsize - 1] = POISON_END;
  434. }
  435. if (s->flags & SLAB_RED_ZONE)
  436. memset(p + s->objsize,
  437. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
  438. s->inuse - s->objsize);
  439. }
  440. static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  441. {
  442. while (bytes) {
  443. if (*start != (u8)value)
  444. return start;
  445. start++;
  446. bytes--;
  447. }
  448. return NULL;
  449. }
  450. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  451. void *from, void *to)
  452. {
  453. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  454. memset(from, data, to - from);
  455. }
  456. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  457. u8 *object, char *what,
  458. u8 *start, unsigned int value, unsigned int bytes)
  459. {
  460. u8 *fault;
  461. u8 *end;
  462. fault = check_bytes(start, value, bytes);
  463. if (!fault)
  464. return 1;
  465. end = start + bytes;
  466. while (end > fault && end[-1] == value)
  467. end--;
  468. slab_bug(s, "%s overwritten", what);
  469. printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  470. fault, end - 1, fault[0], value);
  471. print_trailer(s, page, object);
  472. restore_bytes(s, what, value, fault, end);
  473. return 0;
  474. }
  475. /*
  476. * Object layout:
  477. *
  478. * object address
  479. * Bytes of the object to be managed.
  480. * If the freepointer may overlay the object then the free
  481. * pointer is the first word of the object.
  482. *
  483. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  484. * 0xa5 (POISON_END)
  485. *
  486. * object + s->objsize
  487. * Padding to reach word boundary. This is also used for Redzoning.
  488. * Padding is extended by another word if Redzoning is enabled and
  489. * objsize == inuse.
  490. *
  491. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  492. * 0xcc (RED_ACTIVE) for objects in use.
  493. *
  494. * object + s->inuse
  495. * Meta data starts here.
  496. *
  497. * A. Free pointer (if we cannot overwrite object on free)
  498. * B. Tracking data for SLAB_STORE_USER
  499. * C. Padding to reach required alignment boundary or at mininum
  500. * one word if debugging is on to be able to detect writes
  501. * before the word boundary.
  502. *
  503. * Padding is done using 0x5a (POISON_INUSE)
  504. *
  505. * object + s->size
  506. * Nothing is used beyond s->size.
  507. *
  508. * If slabcaches are merged then the objsize and inuse boundaries are mostly
  509. * ignored. And therefore no slab options that rely on these boundaries
  510. * may be used with merged slabcaches.
  511. */
  512. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  513. {
  514. unsigned long off = s->inuse; /* The end of info */
  515. if (s->offset)
  516. /* Freepointer is placed after the object. */
  517. off += sizeof(void *);
  518. if (s->flags & SLAB_STORE_USER)
  519. /* We also have user information there */
  520. off += 2 * sizeof(struct track);
  521. if (s->size == off)
  522. return 1;
  523. return check_bytes_and_report(s, page, p, "Object padding",
  524. p + off, POISON_INUSE, s->size - off);
  525. }
  526. /* Check the pad bytes at the end of a slab page */
  527. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  528. {
  529. u8 *start;
  530. u8 *fault;
  531. u8 *end;
  532. int length;
  533. int remainder;
  534. if (!(s->flags & SLAB_POISON))
  535. return 1;
  536. start = page_address(page);
  537. length = (PAGE_SIZE << compound_order(page));
  538. end = start + length;
  539. remainder = length % s->size;
  540. if (!remainder)
  541. return 1;
  542. fault = check_bytes(end - remainder, POISON_INUSE, remainder);
  543. if (!fault)
  544. return 1;
  545. while (end > fault && end[-1] == POISON_INUSE)
  546. end--;
  547. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  548. print_section("Padding", end - remainder, remainder);
  549. restore_bytes(s, "slab padding", POISON_INUSE, start, end);
  550. return 0;
  551. }
  552. static int check_object(struct kmem_cache *s, struct page *page,
  553. void *object, int active)
  554. {
  555. u8 *p = object;
  556. u8 *endobject = object + s->objsize;
  557. if (s->flags & SLAB_RED_ZONE) {
  558. unsigned int red =
  559. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
  560. if (!check_bytes_and_report(s, page, object, "Redzone",
  561. endobject, red, s->inuse - s->objsize))
  562. return 0;
  563. } else {
  564. if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
  565. check_bytes_and_report(s, page, p, "Alignment padding",
  566. endobject, POISON_INUSE, s->inuse - s->objsize);
  567. }
  568. }
  569. if (s->flags & SLAB_POISON) {
  570. if (!active && (s->flags & __OBJECT_POISON) &&
  571. (!check_bytes_and_report(s, page, p, "Poison", p,
  572. POISON_FREE, s->objsize - 1) ||
  573. !check_bytes_and_report(s, page, p, "Poison",
  574. p + s->objsize - 1, POISON_END, 1)))
  575. return 0;
  576. /*
  577. * check_pad_bytes cleans up on its own.
  578. */
  579. check_pad_bytes(s, page, p);
  580. }
  581. if (!s->offset && active)
  582. /*
  583. * Object and freepointer overlap. Cannot check
  584. * freepointer while object is allocated.
  585. */
  586. return 1;
  587. /* Check free pointer validity */
  588. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  589. object_err(s, page, p, "Freepointer corrupt");
  590. /*
  591. * No choice but to zap it and thus loose the remainder
  592. * of the free objects in this slab. May cause
  593. * another error because the object count is now wrong.
  594. */
  595. set_freepointer(s, p, NULL);
  596. return 0;
  597. }
  598. return 1;
  599. }
  600. static int check_slab(struct kmem_cache *s, struct page *page)
  601. {
  602. int maxobj;
  603. VM_BUG_ON(!irqs_disabled());
  604. if (!PageSlab(page)) {
  605. slab_err(s, page, "Not a valid slab page");
  606. return 0;
  607. }
  608. maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
  609. if (page->objects > maxobj) {
  610. slab_err(s, page, "objects %u > max %u",
  611. s->name, page->objects, maxobj);
  612. return 0;
  613. }
  614. if (page->inuse > page->objects) {
  615. slab_err(s, page, "inuse %u > max %u",
  616. s->name, page->inuse, page->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. unsigned long max_objects;
  633. while (fp && nr <= page->objects) {
  634. if (fp == search)
  635. return 1;
  636. if (!check_valid_pointer(s, page, fp)) {
  637. if (object) {
  638. object_err(s, page, object,
  639. "Freechain corrupt");
  640. set_freepointer(s, object, NULL);
  641. break;
  642. } else {
  643. slab_err(s, page, "Freepointer corrupt");
  644. page->freelist = NULL;
  645. page->inuse = page->objects;
  646. slab_fix(s, "Freelist cleared");
  647. return 0;
  648. }
  649. break;
  650. }
  651. object = fp;
  652. fp = get_freepointer(s, object);
  653. nr++;
  654. }
  655. max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
  656. if (max_objects > 65535)
  657. max_objects = 65535;
  658. if (page->objects != max_objects) {
  659. slab_err(s, page, "Wrong number of objects. Found %d but "
  660. "should be %d", page->objects, max_objects);
  661. page->objects = max_objects;
  662. slab_fix(s, "Number of objects adjusted.");
  663. }
  664. if (page->inuse != page->objects - nr) {
  665. slab_err(s, page, "Wrong object count. Counter is %d but "
  666. "counted were %d", page->inuse, page->objects - nr);
  667. page->inuse = page->objects - nr;
  668. slab_fix(s, "Object count adjusted.");
  669. }
  670. return search == NULL;
  671. }
  672. static void trace(struct kmem_cache *s, struct page *page, void *object,
  673. int alloc)
  674. {
  675. if (s->flags & SLAB_TRACE) {
  676. printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  677. s->name,
  678. alloc ? "alloc" : "free",
  679. object, page->inuse,
  680. page->freelist);
  681. if (!alloc)
  682. print_section("Object", (void *)object, s->objsize);
  683. dump_stack();
  684. }
  685. }
  686. /*
  687. * Tracking of fully allocated slabs for debugging purposes.
  688. */
  689. static void add_full(struct kmem_cache_node *n, struct page *page)
  690. {
  691. spin_lock(&n->list_lock);
  692. list_add(&page->lru, &n->full);
  693. spin_unlock(&n->list_lock);
  694. }
  695. static void remove_full(struct kmem_cache *s, struct page *page)
  696. {
  697. struct kmem_cache_node *n;
  698. if (!(s->flags & SLAB_STORE_USER))
  699. return;
  700. n = get_node(s, page_to_nid(page));
  701. spin_lock(&n->list_lock);
  702. list_del(&page->lru);
  703. spin_unlock(&n->list_lock);
  704. }
  705. /* Tracking of the number of slabs for debugging purposes */
  706. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  707. {
  708. struct kmem_cache_node *n = get_node(s, node);
  709. return atomic_long_read(&n->nr_slabs);
  710. }
  711. static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
  712. {
  713. struct kmem_cache_node *n = get_node(s, node);
  714. /*
  715. * May be called early in order to allocate a slab for the
  716. * kmem_cache_node structure. Solve the chicken-egg
  717. * dilemma by deferring the increment of the count during
  718. * bootstrap (see early_kmem_cache_node_alloc).
  719. */
  720. if (!NUMA_BUILD || n) {
  721. atomic_long_inc(&n->nr_slabs);
  722. atomic_long_add(objects, &n->total_objects);
  723. }
  724. }
  725. static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
  726. {
  727. struct kmem_cache_node *n = get_node(s, node);
  728. atomic_long_dec(&n->nr_slabs);
  729. atomic_long_sub(objects, &n->total_objects);
  730. }
  731. /* Object debug checks for alloc/free paths */
  732. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  733. void *object)
  734. {
  735. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  736. return;
  737. init_object(s, object, 0);
  738. init_tracking(s, object);
  739. }
  740. static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
  741. void *object, unsigned long addr)
  742. {
  743. if (!check_slab(s, page))
  744. goto bad;
  745. if (!on_freelist(s, page, object)) {
  746. object_err(s, page, object, "Object already allocated");
  747. goto bad;
  748. }
  749. if (!check_valid_pointer(s, page, object)) {
  750. object_err(s, page, object, "Freelist Pointer check fails");
  751. goto bad;
  752. }
  753. if (!check_object(s, page, object, 0))
  754. goto bad;
  755. /* Success perform special debug activities for allocs */
  756. if (s->flags & SLAB_STORE_USER)
  757. set_track(s, object, TRACK_ALLOC, addr);
  758. trace(s, page, object, 1);
  759. init_object(s, object, 1);
  760. return 1;
  761. bad:
  762. if (PageSlab(page)) {
  763. /*
  764. * If this is a slab page then lets do the best we can
  765. * to avoid issues in the future. Marking all objects
  766. * as used avoids touching the remaining objects.
  767. */
  768. slab_fix(s, "Marking all objects used");
  769. page->inuse = page->objects;
  770. page->freelist = NULL;
  771. }
  772. return 0;
  773. }
  774. static int free_debug_processing(struct kmem_cache *s, struct page *page,
  775. void *object, unsigned long addr)
  776. {
  777. if (!check_slab(s, page))
  778. goto fail;
  779. if (!check_valid_pointer(s, page, object)) {
  780. slab_err(s, page, "Invalid object pointer 0x%p", object);
  781. goto fail;
  782. }
  783. if (on_freelist(s, page, object)) {
  784. object_err(s, page, object, "Object already free");
  785. goto fail;
  786. }
  787. if (!check_object(s, page, object, 1))
  788. return 0;
  789. if (unlikely(s != page->slab)) {
  790. if (!PageSlab(page)) {
  791. slab_err(s, page, "Attempt to free object(0x%p) "
  792. "outside of slab", object);
  793. } else if (!page->slab) {
  794. printk(KERN_ERR
  795. "SLUB <none>: no slab for object 0x%p.\n",
  796. object);
  797. dump_stack();
  798. } else
  799. object_err(s, page, object,
  800. "page slab pointer corrupt.");
  801. goto fail;
  802. }
  803. /* Special debug activities for freeing objects */
  804. if (!PageSlubFrozen(page) && !page->freelist)
  805. remove_full(s, page);
  806. if (s->flags & SLAB_STORE_USER)
  807. set_track(s, object, TRACK_FREE, addr);
  808. trace(s, page, object, 0);
  809. init_object(s, object, 0);
  810. return 1;
  811. fail:
  812. slab_fix(s, "Object at 0x%p not freed", object);
  813. return 0;
  814. }
  815. static int __init setup_slub_debug(char *str)
  816. {
  817. slub_debug = DEBUG_DEFAULT_FLAGS;
  818. if (*str++ != '=' || !*str)
  819. /*
  820. * No options specified. Switch on full debugging.
  821. */
  822. goto out;
  823. if (*str == ',')
  824. /*
  825. * No options but restriction on slabs. This means full
  826. * debugging for slabs matching a pattern.
  827. */
  828. goto check_slabs;
  829. slub_debug = 0;
  830. if (*str == '-')
  831. /*
  832. * Switch off all debugging measures.
  833. */
  834. goto out;
  835. /*
  836. * Determine which debug features should be switched on
  837. */
  838. for (; *str && *str != ','; str++) {
  839. switch (tolower(*str)) {
  840. case 'f':
  841. slub_debug |= SLAB_DEBUG_FREE;
  842. break;
  843. case 'z':
  844. slub_debug |= SLAB_RED_ZONE;
  845. break;
  846. case 'p':
  847. slub_debug |= SLAB_POISON;
  848. break;
  849. case 'u':
  850. slub_debug |= SLAB_STORE_USER;
  851. break;
  852. case 't':
  853. slub_debug |= SLAB_TRACE;
  854. break;
  855. default:
  856. printk(KERN_ERR "slub_debug option '%c' "
  857. "unknown. skipped\n", *str);
  858. }
  859. }
  860. check_slabs:
  861. if (*str == ',')
  862. slub_debug_slabs = str + 1;
  863. out:
  864. return 1;
  865. }
  866. __setup("slub_debug", setup_slub_debug);
  867. static unsigned long kmem_cache_flags(unsigned long objsize,
  868. unsigned long flags, const char *name,
  869. void (*ctor)(void *))
  870. {
  871. /*
  872. * Enable debugging if selected on the kernel commandline.
  873. */
  874. if (slub_debug && (!slub_debug_slabs ||
  875. strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
  876. flags |= slub_debug;
  877. return flags;
  878. }
  879. #else
  880. static inline void setup_object_debug(struct kmem_cache *s,
  881. struct page *page, void *object) {}
  882. static inline int alloc_debug_processing(struct kmem_cache *s,
  883. struct page *page, void *object, unsigned long addr) { return 0; }
  884. static inline int free_debug_processing(struct kmem_cache *s,
  885. struct page *page, void *object, unsigned long addr) { return 0; }
  886. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  887. { return 1; }
  888. static inline int check_object(struct kmem_cache *s, struct page *page,
  889. void *object, int active) { return 1; }
  890. static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
  891. static inline unsigned long kmem_cache_flags(unsigned long objsize,
  892. unsigned long flags, const char *name,
  893. void (*ctor)(void *))
  894. {
  895. return flags;
  896. }
  897. #define slub_debug 0
  898. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  899. { return 0; }
  900. static inline void inc_slabs_node(struct kmem_cache *s, int node,
  901. int objects) {}
  902. static inline void dec_slabs_node(struct kmem_cache *s, int node,
  903. int objects) {}
  904. #endif
  905. /*
  906. * Slab allocation and freeing
  907. */
  908. static inline struct page *alloc_slab_page(gfp_t flags, int node,
  909. struct kmem_cache_order_objects oo)
  910. {
  911. int order = oo_order(oo);
  912. if (node == -1)
  913. return alloc_pages(flags, order);
  914. else
  915. return alloc_pages_node(node, flags, order);
  916. }
  917. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  918. {
  919. struct page *page;
  920. struct kmem_cache_order_objects oo = s->oo;
  921. flags |= s->allocflags;
  922. page = alloc_slab_page(flags | __GFP_NOWARN | __GFP_NORETRY, node,
  923. oo);
  924. if (unlikely(!page)) {
  925. oo = s->min;
  926. /*
  927. * Allocation may have failed due to fragmentation.
  928. * Try a lower order alloc if possible
  929. */
  930. page = alloc_slab_page(flags, node, oo);
  931. if (!page)
  932. return NULL;
  933. stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
  934. }
  935. page->objects = oo_objects(oo);
  936. mod_zone_page_state(page_zone(page),
  937. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  938. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  939. 1 << oo_order(oo));
  940. return page;
  941. }
  942. static void setup_object(struct kmem_cache *s, struct page *page,
  943. void *object)
  944. {
  945. setup_object_debug(s, page, object);
  946. if (unlikely(s->ctor))
  947. s->ctor(object);
  948. }
  949. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  950. {
  951. struct page *page;
  952. void *start;
  953. void *last;
  954. void *p;
  955. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  956. page = allocate_slab(s,
  957. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  958. if (!page)
  959. goto out;
  960. inc_slabs_node(s, page_to_nid(page), page->objects);
  961. page->slab = s;
  962. page->flags |= 1 << PG_slab;
  963. if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
  964. SLAB_STORE_USER | SLAB_TRACE))
  965. __SetPageSlubDebug(page);
  966. start = page_address(page);
  967. if (unlikely(s->flags & SLAB_POISON))
  968. memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
  969. last = start;
  970. for_each_object(p, s, start, page->objects) {
  971. setup_object(s, page, last);
  972. set_freepointer(s, last, p);
  973. last = p;
  974. }
  975. setup_object(s, page, last);
  976. set_freepointer(s, last, NULL);
  977. page->freelist = start;
  978. page->inuse = 0;
  979. out:
  980. return page;
  981. }
  982. static void __free_slab(struct kmem_cache *s, struct page *page)
  983. {
  984. int order = compound_order(page);
  985. int pages = 1 << order;
  986. if (unlikely(SLABDEBUG && PageSlubDebug(page))) {
  987. void *p;
  988. slab_pad_check(s, page);
  989. for_each_object(p, s, page_address(page),
  990. page->objects)
  991. check_object(s, page, p, 0);
  992. __ClearPageSlubDebug(page);
  993. }
  994. mod_zone_page_state(page_zone(page),
  995. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  996. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  997. -pages);
  998. __ClearPageSlab(page);
  999. reset_page_mapcount(page);
  1000. __free_pages(page, order);
  1001. }
  1002. static void rcu_free_slab(struct rcu_head *h)
  1003. {
  1004. struct page *page;
  1005. page = container_of((struct list_head *)h, struct page, lru);
  1006. __free_slab(page->slab, page);
  1007. }
  1008. static void free_slab(struct kmem_cache *s, struct page *page)
  1009. {
  1010. if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
  1011. /*
  1012. * RCU free overloads the RCU head over the LRU
  1013. */
  1014. struct rcu_head *head = (void *)&page->lru;
  1015. call_rcu(head, rcu_free_slab);
  1016. } else
  1017. __free_slab(s, page);
  1018. }
  1019. static void discard_slab(struct kmem_cache *s, struct page *page)
  1020. {
  1021. dec_slabs_node(s, page_to_nid(page), page->objects);
  1022. free_slab(s, page);
  1023. }
  1024. /*
  1025. * Per slab locking using the pagelock
  1026. */
  1027. static __always_inline void slab_lock(struct page *page)
  1028. {
  1029. bit_spin_lock(PG_locked, &page->flags);
  1030. }
  1031. static __always_inline void slab_unlock(struct page *page)
  1032. {
  1033. __bit_spin_unlock(PG_locked, &page->flags);
  1034. }
  1035. static __always_inline int slab_trylock(struct page *page)
  1036. {
  1037. int rc = 1;
  1038. rc = bit_spin_trylock(PG_locked, &page->flags);
  1039. return rc;
  1040. }
  1041. /*
  1042. * Management of partially allocated slabs
  1043. */
  1044. static void add_partial(struct kmem_cache_node *n,
  1045. struct page *page, int tail)
  1046. {
  1047. spin_lock(&n->list_lock);
  1048. n->nr_partial++;
  1049. if (tail)
  1050. list_add_tail(&page->lru, &n->partial);
  1051. else
  1052. list_add(&page->lru, &n->partial);
  1053. spin_unlock(&n->list_lock);
  1054. }
  1055. static void remove_partial(struct kmem_cache *s, struct page *page)
  1056. {
  1057. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1058. spin_lock(&n->list_lock);
  1059. list_del(&page->lru);
  1060. n->nr_partial--;
  1061. spin_unlock(&n->list_lock);
  1062. }
  1063. /*
  1064. * Lock slab and remove from the partial list.
  1065. *
  1066. * Must hold list_lock.
  1067. */
  1068. static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
  1069. struct page *page)
  1070. {
  1071. if (slab_trylock(page)) {
  1072. list_del(&page->lru);
  1073. n->nr_partial--;
  1074. __SetPageSlubFrozen(page);
  1075. return 1;
  1076. }
  1077. return 0;
  1078. }
  1079. /*
  1080. * Try to allocate a partial slab from a specific node.
  1081. */
  1082. static struct page *get_partial_node(struct kmem_cache_node *n)
  1083. {
  1084. struct page *page;
  1085. /*
  1086. * Racy check. If we mistakenly see no partial slabs then we
  1087. * just allocate an empty slab. If we mistakenly try to get a
  1088. * partial slab and there is none available then get_partials()
  1089. * will return NULL.
  1090. */
  1091. if (!n || !n->nr_partial)
  1092. return NULL;
  1093. spin_lock(&n->list_lock);
  1094. list_for_each_entry(page, &n->partial, lru)
  1095. if (lock_and_freeze_slab(n, page))
  1096. goto out;
  1097. page = NULL;
  1098. out:
  1099. spin_unlock(&n->list_lock);
  1100. return page;
  1101. }
  1102. /*
  1103. * Get a page from somewhere. Search in increasing NUMA distances.
  1104. */
  1105. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
  1106. {
  1107. #ifdef CONFIG_NUMA
  1108. struct zonelist *zonelist;
  1109. struct zoneref *z;
  1110. struct zone *zone;
  1111. enum zone_type high_zoneidx = gfp_zone(flags);
  1112. struct page *page;
  1113. /*
  1114. * The defrag ratio allows a configuration of the tradeoffs between
  1115. * inter node defragmentation and node local allocations. A lower
  1116. * defrag_ratio increases the tendency to do local allocations
  1117. * instead of attempting to obtain partial slabs from other nodes.
  1118. *
  1119. * If the defrag_ratio is set to 0 then kmalloc() always
  1120. * returns node local objects. If the ratio is higher then kmalloc()
  1121. * may return off node objects because partial slabs are obtained
  1122. * from other nodes and filled up.
  1123. *
  1124. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1125. * defrag_ratio = 1000) then every (well almost) allocation will
  1126. * first attempt to defrag slab caches on other nodes. This means
  1127. * scanning over all nodes to look for partial slabs which may be
  1128. * expensive if we do it every time we are trying to find a slab
  1129. * with available objects.
  1130. */
  1131. if (!s->remote_node_defrag_ratio ||
  1132. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1133. return NULL;
  1134. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1135. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1136. struct kmem_cache_node *n;
  1137. n = get_node(s, zone_to_nid(zone));
  1138. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1139. n->nr_partial > n->min_partial) {
  1140. page = get_partial_node(n);
  1141. if (page)
  1142. return page;
  1143. }
  1144. }
  1145. #endif
  1146. return NULL;
  1147. }
  1148. /*
  1149. * Get a partial page, lock it and return it.
  1150. */
  1151. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  1152. {
  1153. struct page *page;
  1154. int searchnode = (node == -1) ? numa_node_id() : node;
  1155. page = get_partial_node(get_node(s, searchnode));
  1156. if (page || (flags & __GFP_THISNODE))
  1157. return page;
  1158. return get_any_partial(s, flags);
  1159. }
  1160. /*
  1161. * Move a page back to the lists.
  1162. *
  1163. * Must be called with the slab lock held.
  1164. *
  1165. * On exit the slab lock will have been dropped.
  1166. */
  1167. static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
  1168. {
  1169. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1170. struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
  1171. __ClearPageSlubFrozen(page);
  1172. if (page->inuse) {
  1173. if (page->freelist) {
  1174. add_partial(n, page, tail);
  1175. stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
  1176. } else {
  1177. stat(c, DEACTIVATE_FULL);
  1178. if (SLABDEBUG && PageSlubDebug(page) &&
  1179. (s->flags & SLAB_STORE_USER))
  1180. add_full(n, page);
  1181. }
  1182. slab_unlock(page);
  1183. } else {
  1184. stat(c, DEACTIVATE_EMPTY);
  1185. if (n->nr_partial < n->min_partial) {
  1186. /*
  1187. * Adding an empty slab to the partial slabs in order
  1188. * to avoid page allocator overhead. This slab needs
  1189. * to come after the other slabs with objects in
  1190. * so that the others get filled first. That way the
  1191. * size of the partial list stays small.
  1192. *
  1193. * kmem_cache_shrink can reclaim any empty slabs from
  1194. * the partial list.
  1195. */
  1196. add_partial(n, page, 1);
  1197. slab_unlock(page);
  1198. } else {
  1199. slab_unlock(page);
  1200. stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
  1201. discard_slab(s, page);
  1202. }
  1203. }
  1204. }
  1205. /*
  1206. * Remove the cpu slab
  1207. */
  1208. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1209. {
  1210. struct page *page = c->page;
  1211. int tail = 1;
  1212. if (page->freelist)
  1213. stat(c, DEACTIVATE_REMOTE_FREES);
  1214. /*
  1215. * Merge cpu freelist into slab freelist. Typically we get here
  1216. * because both freelists are empty. So this is unlikely
  1217. * to occur.
  1218. */
  1219. while (unlikely(c->freelist)) {
  1220. void **object;
  1221. tail = 0; /* Hot objects. Put the slab first */
  1222. /* Retrieve object from cpu_freelist */
  1223. object = c->freelist;
  1224. c->freelist = c->freelist[c->offset];
  1225. /* And put onto the regular freelist */
  1226. object[c->offset] = page->freelist;
  1227. page->freelist = object;
  1228. page->inuse--;
  1229. }
  1230. c->page = NULL;
  1231. unfreeze_slab(s, page, tail);
  1232. }
  1233. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1234. {
  1235. stat(c, CPUSLAB_FLUSH);
  1236. slab_lock(c->page);
  1237. deactivate_slab(s, c);
  1238. }
  1239. /*
  1240. * Flush cpu slab.
  1241. *
  1242. * Called from IPI handler with interrupts disabled.
  1243. */
  1244. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1245. {
  1246. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1247. if (likely(c && c->page))
  1248. flush_slab(s, c);
  1249. }
  1250. static void flush_cpu_slab(void *d)
  1251. {
  1252. struct kmem_cache *s = d;
  1253. __flush_cpu_slab(s, smp_processor_id());
  1254. }
  1255. static void flush_all(struct kmem_cache *s)
  1256. {
  1257. on_each_cpu(flush_cpu_slab, s, 1);
  1258. }
  1259. /*
  1260. * Check if the objects in a per cpu structure fit numa
  1261. * locality expectations.
  1262. */
  1263. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1264. {
  1265. #ifdef CONFIG_NUMA
  1266. if (node != -1 && c->node != node)
  1267. return 0;
  1268. #endif
  1269. return 1;
  1270. }
  1271. /*
  1272. * Slow path. The lockless freelist is empty or we need to perform
  1273. * debugging duties.
  1274. *
  1275. * Interrupts are disabled.
  1276. *
  1277. * Processing is still very fast if new objects have been freed to the
  1278. * regular freelist. In that case we simply take over the regular freelist
  1279. * as the lockless freelist and zap the regular freelist.
  1280. *
  1281. * If that is not working then we fall back to the partial lists. We take the
  1282. * first element of the freelist as the object to allocate now and move the
  1283. * rest of the freelist to the lockless freelist.
  1284. *
  1285. * And if we were unable to get a new slab from the partial slab lists then
  1286. * we need to allocate a new slab. This is the slowest path since it involves
  1287. * a call to the page allocator and the setup of a new slab.
  1288. */
  1289. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  1290. unsigned long addr, struct kmem_cache_cpu *c)
  1291. {
  1292. void **object;
  1293. struct page *new;
  1294. /* We handle __GFP_ZERO in the caller */
  1295. gfpflags &= ~__GFP_ZERO;
  1296. if (!c->page)
  1297. goto new_slab;
  1298. slab_lock(c->page);
  1299. if (unlikely(!node_match(c, node)))
  1300. goto another_slab;
  1301. stat(c, ALLOC_REFILL);
  1302. load_freelist:
  1303. object = c->page->freelist;
  1304. if (unlikely(!object))
  1305. goto another_slab;
  1306. if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
  1307. goto debug;
  1308. c->freelist = object[c->offset];
  1309. c->page->inuse = c->page->objects;
  1310. c->page->freelist = NULL;
  1311. c->node = page_to_nid(c->page);
  1312. unlock_out:
  1313. slab_unlock(c->page);
  1314. stat(c, ALLOC_SLOWPATH);
  1315. return object;
  1316. another_slab:
  1317. deactivate_slab(s, c);
  1318. new_slab:
  1319. new = get_partial(s, gfpflags, node);
  1320. if (new) {
  1321. c->page = new;
  1322. stat(c, ALLOC_FROM_PARTIAL);
  1323. goto load_freelist;
  1324. }
  1325. if (gfpflags & __GFP_WAIT)
  1326. local_irq_enable();
  1327. new = new_slab(s, gfpflags, node);
  1328. if (gfpflags & __GFP_WAIT)
  1329. local_irq_disable();
  1330. if (new) {
  1331. c = get_cpu_slab(s, smp_processor_id());
  1332. stat(c, ALLOC_SLAB);
  1333. if (c->page)
  1334. flush_slab(s, c);
  1335. slab_lock(new);
  1336. __SetPageSlubFrozen(new);
  1337. c->page = new;
  1338. goto load_freelist;
  1339. }
  1340. return NULL;
  1341. debug:
  1342. if (!alloc_debug_processing(s, c->page, object, addr))
  1343. goto another_slab;
  1344. c->page->inuse++;
  1345. c->page->freelist = object[c->offset];
  1346. c->node = -1;
  1347. goto unlock_out;
  1348. }
  1349. /*
  1350. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1351. * have the fastpath folded into their functions. So no function call
  1352. * overhead for requests that can be satisfied on the fastpath.
  1353. *
  1354. * The fastpath works by first checking if the lockless freelist can be used.
  1355. * If not then __slab_alloc is called for slow processing.
  1356. *
  1357. * Otherwise we can simply pick the next object from the lockless free list.
  1358. */
  1359. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1360. gfp_t gfpflags, int node, unsigned long addr)
  1361. {
  1362. void **object;
  1363. struct kmem_cache_cpu *c;
  1364. unsigned long flags;
  1365. unsigned int objsize;
  1366. local_irq_save(flags);
  1367. c = get_cpu_slab(s, smp_processor_id());
  1368. objsize = c->objsize;
  1369. if (unlikely(!c->freelist || !node_match(c, node)))
  1370. object = __slab_alloc(s, gfpflags, node, addr, c);
  1371. else {
  1372. object = c->freelist;
  1373. c->freelist = object[c->offset];
  1374. stat(c, ALLOC_FASTPATH);
  1375. }
  1376. local_irq_restore(flags);
  1377. if (unlikely((gfpflags & __GFP_ZERO) && object))
  1378. memset(object, 0, objsize);
  1379. return object;
  1380. }
  1381. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1382. {
  1383. void *ret = slab_alloc(s, gfpflags, -1, _RET_IP_);
  1384. kmemtrace_mark_alloc(KMEMTRACE_TYPE_CACHE, _RET_IP_, ret,
  1385. s->objsize, s->size, gfpflags);
  1386. return ret;
  1387. }
  1388. EXPORT_SYMBOL(kmem_cache_alloc);
  1389. #ifdef CONFIG_KMEMTRACE
  1390. void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
  1391. {
  1392. return slab_alloc(s, gfpflags, -1, _RET_IP_);
  1393. }
  1394. EXPORT_SYMBOL(kmem_cache_alloc_notrace);
  1395. #endif
  1396. #ifdef CONFIG_NUMA
  1397. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1398. {
  1399. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1400. kmemtrace_mark_alloc_node(KMEMTRACE_TYPE_CACHE, _RET_IP_, ret,
  1401. s->objsize, s->size, gfpflags, node);
  1402. return ret;
  1403. }
  1404. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1405. #endif
  1406. #ifdef CONFIG_KMEMTRACE
  1407. void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
  1408. gfp_t gfpflags,
  1409. int node)
  1410. {
  1411. return slab_alloc(s, gfpflags, node, _RET_IP_);
  1412. }
  1413. EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
  1414. #endif
  1415. /*
  1416. * Slow patch handling. This may still be called frequently since objects
  1417. * have a longer lifetime than the cpu slabs in most processing loads.
  1418. *
  1419. * So we still attempt to reduce cache line usage. Just take the slab
  1420. * lock and free the item. If there is no additional partial page
  1421. * handling required then we can return immediately.
  1422. */
  1423. static void __slab_free(struct kmem_cache *s, struct page *page,
  1424. void *x, unsigned long addr, unsigned int offset)
  1425. {
  1426. void *prior;
  1427. void **object = (void *)x;
  1428. struct kmem_cache_cpu *c;
  1429. c = get_cpu_slab(s, raw_smp_processor_id());
  1430. stat(c, FREE_SLOWPATH);
  1431. slab_lock(page);
  1432. if (unlikely(SLABDEBUG && PageSlubDebug(page)))
  1433. goto debug;
  1434. checks_ok:
  1435. prior = object[offset] = page->freelist;
  1436. page->freelist = object;
  1437. page->inuse--;
  1438. if (unlikely(PageSlubFrozen(page))) {
  1439. stat(c, FREE_FROZEN);
  1440. goto out_unlock;
  1441. }
  1442. if (unlikely(!page->inuse))
  1443. goto slab_empty;
  1444. /*
  1445. * Objects left in the slab. If it was not on the partial list before
  1446. * then add it.
  1447. */
  1448. if (unlikely(!prior)) {
  1449. add_partial(get_node(s, page_to_nid(page)), page, 1);
  1450. stat(c, FREE_ADD_PARTIAL);
  1451. }
  1452. out_unlock:
  1453. slab_unlock(page);
  1454. return;
  1455. slab_empty:
  1456. if (prior) {
  1457. /*
  1458. * Slab still on the partial list.
  1459. */
  1460. remove_partial(s, page);
  1461. stat(c, FREE_REMOVE_PARTIAL);
  1462. }
  1463. slab_unlock(page);
  1464. stat(c, FREE_SLAB);
  1465. discard_slab(s, page);
  1466. return;
  1467. debug:
  1468. if (!free_debug_processing(s, page, x, addr))
  1469. goto out_unlock;
  1470. goto checks_ok;
  1471. }
  1472. /*
  1473. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1474. * can perform fastpath freeing without additional function calls.
  1475. *
  1476. * The fastpath is only possible if we are freeing to the current cpu slab
  1477. * of this processor. This typically the case if we have just allocated
  1478. * the item before.
  1479. *
  1480. * If fastpath is not possible then fall back to __slab_free where we deal
  1481. * with all sorts of special processing.
  1482. */
  1483. static __always_inline void slab_free(struct kmem_cache *s,
  1484. struct page *page, void *x, unsigned long addr)
  1485. {
  1486. void **object = (void *)x;
  1487. struct kmem_cache_cpu *c;
  1488. unsigned long flags;
  1489. local_irq_save(flags);
  1490. c = get_cpu_slab(s, smp_processor_id());
  1491. debug_check_no_locks_freed(object, c->objsize);
  1492. if (!(s->flags & SLAB_DEBUG_OBJECTS))
  1493. debug_check_no_obj_freed(object, s->objsize);
  1494. if (likely(page == c->page && c->node >= 0)) {
  1495. object[c->offset] = c->freelist;
  1496. c->freelist = object;
  1497. stat(c, FREE_FASTPATH);
  1498. } else
  1499. __slab_free(s, page, x, addr, c->offset);
  1500. local_irq_restore(flags);
  1501. }
  1502. void kmem_cache_free(struct kmem_cache *s, void *x)
  1503. {
  1504. struct page *page;
  1505. page = virt_to_head_page(x);
  1506. slab_free(s, page, x, _RET_IP_);
  1507. kmemtrace_mark_free(KMEMTRACE_TYPE_CACHE, _RET_IP_, x);
  1508. }
  1509. EXPORT_SYMBOL(kmem_cache_free);
  1510. /* Figure out on which slab object the object resides */
  1511. static struct page *get_object_page(const void *x)
  1512. {
  1513. struct page *page = virt_to_head_page(x);
  1514. if (!PageSlab(page))
  1515. return NULL;
  1516. return page;
  1517. }
  1518. /*
  1519. * Object placement in a slab is made very easy because we always start at
  1520. * offset 0. If we tune the size of the object to the alignment then we can
  1521. * get the required alignment by putting one properly sized object after
  1522. * another.
  1523. *
  1524. * Notice that the allocation order determines the sizes of the per cpu
  1525. * caches. Each processor has always one slab available for allocations.
  1526. * Increasing the allocation order reduces the number of times that slabs
  1527. * must be moved on and off the partial lists and is therefore a factor in
  1528. * locking overhead.
  1529. */
  1530. /*
  1531. * Mininum / Maximum order of slab pages. This influences locking overhead
  1532. * and slab fragmentation. A higher order reduces the number of partial slabs
  1533. * and increases the number of allocations possible without having to
  1534. * take the list_lock.
  1535. */
  1536. static int slub_min_order;
  1537. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  1538. static int slub_min_objects;
  1539. /*
  1540. * Merge control. If this is set then no merging of slab caches will occur.
  1541. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1542. */
  1543. static int slub_nomerge;
  1544. /*
  1545. * Calculate the order of allocation given an slab object size.
  1546. *
  1547. * The order of allocation has significant impact on performance and other
  1548. * system components. Generally order 0 allocations should be preferred since
  1549. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1550. * be problematic to put into order 0 slabs because there may be too much
  1551. * unused space left. We go to a higher order if more than 1/16th of the slab
  1552. * would be wasted.
  1553. *
  1554. * In order to reach satisfactory performance we must ensure that a minimum
  1555. * number of objects is in one slab. Otherwise we may generate too much
  1556. * activity on the partial lists which requires taking the list_lock. This is
  1557. * less a concern for large slabs though which are rarely used.
  1558. *
  1559. * slub_max_order specifies the order where we begin to stop considering the
  1560. * number of objects in a slab as critical. If we reach slub_max_order then
  1561. * we try to keep the page order as low as possible. So we accept more waste
  1562. * of space in favor of a small page order.
  1563. *
  1564. * Higher order allocations also allow the placement of more objects in a
  1565. * slab and thereby reduce object handling overhead. If the user has
  1566. * requested a higher mininum order then we start with that one instead of
  1567. * the smallest order which will fit the object.
  1568. */
  1569. static inline int slab_order(int size, int min_objects,
  1570. int max_order, int fract_leftover)
  1571. {
  1572. int order;
  1573. int rem;
  1574. int min_order = slub_min_order;
  1575. if ((PAGE_SIZE << min_order) / size > 65535)
  1576. return get_order(size * 65535) - 1;
  1577. for (order = max(min_order,
  1578. fls(min_objects * size - 1) - PAGE_SHIFT);
  1579. order <= max_order; order++) {
  1580. unsigned long slab_size = PAGE_SIZE << order;
  1581. if (slab_size < min_objects * size)
  1582. continue;
  1583. rem = slab_size % size;
  1584. if (rem <= slab_size / fract_leftover)
  1585. break;
  1586. }
  1587. return order;
  1588. }
  1589. static inline int calculate_order(int size)
  1590. {
  1591. int order;
  1592. int min_objects;
  1593. int fraction;
  1594. /*
  1595. * Attempt to find best configuration for a slab. This
  1596. * works by first attempting to generate a layout with
  1597. * the best configuration and backing off gradually.
  1598. *
  1599. * First we reduce the acceptable waste in a slab. Then
  1600. * we reduce the minimum objects required in a slab.
  1601. */
  1602. min_objects = slub_min_objects;
  1603. if (!min_objects)
  1604. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  1605. while (min_objects > 1) {
  1606. fraction = 16;
  1607. while (fraction >= 4) {
  1608. order = slab_order(size, min_objects,
  1609. slub_max_order, fraction);
  1610. if (order <= slub_max_order)
  1611. return order;
  1612. fraction /= 2;
  1613. }
  1614. min_objects /= 2;
  1615. }
  1616. /*
  1617. * We were unable to place multiple objects in a slab. Now
  1618. * lets see if we can place a single object there.
  1619. */
  1620. order = slab_order(size, 1, slub_max_order, 1);
  1621. if (order <= slub_max_order)
  1622. return order;
  1623. /*
  1624. * Doh this slab cannot be placed using slub_max_order.
  1625. */
  1626. order = slab_order(size, 1, MAX_ORDER, 1);
  1627. if (order <= MAX_ORDER)
  1628. return order;
  1629. return -ENOSYS;
  1630. }
  1631. /*
  1632. * Figure out what the alignment of the objects will be.
  1633. */
  1634. static unsigned long calculate_alignment(unsigned long flags,
  1635. unsigned long align, unsigned long size)
  1636. {
  1637. /*
  1638. * If the user wants hardware cache aligned objects then follow that
  1639. * suggestion if the object is sufficiently large.
  1640. *
  1641. * The hardware cache alignment cannot override the specified
  1642. * alignment though. If that is greater then use it.
  1643. */
  1644. if (flags & SLAB_HWCACHE_ALIGN) {
  1645. unsigned long ralign = cache_line_size();
  1646. while (size <= ralign / 2)
  1647. ralign /= 2;
  1648. align = max(align, ralign);
  1649. }
  1650. if (align < ARCH_SLAB_MINALIGN)
  1651. align = ARCH_SLAB_MINALIGN;
  1652. return ALIGN(align, sizeof(void *));
  1653. }
  1654. static void init_kmem_cache_cpu(struct kmem_cache *s,
  1655. struct kmem_cache_cpu *c)
  1656. {
  1657. c->page = NULL;
  1658. c->freelist = NULL;
  1659. c->node = 0;
  1660. c->offset = s->offset / sizeof(void *);
  1661. c->objsize = s->objsize;
  1662. #ifdef CONFIG_SLUB_STATS
  1663. memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
  1664. #endif
  1665. }
  1666. static void
  1667. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  1668. {
  1669. n->nr_partial = 0;
  1670. /*
  1671. * The larger the object size is, the more pages we want on the partial
  1672. * list to avoid pounding the page allocator excessively.
  1673. */
  1674. n->min_partial = ilog2(s->size);
  1675. if (n->min_partial < MIN_PARTIAL)
  1676. n->min_partial = MIN_PARTIAL;
  1677. else if (n->min_partial > MAX_PARTIAL)
  1678. n->min_partial = MAX_PARTIAL;
  1679. spin_lock_init(&n->list_lock);
  1680. INIT_LIST_HEAD(&n->partial);
  1681. #ifdef CONFIG_SLUB_DEBUG
  1682. atomic_long_set(&n->nr_slabs, 0);
  1683. atomic_long_set(&n->total_objects, 0);
  1684. INIT_LIST_HEAD(&n->full);
  1685. #endif
  1686. }
  1687. #ifdef CONFIG_SMP
  1688. /*
  1689. * Per cpu array for per cpu structures.
  1690. *
  1691. * The per cpu array places all kmem_cache_cpu structures from one processor
  1692. * close together meaning that it becomes possible that multiple per cpu
  1693. * structures are contained in one cacheline. This may be particularly
  1694. * beneficial for the kmalloc caches.
  1695. *
  1696. * A desktop system typically has around 60-80 slabs. With 100 here we are
  1697. * likely able to get per cpu structures for all caches from the array defined
  1698. * here. We must be able to cover all kmalloc caches during bootstrap.
  1699. *
  1700. * If the per cpu array is exhausted then fall back to kmalloc
  1701. * of individual cachelines. No sharing is possible then.
  1702. */
  1703. #define NR_KMEM_CACHE_CPU 100
  1704. static DEFINE_PER_CPU(struct kmem_cache_cpu,
  1705. kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
  1706. static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
  1707. static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
  1708. static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
  1709. int cpu, gfp_t flags)
  1710. {
  1711. struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
  1712. if (c)
  1713. per_cpu(kmem_cache_cpu_free, cpu) =
  1714. (void *)c->freelist;
  1715. else {
  1716. /* Table overflow: So allocate ourselves */
  1717. c = kmalloc_node(
  1718. ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
  1719. flags, cpu_to_node(cpu));
  1720. if (!c)
  1721. return NULL;
  1722. }
  1723. init_kmem_cache_cpu(s, c);
  1724. return c;
  1725. }
  1726. static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
  1727. {
  1728. if (c < per_cpu(kmem_cache_cpu, cpu) ||
  1729. c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
  1730. kfree(c);
  1731. return;
  1732. }
  1733. c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
  1734. per_cpu(kmem_cache_cpu_free, cpu) = c;
  1735. }
  1736. static void free_kmem_cache_cpus(struct kmem_cache *s)
  1737. {
  1738. int cpu;
  1739. for_each_online_cpu(cpu) {
  1740. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1741. if (c) {
  1742. s->cpu_slab[cpu] = NULL;
  1743. free_kmem_cache_cpu(c, cpu);
  1744. }
  1745. }
  1746. }
  1747. static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1748. {
  1749. int cpu;
  1750. for_each_online_cpu(cpu) {
  1751. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1752. if (c)
  1753. continue;
  1754. c = alloc_kmem_cache_cpu(s, cpu, flags);
  1755. if (!c) {
  1756. free_kmem_cache_cpus(s);
  1757. return 0;
  1758. }
  1759. s->cpu_slab[cpu] = c;
  1760. }
  1761. return 1;
  1762. }
  1763. /*
  1764. * Initialize the per cpu array.
  1765. */
  1766. static void init_alloc_cpu_cpu(int cpu)
  1767. {
  1768. int i;
  1769. if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
  1770. return;
  1771. for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
  1772. free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
  1773. cpu_set(cpu, kmem_cach_cpu_free_init_once);
  1774. }
  1775. static void __init init_alloc_cpu(void)
  1776. {
  1777. int cpu;
  1778. for_each_online_cpu(cpu)
  1779. init_alloc_cpu_cpu(cpu);
  1780. }
  1781. #else
  1782. static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
  1783. static inline void init_alloc_cpu(void) {}
  1784. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1785. {
  1786. init_kmem_cache_cpu(s, &s->cpu_slab);
  1787. return 1;
  1788. }
  1789. #endif
  1790. #ifdef CONFIG_NUMA
  1791. /*
  1792. * No kmalloc_node yet so do it by hand. We know that this is the first
  1793. * slab on the node for this slabcache. There are no concurrent accesses
  1794. * possible.
  1795. *
  1796. * Note that this function only works on the kmalloc_node_cache
  1797. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  1798. * memory on a fresh node that has no slab structures yet.
  1799. */
  1800. static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
  1801. int node)
  1802. {
  1803. struct page *page;
  1804. struct kmem_cache_node *n;
  1805. unsigned long flags;
  1806. BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
  1807. page = new_slab(kmalloc_caches, gfpflags, node);
  1808. BUG_ON(!page);
  1809. if (page_to_nid(page) != node) {
  1810. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  1811. "node %d\n", node);
  1812. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  1813. "in order to be able to continue\n");
  1814. }
  1815. n = page->freelist;
  1816. BUG_ON(!n);
  1817. page->freelist = get_freepointer(kmalloc_caches, n);
  1818. page->inuse++;
  1819. kmalloc_caches->node[node] = n;
  1820. #ifdef CONFIG_SLUB_DEBUG
  1821. init_object(kmalloc_caches, n, 1);
  1822. init_tracking(kmalloc_caches, n);
  1823. #endif
  1824. init_kmem_cache_node(n, kmalloc_caches);
  1825. inc_slabs_node(kmalloc_caches, node, page->objects);
  1826. /*
  1827. * lockdep requires consistent irq usage for each lock
  1828. * so even though there cannot be a race this early in
  1829. * the boot sequence, we still disable irqs.
  1830. */
  1831. local_irq_save(flags);
  1832. add_partial(n, page, 0);
  1833. local_irq_restore(flags);
  1834. return n;
  1835. }
  1836. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1837. {
  1838. int node;
  1839. for_each_node_state(node, N_NORMAL_MEMORY) {
  1840. struct kmem_cache_node *n = s->node[node];
  1841. if (n && n != &s->local_node)
  1842. kmem_cache_free(kmalloc_caches, n);
  1843. s->node[node] = NULL;
  1844. }
  1845. }
  1846. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1847. {
  1848. int node;
  1849. int local_node;
  1850. if (slab_state >= UP)
  1851. local_node = page_to_nid(virt_to_page(s));
  1852. else
  1853. local_node = 0;
  1854. for_each_node_state(node, N_NORMAL_MEMORY) {
  1855. struct kmem_cache_node *n;
  1856. if (local_node == node)
  1857. n = &s->local_node;
  1858. else {
  1859. if (slab_state == DOWN) {
  1860. n = early_kmem_cache_node_alloc(gfpflags,
  1861. node);
  1862. continue;
  1863. }
  1864. n = kmem_cache_alloc_node(kmalloc_caches,
  1865. gfpflags, node);
  1866. if (!n) {
  1867. free_kmem_cache_nodes(s);
  1868. return 0;
  1869. }
  1870. }
  1871. s->node[node] = n;
  1872. init_kmem_cache_node(n, s);
  1873. }
  1874. return 1;
  1875. }
  1876. #else
  1877. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1878. {
  1879. }
  1880. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1881. {
  1882. init_kmem_cache_node(&s->local_node, s);
  1883. return 1;
  1884. }
  1885. #endif
  1886. /*
  1887. * calculate_sizes() determines the order and the distribution of data within
  1888. * a slab object.
  1889. */
  1890. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  1891. {
  1892. unsigned long flags = s->flags;
  1893. unsigned long size = s->objsize;
  1894. unsigned long align = s->align;
  1895. int order;
  1896. /*
  1897. * Round up object size to the next word boundary. We can only
  1898. * place the free pointer at word boundaries and this determines
  1899. * the possible location of the free pointer.
  1900. */
  1901. size = ALIGN(size, sizeof(void *));
  1902. #ifdef CONFIG_SLUB_DEBUG
  1903. /*
  1904. * Determine if we can poison the object itself. If the user of
  1905. * the slab may touch the object after free or before allocation
  1906. * then we should never poison the object itself.
  1907. */
  1908. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  1909. !s->ctor)
  1910. s->flags |= __OBJECT_POISON;
  1911. else
  1912. s->flags &= ~__OBJECT_POISON;
  1913. /*
  1914. * If we are Redzoning then check if there is some space between the
  1915. * end of the object and the free pointer. If not then add an
  1916. * additional word to have some bytes to store Redzone information.
  1917. */
  1918. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  1919. size += sizeof(void *);
  1920. #endif
  1921. /*
  1922. * With that we have determined the number of bytes in actual use
  1923. * by the object. This is the potential offset to the free pointer.
  1924. */
  1925. s->inuse = size;
  1926. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  1927. s->ctor)) {
  1928. /*
  1929. * Relocate free pointer after the object if it is not
  1930. * permitted to overwrite the first word of the object on
  1931. * kmem_cache_free.
  1932. *
  1933. * This is the case if we do RCU, have a constructor or
  1934. * destructor or are poisoning the objects.
  1935. */
  1936. s->offset = size;
  1937. size += sizeof(void *);
  1938. }
  1939. #ifdef CONFIG_SLUB_DEBUG
  1940. if (flags & SLAB_STORE_USER)
  1941. /*
  1942. * Need to store information about allocs and frees after
  1943. * the object.
  1944. */
  1945. size += 2 * sizeof(struct track);
  1946. if (flags & SLAB_RED_ZONE)
  1947. /*
  1948. * Add some empty padding so that we can catch
  1949. * overwrites from earlier objects rather than let
  1950. * tracking information or the free pointer be
  1951. * corrupted if an user writes before the start
  1952. * of the object.
  1953. */
  1954. size += sizeof(void *);
  1955. #endif
  1956. /*
  1957. * Determine the alignment based on various parameters that the
  1958. * user specified and the dynamic determination of cache line size
  1959. * on bootup.
  1960. */
  1961. align = calculate_alignment(flags, align, s->objsize);
  1962. /*
  1963. * SLUB stores one object immediately after another beginning from
  1964. * offset 0. In order to align the objects we have to simply size
  1965. * each object to conform to the alignment.
  1966. */
  1967. size = ALIGN(size, align);
  1968. s->size = size;
  1969. if (forced_order >= 0)
  1970. order = forced_order;
  1971. else
  1972. order = calculate_order(size);
  1973. if (order < 0)
  1974. return 0;
  1975. s->allocflags = 0;
  1976. if (order)
  1977. s->allocflags |= __GFP_COMP;
  1978. if (s->flags & SLAB_CACHE_DMA)
  1979. s->allocflags |= SLUB_DMA;
  1980. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  1981. s->allocflags |= __GFP_RECLAIMABLE;
  1982. /*
  1983. * Determine the number of objects per slab
  1984. */
  1985. s->oo = oo_make(order, size);
  1986. s->min = oo_make(get_order(size), size);
  1987. if (oo_objects(s->oo) > oo_objects(s->max))
  1988. s->max = s->oo;
  1989. return !!oo_objects(s->oo);
  1990. }
  1991. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
  1992. const char *name, size_t size,
  1993. size_t align, unsigned long flags,
  1994. void (*ctor)(void *))
  1995. {
  1996. memset(s, 0, kmem_size);
  1997. s->name = name;
  1998. s->ctor = ctor;
  1999. s->objsize = size;
  2000. s->align = align;
  2001. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2002. if (!calculate_sizes(s, -1))
  2003. goto error;
  2004. s->refcount = 1;
  2005. #ifdef CONFIG_NUMA
  2006. s->remote_node_defrag_ratio = 1000;
  2007. #endif
  2008. if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
  2009. goto error;
  2010. if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
  2011. return 1;
  2012. free_kmem_cache_nodes(s);
  2013. error:
  2014. if (flags & SLAB_PANIC)
  2015. panic("Cannot create slab %s size=%lu realsize=%u "
  2016. "order=%u offset=%u flags=%lx\n",
  2017. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2018. s->offset, flags);
  2019. return 0;
  2020. }
  2021. /*
  2022. * Check if a given pointer is valid
  2023. */
  2024. int kmem_ptr_validate(struct kmem_cache *s, const void *object)
  2025. {
  2026. struct page *page;
  2027. page = get_object_page(object);
  2028. if (!page || s != page->slab)
  2029. /* No slab or wrong slab */
  2030. return 0;
  2031. if (!check_valid_pointer(s, page, object))
  2032. return 0;
  2033. /*
  2034. * We could also check if the object is on the slabs freelist.
  2035. * But this would be too expensive and it seems that the main
  2036. * purpose of kmem_ptr_valid() is to check if the object belongs
  2037. * to a certain slab.
  2038. */
  2039. return 1;
  2040. }
  2041. EXPORT_SYMBOL(kmem_ptr_validate);
  2042. /*
  2043. * Determine the size of a slab object
  2044. */
  2045. unsigned int kmem_cache_size(struct kmem_cache *s)
  2046. {
  2047. return s->objsize;
  2048. }
  2049. EXPORT_SYMBOL(kmem_cache_size);
  2050. const char *kmem_cache_name(struct kmem_cache *s)
  2051. {
  2052. return s->name;
  2053. }
  2054. EXPORT_SYMBOL(kmem_cache_name);
  2055. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2056. const char *text)
  2057. {
  2058. #ifdef CONFIG_SLUB_DEBUG
  2059. void *addr = page_address(page);
  2060. void *p;
  2061. DECLARE_BITMAP(map, page->objects);
  2062. bitmap_zero(map, page->objects);
  2063. slab_err(s, page, "%s", text);
  2064. slab_lock(page);
  2065. for_each_free_object(p, s, page->freelist)
  2066. set_bit(slab_index(p, s, addr), map);
  2067. for_each_object(p, s, addr, page->objects) {
  2068. if (!test_bit(slab_index(p, s, addr), map)) {
  2069. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2070. p, p - addr);
  2071. print_tracking(s, p);
  2072. }
  2073. }
  2074. slab_unlock(page);
  2075. #endif
  2076. }
  2077. /*
  2078. * Attempt to free all partial slabs on a node.
  2079. */
  2080. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2081. {
  2082. unsigned long flags;
  2083. struct page *page, *h;
  2084. spin_lock_irqsave(&n->list_lock, flags);
  2085. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2086. if (!page->inuse) {
  2087. list_del(&page->lru);
  2088. discard_slab(s, page);
  2089. n->nr_partial--;
  2090. } else {
  2091. list_slab_objects(s, page,
  2092. "Objects remaining on kmem_cache_close()");
  2093. }
  2094. }
  2095. spin_unlock_irqrestore(&n->list_lock, flags);
  2096. }
  2097. /*
  2098. * Release all resources used by a slab cache.
  2099. */
  2100. static inline int kmem_cache_close(struct kmem_cache *s)
  2101. {
  2102. int node;
  2103. flush_all(s);
  2104. /* Attempt to free all objects */
  2105. free_kmem_cache_cpus(s);
  2106. for_each_node_state(node, N_NORMAL_MEMORY) {
  2107. struct kmem_cache_node *n = get_node(s, node);
  2108. free_partial(s, n);
  2109. if (n->nr_partial || slabs_node(s, node))
  2110. return 1;
  2111. }
  2112. free_kmem_cache_nodes(s);
  2113. return 0;
  2114. }
  2115. /*
  2116. * Close a cache and release the kmem_cache structure
  2117. * (must be used for caches created using kmem_cache_create)
  2118. */
  2119. void kmem_cache_destroy(struct kmem_cache *s)
  2120. {
  2121. down_write(&slub_lock);
  2122. s->refcount--;
  2123. if (!s->refcount) {
  2124. list_del(&s->list);
  2125. up_write(&slub_lock);
  2126. if (kmem_cache_close(s)) {
  2127. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2128. "still has objects.\n", s->name, __func__);
  2129. dump_stack();
  2130. }
  2131. sysfs_slab_remove(s);
  2132. } else
  2133. up_write(&slub_lock);
  2134. }
  2135. EXPORT_SYMBOL(kmem_cache_destroy);
  2136. /********************************************************************
  2137. * Kmalloc subsystem
  2138. *******************************************************************/
  2139. struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
  2140. EXPORT_SYMBOL(kmalloc_caches);
  2141. static int __init setup_slub_min_order(char *str)
  2142. {
  2143. get_option(&str, &slub_min_order);
  2144. return 1;
  2145. }
  2146. __setup("slub_min_order=", setup_slub_min_order);
  2147. static int __init setup_slub_max_order(char *str)
  2148. {
  2149. get_option(&str, &slub_max_order);
  2150. return 1;
  2151. }
  2152. __setup("slub_max_order=", setup_slub_max_order);
  2153. static int __init setup_slub_min_objects(char *str)
  2154. {
  2155. get_option(&str, &slub_min_objects);
  2156. return 1;
  2157. }
  2158. __setup("slub_min_objects=", setup_slub_min_objects);
  2159. static int __init setup_slub_nomerge(char *str)
  2160. {
  2161. slub_nomerge = 1;
  2162. return 1;
  2163. }
  2164. __setup("slub_nomerge", setup_slub_nomerge);
  2165. static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
  2166. const char *name, int size, gfp_t gfp_flags)
  2167. {
  2168. unsigned int flags = 0;
  2169. if (gfp_flags & SLUB_DMA)
  2170. flags = SLAB_CACHE_DMA;
  2171. down_write(&slub_lock);
  2172. if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
  2173. flags, NULL))
  2174. goto panic;
  2175. list_add(&s->list, &slab_caches);
  2176. up_write(&slub_lock);
  2177. if (sysfs_slab_add(s))
  2178. goto panic;
  2179. return s;
  2180. panic:
  2181. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2182. }
  2183. #ifdef CONFIG_ZONE_DMA
  2184. static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
  2185. static void sysfs_add_func(struct work_struct *w)
  2186. {
  2187. struct kmem_cache *s;
  2188. down_write(&slub_lock);
  2189. list_for_each_entry(s, &slab_caches, list) {
  2190. if (s->flags & __SYSFS_ADD_DEFERRED) {
  2191. s->flags &= ~__SYSFS_ADD_DEFERRED;
  2192. sysfs_slab_add(s);
  2193. }
  2194. }
  2195. up_write(&slub_lock);
  2196. }
  2197. static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
  2198. static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
  2199. {
  2200. struct kmem_cache *s;
  2201. char *text;
  2202. size_t realsize;
  2203. s = kmalloc_caches_dma[index];
  2204. if (s)
  2205. return s;
  2206. /* Dynamically create dma cache */
  2207. if (flags & __GFP_WAIT)
  2208. down_write(&slub_lock);
  2209. else {
  2210. if (!down_write_trylock(&slub_lock))
  2211. goto out;
  2212. }
  2213. if (kmalloc_caches_dma[index])
  2214. goto unlock_out;
  2215. realsize = kmalloc_caches[index].objsize;
  2216. text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
  2217. (unsigned int)realsize);
  2218. s = kmalloc(kmem_size, flags & ~SLUB_DMA);
  2219. if (!s || !text || !kmem_cache_open(s, flags, text,
  2220. realsize, ARCH_KMALLOC_MINALIGN,
  2221. SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
  2222. kfree(s);
  2223. kfree(text);
  2224. goto unlock_out;
  2225. }
  2226. list_add(&s->list, &slab_caches);
  2227. kmalloc_caches_dma[index] = s;
  2228. schedule_work(&sysfs_add_work);
  2229. unlock_out:
  2230. up_write(&slub_lock);
  2231. out:
  2232. return kmalloc_caches_dma[index];
  2233. }
  2234. #endif
  2235. /*
  2236. * Conversion table for small slabs sizes / 8 to the index in the
  2237. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2238. * of two cache sizes there. The size of larger slabs can be determined using
  2239. * fls.
  2240. */
  2241. static s8 size_index[24] = {
  2242. 3, /* 8 */
  2243. 4, /* 16 */
  2244. 5, /* 24 */
  2245. 5, /* 32 */
  2246. 6, /* 40 */
  2247. 6, /* 48 */
  2248. 6, /* 56 */
  2249. 6, /* 64 */
  2250. 1, /* 72 */
  2251. 1, /* 80 */
  2252. 1, /* 88 */
  2253. 1, /* 96 */
  2254. 7, /* 104 */
  2255. 7, /* 112 */
  2256. 7, /* 120 */
  2257. 7, /* 128 */
  2258. 2, /* 136 */
  2259. 2, /* 144 */
  2260. 2, /* 152 */
  2261. 2, /* 160 */
  2262. 2, /* 168 */
  2263. 2, /* 176 */
  2264. 2, /* 184 */
  2265. 2 /* 192 */
  2266. };
  2267. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2268. {
  2269. int index;
  2270. if (size <= 192) {
  2271. if (!size)
  2272. return ZERO_SIZE_PTR;
  2273. index = size_index[(size - 1) / 8];
  2274. } else
  2275. index = fls(size - 1);
  2276. #ifdef CONFIG_ZONE_DMA
  2277. if (unlikely((flags & SLUB_DMA)))
  2278. return dma_kmalloc_cache(index, flags);
  2279. #endif
  2280. return &kmalloc_caches[index];
  2281. }
  2282. void *__kmalloc(size_t size, gfp_t flags)
  2283. {
  2284. struct kmem_cache *s;
  2285. void *ret;
  2286. if (unlikely(size > PAGE_SIZE))
  2287. return kmalloc_large(size, flags);
  2288. s = get_slab(size, flags);
  2289. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2290. return s;
  2291. ret = slab_alloc(s, flags, -1, _RET_IP_);
  2292. kmemtrace_mark_alloc(KMEMTRACE_TYPE_KMALLOC, _RET_IP_, ret,
  2293. size, s->size, flags);
  2294. return ret;
  2295. }
  2296. EXPORT_SYMBOL(__kmalloc);
  2297. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2298. {
  2299. struct page *page = alloc_pages_node(node, flags | __GFP_COMP,
  2300. get_order(size));
  2301. if (page)
  2302. return page_address(page);
  2303. else
  2304. return NULL;
  2305. }
  2306. #ifdef CONFIG_NUMA
  2307. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2308. {
  2309. struct kmem_cache *s;
  2310. void *ret;
  2311. if (unlikely(size > PAGE_SIZE)) {
  2312. ret = kmalloc_large_node(size, flags, node);
  2313. kmemtrace_mark_alloc_node(KMEMTRACE_TYPE_KMALLOC,
  2314. _RET_IP_, ret,
  2315. size, PAGE_SIZE << get_order(size),
  2316. flags, node);
  2317. return ret;
  2318. }
  2319. s = get_slab(size, flags);
  2320. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2321. return s;
  2322. ret = slab_alloc(s, flags, node, _RET_IP_);
  2323. kmemtrace_mark_alloc_node(KMEMTRACE_TYPE_KMALLOC, _RET_IP_, ret,
  2324. size, s->size, flags, node);
  2325. return ret;
  2326. }
  2327. EXPORT_SYMBOL(__kmalloc_node);
  2328. #endif
  2329. size_t ksize(const void *object)
  2330. {
  2331. struct page *page;
  2332. struct kmem_cache *s;
  2333. if (unlikely(object == ZERO_SIZE_PTR))
  2334. return 0;
  2335. page = virt_to_head_page(object);
  2336. if (unlikely(!PageSlab(page))) {
  2337. WARN_ON(!PageCompound(page));
  2338. return PAGE_SIZE << compound_order(page);
  2339. }
  2340. s = page->slab;
  2341. #ifdef CONFIG_SLUB_DEBUG
  2342. /*
  2343. * Debugging requires use of the padding between object
  2344. * and whatever may come after it.
  2345. */
  2346. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  2347. return s->objsize;
  2348. #endif
  2349. /*
  2350. * If we have the need to store the freelist pointer
  2351. * back there or track user information then we can
  2352. * only use the space before that information.
  2353. */
  2354. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  2355. return s->inuse;
  2356. /*
  2357. * Else we can use all the padding etc for the allocation
  2358. */
  2359. return s->size;
  2360. }
  2361. void kfree(const void *x)
  2362. {
  2363. struct page *page;
  2364. void *object = (void *)x;
  2365. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2366. return;
  2367. page = virt_to_head_page(x);
  2368. if (unlikely(!PageSlab(page))) {
  2369. BUG_ON(!PageCompound(page));
  2370. put_page(page);
  2371. return;
  2372. }
  2373. slab_free(page->slab, page, object, _RET_IP_);
  2374. kmemtrace_mark_free(KMEMTRACE_TYPE_KMALLOC, _RET_IP_, x);
  2375. }
  2376. EXPORT_SYMBOL(kfree);
  2377. /*
  2378. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2379. * the remaining slabs by the number of items in use. The slabs with the
  2380. * most items in use come first. New allocations will then fill those up
  2381. * and thus they can be removed from the partial lists.
  2382. *
  2383. * The slabs with the least items are placed last. This results in them
  2384. * being allocated from last increasing the chance that the last objects
  2385. * are freed in them.
  2386. */
  2387. int kmem_cache_shrink(struct kmem_cache *s)
  2388. {
  2389. int node;
  2390. int i;
  2391. struct kmem_cache_node *n;
  2392. struct page *page;
  2393. struct page *t;
  2394. int objects = oo_objects(s->max);
  2395. struct list_head *slabs_by_inuse =
  2396. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2397. unsigned long flags;
  2398. if (!slabs_by_inuse)
  2399. return -ENOMEM;
  2400. flush_all(s);
  2401. for_each_node_state(node, N_NORMAL_MEMORY) {
  2402. n = get_node(s, node);
  2403. if (!n->nr_partial)
  2404. continue;
  2405. for (i = 0; i < objects; i++)
  2406. INIT_LIST_HEAD(slabs_by_inuse + i);
  2407. spin_lock_irqsave(&n->list_lock, flags);
  2408. /*
  2409. * Build lists indexed by the items in use in each slab.
  2410. *
  2411. * Note that concurrent frees may occur while we hold the
  2412. * list_lock. page->inuse here is the upper limit.
  2413. */
  2414. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2415. if (!page->inuse && slab_trylock(page)) {
  2416. /*
  2417. * Must hold slab lock here because slab_free
  2418. * may have freed the last object and be
  2419. * waiting to release the slab.
  2420. */
  2421. list_del(&page->lru);
  2422. n->nr_partial--;
  2423. slab_unlock(page);
  2424. discard_slab(s, page);
  2425. } else {
  2426. list_move(&page->lru,
  2427. slabs_by_inuse + page->inuse);
  2428. }
  2429. }
  2430. /*
  2431. * Rebuild the partial list with the slabs filled up most
  2432. * first and the least used slabs at the end.
  2433. */
  2434. for (i = objects - 1; i >= 0; i--)
  2435. list_splice(slabs_by_inuse + i, n->partial.prev);
  2436. spin_unlock_irqrestore(&n->list_lock, flags);
  2437. }
  2438. kfree(slabs_by_inuse);
  2439. return 0;
  2440. }
  2441. EXPORT_SYMBOL(kmem_cache_shrink);
  2442. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  2443. static int slab_mem_going_offline_callback(void *arg)
  2444. {
  2445. struct kmem_cache *s;
  2446. down_read(&slub_lock);
  2447. list_for_each_entry(s, &slab_caches, list)
  2448. kmem_cache_shrink(s);
  2449. up_read(&slub_lock);
  2450. return 0;
  2451. }
  2452. static void slab_mem_offline_callback(void *arg)
  2453. {
  2454. struct kmem_cache_node *n;
  2455. struct kmem_cache *s;
  2456. struct memory_notify *marg = arg;
  2457. int offline_node;
  2458. offline_node = marg->status_change_nid;
  2459. /*
  2460. * If the node still has available memory. we need kmem_cache_node
  2461. * for it yet.
  2462. */
  2463. if (offline_node < 0)
  2464. return;
  2465. down_read(&slub_lock);
  2466. list_for_each_entry(s, &slab_caches, list) {
  2467. n = get_node(s, offline_node);
  2468. if (n) {
  2469. /*
  2470. * if n->nr_slabs > 0, slabs still exist on the node
  2471. * that is going down. We were unable to free them,
  2472. * and offline_pages() function shoudn't call this
  2473. * callback. So, we must fail.
  2474. */
  2475. BUG_ON(slabs_node(s, offline_node));
  2476. s->node[offline_node] = NULL;
  2477. kmem_cache_free(kmalloc_caches, n);
  2478. }
  2479. }
  2480. up_read(&slub_lock);
  2481. }
  2482. static int slab_mem_going_online_callback(void *arg)
  2483. {
  2484. struct kmem_cache_node *n;
  2485. struct kmem_cache *s;
  2486. struct memory_notify *marg = arg;
  2487. int nid = marg->status_change_nid;
  2488. int ret = 0;
  2489. /*
  2490. * If the node's memory is already available, then kmem_cache_node is
  2491. * already created. Nothing to do.
  2492. */
  2493. if (nid < 0)
  2494. return 0;
  2495. /*
  2496. * We are bringing a node online. No memory is available yet. We must
  2497. * allocate a kmem_cache_node structure in order to bring the node
  2498. * online.
  2499. */
  2500. down_read(&slub_lock);
  2501. list_for_each_entry(s, &slab_caches, list) {
  2502. /*
  2503. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2504. * since memory is not yet available from the node that
  2505. * is brought up.
  2506. */
  2507. n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
  2508. if (!n) {
  2509. ret = -ENOMEM;
  2510. goto out;
  2511. }
  2512. init_kmem_cache_node(n, s);
  2513. s->node[nid] = n;
  2514. }
  2515. out:
  2516. up_read(&slub_lock);
  2517. return ret;
  2518. }
  2519. static int slab_memory_callback(struct notifier_block *self,
  2520. unsigned long action, void *arg)
  2521. {
  2522. int ret = 0;
  2523. switch (action) {
  2524. case MEM_GOING_ONLINE:
  2525. ret = slab_mem_going_online_callback(arg);
  2526. break;
  2527. case MEM_GOING_OFFLINE:
  2528. ret = slab_mem_going_offline_callback(arg);
  2529. break;
  2530. case MEM_OFFLINE:
  2531. case MEM_CANCEL_ONLINE:
  2532. slab_mem_offline_callback(arg);
  2533. break;
  2534. case MEM_ONLINE:
  2535. case MEM_CANCEL_OFFLINE:
  2536. break;
  2537. }
  2538. ret = notifier_from_errno(ret);
  2539. return ret;
  2540. }
  2541. #endif /* CONFIG_MEMORY_HOTPLUG */
  2542. /********************************************************************
  2543. * Basic setup of slabs
  2544. *******************************************************************/
  2545. void __init kmem_cache_init(void)
  2546. {
  2547. int i;
  2548. int caches = 0;
  2549. init_alloc_cpu();
  2550. #ifdef CONFIG_NUMA
  2551. /*
  2552. * Must first have the slab cache available for the allocations of the
  2553. * struct kmem_cache_node's. There is special bootstrap code in
  2554. * kmem_cache_open for slab_state == DOWN.
  2555. */
  2556. create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
  2557. sizeof(struct kmem_cache_node), GFP_KERNEL);
  2558. kmalloc_caches[0].refcount = -1;
  2559. caches++;
  2560. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2561. #endif
  2562. /* Able to allocate the per node structures */
  2563. slab_state = PARTIAL;
  2564. /* Caches that are not of the two-to-the-power-of size */
  2565. if (KMALLOC_MIN_SIZE <= 64) {
  2566. create_kmalloc_cache(&kmalloc_caches[1],
  2567. "kmalloc-96", 96, GFP_KERNEL);
  2568. caches++;
  2569. create_kmalloc_cache(&kmalloc_caches[2],
  2570. "kmalloc-192", 192, GFP_KERNEL);
  2571. caches++;
  2572. }
  2573. for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
  2574. create_kmalloc_cache(&kmalloc_caches[i],
  2575. "kmalloc", 1 << i, GFP_KERNEL);
  2576. caches++;
  2577. }
  2578. /*
  2579. * Patch up the size_index table if we have strange large alignment
  2580. * requirements for the kmalloc array. This is only the case for
  2581. * MIPS it seems. The standard arches will not generate any code here.
  2582. *
  2583. * Largest permitted alignment is 256 bytes due to the way we
  2584. * handle the index determination for the smaller caches.
  2585. *
  2586. * Make sure that nothing crazy happens if someone starts tinkering
  2587. * around with ARCH_KMALLOC_MINALIGN
  2588. */
  2589. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2590. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2591. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
  2592. size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
  2593. if (KMALLOC_MIN_SIZE == 128) {
  2594. /*
  2595. * The 192 byte sized cache is not used if the alignment
  2596. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  2597. * instead.
  2598. */
  2599. for (i = 128 + 8; i <= 192; i += 8)
  2600. size_index[(i - 1) / 8] = 8;
  2601. }
  2602. slab_state = UP;
  2603. /* Provide the correct kmalloc names now that the caches are up */
  2604. for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
  2605. kmalloc_caches[i]. name =
  2606. kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
  2607. #ifdef CONFIG_SMP
  2608. register_cpu_notifier(&slab_notifier);
  2609. kmem_size = offsetof(struct kmem_cache, cpu_slab) +
  2610. nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
  2611. #else
  2612. kmem_size = sizeof(struct kmem_cache);
  2613. #endif
  2614. printk(KERN_INFO
  2615. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2616. " CPUs=%d, Nodes=%d\n",
  2617. caches, cache_line_size(),
  2618. slub_min_order, slub_max_order, slub_min_objects,
  2619. nr_cpu_ids, nr_node_ids);
  2620. }
  2621. /*
  2622. * Find a mergeable slab cache
  2623. */
  2624. static int slab_unmergeable(struct kmem_cache *s)
  2625. {
  2626. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2627. return 1;
  2628. if (s->ctor)
  2629. return 1;
  2630. /*
  2631. * We may have set a slab to be unmergeable during bootstrap.
  2632. */
  2633. if (s->refcount < 0)
  2634. return 1;
  2635. return 0;
  2636. }
  2637. static struct kmem_cache *find_mergeable(size_t size,
  2638. size_t align, unsigned long flags, const char *name,
  2639. void (*ctor)(void *))
  2640. {
  2641. struct kmem_cache *s;
  2642. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2643. return NULL;
  2644. if (ctor)
  2645. return NULL;
  2646. size = ALIGN(size, sizeof(void *));
  2647. align = calculate_alignment(flags, align, size);
  2648. size = ALIGN(size, align);
  2649. flags = kmem_cache_flags(size, flags, name, NULL);
  2650. list_for_each_entry(s, &slab_caches, list) {
  2651. if (slab_unmergeable(s))
  2652. continue;
  2653. if (size > s->size)
  2654. continue;
  2655. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2656. continue;
  2657. /*
  2658. * Check if alignment is compatible.
  2659. * Courtesy of Adrian Drzewiecki
  2660. */
  2661. if ((s->size & ~(align - 1)) != s->size)
  2662. continue;
  2663. if (s->size - size >= sizeof(void *))
  2664. continue;
  2665. return s;
  2666. }
  2667. return NULL;
  2668. }
  2669. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2670. size_t align, unsigned long flags, void (*ctor)(void *))
  2671. {
  2672. struct kmem_cache *s;
  2673. down_write(&slub_lock);
  2674. s = find_mergeable(size, align, flags, name, ctor);
  2675. if (s) {
  2676. int cpu;
  2677. s->refcount++;
  2678. /*
  2679. * Adjust the object sizes so that we clear
  2680. * the complete object on kzalloc.
  2681. */
  2682. s->objsize = max(s->objsize, (int)size);
  2683. /*
  2684. * And then we need to update the object size in the
  2685. * per cpu structures
  2686. */
  2687. for_each_online_cpu(cpu)
  2688. get_cpu_slab(s, cpu)->objsize = s->objsize;
  2689. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2690. up_write(&slub_lock);
  2691. if (sysfs_slab_alias(s, name))
  2692. goto err;
  2693. return s;
  2694. }
  2695. s = kmalloc(kmem_size, GFP_KERNEL);
  2696. if (s) {
  2697. if (kmem_cache_open(s, GFP_KERNEL, name,
  2698. size, align, flags, ctor)) {
  2699. list_add(&s->list, &slab_caches);
  2700. up_write(&slub_lock);
  2701. if (sysfs_slab_add(s))
  2702. goto err;
  2703. return s;
  2704. }
  2705. kfree(s);
  2706. }
  2707. up_write(&slub_lock);
  2708. err:
  2709. if (flags & SLAB_PANIC)
  2710. panic("Cannot create slabcache %s\n", name);
  2711. else
  2712. s = NULL;
  2713. return s;
  2714. }
  2715. EXPORT_SYMBOL(kmem_cache_create);
  2716. #ifdef CONFIG_SMP
  2717. /*
  2718. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2719. * necessary.
  2720. */
  2721. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2722. unsigned long action, void *hcpu)
  2723. {
  2724. long cpu = (long)hcpu;
  2725. struct kmem_cache *s;
  2726. unsigned long flags;
  2727. switch (action) {
  2728. case CPU_UP_PREPARE:
  2729. case CPU_UP_PREPARE_FROZEN:
  2730. init_alloc_cpu_cpu(cpu);
  2731. down_read(&slub_lock);
  2732. list_for_each_entry(s, &slab_caches, list)
  2733. s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
  2734. GFP_KERNEL);
  2735. up_read(&slub_lock);
  2736. break;
  2737. case CPU_UP_CANCELED:
  2738. case CPU_UP_CANCELED_FROZEN:
  2739. case CPU_DEAD:
  2740. case CPU_DEAD_FROZEN:
  2741. down_read(&slub_lock);
  2742. list_for_each_entry(s, &slab_caches, list) {
  2743. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2744. local_irq_save(flags);
  2745. __flush_cpu_slab(s, cpu);
  2746. local_irq_restore(flags);
  2747. free_kmem_cache_cpu(c, cpu);
  2748. s->cpu_slab[cpu] = NULL;
  2749. }
  2750. up_read(&slub_lock);
  2751. break;
  2752. default:
  2753. break;
  2754. }
  2755. return NOTIFY_OK;
  2756. }
  2757. static struct notifier_block __cpuinitdata slab_notifier = {
  2758. .notifier_call = slab_cpuup_callback
  2759. };
  2760. #endif
  2761. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  2762. {
  2763. struct kmem_cache *s;
  2764. void *ret;
  2765. if (unlikely(size > PAGE_SIZE))
  2766. return kmalloc_large(size, gfpflags);
  2767. s = get_slab(size, gfpflags);
  2768. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2769. return s;
  2770. ret = slab_alloc(s, gfpflags, -1, caller);
  2771. /* Honor the call site pointer we recieved. */
  2772. kmemtrace_mark_alloc(KMEMTRACE_TYPE_KMALLOC, caller, ret, size,
  2773. s->size, gfpflags);
  2774. return ret;
  2775. }
  2776. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2777. int node, unsigned long caller)
  2778. {
  2779. struct kmem_cache *s;
  2780. void *ret;
  2781. if (unlikely(size > PAGE_SIZE))
  2782. return kmalloc_large_node(size, gfpflags, node);
  2783. s = get_slab(size, gfpflags);
  2784. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2785. return s;
  2786. ret = slab_alloc(s, gfpflags, node, caller);
  2787. /* Honor the call site pointer we recieved. */
  2788. kmemtrace_mark_alloc_node(KMEMTRACE_TYPE_KMALLOC, caller, ret,
  2789. size, s->size, gfpflags, node);
  2790. return ret;
  2791. }
  2792. #ifdef CONFIG_SLUB_DEBUG
  2793. static unsigned long count_partial(struct kmem_cache_node *n,
  2794. int (*get_count)(struct page *))
  2795. {
  2796. unsigned long flags;
  2797. unsigned long x = 0;
  2798. struct page *page;
  2799. spin_lock_irqsave(&n->list_lock, flags);
  2800. list_for_each_entry(page, &n->partial, lru)
  2801. x += get_count(page);
  2802. spin_unlock_irqrestore(&n->list_lock, flags);
  2803. return x;
  2804. }
  2805. static int count_inuse(struct page *page)
  2806. {
  2807. return page->inuse;
  2808. }
  2809. static int count_total(struct page *page)
  2810. {
  2811. return page->objects;
  2812. }
  2813. static int count_free(struct page *page)
  2814. {
  2815. return page->objects - page->inuse;
  2816. }
  2817. static int validate_slab(struct kmem_cache *s, struct page *page,
  2818. unsigned long *map)
  2819. {
  2820. void *p;
  2821. void *addr = page_address(page);
  2822. if (!check_slab(s, page) ||
  2823. !on_freelist(s, page, NULL))
  2824. return 0;
  2825. /* Now we know that a valid freelist exists */
  2826. bitmap_zero(map, page->objects);
  2827. for_each_free_object(p, s, page->freelist) {
  2828. set_bit(slab_index(p, s, addr), map);
  2829. if (!check_object(s, page, p, 0))
  2830. return 0;
  2831. }
  2832. for_each_object(p, s, addr, page->objects)
  2833. if (!test_bit(slab_index(p, s, addr), map))
  2834. if (!check_object(s, page, p, 1))
  2835. return 0;
  2836. return 1;
  2837. }
  2838. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  2839. unsigned long *map)
  2840. {
  2841. if (slab_trylock(page)) {
  2842. validate_slab(s, page, map);
  2843. slab_unlock(page);
  2844. } else
  2845. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  2846. s->name, page);
  2847. if (s->flags & DEBUG_DEFAULT_FLAGS) {
  2848. if (!PageSlubDebug(page))
  2849. printk(KERN_ERR "SLUB %s: SlubDebug not set "
  2850. "on slab 0x%p\n", s->name, page);
  2851. } else {
  2852. if (PageSlubDebug(page))
  2853. printk(KERN_ERR "SLUB %s: SlubDebug set on "
  2854. "slab 0x%p\n", s->name, page);
  2855. }
  2856. }
  2857. static int validate_slab_node(struct kmem_cache *s,
  2858. struct kmem_cache_node *n, unsigned long *map)
  2859. {
  2860. unsigned long count = 0;
  2861. struct page *page;
  2862. unsigned long flags;
  2863. spin_lock_irqsave(&n->list_lock, flags);
  2864. list_for_each_entry(page, &n->partial, lru) {
  2865. validate_slab_slab(s, page, map);
  2866. count++;
  2867. }
  2868. if (count != n->nr_partial)
  2869. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  2870. "counter=%ld\n", s->name, count, n->nr_partial);
  2871. if (!(s->flags & SLAB_STORE_USER))
  2872. goto out;
  2873. list_for_each_entry(page, &n->full, lru) {
  2874. validate_slab_slab(s, page, map);
  2875. count++;
  2876. }
  2877. if (count != atomic_long_read(&n->nr_slabs))
  2878. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  2879. "counter=%ld\n", s->name, count,
  2880. atomic_long_read(&n->nr_slabs));
  2881. out:
  2882. spin_unlock_irqrestore(&n->list_lock, flags);
  2883. return count;
  2884. }
  2885. static long validate_slab_cache(struct kmem_cache *s)
  2886. {
  2887. int node;
  2888. unsigned long count = 0;
  2889. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  2890. sizeof(unsigned long), GFP_KERNEL);
  2891. if (!map)
  2892. return -ENOMEM;
  2893. flush_all(s);
  2894. for_each_node_state(node, N_NORMAL_MEMORY) {
  2895. struct kmem_cache_node *n = get_node(s, node);
  2896. count += validate_slab_node(s, n, map);
  2897. }
  2898. kfree(map);
  2899. return count;
  2900. }
  2901. #ifdef SLUB_RESILIENCY_TEST
  2902. static void resiliency_test(void)
  2903. {
  2904. u8 *p;
  2905. printk(KERN_ERR "SLUB resiliency testing\n");
  2906. printk(KERN_ERR "-----------------------\n");
  2907. printk(KERN_ERR "A. Corruption after allocation\n");
  2908. p = kzalloc(16, GFP_KERNEL);
  2909. p[16] = 0x12;
  2910. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  2911. " 0x12->0x%p\n\n", p + 16);
  2912. validate_slab_cache(kmalloc_caches + 4);
  2913. /* Hmmm... The next two are dangerous */
  2914. p = kzalloc(32, GFP_KERNEL);
  2915. p[32 + sizeof(void *)] = 0x34;
  2916. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  2917. " 0x34 -> -0x%p\n", p);
  2918. printk(KERN_ERR
  2919. "If allocated object is overwritten then not detectable\n\n");
  2920. validate_slab_cache(kmalloc_caches + 5);
  2921. p = kzalloc(64, GFP_KERNEL);
  2922. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  2923. *p = 0x56;
  2924. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  2925. p);
  2926. printk(KERN_ERR
  2927. "If allocated object is overwritten then not detectable\n\n");
  2928. validate_slab_cache(kmalloc_caches + 6);
  2929. printk(KERN_ERR "\nB. Corruption after free\n");
  2930. p = kzalloc(128, GFP_KERNEL);
  2931. kfree(p);
  2932. *p = 0x78;
  2933. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  2934. validate_slab_cache(kmalloc_caches + 7);
  2935. p = kzalloc(256, GFP_KERNEL);
  2936. kfree(p);
  2937. p[50] = 0x9a;
  2938. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  2939. p);
  2940. validate_slab_cache(kmalloc_caches + 8);
  2941. p = kzalloc(512, GFP_KERNEL);
  2942. kfree(p);
  2943. p[512] = 0xab;
  2944. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  2945. validate_slab_cache(kmalloc_caches + 9);
  2946. }
  2947. #else
  2948. static void resiliency_test(void) {};
  2949. #endif
  2950. /*
  2951. * Generate lists of code addresses where slabcache objects are allocated
  2952. * and freed.
  2953. */
  2954. struct location {
  2955. unsigned long count;
  2956. unsigned long addr;
  2957. long long sum_time;
  2958. long min_time;
  2959. long max_time;
  2960. long min_pid;
  2961. long max_pid;
  2962. cpumask_t cpus;
  2963. nodemask_t nodes;
  2964. };
  2965. struct loc_track {
  2966. unsigned long max;
  2967. unsigned long count;
  2968. struct location *loc;
  2969. };
  2970. static void free_loc_track(struct loc_track *t)
  2971. {
  2972. if (t->max)
  2973. free_pages((unsigned long)t->loc,
  2974. get_order(sizeof(struct location) * t->max));
  2975. }
  2976. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  2977. {
  2978. struct location *l;
  2979. int order;
  2980. order = get_order(sizeof(struct location) * max);
  2981. l = (void *)__get_free_pages(flags, order);
  2982. if (!l)
  2983. return 0;
  2984. if (t->count) {
  2985. memcpy(l, t->loc, sizeof(struct location) * t->count);
  2986. free_loc_track(t);
  2987. }
  2988. t->max = max;
  2989. t->loc = l;
  2990. return 1;
  2991. }
  2992. static int add_location(struct loc_track *t, struct kmem_cache *s,
  2993. const struct track *track)
  2994. {
  2995. long start, end, pos;
  2996. struct location *l;
  2997. unsigned long caddr;
  2998. unsigned long age = jiffies - track->when;
  2999. start = -1;
  3000. end = t->count;
  3001. for ( ; ; ) {
  3002. pos = start + (end - start + 1) / 2;
  3003. /*
  3004. * There is nothing at "end". If we end up there
  3005. * we need to add something to before end.
  3006. */
  3007. if (pos == end)
  3008. break;
  3009. caddr = t->loc[pos].addr;
  3010. if (track->addr == caddr) {
  3011. l = &t->loc[pos];
  3012. l->count++;
  3013. if (track->when) {
  3014. l->sum_time += age;
  3015. if (age < l->min_time)
  3016. l->min_time = age;
  3017. if (age > l->max_time)
  3018. l->max_time = age;
  3019. if (track->pid < l->min_pid)
  3020. l->min_pid = track->pid;
  3021. if (track->pid > l->max_pid)
  3022. l->max_pid = track->pid;
  3023. cpu_set(track->cpu, l->cpus);
  3024. }
  3025. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3026. return 1;
  3027. }
  3028. if (track->addr < caddr)
  3029. end = pos;
  3030. else
  3031. start = pos;
  3032. }
  3033. /*
  3034. * Not found. Insert new tracking element.
  3035. */
  3036. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3037. return 0;
  3038. l = t->loc + pos;
  3039. if (pos < t->count)
  3040. memmove(l + 1, l,
  3041. (t->count - pos) * sizeof(struct location));
  3042. t->count++;
  3043. l->count = 1;
  3044. l->addr = track->addr;
  3045. l->sum_time = age;
  3046. l->min_time = age;
  3047. l->max_time = age;
  3048. l->min_pid = track->pid;
  3049. l->max_pid = track->pid;
  3050. cpus_clear(l->cpus);
  3051. cpu_set(track->cpu, l->cpus);
  3052. nodes_clear(l->nodes);
  3053. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3054. return 1;
  3055. }
  3056. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3057. struct page *page, enum track_item alloc)
  3058. {
  3059. void *addr = page_address(page);
  3060. DECLARE_BITMAP(map, page->objects);
  3061. void *p;
  3062. bitmap_zero(map, page->objects);
  3063. for_each_free_object(p, s, page->freelist)
  3064. set_bit(slab_index(p, s, addr), map);
  3065. for_each_object(p, s, addr, page->objects)
  3066. if (!test_bit(slab_index(p, s, addr), map))
  3067. add_location(t, s, get_track(s, p, alloc));
  3068. }
  3069. static int list_locations(struct kmem_cache *s, char *buf,
  3070. enum track_item alloc)
  3071. {
  3072. int len = 0;
  3073. unsigned long i;
  3074. struct loc_track t = { 0, 0, NULL };
  3075. int node;
  3076. if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3077. GFP_TEMPORARY))
  3078. return sprintf(buf, "Out of memory\n");
  3079. /* Push back cpu slabs */
  3080. flush_all(s);
  3081. for_each_node_state(node, N_NORMAL_MEMORY) {
  3082. struct kmem_cache_node *n = get_node(s, node);
  3083. unsigned long flags;
  3084. struct page *page;
  3085. if (!atomic_long_read(&n->nr_slabs))
  3086. continue;
  3087. spin_lock_irqsave(&n->list_lock, flags);
  3088. list_for_each_entry(page, &n->partial, lru)
  3089. process_slab(&t, s, page, alloc);
  3090. list_for_each_entry(page, &n->full, lru)
  3091. process_slab(&t, s, page, alloc);
  3092. spin_unlock_irqrestore(&n->list_lock, flags);
  3093. }
  3094. for (i = 0; i < t.count; i++) {
  3095. struct location *l = &t.loc[i];
  3096. if (len > PAGE_SIZE - 100)
  3097. break;
  3098. len += sprintf(buf + len, "%7ld ", l->count);
  3099. if (l->addr)
  3100. len += sprint_symbol(buf + len, (unsigned long)l->addr);
  3101. else
  3102. len += sprintf(buf + len, "<not-available>");
  3103. if (l->sum_time != l->min_time) {
  3104. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3105. l->min_time,
  3106. (long)div_u64(l->sum_time, l->count),
  3107. l->max_time);
  3108. } else
  3109. len += sprintf(buf + len, " age=%ld",
  3110. l->min_time);
  3111. if (l->min_pid != l->max_pid)
  3112. len += sprintf(buf + len, " pid=%ld-%ld",
  3113. l->min_pid, l->max_pid);
  3114. else
  3115. len += sprintf(buf + len, " pid=%ld",
  3116. l->min_pid);
  3117. if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
  3118. len < PAGE_SIZE - 60) {
  3119. len += sprintf(buf + len, " cpus=");
  3120. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3121. l->cpus);
  3122. }
  3123. if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
  3124. len < PAGE_SIZE - 60) {
  3125. len += sprintf(buf + len, " nodes=");
  3126. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3127. l->nodes);
  3128. }
  3129. len += sprintf(buf + len, "\n");
  3130. }
  3131. free_loc_track(&t);
  3132. if (!t.count)
  3133. len += sprintf(buf, "No data\n");
  3134. return len;
  3135. }
  3136. enum slab_stat_type {
  3137. SL_ALL, /* All slabs */
  3138. SL_PARTIAL, /* Only partially allocated slabs */
  3139. SL_CPU, /* Only slabs used for cpu caches */
  3140. SL_OBJECTS, /* Determine allocated objects not slabs */
  3141. SL_TOTAL /* Determine object capacity not slabs */
  3142. };
  3143. #define SO_ALL (1 << SL_ALL)
  3144. #define SO_PARTIAL (1 << SL_PARTIAL)
  3145. #define SO_CPU (1 << SL_CPU)
  3146. #define SO_OBJECTS (1 << SL_OBJECTS)
  3147. #define SO_TOTAL (1 << SL_TOTAL)
  3148. static ssize_t show_slab_objects(struct kmem_cache *s,
  3149. char *buf, unsigned long flags)
  3150. {
  3151. unsigned long total = 0;
  3152. int node;
  3153. int x;
  3154. unsigned long *nodes;
  3155. unsigned long *per_cpu;
  3156. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3157. if (!nodes)
  3158. return -ENOMEM;
  3159. per_cpu = nodes + nr_node_ids;
  3160. if (flags & SO_CPU) {
  3161. int cpu;
  3162. for_each_possible_cpu(cpu) {
  3163. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  3164. if (!c || c->node < 0)
  3165. continue;
  3166. if (c->page) {
  3167. if (flags & SO_TOTAL)
  3168. x = c->page->objects;
  3169. else if (flags & SO_OBJECTS)
  3170. x = c->page->inuse;
  3171. else
  3172. x = 1;
  3173. total += x;
  3174. nodes[c->node] += x;
  3175. }
  3176. per_cpu[c->node]++;
  3177. }
  3178. }
  3179. if (flags & SO_ALL) {
  3180. for_each_node_state(node, N_NORMAL_MEMORY) {
  3181. struct kmem_cache_node *n = get_node(s, node);
  3182. if (flags & SO_TOTAL)
  3183. x = atomic_long_read(&n->total_objects);
  3184. else if (flags & SO_OBJECTS)
  3185. x = atomic_long_read(&n->total_objects) -
  3186. count_partial(n, count_free);
  3187. else
  3188. x = atomic_long_read(&n->nr_slabs);
  3189. total += x;
  3190. nodes[node] += x;
  3191. }
  3192. } else if (flags & SO_PARTIAL) {
  3193. for_each_node_state(node, N_NORMAL_MEMORY) {
  3194. struct kmem_cache_node *n = get_node(s, node);
  3195. if (flags & SO_TOTAL)
  3196. x = count_partial(n, count_total);
  3197. else if (flags & SO_OBJECTS)
  3198. x = count_partial(n, count_inuse);
  3199. else
  3200. x = n->nr_partial;
  3201. total += x;
  3202. nodes[node] += x;
  3203. }
  3204. }
  3205. x = sprintf(buf, "%lu", total);
  3206. #ifdef CONFIG_NUMA
  3207. for_each_node_state(node, N_NORMAL_MEMORY)
  3208. if (nodes[node])
  3209. x += sprintf(buf + x, " N%d=%lu",
  3210. node, nodes[node]);
  3211. #endif
  3212. kfree(nodes);
  3213. return x + sprintf(buf + x, "\n");
  3214. }
  3215. static int any_slab_objects(struct kmem_cache *s)
  3216. {
  3217. int node;
  3218. for_each_online_node(node) {
  3219. struct kmem_cache_node *n = get_node(s, node);
  3220. if (!n)
  3221. continue;
  3222. if (atomic_long_read(&n->total_objects))
  3223. return 1;
  3224. }
  3225. return 0;
  3226. }
  3227. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3228. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3229. struct slab_attribute {
  3230. struct attribute attr;
  3231. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3232. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3233. };
  3234. #define SLAB_ATTR_RO(_name) \
  3235. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3236. #define SLAB_ATTR(_name) \
  3237. static struct slab_attribute _name##_attr = \
  3238. __ATTR(_name, 0644, _name##_show, _name##_store)
  3239. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3240. {
  3241. return sprintf(buf, "%d\n", s->size);
  3242. }
  3243. SLAB_ATTR_RO(slab_size);
  3244. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3245. {
  3246. return sprintf(buf, "%d\n", s->align);
  3247. }
  3248. SLAB_ATTR_RO(align);
  3249. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3250. {
  3251. return sprintf(buf, "%d\n", s->objsize);
  3252. }
  3253. SLAB_ATTR_RO(object_size);
  3254. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3255. {
  3256. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3257. }
  3258. SLAB_ATTR_RO(objs_per_slab);
  3259. static ssize_t order_store(struct kmem_cache *s,
  3260. const char *buf, size_t length)
  3261. {
  3262. unsigned long order;
  3263. int err;
  3264. err = strict_strtoul(buf, 10, &order);
  3265. if (err)
  3266. return err;
  3267. if (order > slub_max_order || order < slub_min_order)
  3268. return -EINVAL;
  3269. calculate_sizes(s, order);
  3270. return length;
  3271. }
  3272. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3273. {
  3274. return sprintf(buf, "%d\n", oo_order(s->oo));
  3275. }
  3276. SLAB_ATTR(order);
  3277. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3278. {
  3279. if (s->ctor) {
  3280. int n = sprint_symbol(buf, (unsigned long)s->ctor);
  3281. return n + sprintf(buf + n, "\n");
  3282. }
  3283. return 0;
  3284. }
  3285. SLAB_ATTR_RO(ctor);
  3286. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3287. {
  3288. return sprintf(buf, "%d\n", s->refcount - 1);
  3289. }
  3290. SLAB_ATTR_RO(aliases);
  3291. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3292. {
  3293. return show_slab_objects(s, buf, SO_ALL);
  3294. }
  3295. SLAB_ATTR_RO(slabs);
  3296. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3297. {
  3298. return show_slab_objects(s, buf, SO_PARTIAL);
  3299. }
  3300. SLAB_ATTR_RO(partial);
  3301. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3302. {
  3303. return show_slab_objects(s, buf, SO_CPU);
  3304. }
  3305. SLAB_ATTR_RO(cpu_slabs);
  3306. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3307. {
  3308. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3309. }
  3310. SLAB_ATTR_RO(objects);
  3311. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3312. {
  3313. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3314. }
  3315. SLAB_ATTR_RO(objects_partial);
  3316. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3317. {
  3318. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3319. }
  3320. SLAB_ATTR_RO(total_objects);
  3321. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3322. {
  3323. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3324. }
  3325. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3326. const char *buf, size_t length)
  3327. {
  3328. s->flags &= ~SLAB_DEBUG_FREE;
  3329. if (buf[0] == '1')
  3330. s->flags |= SLAB_DEBUG_FREE;
  3331. return length;
  3332. }
  3333. SLAB_ATTR(sanity_checks);
  3334. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3335. {
  3336. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3337. }
  3338. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3339. size_t length)
  3340. {
  3341. s->flags &= ~SLAB_TRACE;
  3342. if (buf[0] == '1')
  3343. s->flags |= SLAB_TRACE;
  3344. return length;
  3345. }
  3346. SLAB_ATTR(trace);
  3347. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3348. {
  3349. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3350. }
  3351. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3352. const char *buf, size_t length)
  3353. {
  3354. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3355. if (buf[0] == '1')
  3356. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3357. return length;
  3358. }
  3359. SLAB_ATTR(reclaim_account);
  3360. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3361. {
  3362. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3363. }
  3364. SLAB_ATTR_RO(hwcache_align);
  3365. #ifdef CONFIG_ZONE_DMA
  3366. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3367. {
  3368. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3369. }
  3370. SLAB_ATTR_RO(cache_dma);
  3371. #endif
  3372. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3373. {
  3374. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3375. }
  3376. SLAB_ATTR_RO(destroy_by_rcu);
  3377. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3378. {
  3379. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3380. }
  3381. static ssize_t red_zone_store(struct kmem_cache *s,
  3382. const char *buf, size_t length)
  3383. {
  3384. if (any_slab_objects(s))
  3385. return -EBUSY;
  3386. s->flags &= ~SLAB_RED_ZONE;
  3387. if (buf[0] == '1')
  3388. s->flags |= SLAB_RED_ZONE;
  3389. calculate_sizes(s, -1);
  3390. return length;
  3391. }
  3392. SLAB_ATTR(red_zone);
  3393. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3394. {
  3395. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3396. }
  3397. static ssize_t poison_store(struct kmem_cache *s,
  3398. const char *buf, size_t length)
  3399. {
  3400. if (any_slab_objects(s))
  3401. return -EBUSY;
  3402. s->flags &= ~SLAB_POISON;
  3403. if (buf[0] == '1')
  3404. s->flags |= SLAB_POISON;
  3405. calculate_sizes(s, -1);
  3406. return length;
  3407. }
  3408. SLAB_ATTR(poison);
  3409. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3410. {
  3411. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3412. }
  3413. static ssize_t store_user_store(struct kmem_cache *s,
  3414. const char *buf, size_t length)
  3415. {
  3416. if (any_slab_objects(s))
  3417. return -EBUSY;
  3418. s->flags &= ~SLAB_STORE_USER;
  3419. if (buf[0] == '1')
  3420. s->flags |= SLAB_STORE_USER;
  3421. calculate_sizes(s, -1);
  3422. return length;
  3423. }
  3424. SLAB_ATTR(store_user);
  3425. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3426. {
  3427. return 0;
  3428. }
  3429. static ssize_t validate_store(struct kmem_cache *s,
  3430. const char *buf, size_t length)
  3431. {
  3432. int ret = -EINVAL;
  3433. if (buf[0] == '1') {
  3434. ret = validate_slab_cache(s);
  3435. if (ret >= 0)
  3436. ret = length;
  3437. }
  3438. return ret;
  3439. }
  3440. SLAB_ATTR(validate);
  3441. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3442. {
  3443. return 0;
  3444. }
  3445. static ssize_t shrink_store(struct kmem_cache *s,
  3446. const char *buf, size_t length)
  3447. {
  3448. if (buf[0] == '1') {
  3449. int rc = kmem_cache_shrink(s);
  3450. if (rc)
  3451. return rc;
  3452. } else
  3453. return -EINVAL;
  3454. return length;
  3455. }
  3456. SLAB_ATTR(shrink);
  3457. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3458. {
  3459. if (!(s->flags & SLAB_STORE_USER))
  3460. return -ENOSYS;
  3461. return list_locations(s, buf, TRACK_ALLOC);
  3462. }
  3463. SLAB_ATTR_RO(alloc_calls);
  3464. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3465. {
  3466. if (!(s->flags & SLAB_STORE_USER))
  3467. return -ENOSYS;
  3468. return list_locations(s, buf, TRACK_FREE);
  3469. }
  3470. SLAB_ATTR_RO(free_calls);
  3471. #ifdef CONFIG_NUMA
  3472. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3473. {
  3474. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3475. }
  3476. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3477. const char *buf, size_t length)
  3478. {
  3479. unsigned long ratio;
  3480. int err;
  3481. err = strict_strtoul(buf, 10, &ratio);
  3482. if (err)
  3483. return err;
  3484. if (ratio <= 100)
  3485. s->remote_node_defrag_ratio = ratio * 10;
  3486. return length;
  3487. }
  3488. SLAB_ATTR(remote_node_defrag_ratio);
  3489. #endif
  3490. #ifdef CONFIG_SLUB_STATS
  3491. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  3492. {
  3493. unsigned long sum = 0;
  3494. int cpu;
  3495. int len;
  3496. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  3497. if (!data)
  3498. return -ENOMEM;
  3499. for_each_online_cpu(cpu) {
  3500. unsigned x = get_cpu_slab(s, cpu)->stat[si];
  3501. data[cpu] = x;
  3502. sum += x;
  3503. }
  3504. len = sprintf(buf, "%lu", sum);
  3505. #ifdef CONFIG_SMP
  3506. for_each_online_cpu(cpu) {
  3507. if (data[cpu] && len < PAGE_SIZE - 20)
  3508. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  3509. }
  3510. #endif
  3511. kfree(data);
  3512. return len + sprintf(buf + len, "\n");
  3513. }
  3514. #define STAT_ATTR(si, text) \
  3515. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  3516. { \
  3517. return show_stat(s, buf, si); \
  3518. } \
  3519. SLAB_ATTR_RO(text); \
  3520. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  3521. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  3522. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  3523. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  3524. STAT_ATTR(FREE_FROZEN, free_frozen);
  3525. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  3526. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  3527. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  3528. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  3529. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  3530. STAT_ATTR(FREE_SLAB, free_slab);
  3531. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  3532. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  3533. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  3534. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  3535. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  3536. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  3537. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  3538. #endif
  3539. static struct attribute *slab_attrs[] = {
  3540. &slab_size_attr.attr,
  3541. &object_size_attr.attr,
  3542. &objs_per_slab_attr.attr,
  3543. &order_attr.attr,
  3544. &objects_attr.attr,
  3545. &objects_partial_attr.attr,
  3546. &total_objects_attr.attr,
  3547. &slabs_attr.attr,
  3548. &partial_attr.attr,
  3549. &cpu_slabs_attr.attr,
  3550. &ctor_attr.attr,
  3551. &aliases_attr.attr,
  3552. &align_attr.attr,
  3553. &sanity_checks_attr.attr,
  3554. &trace_attr.attr,
  3555. &hwcache_align_attr.attr,
  3556. &reclaim_account_attr.attr,
  3557. &destroy_by_rcu_attr.attr,
  3558. &red_zone_attr.attr,
  3559. &poison_attr.attr,
  3560. &store_user_attr.attr,
  3561. &validate_attr.attr,
  3562. &shrink_attr.attr,
  3563. &alloc_calls_attr.attr,
  3564. &free_calls_attr.attr,
  3565. #ifdef CONFIG_ZONE_DMA
  3566. &cache_dma_attr.attr,
  3567. #endif
  3568. #ifdef CONFIG_NUMA
  3569. &remote_node_defrag_ratio_attr.attr,
  3570. #endif
  3571. #ifdef CONFIG_SLUB_STATS
  3572. &alloc_fastpath_attr.attr,
  3573. &alloc_slowpath_attr.attr,
  3574. &free_fastpath_attr.attr,
  3575. &free_slowpath_attr.attr,
  3576. &free_frozen_attr.attr,
  3577. &free_add_partial_attr.attr,
  3578. &free_remove_partial_attr.attr,
  3579. &alloc_from_partial_attr.attr,
  3580. &alloc_slab_attr.attr,
  3581. &alloc_refill_attr.attr,
  3582. &free_slab_attr.attr,
  3583. &cpuslab_flush_attr.attr,
  3584. &deactivate_full_attr.attr,
  3585. &deactivate_empty_attr.attr,
  3586. &deactivate_to_head_attr.attr,
  3587. &deactivate_to_tail_attr.attr,
  3588. &deactivate_remote_frees_attr.attr,
  3589. &order_fallback_attr.attr,
  3590. #endif
  3591. NULL
  3592. };
  3593. static struct attribute_group slab_attr_group = {
  3594. .attrs = slab_attrs,
  3595. };
  3596. static ssize_t slab_attr_show(struct kobject *kobj,
  3597. struct attribute *attr,
  3598. char *buf)
  3599. {
  3600. struct slab_attribute *attribute;
  3601. struct kmem_cache *s;
  3602. int err;
  3603. attribute = to_slab_attr(attr);
  3604. s = to_slab(kobj);
  3605. if (!attribute->show)
  3606. return -EIO;
  3607. err = attribute->show(s, buf);
  3608. return err;
  3609. }
  3610. static ssize_t slab_attr_store(struct kobject *kobj,
  3611. struct attribute *attr,
  3612. const char *buf, size_t len)
  3613. {
  3614. struct slab_attribute *attribute;
  3615. struct kmem_cache *s;
  3616. int err;
  3617. attribute = to_slab_attr(attr);
  3618. s = to_slab(kobj);
  3619. if (!attribute->store)
  3620. return -EIO;
  3621. err = attribute->store(s, buf, len);
  3622. return err;
  3623. }
  3624. static void kmem_cache_release(struct kobject *kobj)
  3625. {
  3626. struct kmem_cache *s = to_slab(kobj);
  3627. kfree(s);
  3628. }
  3629. static struct sysfs_ops slab_sysfs_ops = {
  3630. .show = slab_attr_show,
  3631. .store = slab_attr_store,
  3632. };
  3633. static struct kobj_type slab_ktype = {
  3634. .sysfs_ops = &slab_sysfs_ops,
  3635. .release = kmem_cache_release
  3636. };
  3637. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3638. {
  3639. struct kobj_type *ktype = get_ktype(kobj);
  3640. if (ktype == &slab_ktype)
  3641. return 1;
  3642. return 0;
  3643. }
  3644. static struct kset_uevent_ops slab_uevent_ops = {
  3645. .filter = uevent_filter,
  3646. };
  3647. static struct kset *slab_kset;
  3648. #define ID_STR_LENGTH 64
  3649. /* Create a unique string id for a slab cache:
  3650. *
  3651. * Format :[flags-]size
  3652. */
  3653. static char *create_unique_id(struct kmem_cache *s)
  3654. {
  3655. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3656. char *p = name;
  3657. BUG_ON(!name);
  3658. *p++ = ':';
  3659. /*
  3660. * First flags affecting slabcache operations. We will only
  3661. * get here for aliasable slabs so we do not need to support
  3662. * too many flags. The flags here must cover all flags that
  3663. * are matched during merging to guarantee that the id is
  3664. * unique.
  3665. */
  3666. if (s->flags & SLAB_CACHE_DMA)
  3667. *p++ = 'd';
  3668. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3669. *p++ = 'a';
  3670. if (s->flags & SLAB_DEBUG_FREE)
  3671. *p++ = 'F';
  3672. if (p != name + 1)
  3673. *p++ = '-';
  3674. p += sprintf(p, "%07d", s->size);
  3675. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3676. return name;
  3677. }
  3678. static int sysfs_slab_add(struct kmem_cache *s)
  3679. {
  3680. int err;
  3681. const char *name;
  3682. int unmergeable;
  3683. if (slab_state < SYSFS)
  3684. /* Defer until later */
  3685. return 0;
  3686. unmergeable = slab_unmergeable(s);
  3687. if (unmergeable) {
  3688. /*
  3689. * Slabcache can never be merged so we can use the name proper.
  3690. * This is typically the case for debug situations. In that
  3691. * case we can catch duplicate names easily.
  3692. */
  3693. sysfs_remove_link(&slab_kset->kobj, s->name);
  3694. name = s->name;
  3695. } else {
  3696. /*
  3697. * Create a unique name for the slab as a target
  3698. * for the symlinks.
  3699. */
  3700. name = create_unique_id(s);
  3701. }
  3702. s->kobj.kset = slab_kset;
  3703. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  3704. if (err) {
  3705. kobject_put(&s->kobj);
  3706. return err;
  3707. }
  3708. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3709. if (err)
  3710. return err;
  3711. kobject_uevent(&s->kobj, KOBJ_ADD);
  3712. if (!unmergeable) {
  3713. /* Setup first alias */
  3714. sysfs_slab_alias(s, s->name);
  3715. kfree(name);
  3716. }
  3717. return 0;
  3718. }
  3719. static void sysfs_slab_remove(struct kmem_cache *s)
  3720. {
  3721. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3722. kobject_del(&s->kobj);
  3723. kobject_put(&s->kobj);
  3724. }
  3725. /*
  3726. * Need to buffer aliases during bootup until sysfs becomes
  3727. * available lest we loose that information.
  3728. */
  3729. struct saved_alias {
  3730. struct kmem_cache *s;
  3731. const char *name;
  3732. struct saved_alias *next;
  3733. };
  3734. static struct saved_alias *alias_list;
  3735. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  3736. {
  3737. struct saved_alias *al;
  3738. if (slab_state == SYSFS) {
  3739. /*
  3740. * If we have a leftover link then remove it.
  3741. */
  3742. sysfs_remove_link(&slab_kset->kobj, name);
  3743. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  3744. }
  3745. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  3746. if (!al)
  3747. return -ENOMEM;
  3748. al->s = s;
  3749. al->name = name;
  3750. al->next = alias_list;
  3751. alias_list = al;
  3752. return 0;
  3753. }
  3754. static int __init slab_sysfs_init(void)
  3755. {
  3756. struct kmem_cache *s;
  3757. int err;
  3758. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  3759. if (!slab_kset) {
  3760. printk(KERN_ERR "Cannot register slab subsystem.\n");
  3761. return -ENOSYS;
  3762. }
  3763. slab_state = SYSFS;
  3764. list_for_each_entry(s, &slab_caches, list) {
  3765. err = sysfs_slab_add(s);
  3766. if (err)
  3767. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  3768. " to sysfs\n", s->name);
  3769. }
  3770. while (alias_list) {
  3771. struct saved_alias *al = alias_list;
  3772. alias_list = alias_list->next;
  3773. err = sysfs_slab_alias(al->s, al->name);
  3774. if (err)
  3775. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  3776. " %s to sysfs\n", s->name);
  3777. kfree(al);
  3778. }
  3779. resiliency_test();
  3780. return 0;
  3781. }
  3782. __initcall(slab_sysfs_init);
  3783. #endif
  3784. /*
  3785. * The /proc/slabinfo ABI
  3786. */
  3787. #ifdef CONFIG_SLABINFO
  3788. static void print_slabinfo_header(struct seq_file *m)
  3789. {
  3790. seq_puts(m, "slabinfo - version: 2.1\n");
  3791. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3792. "<objperslab> <pagesperslab>");
  3793. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3794. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3795. seq_putc(m, '\n');
  3796. }
  3797. static void *s_start(struct seq_file *m, loff_t *pos)
  3798. {
  3799. loff_t n = *pos;
  3800. down_read(&slub_lock);
  3801. if (!n)
  3802. print_slabinfo_header(m);
  3803. return seq_list_start(&slab_caches, *pos);
  3804. }
  3805. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3806. {
  3807. return seq_list_next(p, &slab_caches, pos);
  3808. }
  3809. static void s_stop(struct seq_file *m, void *p)
  3810. {
  3811. up_read(&slub_lock);
  3812. }
  3813. static int s_show(struct seq_file *m, void *p)
  3814. {
  3815. unsigned long nr_partials = 0;
  3816. unsigned long nr_slabs = 0;
  3817. unsigned long nr_inuse = 0;
  3818. unsigned long nr_objs = 0;
  3819. unsigned long nr_free = 0;
  3820. struct kmem_cache *s;
  3821. int node;
  3822. s = list_entry(p, struct kmem_cache, list);
  3823. for_each_online_node(node) {
  3824. struct kmem_cache_node *n = get_node(s, node);
  3825. if (!n)
  3826. continue;
  3827. nr_partials += n->nr_partial;
  3828. nr_slabs += atomic_long_read(&n->nr_slabs);
  3829. nr_objs += atomic_long_read(&n->total_objects);
  3830. nr_free += count_partial(n, count_free);
  3831. }
  3832. nr_inuse = nr_objs - nr_free;
  3833. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  3834. nr_objs, s->size, oo_objects(s->oo),
  3835. (1 << oo_order(s->oo)));
  3836. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  3837. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  3838. 0UL);
  3839. seq_putc(m, '\n');
  3840. return 0;
  3841. }
  3842. static const struct seq_operations slabinfo_op = {
  3843. .start = s_start,
  3844. .next = s_next,
  3845. .stop = s_stop,
  3846. .show = s_show,
  3847. };
  3848. static int slabinfo_open(struct inode *inode, struct file *file)
  3849. {
  3850. return seq_open(file, &slabinfo_op);
  3851. }
  3852. static const struct file_operations proc_slabinfo_operations = {
  3853. .open = slabinfo_open,
  3854. .read = seq_read,
  3855. .llseek = seq_lseek,
  3856. .release = seq_release,
  3857. };
  3858. static int __init slab_proc_init(void)
  3859. {
  3860. proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
  3861. return 0;
  3862. }
  3863. module_init(slab_proc_init);
  3864. #endif /* CONFIG_SLABINFO */