slub.c 141 KB

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