我正在使用add_to_swap换出特定页面。然而,即使在我调用了这个返回成功 ( 1 ) 的函数之后,显示页表条目pte_t的系统仍然存在。add_to_swap 是换出页面的正确函数还是我应该查看的 LINUX 内核中的其他函数?我查看了 KSWAPD 模块,但没有找到它用于交换特定页面的函数。
1 回答
add_to_swap
只是分配空间,而不是将页面移动到交换。检查调用add_to_swap
的函数shrink_page_list
: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);
因此,在 swap 中分配空间后,检查页面是否有所有映射到进程的 vm(虚拟内存),未映射try_to_unmap
,检查写回(脏页面,应用程序更改但仍未保存到 FS 的页面),检查缓冲区,再次检查映射...您要换出的页面类型是什么?不确定页面的实际写入是在哪里完成的......可能pageout
是因为它调用writepage
了mapping
. 来源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
交换缓存 - 只有修改(或脏)的页面保存在交换文件中。
只要这些页面在写入交换文件后没有被修改,那么下次页面被换出时就不需要将其写入交换文件,因为页面已经在交换文件中。相反,可以简单地丢弃该页面。在大量交换系统中,这可以节省许多不必要且成本高昂的磁盘操作。
另请查看http://www.tldp.org/LDP/tlk/mm/memory.html的“3.8 换出和丢弃页面”部分