* [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe()
@ 2026-01-26 13:04 Zhaoyang Yu
2026-01-26 17:39 ` Dave Jiang
2026-01-26 20:27 ` Ira Weiny
0 siblings, 2 replies; 4+ messages in thread
From: Zhaoyang Yu @ 2026-01-26 13:04 UTC (permalink / raw)
To: dan.j.williams
Cc: vishal.l.verma, dave.jiang, ira.weiny, nvdimm, linux-kernel,
gszhai, Zhaoyang Yu
The devm_kmalloc() function may return NULL when memory allocation fails.
In nd_pfn_probe() and nd_dax_probe(), the return values of devm_kmalloc()
are not checked. If pfn_sb is NULL, it will cause a NULL pointer
dereference in the subsequent calls to nd_pfn_validate().
Additionally, if the allocation fails, the devices initialized by
nd_pfn_devinit() or nd_dax_devinit() are not properly released, leading
to memory leaks.
Fix this by checking the return value of devm_kmalloc() in both functions.
If the allocation fails, use put_device() to release the initialized device
and return -ENOMEM.
Signed-off-by: Zhaoyang Yu <2426767509@qq.com>
---
drivers/nvdimm/dax_devs.c | 4 ++++
drivers/nvdimm/pfn_devs.c | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
index ba4c409ede65..aa51a9022d12 100644
--- a/drivers/nvdimm/dax_devs.c
+++ b/drivers/nvdimm/dax_devs.c
@@ -111,6 +111,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
return -ENOMEM;
}
pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+ if (!pfn_sb) {
+ put_device(dax_dev);
+ return -ENOMEM;
+ }
nd_pfn = &nd_dax->nd_pfn;
nd_pfn->pfn_sb = pfn_sb;
rc = nd_pfn_validate(nd_pfn, DAX_SIG);
diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c
index 42b172fc5576..6a69d8bfeb7c 100644
--- a/drivers/nvdimm/pfn_devs.c
+++ b/drivers/nvdimm/pfn_devs.c
@@ -635,6 +635,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
if (!pfn_dev)
return -ENOMEM;
pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+ if (!pfn_sb) {
+ put_device(pfn_dev);
+ return -ENOMEM;
+ }
nd_pfn = to_nd_pfn(pfn_dev);
nd_pfn->pfn_sb = pfn_sb;
rc = nd_pfn_validate(nd_pfn, PFN_SIG);
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe()
2026-01-26 13:04 [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe() Zhaoyang Yu
@ 2026-01-26 17:39 ` Dave Jiang
2026-01-26 20:27 ` Ira Weiny
1 sibling, 0 replies; 4+ messages in thread
From: Dave Jiang @ 2026-01-26 17:39 UTC (permalink / raw)
To: Zhaoyang Yu, dan.j.williams
Cc: vishal.l.verma, ira.weiny, nvdimm, linux-kernel, gszhai
On 1/26/26 6:04 AM, Zhaoyang Yu wrote:
> The devm_kmalloc() function may return NULL when memory allocation fails.
> In nd_pfn_probe() and nd_dax_probe(), the return values of devm_kmalloc()
> are not checked. If pfn_sb is NULL, it will cause a NULL pointer
> dereference in the subsequent calls to nd_pfn_validate().
>
> Additionally, if the allocation fails, the devices initialized by
> nd_pfn_devinit() or nd_dax_devinit() are not properly released, leading
> to memory leaks.
>
> Fix this by checking the return value of devm_kmalloc() in both functions.
> If the allocation fails, use put_device() to release the initialized device
> and return -ENOMEM.
>
> Signed-off-by: Zhaoyang Yu <2426767509@qq.com>
Please provide a Fixes tag. Otherwise LGTM.
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> drivers/nvdimm/dax_devs.c | 4 ++++
> drivers/nvdimm/pfn_devs.c | 4 ++++
> 2 files changed, 8 insertions(+)
>
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index ba4c409ede65..aa51a9022d12 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -111,6 +111,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
> return -ENOMEM;
> }
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(dax_dev);
> + return -ENOMEM;
> + }
> nd_pfn = &nd_dax->nd_pfn;
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, DAX_SIG);
> diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c
> index 42b172fc5576..6a69d8bfeb7c 100644
> --- a/drivers/nvdimm/pfn_devs.c
> +++ b/drivers/nvdimm/pfn_devs.c
> @@ -635,6 +635,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
> if (!pfn_dev)
> return -ENOMEM;
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(pfn_dev);
> + return -ENOMEM;
> + }
> nd_pfn = to_nd_pfn(pfn_dev);
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, PFN_SIG);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe()
2026-01-26 13:04 [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe() Zhaoyang Yu
2026-01-26 17:39 ` Dave Jiang
@ 2026-01-26 20:27 ` Ira Weiny
2026-01-27 3:57 ` [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULLpointer " 俞朝阳
1 sibling, 1 reply; 4+ messages in thread
From: Ira Weiny @ 2026-01-26 20:27 UTC (permalink / raw)
To: Zhaoyang Yu, dan.j.williams
Cc: vishal.l.verma, dave.jiang, ira.weiny, nvdimm, linux-kernel,
gszhai, Zhaoyang Yu
Zhaoyang Yu wrote:
> The devm_kmalloc() function may return NULL when memory allocation fails.
> In nd_pfn_probe() and nd_dax_probe(), the return values of devm_kmalloc()
> are not checked. If pfn_sb is NULL, it will cause a NULL pointer
> dereference in the subsequent calls to nd_pfn_validate().
>
> Additionally, if the allocation fails, the devices initialized by
> nd_pfn_devinit() or nd_dax_devinit() are not properly released, leading
> to memory leaks.
>
> Fix this by checking the return value of devm_kmalloc() in both functions.
> If the allocation fails, use put_device() to release the initialized device
> and return -ENOMEM.
>
> Signed-off-by: Zhaoyang Yu <2426767509@qq.com>
> ---
> drivers/nvdimm/dax_devs.c | 4 ++++
> drivers/nvdimm/pfn_devs.c | 4 ++++
> 2 files changed, 8 insertions(+)
>
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index ba4c409ede65..aa51a9022d12 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -111,6 +111,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
> return -ENOMEM;
> }
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(dax_dev);
> + return -ENOMEM;
> + }
Sorry this is a NAK.
While I don't like the implicit nature of the check... This is not
needed.
The validity of pfn_sb is checked in nd_pfn_validate()
It is unfortunate that the errno reported in that case is ENODEV rather
than ENOMEM... But I would not change that now.
> nd_pfn = &nd_dax->nd_pfn;
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, DAX_SIG);
> diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c
> index 42b172fc5576..6a69d8bfeb7c 100644
> --- a/drivers/nvdimm/pfn_devs.c
> +++ b/drivers/nvdimm/pfn_devs.c
> @@ -635,6 +635,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
> if (!pfn_dev)
> return -ENOMEM;
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(pfn_dev);
> + return -ENOMEM;
> + }
> nd_pfn = to_nd_pfn(pfn_dev);
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Same issue here.
Ira
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULLpointer dereference in nd_pfn_probe() and nd_dax_probe()
2026-01-26 20:27 ` Ira Weiny
@ 2026-01-27 3:57 ` 俞朝阳
0 siblings, 0 replies; 4+ messages in thread
From: 俞朝阳 @ 2026-01-27 3:57 UTC (permalink / raw)
To: Ira Weiny, dan.j.williams
Cc: vishal.l.verma, dave.jiang, ira.weiny, nvdimm, linux-kernel,
gszhai
Hi Ira,
Thanks for the detailed explanation.
I see now that nd_pfn_validate() already handles the NULL pfn_sb case, and the error path correctly releases the device. You are right that the patch is unnecessary.
I will drop this patch. Thanks for your time reviewing it.
Best regards,
Zhaoyang
原始邮件
发件人:Ira Weiny <ira.weiny@intel.com>
发件时间:2026年1月27日 04:27
收件人:Zhaoyang Yu <2426767509@qq.com>, dan.j.williams <dan.j.williams@intel.com>
抄送:vishal.l.verma <vishal.l.verma@intel.com>, dave.jiang <dave.jiang@intel.com>, ira.weiny <ira.weiny@intel.com>, nvdimm <nvdimm@lists.linux.dev>, linux-kernel <linux-kernel@vger.kernel.org>, gszhai <gszhai@bjtu.edu.cn>, Zhaoyang Yu <2426767509@qq.com>
主题:Re: [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULLpointer dereference in nd_pfn_probe() and nd_dax_probe()
Zhaoyang Yu wrote:
> The devm_kmalloc() function may return NULL when memory allocation fails.
> In nd_pfn_probe() and nd_dax_probe(), the return values of devm_kmalloc()
> are not checked. If pfn_sb is NULL, it will cause a NULL pointer
> dereference in the subsequent calls to nd_pfn_validate().
>
> Additionally, if the allocation fails, the devices initialized by
> nd_pfn_devinit() or nd_dax_devinit() are not properly released, leading
> to memory leaks.
>
> Fix this by checking the return value of devm_kmalloc() in both functions.
> If the allocation fails, use put_device() to release the initialized device
> and return -ENOMEM.
>
> Signed-off-by: Zhaoyang Yu <2426767509@qq.com>
> ---
> drivers/nvdimm/dax_devs.c | 4 ++++
> drivers/nvdimm/pfn_devs.c | 4 ++++
> 2 files changed, 8 insertions(+)
>
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index ba4c409ede65..aa51a9022d12 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -111,6 +111,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
> return -ENOMEM;
> }
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(dax_dev);
> + return -ENOMEM;
> + }
Sorry this is a NAK.
While I don't like the implicit nature of the check... This is not
needed.
The validity of pfn_sb is checked in nd_pfn_validate()
It is unfortunate that the errno reported in that case is ENODEV rather
than ENOMEM... But I would not change that now.
> nd_pfn = &nd_dax->nd_pfn;
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, DAX_SIG);
> diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c
> index 42b172fc5576..6a69d8bfeb7c 100644
> --- a/drivers/nvdimm/pfn_devs.c
> +++ b/drivers/nvdimm/pfn_devs.c
> @@ -635,6 +635,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
> if (!pfn_dev)
> return -ENOMEM;
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(pfn_dev);
> + return -ENOMEM;
> + }
> nd_pfn = to_nd_pfn(pfn_dev);
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Same issue here.
Ira
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-01-27 3:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-26 13:04 [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer dereference in nd_pfn_probe() and nd_dax_probe() Zhaoyang Yu
2026-01-26 17:39 ` Dave Jiang
2026-01-26 20:27 ` Ira Weiny
2026-01-27 3:57 ` [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULLpointer " 俞朝阳
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox