Skip to contentSkip to conversation

Add CRIU support for amdgpu dmabuf

PATCH Superseded David Francis 5 participants 9 messages May 21, 2025 – Jun 5, 2025

Subsystems (4)

v5 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 Add CRIU support for amdgpu dmabuf

(previous patches were incorrectly called v3 but were actually the 4th version)

This patch series adds support for CRIU checkpointing of processes that share memory with the amdgpu dmabuf interface.

In this v5, the drm interfaces have been changed from creating buffer objects with specified gem handles to changing the gem handle of an existing buffer object. This new ioctl (AMDGPU_GEM_CHANGE_HANDLE) is in drm_gem.c

In the accompanying CRIU patch set, the sockets used by CRIU to exchange dmabuf fds between restoring processes have been refactored to be created within the amdgpu plugin itself.

#1 lore raw

PATCH 1/3 drm: Add DRM prime interfaces to reassign GEM handle

CRIU restore of drm buffer objects requires the ability to create or import a buffer object with a specific gem handle.

Add new drm ioctl DRM_IOCTL_GEM_CHANGE_HANDLE, which takes the gem handle of an object and moves that object to a specified new gem handle.

This ioctl needs to call drm_prime_remove_buf_handle, but that function acquires the prime lock, which the ioctl needs to hold for other purposes.

Make drm_prime_remove_buf_handle not acquire the prime lock, and change its other caller to reflect this.

---
 drivers/gpu/drm/drm_gem.c      | 52 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  4 +++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 drivers/gpu/drm/drm_prime.c    |  6 +---
 include/uapi/drm/drm.h         | 17 +++++++++++
 5 files changed, 75 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_gem.c +52 -0
index c6240bab3fa5..d388bbb7a9de 100644
@@ -282,7 +282,12 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)
 	if (obj->funcs->close)
 		obj->funcs->close(obj, file_priv);
 
+	mutex_lock(&file_priv->prime.lock);
+
 	drm_prime_remove_buf_handle(&file_priv->prime, id);
+
+	mutex_unlock(&file_priv->prime.lock);
+
 	drm_vma_node_revoke(&obj->vma_node, file_priv);
 
 	drm_gem_object_handle_put_unlocked(obj);
@@ -888,6 +893,53 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 	return ret;
 }
 
+/**
+ * drm_gem_open_ioctl - implementation of the GEM_CHANGE_HANDLE ioctl
+ * @dev: drm_device
+ * @data: ioctl data
+ * @file_priv: drm file-private structure
+ *
+ * find the object at the specified gem handle. Remove it from that handle, and assign it
+ * the specified new handle.
+ */
+int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
+				struct drm_file *file_priv)
+{
+	struct drm_gem_change_handle *args = data;
+	struct drm_gem_object *obj;
+	int ret;
+
+	obj = drm_gem_object_lookup(file_priv, args->handle);
+	if (!obj)
+		return -ENOENT;
+
+	if (args->handle == args->new_handle)
+		return 0;
+
+	get_dma_buf(obj->dma_buf);
+	mutex_lock(&file_priv->prime.lock);
+	spin_lock(&file_priv->table_lock);
+
+	ret = idr_alloc(&file_priv->object_idr, obj, args->new_handle, args->new_handle + 1, GFP_NOWAIT);
+	if (ret < 0)
+		goto out_unlock;
+
+	ret = drm_prime_add_buf_handle(&file_priv->prime, obj->dma_buf, args->new_handle);
+	if (ret < 0)
+		goto out_unlock;
+
+	drm_prime_remove_buf_handle(&file_priv->prime, args->handle);
+
+	idr_remove(&file_priv->object_idr, args->handle);
+
+out_unlock:
+	spin_unlock(&file_priv->table_lock);
+	mutex_unlock(&file_priv->prime.lock);
+	dma_buf_put(obj->dma_buf);
+
+	return ret;
+}
+
 /**
  * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl
  * @dev: drm_device
drivers/gpu/drm/drm_internal.h +4 -0
index b2b6a8e49dda..e9d5cdf7e033 100644
@@ -85,6 +85,8 @@ int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
 
 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
+int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
+			     struct dma_buf *dma_buf, uint32_t handle);
 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 				 uint32_t handle);
 
@@ -168,6 +170,8 @@ int drm_gem_close_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file_priv);
 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file_priv);
+int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
+				struct drm_file *file_priv);
 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
 		       struct drm_file *file_priv);
 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
drivers/gpu/drm/drm_ioctl.c +1 -0
index f593dc569d31..d8a24875a7ba 100644
@@ -653,6 +653,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
+	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CHANGE_HANDLE, drm_gem_change_handle_ioctl, DRM_RENDER_ALLOW),
 
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
 
drivers/gpu/drm/drm_prime.c +1 -5
index bdb51c8f262e..1f2e858e5000 100644
@@ -93,7 +93,7 @@ struct drm_prime_member {
 	struct rb_node handle_rb;
 };
 
-static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
+int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
 				    struct dma_buf *dma_buf, uint32_t handle)
 {
 	struct drm_prime_member *member;
@@ -190,8 +190,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 {
 	struct rb_node *rb;
 
-	mutex_lock(&prime_fpriv->lock);
-
 	rb = prime_fpriv->handles.rb_node;
 	while (rb) {
 		struct drm_prime_member *member;
@@ -210,8 +208,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 			rb = rb->rb_left;
 		}
 	}
-
-	mutex_unlock(&prime_fpriv->lock);
 }
 
 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
include/uapi/drm/drm.h +17 -0
index 7fba37b94401..84c819c171d2 100644
@@ -625,6 +625,15 @@ struct drm_gem_open {
 	__u64 size;
 };
 
+/* DRM_IOCTL_GEM_CHANGE_HANDLE ioctl argument type */
+struct drm_gem_change_handle {
+	/** Current handle of object */
+	__u32 handle;
+
+	/** Handle to change that object to */
+	__u32 new_handle;
+};
+
 /**
  * DRM_CAP_DUMB_BUFFER
  *
@@ -1305,6 +1314,14 @@ extern "C" {
  */
 #define DRM_IOCTL_SET_CLIENT_NAME	DRM_IOWR(0xD1, struct drm_set_client_name)
 
+/**
+ * DRM_IOCTL_GEM_CHANGE_HANDLE - Move an object to a different handle
+ *
+ * Some applications (notably CRIU) need objects to have specific gem handles.
+ * This ioctl changes the object at one gem handle to use a new gem handle.
+ */
+#define DRM_IOCTL_GEM_CHANGE_HANDLE    DRM_IOWR(0xD2, struct drm_gem_change_handle)
+
 /*
  * Device specific ioctls should only be in their respective headers
  * The device specific ioctl range is from 0x40 to 0x9f.
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 1/3] drm: Add DRM prime interfaces to reassign GEM handle

Quoted 58 lines · David Francis

CRIU restore of drm buffer objects requires the ability to create or import a buffer object with a specific gem handle.

Add new drm ioctl DRM_IOCTL_GEM_CHANGE_HANDLE, which takes the gem handle of an object and moves that object to a specified new gem handle.

This ioctl needs to call drm_prime_remove_buf_handle, but that function acquires the prime lock, which the ioctl needs to hold for other purposes.

Make drm_prime_remove_buf_handle not acquire the prime lock, and change its other caller to reflect this.

---
 drivers/gpu/drm/drm_gem.c      | 52 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_internal.h |  4 +++
 drivers/gpu/drm/drm_ioctl.c    |  1 +
 drivers/gpu/drm/drm_prime.c    |  6 +---
 include/uapi/drm/drm.h         | 17 +++++++++++
 5 files changed, 75 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_gem.c +19 -0
index c6240bab3fa5..d388bbb7a9de 100644
@@ -282,7 +282,12 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)
 	if (obj->funcs->close)
 		obj->funcs->close(obj, file_priv);
 
+	mutex_lock(&file_priv->prime.lock);
+
 	drm_prime_remove_buf_handle(&file_priv->prime, id);
+
+	mutex_unlock(&file_priv->prime.lock);
+
 	drm_vma_node_revoke(&obj->vma_node, file_priv);
 
 	drm_gem_object_handle_put_unlocked(obj);
@@ -888,6 +893,53 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 	return ret;
 }
 
+/**
+ * drm_gem_open_ioctl - implementation of the GEM_CHANGE_HANDLE ioctl
+ * @dev: drm_device
+ * @data: ioctl data
+ * @file_priv: drm file-private structure
+ *
+ * find the object at the specified gem handle. Remove it from that handle, and assign it
+ * the specified new handle.
+ */
+int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
+				struct drm_file *file_priv)
+{
+	struct drm_gem_change_handle *args = data;
+	struct drm_gem_object *obj;

drivers/gpu/drm/drm_gem.c drm_gem_flink_ioctl(struct drm_device *dev, void *data, · line 919

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +10 -0
+	int ret;
+
+	obj = drm_gem_object_lookup(file_priv, args->handle);
+	if (!obj)
+		return -ENOENT;
+
+	if (args->handle == args->new_handle)
+		return 0;
+
+	get_dma_buf(obj->dma_buf);

That is unecessary now that the new handle is made valid before the old one is removed.

drivers/gpu/drm/drm_gem.c drm_gem_flink_ioctl(struct drm_device *dev, void *data, · line 927

Quoted 9 lines
drivers/gpu/drm/drm_gem.c +8 -0
+	mutex_lock(&file_priv->prime.lock);
+	spin_lock(&file_priv->table_lock);
+
+	ret = idr_alloc(&file_priv->object_idr, obj, args->new_handle, args->new_handle + 1, GFP_NOWAIT);
+	if (ret < 0)
+		goto out_unlock;
+
 
+	ret = drm_prime_add_buf_handle(&file_priv->prime, obj->dma_buf, args->new_handle);

That function allocates memory and so can't easily be called in atomic context. In other words you need to drop the file_priv->table_lock spinlock before calling it.

drivers/gpu/drm/drm_gem.c drm_gem_flink_ioctl(struct drm_device *dev, void *data, · line 933

Quoted 7 lines
drivers/gpu/drm/drm_gem.c +6 -0
+	if (ret < 0)
+		goto out_unlock;
+
+	drm_prime_remove_buf_handle(&file_priv->prime, args->handle);
+
 
+	idr_remove(&file_priv->object_idr, args->handle);

Then re-acquire the spinlock before calling this here.

Regards,
Christian.

Quoted 112 lines
drivers/gpu/drm/drm_gem.c +9 -0
+
+out_unlock:
+	spin_unlock(&file_priv->table_lock);
+	mutex_unlock(&file_priv->prime.lock);
+	dma_buf_put(obj->dma_buf);
+
+	return ret;
+}
+
 /**

* drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl
* @dev: drm_device

drivers/gpu/drm/drm_internal.h +4 -0
index b2b6a8e49dda..e9d5cdf7e033 100644
@@ -85,6 +85,8 @@ int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
 
 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
+int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
+			     struct dma_buf *dma_buf, uint32_t handle);
 void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 				 uint32_t handle);
 
@@ -168,6 +170,8 @@ int drm_gem_close_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file_priv);
 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *file_priv);
+int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
+				struct drm_file *file_priv);
 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
 		       struct drm_file *file_priv);
 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
drivers/gpu/drm/drm_ioctl.c +1 -0
index f593dc569d31..d8a24875a7ba 100644
@@ -653,6 +653,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
 	DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
+	DRM_IOCTL_DEF(DRM_IOCTL_GEM_CHANGE_HANDLE, drm_gem_change_handle_ioctl, DRM_RENDER_ALLOW),
 
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
 
drivers/gpu/drm/drm_prime.c +1 -5
index bdb51c8f262e..1f2e858e5000 100644
@@ -93,7 +93,7 @@ struct drm_prime_member {
 	struct rb_node handle_rb;
 };
 
-static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
+int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
 				    struct dma_buf *dma_buf, uint32_t handle)
 {
 	struct drm_prime_member *member;
@@ -190,8 +190,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 {
 	struct rb_node *rb;
 
-	mutex_lock(&prime_fpriv->lock);
-
 	rb = prime_fpriv->handles.rb_node;
 	while (rb) {
 		struct drm_prime_member *member;
@@ -210,8 +208,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
 			rb = rb->rb_left;
 		}
 	}
-
-	mutex_unlock(&prime_fpriv->lock);
 }
 
 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
include/uapi/drm/drm.h +17 -0
index 7fba37b94401..84c819c171d2 100644
@@ -625,6 +625,15 @@ struct drm_gem_open {
 	__u64 size;
 };
 
+/* DRM_IOCTL_GEM_CHANGE_HANDLE ioctl argument type */
+struct drm_gem_change_handle {
+	/** Current handle of object */
+	__u32 handle;
+
+	/** Handle to change that object to */
+	__u32 new_handle;
+};
+
 /**
  * DRM_CAP_DUMB_BUFFER
  *
@@ -1305,6 +1314,14 @@ extern "C" {
  */
 #define DRM_IOCTL_SET_CLIENT_NAME	DRM_IOWR(0xD1, struct drm_set_client_name)
 
+/**
+ * DRM_IOCTL_GEM_CHANGE_HANDLE - Move an object to a different handle
+ *
+ * Some applications (notably CRIU) need objects to have specific gem handles.
+ * This ioctl changes the object at one gem handle to use a new gem handle.
+ */
+#define DRM_IOCTL_GEM_CHANGE_HANDLE    DRM_IOWR(0xD2, struct drm_gem_change_handle)
+
 /*
  * Device specific ioctls should only be in their respective headers
  * The device specific ioctl range is from 0x40 to 0x9f.
#3 lore raw

PATCH 2/3 drm/amdgpu: Adding amdgpu CRIU ioctl

In reply to David Francis

amdgpu CRIU requires an amdgpu CRIU ioctl. This ioctl has a similar interface to the amdkfd CRIU ioctl.

The objects that can be checkpointed and restored are bos and vm mappings. Because a single amdgpu bo can have multiple mappings. the mappings are recorded separately.

The ioctl has two modes: PROCESS_INFO, which sends to the user how many bos and vms to expect, and CHECKPOINT, which copies data about bos and vms into user-provided buffers.

Restore is handled using existing amdgpu and drm ioctls.

The new ioctl lives in a new file amdgpu_criu.c with its own header amdgpu_criu.h

---
 drivers/gpu/drm/amd/amdgpu/Makefile      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c | 247 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h |  35 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c  |   2 +
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c |   2 +
 include/uapi/drm/amdgpu_drm.h            |  45 +++++
 6 files changed, 332 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 87080c06e5fc..0863edcdd03f 100644
@@ -63,7 +63,7 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_xgmi.o amdgpu_csa.o amdgpu_ras.o amdgpu_vm_cpu.o \
 	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
 	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
-	amdgpu_fw_attestation.o amdgpu_securedisplay.o \
+	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_criu.o \
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_aca.o amdgpu_dev_coredump.o \
 	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +247 -0

247 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h new file +35 -0
new file mode 100644
index 000000000000..01677072f0ed
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+* Copyright 2025 Advanced Micro Devices, Inc.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef __AMDGPU_CRIU_H__
+#define __AMDGPU_CRIU_H__
+
+#include <drm/amdgpu_drm.h>
+
+int amdgpu_criu_op_ioctl(struct drm_device *dev, void *data,
+			    struct drm_file *filp);
+
+struct amdgpu_bo *bo_from_criu_global_handle(uint8_t *handle);
+int insert_bo_at_criu_global_handle(struct amdgpu_bo *bo, uint8_t *handle);
+
+#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index 4db92e0a60da..1b8154395615 100644
@@ -53,6 +53,7 @@
 #include "amdgpu_xgmi.h"
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
+#include "amdgpu_criu.h"
 #include "../amdxcp/amdgpu_xcp_drv.h"
 
 /*
@@ -3021,6 +3022,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ, amdgpu_userq_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_SIGNAL, amdgpu_userq_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_CRIU_OP, amdgpu_criu_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
index a2149afa5803..a8cf2d4580cc 100644
@@ -45,6 +45,8 @@
 #include "amdgpu_dma_buf.h"
 #include "kfd_debug.h"
 
+#include "amdgpu_criu.h"
+
 static long kfd_ioctl(struct file *, unsigned int, unsigned long);
 static int kfd_open(struct inode *, struct file *);
 static int kfd_release(struct inode *, struct file *);
include/uapi/drm/amdgpu_drm.h +45 -0

63 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH 2/3] drm/amdgpu: Adding amdgpu CRIU ioctl

Quoted 87 lines · David Francis

amdgpu CRIU requires an amdgpu CRIU ioctl. This ioctl has a similar interface to the amdkfd CRIU ioctl.

The objects that can be checkpointed and restored are bos and vm mappings. Because a single amdgpu bo can have multiple mappings. the mappings are recorded separately.

The ioctl has two modes: PROCESS_INFO, which sends to the user how many bos and vms to expect, and CHECKPOINT, which copies data about bos and vms into user-provided buffers.

Restore is handled using existing amdgpu and drm ioctls.

The new ioctl lives in a new file amdgpu_criu.c with its own header amdgpu_criu.h

---
 drivers/gpu/drm/amd/amdgpu/Makefile      |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c | 247 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h |  35 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c  |   2 +
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c |   2 +
 include/uapi/drm/amdgpu_drm.h            |  45 +++++
 6 files changed, 332 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 87080c06e5fc..0863edcdd03f 100644
@@ -63,7 +63,7 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_xgmi.o amdgpu_csa.o amdgpu_ras.o amdgpu_vm_cpu.o \
 	amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.o \
 	amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
-	amdgpu_fw_attestation.o amdgpu_securedisplay.o \
+	amdgpu_fw_attestation.o amdgpu_securedisplay.o amdgpu_criu.o \
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_aca.o amdgpu_dev_coredump.o \
 	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +40 -0
new file mode 100644
index 000000000000..dbd2d5049eb6
@@ -0,0 +1,247 @@
+/* SPDX-License-Identifier: MIT */
+/*
+* Copyright 2025 Advanced Micro Devices, Inc.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <linux/dma-buf.h>
+#include <linux/hashtable.h>
+#include <linux/mutex.h>
+#include <linux/random.h>
+
+#include <drm/amdgpu_drm.h>
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+
+#include "amdgpu_criu.h"
+
+#include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_exec.h>
+#include <drm/drm_gem_ttm_helper.h>
+#include <drm/ttm/ttm_tt.h>
+

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 50

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +10 -0
+#include "amdgpu.h"
+#include "amdgpu_display.h"
+#include "amdgpu_dma_buf.h"
+#include "amdgpu_hmm.h"
+#include "amdgpu_xgmi.h"
+
+static bool is_import(struct amdgpu_bo *bo)
+{
+	if (bo->tbo.base.import_attach)
+		return &bo->tbo.base != (struct drm_gem_object *)bo->tbo.base.import_attach->dmabuf->priv;

Please only check import_attach here, the other check is not really valid.

And BTW please make sure to use checkpatch.pl on those patches. That looks like something which might be complained about.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 61

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +11 -0
+	return false;
+}
+
+static int amdgpu_criu_process_info(struct drm_device *dev, struct drm_file *data,
+			    struct drm_amdgpu_criu_args *args)
+{
+	struct drm_gem_object *gobj;
+	int id;
+	int num_bos = 0;
+	int num_vm_mappings = 0;
+	struct amdgpu_vm *avm = &((struct amdgpu_fpriv *)data->driver_priv)->vm;

Please sort that in reverse xmas tree order.

E.g. longer lines first, smaller last.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 72

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +11 -0
+
+	spin_lock(&data->table_lock);
+	idr_for_each_entry(&data->object_idr, gobj, id) {
+		struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
+		struct amdgpu_vm_bo_base *vm_bo_base;
+
+		num_bos += 1;
+
+		vm_bo_base = bo->vm_bo;
+
+		while (vm_bo_base) {

You are not even remotely holding the necessary locks to do this. You need to grab the VM lock for that.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 76

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +4 -0
+			struct amdgpu_bo_va *bo_va = container_of(vm_bo_base, struct amdgpu_bo_va, base);
 
 
+			struct amdgpu_bo_va_mapping *mapping;
+
+			if (vm_bo_base->vm == avm) {

In general please don't mess with code in a different component. E.g. for that here you should probably use amdgpu_vm_bo_find() instead.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 83

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +7 -0
+
+				list_for_each_entry(mapping, &bo_va->invalids, list) {
+					num_vm_mappings += 1;
+				}
+				list_for_each_entry(mapping, &bo_va->valids, list) {
+					num_vm_mappings += 1;
+				}

Using the BO state machine is a very bad idea, probably better to walk over the VA mapping tree.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 92

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +9 -0
+			}
+
+			vm_bo_base = vm_bo_base->next;
+		}
+	}
+	spin_unlock(&data->table_lock);
+
+	args->num_bos = num_bos;
+	args->num_vms = num_vm_mappings;

Those informations become invalid as soon as you drop the looks. Stuff like that is usually not an acceptable design without explicit documentation.

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 93

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +1 -0
+	args->pid = avm->task_info->pid;

This is not something userspace should know about.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +8 -0
+
+	return 0;
+}
+
+static uint32_t hardware_flags_to_uapi_flags(struct amdgpu_device *adev, uint64_t pte_flags)
+{
+	uint32_t gem_flags = 0;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 111

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +10 -0
+	if (pte_flags & AMDGPU_PTE_EXECUTABLE)
+		gem_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
+	if (pte_flags & AMDGPU_PTE_READABLE)
+		gem_flags |= AMDGPU_VM_PAGE_READABLE;
+	if (pte_flags & AMDGPU_PTE_WRITEABLE)
+		gem_flags |= AMDGPU_VM_PAGE_WRITEABLE;
+	if (pte_flags & AMDGPU_PTE_PRT_FLAG(adev))
+		gem_flags |= AMDGPU_VM_PAGE_PRT;
+	if (pte_flags & AMDGPU_PTE_NOALLOC)
+		gem_flags |= AMDGPU_VM_PAGE_NOALLOC;

That won't work like this in some cases.

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +16 -0
+
+	return gem_flags;
+}
+
+static int amdgpu_criu_checkpoint(struct drm_device *dev, struct drm_file *data,
+			    struct drm_amdgpu_criu_args *args)
+{
+
+	struct amdgpu_vm *avm = &((struct amdgpu_fpriv *)data->driver_priv)->vm;
+	struct drm_amdgpu_criu_bo_bucket *bo_buckets;
+	struct drm_amdgpu_criu_vm_bucket *vm_buckets;
+	struct drm_gem_object *gobj;
+	int vm_priv_index = 0;
+	int bo_index = 0;
+	int num_bos = 0;
+	int fd, id, ret;

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 137

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +10 -0
+
+	spin_lock(&data->table_lock);
+	idr_for_each_entry(&data->object_idr, gobj, id)
+		num_bos += 1;
+	spin_unlock(&data->table_lock);
+
+	if (args->num_bos != num_bos) {
+		ret = -EINVAL;
+		goto exit;
+	}

That approach is complete nonsense.

The input arguments should just give you the maximum size of the buffer, never an excapt size.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +4 -0
+
+	bo_buckets = kvzalloc(num_bos * sizeof(*bo_buckets), GFP_KERNEL);
+	if (!bo_buckets) {
+		ret = -ENOMEM;

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 151

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +10 -0
+		goto free_buckets;
+	}
+
+	vm_buckets = kvzalloc(args->num_vms * sizeof(*vm_buckets), GFP_KERNEL);
+	if (!vm_buckets) {
+		ret = -ENOMEM;
+		goto free_vms;
+	}
+
+	idr_for_each_entry(&data->object_idr, gobj, id) {

You can't touch that without holding a lock.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +7 -0
+		struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
+		struct drm_amdgpu_criu_bo_bucket *bo_bucket;
+		struct amdgpu_vm_bo_base *vm_bo_base;
+
+		bo_bucket = &bo_buckets[bo_index];
+
+		bo_bucket->size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c line 168

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +10 -0
+		bo_bucket->offset = amdgpu_bo_mmap_offset(bo);
+		bo_bucket->alloc_flags = bo->flags & (!AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE);
+		bo_bucket->preferred_domains = bo->preferred_domains;
+
+		if (is_import(bo))
+			bo_bucket->flags |= AMDGPU_CRIU_BO_FLAG_IS_IMPORT;
+
+		drm_gem_prime_handle_to_fd(dev, data, id, 0, &fd);
+		if (fd)
+			bo_bucket->dmabuf_fd = fd;

Complete no-go to call that here.

Regards,
Christian.

Quoted 223 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c new file +79 -0
+
+		vm_bo_base = bo->vm_bo;
+
+		while (vm_bo_base) {
+			struct amdgpu_bo_va *bo_va = container_of(vm_bo_base, struct amdgpu_bo_va, base);
+			struct amdgpu_bo_va_mapping *mapping;
+
+			if (vm_bo_base->vm == avm) {
+				list_for_each_entry(mapping, &bo_va->invalids, list) {
+					vm_buckets[vm_priv_index].start = mapping->start;
+					vm_buckets[vm_priv_index].last = mapping->last;
+					vm_buckets[vm_priv_index].offset = mapping->offset;
+					vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
+					vm_buckets[vm_priv_index].gem_handle = id;
+					vm_priv_index += 1;
+
+					bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				}
+				list_for_each_entry(mapping, &bo_va->valids, list) {
+					vm_buckets[vm_priv_index].start = mapping->start;
+					vm_buckets[vm_priv_index].last = mapping->last;
+					vm_buckets[vm_priv_index].offset = mapping->offset;
+					vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
+					vm_buckets[vm_priv_index].gem_handle = id;
+					vm_priv_index += 1;
+
+					bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
+				}
+			}
+
+			vm_bo_base = vm_bo_base->next;
+		}
+
+		bo_index += 1;
+	}
+
+	ret = copy_to_user((void __user *)args->bos, bo_buckets, num_bos * sizeof(*bo_buckets));
+	if (ret) {
+		pr_debug("Failed to copy BO information to user\n");
+		ret = -EFAULT;
+		goto free_vms;
+	}
+
+	ret = copy_to_user((void __user *)args->vms, vm_buckets, args->num_vms * sizeof(*vm_buckets));
+	if (ret) {
+		pr_debug("Failed to copy BO information to user\n");
+		ret = -EFAULT;
+		goto free_vms;
+	}
+
+free_vms:
+	kvfree(vm_buckets);
+free_buckets:
+	kvfree(bo_buckets);
+exit:
+
+	return ret;
+}
+
+int amdgpu_criu_op_ioctl(struct drm_device *dev, void *data,
+			    struct drm_file *filp)
+{
+	struct drm_amdgpu_criu_args *args = data;
+	int ret;
+
+	switch (args->op) {
+	case AMDGPU_CRIU_OP_PROCESS_INFO:
+		ret = amdgpu_criu_process_info(dev, filp, args);
+		break;
+	case AMDGPU_CRIU_OP_CHECKPOINT:
+		ret = amdgpu_criu_checkpoint(dev, filp, args);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.h new file +35 -0
new file mode 100644
index 000000000000..01677072f0ed
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: MIT */
+/*
+* Copyright 2025 Advanced Micro Devices, Inc.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#ifndef __AMDGPU_CRIU_H__
+#define __AMDGPU_CRIU_H__
+
+#include <drm/amdgpu_drm.h>
+
+int amdgpu_criu_op_ioctl(struct drm_device *dev, void *data,
+			    struct drm_file *filp);
+
+struct amdgpu_bo *bo_from_criu_global_handle(uint8_t *handle);
+int insert_bo_at_criu_global_handle(struct amdgpu_bo *bo, uint8_t *handle);
+
+#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index 4db92e0a60da..1b8154395615 100644
@@ -53,6 +53,7 @@
 #include "amdgpu_xgmi.h"
 #include "amdgpu_userq.h"
 #include "amdgpu_userq_fence.h"
+#include "amdgpu_criu.h"
 #include "../amdxcp/amdgpu_xcp_drv.h"
 
 /*
@@ -3021,6 +3022,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ, amdgpu_userq_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_SIGNAL, amdgpu_userq_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_CRIU_OP, amdgpu_criu_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
index a2149afa5803..a8cf2d4580cc 100644
@@ -45,6 +45,8 @@
 #include "amdgpu_dma_buf.h"
 #include "kfd_debug.h"
 
+#include "amdgpu_criu.h"
+
 static long kfd_ioctl(struct file *, unsigned int, unsigned long);
 static int kfd_open(struct inode *, struct file *);
 static int kfd_release(struct inode *, struct file *);
include/uapi/drm/amdgpu_drm.h +45 -0
index 45c4fa13499c..f7c3b160f396 100644
@@ -57,6 +57,7 @@ extern "C" {
 #define DRM_AMDGPU_USERQ		0x16
 #define DRM_AMDGPU_USERQ_SIGNAL		0x17
 #define DRM_AMDGPU_USERQ_WAIT		0x18
+#define DRM_AMDGPU_CRIU_OP		0x19
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -77,6 +78,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_USERQ		DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ, union drm_amdgpu_userq)
 #define DRM_IOCTL_AMDGPU_USERQ_SIGNAL	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_SIGNAL, struct drm_amdgpu_userq_signal)
 #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
+#define DRM_IOCTL_AMDGPU_CRIU_OP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CRIU_OP, struct drm_amdgpu_criu_args)
 
 /**
  * DOC: memory domains
@@ -1626,6 +1628,49 @@ struct drm_color_ctm_3x4 {
 	__u64 matrix[12];
 };
 
+/* CRIU ioctl
+ *
+ * When checkpointing a process, the CRIU amdgpu plugin will perform:
+ * 1. INFO op to get information about state that needs to be saved. This
+ *    pauses execution until the checkpoint is done.
+ * 2. CHECKPOINT op to save state
+ *
+ * Restore uses other ioctls.
+ */
+enum drm_amdgpu_criu_op {
+	AMDGPU_CRIU_OP_PROCESS_INFO,
+	AMDGPU_CRIU_OP_CHECKPOINT,
+};
+
+struct drm_amdgpu_criu_args {
+	__u64 bos; /* user pointer to bos array */
+	__u64 vms; /* user pointer to private data */
+	__u32 num_bos;
+	__u32 num_vms;
+	__u32 pid;
+	__u32 op;
+};
+
+#define AMDGPU_CRIU_BO_FLAG_IS_IMPORT	(1 << 0)
+
+struct drm_amdgpu_criu_bo_bucket {
+	__u64 addr;
+	__u64 size;
+	__u64 offset;
+	__u64 alloc_flags;
+	__u32 preferred_domains;
+	__u32 dmabuf_fd;
+	__u32 flags;
+};
+
+struct drm_amdgpu_criu_vm_bucket {
+	__u64 start;
+	__u64 last;
+	__u64 offset;
+	__u64 flags;
+	__u32 gem_handle;
+};
+
 #if defined(__cplusplus)
 }
 #endif
#5 lore raw

Re: [PATCH 2/3] drm/amdgpu: Adding amdgpu CRIU ioctl

In reply to David Francis
kernel test robot noticed the following build warnings:

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm/drm-next]
[also build test WARNING on next-20250521]
[cannot apply to drm-exynos/exynos-drm-next linus/master v6.15-rc7] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Francis/drm-amdgpu-Adding-amdgpu-CRIU-ioctl/20250521-230706
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20250521140649.4058526-3-David.Francis%40amd.com
patch subject: [PATCH 2/3] drm/amdgpu: Adding amdgpu CRIU ioctl
config: arm-randconfig-002-20250522 (https://download.01.org/0day-ci/archive/20250522/[email protected]/config)
compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f819f46284f2a79790038e1f6649172789734ae8)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250522/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c:160:42: warning: converting the result of '<<' to a boolean always evaluates to true [-Wtautological-constant-compare]
     160 |                 bo_bucket->alloc_flags = bo->flags & (!AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE);
         |                                                        ^
   include/uapi/drm/amdgpu_drm.h:146:51: note: expanded from macro 'AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE'
     146 | #define AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE  (1 << 9)
         |                                                    ^

1 warning generated.

vim +160 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c

119
120 struct amdgpu_vm *avm = &((struct amdgpu_fpriv *)data->driver_priv)->vm;
121 struct drm_amdgpu_criu_bo_bucket *bo_buckets;
122 struct drm_amdgpu_criu_vm_bucket *vm_buckets;
123 struct drm_gem_object *gobj;
124 int vm_priv_index = 0;
125 int bo_index = 0;
126 int num_bos = 0;
127 int fd, id, ret;
128
129 spin_lock(&data->table_lock);
130 idr_for_each_entry(&data->object_idr, gobj, id)
131 num_bos += 1;
132 spin_unlock(&data->table_lock);
133
134 if (args->num_bos != num_bos) {
135 ret = -EINVAL;
136 goto exit;
137 }
138
139 bo_buckets = kvzalloc(num_bos * sizeof(*bo_buckets), GFP_KERNEL);
140 if (!bo_buckets) {
141 ret = -ENOMEM;
142 goto free_buckets;
143 }
144
145 vm_buckets = kvzalloc(args->num_vms * sizeof(*vm_buckets), GFP_KERNEL);
146 if (!vm_buckets) {
147 ret = -ENOMEM;
148 goto free_vms;
149 }
150
151 idr_for_each_entry(&data->object_idr, gobj, id) {
152 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
153 struct drm_amdgpu_criu_bo_bucket *bo_bucket;
154 struct amdgpu_vm_bo_base *vm_bo_base;
155
156 bo_bucket = &bo_buckets[bo_index];
157
158 bo_bucket->size = amdgpu_bo_size(bo);
159 bo_bucket->offset = amdgpu_bo_mmap_offset(bo);

 > 160			bo_bucket->alloc_flags = bo->flags & (!AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE);

161 bo_bucket->preferred_domains = bo->preferred_domains;
162
163 if (is_import(bo))
164 bo_bucket->flags |= AMDGPU_CRIU_BO_FLAG_IS_IMPORT;
165
166 drm_gem_prime_handle_to_fd(dev, data, id, 0, &fd);
167 if (fd)
168 bo_bucket->dmabuf_fd = fd;
169
170 vm_bo_base = bo->vm_bo;
171
172 while (vm_bo_base) {
173 struct amdgpu_bo_va *bo_va = container_of(vm_bo_base, struct amdgpu_bo_va, base);
174 struct amdgpu_bo_va_mapping *mapping;
175
176 if (vm_bo_base->vm == avm) {
177 list_for_each_entry(mapping, &bo_va->invalids, list) {
178 vm_buckets[vm_priv_index].start = mapping->start;
179 vm_buckets[vm_priv_index].last = mapping->last;
180 vm_buckets[vm_priv_index].offset = mapping->offset;
181 vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
182 vm_buckets[vm_priv_index].gem_handle = id;
183 vm_priv_index += 1;
184
185 bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
186 }
187 list_for_each_entry(mapping, &bo_va->valids, list) {
188 vm_buckets[vm_priv_index].start = mapping->start;
189 vm_buckets[vm_priv_index].last = mapping->last;
190 vm_buckets[vm_priv_index].offset = mapping->offset;
191 vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
192 vm_buckets[vm_priv_index].gem_handle = id;
193 vm_priv_index += 1;
194
195 bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
196 }
197 }
198
199 vm_bo_base = vm_bo_base->next;
200 }
201
202 bo_index += 1;
203 }
204
205 ret = copy_to_user((void __user *)args->bos, bo_buckets, num_bos * sizeof(*bo_buckets));
206 if (ret) {
207 pr_debug("Failed to copy BO information to user\n");
208 ret = -EFAULT;
209 goto free_vms;
210 }
211
212 ret = copy_to_user((void __user *)args->vms, vm_buckets, args->num_vms * sizeof(*vm_buckets));
213 if (ret) {
214 pr_debug("Failed to copy BO information to user\n");
215 ret = -EFAULT;
216 goto free_vms;
217 }
218
219 free_vms:
220 kvfree(vm_buckets);
221 free_buckets:
222 kvfree(bo_buckets);
223 exit:
224
225 return ret;
226 }
227

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
build bot
#6 lore raw

Re: [PATCH 2/3] drm/amdgpu: Adding amdgpu CRIU ioctl

In reply to David Francis
BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: David Francis…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: David Francis <[email protected]>
TO: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
CC: David Francis <[email protected]>

Hi David,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm/drm-next]
[also build test WARNING on next-20250522]
[cannot apply to drm-exynos/exynos-drm-next linus/master drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-tip/drm-tip v6.15-rc7] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Francis/drm-amdgpu-Adding-amdgpu-CRIU-ioctl/20250521-230706
base:   git://anongit.freedesktop.org/drm/drm drm-next
patch link:    https://lore.kernel.org/r/20250521140649.4058526-3-David.Francis%40amd.com
patch subject: [PATCH 2/3] drm/amdgpu: Adding amdgpu CRIU ioctl

:::::: branch date: 32 hours ago
:::::: commit date: 32 hours ago
config: loongarch-randconfig-r073-20250522 (https://download.01.org/0day-ci/archive/20250523/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 15.1.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c:160 amdgpu_criu_checkpoint() warn: potential ! vs ~ typo

vim +160 drivers/gpu/drm/amd/amdgpu/amdgpu_criu.c

d970da5c8545bbc David Francis 2025-05-21  119  
d970da5c8545bbc David Francis 2025-05-21  120  	struct amdgpu_vm *avm = &((struct amdgpu_fpriv *)data->driver_priv)->vm;
d970da5c8545bbc David Francis 2025-05-21  121  	struct drm_amdgpu_criu_bo_bucket *bo_buckets;
d970da5c8545bbc David Francis 2025-05-21  122  	struct drm_amdgpu_criu_vm_bucket *vm_buckets;
d970da5c8545bbc David Francis 2025-05-21  123  	struct drm_gem_object *gobj;
d970da5c8545bbc David Francis 2025-05-21  124  	int vm_priv_index = 0;
d970da5c8545bbc David Francis 2025-05-21  125  	int bo_index = 0;
d970da5c8545bbc David Francis 2025-05-21  126  	int num_bos = 0;
d970da5c8545bbc David Francis 2025-05-21  127  	int fd, id, ret;
d970da5c8545bbc David Francis 2025-05-21  128  
d970da5c8545bbc David Francis 2025-05-21  129  	spin_lock(&data->table_lock);
d970da5c8545bbc David Francis 2025-05-21  130  	idr_for_each_entry(&data->object_idr, gobj, id)
d970da5c8545bbc David Francis 2025-05-21  131  		num_bos += 1;
d970da5c8545bbc David Francis 2025-05-21  132  	spin_unlock(&data->table_lock);
d970da5c8545bbc David Francis 2025-05-21  133  
d970da5c8545bbc David Francis 2025-05-21  134  	if (args->num_bos != num_bos) {
d970da5c8545bbc David Francis 2025-05-21  135  		ret = -EINVAL;
d970da5c8545bbc David Francis 2025-05-21  136  		goto exit;
d970da5c8545bbc David Francis 2025-05-21  137  	}
d970da5c8545bbc David Francis 2025-05-21  138  
d970da5c8545bbc David Francis 2025-05-21  139  	bo_buckets = kvzalloc(num_bos * sizeof(*bo_buckets), GFP_KERNEL);
d970da5c8545bbc David Francis 2025-05-21  140  	if (!bo_buckets) {
d970da5c8545bbc David Francis 2025-05-21  141  		ret = -ENOMEM;
d970da5c8545bbc David Francis 2025-05-21  142  		goto free_buckets;
d970da5c8545bbc David Francis 2025-05-21  143  	}
d970da5c8545bbc David Francis 2025-05-21  144  
d970da5c8545bbc David Francis 2025-05-21  145  	vm_buckets = kvzalloc(args->num_vms * sizeof(*vm_buckets), GFP_KERNEL);
d970da5c8545bbc David Francis 2025-05-21  146  	if (!vm_buckets) {
d970da5c8545bbc David Francis 2025-05-21  147  		ret = -ENOMEM;
d970da5c8545bbc David Francis 2025-05-21  148  		goto free_vms;
d970da5c8545bbc David Francis 2025-05-21  149  	}
d970da5c8545bbc David Francis 2025-05-21  150  
d970da5c8545bbc David Francis 2025-05-21  151  	idr_for_each_entry(&data->object_idr, gobj, id) {
d970da5c8545bbc David Francis 2025-05-21  152  		struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
d970da5c8545bbc David Francis 2025-05-21  153  		struct drm_amdgpu_criu_bo_bucket *bo_bucket;
d970da5c8545bbc David Francis 2025-05-21  154  		struct amdgpu_vm_bo_base *vm_bo_base;
d970da5c8545bbc David Francis 2025-05-21  155  
d970da5c8545bbc David Francis 2025-05-21  156  		bo_bucket = &bo_buckets[bo_index];
d970da5c8545bbc David Francis 2025-05-21  157  
d970da5c8545bbc David Francis 2025-05-21  158  		bo_bucket->size = amdgpu_bo_size(bo);
d970da5c8545bbc David Francis 2025-05-21  159  		bo_bucket->offset = amdgpu_bo_mmap_offset(bo);
d970da5c8545bbc David Francis 2025-05-21 @160  		bo_bucket->alloc_flags = bo->flags & (!AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE);
d970da5c8545bbc David Francis 2025-05-21  161  		bo_bucket->preferred_domains = bo->preferred_domains;
d970da5c8545bbc David Francis 2025-05-21  162  
d970da5c8545bbc David Francis 2025-05-21  163  		if (is_import(bo))
d970da5c8545bbc David Francis 2025-05-21  164  			bo_bucket->flags |= AMDGPU_CRIU_BO_FLAG_IS_IMPORT;
d970da5c8545bbc David Francis 2025-05-21  165  
d970da5c8545bbc David Francis 2025-05-21  166  		drm_gem_prime_handle_to_fd(dev, data, id, 0, &fd);
d970da5c8545bbc David Francis 2025-05-21  167  		if (fd)
d970da5c8545bbc David Francis 2025-05-21  168  			bo_bucket->dmabuf_fd = fd;
d970da5c8545bbc David Francis 2025-05-21  169  
d970da5c8545bbc David Francis 2025-05-21  170  		vm_bo_base = bo->vm_bo;
d970da5c8545bbc David Francis 2025-05-21  171  
d970da5c8545bbc David Francis 2025-05-21  172  		while (vm_bo_base) {
d970da5c8545bbc David Francis 2025-05-21  173  			struct amdgpu_bo_va *bo_va = container_of(vm_bo_base, struct amdgpu_bo_va, base);
d970da5c8545bbc David Francis 2025-05-21  174  			struct amdgpu_bo_va_mapping *mapping;
d970da5c8545bbc David Francis 2025-05-21  175  
d970da5c8545bbc David Francis 2025-05-21  176  			if (vm_bo_base->vm == avm) {
d970da5c8545bbc David Francis 2025-05-21  177  				list_for_each_entry(mapping, &bo_va->invalids, list) {
d970da5c8545bbc David Francis 2025-05-21  178  					vm_buckets[vm_priv_index].start = mapping->start;
d970da5c8545bbc David Francis 2025-05-21  179  					vm_buckets[vm_priv_index].last = mapping->last;
d970da5c8545bbc David Francis 2025-05-21  180  					vm_buckets[vm_priv_index].offset = mapping->offset;
d970da5c8545bbc David Francis 2025-05-21  181  					vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
d970da5c8545bbc David Francis 2025-05-21  182  					vm_buckets[vm_priv_index].gem_handle = id;
d970da5c8545bbc David Francis 2025-05-21  183  					vm_priv_index += 1;
d970da5c8545bbc David Francis 2025-05-21  184  
d970da5c8545bbc David Francis 2025-05-21  185  					bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
d970da5c8545bbc David Francis 2025-05-21  186  				}
d970da5c8545bbc David Francis 2025-05-21  187  				list_for_each_entry(mapping, &bo_va->valids, list) {
d970da5c8545bbc David Francis 2025-05-21  188  					vm_buckets[vm_priv_index].start = mapping->start;
d970da5c8545bbc David Francis 2025-05-21  189  					vm_buckets[vm_priv_index].last = mapping->last;
d970da5c8545bbc David Francis 2025-05-21  190  					vm_buckets[vm_priv_index].offset = mapping->offset;
d970da5c8545bbc David Francis 2025-05-21  191  					vm_buckets[vm_priv_index].flags = hardware_flags_to_uapi_flags(drm_to_adev(dev), mapping->flags);
d970da5c8545bbc David Francis 2025-05-21  192  					vm_buckets[vm_priv_index].gem_handle = id;
d970da5c8545bbc David Francis 2025-05-21  193  					vm_priv_index += 1;
d970da5c8545bbc David Francis 2025-05-21  194  
d970da5c8545bbc David Francis 2025-05-21  195  					bo_bucket->addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
d970da5c8545bbc David Francis 2025-05-21  196  				}
d970da5c8545bbc David Francis 2025-05-21  197  			}
d970da5c8545bbc David Francis 2025-05-21  198  
d970da5c8545bbc David Francis 2025-05-21  199  			vm_bo_base = vm_bo_base->next;
d970da5c8545bbc David Francis 2025-05-21  200  		}
d970da5c8545bbc David Francis 2025-05-21  201  
d970da5c8545bbc David Francis 2025-05-21  202  		bo_index += 1;
d970da5c8545bbc David Francis 2025-05-21  203  	}
d970da5c8545bbc David Francis 2025-05-21  204  
d970da5c8545bbc David Francis 2025-05-21  205  	ret = copy_to_user((void __user *)args->bos, bo_buckets, num_bos * sizeof(*bo_buckets));
d970da5c8545bbc David Francis 2025-05-21  206  	if (ret) {
d970da5c8545bbc David Francis 2025-05-21  207  		pr_debug("Failed to copy BO information to user\n");
d970da5c8545bbc David Francis 2025-05-21  208  		ret = -EFAULT;
d970da5c8545bbc David Francis 2025-05-21  209  		goto free_vms;
d970da5c8545bbc David Francis 2025-05-21  210  	}
d970da5c8545bbc David Francis 2025-05-21  211  
d970da5c8545bbc David Francis 2025-05-21  212  	ret = copy_to_user((void __user *)args->vms, vm_buckets, args->num_vms * sizeof(*vm_buckets));
d970da5c8545bbc David Francis 2025-05-21  213  	if (ret) {
d970da5c8545bbc David Francis 2025-05-21  214  		pr_debug("Failed to copy BO information to user\n");
d970da5c8545bbc David Francis 2025-05-21  215  		ret = -EFAULT;
d970da5c8545bbc David Francis 2025-05-21  216  		goto free_vms;
d970da5c8545bbc David Francis 2025-05-21  217  	}
d970da5c8545bbc David Francis 2025-05-21  218  
d970da5c8545bbc David Francis 2025-05-21  219  free_vms:
d970da5c8545bbc David Francis 2025-05-21  220  	kvfree(vm_buckets);
d970da5c8545bbc David Francis 2025-05-21  221  free_buckets:
d970da5c8545bbc David Francis 2025-05-21  222  	kvfree(bo_buckets);
d970da5c8545bbc David Francis 2025-05-21  223  exit:
d970da5c8545bbc David Francis 2025-05-21  224  
d970da5c8545bbc David Francis 2025-05-21  225  	return ret;
d970da5c8545bbc David Francis 2025-05-21  226  }
d970da5c8545bbc David Francis 2025-05-21  227  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

PATCH 3/3 drm/amdgpu: Allow kfd CRIU with no buffer objects

In reply to David Francis

The kfd CRIU checkpoint ioctl would return an error if trying to checkpoint a process with no kfd buffer objects.

This is a normal case and should not be an error.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index a8cf2d4580cc..7aa34bd15958 100644
@@ -2570,7 +2570,7 @@ static int criu_restore(struct file *filep,
 		 args->num_devices, args->num_bos, args->num_objects, args->priv_data_size);
 
 	if (!args->bos || !args->devices || !args->priv_data || !args->priv_data_size ||
-	    !args->num_devices || !args->num_bos)
+	    !args->num_devices)
 		return -EINVAL;
 
 	mutex_lock(&p->mutex);
Signature · 1 line
2.34.1
#8 lore raw

RE: [PATCH 3/3] drm/amdgpu: Allow kfd CRIU with no buffer objects

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 27 lines

-----Original Message-----
From: Francis, David <[email protected]>
Sent: Wednesday, May 21, 2025 10:07 AM
To: [email protected]
Cc: [email protected]; Kuehling, Felix <[email protected]>; Yat Sin, David <[email protected]>; Freehill, Chris <[email protected]>; Koenig, Christian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Francis, David <[email protected]>
Subject: [PATCH 3/3] drm/amdgpu: Allow kfd CRIU with no buffer objects

The kfd CRIU checkpoint ioctl would return an error if trying to checkpoint a process with no kfd buffer objects.

This is a normal case and should not be an error.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index a8cf2d4580cc..7aa34bd15958 100644

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

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -2570,7 +2570,7 @@ static int criu_restore(struct file *filep,
               args->num_devices, args->num_bos, args->num_objects, args-
 >priv_data_size);
 
      if (!args->bos || !args->devices || !args->priv_data || !args->priv_data_size ||
-         !args->num_devices || !args->num_bos)
+         !args->num_devices)
              return -EINVAL;
 

Nit pick: The if statement should also allow args->bos to be NULL.

Regards
David Yat Sin

Quoted 3 lines
      mutex_lock(&p->mutex);

--
2.34.1