Selective swap out of processes
Subsystems (3)
Hi all,
I am trying to reduce the main memory power consumption when the system is idle. In order to achieve it, I want to freeze some processes (user-defined) when the system enters a long idle period and swap them out to the disk. After that, more memory is free and then, the remaining used memory can be moved to a minimal set of memory ranks so the rest of ranks can be switched off.
To the best of my knowledge, a process can own the following types of memory pages:
- Mapped pages
· Executable and Read-only mapped pages that are backed by a file in the disk. These pages can be directly unmapped (if they are not shared) -> UNMAP
· Writable file mapped pages that must be flushed to disk (synced) before they are unmapped -> SYNC + UNMAP
- Anonymous pages in User Mode address spaces -> SWAP
- Mapped pages of tmpfs filesystem -> SWAP
I have implemented the process selection mechanism (using an entry for each PID in proc), and the process freezing/resume (using the refrigerator function, like in the hibernation code).
Now I am implementing the memory freeing. The biggest problem here is that the regular swapping out algorithm of the kernel only frees memory when it is needed, so I don't know which is the behaviour of the standard routines in this situation. I have looked at the standard swapping functions (shrink_zones, shrink_zone, ...) and I think they handle all the process page types I enumerated previously. So, for each VMA of the process, I build a page list with all the pages and pass it as a parameter to shrink_page_list (before that I remove them from the LRU active/inactive lists with del_page_from_lru).
First I have tried with the executable VMA (of a lynx process) mapped to the executable file. However none of the pages is freed. shrink_page_list skips each page due to this check:
referenced = page_referenced(page, 1);
/* In active use or really unfreeable? Activate it. */ if (referenced && page_mapping_inuse(page))
goto activate_locked;
It seems they are mapped somewhere else and they cannot be freed. So, which operations should I perform on the pages (try_to_unmap, pte_mkold, ...) before I call shrink_page_list?
I would be eternally grateful if someone could help me with this :-)
Thanks in advance.
Javi
Signature · 3 lines
Javier Cabezas Rodríguez Phd. Student - DAC (UPC) [email protected]
Quoted 38 lines · Javier Cabezas RodrA-guez
Hi all,
I am trying to reduce the main memory power consumption when the system is idle. In order to achieve it, I want to freeze some processes (user-defined) when the system enters a long idle period and swap them out to the disk. After that, more memory is free and then, the remaining used memory can be moved to a minimal set of memory ranks so the rest of ranks can be switched off.
To the best of my knowledge, a process can own the following types of memory pages:
- Mapped pages
A. Executable and Read-only mapped pages that are backed by a file in the disk. These pages can be directly unmapped (if they are not shared) -> UNMAP
A. Writable file mapped pages that must be flushed to disk (synced) before they are unmapped -> SYNC + UNMAP
- Anonymous pages in User Mode address spaces -> SWAP
- Mapped pages of tmpfs filesystem -> SWAPI have implemented the process selection mechanism (using an entry for each PID in proc), and the process freezing/resume (using the refrigerator function, like in the hibernation code).
Now I am implementing the memory freeing. The biggest problem here is that the regular swapping out algorithm of the kernel only frees memory when it is needed, so I don't know which is the behaviour of the standard routines in this situation. I have looked at the standard swapping functions (
shrink_zones,shrink_zone, ...) and I think they handle all the process page types I enumerated previously. So, for each VMA of the process, I build a page list with all the pages and pass it as a parameter toshrink_page_list(before that I remove them from the LRU active/inactive lists withdel_page_from_lru).First I have tried with the executable VMA (of a lynx process) mapped to the executable file. However none of the pages is freed.
shrink_page_listskips each page due to this check:
Quoted 8 lines
referenced =
page_referenced(page, 1);
/* In active use or really unfreeable? Activate it. */ if (referenced &&page_mapping_inuse(page))goto activate_locked;It seems they are mapped somewhere else and they cannot be freed. So, which operations should I perform on the pages (
try_to_unmap,pte_mkold, ...) before I callshrink_page_list?
Simplest will be just to set referenced to 0 right after calling page_referenced, in the case you want to forcefully swap out the page.
try_to_unmap will get called later in the same function.
unmapped pagecache, and other caches are going to take up a fair bit of memory as well, and fragmentation might mean it is hard to get large enough regions of contiguous memory to switch off chips, though.
Signature · 7 lines
SUSE Labs, Novell Inc. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Javier Cabezas Rodríguez
Now I am implementing the memory freeing. The biggest problem here is that the regular swapping out algorithm of the kernel only frees memory when it is needed, so I don't know which is the behaviour of the standard routines in this situation. I have looked at the standard swapping functions (
shrink_zones,shrink_zone, ...) and I think they handle all the process page types I enumerated previously. So, for each VMA of the process, I build a page list with all the pages and pass it as a parameter toshrink_page_list(before that I remove them from the LRU active/inactive lists withdel_page_from_lru).
You may want to look at the page migration logic and in particular the implementation of memory unplug in Andrew's tree. Memory unplug moves memory to another node. You could use the same logic but instead of moving pages reclaim them. Movable pages are reclaimable and much of the page migration logic is based on reclaim.
Quoted 5 lines · Nick Piggin
Simplest will be just to set referenced to 0 right after calling
page_referenced, in the case you want to forcefully swap out the page.
try_to_unmapwill get called later in the same function.
I have tried this solution, but 0 pages are freed...
- RO/EXEC pages mapped from the executable are now skipped due to this check:
if (!mapping || !remove_mapping(mapping, page))
goto keep_locked;
The offender is this check in remove_mapping:
if (unlikely(page_count(page) != 2))
goto cannot_free;
- RW pages mapped from the executable are skipped because pageout returns PAGE_KEEP.
- Other pages are skipped because try_to_unmap returns SWAP_FAIL.
I also added a call ptep_clear_flush_young for each pte, to satisfy this check in try_to_unmap_one:
if (!migration && ((vma->vm_flags & VM_LOCKED) ||
(ptep_clear_flush_young(vma, address, pte)))) {
ret = SWAP_FAIL;
goto out_unmap;
}My code calls the following function for each VMA of the process. Are there errors in the function?:
int my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm)
{
LIST_HEAD(page_list);
unsigned long nr_taken;
struct zone * zone = NULL;
int ret;
pte_t *pte_k;
pud_t *pud;
pmd_t *pmd;
unsigned long addr;
struct page * p;
struct scan_control sc;
sc.gfp_mask = __GFP_FS;
sc.may_swap = 1;
sc.may_writepage = 1;
for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr +=
PAGE_SIZE, nr_taken++) {
pgd_t *pgd = pgd_offset(mm, addr);
if (pgd_none(*pgd))
return;
pud = pud_offset(pgd, addr);
if (pud_none(*pud))
return;
pmd = pmd_offset(pud, addr);
if (pmd_none(*pmd))
return;
if (pmd_large(*pmd))
pte_k = (pte_t *)pmd;
else
pte_k = pte_offset_kernel(pmd, addr);
if (pte_k && pte_present(*pte_k)) {
p = pte_page(*pte_k);
if (!zone)
zone = page_zone(p);
ptep_clear_flush_young(vma, addr, pte_k);
del_page_from_lru(zone, p);
list_add(&p->lru, &page_list);
}
}
spin_lock_irq(&zone->lru_lock);
__mod_zone_page_state(zone, NR_INACTIVE, -nr_taken);
zone->pages_scanned += nr_taken;
spin_unlock_irq(&zone->lru_lock);
}Thanks
Javi
Signature · 3 lines
Javier Cabezas Rodríguez Phd. Student - DAC (UPC) [email protected]
Quoted 2 lines
My code calls the following function for each VMA of the process. Are there errors in the function?:
Sorry. I forgot some lines:
int my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm)
{
LIST_HEAD(page_list);
unsigned long nr_taken;
struct zone * zone = NULL;
int ret;
pte_t *pte_k;
pud_t *pud;
pmd_t *pmd;
unsigned long addr;
struct page * p;
struct scan_control sc;
sc.gfp_mask = __GFP_FS;
sc.may_swap = 1;
sc.may_writepage = 1;
for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr +=
PAGE_SIZE, nr_taken++) {
pgd_t *pgd = pgd_offset(mm, addr);
if (pgd_none(*pgd))
return;
pud = pud_offset(pgd, addr);
if (pud_none(*pud))
return;
pmd = pmd_offset(pud, addr);
if (pmd_none(*pmd))
return;
if (pmd_large(*pmd))
pte_k = (pte_t *)pmd;
else
pte_k = pte_offset_kernel(pmd, addr);
if (pte_k && pte_present(*pte_k)) {
p = pte_page(*pte_k);
if (!zone)
zone = page_zone(p);
ptep_clear_flush_young(vma, addr, pte_k);
del_page_from_lru(zone, p);
list_add(&p->lru, &page_list);
}
}
spin_lock_irq(&zone->lru_lock);
__mod_zone_page_state(zone, NR_INACTIVE, -nr_taken);
zone->pages_scanned += nr_taken;
spin_unlock_irq(&zone->lru_lock);
printk("VMC: %lu pages set to be freed\n", nr_taken);
printk("VMC: %d pages freed\n", ret =shrink_page_list_vmswap(&page_list, &sc, PAGEOUT_IO_SYNC)); }
Javi
Signature · 3 lines
Javier Cabezas Rodríguez Phd. Student - DAC (UPC) [email protected]
I need the same basic thing for process checkpoint/restart. I just have a syscall to which I give a virtual address, and then have the kernel try to swap it out. It uses follow_page(FOLL_GET) and find_vma() in the higher-level function, but this appears to work just fine.
I meant this as a horrible hack to play with a couple of months ago, but it hasn't quite broken on me, yet.
mm/vmscan.c 2007-03-15 13:03:57.000000000 -0700 renamed from mm/vmscan.c~ptrace-force-swap1 2007-03-15 11:21:06.000000000 -0700 +17 -0
@@ -614,6 +614,23 @@ static unsigned long shrink_page_list(st return nr_reclaimed; } +int try_to_put_page_in_swap(struct page *page) +{ + + get_page(page); + if (page_count(page) == 1) + /* page was freed from under us. So we are done. */ + return -EAGAIN; + lock_page(page); + if (PageWriteback(page)) + wait_on_page_writeback(page); + try_to_unmap(page, 0); + //printk("page mapped: %d\n", page_mapped(page)); + unlock_page(page); + put_page(page); + return 0; +} + /* * zone->lru_lock is heavily contended. Some of the functions that * shrink the lists perform better by taking out a batch of pages
-- Dave
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 19 lines · Javier Cabezas RodrA-guez
El miA(C), 29-08-2007 a las 12:37 +1000, Nick Piggin escribiA3:
Quoted 5 lines
Simplest will be just to set referenced to 0 right after calling
page_referenced, in the case you want to forcefully swap out the page.
try_to_unmapwill get called later in the same function.I have tried this solution, but 0 pages are freed...
- RO/EXEC pages mapped from the executable are now skipped due to this check:
if (!mapping || !remove_mapping(mapping, page))
goto keep_locked;The offender is this check in
remove_mapping:
Quoted 7 lines
if (
unlikely(page_count(page) != 2))goto cannot_free;- RW pages mapped from the executable are skipped because pageout returns
PAGE_KEEP.- Other pages are skipped because
try_to_unmapreturnsSWAP_FAIL.
You still actually have to call page_referenced to clear the young bits in the ptes, right? That should prevent try_to_unmap returning SWAP_FAIL. It can be mapped by multiple processes, so just clearing the young bit for one pte won't help (especially for exec pages, which are very likely to be used by more than one process).
If your page_count is elevated after the page has been unmapped, then there is something else using the page or your function isn't doing the correct refcounting.
Signature · 7 lines
SUSE Labs, Novell Inc. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 57 lines · Javier Cabezas RodrA-guez
Quoted 2 lines
My code calls the following function for each VMA of the process. Are there errors in the function?:
Sorry. I forgot some lines:
int
my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm){ LIST_HEAD(page_list); unsigned long nr_taken; struct zone * zone = NULL; int ret; pte_t *pte_k; pud_t *pud; pmd_t *pmd; unsigned long addr; struct page * p; struct scan_control sc; sc.gfp_mask = __GFP_FS; sc.may_swap = 1; sc.may_writepage = 1; for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr += PAGE_SIZE, nr_taken++) { pgd_t *pgd = pgd_offset(mm, addr); if (pgd_none(*pgd)) return; pud = pud_offset(pgd, addr); if (pud_none(*pud)) return; pmd = pmd_offset(pud, addr); if (pmd_none(*pmd)) return; if (pmd_large(*pmd)) pte_k = (pte_t *)pmd; else pte_k = pte_offset_kernel(pmd, addr); if (pte_k && pte_present(*pte_k)) { p = pte_page(*pte_k); if (!zone) zone = page_zone(p); ptep_clear_flush_young(vma, addr, pte_k); del_page_from_lru(zone, p); list_add(&p->lru, &page_list); } } spin_lock_irq(&zone->lru_lock); __mod_zone_page_state(zone, NR_INACTIVE, -nr_taken); zone->pages_scanned += nr_taken; spin_unlock_irq(&zone->lru_lock); printk("VMC: %lu pages set to be freed\n", nr_taken); printk("VMC: %d pages freed\n", ret =
Quoted 2 lines
shrink_page_list_vmswap(&page_list, &sc, PAGEOUT_IO_SYNC)); }
I don't know if that's right or not really, without more context, but it doesn't look like you have the right page table walking locking or page refcounting (and you probably don't want to simply be returning when you encounter the first empty page table entry).
Anyway. I'd be inclined to not do your own page table walking at this stage and begin by using get_user_pages() to do it for you. Then if you get to the stage of wanting to optimise it, you could copy the get_user_pages code, and use that as a starting point.
Signature · 7 lines
SUSE Labs, Novell Inc. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
I have modified the code so it now uses get_user_pages. I'm also using the function posted by Dave Hansen in this thread to free each page. However my module is still not able to free any page. I inspect the smaps entries of each process in /proc before/after executing my code to check it. The full code is posted next; don't worry, it's quite short. The entry point is free_procs, called from a procfs handler when a number is written to "/proc/swapper" (created by my module). The processes are in UNINTERRUPTIBLE_SLEEP state before I try to swap them out.
Can someone find any obvious problem in the code?
Thanks.
Javi
int try_to_put_page_in_swap(struct page *page)
{
get_page(page);
if (page_count(page) == 1) /* page was freed from under us. So we are done. */
return -EAGAIN;
lock_page(page);
if (PageWriteback(page))
wait_on_page_writeback(page);
try_to_unmap(page, 0); unlock_page(page);
put_page(page);
return 0;
}
int free_process(struct vm_area_struct * vma, struct task_struct * p) {
int write;
int npages;
struct page ** pages;
int i;
spin_lock(&p->mm->page_table_lock); write = (vma->vm_flags & VM_WRITE) != 0; npages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
if (!pages)
return -ENOMEM;
npages = get_user_pages(p, p->mm, vma->vm_start, npages, write, 0,
pages, NULL);
kfree(pages);
spin_unlock(&p->mm->page_table_lock);
for (i = 0; i < npages; i++)
try_to_put_page_in_swap(pages[i]); return npages;
}
void free_procs(void)
{
struct task_struct *g, *p;
struct vm_area_struct * vma;
int c, count;
read_lock(&tasklist_lock);
do_each_thread(g, p) {
if (!p->pinned) { /* This process can be swapped out */
down_read(&p->mm->mmap_sem);
for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next) {
if ((c = free_process(vma, p)) == -ENOMEM) {
printk("VMC: Out of Memory\n");
up_read(&p->mm->mmap_sem);
goto out;
}
count += c;
}
up_read(&p->mm->mmap_sem);
printk("VMC: Process %d. %d pages freed\n", p->pid, count);
}
} while_each_thread(g, p);out:
read_unlock(&tasklist_lock);
}
Quoted 74 lines · Nick Piggin
Quoted 57 lines · Javier Cabezas Rodriguez
Quoted 2 lines
My code calls the following function for each VMA of the process. Are there errors in the function?:
Sorry. I forgot some lines:
int
my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm){ LIST_HEAD(page_list); unsigned long nr_taken; struct zone * zone = NULL; int ret; pte_t *pte_k; pud_t *pud; pmd_t *pmd; unsigned long addr; struct page * p; struct scan_control sc; sc.gfp_mask = __GFP_FS; sc.may_swap = 1; sc.may_writepage = 1; for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr += PAGE_SIZE, nr_taken++) { pgd_t *pgd = pgd_offset(mm, addr); if (pgd_none(*pgd)) return; pud = pud_offset(pgd, addr); if (pud_none(*pud)) return; pmd = pmd_offset(pud, addr); if (pmd_none(*pmd)) return; if (pmd_large(*pmd)) pte_k = (pte_t *)pmd; else pte_k = pte_offset_kernel(pmd, addr); if (pte_k && pte_present(*pte_k)) { p = pte_page(*pte_k); if (!zone) zone = page_zone(p); ptep_clear_flush_young(vma, addr, pte_k); del_page_from_lru(zone, p); list_add(&p->lru, &page_list); } } spin_lock_irq(&zone->lru_lock); __mod_zone_page_state(zone, NR_INACTIVE, -nr_taken); zone->pages_scanned += nr_taken; spin_unlock_irq(&zone->lru_lock); printk("VMC: %lu pages set to be freed\n", nr_taken); printk("VMC: %d pages freed\n", ret =Quoted 2 lines
shrink_page_list_vmswap(&page_list, &sc, PAGEOUT_IO_SYNC)); }I don't know if that's right or not really, without more context, but it doesn't look like you have the right page table walking locking or page refcounting (and you probably don't want to simply be returning when you encounter the first empty page table entry).
Anyway. I'd be inclined to not do your own page table walking at this stage and begin by using
get_user_pages()to do it for you. Then if you get to the stage of wanting to optimise it, you could copy theget_user_pagescode, and use that as a starting point.--
SUSE Labs, Novell Inc.
Signature · 7 lines
Javi -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Sorry. It was an old version:
int try_to_put_page_in_swap(struct page *page)
{
get_page(page);
if (page_count(page) == 1) /* page was freed from under us. So we are done. */
return -EAGAIN;
lock_page(page);
if (PageWriteback(page))
wait_on_page_writeback(page);
try_to_unmap(page, 0);
unlock_page(page);
put_page(page);
return 0;
}
int free_process(struct vm_area_struct * vma, struct task_struct * p) {
int write;
int npages;
struct page ** pages;
int i;
spin_lock(&p->mm->page_table_lock);
write = (vma->vm_flags & VM_WRITE) != 0;
npages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
if (!pages)
return -ENOMEM;
npages = get_user_pages(p, p->mm, vma->vm_start, npages, write, 0,
pages, NULL);
spin_unlock(&p->mm->page_table_lock);
for (i = 0; i < npages; i++)
try_to_put_page_in_swap(pages[i]);
kfree(pages);
return npages;
}
void free_procs(void)
{
struct task_struct *g, *p;
struct vm_area_struct * vma;
int c, count;
read_lock(&tasklist_lock);
do_each_thread(g, p) {
if (!p->pinned) { /* This process can be swapped out */
down_read(&p->mm->mmap_sem);
for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next, count += c) {
if ((c = free_process(vma, p)) == -ENOMEM) {
printk("VMC: Out of Memory\n");
up_read(&p->mm->mmap_sem);
goto out;
}
}
up_read(&p->mm->mmap_sem);
printk("VMC: Process %d. %d pages freed\n", p->pid, count); }
} while_each_thread(g, p);
out:
read_unlock(&tasklist_lock);
Quoted 173 lines · Javier Cabezas Rodriguez
I have modified the code so it now uses
get_user_pages. I'm also using the function posted by Dave Hansen in this thread to free each page. However my module is still not able to free any page. I inspect the smaps entries of each process in /proc before/after executing my code to check it. The full code is posted next; don't worry, it's quite short. The entry point isfree_procs, called from a procfs handler when a number is written to "/proc/swapper" (created by my module). The processes are inUNINTERRUPTIBLE_SLEEPstate before I try to swap them out.Can someone find any obvious problem in the code?
Thanks.
Javi
int
try_to_put_page_in_swap(struct page *page)
{
get_page(page);if (
page_count(page)== 1) /* page was freed from under us. So we are done. */return -EAGAIN;lock_page(page); if (PageWriteback(page)) wait_on_page_writeback(page); try_to_unmap(page, 0);
unlock_page(page);
put_page(page);
return 0;
}int
free_process(struct vm_area_struct * vma, struct task_struct * p){
int write;
int npages;
struct page ** pages;
int i;spin_lock(&p->mm->page_table_lock); write = (vma->vm_flags & VM_WRITE) != 0; npages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);if (!pages)
return -ENOMEM;npages = get_user_pages(p,
p->mm,vma->vm_start, npages, write, 0,
pages, NULL);kfree(pages); spin_unlock(&p->mm->page_table_lock); for (i = 0; i < npages; i++) try_to_put_page_in_swap(pages[i]);return npages;
}void
free_procs(void)
{
structtask_struct*g, *p;
structvm_area_struct* vma;
int c, count;read_lock(&tasklist_lock); do_each_thread(g, p) { if (!p->pinned) { /* This process can be swapped out */ down_read(&p->mm->mmap_sem); for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next) { if ((c = free_process(vma, p)) == -ENOMEM) { printk("VMC: Out of Memory\n"); up_read(&p->mm->mmap_sem); goto out; } count += c; } up_read(&p->mm->mmap_sem); printk("VMC: Process %d. %d pages freed\n", p->pid, count); } } while_each_thread(g, p);out:
read_unlock(&tasklist_lock);
}Quoted 66 lines · Nick Piggin
Quoted 57 lines · Javier Cabezas Rodriguez
Quoted 2 lines
My code calls the following function for each VMA of the process. Are there errors in the function?:
Sorry. I forgot some lines:
int
my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm){ LIST_HEAD(page_list); unsigned long nr_taken; struct zone * zone = NULL; int ret; pte_t *pte_k; pud_t *pud; pmd_t *pmd; unsigned long addr; struct page * p; struct scan_control sc; sc.gfp_mask = __GFP_FS; sc.may_swap = 1; sc.may_writepage = 1; for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr += PAGE_SIZE, nr_taken++) { pgd_t *pgd = pgd_offset(mm, addr); if (pgd_none(*pgd)) return; pud = pud_offset(pgd, addr); if (pud_none(*pud)) return; pmd = pmd_offset(pud, addr); if (pmd_none(*pmd)) return; if (pmd_large(*pmd)) pte_k = (pte_t *)pmd; else pte_k = pte_offset_kernel(pmd, addr); if (pte_k && pte_present(*pte_k)) { p = pte_page(*pte_k); if (!zone) zone = page_zone(p); ptep_clear_flush_young(vma, addr, pte_k); del_page_from_lru(zone, p); list_add(&p->lru, &page_list); } } spin_lock_irq(&zone->lru_lock); __mod_zone_page_state(zone, NR_INACTIVE, -nr_taken); zone->pages_scanned += nr_taken; spin_unlock_irq(&zone->lru_lock); printk("VMC: %lu pages set to be freed\n", nr_taken); printk("VMC: %d pages freed\n", ret =Quoted 2 lines
shrink_page_list_vmswap(&page_list, &sc, PAGEOUT_IO_SYNC)); }I don't know if that's right or not really, without more context, but it doesn't look like you have the right page table walking locking or page refcounting (and you probably don't want to simply be returning when you encounter the first empty page table entry).
Quoted 8 lines
Anyway. I'd be inclined to not do your own page table walking at this stage and begin by using
get_user_pages()to do it for you. Then if you get to the stage of wanting to optimise it, you could copy theget_user_pagescode, and use that as a starting point.--
SUSE Labs, Novell Inc.--
Javi
Signature · 7 lines
Javi -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Sorry. It was an old version:
int try_to_put_page_in_swap(struct page *page)
{
get_page(page);
if (page_count(page) == 1) /* page was freed from under us. So we are done. */
return -EAGAIN;
lock_page(page);
if (PageWriteback(page))
wait_on_page_writeback(page);
try_to_unmap(page, 0);
unlock_page(page);
put_page(page);
return 0;
}
int free_process(struct vm_area_struct * vma, struct task_struct * p) {
int write;
int npages;
struct page ** pages;
int i;
spin_lock(&p->mm->page_table_lock);
write = (vma->vm_flags & VM_WRITE) != 0;
npages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
if (!pages)
return -ENOMEM;
npages = get_user_pages(p, p->mm, vma->vm_start, npages, write, 0,
pages, NULL);
spin_unlock(&p->mm->page_table_lock);
for (i = 0; i < npages; i++)
try_to_put_page_in_swap(pages[i]);
kfree(pages);
return npages;
}
void free_procs(void)
{
struct task_struct *g, *p;
struct vm_area_struct * vma;
int c, count;
read_lock(&tasklist_lock);
do_each_thread(g, p) {
if (!p->pinned) { /* This process can be swapped out */
down_read(&p->mm->mmap_sem);
for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next, count += c) {
if ((c = free_process(vma, p)) == -ENOMEM) {
printk("VMC: Out of Memory\n");
up_read(&p->mm->mmap_sem);
goto out;
}
}
up_read(&p->mm->mmap_sem);
printk("VMC: Process %d. %d pages freed\n", p->pid, count); }
} while_each_thread(g, p);
out:
read_unlock(&tasklist_lock);
Quoted 87 lines · Javier Cabezas Rodriguez
I have modified the code so it now uses
get_user_pages. I'm also using the function posted by Dave Hansen in this thread to free each page. However my module is still not able to free any page. I inspect the smaps entries of each process in /proc before/after executing my code to check it. The full code is posted next; don't worry, it's quite short. The entry point isfree_procs, called from a procfs handler when a number is written to "/proc/swapper" (created by my module). The processes are inUNINTERRUPTIBLE_SLEEPstate before I try to swap them out.Can someone find any obvious problem in the code?
Thanks.
Javi
int
try_to_put_page_in_swap(struct page *page)
{
get_page(page);if (
page_count(page)== 1) /* page was freed from under us. So we are done. */return -EAGAIN;lock_page(page); if (PageWriteback(page)) wait_on_page_writeback(page); try_to_unmap(page, 0);
unlock_page(page);
put_page(page);
return 0;
}int
free_process(struct vm_area_struct * vma, struct task_struct * p){
int write;
int npages;
struct page ** pages;
int i;spin_lock(&p->mm->page_table_lock); write = (vma->vm_flags & VM_WRITE) != 0; npages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);if (!pages)
return -ENOMEM;npages = get_user_pages(p,
p->mm,vma->vm_start, npages, write, 0,
pages, NULL);kfree(pages); spin_unlock(&p->mm->page_table_lock); for (i = 0; i < npages; i++) try_to_put_page_in_swap(pages[i]);return npages;
}void
free_procs(void)
{
structtask_struct*g, *p;
structvm_area_struct* vma;
int c, count;read_lock(&tasklist_lock); do_each_thread(g, p) { if (!p->pinned) { /* This process can be swapped out */ down_read(&p->mm->mmap_sem); for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next) { if ((c = free_process(vma, p)) == -ENOMEM) { printk("VMC: Out of Memory\n"); up_read(&p->mm->mmap_sem); goto out; } count += c; } up_read(&p->mm->mmap_sem); printk("VMC: Process %d. %d pages freed\n", p->pid, count); } } while_each_thread(g, p);
Quoted 78 lines
out:
read_unlock(&tasklist_lock);
}Quoted 73 lines · Nick Piggin
Quoted 57 lines · Javier Cabezas Rodriguez
Quoted 2 lines
My code calls the following function for each VMA of the process. Are there errors in the function?:
Sorry. I forgot some lines:
int
my_free_pages(struct vm_area_struct * vma, struct mm_struct * mm){ LIST_HEAD(page_list); unsigned long nr_taken; struct zone * zone = NULL; int ret; pte_t *pte_k; pud_t *pud; pmd_t *pmd; unsigned long addr; struct page * p; struct scan_control sc; sc.gfp_mask = __GFP_FS; sc.may_swap = 1; sc.may_writepage = 1; for (addr = vma->vm_start, nr_taken = 0; addr < vma->vm_end; addr += PAGE_SIZE, nr_taken++) { pgd_t *pgd = pgd_offset(mm, addr); if (pgd_none(*pgd)) return; pud = pud_offset(pgd, addr); if (pud_none(*pud)) return; pmd = pmd_offset(pud, addr); if (pmd_none(*pmd)) return; if (pmd_large(*pmd)) pte_k = (pte_t *)pmd; else pte_k = pte_offset_kernel(pmd, addr); if (pte_k && pte_present(*pte_k)) { p = pte_page(*pte_k); if (!zone) zone = page_zone(p); ptep_clear_flush_young(vma, addr, pte_k); del_page_from_lru(zone, p); list_add(&p->lru, &page_list); } } spin_lock_irq(&zone->lru_lock); __mod_zone_page_state(zone, NR_INACTIVE, -nr_taken); zone->pages_scanned += nr_taken; spin_unlock_irq(&zone->lru_lock); printk("VMC: %lu pages set to be freed\n", nr_taken); printk("VMC: %d pages freed\n", ret =Quoted 2 lines
shrink_page_list_vmswap(&page_list, &sc, PAGEOUT_IO_SYNC)); }I don't know if that's right or not really, without more context, but it doesn't look like you have the right page table walking locking or page refcounting (and you probably don't want to simply be returning when you encounter the first empty page table entry).
Anyway. I'd be inclined to not do your own page table walking at this stage and begin by using
get_user_pages()to do it for you. Then if you get to the stage of wanting to optimise it, you could copy theget_user_pagescode, and use that as a starting point.--
SUSE Labs, Novell Inc.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 14 lines · Javier Cabezas Rodriguez
Sorry. It was an old version:
int
try_to_put_page_in_swap(struct page *page)
{
get_page(page);if (
page_count(page)== 1) /* page was freed from under us. So we are done. */
return-EAGAIN;
lock_page(page);if (PageWriteback(page))
wait_on_page_writeback(page);
Quoted 6 lines
try_to_unmap(page, 0);
unlock_page(page);
put_page(page);
return 0;
}
You'd surely have to add_to_swap here, and at some point will want to also free the swapcache after writing it out.
Look at how the code in mm/vmscan.c does it.
Quoted 8 lines
int
free_process(struct vm_area_struct * vma, struct task_struct * p){
int write;
int npages;
struct page ** pages;
int i;
spin_lock(&p->mm->page_table_lock);
You rather need down_read(&mm->mmap_sem);
Quoted 6 lines
write = (
vma->vm_flags&VM_WRITE) != 0;
npages = (vma->vm_end-vma->vm_start) >>PAGE_SHIFT; pages =kmalloc(npages * sizeof(struct page *), GFP_KERNEL);if (!pages)
return-ENOMEM;
Careful of just returning while you're holding a spinlock or other resources.
Quoted 11 lines
npages = get_user_pages(p,
p->mm,vma->vm_start, npages, write, 0,
pages, NULL);
spin_unlock(&p->mm->page_table_lock);for (i = 0; i < npages; i++)
try_to_put_page_in_swap(pages[i]);kfree(pages);
return npages;
You have to carefully keep track of what is happening with your page refcounts and make sure you're doing the right thing here. For example, get_user_pages increments the refcounts, and it looks like you don't decrement them again -- this will leave the page permanently pinned in memory.
Quoted 8 lines
}
void
free_procs(void)
{
structtask_struct*g, *p;
structvm_area_struct* vma;
int c, count;
Quoted 4 lines
read_lock(&tasklist_lock);
do_each_thread(g, p){
if (!p->pinned) { /* This process can be swapped out */down_read(&p->mm->mmap_sem);
Ah, you have down_read here. So you don't need ptl above. Unfortunately, down_read sleeps, while tasklist_lock is a spinlock, so you'll need to take some other approach here.
Quoted 9 lines
for (vma = p->mm->mmap, count = 0; vma; vma = vma->vm_next, count += c) {
if ((c = free_process(vma, p)) == -ENOMEM) {
printk("VMC: Out of Memory\n");
up_read(&p->mm->mmap_sem);
goto out;
}
}
up_read(&p->mm->mmap_sem);
printk("VMC: Process %d. %d pages freed\n", p->pid, count);Quoted 5 lines
}
}while_each_thread(g, p);out:
read_unlock(&tasklist_lock);
I won't have time to help more as I'm heading overseas, good luck!
Signature · 7 lines
SUSE Labs, Novell Inc. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Isn't the whole point of get_user_pages() so that the kernel doesn't mess with those pages, and the driver or whatever can have free reign?
Seems to me that you're pinning the pages with get_user_pages(), then trying to get the kernel to swap them out. Not a good idea. ;)
-- Dave
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 5 lines · Dave Hansen
Isn't the whole point of
get_user_pages()so that the kernel doesn't mess with those pages, and the driver or whatever can have free reign?Seems to me that you're pinning the pages with
get_user_pages(), then trying to get the kernel to swap them out. Not a good idea. ;)
That's pretty much what it means... well, it is explicitly defined to simply increment the refcount of each returned page, which happens to be exactly what you want in this case.
Obviously your VM code that's doing the swapout has to account for this refcount... but you'd need to do that anyway.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>