All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Beulich <jbeulich@suse.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Xen-devel <xen-devel@lists.xenproject.org>,
	"Wei Liu" <wl@xen.org>, "Roger Pau Monné" <roger.pau@citrix.com>
Subject: Re: [PATCH 11/11] x86/ucode/amd: Rework parsing logic in cpu_request_microcode()
Date: Tue, 31 Mar 2020 17:07:07 +0200	[thread overview]
Message-ID: <cbb0b2c8-d06b-4b49-f955-dffe002acdae@suse.com> (raw)
In-Reply-To: <20200331100531.4294-12-andrew.cooper3@citrix.com>

On 31.03.2020 12:05, Andrew Cooper wrote:
> @@ -269,55 +265,25 @@ static int apply_microcode(const struct microcode_patch *patch)
>      return 0;
>  }
>  
> -static int scan_equiv_cpu_table(
> -    const void *data,
> -    size_t size_left,
> -    size_t *offset)
> +static int scan_equiv_cpu_table(const struct container_equiv_table *et)
>  {
>      const struct cpu_signature *sig = &this_cpu(cpu_sig);
> -    const struct mpbhdr *mpbuf;
> -    const struct equiv_cpu_entry *eq;
> -    unsigned int i, nr;
> -
> -    if ( size_left < (sizeof(*mpbuf) + 4) ||
> -         (mpbuf = data + *offset + 4,
> -          size_left - sizeof(*mpbuf) - 4 < mpbuf->len) )
> -    {
> -        printk(XENLOG_WARNING "microcode: No space for equivalent cpu table\n");
> -        return -EINVAL;
> -    }
> -
> -    *offset += mpbuf->len + CONT_HDR_SIZE;	/* add header length */
> -
> -    if ( mpbuf->type != UCODE_EQUIV_CPU_TABLE_TYPE )
> -    {
> -        printk(KERN_ERR "microcode: Wrong microcode equivalent cpu table type field\n");
> -        return -EINVAL;
> -    }
> -
> -    if ( mpbuf->len == 0 || mpbuf->len % sizeof(*eq) ||
> -         (eq = (const void *)mpbuf->data,
> -          nr = mpbuf->len / sizeof(*eq),
> -          eq[nr - 1].installed_cpu) )

Did this last check get lost? I can't seem to be able to identify
any possible replacement.

>  static struct microcode_patch *cpu_request_microcode(const void *buf, size_t size)
>  {
>      const struct microcode_patch *saved = NULL;
>      struct microcode_patch *patch = NULL;
> -    size_t offset = 0, saved_size = 0;
> +    size_t saved_size = 0;
>      int error = 0;
> -    unsigned int cpu = smp_processor_id();
> -    const struct cpu_signature *sig = &per_cpu(cpu_sig, cpu);
>  
> -    if ( size < 4 ||
> -         *(const uint32_t *)buf != UCODE_MAGIC )
> +    while ( size )
>      {
> -        printk(KERN_ERR "microcode: Wrong microcode patch file magic\n");
> -        error = -EINVAL;
> -        goto out;
> -    }
> -
> -    /*
> -     * 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 < size )
> -    {
> -        error = scan_equiv_cpu_table(buf, size - offset, &offset);
> -
> -        if ( !error || error != -ESRCH )
> -            break;
> +        const struct container_equiv_table *et;
> +        bool skip_ucode;
>  
> -        error = container_fast_forward(buf, size - offset, &offset);
> -        if ( error == -ENODATA )
> +        if ( size < 4 || *(const uint32_t *)buf != UCODE_MAGIC )
>          {
> -            ASSERT(offset == size);
> +            printk(XENLOG_ERR "microcode: Wrong microcode patch file magic\n");
> +            error = -EINVAL;
>              break;
>          }
> -        if ( error )
> +
> +        /* Move over UCODE_MAGIC. */
> +        buf  += 4;
> +        size -= 4;
> +
> +        if ( size < sizeof(*et) ||
> +             (et = buf)->type != UCODE_EQUIV_CPU_TABLE_TYPE ||
> +             size - sizeof(*et) < et->len ||
> +             et->len % sizeof(et->eq[0]) )
>          {
> -            printk(KERN_ERR "microcode: CPU%d incorrect or corrupt container file\n"
> -                   "microcode: Failed to update patch level. "
> -                   "Current lvl:%#x\n", cpu, sig->rev);
> +            printk(XENLOG_ERR "microcode: Bad equivalent cpu table\n");
> +            error = -EINVAL;
>              break;
>          }
> -    }
>  
> -    if ( error )
> -    {
> -        /*
> -         * -ENODATA here means that the blob was parsed fine but no matching
> -         * ucode was found. Don't return it to the caller.
> -         */
> -        if ( error == -ENODATA )
> -            error = 0;
> -
> -        goto out;
> -    }
> +        /* Move over the Equiv table. */
> +        buf  += sizeof(*et) + et->len;
> +        size -= sizeof(*et) + et->len;
> +
> +        error = scan_equiv_cpu_table(et);
> +        if ( error && error != -ESRCH )
> +            break;

With this the only non-zero value left for error is -ESRCH.
Hence ...

> +        /* -ESRCH means no applicable microcode in this container. */
> +        skip_ucode = error == -ESRCH;

... perhaps omit the "== -ESRCH" here, moving the comment up
ahead of the if()?

Jan


  reply	other threads:[~2020-03-31 15:07 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-31 10:05 [PATCH 00/11] x86/ucode: Cleanup and fixes - Part 4/n (AMD) Andrew Cooper
2020-03-31 10:05 ` [PATCH 01/11] x86/ucode/amd: Fix more potential buffer overruns with microcode parsing Andrew Cooper
2020-03-31 10:05 ` [PATCH 02/11] x86/ucode/amd: Move check_final_patch_levels() to apply_microcode() Andrew Cooper
2020-03-31 14:27   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 03/11] x86/ucode/amd: Don't use void * for microcode_patch->mpb Andrew Cooper
2020-03-31 14:28   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 04/11] x86/ucode/amd: Collect CPUID.1.EAX in collect_cpu_info() Andrew Cooper
2020-03-31 14:29   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 05/11] x86/ucode/amd: Overhaul the equivalent cpu table handling completely Andrew Cooper
2020-03-31 14:36   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 06/11] x86/ucode/amd: Move verify_patch_size() into get_ucode_from_buffer_amd() Andrew Cooper
2020-03-31 14:38   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 07/11] x86/ucode/amd: Alter API for microcode_fits() Andrew Cooper
2020-03-31 14:39   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 08/11] x86/ucode/amd: Rename bufsize to size in cpu_request_microcode() Andrew Cooper
2020-03-31 14:41   ` Jan Beulich
2020-03-31 14:43     ` Andrew Cooper
2020-03-31 10:05 ` [PATCH 09/11] x86/ucode/amd: Remove gratuitous memory allocations from cpu_request_microcode() Andrew Cooper
2020-03-31 14:51   ` Jan Beulich
2020-03-31 14:55     ` Andrew Cooper
2020-03-31 15:13       ` Jan Beulich
2020-03-31 15:47         ` Andrew Cooper
2020-03-31 15:52           ` Jan Beulich
2020-03-31 10:05 ` [PATCH 10/11] x86/ucode/amd: Fold structures together Andrew Cooper
2020-03-31 14:53   ` Jan Beulich
2020-03-31 10:05 ` [PATCH 11/11] x86/ucode/amd: Rework parsing logic in cpu_request_microcode() Andrew Cooper
2020-03-31 15:07   ` Jan Beulich [this message]
2020-03-31 15:19     ` Andrew Cooper
2020-03-31 15:27       ` Jan Beulich
2020-03-31 15:55         ` Andrew Cooper
2020-03-31 16:00           ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cbb0b2c8-d06b-4b49-f955-dffe002acdae@suse.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=roger.pau@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.