slub.c 141 KB

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