Questions about anonymous mapped memory in linux

Viewed 3757

I'm playing around with the idea of using the virtual memory system to allow me to do transparent data conversion (eg int to float) for some numeric data stuff I've got. The basic idea is that the library I'm writing mmaps the data file you want, and at the same time mmaps an anonymous region of an appropriate size to hold the converted data, and this pointer is returned to the user.

The anonymous region is read/write protected, so that whenever the user goes to access data through the pointer, every new page will cause a segfault, which I can catch, then transparently convert data from the mmaped file and fix up the permissions allowing the access to continue. This part of the whole thing works great so far.

However, sometimes I mmap very large files (hundreds of gigabytes), and with the anonymous memory proxying access to it, pretty quickly you'll start eating swap space as anonymous pages are dropped to disk. My thought was that if I could explicitly set the dirty bit on the anonymous pages to false after writing converted data to them, the OS would just drop them and zero-fill on demand later if they were re-accessed.

For this to work though, I think I'd have to set the dirty bit to false and convince the OS to set pages to be read protected when they're swapped out, so I can re-catch the ensuing segfault and reconvert the data on demand. After doing some research I don't think this is possible without kernel hacking, but I thought I'd ask and see if someone that knows more about the virtual memory system knows a way this might be achieved.

2 Answers
Related