Re: Is anonymous memory part of the page cache on Linux?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Mar 14, 2021 at 11:06:12PM +0000, Wxz76@xxxxxxxxxxxxxx wrote:
> Hi Matthew and Peter,
> 
> I had a few questions to clarify my understanding of the page/swap cache.

These are good questions to ask.  I see the confusion, and I'm not
entirely sure how to sort it out for you, but let me try.

> > There's a swap cache, but that's not the same thing as the page cache.
> 
> My understanding of the swap cache comes from:
> 
> 1) Understanding the Linux Kernel by Bovet and Cesati:
> 
> “The swap cache is implemented by the page cache data structures and procedures ” and
> 
> “Pages in the swap cache are stored as every other page in the page cache, with the following special treatment:
> 
> • The mapping field of the page descriptor is set to NULL.
> 
> • The PG_swapcache flag of the page descriptor is set.
> 
> • The private field stores the swapped-out page identifier associated with the page”

They're not wrong, but may be misleading.  The swap cache reuses
(repurposes?)  many of the same data structures used by the page cache.
In particular the address_space.  It's still considered to be separate
from the page cache.

> 2) Understanding the Linux Virtual Memory Manager by Mel Gorman:
> 
> “The swap cache is purely conceptual because it is simply a specialization of the page cache. The first principal difference between pages in the swap cache rather than the page cache is that pages in the swap
> 
> cache always use swapper space as their address space in page→mapping. The second difference is that pages are added to the swap cache with add to swap cache(), shown in Figure 11.3, instead of add to page cache().”
> 
> I understand that those books are more than ten years old, but is what they write no longer the case?

That one, being a little more specific, is now a little more out of date.

> Is the swap cache mechanism not a specialization of the page cache, and, if not, how are they different?
> 
> > Anonymous memory is not handled by the page cache.
> 
> > Anonymous pages enter the storage stack via swap; they are
> 
> > found in the page tables, sent to the swap cache and then written to
> 
> > swap devices or swap files.
> 
> This is for the case of swapping out anonymous memory, but what about anonymous memory that is allocated dynamically with malloc/mmap: where is this memory coming from?

When the kernel needs to allocate a page for stack or malloc (whether it
be implemented through MAP_PRIVATE or brk()), it gets it from its pool of
free pages.  It sets up the process's page tables to refer to that page,
and it adds the page to the LRU list (so it can be swapped out if the
pool of free pages runs low).

> When mmap opens files, it maps a process address spaces to a region in the page cache for the file, does it not?

Yes.  This is the vm_area_struct which records the mapping from the
process address spce to the region of the file.

> Is this behavior not the same for allocating anonymous memory (minus dealing with a file)?

It's also a vm_area_struct, but several of the fields in the
vm_area_struct are used differently by an anonymous mapping than they
are by a file-based mapping.

The particularly interesting case that you didn't ask about is what
happens for mmap(MAP_PRIVATE) of a file.  In that case, we set up for
a file-based mapping, but on a write fault, we allocate a new page,
copy from the page cache into the new page, set up the process page
table to point to this new page and put this new page into the LRU list,
so it can be swapped out if needed.

> I appreciate the help in clarifying this for me.

You're welcome!  I hope this is useful.





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux