换出 LINUX KERNEL 中的特定页面

Swapping out a specific page in LINUX KERNEL

我正在使用 add_to_swap 换出特定页面。但是,即使在我调用了这个函数之后,returns成功(1),系统显示页面table条目pte_t 仍然存在。 add_to_swap 是换出页面的正确函数还是 LINUX 内核中有其他我应该查看的函数? 我查看了 KSWAPD 模块,但没有找到它用于交换特定页面的函数。

add_to_swap 只是分配 space,而不是移动页面进行交换。检查函数,which calls add_to_swapmm/vmscan.cshrink_page_list:

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);

因此,在 swap 中分配 space 后,检查页面是否所有映射到进程的 vm(虚拟内存),使用 try_to_unmap 取消映射,检查写回(脏页,页面已更改)按应用程序但仍未保存到 FS),检查缓冲区,再次检查映射...您尝试换出的页面类型是什么?不确定要交换的页面的实际写入是在哪里完成的...可能 pageout 因为它调用了 mappingwritepage 方法。 pageout 的来源说 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);

我们也应该明白什么是swap和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.

另请查看 http://www.tldp.org/LDP/tlk/mm/memory.html

的“3.8 换出和丢弃页面”部分