On Thu, Nov 14, 2024 at 03:19:33PM +0200, Nikolay Borisov wrote:
For the older families we have a hard upper bound so we want to ensure that
the size in the header is strictly <= than buf_size, which in turn must be
<= max_size .
i.e Is it not valid to have sh_psize < buf_size rather than strictly equal ?
Let's look at all possible cases:
* sh_psize > min_t(sh_psize, buf_size) == buf_size -- means the buffer is
truncated so the patch is incomplete
* sh_psize < min_t(sh_psize, buf_size) == buf_size -- this is actually ok
because we're working with the whole buffer and there can be other patches
following. Now I remember why I had ">" there.
* sh_psize > min_t(u32, buf_size, max_size) == buf_size -- truncated buffer
* sh_psize < min_t(u32, buf_size, max_size) == buf_size -- that's ok
* sh_psize > min_t(u32, buf_size, max_size) == max_size -- some mismatch, fail
* sh_psize < min_t(u32, buf_size, max_size) == max_size -- ditto.
So this needs more staring and I need to make it more readable.
Btw, one more spot:
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c
index 01ea25f31c0c..7554d83f00e6 100644
--- a/arch/x86/kernel/cpu/microcode/amd.c
+++ b/arch/x86/kernel/cpu/microcode/amd.c
@@ -303,7 +303,7 @@ static bool __verify_patch_size(u32 sh_psize, size_t buf_size)
break;
default:
WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
- return 0;
+ return false;
}
return sh_psize == min_t(u32, buf_size, max_size);
---
IOW, I'm thinking about something like this (pasting the whole function here):
static bool __verify_patch_size(u32 sh_psize, size_t buf_size)
{
u8 family = x86_family(bsp_cpuid_1_eax);
u32 max_size;
if (family >= 0x15)
goto ret;
#define F1XH_MPB_MAX_SIZE 2048
#define F14H_MPB_MAX_SIZE 1824
switch (family) {
case 0x10 ... 0x12:
max_size = F1XH_MPB_MAX_SIZE;
break;
case 0x14:
max_size = F14H_MPB_MAX_SIZE;
break;
default:
WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
return false;
}
if (sh_psize != max_size)
return false;
ret:
/* Working with the whole buffer so < is ok. */
return sh_psize <= buf_size;
}