linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Ard Biesheuvel <ardb@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-efi <linux-efi@vger.kernel.org>,
	Hans de Goede <hdegoede@redhat.com>,
	Matthew Garrett <matthewgarrett@google.com>,
	Ingo Molnar <mingo@kernel.org>, Andy Lutomirski <luto@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCH 03/10] efi/libstub: use a helper to iterate over a EFI handle array
Date: Sat, 14 Dec 2019 21:04:10 +0000	[thread overview]
Message-ID: <CAKv+Gu9kEySOrKM0Q01a-ZFbSTZz51TcfmnWbSq=LWqKw=8cNw@mail.gmail.com> (raw)
In-Reply-To: <CAKv+Gu-XAvYf8G+7Oi-XVM+DvR89_zkmETmou-2ftgC41tnvMw@mail.gmail.com>

On Sat, 14 Dec 2019 at 21:40, Ard Biesheuvel <ard.biesheuvel@linaro.org> wrote:
>
> On Sat, 14 Dec 2019 at 21:33, Arvind Sankar <nivedita@alum.mit.edu> wrote:
> >
> > On Sat, Dec 14, 2019 at 06:57:28PM +0100, Ard Biesheuvel wrote:
> > > Iterating over a EFI handle array is a bit finicky, since we have
> > > to take mixed mode into account, where handles are only 32-bit
> > > while the native efi_handle_t type is 64-bit.
> > >
> > > So introduce a helper, and replace the various occurrences of
> > > this pattern.
> > >
> > > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > > ---
> > >
> > > +#define for_each_efi_handle(handle, array, size, i)                  \
> > > +     for (i = 1, handle = efi_is_64bit()                             \
> > > +             ? (efi_handle_t)(unsigned long)((u64 *)(array))[0]      \
> > > +             : (efi_handle_t)(unsigned long)((u32 *)(array))[0];     \
> > > +         i++ <= (size) / (efi_is_64bit() ? sizeof(efi_handle_t)      \
> > > +                                          : sizeof(u32));            \
> > > +         handle = efi_is_64bit()                                     \
> > > +             ? (efi_handle_t)(unsigned long)((u64 *)(array))[i]      \
> > > +             : (efi_handle_t)(unsigned long)((u32 *)(array))[i])
> > > +
> > >  /*
> > >   * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
> > >   * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
> > > --
> > > 2.17.1
> > >
> >
> > This would access one past the array, no? Eg if the array has one
> > handle, i is incremented to 2 the first time the condition is checked,
> > then the loop increment will access array[2] before the condition is
> > checked again. There seem to be at least a couple of other for_each
> > macros that might have similar issues.
> >
>
> Indeed.
>
> > How about the below instead?
> >
> > #define for_each_efi_handle(handle, array, size, i)                     \
> >         for (i = 0;                                                     \
> >             (i < (size) / (efi_is_64bit() ? sizeof(efi_handle_t)        \
> >                                           : sizeof(u32))) &&            \
> >             ((handle = efi_is_64bit()                                   \
> >                 ? ((efi_handle_t *)(array))[i]                          \
> >                 : (efi_handle_t)(unsigned long)((u32 *)(array))[i]), 1);\
> >             i++)
> >
>
> Yeah, that looks correct to me, but perhaps we can come up with
> something slightly more readable? :-)
> (Not saying my code was better in that respect)

How about

#define efi_get_handle_at(array, idx)      \
    (efi_is_64bit() ? (efi_handle_t)(unsigned long)((u64 *)(array))[idx] \
                    : (efi_handle_t)(unsigned long)((u32 *)(array))[i])


#define efi_get_handle_num(size) \
    ((size) / (efi_is_64bit() ? sizeof(u64) : sizeof(u32)))

#define for_each_efi_handle(handle, array, size, i) \
    for (i = 0; \
         i < efi_get_handle_num(size) && \
            ((handle = efi_get_handle_at((array), i)) || true); \
         i++)

  reply	other threads:[~2019-12-14 21:04 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-14 17:57 [PATCH 00/10] efi/x86: confine type unsafe casting to mixed mode Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 01/10] efi/libstub: remove unused __efi_call_early() macro Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 02/10] efi/x86: rename efi_is_native() to efi_is_mixed() Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 03/10] efi/libstub: use a helper to iterate over a EFI handle array Ard Biesheuvel
2019-12-14 20:32   ` Arvind Sankar
2019-12-14 20:40     ` Ard Biesheuvel
2019-12-14 21:04       ` Ard Biesheuvel [this message]
2019-12-14 21:11         ` Arvind Sankar
2019-12-14 21:07       ` Arvind Sankar
2019-12-14 17:57 ` [PATCH 04/10] efi/libstub: add missing apple_properties_protocol_t definition Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 05/10] efi/libstub: distinguish between native/mixed not 32/64 bit Ard Biesheuvel
2019-12-14 19:46   ` Arvind Sankar
2019-12-14 19:49     ` Arvind Sankar
2019-12-14 19:54       ` Ard Biesheuvel
2019-12-14 20:13         ` Arvind Sankar
2019-12-14 20:27           ` Ard Biesheuvel
2019-12-14 21:17             ` Arvind Sankar
2019-12-14 21:30               ` Ard Biesheuvel
2019-12-14 22:14                 ` Arvind Sankar
2019-12-14 22:14                 ` Ard Biesheuvel
2019-12-14 23:02                   ` Arvind Sankar
2019-12-14 17:57 ` [PATCH 06/10] efi/libstub/x86: use mixed mode helpers to populate efi_config Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 07/10] efi/libstub: drop explicit 64-bit protocol definitions Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 08/10] efi/libstub: use stricter typing for firmware function pointers Ard Biesheuvel
2019-12-14 17:57 ` [PATCH 09/10] efi/libstub: annotate firmware routines as __efiapi Ard Biesheuvel
2019-12-17 15:01   ` Brian Gerst
2019-12-14 17:57 ` [PATCH 10/10] efi/libstub/x86: avoid thunking for native firmware calls Ard Biesheuvel
2019-12-15 19:30   ` Arvind Sankar
2019-12-17  8:32     ` Ard Biesheuvel

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='CAKv+Gu9kEySOrKM0Q01a-ZFbSTZz51TcfmnWbSq=LWqKw=8cNw@mail.gmail.com' \
    --to=ard.biesheuvel@linaro.org \
    --cc=ardb@kernel.org \
    --cc=hdegoede@redhat.com \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=matthewgarrett@google.com \
    --cc=mingo@kernel.org \
    --cc=nivedita@alum.mit.edu \
    --cc=tglx@linutronix.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).