[PATCH] x86: pmem: Add NULL check for platform_device_alloc

Li Jun posted 1 patch 5 days, 16 hours ago
arch/x86/kernel/pmem.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] x86: pmem: Add NULL check for platform_device_alloc
Posted by Li Jun 5 days, 16 hours ago
Add proper error handling for the case when platform_device_alloc()
returns NULL due to memory allocation failure. This prevents a
potential NULL pointer dereference when trying to use the pdev pointer
without checking if allocation succeeded.

Signed-off-by: Li Jun <lijun01@kylinos.cn>
---
 arch/x86/kernel/pmem.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/x86/kernel/pmem.c b/arch/x86/kernel/pmem.c
index 23154d24b117..04fb221716ff 100644
--- a/arch/x86/kernel/pmem.c
+++ b/arch/x86/kernel/pmem.c
@@ -27,6 +27,8 @@ static __init int register_e820_pmem(void)
 	 * simply here to trigger the module to load on demand.
 	 */
 	pdev = platform_device_alloc("e820_pmem", -1);
+	if (!pdev)
+		return -ENOMEM;
 
 	rc = platform_device_add(pdev);
 	if (rc)
-- 
2.25.1
[tip: x86/cleanups] x86/pmem: Check for platform_device_alloc() retval
Posted by tip-bot2 for Li Jun 3 days, 10 hours ago
The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     17b22e7a389e02e72912ab2904eac08da613c49e
Gitweb:        https://git.kernel.org/tip/17b22e7a389e02e72912ab2904eac08da613c49e
Author:        Li Jun <lijun01@kylinos.cn>
AuthorDate:    Tue, 02 Jun 2026 18:07:11 +08:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Thu, 04 Jun 2026 08:31:56 -07:00

x86/pmem: Check for platform_device_alloc() retval

Add proper error handling for the case when platform_device_alloc() returns
NULL due to memory allocation failure. This prevents a potential NULL pointer
dereference when trying to use the pdev pointer without checking if allocation
succeeded.

  [ bp: Massage commit message. ]

Signed-off-by: Li Jun <lijun01@kylinos.cn>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://patch.msgid.link/20260602100711.2542568-1-lijun01@kylinos.cn
---
 arch/x86/kernel/pmem.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/x86/kernel/pmem.c b/arch/x86/kernel/pmem.c
index 23154d2..04fb221 100644
--- a/arch/x86/kernel/pmem.c
+++ b/arch/x86/kernel/pmem.c
@@ -27,6 +27,8 @@ static __init int register_e820_pmem(void)
 	 * simply here to trigger the module to load on demand.
 	 */
 	pdev = platform_device_alloc("e820_pmem", -1);
+	if (!pdev)
+		return -ENOMEM;
 
 	rc = platform_device_add(pdev);
 	if (rc)