[PATCH v2] LoongArch: Add a check for the return value when creating the kobj

cuitao posted 1 patch 3 weeks, 2 days ago
There is a newer version of this series
arch/loongarch/kernel/env.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH v2] LoongArch: Add a check for the return value when creating the kobj
Posted by cuitao 3 weeks, 2 days ago
Add a check for the return value of kobject_create_and_add to ensure
that the kobj allocation succeeds.

Signed-off-by: cuitao <cuitao@kylinos.cn>
---
v2: Removed the unnecessary modifications.
https://lore.kernel.org/all/CAAhV-H7ECnVeOgf93SEXLHLc4wvhQMjQXDbGKCR-KXKWf8Vgtg@mail.gmail.com/
---
 arch/loongarch/kernel/env.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
index c0a5dc9aeae2..f4ae894f4219 100644
--- a/arch/loongarch/kernel/env.c
+++ b/arch/loongarch/kernel/env.c
@@ -109,6 +109,10 @@ static int __init boardinfo_init(void)
 	struct kobject *loongson_kobj;
 
 	loongson_kobj = kobject_create_and_add("loongson", firmware_kobj);
+	if (!loongson_kobj) {
+		pr_warn("loongson: Firmware registration failed.\n");
+		return -ENOMEM;
+	}
 
 	return sysfs_create_file(loongson_kobj, &boardinfo_attr.attr);
 }
-- 
2.48.1
Re: [PATCH v2] LoongArch: Add a check for the return value when creating the kobj
Posted by Huacai Chen 3 weeks, 2 days ago
On Tue, Sep 9, 2025 at 3:20 PM cuitao <cuitao@kylinos.cn> wrote:
>
> Add a check for the return value of kobject_create_and_add to ensure
> that the kobj allocation succeeds.
>
> Signed-off-by: cuitao <cuitao@kylinos.cn>
> ---
> v2: Removed the unnecessary modifications.
> https://lore.kernel.org/all/CAAhV-H7ECnVeOgf93SEXLHLc4wvhQMjQXDbGKCR-KXKWf8Vgtg@mail.gmail.com/
> ---
>  arch/loongarch/kernel/env.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/arch/loongarch/kernel/env.c b/arch/loongarch/kernel/env.c
> index c0a5dc9aeae2..f4ae894f4219 100644
> --- a/arch/loongarch/kernel/env.c
> +++ b/arch/loongarch/kernel/env.c
> @@ -109,6 +109,10 @@ static int __init boardinfo_init(void)
>         struct kobject *loongson_kobj;
>
>         loongson_kobj = kobject_create_and_add("loongson", firmware_kobj);
> +       if (!loongson_kobj) {
> +               pr_warn("loongson: Firmware registration failed.\n");
The check is useful but the warning is unnecessary, because it is not
fatal for system use.

Huacai

> +               return -ENOMEM;
> +       }
>
>         return sysfs_create_file(loongson_kobj, &boardinfo_attr.attr);
>  }
> --
> 2.48.1
>