slub.c 138 KB

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