linux内核潜在的内存泄漏?

时间:2014-09-26 18:56:32

标签: c linux memory-leaks linux-kernel

在对内核泄漏进行静态分析时,我遇到了一个有趣的场景,我无法找到变量的de分配。 分配发生在以下函数中(使用kmalloc调用),如下所示:

static int mounts_open_common(struct inode *inode, struct file *file,
              int (*show)(struct seq_file *, struct vfsmount *)){
  struct proc_mounts *p;

  //some code//
  *p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);**
  file->private_data = &p->m;//the allocated variable is escaped to file structure
  //some code

}

我希望这个分配的内存固定在:

static int mounts_release(struct inode *inode, struct file *file)
{
    struct proc_mounts *p = proc_mounts(file->private_data);
    path_put(&p->root);
    put_mnt_ns(p->ns);
    return seq_release(inode, file);
}

但似乎这个函数正在访问已分配的变量来释放一些其内部成员,而不是变量'p'本身。 那么这个变量的内存在哪里被释放?如果它应该在mounts_release函数中释放,那么它可能会发生内存泄漏。

1 个答案:

答案 0 :(得分:6)

如果你看一下seq_release:

int seq_release(struct inode *inode, struct file *file)
{
        struct seq_file *m = file->private_data;
        kvfree(m->buf);
        kfree(m);
        return 0;
}

有效地kfree(file->private_data)

现在,file->private_data设置为file->private_data = &p->m;

p

您问题中的kmalloc'dmstruct proc_mounts成员不是指针,因此不应允许其被释放。但是,它是struct proc_mounts { struct seq_file m; struct mnt_namespace *ns; struct path root; int (*show)(struct seq_file *, struct vfsmount *); void *cached_mount; u64 cached_event; loff_t cached_index; };

的1.成员
seq_release()

因此mp = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);成员的地址执行kfree(),其地址与使用{{1}}获得的地址相同

我猜这对静态分析仪不是很友好。但是没有内存泄漏。

相关问题