Jani Nikula
2024-Mar-04 12:14 UTC
[PATCH] nouveau/dmem: handle kcalloc() allocation failure
On Sun, 03 Mar 2024, Timur Tabi <timur at kernel.org> wrote:> On Sun, Mar 3, 2024 at 4:46?AM Duoming Zhou <duoming at 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
duoming at zju.edu.cn
2024-Mar-05 13:08 UTC
[PATCH] nouveau/dmem: handle kcalloc() allocation failure
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
Jani Nikula
2024-Mar-05 13:26 UTC
[PATCH] nouveau/dmem: handle kcalloc() allocation failure
On Tue, 05 Mar 2024, duoming at 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