* [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free
@ 2022-05-27 10:50 Tvrtko Ursulin
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated Tvrtko Ursulin
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Tvrtko Ursulin @ 2022-05-27 10:50 UTC (permalink / raw)
To: igt-dev; +Cc: Intel-gfx
From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Fix a possible oversight.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
lib/igt_device_scan.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c
index 3c23fe0eb520..a30433ae2cff 100644
--- a/lib/igt_device_scan.c
+++ b/lib/igt_device_scan.c
@@ -814,6 +814,11 @@ void igt_devices_free(void)
igt_device_free(dev);
free(dev);
}
+
+ igt_list_for_each_entry_safe(dev, tmp, &igt_devs.filtered, link) {
+ igt_list_del(&dev->link);
+ free(dev);
+ }
}
/**
--
2.32.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated
2022-05-27 10:50 [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Tvrtko Ursulin
@ 2022-05-27 10:50 ` Tvrtko Ursulin
2022-05-30 13:16 ` Petri Latvala
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit Tvrtko Ursulin
2022-05-30 2:40 ` [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Zbigniew Kempczyński
2 siblings, 1 reply; 7+ messages in thread
From: Tvrtko Ursulin @ 2022-05-27 10:50 UTC (permalink / raw)
To: igt-dev; +Cc: Intel-gfx
From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Ensure buffer is null terminated at the point where the read ended and not
at the end of the whole buffer. Otherwise string parsing can stray into
un-initialised memory.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
lib/igt_drm_fdinfo.c | 8 ++++----
lib/igt_drm_fdinfo.h | 4 ++--
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/lib/igt_drm_fdinfo.c b/lib/igt_drm_fdinfo.c
index b422f67a4ace..250d9e8917f2 100644
--- a/lib/igt_drm_fdinfo.c
+++ b/lib/igt_drm_fdinfo.c
@@ -44,12 +44,12 @@ static size_t read_fdinfo(char *buf, const size_t sz, int at, const char *name)
if (fd < 0)
return 0;
- buf[sz - 1] = 0;
- count = read(fd, buf, sz);
- buf[sz - 1] = 0;
+ count = read(fd, buf, sz - 1);
+ if (count > 0)
+ buf[count - 1] = 0;
close(fd);
- return count;
+ return count > 0 ? count : 0;
}
static int parse_engine(char *line, struct drm_client_fdinfo *info,
diff --git a/lib/igt_drm_fdinfo.h b/lib/igt_drm_fdinfo.h
index 5db63e28b07e..8759471615bd 100644
--- a/lib/igt_drm_fdinfo.h
+++ b/lib/igt_drm_fdinfo.h
@@ -46,7 +46,7 @@ struct drm_client_fdinfo {
* igt_parse_drm_fdinfo: Parses the drm fdinfo file
*
* @drm_fd: DRM file descriptor
- * @info: Structure to populate with read data
+ * @info: Structure to populate with read data. Must be zeroed.
*
* Returns the number of valid drm fdinfo keys found or zero if not all
* mandatory keys were present or no engines found.
@@ -58,7 +58,7 @@ unsigned int igt_parse_drm_fdinfo(int drm_fd, struct drm_client_fdinfo *info);
*
* @dir: File descriptor pointing to /proc/pid/fdinfo directory
* @fd: String representation of the file descriptor number to parse.
- * @info: Structure to populate with read data
+ * @info: Structure to populate with read data. Must be zeroed.
*
* Returns the number of valid drm fdinfo keys found or zero if not all
* mandatory keys were present or no engines found.
--
2.32.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit
2022-05-27 10:50 [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Tvrtko Ursulin
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated Tvrtko Ursulin
@ 2022-05-27 10:50 ` Tvrtko Ursulin
2022-05-30 13:17 ` Petri Latvala
2022-05-30 2:40 ` [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Zbigniew Kempczyński
2 siblings, 1 reply; 7+ messages in thread
From: Tvrtko Ursulin @ 2022-05-27 10:50 UTC (permalink / raw)
To: igt-dev; +Cc: Intel-gfx
From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Be nice and explicitly free all memory on exit.
Also fix a Valgrind reported unitilised conditional jump.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Cc: Petri Latvala <petri.latvala@intel.com>
---
tools/intel_gpu_top.c | 51 +++++++++++++++++++++++++++++++++++--------
1 file changed, 42 insertions(+), 9 deletions(-)
diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 26986a822bb7..997aff582ff7 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -437,6 +437,36 @@ static struct engines *discover_engines(char *device)
return engines;
}
+static void free_engines(struct engines *engines)
+{
+ struct pmu_counter **pmu, *free_list[] = {
+ &engines->r_gpu,
+ &engines->r_pkg,
+ &engines->imc_reads,
+ &engines->imc_writes,
+ NULL
+ };
+ unsigned int i;
+
+ for (pmu = &free_list[0]; *pmu; pmu++) {
+ if ((*pmu)->present)
+ free((char *)(*pmu)->units);
+ }
+
+ for (i = 0; i < engines->num_engines; i++) {
+ struct engine *engine = engine_ptr(engines, i);
+
+ free((char *)engine->name);
+ free((char *)engine->short_name);
+ free((char *)engine->display_name);
+ }
+
+ closedir(engines->root);
+
+ free(engines->class);
+ free(engines);
+}
+
#define _open_pmu(type, cnt, pmu, fd) \
({ \
int fd__; \
@@ -1073,7 +1103,7 @@ static size_t freadat2buf(char *buf, const size_t sz, DIR *at, const char *name)
return count;
}
-static struct clients *scan_clients(struct clients *clients)
+static struct clients *scan_clients(struct clients *clients, bool display)
{
struct dirent *proc_dent;
struct client *c;
@@ -1181,7 +1211,7 @@ next:
break;
}
- return display_clients(clients);
+ return display ? display_clients(clients) : clients;
}
static const char *bars[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" };
@@ -2391,7 +2421,7 @@ static void process_stdin(unsigned int timeout_us)
static bool has_drm_fdinfo(const struct igt_device_card *card)
{
- struct drm_client_fdinfo info;
+ struct drm_client_fdinfo info = { };
unsigned int cnt;
int fd;
@@ -2572,7 +2602,7 @@ int main(int argc, char **argv)
}
pmu_sample(engines);
- scan_clients(clients);
+ scan_clients(clients, false);
codename = igt_device_get_pretty_name(&card, false);
while (!stop_top) {
@@ -2599,7 +2629,7 @@ int main(int argc, char **argv)
pmu_sample(engines);
t = (double)(engines->ts.cur - engines->ts.prev) / 1e9;
- disp_clients = scan_clients(clients);
+ disp_clients = scan_clients(clients, true);
if (stop_top)
break;
@@ -2649,21 +2679,24 @@ int main(int argc, char **argv)
pops->close_struct();
}
- if (stop_top)
- break;
-
if (disp_clients != clients)
free_clients(disp_clients);
+ if (stop_top)
+ break;
+
if (output_mode == INTERACTIVE)
process_stdin(period_us);
else
usleep(period_us);
}
+ if (clients)
+ free_clients(clients);
+
free(codename);
err:
- free(engines);
+ free_engines(engines);
free(pmu_device);
exit:
igt_devices_free();
--
2.32.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free
2022-05-27 10:50 [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Tvrtko Ursulin
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated Tvrtko Ursulin
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit Tvrtko Ursulin
@ 2022-05-30 2:40 ` Zbigniew Kempczyński
2022-05-30 13:21 ` Petri Latvala
2 siblings, 1 reply; 7+ messages in thread
From: Zbigniew Kempczyński @ 2022-05-30 2:40 UTC (permalink / raw)
To: Tvrtko Ursulin; +Cc: igt-dev, Intel-gfx
On Fri, May 27, 2022 at 11:50:40AM +0100, Tvrtko Ursulin wrote:
> From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
>
> Fix a possible oversight.
Yes, properly coded in igt_device_scan() only. Thanks for spotting this.
>
> Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> ---
> lib/igt_device_scan.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c
> index 3c23fe0eb520..a30433ae2cff 100644
> --- a/lib/igt_device_scan.c
> +++ b/lib/igt_device_scan.c
> @@ -814,6 +814,11 @@ void igt_devices_free(void)
> igt_device_free(dev);
> free(dev);
> }
> +
> + igt_list_for_each_entry_safe(dev, tmp, &igt_devs.filtered, link) {
> + igt_list_del(&dev->link);
> + free(dev);
> + }
Small nit - I would change the order (filtered list I would remove first).
igt_device_free() also frees dev->devnode, ... so if we would change the
code to be more "parallel" it would be better to avoid use-after-free.
With this:
Reviewed-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
--
Zbigniew
> }
>
> /**
> --
> 2.32.0
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated Tvrtko Ursulin
@ 2022-05-30 13:16 ` Petri Latvala
0 siblings, 0 replies; 7+ messages in thread
From: Petri Latvala @ 2022-05-30 13:16 UTC (permalink / raw)
To: Tvrtko Ursulin; +Cc: igt-dev, Intel-gfx
On Fri, May 27, 2022 at 11:50:41AM +0100, Tvrtko Ursulin wrote:
> From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
>
> Ensure buffer is null terminated at the point where the read ended and not
> at the end of the whole buffer. Otherwise string parsing can stray into
> un-initialised memory.
>
> Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Petri Latvala <petri.latvala@intel.com>
> ---
> lib/igt_drm_fdinfo.c | 8 ++++----
> lib/igt_drm_fdinfo.h | 4 ++--
> 2 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/lib/igt_drm_fdinfo.c b/lib/igt_drm_fdinfo.c
> index b422f67a4ace..250d9e8917f2 100644
> --- a/lib/igt_drm_fdinfo.c
> +++ b/lib/igt_drm_fdinfo.c
> @@ -44,12 +44,12 @@ static size_t read_fdinfo(char *buf, const size_t sz, int at, const char *name)
> if (fd < 0)
> return 0;
>
> - buf[sz - 1] = 0;
> - count = read(fd, buf, sz);
> - buf[sz - 1] = 0;
> + count = read(fd, buf, sz - 1);
> + if (count > 0)
> + buf[count - 1] = 0;
> close(fd);
>
> - return count;
> + return count > 0 ? count : 0;
> }
>
> static int parse_engine(char *line, struct drm_client_fdinfo *info,
> diff --git a/lib/igt_drm_fdinfo.h b/lib/igt_drm_fdinfo.h
> index 5db63e28b07e..8759471615bd 100644
> --- a/lib/igt_drm_fdinfo.h
> +++ b/lib/igt_drm_fdinfo.h
> @@ -46,7 +46,7 @@ struct drm_client_fdinfo {
> * igt_parse_drm_fdinfo: Parses the drm fdinfo file
> *
> * @drm_fd: DRM file descriptor
> - * @info: Structure to populate with read data
> + * @info: Structure to populate with read data. Must be zeroed.
> *
> * Returns the number of valid drm fdinfo keys found or zero if not all
> * mandatory keys were present or no engines found.
> @@ -58,7 +58,7 @@ unsigned int igt_parse_drm_fdinfo(int drm_fd, struct drm_client_fdinfo *info);
> *
> * @dir: File descriptor pointing to /proc/pid/fdinfo directory
> * @fd: String representation of the file descriptor number to parse.
> - * @info: Structure to populate with read data
> + * @info: Structure to populate with read data. Must be zeroed.
> *
> * Returns the number of valid drm fdinfo keys found or zero if not all
> * mandatory keys were present or no engines found.
> --
> 2.32.0
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit Tvrtko Ursulin
@ 2022-05-30 13:17 ` Petri Latvala
0 siblings, 0 replies; 7+ messages in thread
From: Petri Latvala @ 2022-05-30 13:17 UTC (permalink / raw)
To: Tvrtko Ursulin; +Cc: igt-dev, Intel-gfx
On Fri, May 27, 2022 at 11:50:42AM +0100, Tvrtko Ursulin wrote:
> From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
>
> Be nice and explicitly free all memory on exit.
>
> Also fix a Valgrind reported unitilised conditional jump.
>
> Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> Cc: Petri Latvala <petri.latvala@intel.com>
Reviewed-by: Petri Latvala <petri.latvala@intel.com>
> ---
> tools/intel_gpu_top.c | 51 +++++++++++++++++++++++++++++++++++--------
> 1 file changed, 42 insertions(+), 9 deletions(-)
>
> diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
> index 26986a822bb7..997aff582ff7 100644
> --- a/tools/intel_gpu_top.c
> +++ b/tools/intel_gpu_top.c
> @@ -437,6 +437,36 @@ static struct engines *discover_engines(char *device)
> return engines;
> }
>
> +static void free_engines(struct engines *engines)
> +{
> + struct pmu_counter **pmu, *free_list[] = {
> + &engines->r_gpu,
> + &engines->r_pkg,
> + &engines->imc_reads,
> + &engines->imc_writes,
> + NULL
> + };
> + unsigned int i;
> +
> + for (pmu = &free_list[0]; *pmu; pmu++) {
> + if ((*pmu)->present)
> + free((char *)(*pmu)->units);
> + }
> +
> + for (i = 0; i < engines->num_engines; i++) {
> + struct engine *engine = engine_ptr(engines, i);
> +
> + free((char *)engine->name);
> + free((char *)engine->short_name);
> + free((char *)engine->display_name);
> + }
> +
> + closedir(engines->root);
> +
> + free(engines->class);
> + free(engines);
> +}
> +
> #define _open_pmu(type, cnt, pmu, fd) \
> ({ \
> int fd__; \
> @@ -1073,7 +1103,7 @@ static size_t freadat2buf(char *buf, const size_t sz, DIR *at, const char *name)
> return count;
> }
>
> -static struct clients *scan_clients(struct clients *clients)
> +static struct clients *scan_clients(struct clients *clients, bool display)
> {
> struct dirent *proc_dent;
> struct client *c;
> @@ -1181,7 +1211,7 @@ next:
> break;
> }
>
> - return display_clients(clients);
> + return display ? display_clients(clients) : clients;
> }
>
> static const char *bars[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" };
> @@ -2391,7 +2421,7 @@ static void process_stdin(unsigned int timeout_us)
>
> static bool has_drm_fdinfo(const struct igt_device_card *card)
> {
> - struct drm_client_fdinfo info;
> + struct drm_client_fdinfo info = { };
> unsigned int cnt;
> int fd;
>
> @@ -2572,7 +2602,7 @@ int main(int argc, char **argv)
> }
>
> pmu_sample(engines);
> - scan_clients(clients);
> + scan_clients(clients, false);
> codename = igt_device_get_pretty_name(&card, false);
>
> while (!stop_top) {
> @@ -2599,7 +2629,7 @@ int main(int argc, char **argv)
> pmu_sample(engines);
> t = (double)(engines->ts.cur - engines->ts.prev) / 1e9;
>
> - disp_clients = scan_clients(clients);
> + disp_clients = scan_clients(clients, true);
>
> if (stop_top)
> break;
> @@ -2649,21 +2679,24 @@ int main(int argc, char **argv)
> pops->close_struct();
> }
>
> - if (stop_top)
> - break;
> -
> if (disp_clients != clients)
> free_clients(disp_clients);
>
> + if (stop_top)
> + break;
> +
> if (output_mode == INTERACTIVE)
> process_stdin(period_us);
> else
> usleep(period_us);
> }
>
> + if (clients)
> + free_clients(clients);
> +
> free(codename);
> err:
> - free(engines);
> + free_engines(engines);
> free(pmu_device);
> exit:
> igt_devices_free();
> --
> 2.32.0
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free
2022-05-30 2:40 ` [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Zbigniew Kempczyński
@ 2022-05-30 13:21 ` Petri Latvala
0 siblings, 0 replies; 7+ messages in thread
From: Petri Latvala @ 2022-05-30 13:21 UTC (permalink / raw)
To: Zbigniew Kempczyński; +Cc: igt-dev, Intel-gfx
On Mon, May 30, 2022 at 04:40:06AM +0200, Zbigniew Kempczyński wrote:
> On Fri, May 27, 2022 at 11:50:40AM +0100, Tvrtko Ursulin wrote:
> > From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> >
> > Fix a possible oversight.
>
> Yes, properly coded in igt_device_scan() only. Thanks for spotting this.
>
> >
> > Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> > ---
> > lib/igt_device_scan.c | 5 +++++
> > 1 file changed, 5 insertions(+)
> >
> > diff --git a/lib/igt_device_scan.c b/lib/igt_device_scan.c
> > index 3c23fe0eb520..a30433ae2cff 100644
> > --- a/lib/igt_device_scan.c
> > +++ b/lib/igt_device_scan.c
> > @@ -814,6 +814,11 @@ void igt_devices_free(void)
> > igt_device_free(dev);
> > free(dev);
> > }
> > +
> > + igt_list_for_each_entry_safe(dev, tmp, &igt_devs.filtered, link) {
> > + igt_list_del(&dev->link);
> > + free(dev);
> > + }
>
> Small nit - I would change the order (filtered list I would remove first).
> igt_device_free() also frees dev->devnode, ... so if we would change the
> code to be more "parallel" it would be better to avoid use-after-free.
>
> With this:
>
> Reviewed-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
Tvrtko is away this week so I made this change and merged.
--
Petri Latvala
>
> --
> Zbigniew
>
> > }
> >
> > /**
> > --
> > 2.32.0
> >
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2022-05-30 13:23 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-27 10:50 [Intel-gfx] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Tvrtko Ursulin
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 2/3] lib/drm_fdinfo: Ensure buffer is null terminated Tvrtko Ursulin
2022-05-30 13:16 ` Petri Latvala
2022-05-27 10:50 ` [Intel-gfx] [PATCH i-g-t 3/3] intel_gpu_top: Free all memory on exit Tvrtko Ursulin
2022-05-30 13:17 ` Petri Latvala
2022-05-30 2:40 ` [Intel-gfx] [igt-dev] [PATCH i-g-t 1/3] lib/igt_device_scan: Free filtered devices in igt_devices_free Zbigniew Kempczyński
2022-05-30 13:21 ` Petri Latvala
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox