Skip to content

Commit 5cdd2de

Browse files
jjuhlIngo Molnar
authored andcommitted
x86/microcode: Fix double vfree() and remove redundant pointer checks before vfree()
In arch/x86/kernel/microcode_intel.c::generic_load_microcode() we have this: while (leftover) { ... if (get_ucode_data(mc, ucode_ptr, mc_size) || microcode_sanity_check(mc) < 0) { vfree(mc); break; } ... } if (mc) vfree(mc); This will cause a double free of 'mc'. This patch fixes that by just removing the vfree() call in the loop since 'mc' will be freed nicely just after we break out of the loop. There's also a second change in the patch. I noticed a lot of checks for pointers being NULL before passing them to vfree(). That's completely redundant since vfree() deals gracefully with being passed a NULL pointer. Removing the redundant checks yields a nice size decrease for the object file. Size before the patch: text data bss dec hex filename 4578 240 1032 5850 16da arch/x86/kernel/microcode_intel.o Size after the patch: text data bss dec hex filename 4489 240 984 5713 1651 arch/x86/kernel/microcode_intel.o Signed-off-by: Jesper Juhl <[email protected]> Acked-by: Tigran Aivazian <[email protected]> Cc: Shaohua Li <[email protected]> LKML-Reference: <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent eda4b71 commit 5cdd2de

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

arch/x86/kernel/microcode_intel.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
364364

365365
/* For performance reasons, reuse mc area when possible */
366366
if (!mc || mc_size > curr_mc_size) {
367-
if (mc)
368-
vfree(mc);
367+
vfree(mc);
369368
mc = vmalloc(mc_size);
370369
if (!mc)
371370
break;
@@ -374,13 +373,11 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
374373

375374
if (get_ucode_data(mc, ucode_ptr, mc_size) ||
376375
microcode_sanity_check(mc) < 0) {
377-
vfree(mc);
378376
break;
379377
}
380378

381379
if (get_matching_microcode(&uci->cpu_sig, mc, new_rev)) {
382-
if (new_mc)
383-
vfree(new_mc);
380+
vfree(new_mc);
384381
new_rev = mc_header.rev;
385382
new_mc = mc;
386383
mc = NULL; /* trigger new vmalloc */
@@ -390,12 +387,10 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
390387
leftover -= mc_size;
391388
}
392389

393-
if (mc)
394-
vfree(mc);
390+
vfree(mc);
395391

396392
if (leftover) {
397-
if (new_mc)
398-
vfree(new_mc);
393+
vfree(new_mc);
399394
state = UCODE_ERROR;
400395
goto out;
401396
}
@@ -405,8 +400,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
405400
goto out;
406401
}
407402

408-
if (uci->mc)
409-
vfree(uci->mc);
403+
vfree(uci->mc);
410404
uci->mc = (struct microcode_intel *)new_mc;
411405

412406
pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",

0 commit comments

Comments
 (0)