Re: Page cache and swapping

Benjamin C R LaHaise (blah@dot.superaje.com)
Thu, 26 Jun 1997 13:07:12 +0000 ( )


On Thu, 26 Jun 1997, Krzysztof Strasburger wrote:

> >> The patch allows removing shared pages from page cache and removes setting
> >> of the referenced bit for all shared pages.
>
> >I'm sorry, but this can't work. There is no such thing as a "shared unused"
> >page. I suppose you mean "not referenced" by "unused".
> Sure, you are right. I mean "not referenced".
> >Still, your patch doesn't work. You need to guarantee that the page you
> >are freeing is not in any process page tables. Otherwise, a process could
> >reference the page after you have freed it. This is usually done by
> >try_to_swap_out which will in turn remove the page from the memory map of
> >every process that contains it. After that, it can be freed.
> Sounds judiciously, but I have still two questions.
> 1. If page->count = 1 and the page is in page cache, it belongs exactly
> to one process. Right? Why is it simply freed by shrink_mmap and why
> it can't be freed, if page->count > 1?. Where is it removed from the process
> page table? Or rather the "present" bit is set to zero for page cache
> pages.

This is the difficult point - you need to walk the page tables of all
processes to find out which ones are using the page - a very costly
operation. This is why the current system doesn't do this.

> 2. Why my computer works with this patch? It should crash. Or at least
> programs should terminate very quickly ;-)

Your programs will continue to work under most circumstances - the only
catch is that you've removed the page from the page cache, so an attempt
by another program to use that page will result in another page being
allocated & read for that inode. Similarly, if it's a shared memory
mapping, it will not be updated on writes.

These points aside, you have brought up a major concern - shared pages
are *too* hard to free up at this point, and the kernel makes no attempt
to balance efficiently between shared and non-shared memory. Most of the
time this works out because on a typical system only executables and
libraries are being accessed. Why does the patch actually work? If a
shared page freed *once* by your patch, then when the last user of the
shared page frees their reference to it, the page is completely freed.
You've effectively removed the page cache's reference to the page which
frees the page sooner - a Good Thing (tm) but wrong for the reasons
above. Someone like Linus or Stephen should corrent me if I'm wrong, but
I think this is right... Now to find a clean fix.

-ben