diff mbox series

tracefs/eventfs: Free top level files on removal

Message ID 20230907175859.6fedbaa2@gandalf.local.home (mailing list archive)
State Accepted
Commit d508ee2dd5574b1e84d140b927c25807c9b66d7e
Headers show
Series tracefs/eventfs: Free top level files on removal | expand

Commit Message

Steven Rostedt Sept. 7, 2023, 9:58 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

When an instance is removed, the top level files of the eventfs directory
are not cleaned up. Call the eventfs_remove() on each of the entries to
free them.

This was found via kmemleak:

unreferenced object 0xffff8881047c1280 (size 96):
  comm "mkdir", pid 924, jiffies 4294906489 (age 2013.077s)
  hex dump (first 32 bytes):
    18 31 ed 03 81 88 ff ff 00 31 09 24 81 88 ff ff  .1.......1.$....
    00 00 00 00 00 00 00 00 98 19 7c 04 81 88 ff ff  ..........|.....
  backtrace:
    [<000000000fa46b4d>] kmalloc_trace+0x2a/0xa0
    [<00000000e729cd0c>] eventfs_prepare_ef.constprop.0+0x3a/0x160
    [<000000009032e6a8>] eventfs_add_events_file+0xa0/0x160
    [<00000000fe968442>] create_event_toplevel_files+0x6f/0x130
    [<00000000e364d173>] event_trace_add_tracer+0x14/0x140
    [<00000000411840fa>] trace_array_create_dir+0x52/0xf0
    [<00000000967804fa>] trace_array_create+0x208/0x370
    [<00000000da505565>] instance_mkdir+0x6b/0xb0
    [<00000000dc1215af>] tracefs_syscall_mkdir+0x5b/0x90
    [<00000000a8aca289>] vfs_mkdir+0x272/0x380
    [<000000007709b242>] do_mkdirat+0xfc/0x1d0
    [<00000000c0b6d219>] __x64_sys_mkdir+0x78/0xa0
    [<0000000097b5dd4b>] do_syscall_64+0x3f/0x90
    [<00000000a3f00cfa>] entry_SYSCALL_64_after_hwframe+0x6e/0xd8
unreferenced object 0xffff888103ed3118 (size 8):
  comm "mkdir", pid 924, jiffies 4294906489 (age 2013.077s)
  hex dump (first 8 bytes):
    65 6e 61 62 6c 65 00 00                          enable..
  backtrace:
    [<0000000010f75127>] __kmalloc_node_track_caller+0x51/0x160
    [<000000004b3eca91>] kstrdup+0x34/0x60
    [<0000000050074d7a>] eventfs_prepare_ef.constprop.0+0x53/0x160
    [<000000009032e6a8>] eventfs_add_events_file+0xa0/0x160
    [<00000000fe968442>] create_event_toplevel_files+0x6f/0x130
    [<00000000e364d173>] event_trace_add_tracer+0x14/0x140
    [<00000000411840fa>] trace_array_create_dir+0x52/0xf0
    [<00000000967804fa>] trace_array_create+0x208/0x370
    [<00000000da505565>] instance_mkdir+0x6b/0xb0
    [<00000000dc1215af>] tracefs_syscall_mkdir+0x5b/0x90
    [<00000000a8aca289>] vfs_mkdir+0x272/0x380
    [<000000007709b242>] do_mkdirat+0xfc/0x1d0
    [<00000000c0b6d219>] __x64_sys_mkdir+0x78/0xa0
    [<0000000097b5dd4b>] do_syscall_64+0x3f/0x90
    [<00000000a3f00cfa>] entry_SYSCALL_64_after_hwframe+0x6e/0xd8

Fixes: 5bdcd5f5331a2 eventfs: ("Implement removal of meta data from eventfs")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---

This is on top of:

   https://lore.kernel.org/linux-trace-kernel/20230907024710.866917011@goodmis.org/

 fs/tracefs/event_inode.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

Comments

Masami Hiramatsu (Google) Sept. 8, 2023, 8:48 a.m. UTC | #1
On Thu, 7 Sep 2023 17:58:59 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> When an instance is removed, the top level files of the eventfs directory
> are not cleaned up. Call the eventfs_remove() on each of the entries to
> free them.
> 
> This was found via kmemleak:
> 
> unreferenced object 0xffff8881047c1280 (size 96):
>   comm "mkdir", pid 924, jiffies 4294906489 (age 2013.077s)
>   hex dump (first 32 bytes):
>     18 31 ed 03 81 88 ff ff 00 31 09 24 81 88 ff ff  .1.......1.$....
>     00 00 00 00 00 00 00 00 98 19 7c 04 81 88 ff ff  ..........|.....
>   backtrace:
>     [<000000000fa46b4d>] kmalloc_trace+0x2a/0xa0
>     [<00000000e729cd0c>] eventfs_prepare_ef.constprop.0+0x3a/0x160
>     [<000000009032e6a8>] eventfs_add_events_file+0xa0/0x160
>     [<00000000fe968442>] create_event_toplevel_files+0x6f/0x130
>     [<00000000e364d173>] event_trace_add_tracer+0x14/0x140
>     [<00000000411840fa>] trace_array_create_dir+0x52/0xf0
>     [<00000000967804fa>] trace_array_create+0x208/0x370
>     [<00000000da505565>] instance_mkdir+0x6b/0xb0
>     [<00000000dc1215af>] tracefs_syscall_mkdir+0x5b/0x90
>     [<00000000a8aca289>] vfs_mkdir+0x272/0x380
>     [<000000007709b242>] do_mkdirat+0xfc/0x1d0
>     [<00000000c0b6d219>] __x64_sys_mkdir+0x78/0xa0
>     [<0000000097b5dd4b>] do_syscall_64+0x3f/0x90
>     [<00000000a3f00cfa>] entry_SYSCALL_64_after_hwframe+0x6e/0xd8
> unreferenced object 0xffff888103ed3118 (size 8):
>   comm "mkdir", pid 924, jiffies 4294906489 (age 2013.077s)
>   hex dump (first 8 bytes):
>     65 6e 61 62 6c 65 00 00                          enable..
>   backtrace:
>     [<0000000010f75127>] __kmalloc_node_track_caller+0x51/0x160
>     [<000000004b3eca91>] kstrdup+0x34/0x60
>     [<0000000050074d7a>] eventfs_prepare_ef.constprop.0+0x53/0x160
>     [<000000009032e6a8>] eventfs_add_events_file+0xa0/0x160
>     [<00000000fe968442>] create_event_toplevel_files+0x6f/0x130
>     [<00000000e364d173>] event_trace_add_tracer+0x14/0x140
>     [<00000000411840fa>] trace_array_create_dir+0x52/0xf0
>     [<00000000967804fa>] trace_array_create+0x208/0x370
>     [<00000000da505565>] instance_mkdir+0x6b/0xb0
>     [<00000000dc1215af>] tracefs_syscall_mkdir+0x5b/0x90
>     [<00000000a8aca289>] vfs_mkdir+0x272/0x380
>     [<000000007709b242>] do_mkdirat+0xfc/0x1d0
>     [<00000000c0b6d219>] __x64_sys_mkdir+0x78/0xa0
>     [<0000000097b5dd4b>] do_syscall_64+0x3f/0x90
>     [<00000000a3f00cfa>] entry_SYSCALL_64_after_hwframe+0x6e/0xd8
> 
> Fixes: 5bdcd5f5331a2 eventfs: ("Implement removal of meta data from eventfs")
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Looks good to me.

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thank you,
> ---
> 
> This is on top of:
> 
>    https://lore.kernel.org/linux-trace-kernel/20230907024710.866917011@goodmis.org/
> 
>  fs/tracefs/event_inode.c | 30 ++++++++++++++++++++++++++----
>  1 file changed, 26 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c
> index 609ccb5b7cfc..f168aca45458 100644
> --- a/fs/tracefs/event_inode.c
> +++ b/fs/tracefs/event_inode.c
> @@ -195,17 +195,39 @@ void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry)
>  {
>  	struct tracefs_inode *ti_parent;
>  	struct eventfs_inode *ei;
> -	struct eventfs_file *ef;
> -
> -	mutex_lock(&eventfs_mutex);
> +	struct eventfs_file *ef, *tmp;
>  
>  	/* The top level events directory may be freed by this */
>  	if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) {
> +		LIST_HEAD(ef_del_list);
> +
> +		mutex_lock(&eventfs_mutex);
> +
>  		ei = ti->private;
> +
> +		/* Record all the top level files */
> +		list_for_each_entry_srcu(ef, &ei->e_top_files, list,
> +					 lockdep_is_held(&eventfs_mutex)) {
> +			list_add_tail(&ef->del_list, &ef_del_list);
> +		}
> +
> +		/* Nothing should access this, but just in case! */
> +		ti->private = NULL;
> +
> +		mutex_unlock(&eventfs_mutex);
> +
> +		/* Now safely free the top level files and their children */
> +		list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
> +			list_del(&ef->del_list);
> +			eventfs_remove(ef);
> +		}
> +
>  		kfree(ei);
> -		goto out;
> +		return;
>  	}
>  
> +	mutex_lock(&eventfs_mutex);
> +
>  	ti_parent = get_tracefs(dentry->d_parent->d_inode);
>  	if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
>  		goto out;
> -- 
> 2.40.1
>
diff mbox series

Patch

diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c
index 609ccb5b7cfc..f168aca45458 100644
--- a/fs/tracefs/event_inode.c
+++ b/fs/tracefs/event_inode.c
@@ -195,17 +195,39 @@  void eventfs_set_ef_status_free(struct tracefs_inode *ti, struct dentry *dentry)
 {
 	struct tracefs_inode *ti_parent;
 	struct eventfs_inode *ei;
-	struct eventfs_file *ef;
-
-	mutex_lock(&eventfs_mutex);
+	struct eventfs_file *ef, *tmp;
 
 	/* The top level events directory may be freed by this */
 	if (unlikely(ti->flags & TRACEFS_EVENT_TOP_INODE)) {
+		LIST_HEAD(ef_del_list);
+
+		mutex_lock(&eventfs_mutex);
+
 		ei = ti->private;
+
+		/* Record all the top level files */
+		list_for_each_entry_srcu(ef, &ei->e_top_files, list,
+					 lockdep_is_held(&eventfs_mutex)) {
+			list_add_tail(&ef->del_list, &ef_del_list);
+		}
+
+		/* Nothing should access this, but just in case! */
+		ti->private = NULL;
+
+		mutex_unlock(&eventfs_mutex);
+
+		/* Now safely free the top level files and their children */
+		list_for_each_entry_safe(ef, tmp, &ef_del_list, del_list) {
+			list_del(&ef->del_list);
+			eventfs_remove(ef);
+		}
+
 		kfree(ei);
-		goto out;
+		return;
 	}
 
+	mutex_lock(&eventfs_mutex);
+
 	ti_parent = get_tracefs(dentry->d_parent->d_inode);
 	if (!ti_parent || !(ti_parent->flags & TRACEFS_EVENT_INODE))
 		goto out;