slub.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516
  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. page->flags |= 1 << PG_slab;
  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. VM_BUG_ON(new.frozen);
  1292. new.frozen = 1;
  1293. } while (!__cmpxchg_double_slab(s, page,
  1294. freelist, counters,
  1295. NULL, new.counters,
  1296. "lock and freeze"));
  1297. remove_partial(n, page);
  1298. return freelist;
  1299. }
  1300. static int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
  1301. /*
  1302. * Try to allocate a partial slab from a specific node.
  1303. */
  1304. static void *get_partial_node(struct kmem_cache *s,
  1305. struct kmem_cache_node *n, struct kmem_cache_cpu *c)
  1306. {
  1307. struct page *page, *page2;
  1308. void *object = NULL;
  1309. /*
  1310. * Racy check. If we mistakenly see no partial slabs then we
  1311. * just allocate an empty slab. If we mistakenly try to get a
  1312. * partial slab and there is none available then get_partials()
  1313. * will return NULL.
  1314. */
  1315. if (!n || !n->nr_partial)
  1316. return NULL;
  1317. spin_lock(&n->list_lock);
  1318. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  1319. void *t = acquire_slab(s, n, page, object == NULL);
  1320. int available;
  1321. if (!t)
  1322. break;
  1323. if (!object) {
  1324. c->page = page;
  1325. c->node = page_to_nid(page);
  1326. stat(s, ALLOC_FROM_PARTIAL);
  1327. object = t;
  1328. available = page->objects - page->inuse;
  1329. } else {
  1330. page->freelist = t;
  1331. available = put_cpu_partial(s, page, 0);
  1332. stat(s, CPU_PARTIAL_NODE);
  1333. }
  1334. if (kmem_cache_debug(s) || available > s->cpu_partial / 2)
  1335. break;
  1336. }
  1337. spin_unlock(&n->list_lock);
  1338. return object;
  1339. }
  1340. /*
  1341. * Get a page from somewhere. Search in increasing NUMA distances.
  1342. */
  1343. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags,
  1344. struct kmem_cache_cpu *c)
  1345. {
  1346. #ifdef CONFIG_NUMA
  1347. struct zonelist *zonelist;
  1348. struct zoneref *z;
  1349. struct zone *zone;
  1350. enum zone_type high_zoneidx = gfp_zone(flags);
  1351. void *object;
  1352. unsigned int cpuset_mems_cookie;
  1353. /*
  1354. * The defrag ratio allows a configuration of the tradeoffs between
  1355. * inter node defragmentation and node local allocations. A lower
  1356. * defrag_ratio increases the tendency to do local allocations
  1357. * instead of attempting to obtain partial slabs from other nodes.
  1358. *
  1359. * If the defrag_ratio is set to 0 then kmalloc() always
  1360. * returns node local objects. If the ratio is higher then kmalloc()
  1361. * may return off node objects because partial slabs are obtained
  1362. * from other nodes and filled up.
  1363. *
  1364. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1365. * defrag_ratio = 1000) then every (well almost) allocation will
  1366. * first attempt to defrag slab caches on other nodes. This means
  1367. * scanning over all nodes to look for partial slabs which may be
  1368. * expensive if we do it every time we are trying to find a slab
  1369. * with available objects.
  1370. */
  1371. if (!s->remote_node_defrag_ratio ||
  1372. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1373. return NULL;
  1374. do {
  1375. cpuset_mems_cookie = get_mems_allowed();
  1376. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1377. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1378. struct kmem_cache_node *n;
  1379. n = get_node(s, zone_to_nid(zone));
  1380. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1381. n->nr_partial > s->min_partial) {
  1382. object = get_partial_node(s, n, c);
  1383. if (object) {
  1384. /*
  1385. * Return the object even if
  1386. * put_mems_allowed indicated that
  1387. * the cpuset mems_allowed was
  1388. * updated in parallel. It's a
  1389. * harmless race between the alloc
  1390. * and the cpuset update.
  1391. */
  1392. put_mems_allowed(cpuset_mems_cookie);
  1393. return object;
  1394. }
  1395. }
  1396. }
  1397. } while (!put_mems_allowed(cpuset_mems_cookie));
  1398. #endif
  1399. return NULL;
  1400. }
  1401. /*
  1402. * Get a partial page, lock it and return it.
  1403. */
  1404. static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
  1405. struct kmem_cache_cpu *c)
  1406. {
  1407. void *object;
  1408. int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
  1409. object = get_partial_node(s, get_node(s, searchnode), c);
  1410. if (object || node != NUMA_NO_NODE)
  1411. return object;
  1412. return get_any_partial(s, flags, c);
  1413. }
  1414. #ifdef CONFIG_PREEMPT
  1415. /*
  1416. * Calculate the next globally unique transaction for disambiguiation
  1417. * during cmpxchg. The transactions start with the cpu number and are then
  1418. * incremented by CONFIG_NR_CPUS.
  1419. */
  1420. #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
  1421. #else
  1422. /*
  1423. * No preemption supported therefore also no need to check for
  1424. * different cpus.
  1425. */
  1426. #define TID_STEP 1
  1427. #endif
  1428. static inline unsigned long next_tid(unsigned long tid)
  1429. {
  1430. return tid + TID_STEP;
  1431. }
  1432. static inline unsigned int tid_to_cpu(unsigned long tid)
  1433. {
  1434. return tid % TID_STEP;
  1435. }
  1436. static inline unsigned long tid_to_event(unsigned long tid)
  1437. {
  1438. return tid / TID_STEP;
  1439. }
  1440. static inline unsigned int init_tid(int cpu)
  1441. {
  1442. return cpu;
  1443. }
  1444. static inline void note_cmpxchg_failure(const char *n,
  1445. const struct kmem_cache *s, unsigned long tid)
  1446. {
  1447. #ifdef SLUB_DEBUG_CMPXCHG
  1448. unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
  1449. printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
  1450. #ifdef CONFIG_PREEMPT
  1451. if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
  1452. printk("due to cpu change %d -> %d\n",
  1453. tid_to_cpu(tid), tid_to_cpu(actual_tid));
  1454. else
  1455. #endif
  1456. if (tid_to_event(tid) != tid_to_event(actual_tid))
  1457. printk("due to cpu running other code. Event %ld->%ld\n",
  1458. tid_to_event(tid), tid_to_event(actual_tid));
  1459. else
  1460. printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
  1461. actual_tid, tid, next_tid(tid));
  1462. #endif
  1463. stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
  1464. }
  1465. void init_kmem_cache_cpus(struct kmem_cache *s)
  1466. {
  1467. int cpu;
  1468. for_each_possible_cpu(cpu)
  1469. per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
  1470. }
  1471. /*
  1472. * Remove the cpu slab
  1473. */
  1474. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1475. {
  1476. enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
  1477. struct page *page = c->page;
  1478. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1479. int lock = 0;
  1480. enum slab_modes l = M_NONE, m = M_NONE;
  1481. void *freelist;
  1482. void *nextfree;
  1483. int tail = DEACTIVATE_TO_HEAD;
  1484. struct page new;
  1485. struct page old;
  1486. if (page->freelist) {
  1487. stat(s, DEACTIVATE_REMOTE_FREES);
  1488. tail = DEACTIVATE_TO_TAIL;
  1489. }
  1490. c->tid = next_tid(c->tid);
  1491. c->page = NULL;
  1492. freelist = c->freelist;
  1493. c->freelist = NULL;
  1494. /*
  1495. * Stage one: Free all available per cpu objects back
  1496. * to the page freelist while it is still frozen. Leave the
  1497. * last one.
  1498. *
  1499. * There is no need to take the list->lock because the page
  1500. * is still frozen.
  1501. */
  1502. while (freelist && (nextfree = get_freepointer(s, freelist))) {
  1503. void *prior;
  1504. unsigned long counters;
  1505. do {
  1506. prior = page->freelist;
  1507. counters = page->counters;
  1508. set_freepointer(s, freelist, prior);
  1509. new.counters = counters;
  1510. new.inuse--;
  1511. VM_BUG_ON(!new.frozen);
  1512. } while (!__cmpxchg_double_slab(s, page,
  1513. prior, counters,
  1514. freelist, new.counters,
  1515. "drain percpu freelist"));
  1516. freelist = nextfree;
  1517. }
  1518. /*
  1519. * Stage two: Ensure that the page is unfrozen while the
  1520. * list presence reflects the actual number of objects
  1521. * during unfreeze.
  1522. *
  1523. * We setup the list membership and then perform a cmpxchg
  1524. * with the count. If there is a mismatch then the page
  1525. * is not unfrozen but the page is on the wrong list.
  1526. *
  1527. * Then we restart the process which may have to remove
  1528. * the page from the list that we just put it on again
  1529. * because the number of objects in the slab may have
  1530. * changed.
  1531. */
  1532. redo:
  1533. old.freelist = page->freelist;
  1534. old.counters = page->counters;
  1535. VM_BUG_ON(!old.frozen);
  1536. /* Determine target state of the slab */
  1537. new.counters = old.counters;
  1538. if (freelist) {
  1539. new.inuse--;
  1540. set_freepointer(s, freelist, old.freelist);
  1541. new.freelist = freelist;
  1542. } else
  1543. new.freelist = old.freelist;
  1544. new.frozen = 0;
  1545. if (!new.inuse && n->nr_partial > s->min_partial)
  1546. m = M_FREE;
  1547. else if (new.freelist) {
  1548. m = M_PARTIAL;
  1549. if (!lock) {
  1550. lock = 1;
  1551. /*
  1552. * Taking the spinlock removes the possiblity
  1553. * that acquire_slab() will see a slab page that
  1554. * is frozen
  1555. */
  1556. spin_lock(&n->list_lock);
  1557. }
  1558. } else {
  1559. m = M_FULL;
  1560. if (kmem_cache_debug(s) && !lock) {
  1561. lock = 1;
  1562. /*
  1563. * This also ensures that the scanning of full
  1564. * slabs from diagnostic functions will not see
  1565. * any frozen slabs.
  1566. */
  1567. spin_lock(&n->list_lock);
  1568. }
  1569. }
  1570. if (l != m) {
  1571. if (l == M_PARTIAL)
  1572. remove_partial(n, page);
  1573. else if (l == M_FULL)
  1574. remove_full(s, page);
  1575. if (m == M_PARTIAL) {
  1576. add_partial(n, page, tail);
  1577. stat(s, tail);
  1578. } else if (m == M_FULL) {
  1579. stat(s, DEACTIVATE_FULL);
  1580. add_full(s, n, page);
  1581. }
  1582. }
  1583. l = m;
  1584. if (!__cmpxchg_double_slab(s, page,
  1585. old.freelist, old.counters,
  1586. new.freelist, new.counters,
  1587. "unfreezing slab"))
  1588. goto redo;
  1589. if (lock)
  1590. spin_unlock(&n->list_lock);
  1591. if (m == M_FREE) {
  1592. stat(s, DEACTIVATE_EMPTY);
  1593. discard_slab(s, page);
  1594. stat(s, FREE_SLAB);
  1595. }
  1596. }
  1597. /* Unfreeze all the cpu partial slabs */
  1598. static void unfreeze_partials(struct kmem_cache *s)
  1599. {
  1600. struct kmem_cache_node *n = NULL;
  1601. struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
  1602. struct page *page, *discard_page = NULL;
  1603. while ((page = c->partial)) {
  1604. enum slab_modes { M_PARTIAL, M_FREE };
  1605. enum slab_modes l, m;
  1606. struct page new;
  1607. struct page old;
  1608. c->partial = page->next;
  1609. l = M_FREE;
  1610. do {
  1611. old.freelist = page->freelist;
  1612. old.counters = page->counters;
  1613. VM_BUG_ON(!old.frozen);
  1614. new.counters = old.counters;
  1615. new.freelist = old.freelist;
  1616. new.frozen = 0;
  1617. if (!new.inuse && (!n || n->nr_partial > s->min_partial))
  1618. m = M_FREE;
  1619. else {
  1620. struct kmem_cache_node *n2 = get_node(s,
  1621. page_to_nid(page));
  1622. m = M_PARTIAL;
  1623. if (n != n2) {
  1624. if (n)
  1625. spin_unlock(&n->list_lock);
  1626. n = n2;
  1627. spin_lock(&n->list_lock);
  1628. }
  1629. }
  1630. if (l != m) {
  1631. if (l == M_PARTIAL) {
  1632. remove_partial(n, page);
  1633. stat(s, FREE_REMOVE_PARTIAL);
  1634. } else {
  1635. add_partial(n, page,
  1636. DEACTIVATE_TO_TAIL);
  1637. stat(s, FREE_ADD_PARTIAL);
  1638. }
  1639. l = m;
  1640. }
  1641. } while (!cmpxchg_double_slab(s, page,
  1642. old.freelist, old.counters,
  1643. new.freelist, new.counters,
  1644. "unfreezing slab"));
  1645. if (m == M_FREE) {
  1646. page->next = discard_page;
  1647. discard_page = page;
  1648. }
  1649. }
  1650. if (n)
  1651. spin_unlock(&n->list_lock);
  1652. while (discard_page) {
  1653. page = discard_page;
  1654. discard_page = discard_page->next;
  1655. stat(s, DEACTIVATE_EMPTY);
  1656. discard_slab(s, page);
  1657. stat(s, FREE_SLAB);
  1658. }
  1659. }
  1660. /*
  1661. * Put a page that was just frozen (in __slab_free) into a partial page
  1662. * slot if available. This is done without interrupts disabled and without
  1663. * preemption disabled. The cmpxchg is racy and may put the partial page
  1664. * onto a random cpus partial slot.
  1665. *
  1666. * If we did not find a slot then simply move all the partials to the
  1667. * per node partial list.
  1668. */
  1669. int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  1670. {
  1671. struct page *oldpage;
  1672. int pages;
  1673. int pobjects;
  1674. do {
  1675. pages = 0;
  1676. pobjects = 0;
  1677. oldpage = this_cpu_read(s->cpu_slab->partial);
  1678. if (oldpage) {
  1679. pobjects = oldpage->pobjects;
  1680. pages = oldpage->pages;
  1681. if (drain && pobjects > s->cpu_partial) {
  1682. unsigned long flags;
  1683. /*
  1684. * partial array is full. Move the existing
  1685. * set to the per node partial list.
  1686. */
  1687. local_irq_save(flags);
  1688. unfreeze_partials(s);
  1689. local_irq_restore(flags);
  1690. pobjects = 0;
  1691. pages = 0;
  1692. stat(s, CPU_PARTIAL_DRAIN);
  1693. }
  1694. }
  1695. pages++;
  1696. pobjects += page->objects - page->inuse;
  1697. page->pages = pages;
  1698. page->pobjects = pobjects;
  1699. page->next = oldpage;
  1700. } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) != oldpage);
  1701. return pobjects;
  1702. }
  1703. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1704. {
  1705. stat(s, CPUSLAB_FLUSH);
  1706. deactivate_slab(s, c);
  1707. }
  1708. /*
  1709. * Flush cpu slab.
  1710. *
  1711. * Called from IPI handler with interrupts disabled.
  1712. */
  1713. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1714. {
  1715. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1716. if (likely(c)) {
  1717. if (c->page)
  1718. flush_slab(s, c);
  1719. unfreeze_partials(s);
  1720. }
  1721. }
  1722. static void flush_cpu_slab(void *d)
  1723. {
  1724. struct kmem_cache *s = d;
  1725. __flush_cpu_slab(s, smp_processor_id());
  1726. }
  1727. static bool has_cpu_slab(int cpu, void *info)
  1728. {
  1729. struct kmem_cache *s = info;
  1730. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1731. return !!(c->page);
  1732. }
  1733. static void flush_all(struct kmem_cache *s)
  1734. {
  1735. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  1736. }
  1737. /*
  1738. * Check if the objects in a per cpu structure fit numa
  1739. * locality expectations.
  1740. */
  1741. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1742. {
  1743. #ifdef CONFIG_NUMA
  1744. if (node != NUMA_NO_NODE && c->node != node)
  1745. return 0;
  1746. #endif
  1747. return 1;
  1748. }
  1749. static int count_free(struct page *page)
  1750. {
  1751. return page->objects - page->inuse;
  1752. }
  1753. static unsigned long count_partial(struct kmem_cache_node *n,
  1754. int (*get_count)(struct page *))
  1755. {
  1756. unsigned long flags;
  1757. unsigned long x = 0;
  1758. struct page *page;
  1759. spin_lock_irqsave(&n->list_lock, flags);
  1760. list_for_each_entry(page, &n->partial, lru)
  1761. x += get_count(page);
  1762. spin_unlock_irqrestore(&n->list_lock, flags);
  1763. return x;
  1764. }
  1765. static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
  1766. {
  1767. #ifdef CONFIG_SLUB_DEBUG
  1768. return atomic_long_read(&n->total_objects);
  1769. #else
  1770. return 0;
  1771. #endif
  1772. }
  1773. static noinline void
  1774. slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
  1775. {
  1776. int node;
  1777. printk(KERN_WARNING
  1778. "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1779. nid, gfpflags);
  1780. printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
  1781. "default order: %d, min order: %d\n", s->name, s->objsize,
  1782. s->size, oo_order(s->oo), oo_order(s->min));
  1783. if (oo_order(s->min) > get_order(s->objsize))
  1784. printk(KERN_WARNING " %s debugging increased min order, use "
  1785. "slub_debug=O to disable.\n", s->name);
  1786. for_each_online_node(node) {
  1787. struct kmem_cache_node *n = get_node(s, node);
  1788. unsigned long nr_slabs;
  1789. unsigned long nr_objs;
  1790. unsigned long nr_free;
  1791. if (!n)
  1792. continue;
  1793. nr_free = count_partial(n, count_free);
  1794. nr_slabs = node_nr_slabs(n);
  1795. nr_objs = node_nr_objs(n);
  1796. printk(KERN_WARNING
  1797. " node %d: slabs: %ld, objs: %ld, free: %ld\n",
  1798. node, nr_slabs, nr_objs, nr_free);
  1799. }
  1800. }
  1801. static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
  1802. int node, struct kmem_cache_cpu **pc)
  1803. {
  1804. void *object;
  1805. struct kmem_cache_cpu *c;
  1806. struct page *page = new_slab(s, flags, node);
  1807. if (page) {
  1808. c = __this_cpu_ptr(s->cpu_slab);
  1809. if (c->page)
  1810. flush_slab(s, c);
  1811. /*
  1812. * No other reference to the page yet so we can
  1813. * muck around with it freely without cmpxchg
  1814. */
  1815. object = page->freelist;
  1816. page->freelist = NULL;
  1817. stat(s, ALLOC_SLAB);
  1818. c->node = page_to_nid(page);
  1819. c->page = page;
  1820. *pc = c;
  1821. } else
  1822. object = NULL;
  1823. return object;
  1824. }
  1825. /*
  1826. * Check the page->freelist of a page and either transfer the freelist to the per cpu freelist
  1827. * or deactivate the page.
  1828. *
  1829. * The page is still frozen if the return value is not NULL.
  1830. *
  1831. * If this function returns NULL then the page has been unfrozen.
  1832. */
  1833. static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  1834. {
  1835. struct page new;
  1836. unsigned long counters;
  1837. void *freelist;
  1838. do {
  1839. freelist = page->freelist;
  1840. counters = page->counters;
  1841. new.counters = counters;
  1842. VM_BUG_ON(!new.frozen);
  1843. new.inuse = page->objects;
  1844. new.frozen = freelist != NULL;
  1845. } while (!cmpxchg_double_slab(s, page,
  1846. freelist, counters,
  1847. NULL, new.counters,
  1848. "get_freelist"));
  1849. return freelist;
  1850. }
  1851. /*
  1852. * Slow path. The lockless freelist is empty or we need to perform
  1853. * debugging duties.
  1854. *
  1855. * Processing is still very fast if new objects have been freed to the
  1856. * regular freelist. In that case we simply take over the regular freelist
  1857. * as the lockless freelist and zap the regular freelist.
  1858. *
  1859. * If that is not working then we fall back to the partial lists. We take the
  1860. * first element of the freelist as the object to allocate now and move the
  1861. * rest of the freelist to the lockless freelist.
  1862. *
  1863. * And if we were unable to get a new slab from the partial slab lists then
  1864. * we need to allocate a new slab. This is the slowest path since it involves
  1865. * a call to the page allocator and the setup of a new slab.
  1866. */
  1867. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  1868. unsigned long addr, struct kmem_cache_cpu *c)
  1869. {
  1870. void **object;
  1871. unsigned long flags;
  1872. local_irq_save(flags);
  1873. #ifdef CONFIG_PREEMPT
  1874. /*
  1875. * We may have been preempted and rescheduled on a different
  1876. * cpu before disabling interrupts. Need to reload cpu area
  1877. * pointer.
  1878. */
  1879. c = this_cpu_ptr(s->cpu_slab);
  1880. #endif
  1881. if (!c->page)
  1882. goto new_slab;
  1883. redo:
  1884. if (unlikely(!node_match(c, node))) {
  1885. stat(s, ALLOC_NODE_MISMATCH);
  1886. deactivate_slab(s, c);
  1887. goto new_slab;
  1888. }
  1889. /* must check again c->freelist in case of cpu migration or IRQ */
  1890. object = c->freelist;
  1891. if (object)
  1892. goto load_freelist;
  1893. stat(s, ALLOC_SLOWPATH);
  1894. object = get_freelist(s, c->page);
  1895. if (!object) {
  1896. c->page = NULL;
  1897. stat(s, DEACTIVATE_BYPASS);
  1898. goto new_slab;
  1899. }
  1900. stat(s, ALLOC_REFILL);
  1901. load_freelist:
  1902. c->freelist = get_freepointer(s, object);
  1903. c->tid = next_tid(c->tid);
  1904. local_irq_restore(flags);
  1905. return object;
  1906. new_slab:
  1907. if (c->partial) {
  1908. c->page = c->partial;
  1909. c->partial = c->page->next;
  1910. c->node = page_to_nid(c->page);
  1911. stat(s, CPU_PARTIAL_ALLOC);
  1912. c->freelist = NULL;
  1913. goto redo;
  1914. }
  1915. /* Then do expensive stuff like retrieving pages from the partial lists */
  1916. object = get_partial(s, gfpflags, node, c);
  1917. if (unlikely(!object)) {
  1918. object = new_slab_objects(s, gfpflags, node, &c);
  1919. if (unlikely(!object)) {
  1920. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1921. slab_out_of_memory(s, gfpflags, node);
  1922. local_irq_restore(flags);
  1923. return NULL;
  1924. }
  1925. }
  1926. if (likely(!kmem_cache_debug(s)))
  1927. goto load_freelist;
  1928. /* Only entered in the debug case */
  1929. if (!alloc_debug_processing(s, c->page, object, addr))
  1930. goto new_slab; /* Slab failed checks. Next slab needed */
  1931. c->freelist = get_freepointer(s, object);
  1932. deactivate_slab(s, c);
  1933. c->node = NUMA_NO_NODE;
  1934. local_irq_restore(flags);
  1935. return object;
  1936. }
  1937. /*
  1938. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1939. * have the fastpath folded into their functions. So no function call
  1940. * overhead for requests that can be satisfied on the fastpath.
  1941. *
  1942. * The fastpath works by first checking if the lockless freelist can be used.
  1943. * If not then __slab_alloc is called for slow processing.
  1944. *
  1945. * Otherwise we can simply pick the next object from the lockless free list.
  1946. */
  1947. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1948. gfp_t gfpflags, int node, unsigned long addr)
  1949. {
  1950. void **object;
  1951. struct kmem_cache_cpu *c;
  1952. unsigned long tid;
  1953. if (slab_pre_alloc_hook(s, gfpflags))
  1954. return NULL;
  1955. redo:
  1956. /*
  1957. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1958. * enabled. We may switch back and forth between cpus while
  1959. * reading from one cpu area. That does not matter as long
  1960. * as we end up on the original cpu again when doing the cmpxchg.
  1961. */
  1962. c = __this_cpu_ptr(s->cpu_slab);
  1963. /*
  1964. * The transaction ids are globally unique per cpu and per operation on
  1965. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1966. * occurs on the right processor and that there was no operation on the
  1967. * linked list in between.
  1968. */
  1969. tid = c->tid;
  1970. barrier();
  1971. object = c->freelist;
  1972. if (unlikely(!object || !node_match(c, node)))
  1973. object = __slab_alloc(s, gfpflags, node, addr, c);
  1974. else {
  1975. void *next_object = get_freepointer_safe(s, object);
  1976. /*
  1977. * The cmpxchg will only match if there was no additional
  1978. * operation and if we are on the right processor.
  1979. *
  1980. * The cmpxchg does the following atomically (without lock semantics!)
  1981. * 1. Relocate first pointer to the current per cpu area.
  1982. * 2. Verify that tid and freelist have not been changed
  1983. * 3. If they were not changed replace tid and freelist
  1984. *
  1985. * Since this is without lock semantics the protection is only against
  1986. * code executing on this cpu *not* from access by other cpus.
  1987. */
  1988. if (unlikely(!this_cpu_cmpxchg_double(
  1989. s->cpu_slab->freelist, s->cpu_slab->tid,
  1990. object, tid,
  1991. next_object, next_tid(tid)))) {
  1992. note_cmpxchg_failure("slab_alloc", s, tid);
  1993. goto redo;
  1994. }
  1995. prefetch_freepointer(s, next_object);
  1996. stat(s, ALLOC_FASTPATH);
  1997. }
  1998. if (unlikely(gfpflags & __GFP_ZERO) && object)
  1999. memset(object, 0, s->objsize);
  2000. slab_post_alloc_hook(s, gfpflags, object);
  2001. return object;
  2002. }
  2003. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  2004. {
  2005. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  2006. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  2007. return ret;
  2008. }
  2009. EXPORT_SYMBOL(kmem_cache_alloc);
  2010. #ifdef CONFIG_TRACING
  2011. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  2012. {
  2013. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  2014. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  2015. return ret;
  2016. }
  2017. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  2018. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  2019. {
  2020. void *ret = kmalloc_order(size, flags, order);
  2021. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  2022. return ret;
  2023. }
  2024. EXPORT_SYMBOL(kmalloc_order_trace);
  2025. #endif
  2026. #ifdef CONFIG_NUMA
  2027. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  2028. {
  2029. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  2030. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  2031. s->objsize, s->size, gfpflags, node);
  2032. return ret;
  2033. }
  2034. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2035. #ifdef CONFIG_TRACING
  2036. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  2037. gfp_t gfpflags,
  2038. int node, size_t size)
  2039. {
  2040. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  2041. trace_kmalloc_node(_RET_IP_, ret,
  2042. size, s->size, gfpflags, node);
  2043. return ret;
  2044. }
  2045. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  2046. #endif
  2047. #endif
  2048. /*
  2049. * Slow patch handling. This may still be called frequently since objects
  2050. * have a longer lifetime than the cpu slabs in most processing loads.
  2051. *
  2052. * So we still attempt to reduce cache line usage. Just take the slab
  2053. * lock and free the item. If there is no additional partial page
  2054. * handling required then we can return immediately.
  2055. */
  2056. static void __slab_free(struct kmem_cache *s, struct page *page,
  2057. void *x, unsigned long addr)
  2058. {
  2059. void *prior;
  2060. void **object = (void *)x;
  2061. int was_frozen;
  2062. int inuse;
  2063. struct page new;
  2064. unsigned long counters;
  2065. struct kmem_cache_node *n = NULL;
  2066. unsigned long uninitialized_var(flags);
  2067. stat(s, FREE_SLOWPATH);
  2068. if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
  2069. return;
  2070. do {
  2071. prior = page->freelist;
  2072. counters = page->counters;
  2073. set_freepointer(s, object, prior);
  2074. new.counters = counters;
  2075. was_frozen = new.frozen;
  2076. new.inuse--;
  2077. if ((!new.inuse || !prior) && !was_frozen && !n) {
  2078. if (!kmem_cache_debug(s) && !prior)
  2079. /*
  2080. * Slab was on no list before and will be partially empty
  2081. * We can defer the list move and instead freeze it.
  2082. */
  2083. new.frozen = 1;
  2084. else { /* Needs to be taken off a list */
  2085. n = get_node(s, page_to_nid(page));
  2086. /*
  2087. * Speculatively acquire the list_lock.
  2088. * If the cmpxchg does not succeed then we may
  2089. * drop the list_lock without any processing.
  2090. *
  2091. * Otherwise the list_lock will synchronize with
  2092. * other processors updating the list of slabs.
  2093. */
  2094. spin_lock_irqsave(&n->list_lock, flags);
  2095. }
  2096. }
  2097. inuse = new.inuse;
  2098. } while (!cmpxchg_double_slab(s, page,
  2099. prior, counters,
  2100. object, new.counters,
  2101. "__slab_free"));
  2102. if (likely(!n)) {
  2103. /*
  2104. * If we just froze the page then put it onto the
  2105. * per cpu partial list.
  2106. */
  2107. if (new.frozen && !was_frozen) {
  2108. put_cpu_partial(s, page, 1);
  2109. stat(s, CPU_PARTIAL_FREE);
  2110. }
  2111. /*
  2112. * The list lock was not taken therefore no list
  2113. * activity can be necessary.
  2114. */
  2115. if (was_frozen)
  2116. stat(s, FREE_FROZEN);
  2117. return;
  2118. }
  2119. /*
  2120. * was_frozen may have been set after we acquired the list_lock in
  2121. * an earlier loop. So we need to check it here again.
  2122. */
  2123. if (was_frozen)
  2124. stat(s, FREE_FROZEN);
  2125. else {
  2126. if (unlikely(!inuse && n->nr_partial > s->min_partial))
  2127. goto slab_empty;
  2128. /*
  2129. * Objects left in the slab. If it was not on the partial list before
  2130. * then add it.
  2131. */
  2132. if (unlikely(!prior)) {
  2133. remove_full(s, page);
  2134. add_partial(n, page, DEACTIVATE_TO_TAIL);
  2135. stat(s, FREE_ADD_PARTIAL);
  2136. }
  2137. }
  2138. spin_unlock_irqrestore(&n->list_lock, flags);
  2139. return;
  2140. slab_empty:
  2141. if (prior) {
  2142. /*
  2143. * Slab on the partial list.
  2144. */
  2145. remove_partial(n, page);
  2146. stat(s, FREE_REMOVE_PARTIAL);
  2147. } else
  2148. /* Slab must be on the full list */
  2149. remove_full(s, page);
  2150. spin_unlock_irqrestore(&n->list_lock, flags);
  2151. stat(s, FREE_SLAB);
  2152. discard_slab(s, page);
  2153. }
  2154. /*
  2155. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  2156. * can perform fastpath freeing without additional function calls.
  2157. *
  2158. * The fastpath is only possible if we are freeing to the current cpu slab
  2159. * of this processor. This typically the case if we have just allocated
  2160. * the item before.
  2161. *
  2162. * If fastpath is not possible then fall back to __slab_free where we deal
  2163. * with all sorts of special processing.
  2164. */
  2165. static __always_inline void slab_free(struct kmem_cache *s,
  2166. struct page *page, void *x, unsigned long addr)
  2167. {
  2168. void **object = (void *)x;
  2169. struct kmem_cache_cpu *c;
  2170. unsigned long tid;
  2171. slab_free_hook(s, x);
  2172. redo:
  2173. /*
  2174. * Determine the currently cpus per cpu slab.
  2175. * The cpu may change afterward. However that does not matter since
  2176. * data is retrieved via this pointer. If we are on the same cpu
  2177. * during the cmpxchg then the free will succedd.
  2178. */
  2179. c = __this_cpu_ptr(s->cpu_slab);
  2180. tid = c->tid;
  2181. barrier();
  2182. if (likely(page == c->page)) {
  2183. set_freepointer(s, object, c->freelist);
  2184. if (unlikely(!this_cpu_cmpxchg_double(
  2185. s->cpu_slab->freelist, s->cpu_slab->tid,
  2186. c->freelist, tid,
  2187. object, next_tid(tid)))) {
  2188. note_cmpxchg_failure("slab_free", s, tid);
  2189. goto redo;
  2190. }
  2191. stat(s, FREE_FASTPATH);
  2192. } else
  2193. __slab_free(s, page, x, addr);
  2194. }
  2195. void kmem_cache_free(struct kmem_cache *s, void *x)
  2196. {
  2197. struct page *page;
  2198. page = virt_to_head_page(x);
  2199. slab_free(s, page, x, _RET_IP_);
  2200. trace_kmem_cache_free(_RET_IP_, x);
  2201. }
  2202. EXPORT_SYMBOL(kmem_cache_free);
  2203. /*
  2204. * Object placement in a slab is made very easy because we always start at
  2205. * offset 0. If we tune the size of the object to the alignment then we can
  2206. * get the required alignment by putting one properly sized object after
  2207. * another.
  2208. *
  2209. * Notice that the allocation order determines the sizes of the per cpu
  2210. * caches. Each processor has always one slab available for allocations.
  2211. * Increasing the allocation order reduces the number of times that slabs
  2212. * must be moved on and off the partial lists and is therefore a factor in
  2213. * locking overhead.
  2214. */
  2215. /*
  2216. * Mininum / Maximum order of slab pages. This influences locking overhead
  2217. * and slab fragmentation. A higher order reduces the number of partial slabs
  2218. * and increases the number of allocations possible without having to
  2219. * take the list_lock.
  2220. */
  2221. static int slub_min_order;
  2222. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  2223. static int slub_min_objects;
  2224. /*
  2225. * Merge control. If this is set then no merging of slab caches will occur.
  2226. * (Could be removed. This was introduced to pacify the merge skeptics.)
  2227. */
  2228. static int slub_nomerge;
  2229. /*
  2230. * Calculate the order of allocation given an slab object size.
  2231. *
  2232. * The order of allocation has significant impact on performance and other
  2233. * system components. Generally order 0 allocations should be preferred since
  2234. * order 0 does not cause fragmentation in the page allocator. Larger objects
  2235. * be problematic to put into order 0 slabs because there may be too much
  2236. * unused space left. We go to a higher order if more than 1/16th of the slab
  2237. * would be wasted.
  2238. *
  2239. * In order to reach satisfactory performance we must ensure that a minimum
  2240. * number of objects is in one slab. Otherwise we may generate too much
  2241. * activity on the partial lists which requires taking the list_lock. This is
  2242. * less a concern for large slabs though which are rarely used.
  2243. *
  2244. * slub_max_order specifies the order where we begin to stop considering the
  2245. * number of objects in a slab as critical. If we reach slub_max_order then
  2246. * we try to keep the page order as low as possible. So we accept more waste
  2247. * of space in favor of a small page order.
  2248. *
  2249. * Higher order allocations also allow the placement of more objects in a
  2250. * slab and thereby reduce object handling overhead. If the user has
  2251. * requested a higher mininum order then we start with that one instead of
  2252. * the smallest order which will fit the object.
  2253. */
  2254. static inline int slab_order(int size, int min_objects,
  2255. int max_order, int fract_leftover, int reserved)
  2256. {
  2257. int order;
  2258. int rem;
  2259. int min_order = slub_min_order;
  2260. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  2261. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  2262. for (order = max(min_order,
  2263. fls(min_objects * size - 1) - PAGE_SHIFT);
  2264. order <= max_order; order++) {
  2265. unsigned long slab_size = PAGE_SIZE << order;
  2266. if (slab_size < min_objects * size + reserved)
  2267. continue;
  2268. rem = (slab_size - reserved) % size;
  2269. if (rem <= slab_size / fract_leftover)
  2270. break;
  2271. }
  2272. return order;
  2273. }
  2274. static inline int calculate_order(int size, int reserved)
  2275. {
  2276. int order;
  2277. int min_objects;
  2278. int fraction;
  2279. int max_objects;
  2280. /*
  2281. * Attempt to find best configuration for a slab. This
  2282. * works by first attempting to generate a layout with
  2283. * the best configuration and backing off gradually.
  2284. *
  2285. * First we reduce the acceptable waste in a slab. Then
  2286. * we reduce the minimum objects required in a slab.
  2287. */
  2288. min_objects = slub_min_objects;
  2289. if (!min_objects)
  2290. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  2291. max_objects = order_objects(slub_max_order, size, reserved);
  2292. min_objects = min(min_objects, max_objects);
  2293. while (min_objects > 1) {
  2294. fraction = 16;
  2295. while (fraction >= 4) {
  2296. order = slab_order(size, min_objects,
  2297. slub_max_order, fraction, reserved);
  2298. if (order <= slub_max_order)
  2299. return order;
  2300. fraction /= 2;
  2301. }
  2302. min_objects--;
  2303. }
  2304. /*
  2305. * We were unable to place multiple objects in a slab. Now
  2306. * lets see if we can place a single object there.
  2307. */
  2308. order = slab_order(size, 1, slub_max_order, 1, reserved);
  2309. if (order <= slub_max_order)
  2310. return order;
  2311. /*
  2312. * Doh this slab cannot be placed using slub_max_order.
  2313. */
  2314. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  2315. if (order < MAX_ORDER)
  2316. return order;
  2317. return -ENOSYS;
  2318. }
  2319. /*
  2320. * Figure out what the alignment of the objects will be.
  2321. */
  2322. static unsigned long calculate_alignment(unsigned long flags,
  2323. unsigned long align, unsigned long size)
  2324. {
  2325. /*
  2326. * If the user wants hardware cache aligned objects then follow that
  2327. * suggestion if the object is sufficiently large.
  2328. *
  2329. * The hardware cache alignment cannot override the specified
  2330. * alignment though. If that is greater then use it.
  2331. */
  2332. if (flags & SLAB_HWCACHE_ALIGN) {
  2333. unsigned long ralign = cache_line_size();
  2334. while (size <= ralign / 2)
  2335. ralign /= 2;
  2336. align = max(align, ralign);
  2337. }
  2338. if (align < ARCH_SLAB_MINALIGN)
  2339. align = ARCH_SLAB_MINALIGN;
  2340. return ALIGN(align, sizeof(void *));
  2341. }
  2342. static void
  2343. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  2344. {
  2345. n->nr_partial = 0;
  2346. spin_lock_init(&n->list_lock);
  2347. INIT_LIST_HEAD(&n->partial);
  2348. #ifdef CONFIG_SLUB_DEBUG
  2349. atomic_long_set(&n->nr_slabs, 0);
  2350. atomic_long_set(&n->total_objects, 0);
  2351. INIT_LIST_HEAD(&n->full);
  2352. #endif
  2353. }
  2354. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2355. {
  2356. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2357. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  2358. /*
  2359. * Must align to double word boundary for the double cmpxchg
  2360. * instructions to work; see __pcpu_double_call_return_bool().
  2361. */
  2362. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  2363. 2 * sizeof(void *));
  2364. if (!s->cpu_slab)
  2365. return 0;
  2366. init_kmem_cache_cpus(s);
  2367. return 1;
  2368. }
  2369. static struct kmem_cache *kmem_cache_node;
  2370. /*
  2371. * No kmalloc_node yet so do it by hand. We know that this is the first
  2372. * slab on the node for this slabcache. There are no concurrent accesses
  2373. * possible.
  2374. *
  2375. * Note that this function only works on the kmalloc_node_cache
  2376. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2377. * memory on a fresh node that has no slab structures yet.
  2378. */
  2379. static void early_kmem_cache_node_alloc(int node)
  2380. {
  2381. struct page *page;
  2382. struct kmem_cache_node *n;
  2383. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2384. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2385. BUG_ON(!page);
  2386. if (page_to_nid(page) != node) {
  2387. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2388. "node %d\n", node);
  2389. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2390. "in order to be able to continue\n");
  2391. }
  2392. n = page->freelist;
  2393. BUG_ON(!n);
  2394. page->freelist = get_freepointer(kmem_cache_node, n);
  2395. page->inuse = 1;
  2396. page->frozen = 0;
  2397. kmem_cache_node->node[node] = n;
  2398. #ifdef CONFIG_SLUB_DEBUG
  2399. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2400. init_tracking(kmem_cache_node, n);
  2401. #endif
  2402. init_kmem_cache_node(n, kmem_cache_node);
  2403. inc_slabs_node(kmem_cache_node, node, page->objects);
  2404. add_partial(n, page, DEACTIVATE_TO_HEAD);
  2405. }
  2406. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2407. {
  2408. int node;
  2409. for_each_node_state(node, N_NORMAL_MEMORY) {
  2410. struct kmem_cache_node *n = s->node[node];
  2411. if (n)
  2412. kmem_cache_free(kmem_cache_node, n);
  2413. s->node[node] = NULL;
  2414. }
  2415. }
  2416. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2417. {
  2418. int node;
  2419. for_each_node_state(node, N_NORMAL_MEMORY) {
  2420. struct kmem_cache_node *n;
  2421. if (slab_state == DOWN) {
  2422. early_kmem_cache_node_alloc(node);
  2423. continue;
  2424. }
  2425. n = kmem_cache_alloc_node(kmem_cache_node,
  2426. GFP_KERNEL, node);
  2427. if (!n) {
  2428. free_kmem_cache_nodes(s);
  2429. return 0;
  2430. }
  2431. s->node[node] = n;
  2432. init_kmem_cache_node(n, s);
  2433. }
  2434. return 1;
  2435. }
  2436. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2437. {
  2438. if (min < MIN_PARTIAL)
  2439. min = MIN_PARTIAL;
  2440. else if (min > MAX_PARTIAL)
  2441. min = MAX_PARTIAL;
  2442. s->min_partial = min;
  2443. }
  2444. /*
  2445. * calculate_sizes() determines the order and the distribution of data within
  2446. * a slab object.
  2447. */
  2448. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2449. {
  2450. unsigned long flags = s->flags;
  2451. unsigned long size = s->objsize;
  2452. unsigned long align = s->align;
  2453. int order;
  2454. /*
  2455. * Round up object size to the next word boundary. We can only
  2456. * place the free pointer at word boundaries and this determines
  2457. * the possible location of the free pointer.
  2458. */
  2459. size = ALIGN(size, sizeof(void *));
  2460. #ifdef CONFIG_SLUB_DEBUG
  2461. /*
  2462. * Determine if we can poison the object itself. If the user of
  2463. * the slab may touch the object after free or before allocation
  2464. * then we should never poison the object itself.
  2465. */
  2466. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2467. !s->ctor)
  2468. s->flags |= __OBJECT_POISON;
  2469. else
  2470. s->flags &= ~__OBJECT_POISON;
  2471. /*
  2472. * If we are Redzoning then check if there is some space between the
  2473. * end of the object and the free pointer. If not then add an
  2474. * additional word to have some bytes to store Redzone information.
  2475. */
  2476. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2477. size += sizeof(void *);
  2478. #endif
  2479. /*
  2480. * With that we have determined the number of bytes in actual use
  2481. * by the object. This is the potential offset to the free pointer.
  2482. */
  2483. s->inuse = size;
  2484. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2485. s->ctor)) {
  2486. /*
  2487. * Relocate free pointer after the object if it is not
  2488. * permitted to overwrite the first word of the object on
  2489. * kmem_cache_free.
  2490. *
  2491. * This is the case if we do RCU, have a constructor or
  2492. * destructor or are poisoning the objects.
  2493. */
  2494. s->offset = size;
  2495. size += sizeof(void *);
  2496. }
  2497. #ifdef CONFIG_SLUB_DEBUG
  2498. if (flags & SLAB_STORE_USER)
  2499. /*
  2500. * Need to store information about allocs and frees after
  2501. * the object.
  2502. */
  2503. size += 2 * sizeof(struct track);
  2504. if (flags & SLAB_RED_ZONE)
  2505. /*
  2506. * Add some empty padding so that we can catch
  2507. * overwrites from earlier objects rather than let
  2508. * tracking information or the free pointer be
  2509. * corrupted if a user writes before the start
  2510. * of the object.
  2511. */
  2512. size += sizeof(void *);
  2513. #endif
  2514. /*
  2515. * Determine the alignment based on various parameters that the
  2516. * user specified and the dynamic determination of cache line size
  2517. * on bootup.
  2518. */
  2519. align = calculate_alignment(flags, align, s->objsize);
  2520. s->align = align;
  2521. /*
  2522. * SLUB stores one object immediately after another beginning from
  2523. * offset 0. In order to align the objects we have to simply size
  2524. * each object to conform to the alignment.
  2525. */
  2526. size = ALIGN(size, align);
  2527. s->size = size;
  2528. if (forced_order >= 0)
  2529. order = forced_order;
  2530. else
  2531. order = calculate_order(size, s->reserved);
  2532. if (order < 0)
  2533. return 0;
  2534. s->allocflags = 0;
  2535. if (order)
  2536. s->allocflags |= __GFP_COMP;
  2537. if (s->flags & SLAB_CACHE_DMA)
  2538. s->allocflags |= SLUB_DMA;
  2539. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2540. s->allocflags |= __GFP_RECLAIMABLE;
  2541. /*
  2542. * Determine the number of objects per slab
  2543. */
  2544. s->oo = oo_make(order, size, s->reserved);
  2545. s->min = oo_make(get_order(size), size, s->reserved);
  2546. if (oo_objects(s->oo) > oo_objects(s->max))
  2547. s->max = s->oo;
  2548. return !!oo_objects(s->oo);
  2549. }
  2550. static int kmem_cache_open(struct kmem_cache *s,
  2551. const char *name, size_t size,
  2552. size_t align, unsigned long flags,
  2553. void (*ctor)(void *))
  2554. {
  2555. memset(s, 0, kmem_size);
  2556. s->name = name;
  2557. s->ctor = ctor;
  2558. s->objsize = size;
  2559. s->align = align;
  2560. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2561. s->reserved = 0;
  2562. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2563. s->reserved = sizeof(struct rcu_head);
  2564. if (!calculate_sizes(s, -1))
  2565. goto error;
  2566. if (disable_higher_order_debug) {
  2567. /*
  2568. * Disable debugging flags that store metadata if the min slab
  2569. * order increased.
  2570. */
  2571. if (get_order(s->size) > get_order(s->objsize)) {
  2572. s->flags &= ~DEBUG_METADATA_FLAGS;
  2573. s->offset = 0;
  2574. if (!calculate_sizes(s, -1))
  2575. goto error;
  2576. }
  2577. }
  2578. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
  2579. defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  2580. if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
  2581. /* Enable fast mode */
  2582. s->flags |= __CMPXCHG_DOUBLE;
  2583. #endif
  2584. /*
  2585. * The larger the object size is, the more pages we want on the partial
  2586. * list to avoid pounding the page allocator excessively.
  2587. */
  2588. set_min_partial(s, ilog2(s->size) / 2);
  2589. /*
  2590. * cpu_partial determined the maximum number of objects kept in the
  2591. * per cpu partial lists of a processor.
  2592. *
  2593. * Per cpu partial lists mainly contain slabs that just have one
  2594. * object freed. If they are used for allocation then they can be
  2595. * filled up again with minimal effort. The slab will never hit the
  2596. * per node partial lists and therefore no locking will be required.
  2597. *
  2598. * This setting also determines
  2599. *
  2600. * A) The number of objects from per cpu partial slabs dumped to the
  2601. * per node list when we reach the limit.
  2602. * B) The number of objects in cpu partial slabs to extract from the
  2603. * per node list when we run out of per cpu objects. We only fetch 50%
  2604. * to keep some capacity around for frees.
  2605. */
  2606. if (kmem_cache_debug(s))
  2607. s->cpu_partial = 0;
  2608. else if (s->size >= PAGE_SIZE)
  2609. s->cpu_partial = 2;
  2610. else if (s->size >= 1024)
  2611. s->cpu_partial = 6;
  2612. else if (s->size >= 256)
  2613. s->cpu_partial = 13;
  2614. else
  2615. s->cpu_partial = 30;
  2616. s->refcount = 1;
  2617. #ifdef CONFIG_NUMA
  2618. s->remote_node_defrag_ratio = 1000;
  2619. #endif
  2620. if (!init_kmem_cache_nodes(s))
  2621. goto error;
  2622. if (alloc_kmem_cache_cpus(s))
  2623. return 1;
  2624. free_kmem_cache_nodes(s);
  2625. error:
  2626. if (flags & SLAB_PANIC)
  2627. panic("Cannot create slab %s size=%lu realsize=%u "
  2628. "order=%u offset=%u flags=%lx\n",
  2629. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2630. s->offset, flags);
  2631. return 0;
  2632. }
  2633. /*
  2634. * Determine the size of a slab object
  2635. */
  2636. unsigned int kmem_cache_size(struct kmem_cache *s)
  2637. {
  2638. return s->objsize;
  2639. }
  2640. EXPORT_SYMBOL(kmem_cache_size);
  2641. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2642. const char *text)
  2643. {
  2644. #ifdef CONFIG_SLUB_DEBUG
  2645. void *addr = page_address(page);
  2646. void *p;
  2647. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2648. sizeof(long), GFP_ATOMIC);
  2649. if (!map)
  2650. return;
  2651. slab_err(s, page, "%s", text);
  2652. slab_lock(page);
  2653. get_map(s, page, map);
  2654. for_each_object(p, s, addr, page->objects) {
  2655. if (!test_bit(slab_index(p, s, addr), map)) {
  2656. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2657. p, p - addr);
  2658. print_tracking(s, p);
  2659. }
  2660. }
  2661. slab_unlock(page);
  2662. kfree(map);
  2663. #endif
  2664. }
  2665. /*
  2666. * Attempt to free all partial slabs on a node.
  2667. * This is called from kmem_cache_close(). We must be the last thread
  2668. * using the cache and therefore we do not need to lock anymore.
  2669. */
  2670. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2671. {
  2672. struct page *page, *h;
  2673. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2674. if (!page->inuse) {
  2675. remove_partial(n, page);
  2676. discard_slab(s, page);
  2677. } else {
  2678. list_slab_objects(s, page,
  2679. "Objects remaining on kmem_cache_close()");
  2680. }
  2681. }
  2682. }
  2683. /*
  2684. * Release all resources used by a slab cache.
  2685. */
  2686. static inline int kmem_cache_close(struct kmem_cache *s)
  2687. {
  2688. int node;
  2689. flush_all(s);
  2690. free_percpu(s->cpu_slab);
  2691. /* Attempt to free all objects */
  2692. for_each_node_state(node, N_NORMAL_MEMORY) {
  2693. struct kmem_cache_node *n = get_node(s, node);
  2694. free_partial(s, n);
  2695. if (n->nr_partial || slabs_node(s, node))
  2696. return 1;
  2697. }
  2698. free_kmem_cache_nodes(s);
  2699. return 0;
  2700. }
  2701. /*
  2702. * Close a cache and release the kmem_cache structure
  2703. * (must be used for caches created using kmem_cache_create)
  2704. */
  2705. void kmem_cache_destroy(struct kmem_cache *s)
  2706. {
  2707. down_write(&slub_lock);
  2708. s->refcount--;
  2709. if (!s->refcount) {
  2710. list_del(&s->list);
  2711. up_write(&slub_lock);
  2712. if (kmem_cache_close(s)) {
  2713. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2714. "still has objects.\n", s->name, __func__);
  2715. dump_stack();
  2716. }
  2717. if (s->flags & SLAB_DESTROY_BY_RCU)
  2718. rcu_barrier();
  2719. sysfs_slab_remove(s);
  2720. } else
  2721. up_write(&slub_lock);
  2722. }
  2723. EXPORT_SYMBOL(kmem_cache_destroy);
  2724. /********************************************************************
  2725. * Kmalloc subsystem
  2726. *******************************************************************/
  2727. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2728. EXPORT_SYMBOL(kmalloc_caches);
  2729. static struct kmem_cache *kmem_cache;
  2730. #ifdef CONFIG_ZONE_DMA
  2731. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2732. #endif
  2733. static int __init setup_slub_min_order(char *str)
  2734. {
  2735. get_option(&str, &slub_min_order);
  2736. return 1;
  2737. }
  2738. __setup("slub_min_order=", setup_slub_min_order);
  2739. static int __init setup_slub_max_order(char *str)
  2740. {
  2741. get_option(&str, &slub_max_order);
  2742. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2743. return 1;
  2744. }
  2745. __setup("slub_max_order=", setup_slub_max_order);
  2746. static int __init setup_slub_min_objects(char *str)
  2747. {
  2748. get_option(&str, &slub_min_objects);
  2749. return 1;
  2750. }
  2751. __setup("slub_min_objects=", setup_slub_min_objects);
  2752. static int __init setup_slub_nomerge(char *str)
  2753. {
  2754. slub_nomerge = 1;
  2755. return 1;
  2756. }
  2757. __setup("slub_nomerge", setup_slub_nomerge);
  2758. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2759. int size, unsigned int flags)
  2760. {
  2761. struct kmem_cache *s;
  2762. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2763. /*
  2764. * This function is called with IRQs disabled during early-boot on
  2765. * single CPU so there's no need to take slub_lock here.
  2766. */
  2767. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2768. flags, NULL))
  2769. goto panic;
  2770. list_add(&s->list, &slab_caches);
  2771. return s;
  2772. panic:
  2773. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2774. return NULL;
  2775. }
  2776. /*
  2777. * Conversion table for small slabs sizes / 8 to the index in the
  2778. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2779. * of two cache sizes there. The size of larger slabs can be determined using
  2780. * fls.
  2781. */
  2782. static s8 size_index[24] = {
  2783. 3, /* 8 */
  2784. 4, /* 16 */
  2785. 5, /* 24 */
  2786. 5, /* 32 */
  2787. 6, /* 40 */
  2788. 6, /* 48 */
  2789. 6, /* 56 */
  2790. 6, /* 64 */
  2791. 1, /* 72 */
  2792. 1, /* 80 */
  2793. 1, /* 88 */
  2794. 1, /* 96 */
  2795. 7, /* 104 */
  2796. 7, /* 112 */
  2797. 7, /* 120 */
  2798. 7, /* 128 */
  2799. 2, /* 136 */
  2800. 2, /* 144 */
  2801. 2, /* 152 */
  2802. 2, /* 160 */
  2803. 2, /* 168 */
  2804. 2, /* 176 */
  2805. 2, /* 184 */
  2806. 2 /* 192 */
  2807. };
  2808. static inline int size_index_elem(size_t bytes)
  2809. {
  2810. return (bytes - 1) / 8;
  2811. }
  2812. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2813. {
  2814. int index;
  2815. if (size <= 192) {
  2816. if (!size)
  2817. return ZERO_SIZE_PTR;
  2818. index = size_index[size_index_elem(size)];
  2819. } else
  2820. index = fls(size - 1);
  2821. #ifdef CONFIG_ZONE_DMA
  2822. if (unlikely((flags & SLUB_DMA)))
  2823. return kmalloc_dma_caches[index];
  2824. #endif
  2825. return kmalloc_caches[index];
  2826. }
  2827. void *__kmalloc(size_t size, gfp_t flags)
  2828. {
  2829. struct kmem_cache *s;
  2830. void *ret;
  2831. if (unlikely(size > SLUB_MAX_SIZE))
  2832. return kmalloc_large(size, flags);
  2833. s = get_slab(size, flags);
  2834. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2835. return s;
  2836. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2837. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2838. return ret;
  2839. }
  2840. EXPORT_SYMBOL(__kmalloc);
  2841. #ifdef CONFIG_NUMA
  2842. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2843. {
  2844. struct page *page;
  2845. void *ptr = NULL;
  2846. flags |= __GFP_COMP | __GFP_NOTRACK;
  2847. page = alloc_pages_node(node, flags, get_order(size));
  2848. if (page)
  2849. ptr = page_address(page);
  2850. kmemleak_alloc(ptr, size, 1, flags);
  2851. return ptr;
  2852. }
  2853. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2854. {
  2855. struct kmem_cache *s;
  2856. void *ret;
  2857. if (unlikely(size > SLUB_MAX_SIZE)) {
  2858. ret = kmalloc_large_node(size, flags, node);
  2859. trace_kmalloc_node(_RET_IP_, ret,
  2860. size, PAGE_SIZE << get_order(size),
  2861. flags, node);
  2862. return ret;
  2863. }
  2864. s = get_slab(size, flags);
  2865. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2866. return s;
  2867. ret = slab_alloc(s, flags, node, _RET_IP_);
  2868. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2869. return ret;
  2870. }
  2871. EXPORT_SYMBOL(__kmalloc_node);
  2872. #endif
  2873. size_t ksize(const void *object)
  2874. {
  2875. struct page *page;
  2876. if (unlikely(object == ZERO_SIZE_PTR))
  2877. return 0;
  2878. page = virt_to_head_page(object);
  2879. if (unlikely(!PageSlab(page))) {
  2880. WARN_ON(!PageCompound(page));
  2881. return PAGE_SIZE << compound_order(page);
  2882. }
  2883. return slab_ksize(page->slab);
  2884. }
  2885. EXPORT_SYMBOL(ksize);
  2886. #ifdef CONFIG_SLUB_DEBUG
  2887. bool verify_mem_not_deleted(const void *x)
  2888. {
  2889. struct page *page;
  2890. void *object = (void *)x;
  2891. unsigned long flags;
  2892. bool rv;
  2893. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2894. return false;
  2895. local_irq_save(flags);
  2896. page = virt_to_head_page(x);
  2897. if (unlikely(!PageSlab(page))) {
  2898. /* maybe it was from stack? */
  2899. rv = true;
  2900. goto out_unlock;
  2901. }
  2902. slab_lock(page);
  2903. if (on_freelist(page->slab, page, object)) {
  2904. object_err(page->slab, page, object, "Object is on free-list");
  2905. rv = false;
  2906. } else {
  2907. rv = true;
  2908. }
  2909. slab_unlock(page);
  2910. out_unlock:
  2911. local_irq_restore(flags);
  2912. return rv;
  2913. }
  2914. EXPORT_SYMBOL(verify_mem_not_deleted);
  2915. #endif
  2916. void kfree(const void *x)
  2917. {
  2918. struct page *page;
  2919. void *object = (void *)x;
  2920. trace_kfree(_RET_IP_, x);
  2921. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2922. return;
  2923. page = virt_to_head_page(x);
  2924. if (unlikely(!PageSlab(page))) {
  2925. BUG_ON(!PageCompound(page));
  2926. kmemleak_free(x);
  2927. put_page(page);
  2928. return;
  2929. }
  2930. slab_free(page->slab, page, object, _RET_IP_);
  2931. }
  2932. EXPORT_SYMBOL(kfree);
  2933. /*
  2934. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2935. * the remaining slabs by the number of items in use. The slabs with the
  2936. * most items in use come first. New allocations will then fill those up
  2937. * and thus they can be removed from the partial lists.
  2938. *
  2939. * The slabs with the least items are placed last. This results in them
  2940. * being allocated from last increasing the chance that the last objects
  2941. * are freed in them.
  2942. */
  2943. int kmem_cache_shrink(struct kmem_cache *s)
  2944. {
  2945. int node;
  2946. int i;
  2947. struct kmem_cache_node *n;
  2948. struct page *page;
  2949. struct page *t;
  2950. int objects = oo_objects(s->max);
  2951. struct list_head *slabs_by_inuse =
  2952. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2953. unsigned long flags;
  2954. if (!slabs_by_inuse)
  2955. return -ENOMEM;
  2956. flush_all(s);
  2957. for_each_node_state(node, N_NORMAL_MEMORY) {
  2958. n = get_node(s, node);
  2959. if (!n->nr_partial)
  2960. continue;
  2961. for (i = 0; i < objects; i++)
  2962. INIT_LIST_HEAD(slabs_by_inuse + i);
  2963. spin_lock_irqsave(&n->list_lock, flags);
  2964. /*
  2965. * Build lists indexed by the items in use in each slab.
  2966. *
  2967. * Note that concurrent frees may occur while we hold the
  2968. * list_lock. page->inuse here is the upper limit.
  2969. */
  2970. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2971. list_move(&page->lru, slabs_by_inuse + page->inuse);
  2972. if (!page->inuse)
  2973. n->nr_partial--;
  2974. }
  2975. /*
  2976. * Rebuild the partial list with the slabs filled up most
  2977. * first and the least used slabs at the end.
  2978. */
  2979. for (i = objects - 1; i > 0; i--)
  2980. list_splice(slabs_by_inuse + i, n->partial.prev);
  2981. spin_unlock_irqrestore(&n->list_lock, flags);
  2982. /* Release empty slabs */
  2983. list_for_each_entry_safe(page, t, slabs_by_inuse, lru)
  2984. discard_slab(s, page);
  2985. }
  2986. kfree(slabs_by_inuse);
  2987. return 0;
  2988. }
  2989. EXPORT_SYMBOL(kmem_cache_shrink);
  2990. #if defined(CONFIG_MEMORY_HOTPLUG)
  2991. static int slab_mem_going_offline_callback(void *arg)
  2992. {
  2993. struct kmem_cache *s;
  2994. down_read(&slub_lock);
  2995. list_for_each_entry(s, &slab_caches, list)
  2996. kmem_cache_shrink(s);
  2997. up_read(&slub_lock);
  2998. return 0;
  2999. }
  3000. static void slab_mem_offline_callback(void *arg)
  3001. {
  3002. struct kmem_cache_node *n;
  3003. struct kmem_cache *s;
  3004. struct memory_notify *marg = arg;
  3005. int offline_node;
  3006. offline_node = marg->status_change_nid;
  3007. /*
  3008. * If the node still has available memory. we need kmem_cache_node
  3009. * for it yet.
  3010. */
  3011. if (offline_node < 0)
  3012. return;
  3013. down_read(&slub_lock);
  3014. list_for_each_entry(s, &slab_caches, list) {
  3015. n = get_node(s, offline_node);
  3016. if (n) {
  3017. /*
  3018. * if n->nr_slabs > 0, slabs still exist on the node
  3019. * that is going down. We were unable to free them,
  3020. * and offline_pages() function shouldn't call this
  3021. * callback. So, we must fail.
  3022. */
  3023. BUG_ON(slabs_node(s, offline_node));
  3024. s->node[offline_node] = NULL;
  3025. kmem_cache_free(kmem_cache_node, n);
  3026. }
  3027. }
  3028. up_read(&slub_lock);
  3029. }
  3030. static int slab_mem_going_online_callback(void *arg)
  3031. {
  3032. struct kmem_cache_node *n;
  3033. struct kmem_cache *s;
  3034. struct memory_notify *marg = arg;
  3035. int nid = marg->status_change_nid;
  3036. int ret = 0;
  3037. /*
  3038. * If the node's memory is already available, then kmem_cache_node is
  3039. * already created. Nothing to do.
  3040. */
  3041. if (nid < 0)
  3042. return 0;
  3043. /*
  3044. * We are bringing a node online. No memory is available yet. We must
  3045. * allocate a kmem_cache_node structure in order to bring the node
  3046. * online.
  3047. */
  3048. down_read(&slub_lock);
  3049. list_for_each_entry(s, &slab_caches, list) {
  3050. /*
  3051. * XXX: kmem_cache_alloc_node will fallback to other nodes
  3052. * since memory is not yet available from the node that
  3053. * is brought up.
  3054. */
  3055. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  3056. if (!n) {
  3057. ret = -ENOMEM;
  3058. goto out;
  3059. }
  3060. init_kmem_cache_node(n, s);
  3061. s->node[nid] = n;
  3062. }
  3063. out:
  3064. up_read(&slub_lock);
  3065. return ret;
  3066. }
  3067. static int slab_memory_callback(struct notifier_block *self,
  3068. unsigned long action, void *arg)
  3069. {
  3070. int ret = 0;
  3071. switch (action) {
  3072. case MEM_GOING_ONLINE:
  3073. ret = slab_mem_going_online_callback(arg);
  3074. break;
  3075. case MEM_GOING_OFFLINE:
  3076. ret = slab_mem_going_offline_callback(arg);
  3077. break;
  3078. case MEM_OFFLINE:
  3079. case MEM_CANCEL_ONLINE:
  3080. slab_mem_offline_callback(arg);
  3081. break;
  3082. case MEM_ONLINE:
  3083. case MEM_CANCEL_OFFLINE:
  3084. break;
  3085. }
  3086. if (ret)
  3087. ret = notifier_from_errno(ret);
  3088. else
  3089. ret = NOTIFY_OK;
  3090. return ret;
  3091. }
  3092. #endif /* CONFIG_MEMORY_HOTPLUG */
  3093. /********************************************************************
  3094. * Basic setup of slabs
  3095. *******************************************************************/
  3096. /*
  3097. * Used for early kmem_cache structures that were allocated using
  3098. * the page allocator
  3099. */
  3100. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  3101. {
  3102. int node;
  3103. list_add(&s->list, &slab_caches);
  3104. s->refcount = -1;
  3105. for_each_node_state(node, N_NORMAL_MEMORY) {
  3106. struct kmem_cache_node *n = get_node(s, node);
  3107. struct page *p;
  3108. if (n) {
  3109. list_for_each_entry(p, &n->partial, lru)
  3110. p->slab = s;
  3111. #ifdef CONFIG_SLUB_DEBUG
  3112. list_for_each_entry(p, &n->full, lru)
  3113. p->slab = s;
  3114. #endif
  3115. }
  3116. }
  3117. }
  3118. void __init kmem_cache_init(void)
  3119. {
  3120. int i;
  3121. int caches = 0;
  3122. struct kmem_cache *temp_kmem_cache;
  3123. int order;
  3124. struct kmem_cache *temp_kmem_cache_node;
  3125. unsigned long kmalloc_size;
  3126. if (debug_guardpage_minorder())
  3127. slub_max_order = 0;
  3128. kmem_size = offsetof(struct kmem_cache, node) +
  3129. nr_node_ids * sizeof(struct kmem_cache_node *);
  3130. /* Allocate two kmem_caches from the page allocator */
  3131. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  3132. order = get_order(2 * kmalloc_size);
  3133. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  3134. /*
  3135. * Must first have the slab cache available for the allocations of the
  3136. * struct kmem_cache_node's. There is special bootstrap code in
  3137. * kmem_cache_open for slab_state == DOWN.
  3138. */
  3139. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  3140. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  3141. sizeof(struct kmem_cache_node),
  3142. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  3143. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  3144. /* Able to allocate the per node structures */
  3145. slab_state = PARTIAL;
  3146. temp_kmem_cache = kmem_cache;
  3147. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  3148. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  3149. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3150. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  3151. /*
  3152. * Allocate kmem_cache_node properly from the kmem_cache slab.
  3153. * kmem_cache_node is separately allocated so no need to
  3154. * update any list pointers.
  3155. */
  3156. temp_kmem_cache_node = kmem_cache_node;
  3157. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3158. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  3159. kmem_cache_bootstrap_fixup(kmem_cache_node);
  3160. caches++;
  3161. kmem_cache_bootstrap_fixup(kmem_cache);
  3162. caches++;
  3163. /* Free temporary boot structure */
  3164. free_pages((unsigned long)temp_kmem_cache, order);
  3165. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  3166. /*
  3167. * Patch up the size_index table if we have strange large alignment
  3168. * requirements for the kmalloc array. This is only the case for
  3169. * MIPS it seems. The standard arches will not generate any code here.
  3170. *
  3171. * Largest permitted alignment is 256 bytes due to the way we
  3172. * handle the index determination for the smaller caches.
  3173. *
  3174. * Make sure that nothing crazy happens if someone starts tinkering
  3175. * around with ARCH_KMALLOC_MINALIGN
  3176. */
  3177. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  3178. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  3179. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  3180. int elem = size_index_elem(i);
  3181. if (elem >= ARRAY_SIZE(size_index))
  3182. break;
  3183. size_index[elem] = KMALLOC_SHIFT_LOW;
  3184. }
  3185. if (KMALLOC_MIN_SIZE == 64) {
  3186. /*
  3187. * The 96 byte size cache is not used if the alignment
  3188. * is 64 byte.
  3189. */
  3190. for (i = 64 + 8; i <= 96; i += 8)
  3191. size_index[size_index_elem(i)] = 7;
  3192. } else if (KMALLOC_MIN_SIZE == 128) {
  3193. /*
  3194. * The 192 byte sized cache is not used if the alignment
  3195. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  3196. * instead.
  3197. */
  3198. for (i = 128 + 8; i <= 192; i += 8)
  3199. size_index[size_index_elem(i)] = 8;
  3200. }
  3201. /* Caches that are not of the two-to-the-power-of size */
  3202. if (KMALLOC_MIN_SIZE <= 32) {
  3203. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  3204. caches++;
  3205. }
  3206. if (KMALLOC_MIN_SIZE <= 64) {
  3207. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  3208. caches++;
  3209. }
  3210. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3211. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  3212. caches++;
  3213. }
  3214. slab_state = UP;
  3215. /* Provide the correct kmalloc names now that the caches are up */
  3216. if (KMALLOC_MIN_SIZE <= 32) {
  3217. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  3218. BUG_ON(!kmalloc_caches[1]->name);
  3219. }
  3220. if (KMALLOC_MIN_SIZE <= 64) {
  3221. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  3222. BUG_ON(!kmalloc_caches[2]->name);
  3223. }
  3224. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3225. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  3226. BUG_ON(!s);
  3227. kmalloc_caches[i]->name = s;
  3228. }
  3229. #ifdef CONFIG_SMP
  3230. register_cpu_notifier(&slab_notifier);
  3231. #endif
  3232. #ifdef CONFIG_ZONE_DMA
  3233. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  3234. struct kmem_cache *s = kmalloc_caches[i];
  3235. if (s && s->size) {
  3236. char *name = kasprintf(GFP_NOWAIT,
  3237. "dma-kmalloc-%d", s->objsize);
  3238. BUG_ON(!name);
  3239. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  3240. s->objsize, SLAB_CACHE_DMA);
  3241. }
  3242. }
  3243. #endif
  3244. printk(KERN_INFO
  3245. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  3246. " CPUs=%d, Nodes=%d\n",
  3247. caches, cache_line_size(),
  3248. slub_min_order, slub_max_order, slub_min_objects,
  3249. nr_cpu_ids, nr_node_ids);
  3250. }
  3251. void __init kmem_cache_init_late(void)
  3252. {
  3253. }
  3254. /*
  3255. * Find a mergeable slab cache
  3256. */
  3257. static int slab_unmergeable(struct kmem_cache *s)
  3258. {
  3259. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  3260. return 1;
  3261. if (s->ctor)
  3262. return 1;
  3263. /*
  3264. * We may have set a slab to be unmergeable during bootstrap.
  3265. */
  3266. if (s->refcount < 0)
  3267. return 1;
  3268. return 0;
  3269. }
  3270. static struct kmem_cache *find_mergeable(size_t size,
  3271. size_t align, unsigned long flags, const char *name,
  3272. void (*ctor)(void *))
  3273. {
  3274. struct kmem_cache *s;
  3275. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  3276. return NULL;
  3277. if (ctor)
  3278. return NULL;
  3279. size = ALIGN(size, sizeof(void *));
  3280. align = calculate_alignment(flags, align, size);
  3281. size = ALIGN(size, align);
  3282. flags = kmem_cache_flags(size, flags, name, NULL);
  3283. list_for_each_entry(s, &slab_caches, list) {
  3284. if (slab_unmergeable(s))
  3285. continue;
  3286. if (size > s->size)
  3287. continue;
  3288. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  3289. continue;
  3290. /*
  3291. * Check if alignment is compatible.
  3292. * Courtesy of Adrian Drzewiecki
  3293. */
  3294. if ((s->size & ~(align - 1)) != s->size)
  3295. continue;
  3296. if (s->size - size >= sizeof(void *))
  3297. continue;
  3298. return s;
  3299. }
  3300. return NULL;
  3301. }
  3302. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  3303. size_t align, unsigned long flags, void (*ctor)(void *))
  3304. {
  3305. struct kmem_cache *s;
  3306. char *n;
  3307. if (WARN_ON(!name))
  3308. return NULL;
  3309. down_write(&slub_lock);
  3310. s = find_mergeable(size, align, flags, name, ctor);
  3311. if (s) {
  3312. s->refcount++;
  3313. /*
  3314. * Adjust the object sizes so that we clear
  3315. * the complete object on kzalloc.
  3316. */
  3317. s->objsize = max(s->objsize, (int)size);
  3318. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  3319. if (sysfs_slab_alias(s, name)) {
  3320. s->refcount--;
  3321. goto err;
  3322. }
  3323. up_write(&slub_lock);
  3324. return s;
  3325. }
  3326. n = kstrdup(name, GFP_KERNEL);
  3327. if (!n)
  3328. goto err;
  3329. s = kmalloc(kmem_size, GFP_KERNEL);
  3330. if (s) {
  3331. if (kmem_cache_open(s, n,
  3332. size, align, flags, ctor)) {
  3333. list_add(&s->list, &slab_caches);
  3334. up_write(&slub_lock);
  3335. if (sysfs_slab_add(s)) {
  3336. down_write(&slub_lock);
  3337. list_del(&s->list);
  3338. kfree(n);
  3339. kfree(s);
  3340. goto err;
  3341. }
  3342. return s;
  3343. }
  3344. kfree(n);
  3345. kfree(s);
  3346. }
  3347. err:
  3348. up_write(&slub_lock);
  3349. if (flags & SLAB_PANIC)
  3350. panic("Cannot create slabcache %s\n", name);
  3351. else
  3352. s = NULL;
  3353. return s;
  3354. }
  3355. EXPORT_SYMBOL(kmem_cache_create);
  3356. #ifdef CONFIG_SMP
  3357. /*
  3358. * Use the cpu notifier to insure that the cpu slabs are flushed when
  3359. * necessary.
  3360. */
  3361. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  3362. unsigned long action, void *hcpu)
  3363. {
  3364. long cpu = (long)hcpu;
  3365. struct kmem_cache *s;
  3366. unsigned long flags;
  3367. switch (action) {
  3368. case CPU_UP_CANCELED:
  3369. case CPU_UP_CANCELED_FROZEN:
  3370. case CPU_DEAD:
  3371. case CPU_DEAD_FROZEN:
  3372. down_read(&slub_lock);
  3373. list_for_each_entry(s, &slab_caches, list) {
  3374. local_irq_save(flags);
  3375. __flush_cpu_slab(s, cpu);
  3376. local_irq_restore(flags);
  3377. }
  3378. up_read(&slub_lock);
  3379. break;
  3380. default:
  3381. break;
  3382. }
  3383. return NOTIFY_OK;
  3384. }
  3385. static struct notifier_block __cpuinitdata slab_notifier = {
  3386. .notifier_call = slab_cpuup_callback
  3387. };
  3388. #endif
  3389. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3390. {
  3391. struct kmem_cache *s;
  3392. void *ret;
  3393. if (unlikely(size > SLUB_MAX_SIZE))
  3394. return kmalloc_large(size, gfpflags);
  3395. s = get_slab(size, gfpflags);
  3396. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3397. return s;
  3398. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  3399. /* Honor the call site pointer we received. */
  3400. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3401. return ret;
  3402. }
  3403. #ifdef CONFIG_NUMA
  3404. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3405. int node, unsigned long caller)
  3406. {
  3407. struct kmem_cache *s;
  3408. void *ret;
  3409. if (unlikely(size > SLUB_MAX_SIZE)) {
  3410. ret = kmalloc_large_node(size, gfpflags, node);
  3411. trace_kmalloc_node(caller, ret,
  3412. size, PAGE_SIZE << get_order(size),
  3413. gfpflags, node);
  3414. return ret;
  3415. }
  3416. s = get_slab(size, gfpflags);
  3417. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3418. return s;
  3419. ret = slab_alloc(s, gfpflags, node, caller);
  3420. /* Honor the call site pointer we received. */
  3421. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3422. return ret;
  3423. }
  3424. #endif
  3425. #ifdef CONFIG_SYSFS
  3426. static int count_inuse(struct page *page)
  3427. {
  3428. return page->inuse;
  3429. }
  3430. static int count_total(struct page *page)
  3431. {
  3432. return page->objects;
  3433. }
  3434. #endif
  3435. #ifdef CONFIG_SLUB_DEBUG
  3436. static int validate_slab(struct kmem_cache *s, struct page *page,
  3437. unsigned long *map)
  3438. {
  3439. void *p;
  3440. void *addr = page_address(page);
  3441. if (!check_slab(s, page) ||
  3442. !on_freelist(s, page, NULL))
  3443. return 0;
  3444. /* Now we know that a valid freelist exists */
  3445. bitmap_zero(map, page->objects);
  3446. get_map(s, page, map);
  3447. for_each_object(p, s, addr, page->objects) {
  3448. if (test_bit(slab_index(p, s, addr), map))
  3449. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3450. return 0;
  3451. }
  3452. for_each_object(p, s, addr, page->objects)
  3453. if (!test_bit(slab_index(p, s, addr), map))
  3454. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3455. return 0;
  3456. return 1;
  3457. }
  3458. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3459. unsigned long *map)
  3460. {
  3461. slab_lock(page);
  3462. validate_slab(s, page, map);
  3463. slab_unlock(page);
  3464. }
  3465. static int validate_slab_node(struct kmem_cache *s,
  3466. struct kmem_cache_node *n, unsigned long *map)
  3467. {
  3468. unsigned long count = 0;
  3469. struct page *page;
  3470. unsigned long flags;
  3471. spin_lock_irqsave(&n->list_lock, flags);
  3472. list_for_each_entry(page, &n->partial, lru) {
  3473. validate_slab_slab(s, page, map);
  3474. count++;
  3475. }
  3476. if (count != n->nr_partial)
  3477. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3478. "counter=%ld\n", s->name, count, n->nr_partial);
  3479. if (!(s->flags & SLAB_STORE_USER))
  3480. goto out;
  3481. list_for_each_entry(page, &n->full, lru) {
  3482. validate_slab_slab(s, page, map);
  3483. count++;
  3484. }
  3485. if (count != atomic_long_read(&n->nr_slabs))
  3486. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3487. "counter=%ld\n", s->name, count,
  3488. atomic_long_read(&n->nr_slabs));
  3489. out:
  3490. spin_unlock_irqrestore(&n->list_lock, flags);
  3491. return count;
  3492. }
  3493. static long validate_slab_cache(struct kmem_cache *s)
  3494. {
  3495. int node;
  3496. unsigned long count = 0;
  3497. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3498. sizeof(unsigned long), GFP_KERNEL);
  3499. if (!map)
  3500. return -ENOMEM;
  3501. flush_all(s);
  3502. for_each_node_state(node, N_NORMAL_MEMORY) {
  3503. struct kmem_cache_node *n = get_node(s, node);
  3504. count += validate_slab_node(s, n, map);
  3505. }
  3506. kfree(map);
  3507. return count;
  3508. }
  3509. /*
  3510. * Generate lists of code addresses where slabcache objects are allocated
  3511. * and freed.
  3512. */
  3513. struct location {
  3514. unsigned long count;
  3515. unsigned long addr;
  3516. long long sum_time;
  3517. long min_time;
  3518. long max_time;
  3519. long min_pid;
  3520. long max_pid;
  3521. DECLARE_BITMAP(cpus, NR_CPUS);
  3522. nodemask_t nodes;
  3523. };
  3524. struct loc_track {
  3525. unsigned long max;
  3526. unsigned long count;
  3527. struct location *loc;
  3528. };
  3529. static void free_loc_track(struct loc_track *t)
  3530. {
  3531. if (t->max)
  3532. free_pages((unsigned long)t->loc,
  3533. get_order(sizeof(struct location) * t->max));
  3534. }
  3535. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3536. {
  3537. struct location *l;
  3538. int order;
  3539. order = get_order(sizeof(struct location) * max);
  3540. l = (void *)__get_free_pages(flags, order);
  3541. if (!l)
  3542. return 0;
  3543. if (t->count) {
  3544. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3545. free_loc_track(t);
  3546. }
  3547. t->max = max;
  3548. t->loc = l;
  3549. return 1;
  3550. }
  3551. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3552. const struct track *track)
  3553. {
  3554. long start, end, pos;
  3555. struct location *l;
  3556. unsigned long caddr;
  3557. unsigned long age = jiffies - track->when;
  3558. start = -1;
  3559. end = t->count;
  3560. for ( ; ; ) {
  3561. pos = start + (end - start + 1) / 2;
  3562. /*
  3563. * There is nothing at "end". If we end up there
  3564. * we need to add something to before end.
  3565. */
  3566. if (pos == end)
  3567. break;
  3568. caddr = t->loc[pos].addr;
  3569. if (track->addr == caddr) {
  3570. l = &t->loc[pos];
  3571. l->count++;
  3572. if (track->when) {
  3573. l->sum_time += age;
  3574. if (age < l->min_time)
  3575. l->min_time = age;
  3576. if (age > l->max_time)
  3577. l->max_time = age;
  3578. if (track->pid < l->min_pid)
  3579. l->min_pid = track->pid;
  3580. if (track->pid > l->max_pid)
  3581. l->max_pid = track->pid;
  3582. cpumask_set_cpu(track->cpu,
  3583. to_cpumask(l->cpus));
  3584. }
  3585. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3586. return 1;
  3587. }
  3588. if (track->addr < caddr)
  3589. end = pos;
  3590. else
  3591. start = pos;
  3592. }
  3593. /*
  3594. * Not found. Insert new tracking element.
  3595. */
  3596. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3597. return 0;
  3598. l = t->loc + pos;
  3599. if (pos < t->count)
  3600. memmove(l + 1, l,
  3601. (t->count - pos) * sizeof(struct location));
  3602. t->count++;
  3603. l->count = 1;
  3604. l->addr = track->addr;
  3605. l->sum_time = age;
  3606. l->min_time = age;
  3607. l->max_time = age;
  3608. l->min_pid = track->pid;
  3609. l->max_pid = track->pid;
  3610. cpumask_clear(to_cpumask(l->cpus));
  3611. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3612. nodes_clear(l->nodes);
  3613. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3614. return 1;
  3615. }
  3616. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3617. struct page *page, enum track_item alloc,
  3618. unsigned long *map)
  3619. {
  3620. void *addr = page_address(page);
  3621. void *p;
  3622. bitmap_zero(map, page->objects);
  3623. get_map(s, page, map);
  3624. for_each_object(p, s, addr, page->objects)
  3625. if (!test_bit(slab_index(p, s, addr), map))
  3626. add_location(t, s, get_track(s, p, alloc));
  3627. }
  3628. static int list_locations(struct kmem_cache *s, char *buf,
  3629. enum track_item alloc)
  3630. {
  3631. int len = 0;
  3632. unsigned long i;
  3633. struct loc_track t = { 0, 0, NULL };
  3634. int node;
  3635. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3636. sizeof(unsigned long), GFP_KERNEL);
  3637. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3638. GFP_TEMPORARY)) {
  3639. kfree(map);
  3640. return sprintf(buf, "Out of memory\n");
  3641. }
  3642. /* Push back cpu slabs */
  3643. flush_all(s);
  3644. for_each_node_state(node, N_NORMAL_MEMORY) {
  3645. struct kmem_cache_node *n = get_node(s, node);
  3646. unsigned long flags;
  3647. struct page *page;
  3648. if (!atomic_long_read(&n->nr_slabs))
  3649. continue;
  3650. spin_lock_irqsave(&n->list_lock, flags);
  3651. list_for_each_entry(page, &n->partial, lru)
  3652. process_slab(&t, s, page, alloc, map);
  3653. list_for_each_entry(page, &n->full, lru)
  3654. process_slab(&t, s, page, alloc, map);
  3655. spin_unlock_irqrestore(&n->list_lock, flags);
  3656. }
  3657. for (i = 0; i < t.count; i++) {
  3658. struct location *l = &t.loc[i];
  3659. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3660. break;
  3661. len += sprintf(buf + len, "%7ld ", l->count);
  3662. if (l->addr)
  3663. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3664. else
  3665. len += sprintf(buf + len, "<not-available>");
  3666. if (l->sum_time != l->min_time) {
  3667. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3668. l->min_time,
  3669. (long)div_u64(l->sum_time, l->count),
  3670. l->max_time);
  3671. } else
  3672. len += sprintf(buf + len, " age=%ld",
  3673. l->min_time);
  3674. if (l->min_pid != l->max_pid)
  3675. len += sprintf(buf + len, " pid=%ld-%ld",
  3676. l->min_pid, l->max_pid);
  3677. else
  3678. len += sprintf(buf + len, " pid=%ld",
  3679. l->min_pid);
  3680. if (num_online_cpus() > 1 &&
  3681. !cpumask_empty(to_cpumask(l->cpus)) &&
  3682. len < PAGE_SIZE - 60) {
  3683. len += sprintf(buf + len, " cpus=");
  3684. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3685. to_cpumask(l->cpus));
  3686. }
  3687. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3688. len < PAGE_SIZE - 60) {
  3689. len += sprintf(buf + len, " nodes=");
  3690. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3691. l->nodes);
  3692. }
  3693. len += sprintf(buf + len, "\n");
  3694. }
  3695. free_loc_track(&t);
  3696. kfree(map);
  3697. if (!t.count)
  3698. len += sprintf(buf, "No data\n");
  3699. return len;
  3700. }
  3701. #endif
  3702. #ifdef SLUB_RESILIENCY_TEST
  3703. static void resiliency_test(void)
  3704. {
  3705. u8 *p;
  3706. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3707. printk(KERN_ERR "SLUB resiliency testing\n");
  3708. printk(KERN_ERR "-----------------------\n");
  3709. printk(KERN_ERR "A. Corruption after allocation\n");
  3710. p = kzalloc(16, GFP_KERNEL);
  3711. p[16] = 0x12;
  3712. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3713. " 0x12->0x%p\n\n", p + 16);
  3714. validate_slab_cache(kmalloc_caches[4]);
  3715. /* Hmmm... The next two are dangerous */
  3716. p = kzalloc(32, GFP_KERNEL);
  3717. p[32 + sizeof(void *)] = 0x34;
  3718. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3719. " 0x34 -> -0x%p\n", p);
  3720. printk(KERN_ERR
  3721. "If allocated object is overwritten then not detectable\n\n");
  3722. validate_slab_cache(kmalloc_caches[5]);
  3723. p = kzalloc(64, GFP_KERNEL);
  3724. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3725. *p = 0x56;
  3726. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3727. p);
  3728. printk(KERN_ERR
  3729. "If allocated object is overwritten then not detectable\n\n");
  3730. validate_slab_cache(kmalloc_caches[6]);
  3731. printk(KERN_ERR "\nB. Corruption after free\n");
  3732. p = kzalloc(128, GFP_KERNEL);
  3733. kfree(p);
  3734. *p = 0x78;
  3735. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3736. validate_slab_cache(kmalloc_caches[7]);
  3737. p = kzalloc(256, GFP_KERNEL);
  3738. kfree(p);
  3739. p[50] = 0x9a;
  3740. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3741. p);
  3742. validate_slab_cache(kmalloc_caches[8]);
  3743. p = kzalloc(512, GFP_KERNEL);
  3744. kfree(p);
  3745. p[512] = 0xab;
  3746. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3747. validate_slab_cache(kmalloc_caches[9]);
  3748. }
  3749. #else
  3750. #ifdef CONFIG_SYSFS
  3751. static void resiliency_test(void) {};
  3752. #endif
  3753. #endif
  3754. #ifdef CONFIG_SYSFS
  3755. enum slab_stat_type {
  3756. SL_ALL, /* All slabs */
  3757. SL_PARTIAL, /* Only partially allocated slabs */
  3758. SL_CPU, /* Only slabs used for cpu caches */
  3759. SL_OBJECTS, /* Determine allocated objects not slabs */
  3760. SL_TOTAL /* Determine object capacity not slabs */
  3761. };
  3762. #define SO_ALL (1 << SL_ALL)
  3763. #define SO_PARTIAL (1 << SL_PARTIAL)
  3764. #define SO_CPU (1 << SL_CPU)
  3765. #define SO_OBJECTS (1 << SL_OBJECTS)
  3766. #define SO_TOTAL (1 << SL_TOTAL)
  3767. static ssize_t show_slab_objects(struct kmem_cache *s,
  3768. char *buf, unsigned long flags)
  3769. {
  3770. unsigned long total = 0;
  3771. int node;
  3772. int x;
  3773. unsigned long *nodes;
  3774. unsigned long *per_cpu;
  3775. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3776. if (!nodes)
  3777. return -ENOMEM;
  3778. per_cpu = nodes + nr_node_ids;
  3779. if (flags & SO_CPU) {
  3780. int cpu;
  3781. for_each_possible_cpu(cpu) {
  3782. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3783. int node = ACCESS_ONCE(c->node);
  3784. struct page *page;
  3785. if (node < 0)
  3786. continue;
  3787. page = ACCESS_ONCE(c->page);
  3788. if (page) {
  3789. if (flags & SO_TOTAL)
  3790. x = page->objects;
  3791. else if (flags & SO_OBJECTS)
  3792. x = page->inuse;
  3793. else
  3794. x = 1;
  3795. total += x;
  3796. nodes[node] += x;
  3797. }
  3798. page = c->partial;
  3799. if (page) {
  3800. x = page->pobjects;
  3801. total += x;
  3802. nodes[node] += x;
  3803. }
  3804. per_cpu[node]++;
  3805. }
  3806. }
  3807. lock_memory_hotplug();
  3808. #ifdef CONFIG_SLUB_DEBUG
  3809. if (flags & SO_ALL) {
  3810. for_each_node_state(node, N_NORMAL_MEMORY) {
  3811. struct kmem_cache_node *n = get_node(s, node);
  3812. if (flags & SO_TOTAL)
  3813. x = atomic_long_read(&n->total_objects);
  3814. else if (flags & SO_OBJECTS)
  3815. x = atomic_long_read(&n->total_objects) -
  3816. count_partial(n, count_free);
  3817. else
  3818. x = atomic_long_read(&n->nr_slabs);
  3819. total += x;
  3820. nodes[node] += x;
  3821. }
  3822. } else
  3823. #endif
  3824. if (flags & SO_PARTIAL) {
  3825. for_each_node_state(node, N_NORMAL_MEMORY) {
  3826. struct kmem_cache_node *n = get_node(s, node);
  3827. if (flags & SO_TOTAL)
  3828. x = count_partial(n, count_total);
  3829. else if (flags & SO_OBJECTS)
  3830. x = count_partial(n, count_inuse);
  3831. else
  3832. x = n->nr_partial;
  3833. total += x;
  3834. nodes[node] += x;
  3835. }
  3836. }
  3837. x = sprintf(buf, "%lu", total);
  3838. #ifdef CONFIG_NUMA
  3839. for_each_node_state(node, N_NORMAL_MEMORY)
  3840. if (nodes[node])
  3841. x += sprintf(buf + x, " N%d=%lu",
  3842. node, nodes[node]);
  3843. #endif
  3844. unlock_memory_hotplug();
  3845. kfree(nodes);
  3846. return x + sprintf(buf + x, "\n");
  3847. }
  3848. #ifdef CONFIG_SLUB_DEBUG
  3849. static int any_slab_objects(struct kmem_cache *s)
  3850. {
  3851. int node;
  3852. for_each_online_node(node) {
  3853. struct kmem_cache_node *n = get_node(s, node);
  3854. if (!n)
  3855. continue;
  3856. if (atomic_long_read(&n->total_objects))
  3857. return 1;
  3858. }
  3859. return 0;
  3860. }
  3861. #endif
  3862. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3863. #define to_slab(n) container_of(n, struct kmem_cache, kobj)
  3864. struct slab_attribute {
  3865. struct attribute attr;
  3866. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3867. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3868. };
  3869. #define SLAB_ATTR_RO(_name) \
  3870. static struct slab_attribute _name##_attr = \
  3871. __ATTR(_name, 0400, _name##_show, NULL)
  3872. #define SLAB_ATTR(_name) \
  3873. static struct slab_attribute _name##_attr = \
  3874. __ATTR(_name, 0600, _name##_show, _name##_store)
  3875. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3876. {
  3877. return sprintf(buf, "%d\n", s->size);
  3878. }
  3879. SLAB_ATTR_RO(slab_size);
  3880. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3881. {
  3882. return sprintf(buf, "%d\n", s->align);
  3883. }
  3884. SLAB_ATTR_RO(align);
  3885. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3886. {
  3887. return sprintf(buf, "%d\n", s->objsize);
  3888. }
  3889. SLAB_ATTR_RO(object_size);
  3890. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3891. {
  3892. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3893. }
  3894. SLAB_ATTR_RO(objs_per_slab);
  3895. static ssize_t order_store(struct kmem_cache *s,
  3896. const char *buf, size_t length)
  3897. {
  3898. unsigned long order;
  3899. int err;
  3900. err = strict_strtoul(buf, 10, &order);
  3901. if (err)
  3902. return err;
  3903. if (order > slub_max_order || order < slub_min_order)
  3904. return -EINVAL;
  3905. calculate_sizes(s, order);
  3906. return length;
  3907. }
  3908. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3909. {
  3910. return sprintf(buf, "%d\n", oo_order(s->oo));
  3911. }
  3912. SLAB_ATTR(order);
  3913. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3914. {
  3915. return sprintf(buf, "%lu\n", s->min_partial);
  3916. }
  3917. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3918. size_t length)
  3919. {
  3920. unsigned long min;
  3921. int err;
  3922. err = strict_strtoul(buf, 10, &min);
  3923. if (err)
  3924. return err;
  3925. set_min_partial(s, min);
  3926. return length;
  3927. }
  3928. SLAB_ATTR(min_partial);
  3929. static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
  3930. {
  3931. return sprintf(buf, "%u\n", s->cpu_partial);
  3932. }
  3933. static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
  3934. size_t length)
  3935. {
  3936. unsigned long objects;
  3937. int err;
  3938. err = strict_strtoul(buf, 10, &objects);
  3939. if (err)
  3940. return err;
  3941. if (objects && kmem_cache_debug(s))
  3942. return -EINVAL;
  3943. s->cpu_partial = objects;
  3944. flush_all(s);
  3945. return length;
  3946. }
  3947. SLAB_ATTR(cpu_partial);
  3948. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3949. {
  3950. if (!s->ctor)
  3951. return 0;
  3952. return sprintf(buf, "%pS\n", s->ctor);
  3953. }
  3954. SLAB_ATTR_RO(ctor);
  3955. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3956. {
  3957. return sprintf(buf, "%d\n", s->refcount - 1);
  3958. }
  3959. SLAB_ATTR_RO(aliases);
  3960. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3961. {
  3962. return show_slab_objects(s, buf, SO_PARTIAL);
  3963. }
  3964. SLAB_ATTR_RO(partial);
  3965. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3966. {
  3967. return show_slab_objects(s, buf, SO_CPU);
  3968. }
  3969. SLAB_ATTR_RO(cpu_slabs);
  3970. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3971. {
  3972. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3973. }
  3974. SLAB_ATTR_RO(objects);
  3975. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3976. {
  3977. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3978. }
  3979. SLAB_ATTR_RO(objects_partial);
  3980. static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
  3981. {
  3982. int objects = 0;
  3983. int pages = 0;
  3984. int cpu;
  3985. int len;
  3986. for_each_online_cpu(cpu) {
  3987. struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
  3988. if (page) {
  3989. pages += page->pages;
  3990. objects += page->pobjects;
  3991. }
  3992. }
  3993. len = sprintf(buf, "%d(%d)", objects, pages);
  3994. #ifdef CONFIG_SMP
  3995. for_each_online_cpu(cpu) {
  3996. struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
  3997. if (page && len < PAGE_SIZE - 20)
  3998. len += sprintf(buf + len, " C%d=%d(%d)", cpu,
  3999. page->pobjects, page->pages);
  4000. }
  4001. #endif
  4002. return len + sprintf(buf + len, "\n");
  4003. }
  4004. SLAB_ATTR_RO(slabs_cpu_partial);
  4005. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  4006. {
  4007. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  4008. }
  4009. static ssize_t reclaim_account_store(struct kmem_cache *s,
  4010. const char *buf, size_t length)
  4011. {
  4012. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  4013. if (buf[0] == '1')
  4014. s->flags |= SLAB_RECLAIM_ACCOUNT;
  4015. return length;
  4016. }
  4017. SLAB_ATTR(reclaim_account);
  4018. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  4019. {
  4020. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  4021. }
  4022. SLAB_ATTR_RO(hwcache_align);
  4023. #ifdef CONFIG_ZONE_DMA
  4024. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  4025. {
  4026. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  4027. }
  4028. SLAB_ATTR_RO(cache_dma);
  4029. #endif
  4030. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  4031. {
  4032. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  4033. }
  4034. SLAB_ATTR_RO(destroy_by_rcu);
  4035. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  4036. {
  4037. return sprintf(buf, "%d\n", s->reserved);
  4038. }
  4039. SLAB_ATTR_RO(reserved);
  4040. #ifdef CONFIG_SLUB_DEBUG
  4041. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  4042. {
  4043. return show_slab_objects(s, buf, SO_ALL);
  4044. }
  4045. SLAB_ATTR_RO(slabs);
  4046. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  4047. {
  4048. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  4049. }
  4050. SLAB_ATTR_RO(total_objects);
  4051. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  4052. {
  4053. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  4054. }
  4055. static ssize_t sanity_checks_store(struct kmem_cache *s,
  4056. const char *buf, size_t length)
  4057. {
  4058. s->flags &= ~SLAB_DEBUG_FREE;
  4059. if (buf[0] == '1') {
  4060. s->flags &= ~__CMPXCHG_DOUBLE;
  4061. s->flags |= SLAB_DEBUG_FREE;
  4062. }
  4063. return length;
  4064. }
  4065. SLAB_ATTR(sanity_checks);
  4066. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  4067. {
  4068. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  4069. }
  4070. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  4071. size_t length)
  4072. {
  4073. s->flags &= ~SLAB_TRACE;
  4074. if (buf[0] == '1') {
  4075. s->flags &= ~__CMPXCHG_DOUBLE;
  4076. s->flags |= SLAB_TRACE;
  4077. }
  4078. return length;
  4079. }
  4080. SLAB_ATTR(trace);
  4081. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  4082. {
  4083. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  4084. }
  4085. static ssize_t red_zone_store(struct kmem_cache *s,
  4086. const char *buf, size_t length)
  4087. {
  4088. if (any_slab_objects(s))
  4089. return -EBUSY;
  4090. s->flags &= ~SLAB_RED_ZONE;
  4091. if (buf[0] == '1') {
  4092. s->flags &= ~__CMPXCHG_DOUBLE;
  4093. s->flags |= SLAB_RED_ZONE;
  4094. }
  4095. calculate_sizes(s, -1);
  4096. return length;
  4097. }
  4098. SLAB_ATTR(red_zone);
  4099. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  4100. {
  4101. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  4102. }
  4103. static ssize_t poison_store(struct kmem_cache *s,
  4104. const char *buf, size_t length)
  4105. {
  4106. if (any_slab_objects(s))
  4107. return -EBUSY;
  4108. s->flags &= ~SLAB_POISON;
  4109. if (buf[0] == '1') {
  4110. s->flags &= ~__CMPXCHG_DOUBLE;
  4111. s->flags |= SLAB_POISON;
  4112. }
  4113. calculate_sizes(s, -1);
  4114. return length;
  4115. }
  4116. SLAB_ATTR(poison);
  4117. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  4118. {
  4119. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  4120. }
  4121. static ssize_t store_user_store(struct kmem_cache *s,
  4122. const char *buf, size_t length)
  4123. {
  4124. if (any_slab_objects(s))
  4125. return -EBUSY;
  4126. s->flags &= ~SLAB_STORE_USER;
  4127. if (buf[0] == '1') {
  4128. s->flags &= ~__CMPXCHG_DOUBLE;
  4129. s->flags |= SLAB_STORE_USER;
  4130. }
  4131. calculate_sizes(s, -1);
  4132. return length;
  4133. }
  4134. SLAB_ATTR(store_user);
  4135. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  4136. {
  4137. return 0;
  4138. }
  4139. static ssize_t validate_store(struct kmem_cache *s,
  4140. const char *buf, size_t length)
  4141. {
  4142. int ret = -EINVAL;
  4143. if (buf[0] == '1') {
  4144. ret = validate_slab_cache(s);
  4145. if (ret >= 0)
  4146. ret = length;
  4147. }
  4148. return ret;
  4149. }
  4150. SLAB_ATTR(validate);
  4151. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  4152. {
  4153. if (!(s->flags & SLAB_STORE_USER))
  4154. return -ENOSYS;
  4155. return list_locations(s, buf, TRACK_ALLOC);
  4156. }
  4157. SLAB_ATTR_RO(alloc_calls);
  4158. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  4159. {
  4160. if (!(s->flags & SLAB_STORE_USER))
  4161. return -ENOSYS;
  4162. return list_locations(s, buf, TRACK_FREE);
  4163. }
  4164. SLAB_ATTR_RO(free_calls);
  4165. #endif /* CONFIG_SLUB_DEBUG */
  4166. #ifdef CONFIG_FAILSLAB
  4167. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  4168. {
  4169. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  4170. }
  4171. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  4172. size_t length)
  4173. {
  4174. s->flags &= ~SLAB_FAILSLAB;
  4175. if (buf[0] == '1')
  4176. s->flags |= SLAB_FAILSLAB;
  4177. return length;
  4178. }
  4179. SLAB_ATTR(failslab);
  4180. #endif
  4181. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  4182. {
  4183. return 0;
  4184. }
  4185. static ssize_t shrink_store(struct kmem_cache *s,
  4186. const char *buf, size_t length)
  4187. {
  4188. if (buf[0] == '1') {
  4189. int rc = kmem_cache_shrink(s);
  4190. if (rc)
  4191. return rc;
  4192. } else
  4193. return -EINVAL;
  4194. return length;
  4195. }
  4196. SLAB_ATTR(shrink);
  4197. #ifdef CONFIG_NUMA
  4198. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  4199. {
  4200. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  4201. }
  4202. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  4203. const char *buf, size_t length)
  4204. {
  4205. unsigned long ratio;
  4206. int err;
  4207. err = strict_strtoul(buf, 10, &ratio);
  4208. if (err)
  4209. return err;
  4210. if (ratio <= 100)
  4211. s->remote_node_defrag_ratio = ratio * 10;
  4212. return length;
  4213. }
  4214. SLAB_ATTR(remote_node_defrag_ratio);
  4215. #endif
  4216. #ifdef CONFIG_SLUB_STATS
  4217. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  4218. {
  4219. unsigned long sum = 0;
  4220. int cpu;
  4221. int len;
  4222. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  4223. if (!data)
  4224. return -ENOMEM;
  4225. for_each_online_cpu(cpu) {
  4226. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  4227. data[cpu] = x;
  4228. sum += x;
  4229. }
  4230. len = sprintf(buf, "%lu", sum);
  4231. #ifdef CONFIG_SMP
  4232. for_each_online_cpu(cpu) {
  4233. if (data[cpu] && len < PAGE_SIZE - 20)
  4234. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  4235. }
  4236. #endif
  4237. kfree(data);
  4238. return len + sprintf(buf + len, "\n");
  4239. }
  4240. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  4241. {
  4242. int cpu;
  4243. for_each_online_cpu(cpu)
  4244. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  4245. }
  4246. #define STAT_ATTR(si, text) \
  4247. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  4248. { \
  4249. return show_stat(s, buf, si); \
  4250. } \
  4251. static ssize_t text##_store(struct kmem_cache *s, \
  4252. const char *buf, size_t length) \
  4253. { \
  4254. if (buf[0] != '0') \
  4255. return -EINVAL; \
  4256. clear_stat(s, si); \
  4257. return length; \
  4258. } \
  4259. SLAB_ATTR(text); \
  4260. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  4261. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  4262. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  4263. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  4264. STAT_ATTR(FREE_FROZEN, free_frozen);
  4265. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  4266. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  4267. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  4268. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  4269. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  4270. STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
  4271. STAT_ATTR(FREE_SLAB, free_slab);
  4272. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  4273. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  4274. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  4275. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  4276. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  4277. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  4278. STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
  4279. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  4280. STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
  4281. STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
  4282. STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
  4283. STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
  4284. STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
  4285. STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
  4286. #endif
  4287. static struct attribute *slab_attrs[] = {
  4288. &slab_size_attr.attr,
  4289. &object_size_attr.attr,
  4290. &objs_per_slab_attr.attr,
  4291. &order_attr.attr,
  4292. &min_partial_attr.attr,
  4293. &cpu_partial_attr.attr,
  4294. &objects_attr.attr,
  4295. &objects_partial_attr.attr,
  4296. &partial_attr.attr,
  4297. &cpu_slabs_attr.attr,
  4298. &ctor_attr.attr,
  4299. &aliases_attr.attr,
  4300. &align_attr.attr,
  4301. &hwcache_align_attr.attr,
  4302. &reclaim_account_attr.attr,
  4303. &destroy_by_rcu_attr.attr,
  4304. &shrink_attr.attr,
  4305. &reserved_attr.attr,
  4306. &slabs_cpu_partial_attr.attr,
  4307. #ifdef CONFIG_SLUB_DEBUG
  4308. &total_objects_attr.attr,
  4309. &slabs_attr.attr,
  4310. &sanity_checks_attr.attr,
  4311. &trace_attr.attr,
  4312. &red_zone_attr.attr,
  4313. &poison_attr.attr,
  4314. &store_user_attr.attr,
  4315. &validate_attr.attr,
  4316. &alloc_calls_attr.attr,
  4317. &free_calls_attr.attr,
  4318. #endif
  4319. #ifdef CONFIG_ZONE_DMA
  4320. &cache_dma_attr.attr,
  4321. #endif
  4322. #ifdef CONFIG_NUMA
  4323. &remote_node_defrag_ratio_attr.attr,
  4324. #endif
  4325. #ifdef CONFIG_SLUB_STATS
  4326. &alloc_fastpath_attr.attr,
  4327. &alloc_slowpath_attr.attr,
  4328. &free_fastpath_attr.attr,
  4329. &free_slowpath_attr.attr,
  4330. &free_frozen_attr.attr,
  4331. &free_add_partial_attr.attr,
  4332. &free_remove_partial_attr.attr,
  4333. &alloc_from_partial_attr.attr,
  4334. &alloc_slab_attr.attr,
  4335. &alloc_refill_attr.attr,
  4336. &alloc_node_mismatch_attr.attr,
  4337. &free_slab_attr.attr,
  4338. &cpuslab_flush_attr.attr,
  4339. &deactivate_full_attr.attr,
  4340. &deactivate_empty_attr.attr,
  4341. &deactivate_to_head_attr.attr,
  4342. &deactivate_to_tail_attr.attr,
  4343. &deactivate_remote_frees_attr.attr,
  4344. &deactivate_bypass_attr.attr,
  4345. &order_fallback_attr.attr,
  4346. &cmpxchg_double_fail_attr.attr,
  4347. &cmpxchg_double_cpu_fail_attr.attr,
  4348. &cpu_partial_alloc_attr.attr,
  4349. &cpu_partial_free_attr.attr,
  4350. &cpu_partial_node_attr.attr,
  4351. &cpu_partial_drain_attr.attr,
  4352. #endif
  4353. #ifdef CONFIG_FAILSLAB
  4354. &failslab_attr.attr,
  4355. #endif
  4356. NULL
  4357. };
  4358. static struct attribute_group slab_attr_group = {
  4359. .attrs = slab_attrs,
  4360. };
  4361. static ssize_t slab_attr_show(struct kobject *kobj,
  4362. struct attribute *attr,
  4363. char *buf)
  4364. {
  4365. struct slab_attribute *attribute;
  4366. struct kmem_cache *s;
  4367. int err;
  4368. attribute = to_slab_attr(attr);
  4369. s = to_slab(kobj);
  4370. if (!attribute->show)
  4371. return -EIO;
  4372. err = attribute->show(s, buf);
  4373. return err;
  4374. }
  4375. static ssize_t slab_attr_store(struct kobject *kobj,
  4376. struct attribute *attr,
  4377. const char *buf, size_t len)
  4378. {
  4379. struct slab_attribute *attribute;
  4380. struct kmem_cache *s;
  4381. int err;
  4382. attribute = to_slab_attr(attr);
  4383. s = to_slab(kobj);
  4384. if (!attribute->store)
  4385. return -EIO;
  4386. err = attribute->store(s, buf, len);
  4387. return err;
  4388. }
  4389. static void kmem_cache_release(struct kobject *kobj)
  4390. {
  4391. struct kmem_cache *s = to_slab(kobj);
  4392. kfree(s->name);
  4393. kfree(s);
  4394. }
  4395. static const struct sysfs_ops slab_sysfs_ops = {
  4396. .show = slab_attr_show,
  4397. .store = slab_attr_store,
  4398. };
  4399. static struct kobj_type slab_ktype = {
  4400. .sysfs_ops = &slab_sysfs_ops,
  4401. .release = kmem_cache_release
  4402. };
  4403. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  4404. {
  4405. struct kobj_type *ktype = get_ktype(kobj);
  4406. if (ktype == &slab_ktype)
  4407. return 1;
  4408. return 0;
  4409. }
  4410. static const struct kset_uevent_ops slab_uevent_ops = {
  4411. .filter = uevent_filter,
  4412. };
  4413. static struct kset *slab_kset;
  4414. #define ID_STR_LENGTH 64
  4415. /* Create a unique string id for a slab cache:
  4416. *
  4417. * Format :[flags-]size
  4418. */
  4419. static char *create_unique_id(struct kmem_cache *s)
  4420. {
  4421. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  4422. char *p = name;
  4423. BUG_ON(!name);
  4424. *p++ = ':';
  4425. /*
  4426. * First flags affecting slabcache operations. We will only
  4427. * get here for aliasable slabs so we do not need to support
  4428. * too many flags. The flags here must cover all flags that
  4429. * are matched during merging to guarantee that the id is
  4430. * unique.
  4431. */
  4432. if (s->flags & SLAB_CACHE_DMA)
  4433. *p++ = 'd';
  4434. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  4435. *p++ = 'a';
  4436. if (s->flags & SLAB_DEBUG_FREE)
  4437. *p++ = 'F';
  4438. if (!(s->flags & SLAB_NOTRACK))
  4439. *p++ = 't';
  4440. if (p != name + 1)
  4441. *p++ = '-';
  4442. p += sprintf(p, "%07d", s->size);
  4443. BUG_ON(p > name + ID_STR_LENGTH - 1);
  4444. return name;
  4445. }
  4446. static int sysfs_slab_add(struct kmem_cache *s)
  4447. {
  4448. int err;
  4449. const char *name;
  4450. int unmergeable;
  4451. if (slab_state < SYSFS)
  4452. /* Defer until later */
  4453. return 0;
  4454. unmergeable = slab_unmergeable(s);
  4455. if (unmergeable) {
  4456. /*
  4457. * Slabcache can never be merged so we can use the name proper.
  4458. * This is typically the case for debug situations. In that
  4459. * case we can catch duplicate names easily.
  4460. */
  4461. sysfs_remove_link(&slab_kset->kobj, s->name);
  4462. name = s->name;
  4463. } else {
  4464. /*
  4465. * Create a unique name for the slab as a target
  4466. * for the symlinks.
  4467. */
  4468. name = create_unique_id(s);
  4469. }
  4470. s->kobj.kset = slab_kset;
  4471. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  4472. if (err) {
  4473. kobject_put(&s->kobj);
  4474. return err;
  4475. }
  4476. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4477. if (err) {
  4478. kobject_del(&s->kobj);
  4479. kobject_put(&s->kobj);
  4480. return err;
  4481. }
  4482. kobject_uevent(&s->kobj, KOBJ_ADD);
  4483. if (!unmergeable) {
  4484. /* Setup first alias */
  4485. sysfs_slab_alias(s, s->name);
  4486. kfree(name);
  4487. }
  4488. return 0;
  4489. }
  4490. static void sysfs_slab_remove(struct kmem_cache *s)
  4491. {
  4492. if (slab_state < SYSFS)
  4493. /*
  4494. * Sysfs has not been setup yet so no need to remove the
  4495. * cache from sysfs.
  4496. */
  4497. return;
  4498. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4499. kobject_del(&s->kobj);
  4500. kobject_put(&s->kobj);
  4501. }
  4502. /*
  4503. * Need to buffer aliases during bootup until sysfs becomes
  4504. * available lest we lose that information.
  4505. */
  4506. struct saved_alias {
  4507. struct kmem_cache *s;
  4508. const char *name;
  4509. struct saved_alias *next;
  4510. };
  4511. static struct saved_alias *alias_list;
  4512. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4513. {
  4514. struct saved_alias *al;
  4515. if (slab_state == SYSFS) {
  4516. /*
  4517. * If we have a leftover link then remove it.
  4518. */
  4519. sysfs_remove_link(&slab_kset->kobj, name);
  4520. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4521. }
  4522. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4523. if (!al)
  4524. return -ENOMEM;
  4525. al->s = s;
  4526. al->name = name;
  4527. al->next = alias_list;
  4528. alias_list = al;
  4529. return 0;
  4530. }
  4531. static int __init slab_sysfs_init(void)
  4532. {
  4533. struct kmem_cache *s;
  4534. int err;
  4535. down_write(&slub_lock);
  4536. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4537. if (!slab_kset) {
  4538. up_write(&slub_lock);
  4539. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4540. return -ENOSYS;
  4541. }
  4542. slab_state = SYSFS;
  4543. list_for_each_entry(s, &slab_caches, list) {
  4544. err = sysfs_slab_add(s);
  4545. if (err)
  4546. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4547. " to sysfs\n", s->name);
  4548. }
  4549. while (alias_list) {
  4550. struct saved_alias *al = alias_list;
  4551. alias_list = alias_list->next;
  4552. err = sysfs_slab_alias(al->s, al->name);
  4553. if (err)
  4554. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4555. " %s to sysfs\n", s->name);
  4556. kfree(al);
  4557. }
  4558. up_write(&slub_lock);
  4559. resiliency_test();
  4560. return 0;
  4561. }
  4562. __initcall(slab_sysfs_init);
  4563. #endif /* CONFIG_SYSFS */
  4564. /*
  4565. * The /proc/slabinfo ABI
  4566. */
  4567. #ifdef CONFIG_SLABINFO
  4568. static void print_slabinfo_header(struct seq_file *m)
  4569. {
  4570. seq_puts(m, "slabinfo - version: 2.1\n");
  4571. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4572. "<objperslab> <pagesperslab>");
  4573. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4574. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4575. seq_putc(m, '\n');
  4576. }
  4577. static void *s_start(struct seq_file *m, loff_t *pos)
  4578. {
  4579. loff_t n = *pos;
  4580. down_read(&slub_lock);
  4581. if (!n)
  4582. print_slabinfo_header(m);
  4583. return seq_list_start(&slab_caches, *pos);
  4584. }
  4585. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4586. {
  4587. return seq_list_next(p, &slab_caches, pos);
  4588. }
  4589. static void s_stop(struct seq_file *m, void *p)
  4590. {
  4591. up_read(&slub_lock);
  4592. }
  4593. static int s_show(struct seq_file *m, void *p)
  4594. {
  4595. unsigned long nr_partials = 0;
  4596. unsigned long nr_slabs = 0;
  4597. unsigned long nr_inuse = 0;
  4598. unsigned long nr_objs = 0;
  4599. unsigned long nr_free = 0;
  4600. struct kmem_cache *s;
  4601. int node;
  4602. s = list_entry(p, struct kmem_cache, list);
  4603. for_each_online_node(node) {
  4604. struct kmem_cache_node *n = get_node(s, node);
  4605. if (!n)
  4606. continue;
  4607. nr_partials += n->nr_partial;
  4608. nr_slabs += atomic_long_read(&n->nr_slabs);
  4609. nr_objs += atomic_long_read(&n->total_objects);
  4610. nr_free += count_partial(n, count_free);
  4611. }
  4612. nr_inuse = nr_objs - nr_free;
  4613. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4614. nr_objs, s->size, oo_objects(s->oo),
  4615. (1 << oo_order(s->oo)));
  4616. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4617. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4618. 0UL);
  4619. seq_putc(m, '\n');
  4620. return 0;
  4621. }
  4622. static const struct seq_operations slabinfo_op = {
  4623. .start = s_start,
  4624. .next = s_next,
  4625. .stop = s_stop,
  4626. .show = s_show,
  4627. };
  4628. static int slabinfo_open(struct inode *inode, struct file *file)
  4629. {
  4630. return seq_open(file, &slabinfo_op);
  4631. }
  4632. static const struct file_operations proc_slabinfo_operations = {
  4633. .open = slabinfo_open,
  4634. .read = seq_read,
  4635. .llseek = seq_lseek,
  4636. .release = seq_release,
  4637. };
  4638. static int __init slab_proc_init(void)
  4639. {
  4640. proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
  4641. return 0;
  4642. }
  4643. module_init(slab_proc_init);
  4644. #endif /* CONFIG_SLABINFO */