Linux Page Cache - Deleting a page from the page cache in kernel
Asked Answered
B

2

25

My question is an extension of this one How to manipulate page cache in Linux?

I was trying to do a small project that aims to limit the size of page cache used on a per file basis. The approach I used was as follows -

  1. Maintain a kfifo queue of page pointers as they are added to the page cache.
  2. Add a hook in add_to_page_cache_lru() and see if the size of the radix tree (the address_space) of a file is more than a pre-determined size then choose a victim from the fifo queue and delete the page from page cache.
  3. I used the functions delete_from_page_cache() and try_to_unmap() to evict the page from the page cache, followed by put_page() to release the page.

I expect this code to free the pages and release the memory but that doesn't seem to happen. For example, if I read a file of size 25MB and I've restricted the size of page cache for this file to be 512 pages (2MB), then I expect to see a change of only 2MB in the free memory (free -m). What I see instead is that the full 25MB is eaten up and shows up in the free command.

What more should I do to ensure that my requirements are fulfilled? I've not thought about dirty pages yet as I couldn't even make it work for reads (cat the file). Any pointers would be helpful.

P.S. - I'm using linux 4.0 for this project.

Barouche answered 2/5, 2015 at 5:55 Comment(3)
On a related note, do checkout the implementation of drop_caches within the kernel i.e. how it accomplishes something similar. https://mcmap.net/q/540096/-how-can-i-shrink-the-linux-page-cache-from-within-kernel-spaceHalm
@Halm Thank you! I'll definitely give this one a try.Barouche
I suspect the OS may make assumptions on file open, and is using the memory it on its own, regardless of what you ask for, just to improve access performance. See what it does without asking for any cache, (just file open) and check the free memory. Then try an absolutely massive file, and see just how much the OS sets aside for the cache in this case. That may be your best fit size to work with. Otherwise, you may have to create your own private memory cache space, and copy-in the tiny bit you want cached, and then close the file connection.Turbary
R
1

You might have to do way more than just delete_from_page_cache() + try_to_unmap() + put_page()...

See how shrink_page_list() behaves, including the checks for page_check_references():

More here: How to unmap struct page from all PTEs mapping it

Rugger answered 24/1, 2019 at 8:24 Comment(0)
R
0

I think you are fighting against file system.

  1. If you are adding loadable module linux caching sub system and fs caching system is unaware of this.

    use ftrace with function graph to trace functions in kernel space. will give more insights which functions are called.

  2. cgroup is better module to extend or linux cache. for whatever you are trying to achieve.

I am not sure exact reason but above info can help you find out that.

Resa answered 19/8, 2019 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.