* [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe()
@ 2025-09-25 6:44 Guangshuo Li
2025-09-25 15:42 ` Dave Jiang
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Guangshuo Li @ 2025-09-25 6:44 UTC (permalink / raw)
To: Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny, Guangshuo Li,
Santosh Sivaraj, nvdimm, linux-kernel
Cc: stable
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 under low-memory conditions.
Check all three allocations and return -ENOMEM if any allocation fails,
jumping to the common error path. Do not emit an extra error message
since the allocator already warns on allocation failure.
Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
changelog:
v3:
- Add NULL checks for all three devm_kcalloc() calls and goto the common
error label on failure.
v2:
- Drop pr_err() on allocation failure; only NULL-check and return -ENOMEM.
- No other changes.
---
tools/testing/nvdimm/test/ndtest.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test/ndtest.c
index 68a064ce598c..8e3b6be53839 100644
--- a/tools/testing/nvdimm/test/ndtest.c
+++ b/tools/testing/nvdimm/test/ndtest.c
@@ -850,11 +850,22 @@ static int ndtest_probe(struct platform_device *pdev)
p->dcr_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
sizeof(dma_addr_t), GFP_KERNEL);
+ if (!p->dcr_dma) {
+ rc = -ENOMEM;
+ goto err;
+ }
p->label_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
sizeof(dma_addr_t), GFP_KERNEL);
+ if (!p->label_dma) {
+ rc = -ENOMEM;
+ goto err;
+ }
p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
sizeof(dma_addr_t), GFP_KERNEL);
-
+ if (!p->dimm_dma) {
+ rc = -ENOMEM;
+ goto err;
+ }
rc = ndtest_nvdimm_init(p);
if (rc)
goto err;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe()
2025-09-25 6:44 [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() Guangshuo Li
@ 2025-09-25 15:42 ` Dave Jiang
2025-09-25 16:08 ` Ira Weiny
2025-09-25 16:39 ` Alison Schofield
2 siblings, 0 replies; 4+ messages in thread
From: Dave Jiang @ 2025-09-25 15:42 UTC (permalink / raw)
To: Guangshuo Li, Dan Williams, Vishal Verma, Ira Weiny,
Santosh Sivaraj, nvdimm, linux-kernel
Cc: stable
On 9/24/25 11:44 PM, 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 under low-memory conditions.
>
> Check all three allocations and return -ENOMEM if any allocation fails,
> jumping to the common error path. Do not emit an extra error message
> since the allocator already warns on allocation failure.
>
> Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> changelog:
> v3:
> - Add NULL checks for all three devm_kcalloc() calls and goto the common
> error label on failure.
>
> v2:
> - Drop pr_err() on allocation failure; only NULL-check and return -ENOMEM.
> - No other changes.
> ---
> tools/testing/nvdimm/test/ndtest.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test/ndtest.c
> index 68a064ce598c..8e3b6be53839 100644
> --- a/tools/testing/nvdimm/test/ndtest.c
> +++ b/tools/testing/nvdimm/test/ndtest.c
> @@ -850,11 +850,22 @@ static int ndtest_probe(struct platform_device *pdev)
>
> p->dcr_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> + if (!p->dcr_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> p->label_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> + if (!p->label_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> -
> + if (!p->dimm_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> rc = ndtest_nvdimm_init(p);
> if (rc)
> goto err;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe()
2025-09-25 6:44 [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() Guangshuo Li
2025-09-25 15:42 ` Dave Jiang
@ 2025-09-25 16:08 ` Ira Weiny
2025-09-25 16:39 ` Alison Schofield
2 siblings, 0 replies; 4+ messages in thread
From: Ira Weiny @ 2025-09-25 16:08 UTC (permalink / raw)
To: Guangshuo Li, Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Santosh Sivaraj, nvdimm, linux-kernel
Cc: stable
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 under low-memory conditions.
>
> Check all three allocations and return -ENOMEM if any allocation fails,
> jumping to the common error path. Do not emit an extra error message
> since the allocator already warns on allocation failure.
>
> Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
[snip]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe()
2025-09-25 6:44 [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() Guangshuo Li
2025-09-25 15:42 ` Dave Jiang
2025-09-25 16:08 ` Ira Weiny
@ 2025-09-25 16:39 ` Alison Schofield
2 siblings, 0 replies; 4+ messages in thread
From: Alison Schofield @ 2025-09-25 16:39 UTC (permalink / raw)
To: Guangshuo Li
Cc: Dan Williams, Vishal Verma, Dave Jiang, Ira Weiny,
Santosh Sivaraj, nvdimm, linux-kernel, stable
On Thu, Sep 25, 2025 at 02:44:48PM +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 under low-memory conditions.
>
> Check all three allocations and return -ENOMEM if any allocation fails,
> jumping to the common error path. Do not emit an extra error message
> since the allocator already warns on allocation failure.
Reviewed-by: Alison Schofield <alison.schofield@intel.com>
>
> Fixes: 9399ab61ad82 ("ndtest: Add dimms to the two buses")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
> changelog:
> v3:
> - Add NULL checks for all three devm_kcalloc() calls and goto the common
> error label on failure.
>
> v2:
> - Drop pr_err() on allocation failure; only NULL-check and return -ENOMEM.
> - No other changes.
> ---
> tools/testing/nvdimm/test/ndtest.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/nvdimm/test/ndtest.c b/tools/testing/nvdimm/test/ndtest.c
> index 68a064ce598c..8e3b6be53839 100644
> --- a/tools/testing/nvdimm/test/ndtest.c
> +++ b/tools/testing/nvdimm/test/ndtest.c
> @@ -850,11 +850,22 @@ static int ndtest_probe(struct platform_device *pdev)
>
> p->dcr_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> + if (!p->dcr_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> p->label_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> + if (!p->label_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
> sizeof(dma_addr_t), GFP_KERNEL);
> -
> + if (!p->dimm_dma) {
> + rc = -ENOMEM;
> + goto err;
> + }
> rc = ndtest_nvdimm_init(p);
> if (rc)
> goto err;
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-25 16:39 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-25 6:44 [PATCH v3] nvdimm: ndtest: Return -ENOMEM if devm_kcalloc() fails in ndtest_probe() Guangshuo Li
2025-09-25 15:42 ` Dave Jiang
2025-09-25 16:08 ` Ira Weiny
2025-09-25 16:39 ` Alison Schofield
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox