This is a note to let you know that I've just added the patch titled eventfs: Stop using dcache_readdir() for getdents() to the 6.6-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: eventfs-stop-using-dcache_readdir-for-getdents.patch and it can be found in the queue-6.6 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From SRS0=eEWY=JP=rostedt.homelinux.com=rostedt@xxxxxxxxxx Tue Feb 6 13:11:08 2024 From: Steven Rostedt <rostedt@xxxxxxxxxxx> Date: Tue, 06 Feb 2024 07:09:41 -0500 Subject: eventfs: Stop using dcache_readdir() for getdents() To: linux-kernel@xxxxxxxxxxxxxxx, stable@xxxxxxxxxxxxxxx Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>, Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>, Sasha Levin <sashal@xxxxxxxxxx>, Masami Hiramatsu <mhiramat@xxxxxxxxxx>, Mark Rutland <mark.rutland@xxxxxxx>, Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>, Ajay Kaher <akaher@xxxxxxxxxx>, Al Viro <viro@xxxxxxxxxxxxxxxxxx>, Christian Brauner <brauner@xxxxxxxxxx> Message-ID: <20240206120952.237926780@xxxxxxxxxxxxxxxxxxxxx> From: "Steven Rostedt (Google)" <rostedt@xxxxxxxxxxx> commit 493ec81a8fb8e4ada6f223b8b73791a1280d4774 upstream. The eventfs creates dynamically allocated dentries and inodes. Using the dcache_readdir() logic for its own directory lookups requires hiding the cursor of the dcache logic and playing games to allow the dcache_readdir() to still have access to the cursor while the eventfs saved what it created and what it needs to release. Instead, just have eventfs have its own iterate_shared callback function that will fill in the dent entries. This simplifies the code quite a bit. Link: https://lore.kernel.org/linux-trace-kernel/20240104015435.682218477@xxxxxxxxxxx Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx> Cc: Mark Rutland <mark.rutland@xxxxxxx> Cc: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx> Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> Cc: Ajay Kaher <akaher@xxxxxxxxxx> Cc: Al Viro <viro@xxxxxxxxxxxxxxxxxx> Cc: Christian Brauner <brauner@xxxxxxxxxx> Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/tracefs/event_inode.c | 194 +++++++++++++++-------------------------------- 1 file changed, 64 insertions(+), 130 deletions(-) --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -52,9 +52,7 @@ enum { static struct dentry *eventfs_root_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags); -static int dcache_dir_open_wrapper(struct inode *inode, struct file *file); -static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx); -static int eventfs_release(struct inode *inode, struct file *file); +static int eventfs_iterate(struct file *file, struct dir_context *ctx); static void update_attr(struct eventfs_attr *attr, struct iattr *iattr) { @@ -148,11 +146,9 @@ static const struct inode_operations eve }; static const struct file_operations eventfs_file_operations = { - .open = dcache_dir_open_wrapper, .read = generic_read_dir, - .iterate_shared = dcache_readdir_wrapper, + .iterate_shared = eventfs_iterate, .llseek = generic_file_llseek, - .release = eventfs_release, }; /* Return the evenfs_inode of the "events" directory */ @@ -643,128 +639,87 @@ static struct dentry *eventfs_root_looku return ret; } -struct dentry_list { - void *cursor; - struct dentry **dentries; -}; - -/** - * eventfs_release - called to release eventfs file/dir - * @inode: inode to be released - * @file: file to be released (not used) - */ -static int eventfs_release(struct inode *inode, struct file *file) -{ - struct tracefs_inode *ti; - struct dentry_list *dlist = file->private_data; - void *cursor; - int i; - - ti = get_tracefs(inode); - if (!(ti->flags & TRACEFS_EVENT_INODE)) - return -EINVAL; - - if (WARN_ON_ONCE(!dlist)) - return -EINVAL; - - for (i = 0; dlist->dentries && dlist->dentries[i]; i++) { - dput(dlist->dentries[i]); - } - - cursor = dlist->cursor; - kfree(dlist->dentries); - kfree(dlist); - file->private_data = cursor; - return dcache_dir_close(inode, file); -} - -static int add_dentries(struct dentry ***dentries, struct dentry *d, int cnt) -{ - struct dentry **tmp; - - tmp = krealloc(*dentries, sizeof(d) * (cnt + 2), GFP_NOFS); - if (!tmp) - return -1; - tmp[cnt] = d; - tmp[cnt + 1] = NULL; - *dentries = tmp; - return 0; -} - -/** - * dcache_dir_open_wrapper - eventfs open wrapper - * @inode: not used - * @file: dir to be opened (to create it's children) - * - * Used to dynamic create file/dir with-in @file, all the - * file/dir will be created. If already created then references - * will be increased +/* + * Walk the children of a eventfs_inode to fill in getdents(). */ -static int dcache_dir_open_wrapper(struct inode *inode, struct file *file) +static int eventfs_iterate(struct file *file, struct dir_context *ctx) { const struct file_operations *fops; + struct inode *f_inode = file_inode(file); const struct eventfs_entry *entry; struct eventfs_inode *ei_child; struct tracefs_inode *ti; struct eventfs_inode *ei; - struct dentry_list *dlist; - struct dentry **dentries = NULL; - struct dentry *parent = file_dentry(file); - struct dentry *d; - struct inode *f_inode = file_inode(file); - const char *name = parent->d_name.name; + struct dentry *ei_dentry = NULL; + struct dentry *dentry; + const char *name; umode_t mode; - void *data; - int cnt = 0; int idx; - int ret; - int i; - int r; + int ret = -EINVAL; + int ino; + int i, r, c; + + if (!dir_emit_dots(file, ctx)) + return 0; ti = get_tracefs(f_inode); if (!(ti->flags & TRACEFS_EVENT_INODE)) return -EINVAL; - if (WARN_ON_ONCE(file->private_data)) - return -EINVAL; + c = ctx->pos - 2; idx = srcu_read_lock(&eventfs_srcu); mutex_lock(&eventfs_mutex); ei = READ_ONCE(ti->private); + if (ei && !ei->is_freed) + ei_dentry = READ_ONCE(ei->dentry); mutex_unlock(&eventfs_mutex); - if (!ei) { - srcu_read_unlock(&eventfs_srcu, idx); - return -EINVAL; - } - - - data = ei->data; + if (!ei || !ei_dentry) + goto out; - dlist = kmalloc(sizeof(*dlist), GFP_KERNEL); - if (!dlist) { - srcu_read_unlock(&eventfs_srcu, idx); - return -ENOMEM; - } + ret = 0; - inode_lock(parent->d_inode); + /* + * Need to create the dentries and inodes to have a consistent + * inode number. + */ list_for_each_entry_srcu(ei_child, &ei->children, list, srcu_read_lock_held(&eventfs_srcu)) { - d = create_dir_dentry(ei, ei_child, parent); - if (d) { - ret = add_dentries(&dentries, d, cnt); - dput(d); - if (ret < 0) - break; - cnt++; + + if (c > 0) { + c--; + continue; } + + if (ei_child->is_freed) + continue; + + name = ei_child->name; + + dentry = create_dir_dentry(ei, ei_child, ei_dentry); + if (!dentry) + goto out; + ino = dentry->d_inode->i_ino; + dput(dentry); + + if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) + goto out; + ctx->pos++; } for (i = 0; i < ei->nr_entries; i++) { - void *cdata = data; + void *cdata = ei->data; + + if (c > 0) { + c--; + continue; + } + entry = &ei->entries[i]; name = entry->name; + mutex_lock(&eventfs_mutex); /* If ei->is_freed, then the event itself may be too */ if (!ei->is_freed) @@ -774,42 +729,21 @@ static int dcache_dir_open_wrapper(struc mutex_unlock(&eventfs_mutex); if (r <= 0) continue; - d = create_file_dentry(ei, i, parent, name, mode, cdata, fops); - if (d) { - ret = add_dentries(&dentries, d, cnt); - dput(d); - if (ret < 0) - break; - cnt++; - } - } - inode_unlock(parent->d_inode); - srcu_read_unlock(&eventfs_srcu, idx); - ret = dcache_dir_open(inode, file); - /* - * dcache_dir_open() sets file->private_data to a dentry cursor. - * Need to save that but also save all the dentries that were - * opened by this function. - */ - dlist->cursor = file->private_data; - dlist->dentries = dentries; - file->private_data = dlist; - return ret; -} + dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); + if (!dentry) + goto out; + ino = dentry->d_inode->i_ino; + dput(dentry); -/* - * This just sets the file->private_data back to the cursor and back. - */ -static int dcache_readdir_wrapper(struct file *file, struct dir_context *ctx) -{ - struct dentry_list *dlist = file->private_data; - int ret; + if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) + goto out; + ctx->pos++; + } + ret = 1; + out: + srcu_read_unlock(&eventfs_srcu, idx); - file->private_data = dlist->cursor; - ret = dcache_readdir(file, ctx); - dlist->cursor = file->private_data; - file->private_data = dlist; return ret; } Patches currently in stable-queue which might be from rostedt@xxxxxxxxxx are queue-6.6/eventfs-keep-all-directory-links-at-1.patch queue-6.6/eventfs-make-sure-that-parent-d_inode-is-locked-in-creating-files-dirs.patch queue-6.6/eventfs-save-directory-inodes-in-the-eventfs_inode-structure.patch queue-6.6/revert-eventfs-save-ownership-and-mode.patch queue-6.6/tracefs-zero-out-the-tracefs_inode-when-allocating-it.patch queue-6.6/eventfs-read-ei-entries-before-ei-children-in-eventfs_iterate.patch queue-6.6/eventfs-do-not-invalidate-dentry-in-create_file-dir_dentry.patch queue-6.6/eventfs-fix-file-and-directory-uid-and-gid-ownership.patch queue-6.6/eventfs-remove-lookup-parameter-from-create_dir-file_dentry.patch queue-6.6/eventfs-use-gfp_nofs-for-allocation-when-eventfs_mutex-is-held.patch queue-6.6/eventfs-remove-fsnotify-functions-from-lookup.patch queue-6.6/eventfs-use-err_cast-in-eventfs_create_events_dir.patch queue-6.6/revert-eventfs-use-simple_recursive_removal-to-clean-up-dentries.patch queue-6.6/eventfs-use-simple_recursive_removal-to-clean-up-dentries.patch queue-6.6/eventfs-stop-using-dcache_readdir-for-getdents.patch queue-6.6/eventfs-have-event-files-and-directories-default-to-parent-uid-and-gid.patch queue-6.6/eventfs-use-eventfs_remove_events_dir.patch queue-6.6/eventfs-delete-eventfs_inode-when-the-last-dentry-is-freed.patch queue-6.6/tracefs-avoid-using-the-ei-dentry-pointer-unnecessarily.patch queue-6.6/tracefs-remove-stale-update_gid-code.patch queue-6.6/eventfs-initialize-the-tracefs-inode-properly.patch queue-6.6/eventfs-remove-special-processing-of-dput-of-events-directory.patch queue-6.6/eventfs-save-ownership-and-mode.patch queue-6.6/tracefs-check-for-dentry-d_inode-exists-in-set_gid.patch queue-6.6/eventfs-do-ctx-pos-update-for-all-iterations-in-eventfs_iterate.patch queue-6.6/tracefs-dentry-lookup-crapectomy.patch queue-6.6/eventfs-move-taking-of-inode_lock-into-dcache_dir_open_wrapper.patch queue-6.6/eventfs-have-a-free_ei-that-just-frees-the-eventfs_inode.patch queue-6.6/revert-eventfs-remove-is_freed-union-with-rcu-head.patch queue-6.6/eventfs-have-the-inodes-all-for-files-and-directories-all-be-the-same.patch queue-6.6/eventfs-use-kcalloc-instead-of-kzalloc.patch queue-6.6/eventfs-test-for-ei-is_freed-when-accessing-ei-dentry.patch queue-6.6/eventfs-fix-bitwise-fields-for-is_events.patch queue-6.6/eventfs-fix-warn_on-in-create_file_dentry.patch queue-6.6/eventfs-fix-events-beyond-name_max-blocking-tasks.patch queue-6.6/eventfs-shortcut-eventfs_iterate-by-skipping-entries-already-read.patch queue-6.6/revert-eventfs-do-not-allow-null-parent-to-eventfs_start_creating.patch queue-6.6/eventfs-do-not-allow-null-parent-to-eventfs_start_creating.patch queue-6.6/eventfs-do-not-create-dentries-nor-inodes-in-iterate_shared.patch queue-6.6/eventfs-have-eventfs_iterate-stop-immediately-if-ei-is_freed-is-set.patch queue-6.6/eventfs-fix-typo-in-eventfs_inode-union-comment.patch queue-6.6/eventfs-remove-expectation-that-ei-is_freed-means-ei-dentry-null.patch queue-6.6/eventfs-restructure-eventfs_inode-structure-to-be-more-condensed.patch queue-6.6/eventfs-warn-if-an-eventfs_inode-is-freed-without-is_freed-being-set.patch queue-6.6/eventfs-get-rid-of-dentry-pointers-without-refcounts.patch queue-6.6/eventfs-remove-unused-d_parent-pointer-field.patch queue-6.6/eventfs-hold-eventfs_mutex-when-calling-callback-functions.patch queue-6.6/eventfs-remove-is_freed-union-with-rcu-head.patch queue-6.6/tracefs-eventfs-modify-mismatched-function-name.patch queue-6.6/eventfs-fix-kerneldoc-of-eventfs_remove_rec.patch queue-6.6/tracefs-eventfs-use-root-and-instance-inodes-as-default-ownership.patch queue-6.6/revert-eventfs-check-for-null-ef-in-eventfs_set_attr.patch queue-6.6/eventfs-fix-failure-path-in-eventfs_create_events_dir.patch queue-6.6/eventfs-clean-up-dentry-ops-and-add-revalidate-function.patch