2

Hopefully the title is clear. I have a chunk of memory obtained via mmap(). After some time, I have concluded that I no longer need the data within this range. I still wish to keep this range, however. That is, I do not want to call mummap(). I'm trying to be a good citizen and not make the system swap more than it needs.

Is there a way to tell the Linux kernel that if the given page is backed by a physical page and if the kernel decides it needs that physical page, do not bother writing that page to swap?

I imagine under the hood this magical function call would destroy any mapping between the given virtual page and physical page, if present, without writing to swap first.

tgoodhart
  • 3,111
  • 26
  • 37
  • What's wrong with munmap? It does exactly what you say you want. If you want to ensure a later mmap won't reuse the same virtual address, you could mmap with MAP_FIXED+PROT_NONE after the munmap to reserve the addresses... – Chris Dodd Dec 23 '11 at 19:15
  • How do you know that it does this (writes the backing physical page to swap)? – Janus Troelsen Dec 23 '11 at 19:16
  • @ChrisDodd I could only use munmap() if I could guarentee that a subsequent call to mmap() - made with the appropriate paramters - would rereserve the same page. It is this lack of guarentee that the second mmap() will succeed that prevents me from using munmap(). It's important that the start and end of my chunk of memory do not change, even if I know at various times that particular pages no longer contain useful data. – tgoodhart Dec 23 '11 at 19:25
  • @user309483 I guess I'm not guarenteed that these pages are written to swap, but again, this is purely to be a good citizen on the system. In the event that they would be written to swap, I want to save the system the effort. – tgoodhart Dec 23 '11 at 19:27

2 Answers2

5

Your question (as stated) makes no sense.

Let's assume that there was a way for you to tell the kernel to do what you want.

Let's further assume that it did need the extra RAM, so it took away your page, and didn't swap it out.

Now your program tries to read that page (since you didn't want to munmap the data, presumably you might try to access it). What is the kernel to do? The choices I see:

  1. it can give you a new page filled with 0s.
  2. it can give you SIGSEGV

If you wanted choice 2, you could achieve the same result with munmap.

If you wanted choice 1, you could mremap over the existing mapping with MAP_ANON (or munmap followed by new mmap).

In either case, you can't depend on the old data being there when you need it.

The only way your question would make sense is if there was some additional mechanism for the kernel to let you know that it is taking away your page (e.g. send you a special signal). But the situation you described is likely rare enough to warrant additional complexity.

EDIT:

You might be looking for madvise(..., MADV_DONTNEED)

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Perfect! madvise() is precisely what I was looking for. Behaviour like (1) is what I would want, although zero filling is unnecessary. Any garbage will do. Does mremap() have an MAP_ANON flag on your system? My documentation does not have this. – tgoodhart Dec 23 '11 at 19:33
0

You could munmap the region, then mmap it again with MAP_NORESERVE

If you know at initial mapping time that swapping is not needed, use MAP_NORESERVE

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547