Abhilash V R
Abhilash V R

Reputation: 257

Kernel function to set the PG_Referenced bit of a heap page

I have a small C application shown below. It does the following operations:

  1. Allocate two pages of heap memory.

  2. Clear referenced bit (PG_referenced) of heap page (echo 1 > /proc/pid/clear_refs).

  3. Do a write to the heap allocated pages again.

  4. Check referenced bit is getting set in page after the write operation. (cat /proc/pid/smaps) I found it is getting set.

  5. Repeat step 2 to 4 to verify correctness.

So from this exercise I understood whenever I do a write to the heap page, the PG_referenced bit is getting set, and whenever I clear it using /proc/pid/clear_ref it is getting cleared.

So I looked in the kernel code to find out which kernel function is setting the PG_referenced bit. I thought it was the mark_page_accessed() function in mm/swap.c. But after searching I found some other function is setting the PG_referenced bit of the page for every write, if I clear it before write.

So, please some one, help me to find out which kernel function is doing this?

I am writing down the application I used for testing:

    ptr_obj = malloc(2*4096);

    while(1){
            /* clear all page refernces  */
            sprintf(buffer,"echo 1 > /proc/%d/clear_refs ",pid);
            system(buffer);

            /* move smaps to a file */
            sprintf(buffer,"cat /proc/%d/smaps > temp_before.%d",pid,count);
            system(buffer);

            /* do a  write to malloc addr */

            ptr_obj[1] = 12;;
            ptr_obj[6000] = 12;;

            /* move update smaps to file **/
            sprintf(buffer,"cat /proc/%d/smaps > temp_after.%d",pid,count);
            system(buffer);
            count ++;

            sleep(30);
    }

Upvotes: 1

Views: 870

Answers (2)

Paschalis
Paschalis

Reputation: 12291

Dhyan, if you are asking how to find the pages that are written (not accessed, which includes both read and write), then you should check out the soft dirty bit.

So, instead of 1, write 4 in clear_prefs.

Check out official linux documentation: https://www.kernel.org/doc/Documentation/vm/soft-dirty.txt

*you were asking, 2 years ago actually.

Upvotes: 0

Lai Jiangshan
Lai Jiangshan

Reputation: 1420

"Referenced" of /proc/pid/smaps indicates the amount of memory currently marked as referenced or accessed. It includes PG_referenced of the page flags and the ACCESSED_BIT set by the MMU in page table entry. The code of /proc/pid/smaps:

static void smaps_pte_entry(
    ......
        /* Accumulate the size in pages that have been accessed. */
        if (pte_young(ptent) || PageReferenced(page))
            mss->referenced += ptent_size; 

pte_young() of the X86 test the ACCESSED_BIT of the PTE. This bit is set by MMU when your code writing in to the Heap.

Upvotes: 1

Related Questions