Skip to contentSkip to conversation

pgo: Fix allocate_node() v2

PATCH Superseded Jarmo Tiitto 4 participants 7 messages Jun 3, 2021 – Jun 4, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 pgo: Fix allocate_node() v2

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

---
 kernel/pgo/instrument.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
kernel/pgo/instrument.c +12 -9
index 0e07ee1b17d9..afe9982b07a3 100644
@@ -23,6 +23,7 @@
 #include <linux/export.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
+#include <asm-generic/sections.h>
 #include "pgo.h"
 
 /*
@@ -55,17 +56,19 @@ void prf_unlock(unsigned long flags)
 static struct llvm_prf_value_node *allocate_node(struct llvm_prf_data *p,
 						 u32 index, u64 value)
 {
-	if (&__llvm_prf_vnds_start[current_node + 1] >= __llvm_prf_vnds_end)
-		return NULL; /* Out of nodes */
-
-	current_node++;
-
-	/* Make sure the node is entirely within the section */
-	if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end ||
-	    &__llvm_prf_vnds_start[current_node + 1] > __llvm_prf_vnds_end)
+	const int max_vnds = prf_vnds_count();
+	/* Check that p is within vmlinux __llvm_prf_data section.
+	 * If not, don't allocate since we can't handle modules yet.
+	 */
+	if (!memory_contains(__llvm_prf_data_start,
+		__llvm_prf_data_end, p, sizeof(*p)))
 		return NULL;
 
-	return &__llvm_prf_vnds_start[current_node];
+	if (WARN_ON_ONCE(current_node >= max_vnds))
+		return NULL; /* Out of nodes */
+
+	/* reserve vnode for vmlinux */
+	return &__llvm_prf_vnds_start[current_node++];
 }
 
 /*

base-commit: 5d0cda65918279ada060417c5fecb7e86ccb3def

Signature · 1 line
2.31.1
#1 lore raw
Quoted 4 lines · Jarmo Tiitto

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

Kees can probably cut it when merging, but the above paragraph might be better "below the fold" below next time (doesn't necessitate a v3).

Quoted 7 lines

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

Quoted 7 lines

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

---

^ ie. here. If you put text between the --- and the diffstat, git just discards it when applying. It's a good way to hide commentary just meant for reviewers when sending a patch.

Quoted 18 lines
 kernel/pgo/instrument.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
kernel/pgo/instrument.c +1 -0
index 0e07ee1b17d9..afe9982b07a3 100644
@@ -23,6 +23,7 @@
 #include <linux/export.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
+#include <asm-generic/sections.h>
 #include "pgo.h"
 
 /*
@@ -55,17 +56,19 @@ void prf_unlock(unsigned long flags)
 static struct llvm_prf_value_node *allocate_node(struct llvm_prf_data *p,
                                                 u32 index, u64 value)

kernel/pgo/instrument.c void prf_unlock(unsigned long flags) · line 59

Quoted 10 lines
kernel/pgo/instrument.c +1 -8
 {
-       if (&__llvm_prf_vnds_start[current_node + 1] >= __llvm_prf_vnds_end)
-               return NULL; /* Out of nodes */
-
-       current_node++;
-
-       /* Make sure the node is entirely within the section */
-       if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end ||
-           &__llvm_prf_vnds_start[current_node + 1] > __llvm_prf_vnds_end)
+       const int max_vnds = prf_vnds_count();

Sorry, where was prf_vnds_count() defined? I don't see it in linux-next, but I'm also not sure which tree has 5d0cda65918279ada060417c5fecb7e86ccb3def.

Quoted 17 lines
kernel/pgo/instrument.c +10 -1
+       /* Check that p is within vmlinux __llvm_prf_data section.
+        * If not, don't allocate since we can't handle modules yet.
+        */
+       if (!memory_contains(__llvm_prf_data_start,
+               __llvm_prf_data_end, p, sizeof(*p)))
                return NULL;
 
-       return &__llvm_prf_vnds_start[current_node];
+       if (WARN_ON_ONCE(current_node >= max_vnds))
+               return NULL; /* Out of nodes */
+
+       /* reserve vnode for vmlinux */
+       return &__llvm_prf_vnds_start[current_node++];
 }
 
 /*
Quoted 4 lines

base-commit: 5d0cda65918279ada060417c5fecb7e86ccb3def --
2.31.1

--
Thanks,
~Nick Desaulniers

#2 lore raw
Quoted 53 lines · Nick Desaulniers
Quoted 4 lines · Jarmo Tiitto

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

Kees can probably cut it when merging, but the above paragraph might be better "below the fold" below next time (doesn't necessitate a v3).

Quoted 7 lines

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

Quoted 7 lines

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

---

^ ie. here. If you put text between the --- and the diffstat, git just discards it when applying. It's a good way to hide commentary just meant for reviewers when sending a patch.

Quoted 24 lines
  kernel/pgo/instrument.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
kernel/pgo/instrument.c +1 -5
index 0e07ee1b17d9..afe9982b07a3 100644
@@ -23,6 +23,7 @@
  #include <linux/export.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
+#include <asm-generic/sections.h>
  #include "pgo.h"
 
  /*
@@ -55,17 +56,19 @@ void prf_unlock(unsigned long flags)
  static struct llvm_prf_value_node *allocate_node(struct llvm_prf_data *p,
                                                  u32 index, u64 value)
  {
-       if (&__llvm_prf_vnds_start[current_node + 1] >= __llvm_prf_vnds_end)
-               return NULL; /* Out of nodes */
-
-       current_node++;
-
Quoted 8 lines

kernel/pgo/instrument.c void prf_unlock(unsigned long flags) · line 59

Quoted 4 lines
kernel/pgo/instrument.c +1 -3
-       /* Make sure the node is entirely within the section */
-       if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end ||
-           &__llvm_prf_vnds_start[current_node + 1] > __llvm_prf_vnds_end)
+       const int max_vnds = prf_vnds_count();

Sorry, where was prf_vnds_count() defined? I don't see it in linux-next, but I'm also not sure which tree has 5d0cda65918279ada060417c5fecb7e86ccb3def.

It is generated via the __DEFINE_PRF_SIZE macro in kernel/pgo/pgo.h.

Quoted 27 lines
Quoted 17 lines
kernel/pgo/instrument.c +10 -1
+       /* Check that p is within vmlinux __llvm_prf_data section.
+        * If not, don't allocate since we can't handle modules yet.
+        */
+       if (!memory_contains(__llvm_prf_data_start,
+               __llvm_prf_data_end, p, sizeof(*p)))
                 return NULL;
 
-       return &__llvm_prf_vnds_start[current_node];
+       if (WARN_ON_ONCE(current_node >= max_vnds))
+               return NULL; /* Out of nodes */
+
+       /* reserve vnode for vmlinux */
+       return &__llvm_prf_vnds_start[current_node++];
  }
 
  /*
Quoted 4 lines

base-commit: 5d0cda65918279ada060417c5fecb7e86ccb3def --
2.31.1

--
Thanks,
~Nick Desaulniers

Reviewed-by
#3 lore raw
Quoted 64 lines · Nathan Chancellor
Quoted 53 lines · Nick Desaulniers
Quoted 4 lines · Jarmo Tiitto

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

Kees can probably cut it when merging, but the above paragraph might be better "below the fold" below next time (doesn't necessitate a v3).

Quoted 7 lines

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

Quoted 7 lines

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

---

^ ie. here. If you put text between the --- and the diffstat, git just discards it when applying. It's a good way to hide commentary just meant for reviewers when sending a patch.

Quoted 24 lines
  kernel/pgo/instrument.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
kernel/pgo/instrument.c +1 -5
index 0e07ee1b17d9..afe9982b07a3 100644
@@ -23,6 +23,7 @@
  #include <linux/export.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
+#include <asm-generic/sections.h>
  #include "pgo.h"
 
  /*
@@ -55,17 +56,19 @@ void prf_unlock(unsigned long flags)
  static struct llvm_prf_value_node *allocate_node(struct llvm_prf_data *p,
                                                  u32 index, u64 value)
  {
-       if (&__llvm_prf_vnds_start[current_node + 1] >= __llvm_prf_vnds_end)
-               return NULL; /* Out of nodes */
-
-       current_node++;
-
Quoted 8 lines

kernel/pgo/instrument.c void prf_unlock(unsigned long flags) · line 59

Quoted 4 lines
kernel/pgo/instrument.c +1 -3
-       /* Make sure the node is entirely within the section */
-       if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end ||
-           &__llvm_prf_vnds_start[current_node + 1] > __llvm_prf_vnds_end)
+       const int max_vnds = prf_vnds_count();

Sorry, where was prf_vnds_count() defined? I don't see it in linux-next, but I'm also not sure which tree has 5d0cda65918279ada060417c5fecb7e86ccb3def.

Quoted 1 line

It is generated via the __DEFINE_PRF_SIZE macro in kernel/pgo/pgo.h.

Thanks, it kills me when I can't find something with grep. I wonder if language servers more modern than ctags have figured this out yet.

Patch looks fine to me then.

Quoted 33 lines
Quoted 24 lines
Quoted 18 lines
kernel/pgo/instrument.c +10 -1
+       /* Check that p is within vmlinux __llvm_prf_data section.
+        * If not, don't allocate since we can't handle modules yet.
+        */
+       if (!memory_contains(__llvm_prf_data_start,
+               __llvm_prf_data_end, p, sizeof(*p)))
                 return NULL;
 
-       return &__llvm_prf_vnds_start[current_node];
+       if (WARN_ON_ONCE(current_node >= max_vnds))
+               return NULL; /* Out of nodes */
+
+       /* reserve vnode for vmlinux */
+       return &__llvm_prf_vnds_start[current_node++];
  }
 
  /*
Quoted 4 lines

base-commit: 5d0cda65918279ada060417c5fecb7e86ccb3def --
2.31.1

Quoted 4 lines

--
Thanks,
~Nick Desaulniers

--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/f06200fd-4ce5-e976-20ec-d2ea9d734b3c%40kernel.org.

Signature · 2 lines
Thanks,
~Nick Desaulniers
Reviewed-by on v2 1/1
#4 lore raw
In reply to Jarmo Tiitto
Quoted 10 lines · Jarmo Tiitto

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

Quoted 6 lines

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

I agree with Nick on the comments about the commit message. A few more small nits below, not sure they necessitate a v3, up to you. Thank you for the patch!

kernel/pgo/instrument.c line 26

Quoted 13 lines
---
  kernel/pgo/instrument.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
kernel/pgo/instrument.c +1 -0
index 0e07ee1b17d9..afe9982b07a3 100644
@@ -23,6 +23,7 @@
  #include <linux/export.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
+#include <asm-generic/sections.h>

Not sure that it actually matters but I think this should be

#include <asm/sections.h>

instead. Might be nice to keep this sorted by moving it to the top as well.

Quoted 6 lines
kernel/pgo/instrument.c +0 -0
  #include "pgo.h"
 
  /*
@@ -55,17 +56,19 @@ void prf_unlock(unsigned long flags)
  static struct llvm_prf_value_node *allocate_node(struct llvm_prf_data *p,
  						 u32 index, u64 value)

kernel/pgo/instrument.c void prf_unlock(unsigned long flags) · line 59

Quoted 10 lines
kernel/pgo/instrument.c +1 -8
  {
-	if (&__llvm_prf_vnds_start[current_node + 1] >= __llvm_prf_vnds_end)
-		return NULL; /* Out of nodes */
-
-	current_node++;
-
-	/* Make sure the node is entirely within the section */
-	if (&__llvm_prf_vnds_start[current_node] >= __llvm_prf_vnds_end ||
-	    &__llvm_prf_vnds_start[current_node + 1] > __llvm_prf_vnds_end)
+	const int max_vnds = prf_vnds_count();

A blank line between this variable and the comment below would look nice.

kernel/pgo/instrument.c void prf_unlock(unsigned long flags) · line 61

Quoted 2 lines
kernel/pgo/instrument.c +2 -0
+	/* Check that p is within vmlinux __llvm_prf_data section. > +	 * If not, don't allocate since we can't handle modules yet.
+	 */

For every subsystem except for netdev, there should be a blank line at the beginning of a comment. In other works:

/*
* Check that p is within vmlinux __llvm_prf_data section.
* If not, don't allocate since we can't handle modules yet. */

Quoted 16 lines
kernel/pgo/instrument.c +7 -1
+	if (!memory_contains(__llvm_prf_data_start,
+		__llvm_prf_data_end, p, sizeof(*p)))
  		return NULL;
 
-	return &__llvm_prf_vnds_start[current_node];
+	if (WARN_ON_ONCE(current_node >= max_vnds))
+		return NULL; /* Out of nodes */
+
+	/* reserve vnode for vmlinux */
+	return &__llvm_prf_vnds_start[current_node++];
  }
 
  /*

base-commit: 5d0cda65918279ada060417c5fecb7e86ccb3def

#5 lore raw
Quoted 16 lines · Nathan Chancellor
Quoted 15 lines · Jarmo Tiitto

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

Quoted 5 lines
Quoted 1 line

I agree with Nick on the comments about the commit message. A few more small nits below, not sure they necessitate a v3, up to you. Thank you for the patch!

It would make my life easier to get a v3. :) I agree with all of Nathan's suggestions. :)

Thanks!

-Kees

Signature · 1 line
Kees Cook
#6 lore raw

Kees Cook wrote perjantaina 4. kesäkuuta 2021 0.36.39 EEST:

Quoted 23 lines · Nathan Chancellor
Quoted 16 lines · Jarmo Tiitto
Quoted 15 lines

Based on Kees and others feedback here is v2 patch that clarifies why the current checks in allocate_node() are flawed. I did fair amount of KGDB time on it.

When clang instrumentation eventually calls allocate_node() the struct llvm_prf_data *p argument tells us from what section we should reserve the vnode: It either points into vmlinux's core __llvm_prf_data section or some loaded module's __llvm_prf_data section.

But since we don't have access to corresponding __llvm_prf_vnds section(s) for any module, the function should return just NULL and ignore any profiling attempts from modules for now.

Quoted 5 lines
Quoted 1 line

I agree with Nick on the comments about the commit message. A few more small nits below, not sure they necessitate a v3, up to you. Thank you for the patch!

Quoted 10 lines

It would make my life easier to get a v3. :) I agree with all of Nathan's suggestions. :)

Thanks!

-Kees

Signature · 1 line
Kees Cook

Hello,

Ok, I'll make the requested changes, noted by Nathan and post v3 patch soon. :-) Btw. These patches were based on kees/for-next/clang/features branch.

Thanks for patience.
-Jarmo