Search code examples
linuxlinux-kernelvirtual-memoryswapfile

Swapping out a specific page in LINUX KERNEL


I am using add_to_swap to swap out a specific page. However, even after I have called this function, which returns success (1), the system that shows that the page table entry pte_t is still present. Is add_to_swap the right function to swap out a page or is there some other function in LINUX kernel which I should look at? I have looked at KSWAPD module and have not found which function it uses to swap specific pages out.


Solution

  • add_to_swap just allocates space, not moves page to swap. Check the function, which calls add_to_swap, the shrink_page_list of mm/vmscan.c:

    http://lxr.free-electrons.com/source/mm/vmscan.c?v=4.4#L1043

    903         while (!list_empty(page_list)) {
    1043                 /*
    1044                  * Anonymous process memory has backing store?
    1045                  * Try to allocate it some swap space here.
    1046                  */
    1047                 if (PageAnon(page) && !PageSwapCache(page)) {
    1050                         if (!add_to_swap(page, page_list))
    1051                                 goto activate_locked;
    1052                         may_enter_fs = 1;
    1054                         /* Adding to swap updated mapping */
    1055                         mapping = page_mapping(page);
    1056                 }
    1058                 /*
    1059                  * The page is mapped into the page tables of one or more
    1060                  * processes. Try to unmap it here.
    1061                  */
    1062                 if (page_mapped(page) && mapping) {
    1063                         switch (try_to_unmap(page,
    1064                                         ttu_flags|TTU_BATCH_FLUSH)) 
    1076                 if (PageDirty(page)) {
    1078                          * Only kswapd can writeback filesystem pages to
    1109                         try_to_unmap_flush_dirty();
    1110                         switch (pageout(page, mapping, sc)) {
    1136                  * If the page has buffers, try to free the buffer mappings
    1177                 if (!mapping || !__remove_mapping(mapping, page, true))
    1178                         goto keep_locked;
    1180                 /*
    1181                  * At this point, we have no other references and there is
    1182                  * no way to pick any more up (removed from LRU, removed
    1183                  * from pagecache). Can use non-atomic bitops now (and
    1184                  * we obviously don't have to worry about waking up a process
    1185                  * waiting on the page lock, because there are no references.
    1186                  */
    1187                 __clear_page_locked(page);
    

    So, after allocating the space in swap, page is checked for all mappings into vm (virtual memory) of processes, unmapped with try_to_unmap, checked for writeback (dirty page, page changed by application but still not saved to the FS), checked for buffers, again checked for mappings... What is the type of page you are trying to swap out? Not sure where the actual writing of page to swap is done... Probably pageout as it calls the writepage method of the mapping. Source of pageout says http://lxr.free-electrons.com/source/mm/vmscan.c?v=4.4#L530

    531  * pageout is called by shrink_page_list() for each dirty page.
    532  * Calls ->writepage().
    534 static pageout_t pageout(struct page *page, struct address_space *mapping,
    535                          struct scan_control *sc)
    574         if (clear_page_dirty_for_io(page)) {
    584                 SetPageReclaim(page);
    585                 res = mapping->a_ops->writepage(page, &wbc);
    597                 trace_mm_vmscan_writepage(page, trace_reclaim_flags(page));
    598                 inc_zone_page_state(page, NR_VMSCAN_WRITE);
    

    We should also understand what is the swap and linux mm: http://www.tldp.org/LDP/tlk/mm/memory.html

    Swap Cache - Only modified (or dirty) pages are saved in the swap file.

    So long as these pages are not modified after they have been written to the swap file then the next time the page is swapped out there is no need to write it to the swap file as the page is already in the swap file. Instead the page can simply be discarded. In a heavily swapping system this saves many unnecessary and costly disk operations.

    Also check "3.8 Swapping Out and Discarding Pages" section of http://www.tldp.org/LDP/tlk/mm/memory.html