:: commit 8620b970e9379f0d7b6e2369328d1397000e02ec

Mintsuki <mintsuki@protonmail.com> — 2025-10-13 10:22

parents: cc7d42f309

misc: Prevent some memory leaks after get_device_tree_blob(..., 0) made to allocate

diff --git a/common/sys/cpu_riscv.c b/common/sys/cpu_riscv.c
index 58feec6a..dfe38593 100644
--- a/common/sys/cpu_riscv.c
+++ b/common/sys/cpu_riscv.c
@@ -228,6 +228,7 @@ void init_riscv(const char *config) {
     void *fdt = get_device_tree_blob(config, 0);
     if (fdt != NULL) {
         init_riscv_fdt(fdt);
+        pmm_free(fdt, fdt_totalsize(fdt));
     } else if (acpi_get_rsdp()) {
         init_riscv_acpi();
     } else {
diff --git a/common/sys/smp.c b/common/sys/smp.c
index d40d1d3d..36b95e4e 100644
--- a/common/sys/smp.c
+++ b/common/sys/smp.c
@@ -515,7 +515,7 @@ static struct limine_mp_info *try_acpi_smp(size_t   *cpu_count,
     return ret;
 }
 
-static struct limine_mp_info *try_dtb_smp(const char *config,
+static struct limine_mp_info *try_dtb_smp( void *dtb,
                                            size_t   *cpu_count,
                                            uint64_t *_bsp_mpidr,
                                            pagemap_t pagemap,
@@ -523,8 +523,6 @@ static struct limine_mp_info *try_dtb_smp(const char *config,
                                            uint64_t  tcr,
                                            uint64_t  sctlr,
                                            uint64_t  hhdm_offset) {
-    void *dtb = get_device_tree_blob(config, 0);
-
     uint64_t bsp_mpidr;
     asm volatile ("mrs %0, mpidr_el1" : "=r"(bsp_mpidr));
 
@@ -713,11 +711,14 @@ struct limine_mp_info *init_smp(const char *config,
         return info;
 
     // No RSDP means no ACPI, try device trees in that case.
-    if (get_device_tree_blob(config, 0)
-                    && (info = try_dtb_smp(
-                                config, cpu_count, bsp_mpidr, pagemap,
-                                mair, tcr, sctlr, hhdm_offset)))
+    void *dtb = get_device_tree_blob(config, 0);
+    if (dtb) {
+        info = try_dtb_smp(dtb,
+                           cpu_count, bsp_mpidr, pagemap,
+                           mair, tcr, sctlr, hhdm_offset);
+        pmm_free(dtb, fdt_totalsize(dtb));
         return info;
+    }
 
     printv("Failed to figure out how to start APs.");
 
tab: 248 wrap: offon