diff mbox series

[i-g-t,v4,5/5] lib/gem_engine_topology: Fix broken compare of device links

Message ID 20240718085912.15434-12-janusz.krzysztofik@linux.intel.com (mailing list archive)
State New, archived
Headers show
Series tests/gem_ctx_exec: Fix failing preempt timeout updates | expand

Commit Message

Janusz Krzysztofik July 18, 2024, 8:55 a.m. UTC
When looking for a primary counterpart of a render device, we compare
"device" links of both nodes.  If those links point to the same device
then we know we've found the correct primary node.

However, readlinkat() function we use doesn't explicitly terminate read in
strings with null characters, and then the comparison occasionally fails.

Process the second (potential primary counterpart) node only if its
"device" link is of the same length as that of the render node, and limit
the number of compared characters to that length.

Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/6268
Signed-off-by: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>
---
 lib/i915/gem_engine_topology.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Kamil Konieczny July 18, 2024, 12:17 p.m. UTC | #1
Hi Janusz,
On 2024-07-18 at 10:55:16 +0200, Janusz Krzysztofik wrote:
> When looking for a primary counterpart of a render device, we compare
> "device" links of both nodes.  If those links point to the same device
> then we know we've found the correct primary node.
> 
> However, readlinkat() function we use doesn't explicitly terminate read in
> strings with null characters, and then the comparison occasionally fails.
> 
> Process the second (potential primary counterpart) node only if its
> "device" link is of the same length as that of the render node, and limit
> the number of compared characters to that length.
> 
> Closes: https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/6268
> Signed-off-by: Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>

Reviewed-by: Kamil Konieczny <kamil.konieczny@linux.intel.com>

> ---
>  lib/i915/gem_engine_topology.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/i915/gem_engine_topology.c b/lib/i915/gem_engine_topology.c
> index c8c1079223..c251060341 100644
> --- a/lib/i915/gem_engine_topology.c
> +++ b/lib/i915/gem_engine_topology.c
> @@ -467,7 +467,8 @@ static int __open_primary(int dir)
>  	if (minor < 64)
>  		return dir;
>  
> -	if (igt_debug_on(readlinkat(dir, "device", target, sizeof(target)) < 0))
> +	len = readlinkat(dir, "device", target, sizeof(target));
> +	if (igt_debug_on(len <= 0))
>  		return dir;
>  
>  	close(dir);
> @@ -477,8 +478,8 @@ static int __open_primary(int dir)
>  		if (dir < 0)
>  			continue;
>  
> -		if (readlinkat(dir, "device", device, sizeof(device)) > 0 &&
> -		    !strcmp(device, target))
> +		if (readlinkat(dir, "device", device, sizeof(device)) == len &&
> +		    !strncmp(device, target, len))
>  			break;
>  
>  		close(dir);
> -- 
> 2.45.2
>
diff mbox series

Patch

diff --git a/lib/i915/gem_engine_topology.c b/lib/i915/gem_engine_topology.c
index c8c1079223..c251060341 100644
--- a/lib/i915/gem_engine_topology.c
+++ b/lib/i915/gem_engine_topology.c
@@ -467,7 +467,8 @@  static int __open_primary(int dir)
 	if (minor < 64)
 		return dir;
 
-	if (igt_debug_on(readlinkat(dir, "device", target, sizeof(target)) < 0))
+	len = readlinkat(dir, "device", target, sizeof(target));
+	if (igt_debug_on(len <= 0))
 		return dir;
 
 	close(dir);
@@ -477,8 +478,8 @@  static int __open_primary(int dir)
 		if (dir < 0)
 			continue;
 
-		if (readlinkat(dir, "device", device, sizeof(device)) > 0 &&
-		    !strcmp(device, target))
+		if (readlinkat(dir, "device", device, sizeof(device)) == len &&
+		    !strncmp(device, target, len))
 			break;
 
 		close(dir);