[PATCH] lib/xe/xe_gt: Guard against fopen failure in xe_gt_stats_get_count

Jonathan Cavitt jonathan.cavitt at intel.com
Tue Dec 3 20:26:00 UTC 2024


The function xe_gt_stats_get_count currently performs an fopen on the
/sys/kernel/debug/dri/0/gt*/stats directory without checking that the
fopen is successful.  This can cause a system crash when trying to
perform fgets on a NULL pointer on unsuccessful cases.

Check the results of fopen and guard against this.

Closes: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/3212
Signed-off-by: Jonathan Cavitt <jonathan.cavitt at intel.com>
---
 lib/xe/xe_gt.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/xe/xe_gt.c b/lib/xe/xe_gt.c
index cee892af96..101b887a41 100644
--- a/lib/xe/xe_gt.c
+++ b/lib/xe/xe_gt.c
@@ -159,6 +159,8 @@ int xe_gt_stats_get_count(int fd, int gt, const char *stat)
 	sprintf(path, "/sys/kernel/debug/dri/0/gt%d/stats", gt);
 	f = fopen(path, "r");
 
+	igt_assert_f(f, "Failed to open /sys/kernel/debug/dri/0/gt%d/stats", gt);
+
 	while (fgets(tlb_path, sizeof(tlb_path), f)) {
 		if (strstr(tlb_path, stat) != NULL) {
 			sscanf(tlb_path, "%*[^:]: %d", &count);
-- 
2.43.0



More information about the igt-dev mailing list