Segmentation fault to detect resident memory size

Mauricio Lin
2004-10-29T21:37:51Z

Hi all,

I had implemented a new entry in proc directory called smaps for
kernel 2.6.1 and it is working.

I have tried to upgrade it to kernel 2.6.9 but a segmentation fault is
thrown on my shell when I cat the /proc/PID/smaps entry.

Below is the code. It is the same code I had implemented for kernel
2.6.1 but it is not working for kernel 2.6.9. I have done some
debugging and I notice if I comment the instructions after the second
"if statement" the segmentation fault problem is removed. But if I
write a simple printk after it a segmentation fault is invoked. It is
very strange. I do not know what is going on. Does anyone can help me?

void resident_mem_size(struct mm_struct *mm, unsigned long start_address,
unsigned long end_address, unsigned long *size) {
pgd_t *my_pgd;
pmd_t *my_pmd;
pte_t *my_pte;
unsigned long page;

for (page = start_address; page < end_address; page += PAGE_SIZE) {
my_pgd = pgd_offset(mm, page);
if (pgd_none(*my_pgd) || pgd_bad(*my_pgd)) continue;
my_pmd = pmd_offset(my_pgd, page);
if (pmd_none(*my_pmd) || pmd_bad(*my_pmd)) continue;
my_pte = pte_offset_map(my_pmd, page);
if (pte_present(*my_pte)) {
*size += PAGE_SIZE;
}
}
}

Mauricio Lin.