我的目标是找出正在换出的页面的进程 ID。 Linux 内核函数 swap_writepage()
在后备存储上交换页面时将指向结构页面的指针 作为正式参数的一部分。所有换出操作都由“kswapd”进程完成。我需要找出其页面作为参数传递到 swap_writepage()
函数中的进程的 pid(s)。为了做到这一点,我能够使用 rmap 结构找到与该页面关联的所有页表条目。
如何从 pte 或 struct page 获取 pid?我使用 sytemtap 获取结构页面指针的值,在 swap_writepage()
函数中作为参数接收。此外,pid()
函数打印当前正在运行的进程的 pid,而不是该页面所属的进程的 pid,它总是提供 kswapd 进程。
最佳答案
这是现代 Linux 中如何使用反向映射的示例(从 lxr 复制):
1435 static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
1436 {
1437 struct anon_vma *anon_vma;
1438 struct anon_vma_chain *avc;
1439 int ret = SWAP_AGAIN;
1440
1441 anon_vma = page_lock_anon_vma(page);
1442 if (!anon_vma)
1443 return ret;
1444
1445 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1446 struct vm_area_struct *vma = avc->vma;
1447 unsigned long address;
1448
1449 /*
1450 * During exec, a temporary VMA is setup and later moved.
1451 * The VMA is moved under the anon_vma lock but not the
1452 * page tables leading to a race where migration cannot
1453 * find the migration ptes. Rather than increasing the
1454 * locking requirements of exec(), migration skips
1455 * temporary VMAs until after exec() completes.
1456 */
1457 if (PAGE_MIGRATION && (flags & TTU_MIGRATION) &&
1458 is_vma_temporary_stack(vma))
1459 continue;
1460
1461 address = vma_address(page, vma);
1462 if (address == -EFAULT)
1463 continue;
1464 ret = try_to_unmap_one(page, vma, address, flags);
1465 if (ret != SWAP_AGAIN || !page_mapped(page))
1466 break;
1467 }
1468
1469 page_unlock_anon_vma(anon_vma);
1470 return ret;
1471 }
此示例显示用于取消映射页面的 rmap。所以在 ->mapping 字段中的每个匿名页面都包含一个 anon_vma 对象。 anon_vma 包含页面映射到的 vma 区域列表。有 vma 就有 mm,有 mm 就有 task_struct。就是这样。如果您有任何疑问 - 这是插图
Daniel P. Bovet、Marco Cesati 了解 Linux 内核第 17.2 章
关于linux - 获取正在换出的页面的 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29652268/