slub.c 133 KB

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