All of lore.kernel.org
 help / color / mirror / Atom feed
From: <eagostini@nvidia.com>
To: <dev@dpdk.org>
Cc: Elena Agostini <eagostini@nvidia.com>
Subject: [PATCH] gpu/cuda: GPU_REGISTERED to distinguish GPU memory CPU mapped
Date: Fri, 29 Apr 2022 13:52:51 +0000	[thread overview]
Message-ID: <20220429135251.1373996-1-eagostini@nvidia.com> (raw)

From: Elena Agostini <eagostini@nvidia.com>

Enable GPU_REGISTERED flag in gpu/cuda driver in the memory list.
If a GPU memory address CPU mapped is freed before being
unmapped, CUDA driver unmaps it before freeing the memory.

Signed-off-by: Elena Agostini <eagostini@nvidia.com>
---
 drivers/gpu/cuda/cuda.c | 78 +++++++++++++++++++++++++----------------
 1 file changed, 47 insertions(+), 31 deletions(-)

diff --git a/drivers/gpu/cuda/cuda.c b/drivers/gpu/cuda/cuda.c
index 5b8476ac20..c6bf54c130 100644
--- a/drivers/gpu/cuda/cuda.c
+++ b/drivers/gpu/cuda/cuda.c
@@ -257,7 +257,7 @@ struct cuda_info {
 enum mem_type {
 	GPU_MEM = 0,
 	CPU_REGISTERED,
-	GPU_REGISTERED /* Not used yet */
+	GPU_REGISTERED
 };
 
 /* key associated to a memory address */
@@ -953,13 +953,14 @@ cuda_mem_cpu_map(struct rte_gpu *dev, __rte_unused size_t size, void *ptr_in, vo
 		return -rte_errno;
 	}
 
+	mem_item->mtype = GPU_REGISTERED;
 	*ptr_out = mem_item->ptr_h;
 
 	return 0;
 }
 
 static int
-cuda_mem_free(struct rte_gpu *dev, void *ptr)
+cuda_mem_unregister(struct rte_gpu *dev, void *ptr)
 {
 	CUresult res;
 	struct mem_entry *mem_item;
@@ -978,11 +979,11 @@ cuda_mem_free(struct rte_gpu *dev, void *ptr)
 		return -rte_errno;
 	}
 
-	if (mem_item->mtype == GPU_MEM) {
-		res = pfn_cuMemFree(mem_item->ptr_orig_d);
+	if (mem_item->mtype == CPU_REGISTERED) {
+		res = pfn_cuMemHostUnregister(ptr);
 		if (res != 0) {
 			pfn_cuGetErrorString(res, &(err_string));
-			rte_cuda_log(ERR, "cuMemFree current failed with %s",
+			rte_cuda_log(ERR, "cuMemHostUnregister current failed with %s",
 					err_string);
 			rte_errno = EPERM;
 			return -rte_errno;
@@ -993,74 +994,89 @@ cuda_mem_free(struct rte_gpu *dev, void *ptr)
 
 	rte_cuda_log(ERR, "Memory type %d not supported", mem_item->mtype);
 
-	return -EPERM;
+	rte_errno = EPERM;
+	return -rte_errno;
 }
 
 static int
-cuda_mem_unregister(struct rte_gpu *dev, void *ptr)
+cuda_mem_cpu_unmap(struct rte_gpu *dev, void *ptr_in)
 {
-	CUresult res;
 	struct mem_entry *mem_item;
-	const char *err_string;
 	cuda_ptr_key hk;
 
 	if (dev == NULL)
 		return -ENODEV;
 
-	hk = get_hash_from_ptr((void *)ptr);
+	hk = get_hash_from_ptr((void *)ptr_in);
 
 	mem_item = mem_list_find_item(hk);
 	if (mem_item == NULL) {
-		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory", ptr);
+		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory.", ptr_in);
 		rte_errno = EPERM;
 		return -rte_errno;
 	}
 
-	if (mem_item->mtype == CPU_REGISTERED) {
-		res = pfn_cuMemHostUnregister(ptr);
-		if (res != 0) {
-			pfn_cuGetErrorString(res, &(err_string));
-			rte_cuda_log(ERR, "cuMemHostUnregister current failed with %s",
-					err_string);
+	if (mem_item->mtype == GPU_REGISTERED) {
+		if (gdrcopy_unpin(gdrc_h, mem_item->mh, (void *)mem_item->ptr_d,
+				mem_item->size)) {
+			rte_cuda_log(ERR, "Error unexposing GPU memory address 0x%p.", ptr_in);
 			rte_errno = EPERM;
 			return -rte_errno;
 		}
 
-		return mem_list_del_item(hk);
+		mem_item->mtype = GPU_MEM;
+	} else {
+		rte_errno = EPERM;
+		return -rte_errno;
 	}
 
-	rte_cuda_log(ERR, "Memory type %d not supported", mem_item->mtype);
-
-	rte_errno = EPERM;
-	return -rte_errno;
+	return 0;
 }
 
 static int
-cuda_mem_cpu_unmap(struct rte_gpu *dev, void *ptr_in)
+cuda_mem_free(struct rte_gpu *dev, void *ptr)
 {
+	CUresult res;
 	struct mem_entry *mem_item;
+	const char *err_string;
 	cuda_ptr_key hk;
 
 	if (dev == NULL)
 		return -ENODEV;
 
-	hk = get_hash_from_ptr((void *)ptr_in);
+	hk = get_hash_from_ptr((void *)ptr);
 
 	mem_item = mem_list_find_item(hk);
 	if (mem_item == NULL) {
-		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory.", ptr_in);
+		rte_cuda_log(ERR, "Memory address 0x%p not found in driver memory", ptr);
 		rte_errno = EPERM;
 		return -rte_errno;
 	}
 
-	if (gdrcopy_unpin(gdrc_h, mem_item->mh, (void *)mem_item->ptr_d,
-			mem_item->size)) {
-		rte_cuda_log(ERR, "Error unexposing GPU memory address 0x%p.", ptr_in);
-		rte_errno = EPERM;
-		return -rte_errno;
+	/*
+	 * If a GPU memory area that's CPU mapped is being freed
+	 * without calling cpu_unmap, force the unmapping.
+	*/
+	if (mem_item->mtype == GPU_REGISTERED) {
+		cuda_mem_cpu_unmap(dev, ptr);
 	}
 
-	return 0;
+	if (mem_item->mtype == GPU_MEM) {
+		res = pfn_cuMemFree(mem_item->ptr_orig_d);
+		if (res != 0) {
+			pfn_cuGetErrorString(res, &(err_string));
+			rte_cuda_log(ERR, "cuMemFree current failed with %s",
+					err_string);
+			rte_errno = EPERM;
+			return -rte_errno;
+		}
+
+		return mem_list_del_item(hk);
+	}
+
+	rte_cuda_log(ERR, "Memory type %d not supported", mem_item->mtype);
+
+	return -EPERM;
 }
 
 static int
-- 
2.25.1


             reply	other threads:[~2022-04-29 13:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-29 13:52 eagostini [this message]
2022-04-29 14:14 ` [PATCH] gpu/cuda: GPU_REGISTERED to distinguish GPU memory CPU mapped eagostini
2022-05-24 21:11   ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220429135251.1373996-1-eagostini@nvidia.com \
    --to=eagostini@nvidia.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.