読者です 読者をやめる 読者になる 読者になる

kmemleakデバッグめも

linux kernel

φ(・・*)ゞ ウーン
久々に見たらびっくりですよΣ(´∀`;)

[  677.488751] kmemleak: 14348 new suspected memory leaks (see /sys/kernel/debug/kmemleak)

ここまでいくとむしろウケますがw

そんなことはさておき、まずは適当なシェルスクリプトでメモリリークの内訳を見てみますと、

#!/bin/sh

modules=`grep "  comm" kmemleak.txt | cut -f2 -d"\\"" | sort | uniq`

for mod in $modules
do
    echo -n $mod ":"
    grep "comm \\"$mod\\"" kmemleak.txt | wc -l
done

このようにsystemdでやたらリークしているのがわかります。

[masami@rune:~]$ ./count.sh
modprobe :5
swapper/0 :3
systemd :14339
udevd :1

これがどんな感じかというと

unreferenced object 0xffff88024ec25c20 (size 32):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.381s)
  hex dump (first 32 bytes):
    ff 0f 00 00 23 04 00 00 07 00 00 00 00 00 00 00  ....#...........
    b0 09 65 51 02 88 ff ff 00 00 00 00 00 00 00 00  ..eQ............
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116b096>] kmem_cache_alloc_trace+0xb6/0x160
    [<ffffffff8128331c>] policydb_read+0x100c/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff88024ec24408 (size 8):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.392s)
  hex dump (first 8 bytes):
    62 09 00 00 02 88 ff ff                          b.......
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116b096>] kmem_cache_alloc_trace+0xb6/0x160
    [<ffffffff81283347>] policydb_read+0x1037/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff8802516509b0 (size 16):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.392s)
  hex dump (first 16 bytes):
    63 65 72 74 69 66 69 63 61 74 65 73 00 92 63 00  certificates..c.
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116acb8>] __kmalloc+0x138/0x1a0
    [<ffffffff81283382>] policydb_read+0x1072/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff88024ec25c40 (size 32):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.392s)
  hex dump (first 32 bytes):
    ff 0f 00 00 f7 02 00 00 07 00 00 00 00 00 00 00  ................
    18 44 c2 4e 02 88 ff ff 00 00 00 00 00 00 00 00  .D.N............
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116b096>] kmem_cache_alloc_trace+0xb6/0x160
    [<ffffffff8128331c>] policydb_read+0x100c/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff88024ec24410 (size 8):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.392s)
  hex dump (first 8 bytes):
    ea 0a 00 00 02 88 ff ff                          ........
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116b096>] kmem_cache_alloc_trace+0xb6/0x160
    [<ffffffff81283347>] policydb_read+0x1037/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff
unreferenced object 0xffff88024ec24418 (size 8):
  comm "systemd", pid 1, jiffies 4294677927 (age 7500.392s)
  hex dump (first 8 bytes):
    62 69 6e 00 02 88 ff ff                          bin.....
  backtrace:
    [<ffffffff815dc5db>] kmemleak_alloc+0x5b/0xc0
    [<ffffffff8116acb8>] __kmalloc+0x138/0x1a0
    [<ffffffff81283382>] policydb_read+0x1072/0x1260
    [<ffffffff81287a99>] security_load_policy+0x59/0x480
    [<ffffffff8127a831>] sel_write_load+0xa1/0x720
    [<ffffffff81180eac>] vfs_write+0xac/0x180
    [<ffffffff811811da>] sys_write+0x4a/0x90
    [<ffffffff81605029>] system_call_fastpath+0x16/0x1b
    [<ffffffffffffffff>] 0xffffffffffffffff

commがsystemdなものはほぼこのパターンでした。
まずはkmalloc()の呼び出しを行っているpolicydb_read()から見ていきますか。
メモリリークの場合mallocを呼び出した関数が原因とは限らないですが、最初にチェックする場所として、あとはpolicydb_read()は戻り値はintなのでこの中で完結している可能性もありますしね。違う可能性も当然ありますが。