* [PATCH v2 0/2] eventfs: Create dentries and inodes at dir open
@ 2024-01-16 21:12 Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 1/2] eventfs: Have the inodes all for files and directories all be the same Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 2/2] eventfs: Create list of files and directories at dir open Steven Rostedt
0 siblings, 2 replies; 4+ messages in thread
From: Steven Rostedt @ 2024-01-16 21:12 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Linus Torvalds,
Christian Brauner, Al Viro, Ajay Kaher, linux-fsdevel
[ subject is wrong, but is to match v1, see patch 2 for correct subject ]
Fix reading dir again, this time without creating dentries and inodes.
Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240116114711.7e8637be@gandalf.local.home
Steven Rostedt (Google) (2):
eventfs: Have the inodes all for files and directories all be the same
eventfs: Create list of files and directories at dir open
----
fs/tracefs/event_inode.c | 223 +++++++++++++++++++++++++++++++++--------------
1 file changed, 159 insertions(+), 64 deletions(-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] eventfs: Have the inodes all for files and directories all be the same
2024-01-16 21:12 [PATCH v2 0/2] eventfs: Create dentries and inodes at dir open Steven Rostedt
@ 2024-01-16 21:12 ` Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 2/2] eventfs: Create list of files and directories at dir open Steven Rostedt
1 sibling, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2024-01-16 21:12 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Linus Torvalds,
Christian Brauner, Al Viro, Ajay Kaher, linux-fsdevel
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
The dentries and inodes are created in the readdir for the sole purpose of
getting a consistent inode number. Linus stated that is unnecessary, and
that all inodes can have the same inode number. For a virtual file system
they are pretty meaningless.
Instead use a single unique inode number for all files and one for all
directories.
Link: https://lore.kernel.org/all/20240116133753.2808d45e@gandalf.local.home/
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
fs/tracefs/event_inode.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c
index fdff53d5a1f8..5edf0b96758b 100644
--- a/fs/tracefs/event_inode.c
+++ b/fs/tracefs/event_inode.c
@@ -32,6 +32,10 @@
*/
static DEFINE_MUTEX(eventfs_mutex);
+/* Choose something "unique" ;-) */
+#define EVENTFS_FILE_INODE_INO 0x12c4e37
+#define EVENTFS_DIR_INODE_INO 0x134b2f5
+
/*
* The eventfs_inode (ei) itself is protected by SRCU. It is released from
* its parent's list and will have is_freed set (under eventfs_mutex).
@@ -352,6 +356,9 @@ static struct dentry *create_file(const char *name, umode_t mode,
inode->i_fop = fop;
inode->i_private = data;
+ /* All files will have the same inode number */
+ inode->i_ino = EVENTFS_FILE_INODE_INO;
+
ti = get_tracefs(inode);
ti->flags |= TRACEFS_EVENT_INODE;
d_instantiate(dentry, inode);
@@ -388,6 +395,9 @@ static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent
inode->i_op = &eventfs_root_dir_inode_operations;
inode->i_fop = &eventfs_file_operations;
+ /* All directories will have the same inode number */
+ inode->i_ino = EVENTFS_DIR_INODE_INO;
+
ti = get_tracefs(inode);
ti->flags |= TRACEFS_EVENT_INODE;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] eventfs: Create list of files and directories at dir open
2024-01-16 21:12 [PATCH v2 0/2] eventfs: Create dentries and inodes at dir open Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 1/2] eventfs: Have the inodes all for files and directories all be the same Steven Rostedt
@ 2024-01-16 21:12 ` Steven Rostedt
[not found] ` <CAHk-=wgjSuapZoWfQZMyFi80wJE6a=vjOdgpy_k+YaWwbX9Pig@mail.gmail.com>
1 sibling, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2024-01-16 21:12 UTC (permalink / raw)
To: linux-kernel, linux-trace-kernel
Cc: Masami Hiramatsu, Mark Rutland, Mathieu Desnoyers, Linus Torvalds,
Christian Brauner, Al Viro, Ajay Kaher, linux-fsdevel,
kernel test robot
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
The original eventfs code added a wrapper around the dcache_readdir open
callback and created all the dentries and inodes at open, and increment
their ref count. A wrapper was added around the dcache_readdir release
function to decrement all the ref counts of those created inodes and
dentries. But this proved to be buggy[1] for when a kprobe was created
during a dir read, it would create a dentry between the open and the
release, and because the release would decrement all ref counts of all
files and directories, that would include the kprobe directory that was
not there to have its ref count incremented in open. This would cause the
ref count to go to negative and later crash the kernel.
To solve this, the dentries and inodes that were created and had their ref
count upped in open needed to be saved. That list needed to be passed from
the open to the release, so that the release would only decrement the ref
counts of the entries that were incremented in the open.
Unfortunately, the dcache_readdir logic was already using the
file->private_data, which is the only field that can be used to pass
information from the open to the release. What was done was the eventfs
created another descriptor that had a void pointer to save the
dcache_readdir pointer, and it wrapped all the callbacks, so that it could
save the list of entries that had their ref counts incremented in the
open, and pass it to the release. The wrapped callbacks would just put
back the dcache_readdir pointer and call the functions it used so it could
still use its data[2].
But Linus had an issue with the "hijacking" of the file->private_data
(unfortunately this discussion was on a security list, so no public link).
Which we finally agreed on doing everything within the iterate_shared
callback and leave the dcache_readdir out of it[3]. All the information
needed for the getents() could be created then.
But this ended up being buggy too[4]. The iterate_shared callback was not
the right place to create the dentries and inodes. Even Christian Brauner
had issues with that[5].
An attempt was to go back to creating the inodes and dentries at
the open, create an array to store the information in the
file->private_data, and pass that information to the other callbacks.[6]
The difference between that and the original method, is that it does not
use dcache_readdir. It also does not up the ref counts of the dentries and
pass them. Instead, it creates an array of a structure that saves the
dentry's name and inode number. That information is used in the
iterate_shared callback, and the array is freed in the dir release. The
dentries and inodes created in the open are not used for the iterate_share
or release callbacks. Just their names and inode numbers.
Linus did not like the creation of the dentries for the sole purpose of
getting their inode numbers. Instead, he suggested to just hard code them
even though they would not be unique. With the hard coding of the inode
numbers, there's no need to create the dentries for readdir().
Now the list created at the dir open can just get the names from the meta
data and use the hard coded inode numbers.
This means that the state of the eventfs at the dir open remains the same
from the point of view of the getdents() function, until the dir is closed.
This also means that the getdents() will not fail. If there's an issue, it
fails at the dir open.
[1] https://lore.kernel.org/linux-trace-kernel/20230919211804.230edf1e@gandalf.local.home/
[2] https://lore.kernel.org/linux-trace-kernel/20230922163446.1431d4fa@gandalf.local.home/
[3] https://lore.kernel.org/linux-trace-kernel/20240104015435.682218477@goodmis.org/
[4] https://lore.kernel.org/all/202401152142.bfc28861-oliver.sang@intel.com/
[5] https://lore.kernel.org/all/20240111-unzahl-gefegt-433acb8a841d@brauner/
[6] https://lore.kernel.org/all/20240116114711.7e8637be@gandalf.local.home/
Fixes: 493ec81a8fb8 ("eventfs: Stop using dcache_readdir() for getdents()")
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202401152142.bfc28861-oliver.sang@intel.com
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
fs/tracefs/event_inode.c | 213 +++++++++++++++++++++++++++------------
1 file changed, 149 insertions(+), 64 deletions(-)
diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c
index 5edf0b96758b..05cd825d4cc9 100644
--- a/fs/tracefs/event_inode.c
+++ b/fs/tracefs/event_inode.c
@@ -57,7 +57,9 @@ enum {
static struct dentry *eventfs_root_lookup(struct inode *dir,
struct dentry *dentry,
unsigned int flags);
+static int eventfs_dir_open(struct inode *inode, struct file *file);
static int eventfs_iterate(struct file *file, struct dir_context *ctx);
+static int eventfs_dir_release(struct inode *inode, struct file *file);
static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
{
@@ -216,7 +218,9 @@ static const struct inode_operations eventfs_file_inode_operations = {
static const struct file_operations eventfs_file_operations = {
.read = generic_read_dir,
+ .open = eventfs_dir_open,
.iterate_shared = eventfs_iterate,
+ .release = eventfs_dir_release,
.llseek = generic_file_llseek,
};
@@ -716,117 +720,198 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,
return ret;
}
+struct eventfs_dents {
+ const char *name;
+ int ino;
+ int type;
+};
+
+struct eventfs_list {
+ struct eventfs_dents *dents;
+ int count;
+};
+
+static int update_entry(struct eventfs_dents *dents, const char *name,
+ int type, int cnt)
+{
+ dents[cnt].name = kstrdup_const(name, GFP_KERNEL);
+ if (!dents[cnt].name)
+ return -ENOMEM;
+ if (type == DT_DIR)
+ dents[cnt].ino = EVENTFS_DIR_INODE_INO;
+ else
+ dents[cnt].ino = EVENTFS_FILE_INODE_INO;
+ dents[cnt].type = type;
+ return 0;
+}
+
+static int add_entry(struct eventfs_dents **edents, const char *name,
+ int type, int cnt)
+{
+ struct eventfs_dents *tmp;
+
+ tmp = krealloc(*edents, sizeof(**edents) * (cnt + 1), GFP_NOFS);
+ if (!tmp)
+ return -ENOMEM;
+ *edents = tmp;
+
+ return update_entry(tmp, name, type, cnt);
+}
+
/*
* Walk the children of a eventfs_inode to fill in getdents().
*/
-static int eventfs_iterate(struct file *file, struct dir_context *ctx)
+static int eventfs_dir_open(struct inode *inode, struct file *file)
{
const struct file_operations *fops;
struct inode *f_inode = file_inode(file);
const struct eventfs_entry *entry;
+ struct eventfs_list *edents;
struct eventfs_inode *ei_child;
struct tracefs_inode *ti;
struct eventfs_inode *ei;
- struct dentry *ei_dentry = NULL;
- struct dentry *dentry;
+ struct eventfs_dents *dents;
const char *name;
umode_t mode;
+ void *data;
+ int cnt = 0;
int idx;
- int ret = -EINVAL;
- int ino;
- int i, r, c;
-
- if (!dir_emit_dots(file, ctx))
- return 0;
+ int ret;
+ int i;
+ int r;
ti = get_tracefs(f_inode);
if (!(ti->flags & TRACEFS_EVENT_INODE))
return -EINVAL;
- c = ctx->pos - 2;
+ if (WARN_ON_ONCE(file->private_data))
+ return -EINVAL;
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);
+ if (ei && ei->is_freed)
+ ei = NULL;
mutex_unlock(&eventfs_mutex);
- if (!ei || !ei_dentry)
- goto out;
+ if (!ei) {
+ srcu_read_unlock(&eventfs_srcu, idx);
+ return -ENOENT;
+ }
+
+ data = ei->data;
+
+ edents = kmalloc(sizeof(*edents), GFP_KERNEL);
+ if (!edents) {
+ srcu_read_unlock(&eventfs_srcu, idx);
+ return -ENOMEM;
+ }
/*
- * Need to create the dentries and inodes to have a consistent
- * inode number.
+ * Need to make a struct eventfs_dent array, start by
+ * allocating enough for just the files, which is a fixed
+ * array. Then use realloc via add_entry() for the directories
+ * which is stored in a linked list.
*/
- ret = 0;
-
- /* Start at 'c' to jump over already read entries */
- for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
- void *cdata = ei->data;
+ dents = kcalloc(ei->nr_entries, sizeof(*dents), GFP_KERNEL);
+ if (!dents) {
+ srcu_read_unlock(&eventfs_srcu, idx);
+ kfree(edents);
+ return -ENOMEM;
+ }
+ for (i = 0; i < ei->nr_entries; i++) {
+ void *cdata = data;
entry = &ei->entries[i];
name = entry->name;
-
mutex_lock(&eventfs_mutex);
- /* If ei->is_freed then just bail here, nothing more to do */
- if (ei->is_freed) {
- mutex_unlock(&eventfs_mutex);
- goto out;
- }
- r = entry->callback(name, &mode, &cdata, &fops);
+ /* If ei->is_freed, then the event itself may be too */
+ if (!ei->is_freed)
+ r = entry->callback(name, &mode, &cdata, &fops);
+ else
+ r = -1;
mutex_unlock(&eventfs_mutex);
- if (r <= 0)
+ /* If the ei is being freed, no need to continue */
+ if (r < 0) {
+ ret = -ENOENT;
+ goto fail;
+ }
+ /* callbacks returning zero just means skip this file */
+ if (r == 0)
continue;
+ ret = update_entry(dents, name, DT_REG, cnt);
- dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops);
- if (!dentry)
- goto out;
- ino = dentry->d_inode->i_ino;
- dput(dentry);
+ if (ret < 0)
+ goto fail;
- if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
- goto out;
+ cnt++;
}
- /* Subtract the skipped entries above */
- c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
-
list_for_each_entry_srcu(ei_child, &ei->children, list,
srcu_read_lock_held(&eventfs_srcu)) {
+ ret = add_entry(&dents, ei_child->name, DT_DIR, cnt);
+ if (ret < 0)
+ goto fail;
+ cnt++;
+ }
- if (c > 0) {
- c--;
- continue;
- }
+ edents->count = cnt;
+ edents->dents = dents;
- ctx->pos++;
+ srcu_read_unlock(&eventfs_srcu, idx);
+ file->private_data = edents;
+ return 0;
+ fail:
+ srcu_read_unlock(&eventfs_srcu, idx);
+ for (; cnt >= 0; cnt--)
+ kfree_const(dents[cnt].name);
+ kfree(dents);
+ kfree(edents);
+ return ret;
+}
- if (ei_child->is_freed)
- continue;
+static int eventfs_dir_release(struct inode *inode, struct file *file)
+{
+ struct eventfs_list *edents = file->private_data;
+ struct tracefs_inode *ti;
+ int i;
- name = ei_child->name;
+ ti = get_tracefs(inode);
+ if (!(ti->flags & TRACEFS_EVENT_INODE))
+ return -EINVAL;
- dentry = create_dir_dentry(ei, ei_child, ei_dentry);
- if (!dentry)
- goto out_dec;
- ino = dentry->d_inode->i_ino;
- dput(dentry);
+ if (WARN_ON_ONCE(!edents))
+ return -EINVAL;
- if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
- goto out_dec;
+ for (i = 0; i < edents->count; i++) {
+ kfree_const(edents->dents[i].name);
}
- ret = 1;
- out:
- srcu_read_unlock(&eventfs_srcu, idx);
- return ret;
+ kfree(edents->dents);
+ kfree(edents);
+ return 0;
+}
+
+static int eventfs_iterate(struct file *file, struct dir_context *ctx)
+{
+ struct eventfs_list *edents = file->private_data;
+ int i, c;
- out_dec:
- /* Incremented ctx->pos without adding something, reset it */
- ctx->pos--;
- goto out;
+ if (!dir_emit_dots(file, ctx))
+ return 0;
+
+ c = ctx->pos - 2;
+
+ /* Start at 'c' to jump over already read entries */
+ for (i = c; i < edents->count; i++, ctx->pos++) {
+
+ if (!dir_emit(ctx, edents->dents[i].name,
+ strlen(edents->dents[i].name),
+ edents->dents[i].ino, edents->dents[i].type))
+ break;
+ }
+ return 0;
}
/**
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 2/2] eventfs: Create list of files and directories at dir open
[not found] ` <CAHk-=wgjSuapZoWfQZMyFi80wJE6a=vjOdgpy_k+YaWwbX9Pig@mail.gmail.com>
@ 2024-01-16 22:01 ` Steven Rostedt
0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2024-01-16 22:01 UTC (permalink / raw)
To: Linus Torvalds
Cc: Linux Kernel Mailing List, Linux Trace Kernel, Masami Hiramatsu,
Mark Rutland, Mathieu Desnoyers, Christian Brauner, Al Viro,
Ajay Kaher, linux-fsdevel, kernel test robot
On Tue, 16 Jan 2024 13:39:38 -0800
Linus Torvalds <torvalds@linux-foundation.org> wrote:
> I don't understand why your still insist on this pointless open wrapper.
I just liked the consistency of it.
>
> Just do this all at iterate time. No open wrappers. No nothing. Just
> iterate over the days structures your have.
>
> IOW, instead of iterating in open to create the array, just iterate in -
> look, it's in the *name* for chrissake - iterate_shared.
>
> No array. No random allocation for said array.
>
> If you can iterate at open time, you can iterate at iterate_shared time.
> Stop creating a list that your already have.
>
> And nobody cares if you do a readdir at the same time as modifying the
> directory. This isn't a real filesystem with strict POSIX semantics.
OK, I can do that.
-- Steve
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-01-16 22:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-16 21:12 [PATCH v2 0/2] eventfs: Create dentries and inodes at dir open Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 1/2] eventfs: Have the inodes all for files and directories all be the same Steven Rostedt
2024-01-16 21:12 ` [PATCH v2 2/2] eventfs: Create list of files and directories at dir open Steven Rostedt
[not found] ` <CAHk-=wgjSuapZoWfQZMyFi80wJE6a=vjOdgpy_k+YaWwbX9Pig@mail.gmail.com>
2024-01-16 22:01 ` Steven Rostedt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).