All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nouveau/dmem: handle kcalloc() allocation failure
@ 2024-03-03  7:53 Duoming Zhou
  2024-03-05 13:40 ` Danilo Krummrich
  0 siblings, 1 reply; 7+ messages in thread
From: Duoming Zhou @ 2024-03-03  7:53 UTC (permalink / raw)
  To: linux-kernel
  Cc: nouveau, dri-devel, daniel, airlied, dakr, lyude, kherbst,
	Duoming Zhou

The kcalloc() in nouveau_dmem_evict_chunk() will return null if
the physical memory has run out. As a result, if we dereference
src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
will happen.

This patch uses stack variables to replace the kcalloc().

Fixes: 249881232e14 ("nouveau/dmem: evict device private memory during release")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/gpu/drm/nouveau/nouveau_dmem.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c
index 12feecf71e7..9a578262c6d 100644
--- a/drivers/gpu/drm/nouveau/nouveau_dmem.c
+++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c
@@ -374,13 +374,13 @@ static void
 nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
 {
 	unsigned long i, npages = range_len(&chunk->pagemap.range) >> PAGE_SHIFT;
-	unsigned long *src_pfns, *dst_pfns;
-	dma_addr_t *dma_addrs;
+	unsigned long src_pfns[npages], dst_pfns[npages];
+	dma_addr_t dma_addrs[npages];
 	struct nouveau_fence *fence;
 
-	src_pfns = kcalloc(npages, sizeof(*src_pfns), GFP_KERNEL);
-	dst_pfns = kcalloc(npages, sizeof(*dst_pfns), GFP_KERNEL);
-	dma_addrs = kcalloc(npages, sizeof(*dma_addrs), GFP_KERNEL);
+	memset(src_pfns, 0, npages);
+	memset(dst_pfns, 0, npages);
+	memset(dma_addrs, 0, npages);
 
 	migrate_device_range(src_pfns, chunk->pagemap.range.start >> PAGE_SHIFT,
 			npages);
@@ -406,11 +406,8 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
 	migrate_device_pages(src_pfns, dst_pfns, npages);
 	nouveau_dmem_fence_done(&fence);
 	migrate_device_finalize(src_pfns, dst_pfns, npages);
-	kfree(src_pfns);
-	kfree(dst_pfns);
 	for (i = 0; i < npages; i++)
 		dma_unmap_page(chunk->drm->dev->dev, dma_addrs[i], PAGE_SIZE, DMA_BIDIRECTIONAL);
-	kfree(dma_addrs);
 }
 
 void
-- 
2.17.1


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

* [PATCH] nouveau/dmem: handle kcalloc() allocation failure
@ 2024-03-03  8:13 Duoming Zhou
  2024-03-04  3:02 ` Timur Tabi
  0 siblings, 1 reply; 7+ messages in thread
From: Duoming Zhou @ 2024-03-03  8:13 UTC (permalink / raw)
  To: nouveau; +Cc: dri-devel, Duoming Zhou

The kcalloc() in nouveau_dmem_evict_chunk() will return null if
the physical memory has run out. As a result, if we dereference
src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
will happen.

This patch uses stack variables to replace the kcalloc().

Fixes: 249881232e14 ("nouveau/dmem: evict device private memory during release")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/gpu/drm/nouveau/nouveau_dmem.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c
index 12feecf71e7..9a578262c6d 100644
--- a/drivers/gpu/drm/nouveau/nouveau_dmem.c
+++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c
@@ -374,13 +374,13 @@ static void
 nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
 {
 	unsigned long i, npages = range_len(&chunk->pagemap.range) >> PAGE_SHIFT;
-	unsigned long *src_pfns, *dst_pfns;
-	dma_addr_t *dma_addrs;
+	unsigned long src_pfns[npages], dst_pfns[npages];
+	dma_addr_t dma_addrs[npages];
 	struct nouveau_fence *fence;
 
-	src_pfns = kcalloc(npages, sizeof(*src_pfns), GFP_KERNEL);
-	dst_pfns = kcalloc(npages, sizeof(*dst_pfns), GFP_KERNEL);
-	dma_addrs = kcalloc(npages, sizeof(*dma_addrs), GFP_KERNEL);
+	memset(src_pfns, 0, npages);
+	memset(dst_pfns, 0, npages);
+	memset(dma_addrs, 0, npages);
 
 	migrate_device_range(src_pfns, chunk->pagemap.range.start >> PAGE_SHIFT,
 			npages);
@@ -406,11 +406,8 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
 	migrate_device_pages(src_pfns, dst_pfns, npages);
 	nouveau_dmem_fence_done(&fence);
 	migrate_device_finalize(src_pfns, dst_pfns, npages);
-	kfree(src_pfns);
-	kfree(dst_pfns);
 	for (i = 0; i < npages; i++)
 		dma_unmap_page(chunk->drm->dev->dev, dma_addrs[i], PAGE_SIZE, DMA_BIDIRECTIONAL);
-	kfree(dma_addrs);
 }
 
 void
-- 
2.17.1


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

* Re: [PATCH] nouveau/dmem: handle kcalloc() allocation failure
  2024-03-03  8:13 [PATCH] nouveau/dmem: handle kcalloc() allocation failure Duoming Zhou
@ 2024-03-04  3:02 ` Timur Tabi
  2024-03-04 12:14   ` Jani Nikula
  0 siblings, 1 reply; 7+ messages in thread
From: Timur Tabi @ 2024-03-04  3:02 UTC (permalink / raw)
  To: Duoming Zhou; +Cc: nouveau, dri-devel

On Sun, Mar 3, 2024 at 4:46 AM Duoming Zhou <duoming@zju.edu.cn> wrote:
>
> The kcalloc() in nouveau_dmem_evict_chunk() will return null if
> the physical memory has run out. As a result, if we dereference
> src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
> will happen.
>
> This patch uses stack variables to replace the kcalloc().

Won't this blow the stack?  And why not just test the return value of kcalloc?

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

* Re: [PATCH] nouveau/dmem: handle kcalloc() allocation failure
  2024-03-04  3:02 ` Timur Tabi
@ 2024-03-04 12:14   ` Jani Nikula
  2024-03-05 13:08     ` duoming
  0 siblings, 1 reply; 7+ messages in thread
From: Jani Nikula @ 2024-03-04 12:14 UTC (permalink / raw)
  To: Timur Tabi, Duoming Zhou; +Cc: nouveau, dri-devel

On Sun, 03 Mar 2024, Timur Tabi <timur@kernel.org> wrote:
> On Sun, Mar 3, 2024 at 4:46 AM Duoming Zhou <duoming@zju.edu.cn> wrote:
>>
>> The kcalloc() in nouveau_dmem_evict_chunk() will return null if
>> the physical memory has run out. As a result, if we dereference
>> src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
>> will happen.
>>
>> This patch uses stack variables to replace the kcalloc().
>
> Won't this blow the stack?  And why not just test the return value of
> kcalloc?

VLAs should not be used in the kernel anymore. Building this results in
a warning due to -Wvla. See 0bb95f80a38f ("Makefile: Globally enable VLA
warning").

Error checking and propagation is the way to go.


BR,
Jani.

-- 
Jani Nikula, Intel

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

* Re: [PATCH] nouveau/dmem: handle kcalloc() allocation failure
  2024-03-04 12:14   ` Jani Nikula
@ 2024-03-05 13:08     ` duoming
  2024-03-05 13:26       ` Jani Nikula
  0 siblings, 1 reply; 7+ messages in thread
From: duoming @ 2024-03-05 13:08 UTC (permalink / raw)
  To: Jani Nikula; +Cc: Timur Tabi, nouveau, dri-devel

On Mon, 04 Mar 2024 14:14:52 +0200 Jani Nikula wrote:
> >> The kcalloc() in nouveau_dmem_evict_chunk() will return null if
> >> the physical memory has run out. As a result, if we dereference
> >> src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
> >> will happen.
> >>
> >> This patch uses stack variables to replace the kcalloc().
> >
> > Won't this blow the stack?  And why not just test the return value of
> > kcalloc?
> 
> VLAs should not be used in the kernel anymore. Building this results in
> a warning due to -Wvla. See 0bb95f80a38f ("Makefile: Globally enable VLA
> warning").
> 
> Error checking and propagation is the way to go.

The GPU is going away. If the kcalloc() in nouveau_dmem_evict_chunk() fail,
we could not evict all pages mapping a chunk. Do you think we should add a
__GFP_NOFAIL flag in kcalloc()? I see the __GFP_NOFAIL flag is used in the
following code:

/*
* _GFP_NOFAIL because the GPU is going away and there
* is nothing sensible we can do if we can't copy the
* data back.
*/
dpage = alloc_page(GFP_HIGHUSER | __GFP_NOFAIL);

Best regards,
Duoming Zhou

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

* Re: [PATCH] nouveau/dmem: handle kcalloc() allocation failure
  2024-03-05 13:08     ` duoming
@ 2024-03-05 13:26       ` Jani Nikula
  0 siblings, 0 replies; 7+ messages in thread
From: Jani Nikula @ 2024-03-05 13:26 UTC (permalink / raw)
  To: duoming; +Cc: Timur Tabi, nouveau, dri-devel

On Tue, 05 Mar 2024, duoming@zju.edu.cn wrote:
> On Mon, 04 Mar 2024 14:14:52 +0200 Jani Nikula wrote:
>> >> The kcalloc() in nouveau_dmem_evict_chunk() will return null if
>> >> the physical memory has run out. As a result, if we dereference
>> >> src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
>> >> will happen.
>> >>
>> >> This patch uses stack variables to replace the kcalloc().
>> >
>> > Won't this blow the stack?  And why not just test the return value of
>> > kcalloc?
>> 
>> VLAs should not be used in the kernel anymore. Building this results in
>> a warning due to -Wvla. See 0bb95f80a38f ("Makefile: Globally enable VLA
>> warning").
>> 
>> Error checking and propagation is the way to go.
>
> The GPU is going away. If the kcalloc() in nouveau_dmem_evict_chunk() fail,
> we could not evict all pages mapping a chunk. Do you think we should add a
> __GFP_NOFAIL flag in kcalloc()? I see the __GFP_NOFAIL flag is used in the
> following code:
>
> /*
> * _GFP_NOFAIL because the GPU is going away and there
> * is nothing sensible we can do if we can't copy the
> * data back.
> */
> dpage = alloc_page(GFP_HIGHUSER | __GFP_NOFAIL);

This is all up to the nouveau maintainers, really. All I'm saying is
that VLA isn't the solution you're looking for.

BR,
Jani.


-- 
Jani Nikula, Intel

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

* Re: [PATCH] nouveau/dmem: handle kcalloc() allocation failure
  2024-03-03  7:53 Duoming Zhou
@ 2024-03-05 13:40 ` Danilo Krummrich
  0 siblings, 0 replies; 7+ messages in thread
From: Danilo Krummrich @ 2024-03-05 13:40 UTC (permalink / raw)
  To: Duoming Zhou
  Cc: nouveau, dri-devel, daniel, airlied, lyude, kherbst, linux-kernel

Hi Duoming,

On 3/3/24 08:53, Duoming Zhou wrote:
> The kcalloc() in nouveau_dmem_evict_chunk() will return null if
> the physical memory has run out. As a result, if we dereference
> src_pfns, dst_pfns or dma_addrs, the null pointer dereference bugs
> will happen.
> 
> This patch uses stack variables to replace the kcalloc().
> 
> Fixes: 249881232e14 ("nouveau/dmem: evict device private memory during release")
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
>   drivers/gpu/drm/nouveau/nouveau_dmem.c | 13 +++++--------
>   1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c
> index 12feecf71e7..9a578262c6d 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c
> @@ -374,13 +374,13 @@ static void
>   nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
>   {
>   	unsigned long i, npages = range_len(&chunk->pagemap.range) >> PAGE_SHIFT;
> -	unsigned long *src_pfns, *dst_pfns;
> -	dma_addr_t *dma_addrs;
> +	unsigned long src_pfns[npages], dst_pfns[npages];
> +	dma_addr_t dma_addrs[npages];

Please don't use variable length arrays, this can potentially blow up
the stack.

As a fix I think we should allocate with __GFP_NOFAIL instead.

- Danilo

>   	struct nouveau_fence *fence;
>   
> -	src_pfns = kcalloc(npages, sizeof(*src_pfns), GFP_KERNEL);
> -	dst_pfns = kcalloc(npages, sizeof(*dst_pfns), GFP_KERNEL);
> -	dma_addrs = kcalloc(npages, sizeof(*dma_addrs), GFP_KERNEL);
> +	memset(src_pfns, 0, npages);
> +	memset(dst_pfns, 0, npages);
> +	memset(dma_addrs, 0, npages);
>   
>   	migrate_device_range(src_pfns, chunk->pagemap.range.start >> PAGE_SHIFT,
>   			npages);
> @@ -406,11 +406,8 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)
>   	migrate_device_pages(src_pfns, dst_pfns, npages);
>   	nouveau_dmem_fence_done(&fence);
>   	migrate_device_finalize(src_pfns, dst_pfns, npages);
> -	kfree(src_pfns);
> -	kfree(dst_pfns);
>   	for (i = 0; i < npages; i++)
>   		dma_unmap_page(chunk->drm->dev->dev, dma_addrs[i], PAGE_SIZE, DMA_BIDIRECTIONAL);
> -	kfree(dma_addrs);
>   }
>   
>   void


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

end of thread, other threads:[~2024-03-20  9:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-03  8:13 [PATCH] nouveau/dmem: handle kcalloc() allocation failure Duoming Zhou
2024-03-04  3:02 ` Timur Tabi
2024-03-04 12:14   ` Jani Nikula
2024-03-05 13:08     ` duoming
2024-03-05 13:26       ` Jani Nikula
  -- strict thread matches above, loose matches on Subject: below --
2024-03-03  7:53 Duoming Zhou
2024-03-05 13:40 ` Danilo Krummrich

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.