1

I have a memory page for which I used mmap to map it to flash in a userspace linux program (written in c). Currently, the memory is written to very infrequently (say once a day), so I'm fine.

If, however, someone down the road modifies the code to do frequent writes to this memory, they could cause premature flash failure. I'd like to have the code self-policing, and have it detect if it's being abused, and if so take some appropriate actions.

I'm wondering if it's possible to monitor the write access to a page in memory.

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44
  • 1
    You can [mprotect the page](https://stackoverflow.com/questions/2663456/how-to-write-a-signal-handler-to-catch-sigsegv) and do your accounting in the signal handler. Have you considered doing it through plain ol' programming though? Maybe instead of passing the pointer around indiscriminately, you can have a `update_particular_page(char* ptr, size_t len)` memcpy equivalent that does any accounting you want. – that other guy Sep 26 '18 at 19:07
  • 2
    If you have a recent enough kernel, you can use [userfaultfd](http://man7.org/linux/man-pages/man2/userfaultfd.2.html) for this. However, it sounds like keeping the actual memory map in RAM, and using a helper daemon to regularly save it to flash, would be a better match for your needs. (Especially if you have free flash available, and you can add a generation counter, basically just an int or a long, to the mapping, so subsequent copies are written at different places, and at startup one can read all possible pages to find the latest one; this would add additional wear leveling.) – Nominal Animal Sep 26 '18 at 19:48
  • Part of the motivation for this is to free up RAM, which the helper daemon would not accomplish. Unfortunately, using the system handler has a problem -- the exception occurs, at which point the handler is invoked, does the write manually, but when the handler returns, the same instruction is re-run, causing another exception (infinate loop)... If, on the other hand, I enable write access in the handler before returning, then I don't get an exception on the next write... – HardcoreHenry Oct 01 '18 at 17:58

0 Answers0