Proof of Concept: Linux カーネルモジュールで特定のディレクトリ以下の dentry キャッシュを破棄する

動機

社内の同僚がカーネル周りの問題を調べていて slab キャッシュ ( = dentry, inode ) に関する内容を追っていた。 その問題自体の内容は、アレがコレで、伏せておく。

その問題をみているうちに 「特定のディレクトリ以下の slab キャッシュ = dentry / inode キャッシュ を選択的に破棄することはできないのかな?」 という関心がわきいろいろ調べていた。

ビジネスロジックを担当するアプリケーションの実行とは関係ない箇所で大量に slab キャッシュ (dentry, inode) を蓄えてしまうプロセスがいて、そいつらが作ったキャッシュを意図して破棄したいようなユースケースを考えている (例: バックアップやウィルススキャン)

(注意: メモリプレッシャーがかかるとカーネルがよしなに扱ってくれるはずで、「通常」のサーバ用途では slab キャッシュをあれこれ触る必要は無いと思う )

/proc/sys/vm/drop_caches, vm.drop_caches

slab キャッシュを破棄するには

  • echo {1,2,3} > /proc/sys/vm/drop_caches ( 1, 2, 3 のどれか )

あるいは

  • sysctl -w vm.drop_caches={1,2,3} を実行すればよい。

ただし、このインタフェースでは特定のキャッシュを選択的に破棄することはできず 全ての SReclaimable な slab キャッシュを破棄する

実装

/proc/sys/vm/drop_caches 内部では shrink_slab() を読んでおり、 struct shrinker *shrinker という slab キャッシュを破棄するために登録されたコールバック関数をイテレートして slab を破棄している

(ところで、この内容も Qiita で詳しくまとめてる人がいるのでググるとよいですぞ )

static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
                 struct mem_cgroup *memcg,
                 unsigned long nr_scanned,
                 unsigned long nr_eligible)
{
    struct shrinker *shrinker;
    unsigned long freed = 0;

....

    list_for_each_entry(shrinker, &shrinker_list, list) {
        struct shrink_control sc = {
            .gfp_mask = gfp_mask,
            .nid = nid,
            .memcg = memcg,
        };

...

        freed += do_shrink_slab(&sc, shrinker, nr_scanned, nr_eligible);
    }

...

shrink_dcache_parent(), dentry_unhash() を見つける

struct shrinker *shrinker を直接触るのはどうやら無理なようなので、別の方策を探っていた。

ソースを追っていたら shrink_dcache_parent(), dentry_unhash() という関数がそれっぽい用途に使えそうだった

/**
 * shrink_dcache_parent - prune dcache
 * @parent: parent of entries to prune
 *
 * Prune the dcache to remove unused children of the parent dentry.
 */
void shrink_dcache_parent(struct dentry *parent)
{
    for (;;) {
        struct select_data data;

        INIT_LIST_HEAD(&data.dispose);
        data.start = parent;
        data.found = 0;

        d_walk(parent, &data, select_collect, NULL);
        if (!data.found)
            break;

        shrink_dentry_list(&data.dispose);
        cond_resched();
    }
}
EXPORT_SYMBOL(shrink_dcache_parent);

再帰的に dentry のツリーを辿って dentry を破棄するぽい

dentry_unhash() は shrink_dcache_parent() のラッパー

/*
 * The dentry_unhash() helper will try to drop the dentry early: we
 * should have a usage count of 1 if we're the only user of this
 * dentry, and if that is true (possibly after pruning the dcache),
 * then we drop the dentry now.
 *
 * A low-level filesystem can, if it choses, legally
 * do a
 *
 * if (!d_unhashed(dentry))
 *     return -EBUSY;
 *
 * if it cannot handle the case of removing a directory
 * that is still in use by something else..
 */
void dentry_unhash(struct dentry *dentry)
{
    shrink_dcache_parent(dentry);
    spin_lock(&dentry->d_lock);
    if (dentry->d_lockref.count == 1)
        __d_drop(dentry);
    spin_unlock(&dentry->d_lock);
}
EXPORT_SYMBOL(dentry_unhash);

見た所 dentry キャッシュだけを破棄するようで inode キャッシュは扱えないが、まぁ 試してみるだけ試してみよう

Proof of Concept

で、dentry_unhash() を使って、 Proof of Concept として作ったカーネルモジュールがこれ

github.com

肝となるコードは下記の部分だけ

 err = kern_path(buffer, 0, &path);
    if (err)
        return err;

    dentry_unhash(path.dentry);
    path_put(path);

モジュールをビルドしてinsmod して、下記のような debugfs のファイルにパスを write(2) すると、指定したパス = ディレクトリ以下の dentry を破棄する

echo /var/lib >  /sys/kernel/debug/shrink_dcache

dentry_unhash() 自体の使い方が間違っている可能性もあるし、実用に耐えうるようなクオリィテではないので勘弁を

実験

まずは slabtop コマンドで現在の dentry のサイズを調べます

vagrant@second:~/shrink_dcache$ sudo slabtop -o | grep dentry
 17619  10532  59%    0.19K    839       21      3356K dentry                 

3356K ありますね

次にサブディレクトリの negative dentry を生成します ( negative dentry については過去の私の書いたブログがあるので参考にしてください。最近だと、 Qiita などにも詳しい解説がたくさんあります )

hiboma.hatenadiary.jp

vagrant@second:~/shrink_dcache$ mkdir -p /tmp/1/2/3
vagrant@second:~/shrink_dcache$ perl -e 'stat "/tmp/1/2/3/$_" for 1..1000000'

dentry のサイズを調べ直します

vagrant@second:~/shrink_dcache$ sudo slabtop -o | grep dentry
1045380 1045380 100%    0.19K  49780       21    199120K dentry                 

199120K に増えましたね

ここで先ほどのカーネルモジュールを使って dentry を破棄します

# 再帰的に dentry を辿るはずなので上位のディレクトリを起点にして破棄する
vagrant@second:~/shrink_dcache$ echo /tmp/1 | sudo time tee /sys/kernel/debug/shrink_dcache
/tmp/1

もう一度 dentry のサイズを調べ直します

vagrant@second:~/shrink_dcache$ sudo slabtop -o | grep dentry
 50358  42113  83%    0.19K   2398       21      9592K dentry                 

おお、9592K に減っていますね.

まとめ

  • とりあえず PoC として、特定のディレクトリ以下の dentry を破棄するのはできた
  • inode キャッシュも破棄できたらいいのだが、inode キャッシュの破棄を実行する関数はファイルシステムごとにカプセル化されているようなコードになっていて、任意の場所から触れるようなもんでもないっぽい