Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1) .. _soft_dirty:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4) Soft-Dirty PTEs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7) The soft-dirty is a bit on a PTE which helps to track which pages a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8) writes to. In order to do this tracking one should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)   1. Clear soft-dirty bits from the task's PTEs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)      This is done by writing "4" into the ``/proc/PID/clear_refs`` file of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)      task in question.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)   2. Wait some time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)   3. Read soft-dirty bits from the PTEs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)      This is done by reading from the ``/proc/PID/pagemap``. The bit 55 of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)      64-bit qword is the soft-dirty one. If set, the respective PTE was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)      written to since step 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) Internally, to do this tracking, the writable bit is cleared from PTEs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) when the soft-dirty bit is cleared. So, after this, when the task tries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) modify a page at some virtual address the #PF occurs and the kernel sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) the soft-dirty bit on the respective PTE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) Note, that although all the task's address space is marked as r/o after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) soft-dirty bits clear, the #PF-s that occur after that are processed fast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) This is so, since the pages are still mapped to physical memory, and thus all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) the kernel does is finds this fact out and puts both writable and soft-dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) bits on the PTE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) While in most cases tracking memory changes by #PF-s is more than enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) there is still a scenario when we can lose soft dirty bits -- a task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unmaps a previously mapped memory region and then maps a new one at exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) the same place. When unmap is called, the kernel internally clears PTE values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) including soft dirty bits. To notify user space application about such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) memory region renewal the kernel always marks new memory regions (and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) expanded regions) as soft dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) This feature is actively used by the checkpoint-restore project. You
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) can find more details about it on http://criu.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) -- Pavel Emelyanov, Apr 9, 2013