x86, amd_ucode: Support multiple container files appended together
PATCH v3 1/1 x86, amd_ucode: Support multiple container files appended together
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]
Other changes introduced:
- Define HDR_SIZE's explicitly for code clarity.
Changes in V3
- Change approach that used AMD_MAX_CONT_APPEND to handle edge case
- No need of a 'found' variable
- return 0 for success and not AMD_UCODE_APPLY_SUCCESS as someone could just as easily break things by redefining it as bool
- No need of 'header' in container_fast_forward
- Handle more corner cases in container_fast_forward
- Fix code style issues
Changes in V2
- per Jan suggestion
- return bool_t from find_equiv_cpu_id
=> drop the respective initializers of equiv_cpu_id in the callers
- Reduce number of casts by using void * for passing raw binary whenever possible
- Need size_left >= size check in container_fast_forward
- Use error value returned from container_fast_forward in cpu_request_microcode
- If changing code around 'error = save_error', make sure the behavior for single container files does not change
- per Boris suggestion
- No need use pointer type for tot_size
=> We can remove this from the caller too as it is unnecessary
- if ( mpbuf->len + CONT_HDR_SIZE >= *tot_size ) check can be removed
- Fix logic
- if the first container is corrupted for some reason and returns error then we return pre-maturely. Instead, now we at least (try to) forward to next container and look for patches there as well
- Using AMD_MAX_CONT_APPEND as loop condition check simply because I wanted to avoid using a while (1) which would also work just as well
- This check makes some logical sense too as there are only 2 available AMD containers out there now. So if we did not find correct patch by then, we might as well stop trying.
--- xen/arch/x86/microcode_amd.c | 150 +++++++++++++++++++++++++++++++++++------- 1 file changed, 128 insertions(+), 22 deletions(-)
xen/arch/x86/microcode_amd.c +128 -22
@@ -29,6 +29,9 @@ #define pr_debug(x...) ((void)0) +#define CONT_HDR_SIZE 12 +#define SECTION_HDR_SIZE 8 +struct __packed equiv_cpu_entry {uint32_t installed_cpu;uint32_t fixed_errata_mask;
@@ -124,30 +127,41 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static bool_t find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return 0 ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +return 1 ; + } + } + +return 0 ; +} +static bool_t microcode_fits (const struct microcode_amd *mc_amd,int cpu) {struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu);const struct microcode_header_amd *mc_header = mc_amd->mpb;const struct equiv_cpu_entry *equiv_cpu_table = mc_amd->equiv_cpu_table;unsigned int current_cpu_id; -unsigned int equiv_cpu_id =0x0 ; -unsigned int i; +unsigned int equiv_cpu_id;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); current_cpu_id =cpuid_eax (0x00000001 ); -for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) - { -if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) - { - equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; -break ; - } - } - -if ( !equiv_cpu_id ) +if ( !find_equiv_cpu_id (equiv_cpu_table, current_cpu_id, &equiv_cpu_id) )return 0 ;if ( (mc_header->processor_rev_id) != equiv_cpu_id )
@@ -236,7 +250,14 @@ static int get_ucode_from_buffer_amd( mpbuf = (const struct mpbhdr *)&bufp[off];if ( mpbuf->type !=UCODE_UCODE_TYPE ) { -printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +/* +* In a situation where ucode update has succeeded; +* but there is a subsequent container file being parsed, +* then, there is no need of this ERR message to be printed. +*/ +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) +printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +return -EINVAL ; }
@@ -260,7 +281,7 @@ static int get_ucode_from_buffer_amd( }memcpy (mc_amd->mpb, mpbuf->data, mpbuf->len); - *offset = off + mpbuf->len +8 ; + *offset = off + mpbuf->len +SECTION_HDR_SIZE ; pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID %#x rev %#x\n", raw_smp_processor_id(), bufsize, mpbuf->len, off,
@@ -272,14 +293,13 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, +const void *data,size_t *offset) { +uint32_t *buf = (uint32_t *) (data + *offset);const struct mpbhdr *mpbuf = (const struct mpbhdr *)&buf[1 ]; -/* No more data */ -if ( mpbuf->len +12 >= *offset ) -return -EINVAL ; + *offset += mpbuf->len +CONT_HDR_SIZE ;/* add header length */ if ( mpbuf->type !=UCODE_EQUIV_CPU_TABLE_TYPE ) {
@@ -303,7 +323,33 @@ static int install_equiv_cpu_table(memcpy (mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len); mc_amd->equiv_cpu_table_size = mpbuf->len; - *offset = mpbuf->len +12 ;/* add header length */ +return 0 ; +} + +static int container_fast_forward (const void *data,size_t size_left,size_t *offset) +{ +size_t size; + +while ( size_left ) + { +if ( size_left <SECTION_HDR_SIZE ) +return -EINVAL ; + +if ( *(const uint32_t *)(data + *offset) ==UCODE_MAGIC && + *(const uint32_t *)(data + *offset +4 ) == +UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = *(uint32_t *)(data + *offset +4 ) +SECTION_HDR_SIZE ; +if ( size_left < size ) +return -EINVAL ; + + size_left -= size; + *offset += size; + } + +if ( !size_left ) +return -EINVAL ;return 0 ; }
@@ -311,14 +357,18 @@ static int install_equiv_cpu_table(static int cpu_request_microcode (int cpu,const void *buf,size_t bufsize) {struct microcode_amd *mc_amd, *mc_old; -size_t offset = bufsize; +size_t offset =0 ;size_t last_offset, applied_offset =0 ;int error =0 , save_error =1 ;struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu); +unsigned int current_cpu_id; +unsigned int equiv_cpu_id;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); + current_cpu_id =cpuid_eax (0x00000001 ); +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) {printk (KERN_ERR "microcode: Wrong microcode patch file magic\n" );
@@ -334,7 +384,40 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( offset < bufsize ) + { + error =install_equiv_cpu_table (mc_amd, buf, &offset); + +if ( !error && +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id) ) +break ; + +/* +* Could happen as we advance 'offset' early +* in install_equiv_cpu_table +*/ +if ( offset > bufsize ) + { +printk (KERN_ERR "microcode: Microcode buffer overrun\n" ); +return -EINVAL ; + } + + error =container_fast_forward (buf, bufsize - offset, &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + goto out; + } + } +if ( error ) {xfree (mc_amd);
@@ -379,12 +462,35 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); -if ( save_error ) +/* +* If there happens to be multiple container files and if patch +* update succeeded on an earlier container itself, a stale error +* val is returned from get_ucode_from_buffer_amd. Since we already +* succeeded in patch application, force error = 0 +*/ +if ( offset < bufsize ) + error =0 ; +else if ( save_error ) error = save_error; }if ( save_error ) { +/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as +* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ; +xfree (mc_amd); uci->mc.mc_amd = mc_old; }
Signature · 1 line
1.7.9.5
Quoted 245 lines · Aravind Gopalakrishnan
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]Other changes introduced:
- Define HDR_SIZE's explicitly for code clarity.Changes in V3
- Change approach that usedAMD_MAX_CONT_APPENDto handle edge case
- No need of a 'found' variable
- return 0 for success and notAMD_UCODE_APPLY_SUCCESSas someone could just as easily break things by redefining it as bool
- No need of 'header' incontainer_fast_forward
- Handle more corner cases incontainer_fast_forward
- Fix code style issuesChanges in V2
- per Jan suggestion
- returnbool_tfromfind_equiv_cpu_id
=> drop the respective initializers ofequiv_cpu_idin the callers
- Reduce number of casts by using void * for passing raw binary whenever possible
- Needsize_left>= size check incontainer_fast_forward
- Use error value returned fromcontainer_fast_forwardincpu_request_microcode
- If changing code around 'error =save_error', make sure the behavior for single container files does not change- per Boris suggestion
- No need use pointer type fortot_size
=> We can remove this from the caller too as it is unnecessary
- if (mpbuf->len+CONT_HDR_SIZE>=*tot_size) check can be removed- Fix logic
- if the first container is corrupted for some reason and returns error then we return pre-maturely. Instead, now we at least (try to) forward to next container and look for patches there as well
- UsingAMD_MAX_CONT_APPENDas loop condition check simply because I wanted to avoid using a while (1) which would also work just as well
- This check makes some logical sense too as there are only 2 available AMD containers out there now. So if we did not find correct patch by then, we might as well stop trying.Signed-off-by Aravind Gopalakrishnan <[email protected]>--- xen/arch/x86/microcode_amd.c | 150 +++++++++++++++++++++++++++++++++++------- 1 file changed, 128 insertions(+), 22 deletions(-)xen/arch/x86/microcode_amd.c +94
-21@@ -29,6 +29,9 @@ #define pr_debug(x...) ((void)0) +#define CONT_HDR_SIZE 12 +#define SECTION_HDR_SIZE 8 +struct __packed equiv_cpu_entry {uint32_t installed_cpu;uint32_t fixed_errata_mask;@@ -124,30 +127,41 @@ static bool_t verify_patch_size(uint32_t patch_size)return (patch_size <= max_size); } +static bool_t find_equiv_cpu_id (const struct equiv_cpu_entry *equiv_cpu_table, +unsigned int current_cpu_id, +unsigned int *equiv_cpu_id) +{ +unsigned int i; + +if ( !equiv_cpu_table ) +return 0 ; + +for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) + { +if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) + { + *equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; +return 1 ; + } + } + +return 0 ; +} +static bool_t microcode_fits (const struct microcode_amd *mc_amd,int cpu) {struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu);const struct microcode_header_amd *mc_header = mc_amd->mpb;const struct equiv_cpu_entry *equiv_cpu_table = mc_amd->equiv_cpu_table;unsigned int current_cpu_id; -unsigned int equiv_cpu_id =0x0 ; -unsigned int i; +unsigned int equiv_cpu_id;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); current_cpu_id =cpuid_eax (0x00000001 ); -for ( i =0 ; equiv_cpu_table[i].installed_cpu !=0 ; i++ ) - { -if ( current_cpu_id == equiv_cpu_table[i].installed_cpu ) - { - equiv_cpu_id = equiv_cpu_table[i].equiv_cpu &0xffff ; -break ; - } - } - -if ( !equiv_cpu_id ) +if ( !find_equiv_cpu_id (equiv_cpu_table, current_cpu_id, &equiv_cpu_id) )return 0 ;if ( (mc_header->processor_rev_id) != equiv_cpu_id )@@ -236,7 +250,14 @@ static int get_ucode_from_buffer_amd( mpbuf = (const struct mpbhdr *)&bufp[off];if ( mpbuf->type !=UCODE_UCODE_TYPE ) { -printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +/* +* In a situation where ucode update has succeeded; +* but there is a subsequent container file being parsed, +* then, there is no need of this ERR message to be printed. +*/ +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) +printk (KERN_ERR "microcode: Wrong microcode payload type field\n" ); +return -EINVAL ; }@@ -260,7 +281,7 @@ static int get_ucode_from_buffer_amd( }memcpy (mc_amd->mpb, mpbuf->data, mpbuf->len); - *offset = off + mpbuf->len +8 ; + *offset = off + mpbuf->len +SECTION_HDR_SIZE ; pr_debug("microcode: CPU%d size %zu, block size %u offset %zu equivID %#x rev %#x\n", raw_smp_processor_id(), bufsize, mpbuf->len, off,@@ -272,14 +293,13 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, +const void *data,size_t *offset) { +uint32_t *buf = (uint32_t *) (data + *offset);const struct mpbhdr *mpbuf = (const struct mpbhdr *)&buf[1 ]; -/* No more data */ -if ( mpbuf->len +12 >= *offset ) -return -EINVAL ; + *offset += mpbuf->len +CONT_HDR_SIZE ;/* add header length */ if ( mpbuf->type !=UCODE_EQUIV_CPU_TABLE_TYPE ) {@@ -303,7 +323,33 @@ static int install_equiv_cpu_table(memcpy (mc_amd->equiv_cpu_table, mpbuf->data, mpbuf->len); mc_amd->equiv_cpu_table_size = mpbuf->len; - *offset = mpbuf->len +12 ;/* add header length */ +return 0 ; +} + +static int container_fast_forward (const void *data,size_t size_left,size_t *offset) +{ +size_t size; + +while ( size_left ) + { +if ( size_left <SECTION_HDR_SIZE ) +return -EINVAL ; + +if ( *(const uint32_t *)(data + *offset) ==UCODE_MAGIC && + *(const uint32_t *)(data + *offset +4 ) == +UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = *(uint32_t *)(data + *offset +4 ) +SECTION_HDR_SIZE ; +if ( size_left < size ) +return -EINVAL ; + + size_left -= size; + *offset += size; + } + +if ( !size_left ) +return -EINVAL ;return 0 ; }@@ -311,14 +357,18 @@ static int install_equiv_cpu_table(static int cpu_request_microcode (int cpu,const void *buf,size_t bufsize) {struct microcode_amd *mc_amd, *mc_old; -size_t offset = bufsize; +size_t offset =0 ;size_t last_offset, applied_offset =0 ;int error =0 , save_error =1 ;struct ucode_cpu_info *uci = &per_cpu (ucode_cpu_info, cpu); +unsigned int current_cpu_id; +unsigned int equiv_cpu_id;/* We should bind the task to the CPU */ BUG_ON (cpu !=raw_smp_processor_id ()); + current_cpu_id =cpuid_eax (0x00000001 ); +if ( *(const uint32_t *)buf !=UCODE_MAGIC ) {printk (KERN_ERR "microcode: Wrong microcode patch file magic\n" );@@ -334,7 +384,40 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( offset < bufsize ) + { + error =install_equiv_cpu_table (mc_amd, buf, &offset); + +if ( !error && +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id) ) +break ; + +/* +* Could happen as we advance 'offset' early +* in install_equiv_cpu_table +*/ +if ( offset > bufsize ) + { +printk (KERN_ERR "microcode: Microcode buffer overrun\n" ); +return -EINVAL ; + } +
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 420
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+ error =container_fast_forward (buf, bufsize - offset, &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + goto out; + } + } +
I just realized that I don't understand something: if you have two merged container files, both having an entry for current processor in the equivalence table but only the second file having the actual patch (or at least the patch with higher version), will we get to load that patch?
It seems to me that in this case we will break from the loop above pointing to microcode data in the first container. We then enter get_ucode_from_buffer_amd() loop and when we reach the second container that routine will return -EINVAL when it sees that type is not UCODE_UCODE_TYPE. It won't print the warning if sees UCODE_MAGIC but still gives back an error.
In other words, do we need to somehow fast-forward over the second header in this loop as well?
-boris
Quoted 40 lines
xen/arch/x86/microcode_amd.c +24
-1if ( error ) {xfree (mc_amd);@@ -379,12 +462,35 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); -if ( save_error ) +/* +* If there happens to be multiple container files and if patch +* update succeeded on an earlier container itself, a stale error +* val is returned from get_ucode_from_buffer_amd. Since we already +* succeeded in patch application, force error = 0 +*/ +if ( offset < bufsize ) + error =0 ; +else if ( save_error ) error = save_error; }if ( save_error ) { +/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as +* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ; +xfree (mc_amd); uci->mc.mc_amd = mc_old; }
Quoted 30 lines · Boris Ostrovsky
Quoted 30 lines
xen/arch/x86/microcode_amd.c +28
-0+/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ + while ( offset < bufsize ) + { + error = install_equiv_cpu_table(mc_amd, buf, &offset); + + if ( !error && + find_equiv_cpu_id(mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id) ) + break; + + /* + * Could happen as we advance 'offset' early + * in install_equiv_cpu_table + */ + if ( offset > bufsize ) + { + printk(KERN_ERR "microcode: Microcode buffer overrun\n"); + return -EINVAL; + } + + error = container_fast_forward(buf, bufsize - offset, &offset); + if ( error ) + { + printk(KERN_ERR "microcode: CPU%d incorrect or corruptcontainer file\n"
Quoted 9 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 420
Quoted 6 lines
xen/arch/x86/microcode_amd.c +6
-0+"microcode: Failed to update patch level. " + "Current lvl:%#x\n", cpu, uci->cpu_sig.rev); + goto out; + } + } +I just realized that I don't understand something: if you have two merged container files, both having an entry for current processor in the equivalence table but only the second file having the actual patch (or at least the patch with higher version), will we get to load that patch?
We will not come across such a situation:
One container has patch files for families 10h,11h,12h,14h and the other has patches for 15h.
So there will be an equiv_cpu_id match in (at least and) only *one* of the containers.
(If there ever are patches for 16h, then I suspect it will have a separate container of it's own. So there will not be any clashes with older containers)
Besides, the patches themselves are not incremental, i.e;
If there is a newer patch, then it handles all errata/bugs that were handled by the previous patch and then some. Which means, you will not have a container that has two patches for the same processor.
So, *strictly speaking*, even the loop to get_ucode_from_buffer_amd() till the end of buffer is not necessary once we have already applied a patch.
But, to change this behavior now will need more logic rework..
Then again, if it works fine right now, would we really want to change this behavior?
Thanks,
-Aravind.
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.06.14 at 21:34, <[email protected]> wrote:
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]Other changes introduced:
- Define HDR_SIZE's explicitly for code clarity.
Quoted 8 lines
Changes in V3
- Change approach that usedAMD_MAX_CONT_APPENDto handle edge case
- No need of a 'found' variable
- return 0 for success and notAMD_UCODE_APPLY_SUCCESSas someone could just as easily break things by redefining it as bool
- No need of 'header' incontainer_fast_forward
- Handle more corner cases incontainer_fast_forward
- Fix code style issues
Can you please get used to put this revision log after the first --- marker? It doesn't belong in ti actual commit message.
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 299
Quoted 9 lines
xen/arch/x86/microcode_amd.c +2
-1@@ -272,14 +293,13 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, +const void *data,size_t *offset) { +uint32_t *buf = (uint32_t *) (data + *offset);
I know I complained about this or a similar pointless cast for the previous version.
Quoted 5 lines
xen/arch/x86/microcode_amd.c +5
-0+static int container_fast_forward (const void *data,size_t size_left,size_t *offset) +{ +size_t size; + +while ( size_left )
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 343
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+ { +if ( size_left <SECTION_HDR_SIZE ) +return -EINVAL ; + +if ( *(const uint32_t *)(data + *offset) ==UCODE_MAGIC && + *(const uint32_t *)(data + *offset +4 ) == +UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = *(uint32_t *)(data + *offset +4 ) +SECTION_HDR_SIZE ;
Now these three casts+derefs surely warrant a "const uint32_t *" variable, making the resulting code quite a bit easier to read.
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 348
Quoted 5 lines
xen/arch/x86/microcode_amd.c +5
-0+if ( size_left < size ) +return -EINVAL ; + + size_left -= size; + *offset += size;
Endless loop if size ends up being zero? (If you do such verifications at all, you should perhaps be as strict as possible, i.e. if size is required to be a multiple of 4 [which I think it is], you should also check that.)
Quoted 28 lines
xen/arch/x86/microcode_amd.c +22
-1@@ -334,7 +384,40 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( offset < bufsize ) + { + error =install_equiv_cpu_table (mc_amd, buf, &offset); + +if ( !error && +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id) ) +break ; + +/* +* Could happen as we advance 'offset' early +* in install_equiv_cpu_table +*/ +if ( offset > bufsize ) + { +printk (KERN_ERR "microcode: Microcode buffer overrun\n" ); +return -EINVAL ;
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void · line 419
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+ } + + error =container_fast_forward (buf, bufsize - offset, &offset); +if ( error ) + { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + goto out; + }
So the immediately preceding error path used just "return" - why are they different (and I'm not asking about the printk())?
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 467
Quoted 8 lines
xen/arch/x86/microcode_amd.c +3
-1@@ -379,12 +462,35 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); -if ( save_error ) +/* +* If there happens to be multiple container files and if patch +* update succeeded on an earlier container itself, a stale error
Do you perhaps mean bogus instead of stale?
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 474
Quoted 7 lines
xen/arch/x86/microcode_amd.c +6
-0+* val is returned from get_ucode_from_buffer_amd. Since we already +* succeeded in patch application, force error = 0 +*/ +if ( offset < bufsize ) + error =0 ; +else if ( save_error ) error = save_error;
And still my question stands: Since you don't look at further containers if you found a match and successfully applied the updated, why is this change needed (or is the comment saying the wrong thing)?
Quoted 8 lines
xen/arch/x86/microcode_amd.c +4
-0}if ( save_error ) { +/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 492
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ;
Same for this change/comment.
Jan
Quoted 17 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.06.14 at 21:34, <[email protected]> wrote:
This patch adds support for parsing through multiple AMD container binaries concatenated together. It is a feature already present in Linux. Link to linux patch:
http://lkml.kernel.org/r/[email protected]Other changes introduced:
- Define HDR_SIZE's explicitly for code clarity.Quoted 8 lines
Changes in V3
- Change approach that usedAMD_MAX_CONT_APPENDto handle edge case
- No need of a 'found' variable
- return 0 for success and notAMD_UCODE_APPLY_SUCCESSas someone could just as easily break things by redefining it as bool
- No need of 'header' incontainer_fast_forward
- Handle more corner cases incontainer_fast_forward
- Fix code style issues
Quoted 2 lines
Can you please get used to put this revision log after the first --- marker? It doesn't belong in ti actual commit message.
Okay.
Quoted 11 lines
xen/arch/x86/microcode_amd.c static int get_ucode_from_buffer_amd( · line 299
Quoted 9 lines
xen/arch/x86/microcode_amd.c +2
-1@@ -272,14 +293,13 @@ static int get_ucode_from_buffer_amd(static int install_equiv_cpu_table (struct microcode_amd *mc_amd, -const uint32_t *buf, +const void *data,size_t *offset) { +uint32_t *buf = (uint32_t *) (data + *offset);I know I complained about this or a similar pointless cast for the previous version.
Yes, I thought I'll handle it as a separate cleanup patch as it was not directly related to the subject of the patch. But it's a small change, so I'll do this and include it in the commit message as well..
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/microcode_amd.c +9
-0+static int container_fast_forward (const void *data,size_t size_left,size_t *offset) +{ +size_t size; + +while ( size_left ) + { +if ( size_left <SECTION_HDR_SIZE ) +return -EINVAL ; +
Quoted 8 lines
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 343
Quoted 6 lines
xen/arch/x86/microcode_amd.c +6
-0+if ( *(const uint32_t *)(data + *offset) ==UCODE_MAGIC && + *(const uint32_t *)(data + *offset +4 ) == +UCODE_EQUIV_CPU_TABLE_TYPE ) +break ; + + size = *(uint32_t *)(data + *offset +4 ) +SECTION_HDR_SIZE ;Now these three casts+derefs surely warrant a "const
uint32_t*" variable, making the resulting code quite a bit easier to read.
Okay. Will just use the bits in V2 with a const qualifier
Quoted 7 lines
xen/arch/x86/microcode_amd.c static int install_equiv_cpu_table( · line 348
Quoted 5 lines
xen/arch/x86/microcode_amd.c +5
-0+if ( size_left < size ) +return -EINVAL ; + + size_left -= size; + *offset += size;Endless loop if size ends up being zero? (If you do such verifications at all, you should perhaps be as strict as possible,
Sorry about that (Again). Will fix.
Quoted 3 lines
i.e. if size is
required to be a multiple of 4 [which I think it is], you should also check that.)
I don't think that's a rule per-se.
#define F16H_MPB_MAX_SIZE 3458
Which is indivisible by 4.
Quoted 32 lines
Quoted 32 lines
xen/arch/x86/microcode_amd.c +26
-1@@ -334,7 +384,40 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) goto out; } - error =install_equiv_cpu_table (mc_amd, buf, &offset); +/* +* Multiple container file support: +* 1. check if this container file has equiv_cpu_id match +* 2. If not, fast-fwd to next container file +*/ +while ( offset < bufsize ) + { + error =install_equiv_cpu_table (mc_amd, buf, &offset); + +if ( !error && +find_equiv_cpu_id (mc_amd->equiv_cpu_table, current_cpu_id, + &equiv_cpu_id) ) +break ; + +/* +* Could happen as we advance 'offset' early +* in install_equiv_cpu_table +*/ +if ( offset > bufsize ) + { +printk (KERN_ERR "microcode: Microcode buffer overrun\n" ); +return -EINVAL ; + } + + error =container_fast_forward (buf, bufsize - offset, &offset); +if ( error )
Quoted 8 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 418
Quoted 6 lines
xen/arch/x86/microcode_amd.c +6
-0+ { +printk (KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n" +"microcode: Failed to update patch level. " +"Current lvl:%#x\n" , cpu, uci->cpu_sig.rev); + goto out; + }So the immediately preceding error path used just "return" - why are they different (and I'm not asking about the
printk())?
Ok, Shall use one or the other.. (Probably the goto as I see that's the favored one in this function)
Quoted 9 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 467
Quoted 8 lines
xen/arch/x86/microcode_amd.c +3
-1@@ -379,12 +462,35 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); -if ( save_error ) +/* +* If there happens to be multiple container files and if patch +* update succeeded on an earlier container itself, a stale error Do you perhaps mean bogus instead of stale?
Yes. Wrong word. Will fix.
Quoted 10 lines
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 474
Quoted 7 lines
xen/arch/x86/microcode_amd.c +6
-0+* val is returned from get_ucode_from_buffer_amd. Since we already +* succeeded in patch application, force error = 0 +*/ +if ( offset < bufsize ) + error =0 ; +else if ( save_error ) error = save_error;And still my question stands: Since you don't look at further containers if you found a match and successfully applied the updated, why is this change needed (or is the comment saying the wrong thing)?
Maybe the comment needs to be more verbose(?)
Yes, we found a match and yes, we applied the patch successfully.
But, while ( (error = get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 ) is going to, at some point hit if ( mpbuf->type != UCODE_UCODE_TYPE ) and return -EINVAL which is assigned to the variable 'error'
(Assuming ofc that there is a second container there which we don't need to parse because we have already succeeded in patch application)
This is what I wanted to convey from
"astale bogus error val is returned from get_ucode_from_buffer_amd."
But, we need to return 0 on success; which is why this change is needed here..
Quoted 19 lines
Quoted 9 lines
xen/arch/x86/microcode_amd.c +4
-0}if ( save_error ) { +/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 492
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ;
Quoted 2 lines
Same for this change/comment.
Hmm.. I'm having trouble trying to re-word this comment then..
Given the situation where - we have already applied the patch update after 'microcode_presmp_init' and 'microcode_resume_cpu';
|
v
Now 'microcode_init' runs and calls into 'cpu_request_microcode';
|
v
We use 1st while loop to find_equiv_cpu_id() and match it with the container
|
v
For this particular case, we assume it's a match on the 1st container; so break
|
v
Enter while ( (error = get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 )
|
v
At some point, it will find the correct patch; but this time there is no need to update
|
v
The behavior is now similar to what I have described above. i.e
while ( (error = get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 ) is going to, at some point hit if ( mpbuf->type != UCODE_UCODE_TYPE ) and return -EINVAL which is assigned to the variable 'error'
|
v
But, now (as stated in the comment..)
* Only this time, there is no 'applied_offset' as well.
+ * So, 'save_error' = 1. But error = -EINVAL.
|
v
And since we need to return 0 for success, this change is needed here.
Thanks,
-Aravind
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 27.06.14 at 19:07, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 467
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.06.14 at 21:34, <[email protected]> wrote:
xen/arch/x86/microcode_amd.c +3
-1@@ -379,12 +462,35 @@ static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) save_error =get_ucode_from_buffer_amd ( mc_amd, buf, bufsize, &applied_offset); -if ( save_error ) +/* +* If there happens to be multiple container files and if patch +* update succeeded on an earlier container itself, a stale error Do you perhaps mean bogus instead of stale?
Yes. Wrong word. Will fix.
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/microcode_amd.c +6
-0+* val is returned from get_ucode_from_buffer_amd. Since we already +* succeeded in patch application, force error = 0 +*/ +if ( offset < bufsize ) + error =0 ; +else if ( save_error ) error = save_error;
Quoted 5 lines
Quoted 3 lines
And still my question stands: Since you don't look at further containers if you found a match and successfully applied the updated, why is this change needed (or is the comment saying the wrong thing)?
Maybe the comment needs to be more verbose(?)
It's not about the amount of comment, but about it needing to be precise.
Quoted 10 lines
Yes, we found a match and yes, we applied the patch successfully. But, while ( (error =
get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 )
is going to, at some point hit if (mpbuf->type!=UCODE_UCODE_TYPE) and return-EINVAL
which is assigned to the variable 'error'
(Assuming ofc that there is a second container there which we don't need to parse because
we have already succeeded in patch application)
Quoted 6 lines
This is what I wanted to convey from
"astale bogus error val is returned from
get_ucode_from_buffer_amd."But, we need to return 0 on success; which is why this change is needed here..
I think I understand now: This talks about the case of a _subsequent_ container (never really looked at) following, causing the unwanted -EINVAL. Whereas your comment said "earlier container", implying (to me) that it talks about one that earlier code did look at.
Quoted 53 lines
Quoted 18 lines
Quoted 8 lines
xen/arch/x86/microcode_amd.c +4
-0}if ( save_error ) { +/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 492
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ;Quoted 2 lines
Same for this change/comment.
Hmm.. I'm having trouble trying to re-word this comment then..
Given the situation where - we have already applied the patch update after
'microcode_presmp_init' and 'microcode_resume_cpu'; | v Now 'microcode_init' runs and calls into 'cpu_request_microcode'; | v We use 1st while loop to find_equiv_cpu_id() and match it with the container | v For this particular case, we assume it's a match on the 1st container; so break | v Enter while ( (error = get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 ) | v At some point, it will find the correct patch; but this time there is no need to update | vThe behavior is now similar to what I have described above. i.e while ( (error =
get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 )
is going to, at some point hit if (mpbuf->type!=UCODE_UCODE_TYPE) and return-EINVAL
which is assigned to the variable 'error'| v
Quoted 8 lines
But, now (as stated in the comment..)
* Only this time, there is no '
applied_offset' as well.
+ * So, 'save_error' = 1. But error =-EINVAL.| vAnd since we need to return 0 for success, this change is needed here.
So since this is similar to the previous comment, rather than duplicating information, perhaps just refer to the earlier one, adding _only_ the information of the different aspect(s) here. And use the right words: To me at least the "Only this time" implies something different than what I think you mean - "Except that this time ..." would be the words I'd use (but a native English speaker may need to be consulted in case you view this differently).
Jan
Quoted 81 lines · Jan Beulich
Quoted 10 lines
Yes, we found a match and yes, we applied the patch successfully. But, while ( (error =
get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 )
is going to, at some point hit if (mpbuf->type!=UCODE_UCODE_TYPE) and return-EINVAL
which is assigned to the variable 'error'
(Assuming ofc that there is a second container there which we don't need to parse because
we have already succeeded in patch application)Quoted 6 lines
This is what I wanted to convey from
"astale bogus error val is returned from
get_ucode_from_buffer_amd."But, we need to return 0 on success; which is why this change is needed here..
I think I understand now: This talks about the case of a _subsequent_ container (never really looked at) following, causing the unwanted
-EINVAL. Whereas your comment said "earlier container", implying (to me) that it talks about one that earlier code did look at.Quoted 52 lines
Quoted 18 lines
Quoted 8 lines
} if ( save_error ) {xen/arch/x86/microcode_amd.c +4
-0+/* +* By the time 'microcode_init' runs, we have already updated the +* patch level on all (currently) running cpus. +* But, get_ucode_from_buffer_amd will return -EINVAL as xen/arch/x86/microcode_amd.c static int cpu_request_microcode(int cpu, const void *buf, size_t bufsize) · line 492
Quoted 10 lines
xen/arch/x86/microcode_amd.c +10
-0+* if ( mpbuf->type != UCODE_UCODE_TYPE ) fails in this case: +* Multiple containers are present && update succeeded with the +* first container file itself. +* +* Only this time, there is no 'applied_offset' as well. +* So, 'save_error' = 1. But error = -EINVAL. +* Hence, this check is necessary to return 0 for success. +*/ +if ( (error != save_error) && (offset < bufsize) ) + error =0 ;Quoted 2 lines
Same for this change/comment.
Hmm.. I'm having trouble trying to re-word this comment then..
Given the situation where - we have already applied the patch update after
'microcode_presmp_init' and 'microcode_resume_cpu'; | v Now 'microcode_init' runs and calls into 'cpu_request_microcode'; | v We use 1st while loop to find_equiv_cpu_id() and match it with the container | v For this particular case, we assume it's a match on the 1st container; so break | v Enter while ( (error = get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 ) | v At some point, it will find the correct patch; but this time there is no need to update | vThe behavior is now similar to what I have described above. i.e while ( (error =
get_ucode_from_buffer_amd(mc_amd, buf, bufsize,&offset)) == 0 )
is going to, at some point hit if (mpbuf->type!=UCODE_UCODE_TYPE) and return-EINVAL
which is assigned to the variable 'error'| vQuoted 8 lines
But, now (as stated in the comment..)
* Only this time, there is no '
applied_offset' as well.
+ * So, 'save_error' = 1. But error =-EINVAL.| vAnd since we need to return 0 for success, this change is needed here.
Quoted 9 lines
So since this is similar to the previous comment, rather than duplicating information, perhaps just refer to the earlier one, adding _only_ the information of the different aspect(s) here. And use the right words: To me at least the "Only this time" implies something different than what I think you mean - "Except that this time ..." would be the words I'd use (but a native English speaker may need to be consulted in case you view this differently).
Ok. I have followed a similar step-wise approach (think this is simpler to follow) in the comments for V4. Hopefully it's more precisely worded..
I am also working to document container file format, where I will also talk about why a correct patch can be on only one or the other container, but not both.
Thanks,
-Aravind.