slub.c 119 KB

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