diff mbox series

[v3,3/8] xen/hypfs: add new enter() and exit() per node callbacks

Message ID 20201209160956.32456-4-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series xen: support per-cpupool scheduling granularity | expand

Commit Message

Jürgen Groß Dec. 9, 2020, 4:09 p.m. UTC
In order to better support resource allocation and locking for dynamic
hypfs nodes add enter() and exit() callbacks to struct hypfs_funcs.

The enter() callback is called when entering a node during hypfs user
actions (traversing, reading or writing it), while the exit() callback
is called when leaving a node (accessing another node at the same or a
higher directory level, or when returning to the user).

For avoiding recursion this requires a parent pointer in each node.
Let the enter() callback return the entry address which is stored as
the last accessed node in order to be able to use a template entry for
that purpose in case of dynamic entries.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch

V3:
- add ASSERT(entry); (Jan Beulich)

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 xen/common/hypfs.c      | 80 +++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/hypfs.h |  5 +++
 2 files changed, 85 insertions(+)

Comments

Jan Beulich Dec. 16, 2020, 4:16 p.m. UTC | #1
On 09.12.2020 17:09, Juergen Gross wrote:
> In order to better support resource allocation and locking for dynamic
> hypfs nodes add enter() and exit() callbacks to struct hypfs_funcs.
> 
> The enter() callback is called when entering a node during hypfs user
> actions (traversing, reading or writing it), while the exit() callback
> is called when leaving a node (accessing another node at the same or a
> higher directory level, or when returning to the user).
> 
> For avoiding recursion this requires a parent pointer in each node.
> Let the enter() callback return the entry address which is stored as
> the last accessed node in order to be able to use a template entry for
> that purpose in case of dynamic entries.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
> V2:
> - new patch
> 
> V3:
> - add ASSERT(entry); (Jan Beulich)
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>  xen/common/hypfs.c      | 80 +++++++++++++++++++++++++++++++++++++++++
>  xen/include/xen/hypfs.h |  5 +++
>  2 files changed, 85 insertions(+)
> 
> diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c
> index 6f822ae097..f04934db10 100644
> --- a/xen/common/hypfs.c
> +++ b/xen/common/hypfs.c
> @@ -25,30 +25,40 @@ CHECK_hypfs_dirlistentry;
>       ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry)))
>  
>  const struct hypfs_funcs hypfs_dir_funcs = {
> +    .enter = hypfs_node_enter,
> +    .exit = hypfs_node_exit,
>      .read = hypfs_read_dir,
>      .write = hypfs_write_deny,
>      .getsize = hypfs_getsize,
>      .findentry = hypfs_dir_findentry,
>  };
>  const struct hypfs_funcs hypfs_leaf_ro_funcs = {
> +    .enter = hypfs_node_enter,
> +    .exit = hypfs_node_exit,
>      .read = hypfs_read_leaf,
>      .write = hypfs_write_deny,
>      .getsize = hypfs_getsize,
>      .findentry = hypfs_leaf_findentry,
>  };
>  const struct hypfs_funcs hypfs_leaf_wr_funcs = {
> +    .enter = hypfs_node_enter,
> +    .exit = hypfs_node_exit,
>      .read = hypfs_read_leaf,
>      .write = hypfs_write_leaf,
>      .getsize = hypfs_getsize,
>      .findentry = hypfs_leaf_findentry,
>  };
>  const struct hypfs_funcs hypfs_bool_wr_funcs = {
> +    .enter = hypfs_node_enter,
> +    .exit = hypfs_node_exit,
>      .read = hypfs_read_leaf,
>      .write = hypfs_write_bool,
>      .getsize = hypfs_getsize,
>      .findentry = hypfs_leaf_findentry,
>  };
>  const struct hypfs_funcs hypfs_custom_wr_funcs = {
> +    .enter = hypfs_node_enter,
> +    .exit = hypfs_node_exit,
>      .read = hypfs_read_leaf,
>      .write = hypfs_write_custom,
>      .getsize = hypfs_getsize,
> @@ -63,6 +73,8 @@ enum hypfs_lock_state {
>  };
>  static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
>  
> +static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
> +
>  HYPFS_DIR_INIT(hypfs_root, "");
>  
>  static void hypfs_read_lock(void)
> @@ -100,11 +112,59 @@ static void hypfs_unlock(void)
>      }
>  }
>  
> +const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry)
> +{
> +    return entry;
> +}
> +
> +void hypfs_node_exit(const struct hypfs_entry *entry)
> +{
> +}
> +
> +static int node_enter(const struct hypfs_entry *entry)
> +{
> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
> +
> +    entry = entry->funcs->enter(entry);
> +    if ( IS_ERR(entry) )
> +        return PTR_ERR(entry);
> +
> +    ASSERT(entry);
> +    ASSERT(!*last || *last == entry->parent);
> +
> +    *last = entry;
> +
> +    return 0;
> +}
> +
> +static void node_exit(const struct hypfs_entry *entry)
> +{
> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
> +
> +    if ( !*last )
> +        return;

To my question regarding this in v2 you replied

"I rechecked and have found that this was a remnant from an earlier
 variant. *last won't ever be NULL, so the if can be dropped (a NULL
 will be catched by the following ASSERT())."

Now this if() is still there. Why? (My alternative suggestion was
to have ASSERT(!entry->parent) inside the if() body, since prior to
that you said this would be an indication of the root entry.)

Jan
Jürgen Groß Dec. 16, 2020, 4:24 p.m. UTC | #2
On 16.12.20 17:16, Jan Beulich wrote:
> On 09.12.2020 17:09, Juergen Gross wrote:
>> In order to better support resource allocation and locking for dynamic
>> hypfs nodes add enter() and exit() callbacks to struct hypfs_funcs.
>>
>> The enter() callback is called when entering a node during hypfs user
>> actions (traversing, reading or writing it), while the exit() callback
>> is called when leaving a node (accessing another node at the same or a
>> higher directory level, or when returning to the user).
>>
>> For avoiding recursion this requires a parent pointer in each node.
>> Let the enter() callback return the entry address which is stored as
>> the last accessed node in order to be able to use a template entry for
>> that purpose in case of dynamic entries.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> V2:
>> - new patch
>>
>> V3:
>> - add ASSERT(entry); (Jan Beulich)
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>>   xen/common/hypfs.c      | 80 +++++++++++++++++++++++++++++++++++++++++
>>   xen/include/xen/hypfs.h |  5 +++
>>   2 files changed, 85 insertions(+)
>>
>> diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c
>> index 6f822ae097..f04934db10 100644
>> --- a/xen/common/hypfs.c
>> +++ b/xen/common/hypfs.c
>> @@ -25,30 +25,40 @@ CHECK_hypfs_dirlistentry;
>>        ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry)))
>>   
>>   const struct hypfs_funcs hypfs_dir_funcs = {
>> +    .enter = hypfs_node_enter,
>> +    .exit = hypfs_node_exit,
>>       .read = hypfs_read_dir,
>>       .write = hypfs_write_deny,
>>       .getsize = hypfs_getsize,
>>       .findentry = hypfs_dir_findentry,
>>   };
>>   const struct hypfs_funcs hypfs_leaf_ro_funcs = {
>> +    .enter = hypfs_node_enter,
>> +    .exit = hypfs_node_exit,
>>       .read = hypfs_read_leaf,
>>       .write = hypfs_write_deny,
>>       .getsize = hypfs_getsize,
>>       .findentry = hypfs_leaf_findentry,
>>   };
>>   const struct hypfs_funcs hypfs_leaf_wr_funcs = {
>> +    .enter = hypfs_node_enter,
>> +    .exit = hypfs_node_exit,
>>       .read = hypfs_read_leaf,
>>       .write = hypfs_write_leaf,
>>       .getsize = hypfs_getsize,
>>       .findentry = hypfs_leaf_findentry,
>>   };
>>   const struct hypfs_funcs hypfs_bool_wr_funcs = {
>> +    .enter = hypfs_node_enter,
>> +    .exit = hypfs_node_exit,
>>       .read = hypfs_read_leaf,
>>       .write = hypfs_write_bool,
>>       .getsize = hypfs_getsize,
>>       .findentry = hypfs_leaf_findentry,
>>   };
>>   const struct hypfs_funcs hypfs_custom_wr_funcs = {
>> +    .enter = hypfs_node_enter,
>> +    .exit = hypfs_node_exit,
>>       .read = hypfs_read_leaf,
>>       .write = hypfs_write_custom,
>>       .getsize = hypfs_getsize,
>> @@ -63,6 +73,8 @@ enum hypfs_lock_state {
>>   };
>>   static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
>>   
>> +static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
>> +
>>   HYPFS_DIR_INIT(hypfs_root, "");
>>   
>>   static void hypfs_read_lock(void)
>> @@ -100,11 +112,59 @@ static void hypfs_unlock(void)
>>       }
>>   }
>>   
>> +const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry)
>> +{
>> +    return entry;
>> +}
>> +
>> +void hypfs_node_exit(const struct hypfs_entry *entry)
>> +{
>> +}
>> +
>> +static int node_enter(const struct hypfs_entry *entry)
>> +{
>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>> +
>> +    entry = entry->funcs->enter(entry);
>> +    if ( IS_ERR(entry) )
>> +        return PTR_ERR(entry);
>> +
>> +    ASSERT(entry);
>> +    ASSERT(!*last || *last == entry->parent);
>> +
>> +    *last = entry;
>> +
>> +    return 0;
>> +}
>> +
>> +static void node_exit(const struct hypfs_entry *entry)
>> +{
>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>> +
>> +    if ( !*last )
>> +        return;
> 
> To my question regarding this in v2 you replied
> 
> "I rechecked and have found that this was a remnant from an earlier
>   variant. *last won't ever be NULL, so the if can be dropped (a NULL
>   will be catched by the following ASSERT())."
> 
> Now this if() is still there. Why?

I really thought I did remove the if(). Seems as if I did that on
my test machine only and not in my git tree. Sorry for that.


Juergen
Jan Beulich Dec. 16, 2020, 4:36 p.m. UTC | #3
On 16.12.2020 17:24, Jürgen Groß wrote:
> On 16.12.20 17:16, Jan Beulich wrote:
>> On 09.12.2020 17:09, Juergen Gross wrote:
>>> In order to better support resource allocation and locking for dynamic
>>> hypfs nodes add enter() and exit() callbacks to struct hypfs_funcs.
>>>
>>> The enter() callback is called when entering a node during hypfs user
>>> actions (traversing, reading or writing it), while the exit() callback
>>> is called when leaving a node (accessing another node at the same or a
>>> higher directory level, or when returning to the user).
>>>
>>> For avoiding recursion this requires a parent pointer in each node.
>>> Let the enter() callback return the entry address which is stored as
>>> the last accessed node in order to be able to use a template entry for
>>> that purpose in case of dynamic entries.
>>>
>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>> ---
>>> V2:
>>> - new patch
>>>
>>> V3:
>>> - add ASSERT(entry); (Jan Beulich)
>>>
>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>> ---
>>>   xen/common/hypfs.c      | 80 +++++++++++++++++++++++++++++++++++++++++
>>>   xen/include/xen/hypfs.h |  5 +++
>>>   2 files changed, 85 insertions(+)
>>>
>>> diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c
>>> index 6f822ae097..f04934db10 100644
>>> --- a/xen/common/hypfs.c
>>> +++ b/xen/common/hypfs.c
>>> @@ -25,30 +25,40 @@ CHECK_hypfs_dirlistentry;
>>>        ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry)))
>>>   
>>>   const struct hypfs_funcs hypfs_dir_funcs = {
>>> +    .enter = hypfs_node_enter,
>>> +    .exit = hypfs_node_exit,
>>>       .read = hypfs_read_dir,
>>>       .write = hypfs_write_deny,
>>>       .getsize = hypfs_getsize,
>>>       .findentry = hypfs_dir_findentry,
>>>   };
>>>   const struct hypfs_funcs hypfs_leaf_ro_funcs = {
>>> +    .enter = hypfs_node_enter,
>>> +    .exit = hypfs_node_exit,
>>>       .read = hypfs_read_leaf,
>>>       .write = hypfs_write_deny,
>>>       .getsize = hypfs_getsize,
>>>       .findentry = hypfs_leaf_findentry,
>>>   };
>>>   const struct hypfs_funcs hypfs_leaf_wr_funcs = {
>>> +    .enter = hypfs_node_enter,
>>> +    .exit = hypfs_node_exit,
>>>       .read = hypfs_read_leaf,
>>>       .write = hypfs_write_leaf,
>>>       .getsize = hypfs_getsize,
>>>       .findentry = hypfs_leaf_findentry,
>>>   };
>>>   const struct hypfs_funcs hypfs_bool_wr_funcs = {
>>> +    .enter = hypfs_node_enter,
>>> +    .exit = hypfs_node_exit,
>>>       .read = hypfs_read_leaf,
>>>       .write = hypfs_write_bool,
>>>       .getsize = hypfs_getsize,
>>>       .findentry = hypfs_leaf_findentry,
>>>   };
>>>   const struct hypfs_funcs hypfs_custom_wr_funcs = {
>>> +    .enter = hypfs_node_enter,
>>> +    .exit = hypfs_node_exit,
>>>       .read = hypfs_read_leaf,
>>>       .write = hypfs_write_custom,
>>>       .getsize = hypfs_getsize,
>>> @@ -63,6 +73,8 @@ enum hypfs_lock_state {
>>>   };
>>>   static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
>>>   
>>> +static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
>>> +
>>>   HYPFS_DIR_INIT(hypfs_root, "");
>>>   
>>>   static void hypfs_read_lock(void)
>>> @@ -100,11 +112,59 @@ static void hypfs_unlock(void)
>>>       }
>>>   }
>>>   
>>> +const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry)
>>> +{
>>> +    return entry;
>>> +}
>>> +
>>> +void hypfs_node_exit(const struct hypfs_entry *entry)
>>> +{
>>> +}
>>> +
>>> +static int node_enter(const struct hypfs_entry *entry)
>>> +{
>>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>>> +
>>> +    entry = entry->funcs->enter(entry);
>>> +    if ( IS_ERR(entry) )
>>> +        return PTR_ERR(entry);
>>> +
>>> +    ASSERT(entry);
>>> +    ASSERT(!*last || *last == entry->parent);
>>> +
>>> +    *last = entry;
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +static void node_exit(const struct hypfs_entry *entry)
>>> +{
>>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>>> +
>>> +    if ( !*last )
>>> +        return;
>>
>> To my question regarding this in v2 you replied
>>
>> "I rechecked and have found that this was a remnant from an earlier
>>   variant. *last won't ever be NULL, so the if can be dropped (a NULL
>>   will be catched by the following ASSERT())."
>>
>> Now this if() is still there. Why?
> 
> I really thought I did remove the if(). Seems as if I did that on
> my test machine only and not in my git tree. Sorry for that.

So should I drop it while committing and adding
Reviewed-by: Jan Beulich <jbeulich@suse.com>
?

Jan
Jürgen Groß Dec. 16, 2020, 5:12 p.m. UTC | #4
On 16.12.20 17:36, Jan Beulich wrote:
> On 16.12.2020 17:24, Jürgen Groß wrote:
>> On 16.12.20 17:16, Jan Beulich wrote:
>>> On 09.12.2020 17:09, Juergen Gross wrote:
>>>> In order to better support resource allocation and locking for dynamic
>>>> hypfs nodes add enter() and exit() callbacks to struct hypfs_funcs.
>>>>
>>>> The enter() callback is called when entering a node during hypfs user
>>>> actions (traversing, reading or writing it), while the exit() callback
>>>> is called when leaving a node (accessing another node at the same or a
>>>> higher directory level, or when returning to the user).
>>>>
>>>> For avoiding recursion this requires a parent pointer in each node.
>>>> Let the enter() callback return the entry address which is stored as
>>>> the last accessed node in order to be able to use a template entry for
>>>> that purpose in case of dynamic entries.
>>>>
>>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>>> ---
>>>> V2:
>>>> - new patch
>>>>
>>>> V3:
>>>> - add ASSERT(entry); (Jan Beulich)
>>>>
>>>> Signed-off-by: Juergen Gross <jgross@suse.com>
>>>> ---
>>>>    xen/common/hypfs.c      | 80 +++++++++++++++++++++++++++++++++++++++++
>>>>    xen/include/xen/hypfs.h |  5 +++
>>>>    2 files changed, 85 insertions(+)
>>>>
>>>> diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c
>>>> index 6f822ae097..f04934db10 100644
>>>> --- a/xen/common/hypfs.c
>>>> +++ b/xen/common/hypfs.c
>>>> @@ -25,30 +25,40 @@ CHECK_hypfs_dirlistentry;
>>>>         ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry)))
>>>>    
>>>>    const struct hypfs_funcs hypfs_dir_funcs = {
>>>> +    .enter = hypfs_node_enter,
>>>> +    .exit = hypfs_node_exit,
>>>>        .read = hypfs_read_dir,
>>>>        .write = hypfs_write_deny,
>>>>        .getsize = hypfs_getsize,
>>>>        .findentry = hypfs_dir_findentry,
>>>>    };
>>>>    const struct hypfs_funcs hypfs_leaf_ro_funcs = {
>>>> +    .enter = hypfs_node_enter,
>>>> +    .exit = hypfs_node_exit,
>>>>        .read = hypfs_read_leaf,
>>>>        .write = hypfs_write_deny,
>>>>        .getsize = hypfs_getsize,
>>>>        .findentry = hypfs_leaf_findentry,
>>>>    };
>>>>    const struct hypfs_funcs hypfs_leaf_wr_funcs = {
>>>> +    .enter = hypfs_node_enter,
>>>> +    .exit = hypfs_node_exit,
>>>>        .read = hypfs_read_leaf,
>>>>        .write = hypfs_write_leaf,
>>>>        .getsize = hypfs_getsize,
>>>>        .findentry = hypfs_leaf_findentry,
>>>>    };
>>>>    const struct hypfs_funcs hypfs_bool_wr_funcs = {
>>>> +    .enter = hypfs_node_enter,
>>>> +    .exit = hypfs_node_exit,
>>>>        .read = hypfs_read_leaf,
>>>>        .write = hypfs_write_bool,
>>>>        .getsize = hypfs_getsize,
>>>>        .findentry = hypfs_leaf_findentry,
>>>>    };
>>>>    const struct hypfs_funcs hypfs_custom_wr_funcs = {
>>>> +    .enter = hypfs_node_enter,
>>>> +    .exit = hypfs_node_exit,
>>>>        .read = hypfs_read_leaf,
>>>>        .write = hypfs_write_custom,
>>>>        .getsize = hypfs_getsize,
>>>> @@ -63,6 +73,8 @@ enum hypfs_lock_state {
>>>>    };
>>>>    static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
>>>>    
>>>> +static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
>>>> +
>>>>    HYPFS_DIR_INIT(hypfs_root, "");
>>>>    
>>>>    static void hypfs_read_lock(void)
>>>> @@ -100,11 +112,59 @@ static void hypfs_unlock(void)
>>>>        }
>>>>    }
>>>>    
>>>> +const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry)
>>>> +{
>>>> +    return entry;
>>>> +}
>>>> +
>>>> +void hypfs_node_exit(const struct hypfs_entry *entry)
>>>> +{
>>>> +}
>>>> +
>>>> +static int node_enter(const struct hypfs_entry *entry)
>>>> +{
>>>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>>>> +
>>>> +    entry = entry->funcs->enter(entry);
>>>> +    if ( IS_ERR(entry) )
>>>> +        return PTR_ERR(entry);
>>>> +
>>>> +    ASSERT(entry);
>>>> +    ASSERT(!*last || *last == entry->parent);
>>>> +
>>>> +    *last = entry;
>>>> +
>>>> +    return 0;
>>>> +}
>>>> +
>>>> +static void node_exit(const struct hypfs_entry *entry)
>>>> +{
>>>> +    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
>>>> +
>>>> +    if ( !*last )
>>>> +        return;
>>>
>>> To my question regarding this in v2 you replied
>>>
>>> "I rechecked and have found that this was a remnant from an earlier
>>>    variant. *last won't ever be NULL, so the if can be dropped (a NULL
>>>    will be catched by the following ASSERT())."
>>>
>>> Now this if() is still there. Why?
>>
>> I really thought I did remove the if(). Seems as if I did that on
>> my test machine only and not in my git tree. Sorry for that.
> 
> So should I drop it while committing and adding
> Reviewed-by: Jan Beulich <jbeulich@suse.com>
> ?

Yes, please.


Juergen
diff mbox series

Patch

diff --git a/xen/common/hypfs.c b/xen/common/hypfs.c
index 6f822ae097..f04934db10 100644
--- a/xen/common/hypfs.c
+++ b/xen/common/hypfs.c
@@ -25,30 +25,40 @@  CHECK_hypfs_dirlistentry;
      ROUNDUP((name_len) + 1, alignof(struct xen_hypfs_direntry)))
 
 const struct hypfs_funcs hypfs_dir_funcs = {
+    .enter = hypfs_node_enter,
+    .exit = hypfs_node_exit,
     .read = hypfs_read_dir,
     .write = hypfs_write_deny,
     .getsize = hypfs_getsize,
     .findentry = hypfs_dir_findentry,
 };
 const struct hypfs_funcs hypfs_leaf_ro_funcs = {
+    .enter = hypfs_node_enter,
+    .exit = hypfs_node_exit,
     .read = hypfs_read_leaf,
     .write = hypfs_write_deny,
     .getsize = hypfs_getsize,
     .findentry = hypfs_leaf_findentry,
 };
 const struct hypfs_funcs hypfs_leaf_wr_funcs = {
+    .enter = hypfs_node_enter,
+    .exit = hypfs_node_exit,
     .read = hypfs_read_leaf,
     .write = hypfs_write_leaf,
     .getsize = hypfs_getsize,
     .findentry = hypfs_leaf_findentry,
 };
 const struct hypfs_funcs hypfs_bool_wr_funcs = {
+    .enter = hypfs_node_enter,
+    .exit = hypfs_node_exit,
     .read = hypfs_read_leaf,
     .write = hypfs_write_bool,
     .getsize = hypfs_getsize,
     .findentry = hypfs_leaf_findentry,
 };
 const struct hypfs_funcs hypfs_custom_wr_funcs = {
+    .enter = hypfs_node_enter,
+    .exit = hypfs_node_exit,
     .read = hypfs_read_leaf,
     .write = hypfs_write_custom,
     .getsize = hypfs_getsize,
@@ -63,6 +73,8 @@  enum hypfs_lock_state {
 };
 static DEFINE_PER_CPU(enum hypfs_lock_state, hypfs_locked);
 
+static DEFINE_PER_CPU(const struct hypfs_entry *, hypfs_last_node_entered);
+
 HYPFS_DIR_INIT(hypfs_root, "");
 
 static void hypfs_read_lock(void)
@@ -100,11 +112,59 @@  static void hypfs_unlock(void)
     }
 }
 
+const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry)
+{
+    return entry;
+}
+
+void hypfs_node_exit(const struct hypfs_entry *entry)
+{
+}
+
+static int node_enter(const struct hypfs_entry *entry)
+{
+    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
+
+    entry = entry->funcs->enter(entry);
+    if ( IS_ERR(entry) )
+        return PTR_ERR(entry);
+
+    ASSERT(entry);
+    ASSERT(!*last || *last == entry->parent);
+
+    *last = entry;
+
+    return 0;
+}
+
+static void node_exit(const struct hypfs_entry *entry)
+{
+    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
+
+    if ( !*last )
+        return;
+
+    ASSERT(*last == entry);
+    *last = entry->parent;
+
+    entry->funcs->exit(entry);
+}
+
+static void node_exit_all(void)
+{
+    const struct hypfs_entry **last = &this_cpu(hypfs_last_node_entered);
+
+    while ( *last )
+        node_exit(*last);
+}
+
 static int add_entry(struct hypfs_entry_dir *parent, struct hypfs_entry *new)
 {
     int ret = -ENOENT;
     struct hypfs_entry *e;
 
+    ASSERT(new->funcs->enter);
+    ASSERT(new->funcs->exit);
     ASSERT(new->funcs->read);
     ASSERT(new->funcs->write);
     ASSERT(new->funcs->getsize);
@@ -140,6 +200,7 @@  static int add_entry(struct hypfs_entry_dir *parent, struct hypfs_entry *new)
         unsigned int sz = strlen(new->name);
 
         parent->e.size += DIRENTRY_SIZE(sz);
+        new->parent = &parent->e;
     }
 
     hypfs_unlock();
@@ -221,6 +282,7 @@  static struct hypfs_entry *hypfs_get_entry_rel(struct hypfs_entry_dir *dir,
     const char *end;
     struct hypfs_entry *entry;
     unsigned int name_len;
+    int ret;
 
     for ( ; ; )
     {
@@ -235,6 +297,10 @@  static struct hypfs_entry *hypfs_get_entry_rel(struct hypfs_entry_dir *dir,
             end = strchr(path, '\0');
         name_len = end - path;
 
+        ret = node_enter(&dir->e);
+        if ( ret )
+            return ERR_PTR(ret);
+
         entry = dir->e.funcs->findentry(dir, path, name_len);
         if ( IS_ERR(entry) || !*end )
             return entry;
@@ -265,6 +331,7 @@  int hypfs_read_dir(const struct hypfs_entry *entry,
     const struct hypfs_entry_dir *d;
     const struct hypfs_entry *e;
     unsigned int size = entry->funcs->getsize(entry);
+    int ret;
 
     ASSERT(this_cpu(hypfs_locked) != hypfs_unlocked);
 
@@ -276,12 +343,19 @@  int hypfs_read_dir(const struct hypfs_entry *entry,
         unsigned int e_namelen = strlen(e->name);
         unsigned int e_len = DIRENTRY_SIZE(e_namelen);
 
+        ret = node_enter(e);
+        if ( ret )
+            return ret;
+
         direntry.e.pad = 0;
         direntry.e.type = e->type;
         direntry.e.encoding = e->encoding;
         direntry.e.content_len = e->funcs->getsize(e);
         direntry.e.max_write_len = e->max_size;
         direntry.off_next = list_is_last(&e->list, &d->dirlist) ? 0 : e_len;
+
+        node_exit(e);
+
         if ( copy_to_guest(uaddr, &direntry, 1) )
             return -EFAULT;
 
@@ -495,6 +569,10 @@  long do_hypfs_op(unsigned int cmd,
         goto out;
     }
 
+    ret = node_enter(entry);
+    if ( ret )
+        goto out;
+
     switch ( cmd )
     {
     case XEN_HYPFS_OP_read:
@@ -511,6 +589,8 @@  long do_hypfs_op(unsigned int cmd,
     }
 
  out:
+    node_exit_all();
+
     hypfs_unlock();
 
     return ret;
diff --git a/xen/include/xen/hypfs.h b/xen/include/xen/hypfs.h
index 99fd4b036d..a6dfdb7d8e 100644
--- a/xen/include/xen/hypfs.h
+++ b/xen/include/xen/hypfs.h
@@ -35,6 +35,8 @@  struct hypfs_entry;
  * "/a/b/c" findentry() will be called for "/", "/a", and "/a/b").
  */
 struct hypfs_funcs {
+    const struct hypfs_entry *(*enter)(const struct hypfs_entry *entry);
+    void (*exit)(const struct hypfs_entry *entry);
     int (*read)(const struct hypfs_entry *entry,
                 XEN_GUEST_HANDLE_PARAM(void) uaddr);
     int (*write)(struct hypfs_entry_leaf *leaf,
@@ -56,6 +58,7 @@  struct hypfs_entry {
     unsigned int size;
     unsigned int max_size;
     const char *name;
+    struct hypfs_entry *parent;
     struct list_head list;
     const struct hypfs_funcs *funcs;
 };
@@ -149,6 +152,8 @@  int hypfs_add_dir(struct hypfs_entry_dir *parent,
                   struct hypfs_entry_dir *dir, bool nofault);
 int hypfs_add_leaf(struct hypfs_entry_dir *parent,
                    struct hypfs_entry_leaf *leaf, bool nofault);
+const struct hypfs_entry *hypfs_node_enter(const struct hypfs_entry *entry);
+void hypfs_node_exit(const struct hypfs_entry *entry);
 int hypfs_read_dir(const struct hypfs_entry *entry,
                    XEN_GUEST_HANDLE_PARAM(void) uaddr);
 int hypfs_read_leaf(const struct hypfs_entry *entry,