Tải bản đầy đủ - 0 (trang)
Chapter 17.  Page Frame Reclaiming

Chapter 17.  Page Frame Reclaiming

Tải bản đầy đủ - 0trang

17.1.ThePageFrameReclaimingAlgorithm

OneofthefascinatingaspectsofLinuxisthatthechecks

performedbeforeallocatingdynamicmemorytoUserMode

processesortothekernelaresomewhatperfunctory.

Norigorouscheckismade,forinstance,onthetotalamountof

RAMassignedtotheprocessescreatedbyasingleuser(the

limitsmentionedinthesection"ProcessResourceLimits"in

Chapter3mostlyaffectsingleprocesses).Similarly,nolimitis

placedonthesizeofthemanydiskcachesandmemorycaches

usedbythekernel.

Thislackofcontrolsisadesignchoicethatallowsthekernelto

usetheavailableRAMinthebestpossibleway.Whenthe

systemloadislow,theRAMisfilledmostlybythediskcaches

andthefewrunningprocessescanbenefitfromtheinformation

storedinthem.However,whenthesystemloadincreases,the

RAMisfilledmostlybypagesoftheprocessesandthecaches

areshrunkentomakeroomforadditionalprocesses.

Aswesawinpreviouschapters,bothmemoryanddiskcaches

grabmoreandmorepageframesbutneverreleaseanyof

them.Thisisreasonablebecausecachesystemsdon'tknowif

andwhenprocesseswillreusesomeofthecacheddataandare

thereforeunabletoidentifytheportionsofcachethatshouldbe

released.Moreover,thankstothedemandpagingmechanism

describedinChapter9,UserModeprocessesgetpageframes

aslongastheyproceedwiththeirexecution;however,demand

paginghasnowaytoforceprocessestoreleasethepage

frameswhenevertheyarenolongerused.

Thus,soonerorlaterallthefreememorywillbeassignedto

processesandcaches.Thepageframereclaimingalgorithmof

theLinuxkernelrefillsthelistsoffreeblocksofthebuddy

systemby"stealing"pageframesfrombothUserMode



processesandkernelcaches.

Actually,pageframereclaimingmustbeperformedbeforeall

thefreememoryhasbeenusedup.Otherwise,thekernelmight

beeasilytrappedinadeadlychainofmemoryrequeststhat

leadstoasystemcrash.Essentially,tofreeapageframethe

kernelmustwriteitsdatatodisk;however,toaccomplishthis

operation,thekernelrequiresanotherpageframe(forinstance,

toallocatethebufferheadsfortheI/Odatatransfer).Ifnofree

pageframeexists,nopageframecanbefreed.

Oneofthegoalsofpageframereclaimingisthustoconservea

minimalpooloffreepageframessothatthekernelmaysafely

recoverfrom"lowonmemory"conditions.



17.1.1.SelectingaTargetPage

Theobjectiveofthepageframereclaimingalgorithm(PFRA)is

topickuppageframesandmakethemfree.Clearlythepage

framesselectedbythePFRAmustbenon-free,thatis,they

mustnotbealreadyincludedinoneofthefree_areaarraysused

bythebuddysystem(seethesection"TheBuddySystem

Algorithm"inChapter8).

ThePFRAhandlesthepageframesindifferentways,according

totheircontents.Wecandistinguishbetweenunreclaimable

pages,swappablepages,syncablepages,anddiscardable

pages.ThesetypesareexplainedinTable17-1.

Table17-1.ThetypesofpagesconsideredbythePFRA

Typeof

pages



Description



Freepages(includedinbuddysystem

lists)

Reservedpages(withPG_reservedflagset)



Reclaimaction



Pagesdynamicallyallocatedbythekernel

Unreclaimable PagesintheKernelModestacksofthe

processes



(Noreclaimingallowedorneeded)



Temporarilylockedpages(withPG_locked

flagset)

Memorylockedpages(inmemoryregions

withVM_LOCKEDflagset)

AnonymouspagesinUserModeaddress

spaces

Swappable

Mappedpagesoftmpfsfilesystem(e.g.,

pagesofIPCsharedmemory)



Savethepagecontentsinaswap

area



MappedpagesinUserModeaddress

spaces



Syncable



Pagesincludedinthepagecacheand

containingdataofdiskfiles



Synchronizethepagewithits

imageondisk,ifnecessary



Blockdevicebufferpages

Pagesofsomediskcaches(e.g.,the

inodecache)



Discardable



Unusedpagesincludedinmemorycaches

(e.g.,slaballocatorcaches)

Nothingtobedone

Unusedpagesofthedentrycache



Intheabovetable,apageissaidtobemappedifitmapsa

portionofafile.Forinstance,allpagesintheUserMode

addressspacesbelongingtofilememorymappingsare

mapped,aswellasanyotherpageincludedinthepagecache.

Inalmostallcases,mappedpagesaresyncable:inorderto

reclaimthepageframe,thekernelmustcheckwhetherthe

pageisdirtyand,ifnecessary,writethepagecontentsinthe

correspondingdiskfile.

Conversely,apageissaidtobeanonymousifitbelongstoan



anonymousmemoryregionofaprocess(forinstance,allpages

intheUserModeheaporstackofaprocessareanonymous).In

ordertoreclaimthepageframe,thekernelmustsavethepage

contentsinadedicateddiskpartitionordiskfilecalled"swap

area"(seethelatersection"Swapping");therefore,all

anonymouspagesareswappable.

Usually,thepagesofspecialfilesystemsarenotreclaimable.

Theonlyexceptionsarethepagesofthetmpfsspecial

filesystem,whichcanbereclaimedbysavingtheminaswap

area.Aswe'llseeinChapter19,thetmpfsspecialfilesystemis

usedbytheIPCsharedmemorymechanism.

WhenthePFRAmustreclaimapageframebelongingtothe

UserModeaddressspaceofaprocess,itmusttakeinto

considerationwhetherthepageframeissharedornon-shared.

AsharedpageframebelongstomultipleUserModeaddress

spaces,whileanon-sharedpageframebelongstojustone.

Noticethatanon-sharedpageframemightbelongtoseveral

lightweightprocessesreferringtothesamememorydescriptor.

Sharedpageframesaretypicallycreatedwhenaprocess

spawnsachild;asexplainedinthesection"CopyOnWrite"in

Chapter9,thepagetablesofthechildarecopiedfromthoseof

theparent,thusparentandchildsharethesamepageframes.

Anothercommoncaseoccurswhentwoormoreprocesses

accessthesamefilebymeansofasharedmemorymapping

(seethesection"MemoryMapping"inChapter16).[*]

[*]Itshouldbenoted,however,thatwhenasingleprocessaccessesafilethroughasharedmemorymapping,

thecorrespondingpagesarenon-sharedasfarasthePFRAisconcerned.Similarly,apagebelongingtoa

privatememorymappingmaybetreatedassharedbythePFRA(forinstance,becausetwoprocessesreadthe

samefileportionandnoneofthemmodifiedthedatainthepage).



17.1.2.DesignofthePFRA

Whileitiseasytoidentifythepagecandidatesformemory



reclaimingroughlyspeaking,anypagebelongingtoadiskor

memorycache,ortotheUserModeaddressspaceofa

processselectingthepropertargetpagesisperhapsthemost

sensitiveissueinkerneldesign.

Asamatteroffact,thehardestjobofadeveloperworkingon

thevirtualmemorysubsystemconsistsoffindinganalgorithm

thatensuresacceptableperformancebothfordesktopmachines

(onwhichmemoryrequestsarequitelimitedbutsystem

responsivenessiscrucial)andforhigh-levelmachinessuchas

largedatabaseservers(onwhichmemoryrequeststendtobe

huge).

Unfortunately,findingagoodpageframereclaimingalgorithm

isaratherempiricaljob,withverylittlesupportfromtheory.

Thesituationissomewhatsimilartoevaluatingthefactorsthat

determinethedynamicpriorityofaprocess:themainobjective

istotunetheparametersinsuchawaytoachievegoodsystem

performance,withoutaskingtoomanyquestionsaboutwhyit

workswell.Often,it'sjustamatterof"let'strythisapproach

andseewhathappens."Anunpleasantsideeffectofthis

empiricaldesignisthatthecodechangesquickly.Forthat

reason,wecannotensurethatthememoryreclaiming

algorithmwearegoingtodescribetheoneusedinLinux

2.6.11willbeexactlythesame,bythetimeyou'llreadthis

chapter,astheoneadoptedbythemostup-to-dateversionof

theLinux2.6kernel.However,thegeneralideasandthemain

heuristicrulesdescribedhereshouldcontinuetohold.

Lookingtooclosetothetrees'leavesmightleadustomissthe

wholeforest.Therefore,letuspresentafewgeneralrules

adoptedbythePFRA.Theserulesareembeddedinthe

functionsthatwillbedescribedlaterinthischapter.



Freethe"harmless"pagesfirst



Pagesincludedindiskandmemorycachesnotreferenced

byanyprocessshouldbereclaimedbeforepagesbelonging

totheUserModeaddressspacesoftheprocesses;inthe

formercase,infact,thepageframereclaimingcanbedone

withoutmodifyinganyPageTableentry.Aswewillseein

thesection"TheLeastRecentlyUsed(LRU)Lists"laterin

thischapter,thisruleissomewhatmitigatedbyintroducing

a"swaptendencyfactor."



MakeallpagesofaUserModeprocessreclaimable

Withtheexceptionoflockedpages,thePFRAmustbeable

tostealanypageofaUserModeprocess,includingthe

anonymouspages.Inthisway,processesthathavebeen

sleepingforalongperiodoftimewillprogressivelyloseall

theirpageframes.



Reclaimasharedpageframebyunmappingatonceallpage

tableentriesthatreferenceit

WhenthePFRAwantstofreeapageframesharedby

severalprocesses,itclearsallpagetableentriesthatrefer

tothesharedpageframe,andthenreclaimsthepage

frame.



Reclaim"unused"pagesonly

ThePFRAusesasimplifiedLeastRecentlyUsed(LRU)

replacementalgorithmtoclassifypagesasin-useand

unused.[*]Ifapagehasnotbeenaccessedforalongtime,

theprobabilitythatitwillbeaccessedinthenearfutureis

lowanditcanbeconsidered"unused;"ontheotherhand,if

apagehasbeenaccessedrecently,theprobabilitythatit



willcontinuetobeaccessedishighanditmustbe

consideredas"in-use."ThePFRAreclaimsonlyunused

pages.Thisisjustanotherapplicationofthelocality

principlementionedinthesection"HardwareCache"in

Chapter2.

[*]ThePFRAcouldalsobeconsideredasa"used-once"algorithm,whichhasitsrootsinthe

2QbuffermanagementreplacementalgorithmproposedbyT.JohnsonandD.Shashain

1994.



ThemainideabehindtheLRUalgorithmistoassociatea

counterstoringtheageofthepagewitheachpagein

RAMthatis,theintervaloftimeelapsedsincethelast

accesstothepage.ThiscounterallowsthePFRAtoreclaim

onlytheoldestpageofanyprocess.Somecomputer

platformsprovidesophisticatedsupportforLRUalgorithms;[

]

unfortunately,80x86processorsdonotoffersucha

hardwarefeature,thustheLinuxkernelcannotrelyona

pagecounterthatkeepstrackoftheageofeverypage.To

copewiththisrestriction,Linuxtakesadvantageofthe

AccessedbitincludedineachPageTableentry,whichis

automaticallysetbythehardwarewhenthepageis

accessed;moreover,theageofapageisrepresentedby

thepositionofthepagedescriptorinoneoftwodifferent

lists(seethesection"TheLeastRecentlyUsed(LRU)Lists"

laterinthischapter).

[ ]Forinstance,theCPUsofsomemainframesautomaticallyupdatethevalueofacounter

includedineachpagetableentrytospecifytheageofthecorrespondingpage.



Therefore,thepageframereclaimingalgorithmisablendof

severalheuristics:

Carefulselectionoftheorderinwhichcachesareexamined.

Orderingofpagesbasedonaging(leastrecentlyused

pagesshouldbefreedbeforepagesaccessedrecently).



Distinctionofpagesbasedonthepagestate(forexample,

non-dirtypagesarebettercandidatesthandirtypages

becausetheydon'thavetobewrittentodisk).



17.2.ReverseMapping

Asstatedintheprevioussection,oneoftheobjectivesofthe

PFRAistobeabletofreeasharedpageframe.Tothatend,the

Linux2.6kernelisabletolocatequicklyallthePageTable

entriesthatpointtothesamepageframe.Thisactivityiscalled

reversemapping.

Atrivialsolutionforreversemappingwouldbetoincludein

eachpagedescriptoradditionalfieldstolinktogetherallthe

PageTableentriesthatpointtothepageframeassociatedwith

thepagedescriptor.However,keepingsuchlistsup-to-date

wouldincreasesignificantlythekerneloverhead;forthat

reason,moresophisticatedsolutionshavebeendevised.The

techniqueusedinLinux2.6isnamedobject-basedreverse

mapping.Essentially,foranyreclaimableUserModepage,the

kernelstoresthebackwardlinkstoallmemoryregionsinthe

system(the"objects")thatincludethepageitself.Each

memoryregiondescriptorstoresapointertoamemory

descriptor,whichinturnincludesapointertoaPageGlobal

Directory.Therefore,thebackwardlinksenablethePFRAto

retrieveallPageTableentriesreferencingagivenapage.

Becausetherearefewermemoryregiondescriptorsthanpage

descriptors,updatingthebackwardlinksofasharedpageis

lesstimeconsuming.Let'sseehowthisschemeisworkedout.

Firstofall,thePFRAmusthaveawaytodeterminewhetherthe

pagetobereclaimedissharedornon-shared,andwhetheritis

mappedoranonymous.Inordertodothis,thekernellooksat

twofieldsofthepagedescriptor:_mapcountandmapping.

The_mapcountfieldstoresthenumberofPageTableentriesthat

refertothepageframe.Thecounterstartsfrom-1:thisvalue

meansthatnoPageTableentryreferencesthepageframe.

Thus,ifthecounteriszero,thepageisnon-shared,whileifitis

greaterthanzerothepageisshared.Thepage_mapcount()



functionreceivestheaddressofapagedescriptorandreturns

thevalueofits_mapcountplusone(thus,forinstance,itreturns

oneforanon-sharedpageincludedintheUserModeaddress

spaceofsomeprocess).

Themappingfieldofthepagedescriptordetermineswhetherthe

pageismappedoranonymous,asfollows:

IfthemappingfieldisNULL,thepagebelongstotheswap

cache(seethesection"TheSwapCache"laterinthis

chapter).

IfthemappingfieldisnotNULLanditsleastsignificantbitis

1,itmeansthepageisanonymousandthemappingfield

encodesthepointertoananon_vmadescriptor(seethenext

section,"ReverseMappingforAnonymousPages").

Ifthemappingfieldisnon-NULLanditsleastsignificantbitis0,

thepageismapped;themappingfieldpointstothe

address_spaceobjectofthecorrespondingfile(seethesection

"Theaddress_spaceObject"inChapter15).

Everyaddress_spaceobjectusedbyLinuxisalignedinRAMso

thatitsstartinglinearaddressisamultipleoffour.Therefore,

theleastsignificantbitofthemappingfieldcanbeusedasaflag

denotingwhetherthefieldcontainsapointertoanaddress_space

objectortoananon_vmadescriptor.Thisisadirtyprogramming

trick,butthekernelusesalotofpagedescriptors,thusthese

datastructuresshouldbeassmallaspossible.ThePageAnon()

functionreceivesasitsparametertheaddressofapage

descriptorandreturns1iftheleastsignificantbitofthemapping

fieldisset,0otherwise.

TheTRy_to_unmap()function,whichreceivesasitsparametera

pointertoapagedescriptor,triestoclearallthePageTable

entriesthatpointtothepageframeassociatedwiththatpage



descriptor.ThefunctionreturnsSWAP_SUCCESS(zero)ifthefunction

succeededinremovinganyreferencetothepageframefromall

PageTableentries,itreturnsSWAP_AGAIN(one)ifsomereference

couldnotberemoved,andreturnsSWAP_FAIL(two)incaseof

errors.Thefunctionisquiteshort:

inttry_to_unmap(structpage*page)

{

intret;

if(PageAnon(page))

ret=try_to_unmap_anon(page);

else

ret=try_to_unmap_file(page);

if(!page_mapped(page))

ret=SWAP_SUCCESS;

returnret;

}



TheTRy_to_unmap_anon()andtry_to_unmap_file()functionstake

careofanonymouspagesandmappedpages,respectively.

Thesefunctionswillbedescribedintheforthcomingsections.



17.2.1.ReverseMappingforAnonymousPages

Anonymouspagesareoftensharedamongseveralprocesses.

Themostcommoncaseoccurswhenforkinganewprocess:as

explainedinthesection"CopyOnWrite"inChapter9,allpage

framesownedbytheparentincludingtheanonymouspagesare

assignedalsotothechild.Another(quiteunusual)caseoccurs

whenaprocesscreatesamemoryregionspecifyingboththe

MAP_ANONYMOUSandMAP_SHAREDflag:thepagesofsucharegionwill

besharedamongthefuturedescendantsoftheprocess.

Thestrategytolinktogetheralltheanonymouspagesthatrefer



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 17.  Page Frame Reclaiming

Tải bản đầy đủ ngay(0 tr)

×