Skip to content

amdkfd: Implement kfd multiple contexts

Zhu Lingshan amd-gfx 8 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest18 patches 0 repliesNew
  2. v618 patches 4 repliesSuperseded
  3. v518 patches 6 repliesSuperseded
Earlier versions · 5
VersionPostedPatchesRepliesStatus
v4 posting 2/20 of 18 patches2 repliesSuperseded
v4 posting 1/218 patches9 repliesSuperseded
v313 patches7 repliesSuperseded
v210 patches4 repliesSuperseded
v19 patches5 repliesSuperseded

Changes in v7 from the cover letter

1) Minor improvements in ioctl AMDKFD_IOC_CREATE_PROCESS

Patches in v7 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/18amdkfd: enlarge the hashtable of kfd_processdrivers/gpu/drm/amd/amdkfd/kfd_priv.h+1 -10 replies
2/18amdkfd: mark the first kfd_process as the primary onedrivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+17 -80 replies
3/18amdkfd: find_process_by_mm always return the primary contextdrivers/gpu/drm/amd/amdkfd/kfd_process.c+1 -10 replies
4/18amdkfd: Introduce kfd_create_process_sysfs as a separate functiondrivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+42 -250 replies
5/18amdkfd: destroy kfd secondary contexts through fd closedrivers/gpu/drm/amd/amdkfd/kfd_chardev.c drivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+31 -200 replies
6/18amdkfd: process svm ioctl only on the primary kfd processdrivers/gpu/drm/amd/amdkfd/kfd_chardev.c+6 -00 replies
7/18amdkfd: process USERPTR allocation only on the primary kfd processdrivers/gpu/drm/amd/amdkfd/kfd_chardev.c+6 -00 replies
8/18amdkfd: identify a secondary kfd process by its iddrivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+80 -30 replies
9/18amdkfd: find kfd_process by filep->private_data in kfd_mmapdrivers/gpu/drm/amd/amdkfd/kfd_chardev.c+7 -40 replies
10/18amdkfd: remove DIQ supportdrivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c drivers/gpu/drm/amd/amdkfd/kfd_packet_manager_v9.c +3 more+5 -780 replies
11/18amdkfd: process pointer of a HIQ should be NULLdrivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c drivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+0 -200 replies
12/18amdkfd: remove test_kqdrivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c+0 -310 replies
13/18amdkfd: introduce new helper kfd_lookup_process_by_iddrivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c+22 -00 replies
14/18amdkfd: record kfd context id into kfd process_infodrivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c+6 -00 replies
15/18amdkfd: record kfd context id in amdkfd_fencedrivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 more+9 -50 replies
16/18amdkfd: fence handler evict and restore a kfd process by its context iddrivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c drivers/gpu/drm/amd/amdkfd/kfd_device.c+10 -50 replies
17/18amdkfd: process debug trap ioctl only on a primary contextdrivers/gpu/drm/amd/amdkfd/kfd_chardev.c+12 -00 replies
18/18amdkfd: introduce new ioctl AMDKFD_IOC_CREATE_PROCESSdrivers/gpu/drm/amd/amdkfd/kfd_chardev.c drivers/gpu/drm/amd/amdkfd/kfd_priv.h drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 more+53 -40 replies

Reviews trailers per patch

17 of 18 patches have a Reviewed-by or Acked-by.

PatchFelix Kuehling
1/18R-b
2/18R-b
3/18R-b
4/18R-b
5/18R-b
6/18R-b
7/18R-b
8/18R-b
9/18R-b
10/18R-b
11/18R-b
12/18R-b
13/18R-b
14/18R-b
15/18R-b
16/18R-b
17/18R-b
18/18

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v7 thread

Cover letter [PATCH V7 00/18] amdkfd: Implement kfd multiple contexts

Currently kfd manages kfd_process in a one context (kfd_process) per program manner, thus each user space program
only onws one kfd context (kfd_process).

This model works fine for most of the programs, but imperfect for a hypervisor like QEMU. Because all programs in the guest user space share the same only one kfd context, which is problematic, including but not limited to:

As illustrated in Figure 1, all guest user space programs share the same fd of /dev/kfd and the same kfd_process, and the same PASID leading to the same
GPU_VM address space. Therefore the IOVA range of each
guest user space programs are not isolated,
they can attack each other through GPU DMA.

 +----------------------------------------------------------------------------------+
 |                                                                                  |
 |  +-----------+      +-----------+      +------------+      +------------+        |
 |  |           |      |           |      |            |      |            |        |
 |  | Program 1 |      | Program 2 |      | Program 3  |      | Program N  |        |
 |  |           |      |           |      |            |      |            |        |
 |  +----+------+      +--------+--+      +--+---------+      +-----+------+        |
 |       |                      |            |                      |               |
 |       |                      |            |                      |        Guest  |
 |       |                      |            |                      |               |
 +-------+----------------------+------------+----------------------+---------------+
         |                      |            |                      |
         |                      |            |                      |
         |                      |            |                      |
         |                      |            |                      |
         |                   +--+------------+---+                  |
         |                   | file descriptor   |                  |
         +-------------------+ of /dev/kfd       +------------------+
                             | opened by QEMU    |
                             |                   |
                             +---------+---------+                   User Space
                                       |                             QEMU
                                       |
---------------------------------------+-----------------------------------------------------
                                       |                             Kernel Space
                                       |                             KFD Module
                                       |
                              +--------+--------+
                              |                 |
                              |   kfd_process   |<------------------The only one KFD context
                              |                 |
                              +--------+--------+
                                       |
                              +--------+--------+
                              |     PASID       |
                              +--------+--------+
                                       |
                              +--------+--------+
                              |      GPU_VM     |
                              +-----------------+

Fiture 1

This series implements a multiple contexts solution:
- Allow each program to create and hold multiple contexts (kfd processes)
- Each context has its own fd of /dev/kfd and an exclusive kfd_process,
which is a secondary kfd context. So that PASID/GPU VM isolates their IOVA address spaces. Therefore, they can not attack each other through GPU DMA.

The design is illustrated in Figure 2 below:

   +---------------------------------------------------------------------------------------------------------+                    
   |                                                                                                         |                    
   |                                                                                                         |                    
   |                                                                                                         |                    
   |       +----------------------------------------------------------------------------------+              |                    
   |       |                                                                                  |              |                    
   |       | +-----------+      +-----------+     +-----------+    +-----------+              |              |                    
   |       | |           |      |           |     |           |    |           |              |              |                    
   |       | | Program 1 |      | Program 2 |     | Program 3 |    | Program N |              |              |                    
   |       | |           |      |           |     |           |    |           |              |              |                    
   |       | +-----+-----+      +-----+-----+     +-----+-----+    +-----+-----+              |              |                    
   |       |       |                  |                 |                |                    |              |                    
   |       |       |                  |                 |                |        Guest       |              |                    
   |       |       |                  |                 |                |                    |              |                    
   |       +-------+------------------+-----------------+----------------+--------------------+              |                    
   |               |                  |                 |                |                            QEMU   |                    
   |               |                  |                 |                |                                   |                    
   +---------------+------------------+-----------------+----------------+--------------------------+--------+                    
                   |                  |                 |                |                          |                             
                   |                  |                 |                |                          |                             
                   |                  |                 |                |                          |                             
               +---+----+         +---+----+        +---+----+       +---+----+                 +---+-----+                       
               |        |         |        |        |        |       |        |                 | Primary |                       
               |  FD 1  |         |  FD 2  |        |  FD 3  |       |  FD 4  |                 |   FD    |                       
               |        |         |        |        |        |       |        |                 |         |                       
               +---+----+         +---+----+        +---+----+       +----+---+                 +----+----+                       
                   |                  |                 |                 |                          |             User Space     
                   |                  |                 |                 |                          |                            
-------------------+------------------+-----------------+-----------------+--------------------------+----------------------------
                   |                  |                 |                 |                          |             Kernel SPace   
                   |                  |                 |                 |                          |                            
                   |                  |                 |                 |                          |                            
   +--------------------------------------------------------------------------------------------------------------------------+   
   |        +------+------+    +------+------+   +------+------+   +------+------+            +------+------+                 |   
   |        | Secondary   |    | Secondary   |   | Secondary   |   | Secondary   |            |  Primary    |   KFD Module    |   
   |        |kfd_process 1|    |kfd_process 2|   |kfd_process 3|   |kfd_process 4|            | kfd_process |                 |   
   |        |             |    |             |   |             |   |             |            |             |                 |   
   |        +------+------+    +------+------+   +------+------+   +------+------+            +------+------+                 |   
   |               |                  |                 |                 |                          |                        |   
   |        +------+------+    +------+------+   +------+------+   +------+------+            +------+------+                 |   
   |        |   PASID     |    |   PASID     |   |   PASID     |   |   PASID     |            |   PASID     |                 |   
   |        +------+------+    +------+------+   +------+------+   +------+------+            +------+------+                 |   
   |               |                  |                 |                 |                          |                        |   
   |               |                  |                 |                 |                          |                        |   
   |        +------+------+    +------+------+   +------+------+   +------+------+            +------+------+                 |   
   |        |   GPU_VM    |    |   GPU_VM    |   |   GPU_VM    |   |   GPU_VM    |            |   GPU_VM    |                 |   
   |        +-------------+    +-------------+   +-------------+   +-------------+            +-------------+                 |   
   |                                                                                                                          |   
   +--------------------------------------------------------------------------------------------------------------------------+   

Figure 2

The relevant reference user space rocm changes could be found at:
https://github.com/ROCm/rocm-systems/pull/1701
https://github.com/ROCm/rocm-systems/pull/1705

Changes from V6:
1) Minor improvements in ioctl AMDKFD_IOC_CREATE_PROCESS

Changes from V5:
1) change upper limit of ida_alloc_range() to KFD_CONTEXT_ID_PRIMARY - 1
2) in set_debug_trap ioctl, check whether the target kfd_process is a primary one after checking validity of the target pointer
3) in set_debug_trap ioctl, check whether the kfd_process which the ioctl is processed on is a primary one
4) in AMDKFD_IOC_CREATE_PROCESS ioctl, place filep->private_data and related code in the critical zone that protected by kfd_processes_mutex
5) add Felix's reviewed-by tag to the series except patch 8,17 and 18

Changes from V4:
1) rename process_id to context_id in struct kfd_process
2) remove primary flag in struct kfd_process
3) reject set_debug_trap ioctl request when
the target kfd_process is non-primary
4) other than default 0, assign context_id 0xFFFF to the primary kfd process

Zhu Lingshan (18):
amdkfd: enlarge the hashtable of kfd_process
amdkfd: mark the first kfd_process as the primary one
amdkfd: find_process_by_mm always return the primary context
amdkfd: Introduce kfd_create_process_sysfs as a separate function
amdkfd: destroy kfd secondary contexts through fd close
amdkfd: process svm ioctl only on the primary kfd process
amdkfd: process USERPTR allocation only on the primary kfd process
amdkfd: identify a secondary kfd process by its id
amdkfd: find kfd_process by filep->private_data in kfd_mmap
amdkfd: remove DIQ support
amdkfd: process pointer of a HIQ should be NULL
amdkfd: remove test_kq
amdkfd: introduce new helper kfd_lookup_process_by_id
amdkfd: record kfd context id into kfd process_info
amdkfd: record kfd context id in amdkfd_fence
amdkfd: fence handler evict and restore a kfd process by its context
id
amdkfd: process debug trap ioctl only on a primary context
amdkfd: introduce new ioctl AMDKFD_IOC_CREATE_PROCESS

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |   8 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c  |  10 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |   8 +-
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  89 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c       |   7 +-
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |   6 +-
 drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c |  61 +----
 .../drm/amd/amdkfd/kfd_packet_manager_v9.c    |   4 -
 .../drm/amd/amdkfd/kfd_packet_manager_vi.c    |   4 -
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  18 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      | 239 +++++++++++++-----
 .../amd/amdkfd/kfd_process_queue_manager.c    |  39 +--
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c          |   2 +-
 include/uapi/linux/kfd_ioctl.h                |   8 +-
 14 files changed, 303 insertions(+), 200 deletions(-)
Signature · 1 line
2.51.0