#include <linux/delay.h>
 #include "internal.h"
 
+/*
+ * eventfs_mutex protects the eventfs_inode (ei) dentry. Any access
+ * to the ei->dentry must be done under this mutex and after checking
+ * if ei->is_freed is not set. The ei->dentry is released under the
+ * mutex at the same time ei->is_freed is set. If ei->is_freed is set
+ * then the ei->dentry is invalid.
+ */
 static DEFINE_MUTEX(eventfs_mutex);
+
+/*
+ * 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).
+ * After the SRCU grace period is over, the ei may be freed.
+ */
 DEFINE_STATIC_SRCU(eventfs_srcu);
 
 static struct dentry *eventfs_root_lookup(struct inode *dir,
        bool invalidate = false;
 
        mutex_lock(&eventfs_mutex);
+       if (ei->is_freed) {
+               mutex_unlock(&eventfs_mutex);
+               return NULL;
+       }
        /* If the e_dentry already has a dentry, use it */
        if (*e_dentry) {
                /* lookup does not need to up the ref count */
        struct eventfs_inode *ei_child;
        struct tracefs_inode *ti;
 
+       lockdep_assert_held(&eventfs_mutex);
+
        /* srcu lock already held */
        /* fill parent-child relation */
        list_for_each_entry_srcu(ei_child, &ei->children, list,
 
 /**
  * create_dir_dentry - Create a directory dentry for the eventfs_inode
+ * @pei: The eventfs_inode parent of ei.
  * @ei: The eventfs_inode to create the directory for
  * @parent: The dentry of the parent of this directory
  * @lookup: True if this is called by the lookup code
  * This creates and attaches a directory dentry to the eventfs_inode @ei.
  */
 static struct dentry *
-create_dir_dentry(struct eventfs_inode *ei, struct dentry *parent, bool lookup)
+create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei,
+                 struct dentry *parent, bool lookup)
 {
        bool invalidate = false;
        struct dentry *dentry = NULL;
 
        mutex_lock(&eventfs_mutex);
+       if (pei->is_freed || ei->is_freed) {
+               mutex_unlock(&eventfs_mutex);
+               return NULL;
+       }
        if (ei->dentry) {
                /* If the dentry already has a dentry, use it */
                dentry = ei->dentry;
         */
        mutex_lock(&eventfs_mutex);
        ei = READ_ONCE(ti->private);
-       if (ei)
+       if (ei && !ei->is_freed)
                ei_dentry = READ_ONCE(ei->dentry);
        mutex_unlock(&eventfs_mutex);
 
                if (strcmp(ei_child->name, name) != 0)
                        continue;
                ret = simple_lookup(dir, dentry, flags);
-               create_dir_dentry(ei_child, ei_dentry, true);
+               create_dir_dentry(ei, ei_child, ei_dentry, true);
                created = true;
                break;
        }
 
        list_for_each_entry_srcu(ei_child, &ei->children, list,
                                 srcu_read_lock_held(&eventfs_srcu)) {
-               d = create_dir_dentry(ei_child, parent, false);
+               d = create_dir_dentry(ei, ei_child, parent, false);
                if (d) {
                        ret = add_dentries(&dentries, d, cnt);
                        if (ret < 0)
        ei->nr_entries = size;
        ei->data = data;
        INIT_LIST_HEAD(&ei->children);
+       INIT_LIST_HEAD(&ei->list);
 
        mutex_lock(&eventfs_mutex);
-       list_add_tail(&ei->list, &parent->children);
-       ei->d_parent = parent->dentry;
+       if (!parent->is_freed) {
+               list_add_tail(&ei->list, &parent->children);
+               ei->d_parent = parent->dentry;
+       }
        mutex_unlock(&eventfs_mutex);
 
+       /* Was the parent freed? */
+       if (list_empty(&ei->list)) {
+               free_ei(ei);
+               ei = NULL;
+       }
        return ei;
 }