0

Virtual address to physical page mapping can be changed during application runtime by swapping or physical page reallocation for memory defragmentation or etc.

What if I want to cache physical page numbers (PPNs) of some virtual address range from /proc/PID/pagemap, since accessing proc/PID/pagemap is extremely expansive overhead to be checked every time, is there a way to be notified if a page has been moved to other physical address or swapped, on the effective address or just any part of memory?

Any kind of method will be ok(not just userspace method, but also those that can be only implemented in kernel space).

wxz
  • 2,254
  • 1
  • 10
  • 31
  • Can you define PPNs in your post? Also, it sounds like you're heading down into "modify the linux kernel to intercept certain page faults/swap operations and log them" territory. Can be done, but very tricky. – wxz Apr 02 '21 at 13:32
  • @wxz Hi! PPN means Physical Page Number, which just means the start point of physical address of each individual page. / I prefer to use whatever method is already in current Linux system, but if not, I'm willing to make kernel module for that. – user3498780 Apr 02 '21 at 16:39
  • I submitted an edit clarifying that. I'm used to it being called physical frame number (PFN) or just physical address. As for the task at hand, I don't think Linux natively provides user-level tools for getting physical addresses (see [here](https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li)). So I think a kernel module that gets associated with your process PID and then gets called during page faults or swapping will be your best option. – wxz Apr 02 '21 at 17:14
  • 1
    You can just get the pages so they don't move. – stark Apr 02 '21 at 19:40
  • `mlock(2)` at least will ensure that the page isn't swapped out, and that accesses to it don't page fault. Theoretically the kernel could copy it to a different physical page while your process sleeps, but I'd be surprised if that could actually happen. So I think what you'd normally do is to first `mlock()` the virtual pages, and then check `/proc/self/pagemap` to get their physical address; then you know they won't change. – Nate Eldredge Apr 03 '21 at 02:19

0 Answers0