linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
@ 2025-08-11  9:19 Qianfeng Rong
  2025-08-12 22:52 ` Timur Tabi
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Qianfeng Rong @ 2025-08-11  9:19 UTC (permalink / raw)
  To: Lyude Paul, Danilo Krummrich, David Airlie, Simona Vetter,
	Dave Airlie, Ben Skeggs, Timur Tabi, Qianfeng Rong, Zhi Wang,
	dri-devel, nouveau, linux-kernel

Replace kfree() with kvfree() for memory allocated by kvmalloc().

Compile-tested only.

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
index 9d06ff722fea..0dc4782df8c0 100644
--- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
+++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
@@ -325,7 +325,7 @@ r535_gsp_msgq_recv(struct nvkm_gsp *gsp, u32 gsp_rpc_len, int *retries)
 
 		rpc = r535_gsp_msgq_peek(gsp, sizeof(*rpc), info.retries);
 		if (IS_ERR_OR_NULL(rpc)) {
-			kfree(buf);
+			kvfree(buf);
 			return rpc;
 		}
 
@@ -334,7 +334,7 @@ r535_gsp_msgq_recv(struct nvkm_gsp *gsp, u32 gsp_rpc_len, int *retries)
 
 		rpc = r535_gsp_msgq_recv_one_elem(gsp, &info);
 		if (IS_ERR_OR_NULL(rpc)) {
-			kfree(buf);
+			kvfree(buf);
 			return rpc;
 		}
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-11  9:19 [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc() Qianfeng Rong
@ 2025-08-12 22:52 ` Timur Tabi
  2025-08-13 11:00   ` Danilo Krummrich
  2025-08-13 11:01   ` Zhi Wang
  2025-08-13 10:46 ` Zhi Wang
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 10+ messages in thread
From: Timur Tabi @ 2025-08-12 22:52 UTC (permalink / raw)
  To: rongqianfeng@vivo.com, airlied@redhat.com, simona@ffwll.ch,
	lyude@redhat.com, dri-devel@lists.freedesktop.org, Zhi Wang,
	nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	dakr@kernel.org

On Mon, 2025-08-11 at 17:19 +0800, Qianfeng Rong wrote:
> Replace kfree() with kvfree() for memory allocated by kvmalloc().
> 
> Compile-tested only.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>

Reviewed-by: Timur Tabi <ttabi@nvidia.com>

This does fix a real bug.

However, I think the real problem is that it's really confusing that
r535_gsp_msgq_recv_one_elem(gsp, &info) returns info.gsp_rpc_buf instead of just success/failure. 
r535_gsp_msgq_recv() does this:

	buf = kvmalloc(max_t(u32, rpc->length, expected), GFP_KERNEL);
...
	info.gsp_rpc_buf = buf;
... 
	buf = r535_gsp_msgq_recv_one_elem(gsp, &info);

You wouldn't know it, but this does not change the value of 'buf' unless
r535_gsp_msgq_recv_one_elem() fails.  If it does fail, the code does this:

	if (IS_ERR(buf)) {
		kvfree(info.gsp_rpc_buf);

It would be a lot clearer if we could kvfree(buf) here, but we can't because 'buf' no longer points
to the buffer, even though the buffer still exists.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-11  9:19 [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc() Qianfeng Rong
  2025-08-12 22:52 ` Timur Tabi
@ 2025-08-13 10:46 ` Zhi Wang
  2025-08-13 11:01   ` Danilo Krummrich
  2025-08-13 12:23 ` Markus Elfring
  2025-08-13 12:56 ` Markus Elfring
  3 siblings, 1 reply; 10+ messages in thread
From: Zhi Wang @ 2025-08-13 10:46 UTC (permalink / raw)
  To: Qianfeng Rong
  Cc: Lyude Paul, Danilo Krummrich, David Airlie, Simona Vetter,
	Dave Airlie, Ben Skeggs, Timur Tabi, dri-devel, nouveau,
	linux-kernel

On Mon, 11 Aug 2025 17:19:00 +0800
Qianfeng Rong <rongqianfeng@vivo.com> wrote:

Acked-by: Zhi Wang <zhiw@nvidia.com>

Please add a Fixes: tag.

> Replace kfree() with kvfree() for memory allocated by kvmalloc().
> 
> Compile-tested only.
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> ---
>  drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
> index 9d06ff722fea..0dc4782df8c0 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c
> @@ -325,7 +325,7 @@ r535_gsp_msgq_recv(struct nvkm_gsp *gsp, u32 gsp_rpc_len, int *retries)
>  
>  		rpc = r535_gsp_msgq_peek(gsp, sizeof(*rpc), info.retries);
>  		if (IS_ERR_OR_NULL(rpc)) {
> -			kfree(buf);
> +			kvfree(buf);
>  			return rpc;
>  		}
>  
> @@ -334,7 +334,7 @@ r535_gsp_msgq_recv(struct nvkm_gsp *gsp, u32 gsp_rpc_len, int *retries)
>  
>  		rpc = r535_gsp_msgq_recv_one_elem(gsp, &info);
>  		if (IS_ERR_OR_NULL(rpc)) {
> -			kfree(buf);
> +			kvfree(buf);
>  			return rpc;
>  		}
>  


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-12 22:52 ` Timur Tabi
@ 2025-08-13 11:00   ` Danilo Krummrich
  2025-08-13 14:02     ` Timur Tabi
  2025-08-13 11:01   ` Zhi Wang
  1 sibling, 1 reply; 10+ messages in thread
From: Danilo Krummrich @ 2025-08-13 11:00 UTC (permalink / raw)
  To: Timur Tabi
  Cc: rongqianfeng@vivo.com, airlied@redhat.com, simona@ffwll.ch,
	lyude@redhat.com, dri-devel@lists.freedesktop.org, Zhi Wang,
	nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org

On Wed Aug 13, 2025 at 12:52 AM CEST, Timur Tabi wrote:
> On Mon, 2025-08-11 at 17:19 +0800, Qianfeng Rong wrote:
>> Replace kfree() with kvfree() for memory allocated by kvmalloc().
>> 
>> Compile-tested only.
>> 
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>
> Reviewed-by: Timur Tabi <ttabi@nvidia.com>
>
> This does fix a real bug.
>
> However, I think the real problem is that it's really confusing that
> r535_gsp_msgq_recv_one_elem(gsp, &info) returns info.gsp_rpc_buf instead of just success/failure. 
> r535_gsp_msgq_recv() does this:
>
> 	buf = kvmalloc(max_t(u32, rpc->length, expected), GFP_KERNEL);
> ...
> 	info.gsp_rpc_buf = buf;
> ... 
> 	buf = r535_gsp_msgq_recv_one_elem(gsp, &info);
>
> You wouldn't know it, but this does not change the value of 'buf' unless
> r535_gsp_msgq_recv_one_elem() fails.  If it does fail, the code does this:

Ick! That makes no sense, r535_gsp_msgq_recv_one_elem() should just return an
int and we shouldn't overwrite buf -- that's a footgun.

> 	if (IS_ERR(buf)) {
> 		kvfree(info.gsp_rpc_buf);

Should just be

	if (ret) {
		kvfree(buf);
		return ERR_PTR(ret);
	}

It also doesn't need the info.gsp_rpc_buf = NULL; assignment, info is local
anyways.

> It would be a lot clearer if we could kvfree(buf) here, but we can't because 'buf' no longer points
> to the buffer, even though the buffer still exists.

Agreed.

Zhi, Timur do you want to send a follow-up patch for this?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-12 22:52 ` Timur Tabi
  2025-08-13 11:00   ` Danilo Krummrich
@ 2025-08-13 11:01   ` Zhi Wang
  1 sibling, 0 replies; 10+ messages in thread
From: Zhi Wang @ 2025-08-13 11:01 UTC (permalink / raw)
  To: Timur Tabi, rongqianfeng@vivo.com, airlied@redhat.com,
	simona@ffwll.ch, lyude@redhat.com,
	dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, dakr@kernel.org

On 13/08/2025 1.52, Timur Tabi wrote:
> On Mon, 2025-08-11 at 17:19 +0800, Qianfeng Rong wrote:
>> Replace kfree() with kvfree() for memory allocated by kvmalloc().
>>
>> Compile-tested only.
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> 
> Reviewed-by: Timur Tabi <ttabi@nvidia.com>
> 
> This does fix a real bug.
> 

Agree with the coding details.

I felt the core issue is that GSP RPC lifecycle management in NVKM is 
not handled cleanly. For example, the caller’s RPC buffer is freed 
silently in the receive path, and a new buffer is allocated and returned 
without explicit coordination.

Introducing large GSP RPCs - such as factoring out 
r535_gsp_msgq_recv_one_elem() - only makes this flaw more apparent, and 
even the refactoring process is cumbersome and tricky.

Ideally, there should be a clear ownership and lifecycle flow between 
the caller and the GSP RPC routines: the caller allocates and frees the 
RPC buffer, while the low-level routines focus solely on send/receive 
operations. r535_gsp_msgq_recv_one_elem() is just on its half way.

Z.

> However, I think the real problem is that it's really confusing that
> r535_gsp_msgq_recv_one_elem(gsp, &info) returns info.gsp_rpc_buf instead of just success/failure.
> r535_gsp_msgq_recv() does this:
> 
> 	buf = kvmalloc(max_t(u32, rpc->length, expected), GFP_KERNEL);
> ...
> 	info.gsp_rpc_buf = buf;
> ...
> 	buf = r535_gsp_msgq_recv_one_elem(gsp, &info);
> 
> You wouldn't know it, but this does not change the value of 'buf' unless
> r535_gsp_msgq_recv_one_elem() fails.  If it does fail, the code does this:
> 
> 	if (IS_ERR(buf)) {
> 		kvfree(info.gsp_rpc_buf);
> 
> It would be a lot clearer if we could kvfree(buf) here, but we can't because 'buf' no longer points
> to the buffer, even though the buffer still exists.
> 
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-13 10:46 ` Zhi Wang
@ 2025-08-13 11:01   ` Danilo Krummrich
  2025-08-13 11:37     ` Qianfeng Rong
  0 siblings, 1 reply; 10+ messages in thread
From: Danilo Krummrich @ 2025-08-13 11:01 UTC (permalink / raw)
  To: Zhi Wang
  Cc: Qianfeng Rong, Lyude Paul, David Airlie, Simona Vetter,
	Dave Airlie, Ben Skeggs, Timur Tabi, dri-devel, nouveau,
	linux-kernel

On Wed Aug 13, 2025 at 12:46 PM CEST, Zhi Wang wrote:
> On Mon, 11 Aug 2025 17:19:00 +0800
> Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>
> Acked-by: Zhi Wang <zhiw@nvidia.com>
>
> Please add a Fixes: tag.

And please also add "Cc: stable@vger.kernel.org", thanks!

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-13 11:01   ` Danilo Krummrich
@ 2025-08-13 11:37     ` Qianfeng Rong
  0 siblings, 0 replies; 10+ messages in thread
From: Qianfeng Rong @ 2025-08-13 11:37 UTC (permalink / raw)
  To: Danilo Krummrich, Zhi Wang
  Cc: Lyude Paul, David Airlie, Simona Vetter, Dave Airlie, Ben Skeggs,
	Timur Tabi, dri-devel, nouveau, linux-kernel


在 2025/8/13 19:01, Danilo Krummrich 写道:
> [You don't often get email from dakr@kernel.org. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Wed Aug 13, 2025 at 12:46 PM CEST, Zhi Wang wrote:
>> On Mon, 11 Aug 2025 17:19:00 +0800
>> Qianfeng Rong <rongqianfeng@vivo.com> wrote:
>>
>> Acked-by: Zhi Wang <zhiw@nvidia.com>
>>
>> Please add a Fixes: tag.
> And please also add "Cc: stable@vger.kernel.org", thanks!
Ok,  Will do in the next version.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-11  9:19 [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc() Qianfeng Rong
  2025-08-12 22:52 ` Timur Tabi
  2025-08-13 10:46 ` Zhi Wang
@ 2025-08-13 12:23 ` Markus Elfring
  2025-08-13 12:56 ` Markus Elfring
  3 siblings, 0 replies; 10+ messages in thread
From: Markus Elfring @ 2025-08-13 12:23 UTC (permalink / raw)
  To: Qianfeng Rong, nouveau, dri-devel, Ben Skeggs, Danilo Krummrich,
	Dave Airlie, Lyude Paul, Simona Vetter, Timur Tabi, Zhi Wang
  Cc: LKML, David Airlie

> Replace kfree() with kvfree() for memory allocated by kvmalloc().

* Would you like to improve the exception handling by using another goto chain?

* How do you think about to increase the application of scope-based resource management?
  https://elixir.bootlin.com/linux/v6.16/source/include/linux/slab.h#L1081


Regards,
Markus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-11  9:19 [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc() Qianfeng Rong
                   ` (2 preceding siblings ...)
  2025-08-13 12:23 ` Markus Elfring
@ 2025-08-13 12:56 ` Markus Elfring
  3 siblings, 0 replies; 10+ messages in thread
From: Markus Elfring @ 2025-08-13 12:56 UTC (permalink / raw)
  To: Qianfeng Rong, nouveau, dri-devel, Ben Skeggs, Danilo Krummrich,
	Dave Airlie, Lyude Paul, Simona Vetter, Timur Tabi, Zhi Wang
  Cc: LKML, David Airlie

> Replace …

Is there a need to adjust also the following statement combination?
https://elixir.bootlin.com/linux/v6.16/source/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/rpc.c#L312-L314

…
		kvfree(info.gsp_rpc_buf);
		info.gsp_rpc_buf = NULL;
		return buf;
…

Regards,
Markus

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc()
  2025-08-13 11:00   ` Danilo Krummrich
@ 2025-08-13 14:02     ` Timur Tabi
  0 siblings, 0 replies; 10+ messages in thread
From: Timur Tabi @ 2025-08-13 14:02 UTC (permalink / raw)
  To: dakr@kernel.org
  Cc: rongqianfeng@vivo.com, airlied@redhat.com, simona@ffwll.ch,
	lyude@redhat.com, dri-devel@lists.freedesktop.org, Zhi Wang,
	linux-kernel@vger.kernel.org, nouveau@lists.freedesktop.org

On Wed, 2025-08-13 at 13:00 +0200, Danilo Krummrich wrote:
> > It would be a lot clearer if we could kvfree(buf) here, but we can't because 'buf' no longer
> > points
> > to the buffer, even though the buffer still exists.
> 
> Agreed.
> 
> Zhi, Timur do you want to send a follow-up patch for this?

Yes, I'll send a patch this week that sits on top of Qianfeng's patch.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-08-13 14:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-11  9:19 [PATCH] drm/nouveau/gsp: fix mismatched alloc/free for kvmalloc() Qianfeng Rong
2025-08-12 22:52 ` Timur Tabi
2025-08-13 11:00   ` Danilo Krummrich
2025-08-13 14:02     ` Timur Tabi
2025-08-13 11:01   ` Zhi Wang
2025-08-13 10:46 ` Zhi Wang
2025-08-13 11:01   ` Danilo Krummrich
2025-08-13 11:37     ` Qianfeng Rong
2025-08-13 12:23 ` Markus Elfring
2025-08-13 12:56 ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).