Search code examples
x86cpuprocessortlbpage-tables

Does dirty bit (of TLB) need to be setted always on a store?


Supose this dumb C code:

int n = 2;

int main(){
    n = 5;
}

When my professor tought us how TLB and page table entry worked, he told us that, if dirty bit == 1, in an eviction of that page from memory to disk, the page needed to be copied to disk.

But my question is: in this example we have a store, so will be the dirty bit of TLB (and page table) setted? From the professor I understood that yes, but my common sense tells me that no, because when I execute this same program tomorrow I will want to find (at the beginning) that n = 2 and not 5.


Solution

  • The data section of an executable is mapped into memory with the equivalent of MAP_PRIVATE, not MAP_SHARED.

    Writes don't update the file, but they do put that page out of sync with what's on disk so it can't just be dropped an reloaded from disk. A clean private page can just be dropped, but a dirty page in a private mapping basically becomes anonymous, backed by swap space not the disk file.

    All of this is why it's necessary for stores to set the dirty bit on the page, so the OS knows this page of RAM has the only copy of this page of the current process's state.