All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian Brzezinka <sebastian.brzezinka@intel.com>
To: Krzysztof Karas <krzysztof.karas@intel.com>,
	Sebastian Brzezinka <sebastian.brzezinka@intel.com>
Cc: <igt-dev@lists.freedesktop.org>,
	<kamil.konieczny@linux.intel.com>, <krzysztof.niemiec@intel.com>
Subject: Re: [PATCH i-g-t 1/8] tools/gputop/xe_gputop: fix struct layout and PMU read
Date: Wed, 30 Sep 2026 14:30:28 +0200	[thread overview]
Message-ID: <DLSNHP88FJSD.36EMO2RGBKGAB@intel.com> (raw)
In-Reply-To: <cvzmslku3u4tnyljjwtzeocdz2mkxopxpi6amy7n4icjbrmtqe@svjtvfgg2ii5>

Hi Krzysztof,

On Mon Sep 28, 2026 at 10:49 AM CEST, Krzysztof Karas wrote:
> Hi Sebastian,
>
> On 2026-09-25 at 12:57:41 +0200, Sebastian Brzezinka wrote:
>> struct xe_pmu_device ended with a plain 'struct xe_engine engine' member,
>> but xe_populate_engines() reallocs the struct to hold multiple engines
>> beyond that member, which is undefined behaviour. It is replaced with a
>> proper C99 flexible array member (engine[]), and engine_ptr() is updated
>> to use [] indexing accordingly.
>> 
>> pmu_read_multi() and xe_pmu_sample() also used VLAs (uint64_t buf[2+num],
>> val[2+num_val]) which are forbidden by -Wvla in IGT builds, so both are
>> replaced with heap allocated buffers.
>> 
>> pmu_read_multi() aborted the process via assert() whenever the perf
>> read returned an unexpected length since a monitoring tool should never
> There needs to be some separation between these two sentences:
> ...read returned an unexpected length. Since a monitoring tool
> should never...
>
>> crash on a I/O error, the assert is replaced with an error message and
>> a graceful return.
>> 
>> An explicit #include <glib.h> is also added for the CLAMP() macro used
>> in pmu_active_percentage().
>> 
>> Signed-off-by: Sebastian Brzezinka <sebastian.brzezinka@intel.com>
>> ---
>>  tools/gputop/xe_gputop.c | 35 +++++++++++++++++++++++++++--------
>>  tools/gputop/xe_gputop.h |  2 +-
>>  2 files changed, 28 insertions(+), 9 deletions(-)
>> 
>> diff --git a/tools/gputop/xe_gputop.c b/tools/gputop/xe_gputop.c
>> index bb2caa6ea..119545c75 100644
>> --- a/tools/gputop/xe_gputop.c
>> +++ b/tools/gputop/xe_gputop.c
>> @@ -3,9 +3,10 @@
>>   * Copyright © 2025 Intel Corporation
>>   */
>>  
>> +#include <glib.h>
>>  #include "xe_gputop.h"
>>  
>> -#define engine_ptr(pmu_device, n) (&(pmu_device)->engine + (n))
>> +#define engine_ptr(pmu_device, n) (&(pmu_device)->engine[(n)])
>>  
>>  static void __update_sample(struct xe_pmu_counter *counter, uint64_t val)
>>  {
>> @@ -212,29 +213,45 @@ void *xe_populate_engines(const void *obj, int index)
>>  
>>  static uint64_t pmu_read_multi(int fd, unsigned int num, uint64_t *val)
>>  {
>> -	uint64_t buf[2 + num];
>> +	const size_t buf_size = (2 + num) * sizeof(uint64_t);
>> +	uint64_t *buf;
>> +	uint64_t ts = 0;
>>  	unsigned int i;
>>  	ssize_t len;
>>  
>> -	memset(buf, 0, sizeof(buf));
>> +	buf = malloc(buf_size);
>> +	if (!buf)
>> +		return 0;
>>  
>> -	len = read(fd, buf, sizeof(buf));
>> -	assert(len == sizeof(buf));
>> +	memset(buf, 0, buf_size);
>> +	len = read(fd, buf, buf_size);
>> +	if (len != (ssize_t)buf_size) {
>> +		fprintf(stderr, "xe: PMU read error: expected %zu bytes, got %zd\n",
> Please remove "xe: " prefix from the message. It may suggest
> that this has been emitted from the driver somehow and other
> tools do not use this convention.
I removed it at one point and brought it back, since it's useful. It
only goes to stderr, not syslog/dmesg, so there's no risk of it being
mistaken for a message from the kernel driver. It's a simple way to tell
which backend failed without needing more elaborate logging.


>> +			buf_size, len);
>> +		free(buf);
>> +		return 0;
>> +	}
>>  
>>  	for (i = 0; i < num; i++)
>>  		val[i] = buf[2 + i];
>>  
>> -	return buf[1];
>> +	ts = buf[1];
>> +	free(buf);
>> +	return ts;
> This value is ignored (currently as well). Maybe, convert the
> function to void type or add the return check to
> xe_pmu_sample()?
>
>>  }
>>  
>>  void xe_pmu_sample(const void *obj, int index)
>>  {
>>  	struct xe_gputop *ptr = ((struct xe_gputop *)obj) + index;
>>  	struct xe_pmu_device *engines = ptr->pmu_device_obj;
>> -	const int num_val = engines->num_counters;
>> -	uint64_t val[2 + num_val];
>> +	const unsigned int num_val = engines->num_counters;
>> +	uint64_t *val;
>>  	unsigned int i;
>>  
>> +	val = calloc(num_val, sizeof(*val));
>> +	if (!val)
>> +		return;
>> +
>>  	pmu_read_multi(engines->fd, num_val, val);
>>  
>>  	for (i = 0; i < engines->num_engines; i++) {
>> @@ -243,6 +260,8 @@ void xe_pmu_sample(const void *obj, int index)
>>  		update_sample(&engine->engine_active_ticks, val);
>>  		update_sample(&engine->engine_total_ticks, val);
>>  	}
>> +
>> +	free(val);
>>  }
>>  
>>  int xe_pmu_init(const void *obj, int index)
>> diff --git a/tools/gputop/xe_gputop.h b/tools/gputop/xe_gputop.h
>> index 1e3856071..c26db0e7e 100644
>> --- a/tools/gputop/xe_gputop.h
>> +++ b/tools/gputop/xe_gputop.h
>> @@ -41,7 +41,7 @@ struct xe_pmu_device {
>>  	unsigned int num_counters;
>>  	int fd;
>>  	char *device;
>> -	struct xe_engine engine;
>> +	struct xe_engine engine[];
>>  };
>>  
>>  struct xe_gputop {
>> -- 
>> 2.55.0
>> 





-- 
Best regards,
Sebastian


  reply	other threads:[~2026-09-30 12:31 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 10:57 [PATCH i-g-t 0/8] tools/gputop: add i915 PMU engine monitoring Sebastian Brzezinka
2026-09-25 10:57 ` [PATCH i-g-t 1/8] tools/gputop/xe_gputop: fix struct layout and PMU read Sebastian Brzezinka
2026-09-28  8:49   ` Krzysztof Karas
2026-09-30 12:30     ` Sebastian Brzezinka [this message]
2026-09-25 10:57 ` [PATCH i-g-t 2/8] tools/gputop/xe_gputop: fix CLAMP() usage Sebastian Brzezinka
2026-09-28  8:50   ` Krzysztof Karas
2026-09-29 10:14   ` Kamil Konieczny
2026-09-25 10:57 ` [PATCH i-g-t 3/8] tools/gputop/xe_gputop: fix return logic, cleanup and div by zero bugs Sebastian Brzezinka
2026-09-28  9:12   ` Krzysztof Karas
2026-09-25 10:57 ` [PATCH i-g-t 4/8] tools/gputop/i915_gputop: add i915 PMU engine monitoring Sebastian Brzezinka
2026-09-28 11:01   ` Krzysztof Karas
2026-09-30 11:06     ` Sebastian Brzezinka
2026-09-30 14:39       ` Kamil Konieczny
2026-10-02  2:29         ` vitaly prosyak
2026-10-02  8:59   ` Tvrtko Ursulin
2026-09-25 10:57 ` [PATCH i-g-t 5/8] tools/meson: build gputop PMU backends as part of gputop Sebastian Brzezinka
2026-09-29  5:58   ` Krzysztof Karas
2026-09-25 10:57 ` [PATCH i-g-t 6/8] tools/gputop/utils: guard ANSI macros against redefinition Sebastian Brzezinka
2026-09-29  5:58   ` Krzysztof Karas
2026-09-25 10:57 ` [PATCH i-g-t 7/8] tools/gputop: integrate i915 and xe PMU engine monitoring Sebastian Brzezinka
2026-09-29  8:49   ` Krzysztof Karas
2026-09-29  9:16     ` Jani Nikula
2026-09-25 10:57 ` [PATCH i-g-t 8/8] tests/tools_test: add gputop PMU engine monitoring subtests Sebastian Brzezinka
2026-09-29  9:39   ` Krzysztof Karas
2026-09-25 12:48 ` ✓ i915.CI.BAT: success for tools/gputop: add i915 PMU engine monitoring Patchwork
2026-09-25 13:02 ` ✓ Xe.CI.BAT: " Patchwork
2026-09-25 20:21 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-09-26  5:44 ` ✗ i915.CI.Full: " Patchwork
2026-09-28 20:59 ` ✗ Xe.CI.BAT: failure for tools/gputop: add i915 PMU engine monitoring (rev2) Patchwork
2026-09-28 21:05 ` ✓ i915.CI.BAT: success " Patchwork
2026-09-29  2:59 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-09-29  5:06 ` ✓ i915.CI.Full: success " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DLSNHP88FJSD.36EMO2RGBKGAB@intel.com \
    --to=sebastian.brzezinka@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=kamil.konieczny@linux.intel.com \
    --cc=krzysztof.karas@intel.com \
    --cc=krzysztof.niemiec@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.