Skip to contentSkip to conversation

drm/amd/amdkfd: Fix large framesize for kfd_smi_ev_read()

PATCH In review Aurabindo Pillai 3 participants 4 messages May 20, 2020 – May 21, 2020

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

PATCH 1/1 drm/amd/amdkfd: Fix large framesize for kfd_smi_ev_read()

The buffer allocated is of 1024 bytes. Allocate this from heap instead of stack.

Also remove check for stack size since we're allocating from heap

---
 drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 26 +++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +19 -7
index f5fd18eacf0d..5aebe169f8c6 100644
@@ -77,9 +77,11 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
 	int ret;
 	size_t to_copy;
 	struct kfd_smi_client *client = filep->private_data;
-	unsigned char buf[MAX_KFIFO_SIZE];
+	unsigned char *buf;
 
-	BUILD_BUG_ON(MAX_KFIFO_SIZE > 1024);
+	buf = kzalloc(MAX_KFIFO_SIZE * sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
 
 	/* kfifo_to_user can sleep so we can't use spinlock protection around
 	 * it. Instead, we kfifo out as spinlocked then copy them to the user.
@@ -88,19 +90,29 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
 	to_copy = kfifo_len(&client->fifo);
 	if (!to_copy) {
 		spin_unlock(&client->lock);
-		return -EAGAIN;
+		ret = -EAGAIN;
+		goto ret_err;
 	}
 	to_copy = min3(size, sizeof(buf), to_copy);
 	ret = kfifo_out(&client->fifo, buf, to_copy);
 	spin_unlock(&client->lock);
-	if (ret <= 0)
-		return -EAGAIN;
+	if (ret <= 0) {
+		ret = -EAGAIN;
+		goto ret_err;
+	}
 
 	ret = copy_to_user(user, buf, to_copy);
-	if (ret)
-		return -EFAULT;
+	if (ret) {
+		ret = -EFAULT;
+		goto ret_err;
+	}
 
+	kfree(buf);
 	return to_copy;
+
+ret_err:
+	kfree(buf);
+	return ret;
 }
 
 static ssize_t kfd_smi_ev_write(struct file *filep, const char __user *user,
Signature · 6 lines
2.25.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Aurabindo Pillai

The buffer allocated is of 1024 bytes. Allocate this from heap instead of stack.

Also remove check for stack size since we're allocating from heap

Quoted 60 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 26 +++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +19 -8
index f5fd18eacf0d..5aebe169f8c6 100644
@@ -77,9 +77,11 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
        int ret;
        size_t to_copy;
        struct kfd_smi_client *client = filep->private_data;
-       unsigned char buf[MAX_KFIFO_SIZE];
+       unsigned char *buf;
 
-       BUILD_BUG_ON(MAX_KFIFO_SIZE > 1024);
+       buf = kzalloc(MAX_KFIFO_SIZE * sizeof(*buf), GFP_KERNEL);
+       if (!buf)
+               return -ENOMEM;
 
        /* kfifo_to_user can sleep so we can't use spinlock protection around
         * it. Instead, we kfifo out as spinlocked then copy them to the user.
@@ -88,19 +90,29 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
        to_copy = kfifo_len(&client->fifo);
        if (!to_copy) {
                spin_unlock(&client->lock);
-               return -EAGAIN;
+               ret = -EAGAIN;
+               goto ret_err;
        }
        to_copy = min3(size, sizeof(buf), to_copy);
        ret = kfifo_out(&client->fifo, buf, to_copy);
        spin_unlock(&client->lock);
-       if (ret <= 0)
-               return -EAGAIN;
+       if (ret <= 0) {
+               ret = -EAGAIN;
+               goto ret_err;
+       }
 
        ret = copy_to_user(user, buf, to_copy);
-       if (ret)
-               return -EFAULT;
+       if (ret) {
+               ret = -EFAULT;
+               goto ret_err;
+       }
 
+       kfree(buf);
        return to_copy;
+
+ret_err:
+       kfree(buf);
+       return ret;
 }
 
 static ssize_t kfd_smi_ev_write(struct file *filep, const char __user *user,
--

2.25.1

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Aurabindo Pillai

Am 2020-05-20 um 9:53 a.m. schrieb Aurabindo Pillai:

Quoted 7 lines

The buffer allocated is of 1024 bytes. Allocate this from heap instead of stack.

Also remove check for stack size since we're allocating from heap

See one comment inline. With that fixed, the patch is

Quoted 9 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 26 +++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +0 -0
index f5fd18eacf0d..5aebe169f8c6 100644

drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user, · line 82

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +2 -2
@@ -77,9 +77,11 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
 	int ret;
 	size_t to_copy;
 	struct kfd_smi_client *client = filep->private_data;
-	unsigned char buf[MAX_KFIFO_SIZE];
+	unsigned char *buf;
 
-	BUILD_BUG_ON(MAX_KFIFO_SIZE > 1024);
+	buf = kzalloc(MAX_KFIFO_SIZE * sizeof(*buf), GFP_KERNEL);

kzalloc is not necessary here, you could use kmalloc. The part of that allocation that matters will be overwritten by kfifo_out.

Regards,
  Felix

Quoted 30 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +12 -5
+	if (!buf)
+		return -ENOMEM;
 
 	/* kfifo_to_user can sleep so we can't use spinlock protection around
 	 * it. Instead, we kfifo out as spinlocked then copy them to the user.
@@ -88,19 +90,29 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
 	to_copy = kfifo_len(&client->fifo);
 	if (!to_copy) {
 		spin_unlock(&client->lock);
-		return -EAGAIN;
+		ret = -EAGAIN;
+		goto ret_err;
 	}
 	to_copy = min3(size, sizeof(buf), to_copy);
 	ret = kfifo_out(&client->fifo, buf, to_copy);
 	spin_unlock(&client->lock);
-	if (ret <= 0)
-		return -EAGAIN;
+	if (ret <= 0) {
+		ret = -EAGAIN;
+		goto ret_err;
+	}
 
 	ret = copy_to_user(user, buf, to_copy);
-	if (ret)
-		return -EFAULT;
+	if (ret) {
+		ret = -EFAULT;
+		goto ret_err;
+	}

drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user, · line 118

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +5 -0
 
+	kfree(buf);
 	return to_copy;
+
+ret_err:
+	kfree(buf);
+	return ret;
 }
 
 static ssize_t kfd_smi_ev_write(struct file *filep, const char __user *user,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw
Quoted 29 lines · Felix Kuehling

Am 2020-05-20 um 9:53 a.m. schrieb Aurabindo Pillai:

Quoted 7 lines

The buffer allocated is of 1024 bytes. Allocate this from heap instead of stack.

Also remove check for stack size since we're allocating from heap

See one comment inline. With that fixed, the patch is

Quoted 15 lines
---
 drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 26 +++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +1 -1
index f5fd18eacf0d..5aebe169f8c6 100644
@@ -77,9 +77,11 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,
 	int ret;
 	size_t to_copy;
 	struct kfd_smi_client *client = filep->private_data;
-	unsigned char buf[MAX_KFIFO_SIZE];
+	unsigned char *buf;
 
Quoted 10 lines

drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user, · line 82

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +1 -1
-	BUILD_BUG_ON(MAX_KFIFO_SIZE > 1024);
+	buf = kzalloc(MAX_KFIFO_SIZE * sizeof(*buf), GFP_KERNEL);

kzalloc is not necessary here, you could use kmalloc. The part of that allocation that matters will be overwritten by kfifo_out.

Regards,
  Felix

Thank you Felix, Alex for the review. I shall make that change and submit it.

Thanks & Regards,
Aurabindo

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx