slub.c 129 KB

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