Skip to contentSkip to conversation

drm/amdkfd: Add upper bound check for num_of_nodes

PATCH Superseded Alysa Liu 2 participants 2 messages Mar 31

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alysa Liu

author
#0 lore raw

PATCH v3 1/1 drm/amdkfd: Add upper bound check for num_of_nodes

drm/amdkfd: Add upper bound check for num_of_nodes in kfd_ioctl_get_process_apertures_new.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c  | 3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h     | 1 +
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 5 +++++
 3 files changed, 9 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3 -0
index 1db565442c48..5435419a7bd9 100644
@@ -776,6 +776,9 @@ static int kfd_ioctl_get_process_apertures_new(struct file *filp,
 		goto out_unlock;
 	}
 
+	if (args->num_of_nodes > kfd_topology_get_num_devices())
+		return -EINVAL;
+
 	/* Fill in process-aperture information for all available
 	 * nodes, but not more than args->num_of_nodes as that is
 	 * the amount of memory allocated by user
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index e7a8f3e17872..af7b687d729c 100644
@@ -1191,6 +1191,7 @@ static inline struct kfd_node *kfd_node_by_irq_ids(struct amdgpu_device *adev,
 	return NULL;
 }
 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev);
+uint32_t kfd_topology_get_num_devices(void);
 int kfd_numa_node_to_apic_id(int numa_node_id);
 uint32_t kfd_gpu_node_num(void);
 
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +5 -0
index 1ccd4514d3ee..453b3577ea0e 100644
@@ -2297,6 +2297,11 @@ int kfd_topology_remove_device(struct kfd_node *gpu)
 	return res;
 }
 
+uint32_t kfd_topology_get_num_devices(void)
+{
+	return sys_props.num_devices;
+}
+
 /* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
  *	topology. If GPU device is found @idx, then valid kfd_dev pointer is
  *	returned through @kdev
Signature · 1 line
2.34.1
#1 lore raw

[Public]

One comment is below.
With that fixed, this patch is Reviewed-by: Harish Kasiviswanathan <[email protected]

________________________________
From: Liu, Alysa <[email protected]>
Sent: Tuesday, March 31, 2026 10:51 AM
To: [email protected] <[email protected]>
Cc: Kasiviswanathan, Harish <[email protected]>; Liu, Alysa <[email protected]>
Subject: [PATCH v3] drm/amdkfd: Add upper bound check for num_of_nodes

drm/amdkfd: Add upper bound check for num_of_nodes in kfd_ioctl_get_process_apertures_new.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c  | 3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h     | 1 +
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c | 5 +++++
 3 files changed, 9 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3 -0
index 1db565442c48..5435419a7bd9 100644
@@ -776,6 +776,9 @@ static int kfd_ioctl_get_process_apertures_new(struct file *filp,
                 goto out_unlock;
         }
 
+       if (args->num_of_nodes > kfd_topology_get_num_devices())
+               return -EINVAL;
+
         /* Fill in process-aperture information for all available
          * nodes, but not more than args->num_of_nodes as that is
          * the amount of memory allocated by user
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index e7a8f3e17872..af7b687d729c 100644
@@ -1191,6 +1191,7 @@ static inline struct kfd_node *kfd_node_by_irq_ids(struct amdgpu_device *adev,
         return NULL;
 }
 int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev);
+uint32_t kfd_topology_get_num_devices(void);
 int kfd_numa_node_to_apic_id(int numa_node_id);
 uint32_t kfd_gpu_node_num(void);
 
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +2 -0
index 1ccd4514d3ee..453b3577ea0e 100644
@@ -2297,6 +2297,11 @@ int kfd_topology_remove_device(struct kfd_node *gpu)
         return res;
 }
 
+uint32_t kfd_topology_get_num_devices(void)
+{
 

It is safer to have a read lock here

down_read(&topology_lock);

+ return sys_props.num_devices;

up_read(&topology_lock);

+}
+
/* kfd_topology_enum_kfd_devices - Enumerate through all devices in KFD
* topology. If GPU device is found @idx, then valid kfd_dev pointer is
* returned through @kdev
--
2.34.1