[PATCH] nvdimm: ndtest: Add check for devm_kcalloc() allocations in ndtest_probe()

Guangshuo Li posted 1 patch 1 week, 6 days ago
tools/testing/nvdimm/test/ndtest.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] nvdimm: ndtest: Add check for devm_kcalloc() allocations in ndtest_probe()
Posted by Guangshuo Li 1 week, 6 days ago
devm_kcalloc() may fail. ndtest_probe() allocates three DMA address
arrays (dcr_dma, label_dma, dimm_dma) and later unconditionally uses
them in ndtest_nvdimm_init(), which can lead to a NULL pointer
dereference on allocation failure.

Add NULL checks for all three allocations and return -ENOMEM if any
allocation fails.

Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 tools/testing/nvdimm/test/ndtest.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test/ndtest.c
index 68a064ce598c..516f304bb0b9 100644
--- a/tools/testing/nvdimm/test/ndtest.c
+++ b/tools/testing/nvdimm/test/ndtest.c
@@ -855,6 +855,11 @@ static int ndtest_probe(struct platform_device *pdev)
 	p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
 				  sizeof(dma_addr_t), GFP_KERNEL);
 
+	if (!p->dcr_dma || !p->label_dma || !p->dimm_dma) {
+		pr_err("%s: failed to allocate DMA address arrays\n", __func__);
+		return -ENOMEM;
+	}
+
 	rc = ndtest_nvdimm_init(p);
 	if (rc)
 		goto err;
-- 
2.43.0
Re: [PATCH] nvdimm: ndtest: Add check for devm_kcalloc() allocations in ndtest_probe()
Posted by Alison Schofield 1 week, 6 days ago
On Thu, Sep 18, 2025 at 10:16:06PM +0800, Guangshuo Li wrote:
> devm_kcalloc() may fail. ndtest_probe() allocates three DMA address
> arrays (dcr_dma, label_dma, dimm_dma) and later unconditionally uses
> them in ndtest_nvdimm_init(), which can lead to a NULL pointer
> dereference on allocation failure.
> 
> Add NULL checks for all three allocations and return -ENOMEM if any
> allocation fails.
> 
> Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  tools/testing/nvdimm/test/ndtest.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test/ndtest.c
> index 68a064ce598c..516f304bb0b9 100644
> --- a/tools/testing/nvdimm/test/ndtest.c
> +++ b/tools/testing/nvdimm/test/ndtest.c
> @@ -855,6 +855,11 @@ static int ndtest_probe(struct platform_device *pdev)
>  	p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
>  				  sizeof(dma_addr_t), GFP_KERNEL);
>  
> +	if (!p->dcr_dma || !p->label_dma || !p->dimm_dma) {
> +		pr_err("%s: failed to allocate DMA address arrays\n", __func__);
> +		return -ENOMEM;
> +	}
> +

Hi Guangshuo Li,
Can you check on correctness of inserting that pr_err()?
ie. does it fall into the category of excessive OOM messaging?



>  	rc = ndtest_nvdimm_init(p);
>  	if (rc)
>  		goto err;
> -- 
> 2.43.0
> 
>