Skip to content

Commit

Permalink
Fix CPU_SET dynamic allocation and leak (#205)
Browse files Browse the repository at this point in the history
The initial implementation had a number of issues:
- The allocation of the CPU_SET should be checked for a NULL return.
- The CPU_*_S macros should be used when working with dynamic sets.
- The CPU_SET needs to be cleared via CPU_ZERO_S before use.
- Dynamic CPU_SETs need to be freed after use.
- The __riscv_hwprobe syscall is expecting a set *size* not a *count*.
  • Loading branch information
prashanthswami authored Nov 28, 2023
1 parent ef63460 commit 9d80992
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/riscv/linux/riscv-hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
*uarch = cpuinfo_uarch_unknown;

/* Create a CPU set with this processor flagged. */
const size_t cpu_set_size = processor + 1;
cpu_set_t* cpu_set = CPU_ALLOC(cpu_set_size);
CPU_SET(processor, cpu_set);
const size_t cpu_count = processor + 1;
cpu_set_t* cpu_set = CPU_ALLOC(cpu_count);
if (cpu_set == NULL) {
cpuinfo_log_warning("failed to allocate space for cpu_set");
return;
}

const size_t cpu_set_size = CPU_ALLOC_SIZE(cpu_count);
CPU_ZERO_S(cpu_set_size, cpu_set);
CPU_SET_S(processor, cpu_set_size, cpu_set);

/* Request all available information from hwprobe. */
int ret = __riscv_hwprobe(pairs, pairs_count,
cpu_set_size, (unsigned long*)cpu_set,
0 /* flags */);
if (ret < 0) {
cpuinfo_log_warning("failed to get hwprobe information, err: %d", ret);
return;
goto cleanup;
}

/**
Expand Down Expand Up @@ -59,4 +66,7 @@ void cpuinfo_riscv_linux_decode_vendor_uarch_from_hwprobe(
}
cpuinfo_riscv_decode_vendor_uarch(vendor_id, arch_id, imp_id,
vendor, uarch);

cleanup:
CPU_FREE(cpu_set);
}

0 comments on commit 9d80992

Please sign in to comment.