0

I would like to write a simple kernel function that iterates over all the vm_area_structs that belong to a specific process and mark each one of them as belonging to the heap or not. Assume that I can add a boolean field in the vm_area_struct that will be set for heap pages and reset for other pages.

I have looked into the mm_struct, vm_area_struct, and task_struct... but found nothing that can help.

Update: I am guessing start_brk and brk have something to do with this?

6
  • Define what you exactly mean by heap. Look at /proc/self/maps or /proc/$(pidof app)/maps for some Linux application. Commented Mar 9, 2014 at 20:55
  • I am referring to the part of memory where malloc() gets the memory from. For this, is there more than one region? Commented Mar 9, 2014 at 21:01
  • @hebbo : I'm not sure that the os has this information... Commented Mar 9, 2014 at 21:05
  • I think there should be a way. Since malloc() under the hood calls the OS to give it more memory using either mmap() or sbrk(). The OS also loads the process into memory and marks the bss, code and other segments. Commented Mar 9, 2014 at 21:07
  • As @BasileStarynkevitch points out, the OS already implements /proc/<pid>/maps which shows all VMAs of that process, including the heap and of course the mmap-ed ones. Why not check out its code? (Right now I don't have access to the codebase). Commented Mar 10, 2014 at 2:39

1 Answer 1

1

(Am inserting my last comment as an answer, as the formatting within "Comment" is not that great):

Wrt my prev comment: the relevant code (to look up VMAs of a given PID) seems to be here: fs/proc/task_mmu.c .

And, yes indeed, the "[heap]" is marked by this code snippet from the above src file (kernel ver 3.10.24):

*fs/proc/task_mmu.c:show_map_vma()* ... if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) { name = "[heap]"; goto done; } ...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.