/proc/$pid/status の RssFile の挙動を確かめる (2) - mmap(2) したファイルのページキャッシュは vm.drop_caches で破棄されないのをソースで追う

hiboma.hatenadiary.jp

先のエントリの中で、mmap(2) しているファイルのページ(キャッシュ?) は sysctl vm.drop_caches で破棄されないことを確かめたのだが、どのように実装されているのかソースを確かめる

vm.drop_caches の詳細は?

なお、 vm.drop_caches の実装を追いかけている すてきなエントリがあるので、細部を追いたい人はこちらを参照していただきたい

qiita.com

ソースのバージョン

CentOS7.3 3.10.0-514.26.2.el7.x86_64 を読んだ

ソースを追いかける

sysctl vm.drop_caches=1 した際にページキャッシュを破棄するので、 エントリポイントである sysctl のハンドラから追いかける。細部には立ち入らない ( Qiita を読んで! )

int drop_caches_sysctl_handler(ctl_table *table, int write,
    void __user *buffer, size_t *length, loff_t *ppos)
{
    int ret;

    ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
    if (ret)
        return ret;
    if (write) {
        static int stfu;

        if (sysctl_drop_caches & 1) {
            iterate_supers(drop_pagecache_sb, NULL); ⭐
            count_vm_event(DROP_PAGECACHE);
        }
        if (sysctl_drop_caches & 2) {
            drop_slab();
            count_vm_event(DROP_SLAB);
        }
        if (!stfu) {
            pr_info("%s (%d): drop_caches: %d\n",
                current->comm, task_pid_nr(current),
                sysctl_drop_caches);
        }
        stfu |= sysctl_drop_caches & 4;
    }
    return 0;
}

drop_pagecache_sb

superblock ごとに inode をイテレートして invalidate_mapping_pages でページを破棄していく

/* A global variable is a bit ugly, but it keeps the code simple */
int sysctl_drop_caches;

static void drop_pagecache_sb(struct super_block *sb, void *unused)
{
    struct inode *inode, *toput_inode = NULL;

    spin_lock(&inode_sb_list_lock);
    list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
        spin_lock(&inode->i_lock);
        if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
            (inode->i_mapping->nrpages == 0)) {
            spin_unlock(&inode->i_lock);
            continue;
        }
        __iget(inode);
        spin_unlock(&inode->i_lock);
        spin_unlock(&inode_sb_list_lock);
        invalidate_mapping_pages(inode->i_mapping, 0, -1); ⭐
        iput(toput_inode);
        toput_inode = inode;
        spin_lock(&inode_sb_list_lock);
    }
    spin_unlock(&inode_sb_list_lock);
    iput(toput_inode);
}

invalidate_mapping_pages

d.hatena.ne.jp

を書いた時に調べたことのあるメソッドだった。

/**
 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
 * @mapping: the address_space which holds the pages to invalidate
 * @start: the offset 'from' which to invalidate
 * @end: the offset 'to' which to invalidate (inclusive)
 *
 * This function only removes the unlocked pages, if you want to
 * remove all the pages of one inode, you must call truncate_inode_pages.
 *
 * invalidate_mapping_pages() will not block on IO activity. It will not
 * invalidate pages which are dirty, locked, under writeback or mapped into
 * pagetables.
 */
unsigned long invalidate_mapping_pages(struct address_space *mapping,
        pgoff_t start, pgoff_t end)
{
    pgoff_t indices[PAGEVEC_SIZE];
    struct pagevec pvec;
    pgoff_t index = start;
    unsigned long ret;
    unsigned long count = 0;
    int i;

    pagevec_init(&pvec, 0);
    while (index <= end && __pagevec_lookup(&pvec, mapping, index,
            min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
            indices)) {
        mem_cgroup_uncharge_start();
        for (i = 0; i < pagevec_count(&pvec); i++) {
            struct page *page = pvec.pages[i];

            /* We rely upon deletion not changing page->index */
            index = indices[i];
            if (index > end)
                break;

            if (radix_tree_exceptional_entry(page)) {
                clear_exceptional_entry(mapping, index, page);
                continue;
            }

            if (!trylock_page(page))
                continue;
            WARN_ON(page->index != index);
            ret = invalidate_inode_page(page); ⭐
            unlock_page(page);
            /*
            * Invalidation is a hint that the page is no longer
            * of interest and try to speed up its reclaim.
            */
            if (!ret)
                deactivate_page(page);
            count += ret;
        }
        pagevec_remove_exceptionals(&pvec);
        pagevec_release(&pvec);
        mem_cgroup_uncharge_end();
        cond_resched();
        index++;
    }
    return count;
}
EXPORT_SYMBOL(invalidate_mapping_pages);

invalidate_inode_page

/*
 * Safely invalidate one page from its pagecache mapping.
 * It only drops clean, unused pages. The page must be locked.
 *
 * Returns 1 if the page is successfully invalidated, otherwise 0.
 */
int invalidate_inode_page(struct page *page)
{
    struct address_space *mapping = page_mapping(page);
    if (!mapping)
        return 0;
    if (PageDirty(page) || PageWriteback(page))
        return 0;
    if (page_mapped(page)) ⭐
        return 0;
    return invalidate_complete_page(mapping, page);
}

page_mapped によって「ページテーブルに map されているか (プロセスのアドレス空間に属していると解釈してOK? ) 」を確かめ、 map されていたら invalidate_complete_page しない、つまり、ページキャッシュの破棄をしない

/*
 * Return true if this page is mapped into pagetables.
 */
static inline int page_mapped(struct page *page)
{
    return atomic_read(&(page)->_mapcount) >= 0;
}

mmap(2) したページの場合は page_mapped ( _mapcount ) の結果が 0 以上のカウントになり、除外されるのだろう

struct page {

// ...

            union {
                /*
                * Count of ptes mapped in mms, to show when
                * page is mapped & limit reverse map searches.
                *
                * Extra information about page type may be
                * stored here for pages that are never mapped,
                * in which case the value MUST BE <= -2.
                * See page-flags.h for more details.
                */
                atomic_t _mapcount;

枝葉に別れて細かい疑問がわいてきたが、発散しそうなので ここまでにとどめておく