]> Git Repo - linux.git/commitdiff
kernfs: fix memleak in kernel_ops_readdir()
authorAndrea Arcangeli <[email protected]>
Mon, 5 Aug 2019 17:34:04 +0000 (10:34 -0700)
committerGreg Kroah-Hartman <[email protected]>
Mon, 5 Aug 2019 18:34:11 +0000 (20:34 +0200)
If getdents64 is killed or hits on segfault, it'll leave cgroups
directories in sysfs pinned leaking memory because the kernfs node
won't be freed on rmdir and the parent neither.

Repro:

  # for i in `seq 1000`; do mkdir $i; done
  # rmdir *
  # for i in `seq 1000`; do mkdir $i; done
  # rmdir *

  # for i in `seq 1000`; do while :; do ls $i/ >/dev/null; done & done
  # while :; do killall ls; done

  kernfs_node_cache in /proc/slabinfo keeps going up as expected.

Signed-off-by: Andrea Arcangeli <[email protected]>
Signed-off-by: Tejun Heo <[email protected]>
Cc: [email protected] # goes way back to original sysfs days
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
fs/kernfs/dir.c

index a387534c9577855b6ea6d8e2868e4f85c52551b4..1e98efc2bf6d83946b934e2e1b6021c9f09c4fc1 100644 (file)
@@ -1684,11 +1684,14 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
                kernfs_get(pos);
 
                mutex_unlock(&kernfs_mutex);
-               if (!dir_emit(ctx, name, len, ino, type))
-                       return 0;
+               if (unlikely(!dir_emit(ctx, name, len, ino, type))) {
+                       kernfs_put(pos);
+                       goto out;
+               }
                mutex_lock(&kernfs_mutex);
        }
        mutex_unlock(&kernfs_mutex);
+out:
        file->private_data = NULL;
        ctx->pos = INT_MAX;
        return 0;
This page took 0.05602 seconds and 4 git commands to generate.