Skip to contentSkip to conversation

drm/amdkfd: Check cgroup when returning DMABuf info

PATCH In review Mukul Joshi 3 participants 7 messages Mar 15, 2024 – Mar 21, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Check cgroup when returning DMABuf info

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -2
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep,
 
 	/* Find a KFD GPU device that supports the get_dmabuf_info query */
 	for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-		if (dev)
+		if (dev && !kfd_devcgroup_check_permission(dev))
 			break;
 	if (!dev)
 		return -EINVAL;
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep,
 	if (xcp_id >= 0)
 		args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
 	else
-		args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+		args->gpu_id = dev->id;
 	args->flags = flags;
 
 	/* Copy metadata buffer to user mode */
Signature · 1 line
2.35.1
maintainer
#1 lore raw
Quoted 23 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep,
 
  	/* Find a KFD GPU device that supports the get_dmabuf_info query */
  	for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-		if (dev)
+		if (dev && !kfd_devcgroup_check_permission(dev))
  			break;
  	if (!dev)
  		return -EINVAL;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file *filep, · line 1548

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file *filep,
  	if (xcp_id >= 0)
  		args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
  	else
-		args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+		args->gpu_id = dev->id;

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended. Maybe this would preserve the behaviour for that case:

	...

- else
+ else if (!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev->nodes[0]))
args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+ else
+ args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
  	args->flags = flags;
 
  	/* Copy metadata buffer to user mode */
#2 lore raw

[AMD Official Use Only - General]

Quoted 39 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Monday, March 18, 2024 4:13 PM
To: Joshi, Mukul <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdkfd: Check cgroup when returning DMABuf info

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file

Quoted 24 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file
 *filep,
 
    /* Find a KFD GPU device that supports the get_dmabuf_info query */
    for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-           if (dev)
+           if (dev && !kfd_devcgroup_check_permission(dev))
                    break;
    if (!dev)
            return -EINVAL;
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file

*filep,

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file *filep, · line 1548

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
    if (xcp_id >= 0)
            args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
    else
-           args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+           args->gpu_id = dev->id;
Quoted 7 lines

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended. Maybe this would preserve the behaviour for that case:

Can you please explain why this could be a issue on a single partition GPU?

Regards,
Mukul

Quoted 21 lines
      ...
(quoted diff) +1 -1
-     else
+     else if (!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev-

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file · line 1549

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
 nodes[0]))
              args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
(quoted diff) +2 -0
+     else
+             args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the
exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
    args->flags = flags;
 
    /* Copy metadata buffer to user mode */
maintainer
#3 lore raw
Quoted 40 lines · Mukul

[AMD Official Use Only - General]

Quoted 38 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Monday, March 18, 2024 4:13 PM
To: Joshi, Mukul <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdkfd: Check cgroup when returning DMABuf info

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file

Quoted 24 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
   drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file
 *filep,
 
     /* Find a KFD GPU device that supports the get_dmabuf_info query */
     for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-           if (dev)
+           if (dev && !kfd_devcgroup_check_permission(dev))
                     break;
     if (!dev)
             return -EINVAL;
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct file

*filep,

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
     if (xcp_id >= 0)
             args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
     else
-           args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+           args->gpu_id = dev->id;
Quoted 8 lines
Quoted 7 lines

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended. Maybe this would preserve the behaviour for that case:

Can you please explain why this could be a issue on a single partition GPU?

What would xcp_id be on a single-partition GPU? If it's < 0, then your patch changes the behaviour. Instead or returning the GPU ID from the GPU where the memory was allocated, it returns some arbitrary GPU that the application has access to.

Regards,
  Felix

Quoted 25 lines

Regards,
Mukul

Quoted 21 lines
       ...
(quoted diff) +1 -1
-     else
+     else if (!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev-

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct file · line 1549

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
 nodes[0]))
               args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
(quoted diff) +2 -0
+     else
+             args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the
exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
     args->flags = flags;
 
     /* Copy metadata buffer to user mode */
maintainer Reviewed-by
#4 lore raw
In reply to Felix Kuehling
Quoted 25 lines · Felix Kuehling
Quoted 23 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct 
 file *filep,
         /* Find a KFD GPU device that supports the get_dmabuf_info query */
       for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-        if (dev)
+        if (dev && !kfd_devcgroup_check_permission(dev))
               break;
       if (!dev)
           return -EINVAL;
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct 
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
 file *filep,
       if (xcp_id >= 0)
           args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;

      else
-        args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id; +        args->gpu_id = dev->id;

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended.

Never mind. I double checked: On single-partition GPUs, bo->xcp_id always seems to be 0. So your code won't change the behaviour here. The patch is

Quoted 18 lines

Maybe this would preserve the behaviour for that case:

    ...
-    else
+    else if (!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev->nodes[0])) args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id; +    else
+        args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
  Felix

Quoted 2 lines

      args->flags = flags;
        /* Copy metadata buffer to user mode */

#5 lore raw
Quoted 35 lines · Felix Kuehling
Quoted 25 lines · Felix Kuehling
Quoted 23 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct 
 file *filep,
         /* Find a KFD GPU device that supports the get_dmabuf_info query */
       for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
-        if (dev)
+        if (dev && !kfd_devcgroup_check_permission(dev))
               break;
       if (!dev)
           return -EINVAL;
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct 
Quoted 8 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
 file *filep,
       if (xcp_id >= 0)
           args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;

      else
-        args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id; +        args->gpu_id = dev->id;

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended.

Quoted 1 line

Never mind. I double checked: On single-partition GPUs, bo->xcp_id always seems to be 0. So your code won't change the behaviour here. The patch is

should bo->xcp_id be >= 0 for all cases? if yes then I think all tests can be moved. (like below) args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;

David

Quoted 23 lines
Quoted 18 lines

Maybe this would preserve the behaviour for that case:

    ...
-    else
+    else if (!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev->nodes[0])) args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id; +    else
+        args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
  Felix

Quoted 2 lines

      args->flags = flags;
        /* Copy metadata buffer to user mode */

#6 lore raw

[AMD Official Use Only - General]

Quoted 52 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Wu, David
Sent: Wednesday, March 20, 2024 8:02 PM
To: [email protected]
Subject: Re: [PATCH] drm/amdkfd: Check cgroup when returning DMABuf info

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 40 lines · Felix Kuehling
Quoted 38 lines · Felix Kuehling
Quoted 24 lines · Mukul Joshi

Check cgroup permissions when returning DMA-buf info and based on cgroup check return the id of the GPU that has access to the BO.

---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index dfa8c69532d4..f9631f4b1a02 100644
@@ -1523,7 +1523,7 @@ static int kfd_ioctl_get_dmabuf_info(struct
 file *filep,
        /* Find a KFD GPU device that supports the get_dmabuf_info

query */

      for (i = 0; kfd_topology_enum_kfd_devices(i, &dev) == 0; i++)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
-        if (dev)
+        if (dev && !kfd_devcgroup_check_permission(dev))
              break;
      if (!dev)
          return -EINVAL;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_get_dmabuf_info(struct · line 1549

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -1545,7 +1545,7 @@ static int kfd_ioctl_get_dmabuf_info(struct
 file *filep,
      if (xcp_id >= 0)
          args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;
      else
-        args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
+        args->gpu_id = dev->id;

If I remember correctly, this was meant as a fallback in case for GTT BOs where the exporting partition wasn't known and the application didn't have access to the first partition. I think the way you wrote this, it could also change the behaviour (report the wrong GPU ID) on single-partition GPUs, which is probably not intended.

Quoted 7 lines
Quoted 3 lines

Never mind. I double checked: On single-partition GPUs, bo->xcp_id always seems to be 0. So your code won't change the behaviour here. The patch is

should bo->xcp_id be >= 0 for all cases? if yes then I think all tests can be moved. (like below)
args->gpu_id = dmabuf_adev->kfd.dev->nodes[xcp_id]->id;

No it can be -1 on GPUs that support spatial partitioning, especially for GTT BOs.

Regards,
Mukul

Quoted 27 lines

David

Quoted 26 lines
Quoted 21 lines

Maybe this would preserve the behaviour for that case:

    ...
(quoted diff) +1 -1
-    else
+    else if

(!kfd_devcgroup_check_permission(dmabuf_adev->kfd.dev->nodes[0]))

         args->gpu_id = dmabuf_adev->kfd.dev->nodes[0]->id;
(quoted diff) +2 -0
+    else
+        args->gpu_id = dev->id;

Or maybe a more general solution would make DMABuf import work when the exporter is really unknown or not even a GPU. This came up not so long ago in the context of interop with 3rd-party devices. This may require user mode changes as well.

Regards,
Felix

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
      args->flags = flags;
        /* Copy metadata buffer to user mode */