[PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset

Zecheng Li posted 10 patches 1 month, 1 week ago
Only 5 patches received!
There is a newer version of this series
[PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Zecheng Li 1 month, 1 week ago
match_var_offset compares address offsets to determine if an access
falls within a variable's bounds. The offsets involved for those
relative to base registers from DW_OP_breg can be negative.

The current implementation uses unsigned types (u64) for these offsets,
which rejects almost all negative values.

Change the signature of match_var_offset to use signed types (s64). This
ensures correct behavior when addr_offset or addr_type are negative.

Signed-off-by: Zecheng Li <zecheng@google.com>
---
 tools/perf/util/dwarf-aux.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
index 559c953ca172..920054425578 100644
--- a/tools/perf/util/dwarf-aux.c
+++ b/tools/perf/util/dwarf-aux.c
@@ -1388,18 +1388,19 @@ struct find_var_data {
 #define DWARF_OP_DIRECT_REGS  32
 
 static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data,
-			     u64 addr_offset, u64 addr_type, bool is_pointer)
+			     s64 addr_offset, s64 addr_type, bool is_pointer)
 {
 	Dwarf_Die type_die;
 	Dwarf_Word size;
+	s64 offset = addr_offset - addr_type;
 
-	if (addr_offset == addr_type) {
+	if (offset == 0) {
 		/* Update offset relative to the start of the variable */
 		data->offset = 0;
 		return true;
 	}
 
-	if (addr_offset < addr_type)
+	if (offset < 0)
 		return false;
 
 	if (die_get_real_type(die_mem, &type_die) == NULL)
@@ -1414,11 +1415,11 @@ static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data,
 	if (dwarf_aggregate_size(&type_die, &size) < 0)
 		return false;
 
-	if (addr_offset >= addr_type + size)
+	if ((u64)offset >= size)
 		return false;
 
 	/* Update offset relative to the start of the variable */
-	data->offset = addr_offset - addr_type;
+	data->offset = offset;
 	return true;
 }
 
-- 
2.51.0.261.g7ce5a0a67e-goog
Re: [PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Namhyung Kim 1 month ago
On Mon, Aug 25, 2025 at 07:54:03PM +0000, Zecheng Li wrote:
> match_var_offset compares address offsets to determine if an access
> falls within a variable's bounds. The offsets involved for those
> relative to base registers from DW_OP_breg can be negative.
> 
> The current implementation uses unsigned types (u64) for these offsets,
> which rejects almost all negative values.

Right, I thought it cannot get negative offsets except for stack access
(e.g. fbreg).  But it turns out that container_of() trick can generate
them with optimizing compilers.

> 
> Change the signature of match_var_offset to use signed types (s64). This
> ensures correct behavior when addr_offset or addr_type are negative.
> 
> Signed-off-by: Zecheng Li <zecheng@google.com>

I've confirmed it produced slightly better results on my test sets.

Reviewed-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung

> ---
>  tools/perf/util/dwarf-aux.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c
> index 559c953ca172..920054425578 100644
> --- a/tools/perf/util/dwarf-aux.c
> +++ b/tools/perf/util/dwarf-aux.c
> @@ -1388,18 +1388,19 @@ struct find_var_data {
>  #define DWARF_OP_DIRECT_REGS  32
>  
>  static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data,
> -			     u64 addr_offset, u64 addr_type, bool is_pointer)
> +			     s64 addr_offset, s64 addr_type, bool is_pointer)
>  {
>  	Dwarf_Die type_die;
>  	Dwarf_Word size;
> +	s64 offset = addr_offset - addr_type;
>  
> -	if (addr_offset == addr_type) {
> +	if (offset == 0) {
>  		/* Update offset relative to the start of the variable */
>  		data->offset = 0;
>  		return true;
>  	}
>  
> -	if (addr_offset < addr_type)
> +	if (offset < 0)
>  		return false;
>  
>  	if (die_get_real_type(die_mem, &type_die) == NULL)
> @@ -1414,11 +1415,11 @@ static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data,
>  	if (dwarf_aggregate_size(&type_die, &size) < 0)
>  		return false;
>  
> -	if (addr_offset >= addr_type + size)
> +	if ((u64)offset >= size)
>  		return false;
>  
>  	/* Update offset relative to the start of the variable */
> -	data->offset = addr_offset - addr_type;
> +	data->offset = offset;
>  	return true;
>  }
>  
> -- 
> 2.51.0.261.g7ce5a0a67e-goog
>
Re: [PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Arnaldo Carvalho de Melo 1 month ago
On Wed, Aug 27, 2025 at 11:52:02PM -0700, Namhyung Kim wrote:
> On Mon, Aug 25, 2025 at 07:54:03PM +0000, Zecheng Li wrote:
> > match_var_offset compares address offsets to determine if an access
> > falls within a variable's bounds. The offsets involved for those
> > relative to base registers from DW_OP_breg can be negative.

> > The current implementation uses unsigned types (u64) for these offsets,
> > which rejects almost all negative values.
 
> Right, I thought it cannot get negative offsets except for stack access
> (e.g. fbreg).  But it turns out that container_of() trick can generate
> them with optimizing compilers.
 
> > Change the signature of match_var_offset to use signed types (s64). This
> > ensures correct behavior when addr_offset or addr_type are negative.

> > Signed-off-by: Zecheng Li <zecheng@google.com>
 
> I've confirmed it produced slightly better results on my test sets.
 
> Reviewed-by: Namhyung Kim <namhyung@kernel.org>

Cherry picked this first patch to make a bit of progress in the
perf-tools-next front.

- Arnaldo
Re: [PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Arnaldo Carvalho de Melo 4 weeks, 1 day ago
On Wed, Sep 03, 2025 at 12:49:49PM -0300, Arnaldo Carvalho de Melo wrote:
> On Wed, Aug 27, 2025 at 11:52:02PM -0700, Namhyung Kim wrote:
> > On Mon, Aug 25, 2025 at 07:54:03PM +0000, Zecheng Li wrote:
> > > match_var_offset compares address offsets to determine if an access
> > > falls within a variable's bounds. The offsets involved for those
> > > relative to base registers from DW_OP_breg can be negative.
 
> > > The current implementation uses unsigned types (u64) for these offsets,
> > > which rejects almost all negative values.
  
> > Right, I thought it cannot get negative offsets except for stack access
> > (e.g. fbreg).  But it turns out that container_of() trick can generate
> > them with optimizing compilers.
  
> > > Change the signature of match_var_offset to use signed types (s64). This
> > > ensures correct behavior when addr_offset or addr_type are negative.
 
> > > Signed-off-by: Zecheng Li <zecheng@google.com>
  
> > I've confirmed it produced slightly better results on my test sets.
  
> > Reviewed-by: Namhyung Kim <namhyung@kernel.org>
 
> Cherry picked this first patch to make a bit of progress in the
> perf-tools-next front.

It is in perf-tools-next/perf-tools-next now (this first reviewed one):

https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/log/?h=perf-tools-next

- Arnaldo
Re: [PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Namhyung Kim 4 weeks ago
On Wed, Sep 03, 2025 at 07:05:23PM -0300, Arnaldo Carvalho de Melo wrote:
> On Wed, Sep 03, 2025 at 12:49:49PM -0300, Arnaldo Carvalho de Melo wrote:
> > On Wed, Aug 27, 2025 at 11:52:02PM -0700, Namhyung Kim wrote:
> > > On Mon, Aug 25, 2025 at 07:54:03PM +0000, Zecheng Li wrote:
> > > > match_var_offset compares address offsets to determine if an access
> > > > falls within a variable's bounds. The offsets involved for those
> > > > relative to base registers from DW_OP_breg can be negative.
>  
> > > > The current implementation uses unsigned types (u64) for these offsets,
> > > > which rejects almost all negative values.
>   
> > > Right, I thought it cannot get negative offsets except for stack access
> > > (e.g. fbreg).  But it turns out that container_of() trick can generate
> > > them with optimizing compilers.
>   
> > > > Change the signature of match_var_offset to use signed types (s64). This
> > > > ensures correct behavior when addr_offset or addr_type are negative.
>  
> > > > Signed-off-by: Zecheng Li <zecheng@google.com>
>   
> > > I've confirmed it produced slightly better results on my test sets.
>   
> > > Reviewed-by: Namhyung Kim <namhyung@kernel.org>
>  
> > Cherry picked this first patch to make a bit of progress in the
> > perf-tools-next front.
> 
> It is in perf-tools-next/perf-tools-next now (this first reviewed one):
> 
> https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/log/?h=perf-tools-next

Thanks for doing that.  Please pick up the patch 2 and 3 as well.
Namhyung
Re: [PATCH v2 01/10] perf dwarf-aux: Use signed variable types in match_var_offset
Posted by Arnaldo Carvalho de Melo 2 weeks, 6 days ago
On Fri, Sep 05, 2025 at 12:50:30PM -0700, Namhyung Kim wrote:
> On Wed, Sep 03, 2025 at 07:05:23PM -0300, Arnaldo Carvalho de Melo wrote:
> > On Wed, Sep 03, 2025 at 12:49:49PM -0300, Arnaldo Carvalho de Melo wrote:
> > > On Wed, Aug 27, 2025 at 11:52:02PM -0700, Namhyung Kim wrote:
> > > > On Mon, Aug 25, 2025 at 07:54:03PM +0000, Zecheng Li wrote:
> > > > > Change the signature of match_var_offset to use signed types (s64). This
> > > > > ensures correct behavior when addr_offset or addr_type are negative.
  
> > > > > Signed-off-by: Zecheng Li <zecheng@google.com>
   
> > > > I've confirmed it produced slightly better results on my test sets.
  
> > > > Reviewed-by: Namhyung Kim <namhyung@kernel.org>
  
> > > Cherry picked this first patch to make a bit of progress in the
> > > perf-tools-next front.

> > It is in perf-tools-next/perf-tools-next now (this first reviewed one):
 
> > https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/log/?h=perf-tools-next
 
> Thanks for doing that.  Please pick up the patch 2 and 3 as well.

Done.

- Arnaldo