Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-17 Thread Ard Biesheuvel
On 16 January 2017 at 15:15, Bhupesh Sharma  wrote:
> Thanks Dave.
>
> On Fri, Jan 13, 2017 at 3:03 AM, Dave Young  wrote:
>> On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
>>> On 12 January 2017 at 09:41, Dave Young  wrote:
>>> > Before invoking the arch specific handler, efi_mem_reserve() reserves
>>> > the given memory region through memblock.
>>> >
>>> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
>>> > memblock is dead and it should not be used any more.
>>> >
>>> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
>>> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
>>> > in efi bgrt code still use memblock safely.
>>> >
>>> > Signed-off-by: Dave Young 
>>>
>>> I know this is probably out of scope for you, but since we're moving
>>> things around, any chance we could do so in a manner that will enable
>>> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
>>>
>>
>> I'm happy to do so, Bhupesh Sharma  said he had
>> some investigation on that already, I would like to ask him to help on that.
>>
>> Already cced him..
>
>
> Hi Ard,
>
> I have started working on an implementation where most of the BGRT
> code which exists inside 'arch/x86/platform/efi-bgrt.c' can be reused
> for ARM/ARM64.
>
> I am testing a RFC approach for the same using Qemu for AARCH64. I
> have sent out a patch to enable BGRT support in ArmVirtPkg (see [1])
>
> I have one question regarding the placement of the early bgrt handling
> code so that it can be reused on both arm/arm64 and x86:
>
> - Should I consider moving the current code from
> arch/x86/platform/efi-bgrt.c to outside arch/x86 so that it can be
> used by both the ARCHs or should I reuse the existing x86 stuff in a
> ARM specific way - no mem_remap for e.g. in a find inside arch/arm -
> say efi-arm-bgrt.c
>

-ENOPARSE

Please first move the code from x86 to a generic location
(drivers/firmware/efi or drivers/acpi), and make sure that it still
works as expected.
Then you can modify the code so that it works for arm64 as well as
x86, in a separate patch.

Thanks,
Ard.

> Suggestions?
>
>
> [1] https://lists.01.org/pipermail/edk2-devel/2017-January/006588.html
>
> Regards,
> Bhupesh


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-17 Thread Ard Biesheuvel
On 16 January 2017 at 15:15, Bhupesh Sharma  wrote:
> Thanks Dave.
>
> On Fri, Jan 13, 2017 at 3:03 AM, Dave Young  wrote:
>> On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
>>> On 12 January 2017 at 09:41, Dave Young  wrote:
>>> > Before invoking the arch specific handler, efi_mem_reserve() reserves
>>> > the given memory region through memblock.
>>> >
>>> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
>>> > memblock is dead and it should not be used any more.
>>> >
>>> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
>>> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
>>> > in efi bgrt code still use memblock safely.
>>> >
>>> > Signed-off-by: Dave Young 
>>>
>>> I know this is probably out of scope for you, but since we're moving
>>> things around, any chance we could do so in a manner that will enable
>>> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
>>>
>>
>> I'm happy to do so, Bhupesh Sharma  said he had
>> some investigation on that already, I would like to ask him to help on that.
>>
>> Already cced him..
>
>
> Hi Ard,
>
> I have started working on an implementation where most of the BGRT
> code which exists inside 'arch/x86/platform/efi-bgrt.c' can be reused
> for ARM/ARM64.
>
> I am testing a RFC approach for the same using Qemu for AARCH64. I
> have sent out a patch to enable BGRT support in ArmVirtPkg (see [1])
>
> I have one question regarding the placement of the early bgrt handling
> code so that it can be reused on both arm/arm64 and x86:
>
> - Should I consider moving the current code from
> arch/x86/platform/efi-bgrt.c to outside arch/x86 so that it can be
> used by both the ARCHs or should I reuse the existing x86 stuff in a
> ARM specific way - no mem_remap for e.g. in a find inside arch/arm -
> say efi-arm-bgrt.c
>

-ENOPARSE

Please first move the code from x86 to a generic location
(drivers/firmware/efi or drivers/acpi), and make sure that it still
works as expected.
Then you can modify the code so that it works for arm64 as well as
x86, in a separate patch.

Thanks,
Ard.

> Suggestions?
>
>
> [1] https://lists.01.org/pipermail/edk2-devel/2017-January/006588.html
>
> Regards,
> Bhupesh


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-16 Thread Bhupesh Sharma
Thanks Dave.

On Fri, Jan 13, 2017 at 3:03 AM, Dave Young  wrote:
> On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
>> On 12 January 2017 at 09:41, Dave Young  wrote:
>> > Before invoking the arch specific handler, efi_mem_reserve() reserves
>> > the given memory region through memblock.
>> >
>> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
>> > memblock is dead and it should not be used any more.
>> >
>> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
>> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
>> > in efi bgrt code still use memblock safely.
>> >
>> > Signed-off-by: Dave Young 
>>
>> I know this is probably out of scope for you, but since we're moving
>> things around, any chance we could do so in a manner that will enable
>> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
>>
>
> I'm happy to do so, Bhupesh Sharma  said he had
> some investigation on that already, I would like to ask him to help on that.
>
> Already cced him..


Hi Ard,

I have started working on an implementation where most of the BGRT
code which exists inside 'arch/x86/platform/efi-bgrt.c' can be reused
for ARM/ARM64.

I am testing a RFC approach for the same using Qemu for AARCH64. I
have sent out a patch to enable BGRT support in ArmVirtPkg (see [1])

I have one question regarding the placement of the early bgrt handling
code so that it can be reused on both arm/arm64 and x86:

- Should I consider moving the current code from
arch/x86/platform/efi-bgrt.c to outside arch/x86 so that it can be
used by both the ARCHs or should I reuse the existing x86 stuff in a
ARM specific way - no mem_remap for e.g. in a find inside arch/arm -
say efi-arm-bgrt.c

Suggestions?


[1] https://lists.01.org/pipermail/edk2-devel/2017-January/006588.html

Regards,
Bhupesh


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-16 Thread Bhupesh Sharma
Thanks Dave.

On Fri, Jan 13, 2017 at 3:03 AM, Dave Young  wrote:
> On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
>> On 12 January 2017 at 09:41, Dave Young  wrote:
>> > Before invoking the arch specific handler, efi_mem_reserve() reserves
>> > the given memory region through memblock.
>> >
>> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
>> > memblock is dead and it should not be used any more.
>> >
>> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
>> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
>> > in efi bgrt code still use memblock safely.
>> >
>> > Signed-off-by: Dave Young 
>>
>> I know this is probably out of scope for you, but since we're moving
>> things around, any chance we could do so in a manner that will enable
>> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
>>
>
> I'm happy to do so, Bhupesh Sharma  said he had
> some investigation on that already, I would like to ask him to help on that.
>
> Already cced him..


Hi Ard,

I have started working on an implementation where most of the BGRT
code which exists inside 'arch/x86/platform/efi-bgrt.c' can be reused
for ARM/ARM64.

I am testing a RFC approach for the same using Qemu for AARCH64. I
have sent out a patch to enable BGRT support in ArmVirtPkg (see [1])

I have one question regarding the placement of the early bgrt handling
code so that it can be reused on both arm/arm64 and x86:

- Should I consider moving the current code from
arch/x86/platform/efi-bgrt.c to outside arch/x86 so that it can be
used by both the ARCHs or should I reuse the existing x86 stuff in a
ARM specific way - no mem_remap for e.g. in a find inside arch/arm -
say efi-arm-bgrt.c

Suggestions?


[1] https://lists.01.org/pipermail/edk2-devel/2017-January/006588.html

Regards,
Bhupesh


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-15 Thread Dave Young
On 01/13/17 at 01:21pm, Nicolai Stange wrote:
> On Fri, Jan 13 2017, Dave Young wrote:
> 
> > On 01/13/17 at 10:21am, Dave Young wrote:
> >> On 01/13/17 at 12:11am, Nicolai Stange wrote:
> >> > On Fri, Jan 13 2017, Dave Young wrote:
> >> > 
> >> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> >> > >> On Thu, Jan 12 2017, Dave Young wrote:
> >> > >> 
> >> > >> > -void __init efi_bgrt_init(void)
> >> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >> > >> >  {
> >> > >> > -   acpi_status status;
> >> > >> > void *image;
> >> > >> > struct bmp_header bmp_header;
> >> > >> >  
> >> > >> > if (acpi_disabled)
> >> > >> > return;
> >> > >> >  
> >> > >> > -   status = acpi_get_table("BGRT", 0,
> >> > >> > -   (struct acpi_table_header **)_tab);
> >> > >> > -   if (ACPI_FAILURE(status))
> >> > >> > -   return;
> >> > >> 
> >> > >> 
> >> > >> Not sure, but wouldn't it be safer to reverse the order of this
> >> > >> assignment
> >> > >> 
> >> > >> > +   bgrt_tab = *(struct acpi_table_bgrt *)table;
> >> > >
> >> > > Nicolai, sorry, I'm not sure I understand the comment, is it
> >> > > about above line?
> >> > > Could you elaborate a bit?
> >> > >
> >> > >> 
> >> > >> and this length check
> >> > >> 
> >> > >
> >> > > I also do not get this :(
> >> > 
> >> > Ah sorry, my point is this: the length check should perhaps be made
> >> > before doing the assignment to bgrt_tab because otherwise, we might end
> >> > up reading from invalid memory.
> >> > 
> >> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> >> > 
> >> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
> >> > 
> >> > would read past the table's end.
> >> > 
> >> > I'm not sure whether this is a real problem though -- that is, whether
> >> > this read could ever hit some unmapped memory.
> >> 
> >> Nicolai, thanks for the explanation. It make sense to move it to even later
> >> at the end of the function.
> >
> > Indeed assignment should be after the length checking, but with another
> > tmp variable the assignment to global var can be moved to the end to
> > avoid clear the image_address field..
> 
> I had a look at your updated patches at
> http://people.redhat.com/~ruyang/efi-bgrt/ and they look fine to me.

Many thanks~

> 
> One minor remark:
> 
> sizeof(acpi_table_bgrt) == 56 and it might be better to avoid the extra
> tmp copy in efi_bgrt_init() by
> - assigning directly to bgrt_tab
> - do a 'goto err' rather than a 'return' from all the error paths
> - do a memset(_tab, 0, sizeof(bgrt_tab)) at 'err:'

Updated in V2, indeed text size shrunk from 1199 to 762.

> 
> 
> With the copy to the on-stack 'bgrt', gcc 6.2.0 emits this for each of
> the two copies:
> 
>   41:   8a 07   mov(%rdi),%al
>   43:   88 45 d7mov%al,-0x29(%rbp)
>   46:   8a 47 01mov0x1(%rdi),%al
>   49:   88 45 d6mov%al,-0x2a(%rbp)
>   4c:   8a 47 02mov0x2(%rdi),%al
>   4f:   88 45 d5mov%al,-0x2b(%rbp)
>   52:   8a 47 03mov0x3(%rdi),%al
>   55:   88 45 d4mov%al,-0x2c(%rbp)
>   58:   8a 47 08mov0x8(%rdi),%al
>   5b:   88 45 d3mov%al,-0x2d(%rbp)
>   5e:   8a 47 09mov0x9(%rdi),%al
>   61:   88 45 d2mov%al,-0x2e(%rbp)
>   64:   8a 47 0amov0xa(%rdi),%al
>   67:   88 45 d1mov%al,-0x2f(%rbp)
>   6a:   8a 47 0bmov0xb(%rdi),%al
>   6d:   88 45 d0mov%al,-0x30(%rbp)
>   70:   8a 47 0cmov0xc(%rdi),%al
>   73:   88 45 cfmov%al,-0x31(%rbp)
>   76:   8a 47 0dmov0xd(%rdi),%al
>   79:   88 45 cemov%al,-0x32(%rbp)
>   7c:   8a 47 0emov0xe(%rdi),%al
>   7f:   88 45 cdmov%al,-0x33(%rbp)
>   82:   8a 47 0fmov0xf(%rdi),%al
>   85:   88 45 ccmov%al,-0x34(%rbp)
>   88:   8a 47 10mov0x10(%rdi),%al
>   8b:   88 45 cbmov%al,-0x35(%rbp)
>   8e:   8a 47 11mov0x11(%rdi),%al
>   91:   88 45 camov%al,-0x36(%rbp)
>   94:   8a 47 12mov0x12(%rdi),%al
>   97:   88 45 c9mov%al,-0x37(%rbp)
>   9a:   8a 47 13mov0x13(%rdi),%al
>   9d:   88 45 c8mov%al,-0x38(%rbp)
>   a0:   8a 47 14mov0x14(%rdi),%al
>   a3:   8a 5f 26mov0x26(%rdi),%bl
>   a6:   0f b6 77 27 movzbl 0x27(%rdi),%esi
>   aa:   4c 8b 67 28 mov0x28(%rdi),%r12
>   ae:   88 45 c7mov%al,-0x39(%rbp)
>   b1:   8a 47 15mov0x15(%rdi),%al
>   b4:   44 8b 6f 30 mov0x30(%rdi),%r13d
>   b8:   44 8b 7f 34 mov

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-15 Thread Dave Young
On 01/13/17 at 01:21pm, Nicolai Stange wrote:
> On Fri, Jan 13 2017, Dave Young wrote:
> 
> > On 01/13/17 at 10:21am, Dave Young wrote:
> >> On 01/13/17 at 12:11am, Nicolai Stange wrote:
> >> > On Fri, Jan 13 2017, Dave Young wrote:
> >> > 
> >> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> >> > >> On Thu, Jan 12 2017, Dave Young wrote:
> >> > >> 
> >> > >> > -void __init efi_bgrt_init(void)
> >> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >> > >> >  {
> >> > >> > -   acpi_status status;
> >> > >> > void *image;
> >> > >> > struct bmp_header bmp_header;
> >> > >> >  
> >> > >> > if (acpi_disabled)
> >> > >> > return;
> >> > >> >  
> >> > >> > -   status = acpi_get_table("BGRT", 0,
> >> > >> > -   (struct acpi_table_header **)_tab);
> >> > >> > -   if (ACPI_FAILURE(status))
> >> > >> > -   return;
> >> > >> 
> >> > >> 
> >> > >> Not sure, but wouldn't it be safer to reverse the order of this
> >> > >> assignment
> >> > >> 
> >> > >> > +   bgrt_tab = *(struct acpi_table_bgrt *)table;
> >> > >
> >> > > Nicolai, sorry, I'm not sure I understand the comment, is it
> >> > > about above line?
> >> > > Could you elaborate a bit?
> >> > >
> >> > >> 
> >> > >> and this length check
> >> > >> 
> >> > >
> >> > > I also do not get this :(
> >> > 
> >> > Ah sorry, my point is this: the length check should perhaps be made
> >> > before doing the assignment to bgrt_tab because otherwise, we might end
> >> > up reading from invalid memory.
> >> > 
> >> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> >> > 
> >> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
> >> > 
> >> > would read past the table's end.
> >> > 
> >> > I'm not sure whether this is a real problem though -- that is, whether
> >> > this read could ever hit some unmapped memory.
> >> 
> >> Nicolai, thanks for the explanation. It make sense to move it to even later
> >> at the end of the function.
> >
> > Indeed assignment should be after the length checking, but with another
> > tmp variable the assignment to global var can be moved to the end to
> > avoid clear the image_address field..
> 
> I had a look at your updated patches at
> http://people.redhat.com/~ruyang/efi-bgrt/ and they look fine to me.

Many thanks~

> 
> One minor remark:
> 
> sizeof(acpi_table_bgrt) == 56 and it might be better to avoid the extra
> tmp copy in efi_bgrt_init() by
> - assigning directly to bgrt_tab
> - do a 'goto err' rather than a 'return' from all the error paths
> - do a memset(_tab, 0, sizeof(bgrt_tab)) at 'err:'

Updated in V2, indeed text size shrunk from 1199 to 762.

> 
> 
> With the copy to the on-stack 'bgrt', gcc 6.2.0 emits this for each of
> the two copies:
> 
>   41:   8a 07   mov(%rdi),%al
>   43:   88 45 d7mov%al,-0x29(%rbp)
>   46:   8a 47 01mov0x1(%rdi),%al
>   49:   88 45 d6mov%al,-0x2a(%rbp)
>   4c:   8a 47 02mov0x2(%rdi),%al
>   4f:   88 45 d5mov%al,-0x2b(%rbp)
>   52:   8a 47 03mov0x3(%rdi),%al
>   55:   88 45 d4mov%al,-0x2c(%rbp)
>   58:   8a 47 08mov0x8(%rdi),%al
>   5b:   88 45 d3mov%al,-0x2d(%rbp)
>   5e:   8a 47 09mov0x9(%rdi),%al
>   61:   88 45 d2mov%al,-0x2e(%rbp)
>   64:   8a 47 0amov0xa(%rdi),%al
>   67:   88 45 d1mov%al,-0x2f(%rbp)
>   6a:   8a 47 0bmov0xb(%rdi),%al
>   6d:   88 45 d0mov%al,-0x30(%rbp)
>   70:   8a 47 0cmov0xc(%rdi),%al
>   73:   88 45 cfmov%al,-0x31(%rbp)
>   76:   8a 47 0dmov0xd(%rdi),%al
>   79:   88 45 cemov%al,-0x32(%rbp)
>   7c:   8a 47 0emov0xe(%rdi),%al
>   7f:   88 45 cdmov%al,-0x33(%rbp)
>   82:   8a 47 0fmov0xf(%rdi),%al
>   85:   88 45 ccmov%al,-0x34(%rbp)
>   88:   8a 47 10mov0x10(%rdi),%al
>   8b:   88 45 cbmov%al,-0x35(%rbp)
>   8e:   8a 47 11mov0x11(%rdi),%al
>   91:   88 45 camov%al,-0x36(%rbp)
>   94:   8a 47 12mov0x12(%rdi),%al
>   97:   88 45 c9mov%al,-0x37(%rbp)
>   9a:   8a 47 13mov0x13(%rdi),%al
>   9d:   88 45 c8mov%al,-0x38(%rbp)
>   a0:   8a 47 14mov0x14(%rdi),%al
>   a3:   8a 5f 26mov0x26(%rdi),%bl
>   a6:   0f b6 77 27 movzbl 0x27(%rdi),%esi
>   aa:   4c 8b 67 28 mov0x28(%rdi),%r12
>   ae:   88 45 c7mov%al,-0x39(%rbp)
>   b1:   8a 47 15mov0x15(%rdi),%al
>   b4:   44 8b 6f 30 mov0x30(%rdi),%r13d
>   b8:   44 8b 7f 34 mov

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-13 Thread Nicolai Stange
On Fri, Jan 13 2017, Dave Young wrote:

> On 01/13/17 at 10:21am, Dave Young wrote:
>> On 01/13/17 at 12:11am, Nicolai Stange wrote:
>> > On Fri, Jan 13 2017, Dave Young wrote:
>> > 
>> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
>> > >> On Thu, Jan 12 2017, Dave Young wrote:
>> > >> 
>> > >> > -void __init efi_bgrt_init(void)
>> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
>> > >> >  {
>> > >> > - acpi_status status;
>> > >> >   void *image;
>> > >> >   struct bmp_header bmp_header;
>> > >> >  
>> > >> >   if (acpi_disabled)
>> > >> >   return;
>> > >> >  
>> > >> > - status = acpi_get_table("BGRT", 0,
>> > >> > - (struct acpi_table_header **)_tab);
>> > >> > - if (ACPI_FAILURE(status))
>> > >> > - return;
>> > >> 
>> > >> 
>> > >> Not sure, but wouldn't it be safer to reverse the order of this
>> > >> assignment
>> > >> 
>> > >> > + bgrt_tab = *(struct acpi_table_bgrt *)table;
>> > >
>> > > Nicolai, sorry, I'm not sure I understand the comment, is it
>> > > about above line?
>> > > Could you elaborate a bit?
>> > >
>> > >> 
>> > >> and this length check
>> > >> 
>> > >
>> > > I also do not get this :(
>> > 
>> > Ah sorry, my point is this: the length check should perhaps be made
>> > before doing the assignment to bgrt_tab because otherwise, we might end
>> > up reading from invalid memory.
>> > 
>> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
>> > 
>> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
>> > 
>> > would read past the table's end.
>> > 
>> > I'm not sure whether this is a real problem though -- that is, whether
>> > this read could ever hit some unmapped memory.
>> 
>> Nicolai, thanks for the explanation. It make sense to move it to even later
>> at the end of the function.
>
> Indeed assignment should be after the length checking, but with another
> tmp variable the assignment to global var can be moved to the end to
> avoid clear the image_address field..

I had a look at your updated patches at
http://people.redhat.com/~ruyang/efi-bgrt/ and they look fine to me.

One minor remark:

sizeof(acpi_table_bgrt) == 56 and it might be better to avoid the extra
tmp copy in efi_bgrt_init() by
- assigning directly to bgrt_tab
- do a 'goto err' rather than a 'return' from all the error paths
- do a memset(_tab, 0, sizeof(bgrt_tab)) at 'err:'


With the copy to the on-stack 'bgrt', gcc 6.2.0 emits this for each of
the two copies:

  41:   8a 07   mov(%rdi),%al
  43:   88 45 d7mov%al,-0x29(%rbp)
  46:   8a 47 01mov0x1(%rdi),%al
  49:   88 45 d6mov%al,-0x2a(%rbp)
  4c:   8a 47 02mov0x2(%rdi),%al
  4f:   88 45 d5mov%al,-0x2b(%rbp)
  52:   8a 47 03mov0x3(%rdi),%al
  55:   88 45 d4mov%al,-0x2c(%rbp)
  58:   8a 47 08mov0x8(%rdi),%al
  5b:   88 45 d3mov%al,-0x2d(%rbp)
  5e:   8a 47 09mov0x9(%rdi),%al
  61:   88 45 d2mov%al,-0x2e(%rbp)
  64:   8a 47 0amov0xa(%rdi),%al
  67:   88 45 d1mov%al,-0x2f(%rbp)
  6a:   8a 47 0bmov0xb(%rdi),%al
  6d:   88 45 d0mov%al,-0x30(%rbp)
  70:   8a 47 0cmov0xc(%rdi),%al
  73:   88 45 cfmov%al,-0x31(%rbp)
  76:   8a 47 0dmov0xd(%rdi),%al
  79:   88 45 cemov%al,-0x32(%rbp)
  7c:   8a 47 0emov0xe(%rdi),%al
  7f:   88 45 cdmov%al,-0x33(%rbp)
  82:   8a 47 0fmov0xf(%rdi),%al
  85:   88 45 ccmov%al,-0x34(%rbp)
  88:   8a 47 10mov0x10(%rdi),%al
  8b:   88 45 cbmov%al,-0x35(%rbp)
  8e:   8a 47 11mov0x11(%rdi),%al
  91:   88 45 camov%al,-0x36(%rbp)
  94:   8a 47 12mov0x12(%rdi),%al
  97:   88 45 c9mov%al,-0x37(%rbp)
  9a:   8a 47 13mov0x13(%rdi),%al
  9d:   88 45 c8mov%al,-0x38(%rbp)
  a0:   8a 47 14mov0x14(%rdi),%al
  a3:   8a 5f 26mov0x26(%rdi),%bl
  a6:   0f b6 77 27 movzbl 0x27(%rdi),%esi
  aa:   4c 8b 67 28 mov0x28(%rdi),%r12
  ae:   88 45 c7mov%al,-0x39(%rbp)
  b1:   8a 47 15mov0x15(%rdi),%al
  b4:   44 8b 6f 30 mov0x30(%rdi),%r13d
  b8:   44 8b 7f 34 mov0x34(%rdi),%r15d
  bc:   88 45 c6mov%al,-0x3a(%rbp)
  bf:   8a 47 16mov0x16(%rdi),%al
  c2:   88 45 c5mov%al,-0x3b(%rbp)
  c5:   8a 47 17mov0x17(%rdi),%al
  c8:   88 45 c4mov%al,-0x3c(%rbp)
  cb:   8b 47 18mov

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-13 Thread Nicolai Stange
On Fri, Jan 13 2017, Dave Young wrote:

> On 01/13/17 at 10:21am, Dave Young wrote:
>> On 01/13/17 at 12:11am, Nicolai Stange wrote:
>> > On Fri, Jan 13 2017, Dave Young wrote:
>> > 
>> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
>> > >> On Thu, Jan 12 2017, Dave Young wrote:
>> > >> 
>> > >> > -void __init efi_bgrt_init(void)
>> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
>> > >> >  {
>> > >> > - acpi_status status;
>> > >> >   void *image;
>> > >> >   struct bmp_header bmp_header;
>> > >> >  
>> > >> >   if (acpi_disabled)
>> > >> >   return;
>> > >> >  
>> > >> > - status = acpi_get_table("BGRT", 0,
>> > >> > - (struct acpi_table_header **)_tab);
>> > >> > - if (ACPI_FAILURE(status))
>> > >> > - return;
>> > >> 
>> > >> 
>> > >> Not sure, but wouldn't it be safer to reverse the order of this
>> > >> assignment
>> > >> 
>> > >> > + bgrt_tab = *(struct acpi_table_bgrt *)table;
>> > >
>> > > Nicolai, sorry, I'm not sure I understand the comment, is it
>> > > about above line?
>> > > Could you elaborate a bit?
>> > >
>> > >> 
>> > >> and this length check
>> > >> 
>> > >
>> > > I also do not get this :(
>> > 
>> > Ah sorry, my point is this: the length check should perhaps be made
>> > before doing the assignment to bgrt_tab because otherwise, we might end
>> > up reading from invalid memory.
>> > 
>> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
>> > 
>> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
>> > 
>> > would read past the table's end.
>> > 
>> > I'm not sure whether this is a real problem though -- that is, whether
>> > this read could ever hit some unmapped memory.
>> 
>> Nicolai, thanks for the explanation. It make sense to move it to even later
>> at the end of the function.
>
> Indeed assignment should be after the length checking, but with another
> tmp variable the assignment to global var can be moved to the end to
> avoid clear the image_address field..

I had a look at your updated patches at
http://people.redhat.com/~ruyang/efi-bgrt/ and they look fine to me.

One minor remark:

sizeof(acpi_table_bgrt) == 56 and it might be better to avoid the extra
tmp copy in efi_bgrt_init() by
- assigning directly to bgrt_tab
- do a 'goto err' rather than a 'return' from all the error paths
- do a memset(_tab, 0, sizeof(bgrt_tab)) at 'err:'


With the copy to the on-stack 'bgrt', gcc 6.2.0 emits this for each of
the two copies:

  41:   8a 07   mov(%rdi),%al
  43:   88 45 d7mov%al,-0x29(%rbp)
  46:   8a 47 01mov0x1(%rdi),%al
  49:   88 45 d6mov%al,-0x2a(%rbp)
  4c:   8a 47 02mov0x2(%rdi),%al
  4f:   88 45 d5mov%al,-0x2b(%rbp)
  52:   8a 47 03mov0x3(%rdi),%al
  55:   88 45 d4mov%al,-0x2c(%rbp)
  58:   8a 47 08mov0x8(%rdi),%al
  5b:   88 45 d3mov%al,-0x2d(%rbp)
  5e:   8a 47 09mov0x9(%rdi),%al
  61:   88 45 d2mov%al,-0x2e(%rbp)
  64:   8a 47 0amov0xa(%rdi),%al
  67:   88 45 d1mov%al,-0x2f(%rbp)
  6a:   8a 47 0bmov0xb(%rdi),%al
  6d:   88 45 d0mov%al,-0x30(%rbp)
  70:   8a 47 0cmov0xc(%rdi),%al
  73:   88 45 cfmov%al,-0x31(%rbp)
  76:   8a 47 0dmov0xd(%rdi),%al
  79:   88 45 cemov%al,-0x32(%rbp)
  7c:   8a 47 0emov0xe(%rdi),%al
  7f:   88 45 cdmov%al,-0x33(%rbp)
  82:   8a 47 0fmov0xf(%rdi),%al
  85:   88 45 ccmov%al,-0x34(%rbp)
  88:   8a 47 10mov0x10(%rdi),%al
  8b:   88 45 cbmov%al,-0x35(%rbp)
  8e:   8a 47 11mov0x11(%rdi),%al
  91:   88 45 camov%al,-0x36(%rbp)
  94:   8a 47 12mov0x12(%rdi),%al
  97:   88 45 c9mov%al,-0x37(%rbp)
  9a:   8a 47 13mov0x13(%rdi),%al
  9d:   88 45 c8mov%al,-0x38(%rbp)
  a0:   8a 47 14mov0x14(%rdi),%al
  a3:   8a 5f 26mov0x26(%rdi),%bl
  a6:   0f b6 77 27 movzbl 0x27(%rdi),%esi
  aa:   4c 8b 67 28 mov0x28(%rdi),%r12
  ae:   88 45 c7mov%al,-0x39(%rbp)
  b1:   8a 47 15mov0x15(%rdi),%al
  b4:   44 8b 6f 30 mov0x30(%rdi),%r13d
  b8:   44 8b 7f 34 mov0x34(%rdi),%r15d
  bc:   88 45 c6mov%al,-0x3a(%rbp)
  bf:   8a 47 16mov0x16(%rdi),%al
  c2:   88 45 c5mov%al,-0x3b(%rbp)
  c5:   8a 47 17mov0x17(%rdi),%al
  c8:   88 45 c4mov%al,-0x3c(%rbp)
  cb:   8b 47 18mov

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/13/17 at 10:21am, Dave Young wrote:
> On 01/13/17 at 12:11am, Nicolai Stange wrote:
> > On Fri, Jan 13 2017, Dave Young wrote:
> > 
> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> > >> On Thu, Jan 12 2017, Dave Young wrote:
> > >> 
> > >> > -void __init efi_bgrt_init(void)
> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> > >> >  {
> > >> > -  acpi_status status;
> > >> >void *image;
> > >> >struct bmp_header bmp_header;
> > >> >  
> > >> >if (acpi_disabled)
> > >> >return;
> > >> >  
> > >> > -  status = acpi_get_table("BGRT", 0,
> > >> > -  (struct acpi_table_header **)_tab);
> > >> > -  if (ACPI_FAILURE(status))
> > >> > -  return;
> > >> 
> > >> 
> > >> Not sure, but wouldn't it be safer to reverse the order of this 
> > >> assignment
> > >> 
> > >> > +  bgrt_tab = *(struct acpi_table_bgrt *)table;
> > >
> > > Nicolai, sorry, I'm not sure I understand the comment, is it about above 
> > > line?
> > > Could you elaborate a bit?
> > >
> > >> 
> > >> and this length check
> > >> 
> > >
> > > I also do not get this :(
> > 
> > Ah sorry, my point is this: the length check should perhaps be made
> > before doing the assignment to bgrt_tab because otherwise, we might end
> > up reading from invalid memory.
> > 
> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> > 
> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
> > 
> > would read past the table's end.
> > 
> > I'm not sure whether this is a real problem though -- that is, whether
> > this read could ever hit some unmapped memory.
> 
> Nicolai, thanks for the explanation. It make sense to move it to even later
> at the end of the function.

Indeed assignment should be after the length checking, but with another
tmp variable the assignment to global var can be moved to the end to
avoid clear the image_address field..

> 
> Thanks
> Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/13/17 at 10:21am, Dave Young wrote:
> On 01/13/17 at 12:11am, Nicolai Stange wrote:
> > On Fri, Jan 13 2017, Dave Young wrote:
> > 
> > > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> > >> On Thu, Jan 12 2017, Dave Young wrote:
> > >> 
> > >> > -void __init efi_bgrt_init(void)
> > >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> > >> >  {
> > >> > -  acpi_status status;
> > >> >void *image;
> > >> >struct bmp_header bmp_header;
> > >> >  
> > >> >if (acpi_disabled)
> > >> >return;
> > >> >  
> > >> > -  status = acpi_get_table("BGRT", 0,
> > >> > -  (struct acpi_table_header **)_tab);
> > >> > -  if (ACPI_FAILURE(status))
> > >> > -  return;
> > >> 
> > >> 
> > >> Not sure, but wouldn't it be safer to reverse the order of this 
> > >> assignment
> > >> 
> > >> > +  bgrt_tab = *(struct acpi_table_bgrt *)table;
> > >
> > > Nicolai, sorry, I'm not sure I understand the comment, is it about above 
> > > line?
> > > Could you elaborate a bit?
> > >
> > >> 
> > >> and this length check
> > >> 
> > >
> > > I also do not get this :(
> > 
> > Ah sorry, my point is this: the length check should perhaps be made
> > before doing the assignment to bgrt_tab because otherwise, we might end
> > up reading from invalid memory.
> > 
> > I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> > 
> >   bgrt_tab = *(struct acpi_table_bgrt *)table;
> > 
> > would read past the table's end.
> > 
> > I'm not sure whether this is a real problem though -- that is, whether
> > this read could ever hit some unmapped memory.
> 
> Nicolai, thanks for the explanation. It make sense to move it to even later
> at the end of the function.

Indeed assignment should be after the length checking, but with another
tmp variable the assignment to global var can be moved to the end to
avoid clear the image_address field..

> 
> Thanks
> Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/13/17 at 12:11am, Nicolai Stange wrote:
> On Fri, Jan 13 2017, Dave Young wrote:
> 
> > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> >> On Thu, Jan 12 2017, Dave Young wrote:
> >> 
> >> > -void __init efi_bgrt_init(void)
> >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >> >  {
> >> > -acpi_status status;
> >> >  void *image;
> >> >  struct bmp_header bmp_header;
> >> >  
> >> >  if (acpi_disabled)
> >> >  return;
> >> >  
> >> > -status = acpi_get_table("BGRT", 0,
> >> > -(struct acpi_table_header **)_tab);
> >> > -if (ACPI_FAILURE(status))
> >> > -return;
> >> 
> >> 
> >> Not sure, but wouldn't it be safer to reverse the order of this assignment
> >> 
> >> > +bgrt_tab = *(struct acpi_table_bgrt *)table;
> >
> > Nicolai, sorry, I'm not sure I understand the comment, is it about above 
> > line?
> > Could you elaborate a bit?
> >
> >> 
> >> and this length check
> >> 
> >
> > I also do not get this :(
> 
> Ah sorry, my point is this: the length check should perhaps be made
> before doing the assignment to bgrt_tab because otherwise, we might end
> up reading from invalid memory.
> 
> I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> 
>   bgrt_tab = *(struct acpi_table_bgrt *)table;
> 
> would read past the table's end.
> 
> I'm not sure whether this is a real problem though -- that is, whether
> this read could ever hit some unmapped memory.

Nicolai, thanks for the explanation. It make sense to move it to even later
at the end of the function.

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/13/17 at 12:11am, Nicolai Stange wrote:
> On Fri, Jan 13 2017, Dave Young wrote:
> 
> > On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> >> On Thu, Jan 12 2017, Dave Young wrote:
> >> 
> >> > -void __init efi_bgrt_init(void)
> >> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >> >  {
> >> > -acpi_status status;
> >> >  void *image;
> >> >  struct bmp_header bmp_header;
> >> >  
> >> >  if (acpi_disabled)
> >> >  return;
> >> >  
> >> > -status = acpi_get_table("BGRT", 0,
> >> > -(struct acpi_table_header **)_tab);
> >> > -if (ACPI_FAILURE(status))
> >> > -return;
> >> 
> >> 
> >> Not sure, but wouldn't it be safer to reverse the order of this assignment
> >> 
> >> > +bgrt_tab = *(struct acpi_table_bgrt *)table;
> >
> > Nicolai, sorry, I'm not sure I understand the comment, is it about above 
> > line?
> > Could you elaborate a bit?
> >
> >> 
> >> and this length check
> >> 
> >
> > I also do not get this :(
> 
> Ah sorry, my point is this: the length check should perhaps be made
> before doing the assignment to bgrt_tab because otherwise, we might end
> up reading from invalid memory.
> 
> I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then
> 
>   bgrt_tab = *(struct acpi_table_bgrt *)table;
> 
> would read past the table's end.
> 
> I'm not sure whether this is a real problem though -- that is, whether
> this read could ever hit some unmapped memory.

Nicolai, thanks for the explanation. It make sense to move it to even later
at the end of the function.

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Nicolai Stange
On Fri, Jan 13 2017, Dave Young wrote:

> On 01/12/17 at 12:54pm, Nicolai Stange wrote:
>> On Thu, Jan 12 2017, Dave Young wrote:
>> 
>> > -void __init efi_bgrt_init(void)
>> > +void __init efi_bgrt_init(struct acpi_table_header *table)
>> >  {
>> > -  acpi_status status;
>> >void *image;
>> >struct bmp_header bmp_header;
>> >  
>> >if (acpi_disabled)
>> >return;
>> >  
>> > -  status = acpi_get_table("BGRT", 0,
>> > -  (struct acpi_table_header **)_tab);
>> > -  if (ACPI_FAILURE(status))
>> > -  return;
>> 
>> 
>> Not sure, but wouldn't it be safer to reverse the order of this assignment
>> 
>> > +  bgrt_tab = *(struct acpi_table_bgrt *)table;
>
> Nicolai, sorry, I'm not sure I understand the comment, is it about above line?
> Could you elaborate a bit?
>
>> 
>> and this length check
>> 
>
> I also do not get this :(

Ah sorry, my point is this: the length check should perhaps be made
before doing the assignment to bgrt_tab because otherwise, we might end
up reading from invalid memory.

I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then

  bgrt_tab = *(struct acpi_table_bgrt *)table;

would read past the table's end.

I'm not sure whether this is a real problem though -- that is, whether
this read could ever hit some unmapped memory.


>> > -  if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
>> > +  if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
>> >pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
>> > - bgrt_tab->header.length, sizeof(*bgrt_tab));
>> > + bgrt_tab.header.length, sizeof(bgrt_tab));
>> >return;
>> >}
>> 
>> ?

Thanks,

Nicolai


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Nicolai Stange
On Fri, Jan 13 2017, Dave Young wrote:

> On 01/12/17 at 12:54pm, Nicolai Stange wrote:
>> On Thu, Jan 12 2017, Dave Young wrote:
>> 
>> > -void __init efi_bgrt_init(void)
>> > +void __init efi_bgrt_init(struct acpi_table_header *table)
>> >  {
>> > -  acpi_status status;
>> >void *image;
>> >struct bmp_header bmp_header;
>> >  
>> >if (acpi_disabled)
>> >return;
>> >  
>> > -  status = acpi_get_table("BGRT", 0,
>> > -  (struct acpi_table_header **)_tab);
>> > -  if (ACPI_FAILURE(status))
>> > -  return;
>> 
>> 
>> Not sure, but wouldn't it be safer to reverse the order of this assignment
>> 
>> > +  bgrt_tab = *(struct acpi_table_bgrt *)table;
>
> Nicolai, sorry, I'm not sure I understand the comment, is it about above line?
> Could you elaborate a bit?
>
>> 
>> and this length check
>> 
>
> I also do not get this :(

Ah sorry, my point is this: the length check should perhaps be made
before doing the assignment to bgrt_tab because otherwise, we might end
up reading from invalid memory.

I.e. if (struct acpi_table_bgrt *)table->length < sizeof(bgrt_tab), then

  bgrt_tab = *(struct acpi_table_bgrt *)table;

would read past the table's end.

I'm not sure whether this is a real problem though -- that is, whether
this read could ever hit some unmapped memory.


>> > -  if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
>> > +  if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
>> >pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
>> > - bgrt_tab->header.length, sizeof(*bgrt_tab));
>> > + bgrt_tab.header.length, sizeof(bgrt_tab));
>> >return;
>> >}
>> 
>> ?

Thanks,

Nicolai


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> On Thu, Jan 12 2017, Dave Young wrote:
> 
> > -void __init efi_bgrt_init(void)
> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >  {
> > -   acpi_status status;
> > void *image;
> > struct bmp_header bmp_header;
> >  
> > if (acpi_disabled)
> > return;
> >  
> > -   status = acpi_get_table("BGRT", 0,
> > -   (struct acpi_table_header **)_tab);
> > -   if (ACPI_FAILURE(status))
> > -   return;
> 
> 
> Not sure, but wouldn't it be safer to reverse the order of this assignment
> 
> > +   bgrt_tab = *(struct acpi_table_bgrt *)table;

Nicolai, sorry, I'm not sure I understand the comment, is it about above line?
Could you elaborate a bit?

> 
> and this length check
> 

I also do not get this :(

> > -   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> > +   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
> > pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> > -  bgrt_tab->header.length, sizeof(*bgrt_tab));
> > +  bgrt_tab.header.length, sizeof(bgrt_tab));
> > return;
> > }
> 
> ?
> 
> Also, from here on, all error paths should zero out
> bgrt_tab.image_address (or so) to signal failure to bgrt_init():
> bgrt_init() now checks for !bgrt_tab.image_address whereas before it had
> tested bgrt_image and the latter used to be set at the very end of
> efi_bgrt_init().
> 

Will do, thanks!

> 
> > -   if (bgrt_tab->version != 1) {
> > +   if (bgrt_tab.version != 1) {
> > pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> > -  bgrt_tab->version);
> > +  bgrt_tab.version);
> > return;
> > }
> > -   if (bgrt_tab->status & 0xfe) {
> > +   if (bgrt_tab.status & 0xfe) {
> > pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> > %u\n",
> > -  bgrt_tab->status);
> > +  bgrt_tab.status);
> > return;
> > }
> > -   if (bgrt_tab->image_type != 0) {
> > +   if (bgrt_tab.image_type != 0) {
> > pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
> > -  bgrt_tab->image_type);
> > +  bgrt_tab.image_type);
> > return;
> > }
> > -   if (!bgrt_tab->image_address) {
> > +   if (!bgrt_tab.image_address) {
> > pr_notice("Ignoring BGRT: null image address\n");
> > return;
> > }
> >  
> > -   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> > MEMREMAP_WB);
> > +   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
> > if (!image) {
> > pr_notice("Ignoring BGRT: failed to map image header memory\n");
> > return;
> > }
> >  
> > memcpy(_header, image, sizeof(bmp_header));
> > -   memunmap(image);
> > +   early_memunmap(image, sizeof(bmp_header));
> > if (bmp_header.id != 0x4d42) {
> > pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> > (expected 0x4d42)\n",
> > bmp_header.id);
> > @@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
> > }
> > bgrt_image_size = bmp_header.size;
> >  
> > -   bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
> > MEMREMAP_WB);
> > -   if (!bgrt_image) {
> > -   pr_notice("Ignoring BGRT: failed to map image memory\n");
> > -   bgrt_image = NULL;
> > -   return;
> > -   }
> > -
> > -   efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
> > +   efi_mem_reserve(bgrt_tab.image_address, bgrt_image_size);
> >  }
> > --- linux-x86.orig/drivers/acpi/bgrt.c
> > +++ linux-x86/drivers/acpi/bgrt.c
> > @@ -15,40 +15,41 @@
> >  #include 
> >  #include 
> >  
> > +static void *bgrt_image;
> 
> [...]
> 
> > @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
> >  {
> > int ret;
> >  
> > -   if (!bgrt_image)
> > +   if (!bgrt_tab.image_address)
> > return -ENODEV;
> >  
> > +   bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> > + MEMREMAP_WB);
> > +   if (!bgrt_image) {
> > +   pr_notice("Ignoring BGRT: failed to map image memory\n");
> > +   bgrt_image = NULL;
> > +   return -ENOMEM;
> > +   }
> > +
> > bin_attr_image.private = bgrt_image;
> > bin_attr_image.size = bgrt_image_size;
> >  
> 
> Thanks,
> 
> Nicolai

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/12/17 at 12:54pm, Nicolai Stange wrote:
> On Thu, Jan 12 2017, Dave Young wrote:
> 
> > -void __init efi_bgrt_init(void)
> > +void __init efi_bgrt_init(struct acpi_table_header *table)
> >  {
> > -   acpi_status status;
> > void *image;
> > struct bmp_header bmp_header;
> >  
> > if (acpi_disabled)
> > return;
> >  
> > -   status = acpi_get_table("BGRT", 0,
> > -   (struct acpi_table_header **)_tab);
> > -   if (ACPI_FAILURE(status))
> > -   return;
> 
> 
> Not sure, but wouldn't it be safer to reverse the order of this assignment
> 
> > +   bgrt_tab = *(struct acpi_table_bgrt *)table;

Nicolai, sorry, I'm not sure I understand the comment, is it about above line?
Could you elaborate a bit?

> 
> and this length check
> 

I also do not get this :(

> > -   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> > +   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
> > pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> > -  bgrt_tab->header.length, sizeof(*bgrt_tab));
> > +  bgrt_tab.header.length, sizeof(bgrt_tab));
> > return;
> > }
> 
> ?
> 
> Also, from here on, all error paths should zero out
> bgrt_tab.image_address (or so) to signal failure to bgrt_init():
> bgrt_init() now checks for !bgrt_tab.image_address whereas before it had
> tested bgrt_image and the latter used to be set at the very end of
> efi_bgrt_init().
> 

Will do, thanks!

> 
> > -   if (bgrt_tab->version != 1) {
> > +   if (bgrt_tab.version != 1) {
> > pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> > -  bgrt_tab->version);
> > +  bgrt_tab.version);
> > return;
> > }
> > -   if (bgrt_tab->status & 0xfe) {
> > +   if (bgrt_tab.status & 0xfe) {
> > pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> > %u\n",
> > -  bgrt_tab->status);
> > +  bgrt_tab.status);
> > return;
> > }
> > -   if (bgrt_tab->image_type != 0) {
> > +   if (bgrt_tab.image_type != 0) {
> > pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
> > -  bgrt_tab->image_type);
> > +  bgrt_tab.image_type);
> > return;
> > }
> > -   if (!bgrt_tab->image_address) {
> > +   if (!bgrt_tab.image_address) {
> > pr_notice("Ignoring BGRT: null image address\n");
> > return;
> > }
> >  
> > -   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> > MEMREMAP_WB);
> > +   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
> > if (!image) {
> > pr_notice("Ignoring BGRT: failed to map image header memory\n");
> > return;
> > }
> >  
> > memcpy(_header, image, sizeof(bmp_header));
> > -   memunmap(image);
> > +   early_memunmap(image, sizeof(bmp_header));
> > if (bmp_header.id != 0x4d42) {
> > pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> > (expected 0x4d42)\n",
> > bmp_header.id);
> > @@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
> > }
> > bgrt_image_size = bmp_header.size;
> >  
> > -   bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
> > MEMREMAP_WB);
> > -   if (!bgrt_image) {
> > -   pr_notice("Ignoring BGRT: failed to map image memory\n");
> > -   bgrt_image = NULL;
> > -   return;
> > -   }
> > -
> > -   efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
> > +   efi_mem_reserve(bgrt_tab.image_address, bgrt_image_size);
> >  }
> > --- linux-x86.orig/drivers/acpi/bgrt.c
> > +++ linux-x86/drivers/acpi/bgrt.c
> > @@ -15,40 +15,41 @@
> >  #include 
> >  #include 
> >  
> > +static void *bgrt_image;
> 
> [...]
> 
> > @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
> >  {
> > int ret;
> >  
> > -   if (!bgrt_image)
> > +   if (!bgrt_tab.image_address)
> > return -ENODEV;
> >  
> > +   bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> > + MEMREMAP_WB);
> > +   if (!bgrt_image) {
> > +   pr_notice("Ignoring BGRT: failed to map image memory\n");
> > +   bgrt_image = NULL;
> > +   return -ENOMEM;
> > +   }
> > +
> > bin_attr_image.private = bgrt_image;
> > bin_attr_image.size = bgrt_image_size;
> >  
> 
> Thanks,
> 
> Nicolai

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
> On 12 January 2017 at 09:41, Dave Young  wrote:
> > Before invoking the arch specific handler, efi_mem_reserve() reserves
> > the given memory region through memblock.
> >
> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
> > memblock is dead and it should not be used any more.
> >
> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
> > in efi bgrt code still use memblock safely.
> >
> > Signed-off-by: Dave Young 
> 
> I know this is probably out of scope for you, but since we're moving
> things around, any chance we could do so in a manner that will enable
> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
> 

I'm happy to do so, Bhupesh Sharma  said he had
some investigation on that already, I would like to ask him to help on that.

Already cced him..

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
On 01/12/17 at 04:20pm, Ard Biesheuvel wrote:
> On 12 January 2017 at 09:41, Dave Young  wrote:
> > Before invoking the arch specific handler, efi_mem_reserve() reserves
> > the given memory region through memblock.
> >
> > efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
> > memblock is dead and it should not be used any more.
> >
> > efi bgrt code depend on acpi intialization to get the bgrt acpi table,
> > moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
> > in efi bgrt code still use memblock safely.
> >
> > Signed-off-by: Dave Young 
> 
> I know this is probably out of scope for you, but since we're moving
> things around, any chance we could do so in a manner that will enable
> BGRT support for arm64/ACPI? Happy to test/collaborate on this.
> 

I'm happy to do so, Bhupesh Sharma  said he had
some investigation on that already, I would like to ask him to help on that.

Already cced him..

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Ard Biesheuvel
On 12 January 2017 at 09:41, Dave Young  wrote:
> Before invoking the arch specific handler, efi_mem_reserve() reserves
> the given memory region through memblock.
>
> efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
> memblock is dead and it should not be used any more.
>
> efi bgrt code depend on acpi intialization to get the bgrt acpi table,
> moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
> in efi bgrt code still use memblock safely.
>
> Signed-off-by: Dave Young 

I know this is probably out of scope for you, but since we're moving
things around, any chance we could do so in a manner that will enable
BGRT support for arm64/ACPI? Happy to test/collaborate on this.


> ---
>  arch/x86/kernel/acpi/boot.c  |   12 +++
>  arch/x86/platform/efi/efi-bgrt.c |   42 
> +--
>  arch/x86/platform/efi/efi.c  |5 
>  drivers/acpi/bgrt.c  |   21 +--
>  include/linux/efi-bgrt.h |7 ++
>  init/main.c  |1
>  6 files changed, 45 insertions(+), 43 deletions(-)
>
> --- linux-x86.orig/arch/x86/kernel/acpi/boot.c
> +++ linux-x86/arch/x86/kernel/acpi/boot.c
> @@ -35,6 +35,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>
>  #include 
>  #include 
> @@ -1557,6 +1558,14 @@ int __init early_acpi_boot_init(void)
> return 0;
>  }
>
> +#ifdef CONFIG_ACPI_BGRT
> +static int __init acpi_parse_bgrt(struct acpi_table_header *table)
> +{
> +   efi_bgrt_init(table);
> +   return 0;
> +}
> +#endif
> +
>  int __init acpi_boot_init(void)
>  {
> /* those are executed after early-quirks are executed */
> @@ -1581,6 +1590,9 @@ int __init acpi_boot_init(void)
> acpi_process_madt();
>
> acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
> +#ifdef CONFIG_ACPI_BGRT
> +   acpi_table_parse("BGRT", acpi_parse_bgrt);
> +#endif
>
> if (!acpi_noirq)
> x86_init.pci.init = pci_acpi_init;
> --- linux-x86.orig/arch/x86/platform/efi/efi-bgrt.c
> +++ linux-x86/arch/x86/platform/efi/efi-bgrt.c
> @@ -19,8 +19,7 @@
>  #include 
>  #include 
>
> -struct acpi_table_bgrt *bgrt_tab;
> -void *__initdata bgrt_image;
> +struct acpi_table_bgrt bgrt_tab;
>  size_t __initdata bgrt_image_size;
>
>  struct bmp_header {
> @@ -28,53 +27,49 @@ struct bmp_header {
> u32 size;
>  } __packed;
>
> -void __init efi_bgrt_init(void)
> +void __init efi_bgrt_init(struct acpi_table_header *table)
>  {
> -   acpi_status status;
> void *image;
> struct bmp_header bmp_header;
>
> if (acpi_disabled)
> return;
>
> -   status = acpi_get_table("BGRT", 0,
> -   (struct acpi_table_header **)_tab);
> -   if (ACPI_FAILURE(status))
> -   return;
> +   bgrt_tab = *(struct acpi_table_bgrt *)table;
>
> -   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> +   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
> pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> -  bgrt_tab->header.length, sizeof(*bgrt_tab));
> +  bgrt_tab.header.length, sizeof(bgrt_tab));
> return;
> }
> -   if (bgrt_tab->version != 1) {
> +   if (bgrt_tab.version != 1) {
> pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> -  bgrt_tab->version);
> +  bgrt_tab.version);
> return;
> }
> -   if (bgrt_tab->status & 0xfe) {
> +   if (bgrt_tab.status & 0xfe) {
> pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> %u\n",
> -  bgrt_tab->status);
> +  bgrt_tab.status);
> return;
> }
> -   if (bgrt_tab->image_type != 0) {
> +   if (bgrt_tab.image_type != 0) {
> pr_notice("Ignoring BGRT: invalid image type %u (expected 
> 0)\n",
> -  bgrt_tab->image_type);
> +  bgrt_tab.image_type);
> return;
> }
> -   if (!bgrt_tab->image_address) {
> +   if (!bgrt_tab.image_address) {
> pr_notice("Ignoring BGRT: null image address\n");
> return;
> }
>
> -   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> MEMREMAP_WB);
> +   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
> if (!image) {
> pr_notice("Ignoring BGRT: failed to map image header 
> memory\n");
> return;
> }
>
> memcpy(_header, image, sizeof(bmp_header));
> -   memunmap(image);
> +   early_memunmap(image, sizeof(bmp_header));
> if (bmp_header.id != 0x4d42) {
> pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> (expected 0x4d42)\n",
>   

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Ard Biesheuvel
On 12 January 2017 at 09:41, Dave Young  wrote:
> Before invoking the arch specific handler, efi_mem_reserve() reserves
> the given memory region through memblock.
>
> efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
> memblock is dead and it should not be used any more.
>
> efi bgrt code depend on acpi intialization to get the bgrt acpi table,
> moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
> in efi bgrt code still use memblock safely.
>
> Signed-off-by: Dave Young 

I know this is probably out of scope for you, but since we're moving
things around, any chance we could do so in a manner that will enable
BGRT support for arm64/ACPI? Happy to test/collaborate on this.


> ---
>  arch/x86/kernel/acpi/boot.c  |   12 +++
>  arch/x86/platform/efi/efi-bgrt.c |   42 
> +--
>  arch/x86/platform/efi/efi.c  |5 
>  drivers/acpi/bgrt.c  |   21 +--
>  include/linux/efi-bgrt.h |7 ++
>  init/main.c  |1
>  6 files changed, 45 insertions(+), 43 deletions(-)
>
> --- linux-x86.orig/arch/x86/kernel/acpi/boot.c
> +++ linux-x86/arch/x86/kernel/acpi/boot.c
> @@ -35,6 +35,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>
>  #include 
>  #include 
> @@ -1557,6 +1558,14 @@ int __init early_acpi_boot_init(void)
> return 0;
>  }
>
> +#ifdef CONFIG_ACPI_BGRT
> +static int __init acpi_parse_bgrt(struct acpi_table_header *table)
> +{
> +   efi_bgrt_init(table);
> +   return 0;
> +}
> +#endif
> +
>  int __init acpi_boot_init(void)
>  {
> /* those are executed after early-quirks are executed */
> @@ -1581,6 +1590,9 @@ int __init acpi_boot_init(void)
> acpi_process_madt();
>
> acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
> +#ifdef CONFIG_ACPI_BGRT
> +   acpi_table_parse("BGRT", acpi_parse_bgrt);
> +#endif
>
> if (!acpi_noirq)
> x86_init.pci.init = pci_acpi_init;
> --- linux-x86.orig/arch/x86/platform/efi/efi-bgrt.c
> +++ linux-x86/arch/x86/platform/efi/efi-bgrt.c
> @@ -19,8 +19,7 @@
>  #include 
>  #include 
>
> -struct acpi_table_bgrt *bgrt_tab;
> -void *__initdata bgrt_image;
> +struct acpi_table_bgrt bgrt_tab;
>  size_t __initdata bgrt_image_size;
>
>  struct bmp_header {
> @@ -28,53 +27,49 @@ struct bmp_header {
> u32 size;
>  } __packed;
>
> -void __init efi_bgrt_init(void)
> +void __init efi_bgrt_init(struct acpi_table_header *table)
>  {
> -   acpi_status status;
> void *image;
> struct bmp_header bmp_header;
>
> if (acpi_disabled)
> return;
>
> -   status = acpi_get_table("BGRT", 0,
> -   (struct acpi_table_header **)_tab);
> -   if (ACPI_FAILURE(status))
> -   return;
> +   bgrt_tab = *(struct acpi_table_bgrt *)table;
>
> -   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> +   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
> pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> -  bgrt_tab->header.length, sizeof(*bgrt_tab));
> +  bgrt_tab.header.length, sizeof(bgrt_tab));
> return;
> }
> -   if (bgrt_tab->version != 1) {
> +   if (bgrt_tab.version != 1) {
> pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> -  bgrt_tab->version);
> +  bgrt_tab.version);
> return;
> }
> -   if (bgrt_tab->status & 0xfe) {
> +   if (bgrt_tab.status & 0xfe) {
> pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> %u\n",
> -  bgrt_tab->status);
> +  bgrt_tab.status);
> return;
> }
> -   if (bgrt_tab->image_type != 0) {
> +   if (bgrt_tab.image_type != 0) {
> pr_notice("Ignoring BGRT: invalid image type %u (expected 
> 0)\n",
> -  bgrt_tab->image_type);
> +  bgrt_tab.image_type);
> return;
> }
> -   if (!bgrt_tab->image_address) {
> +   if (!bgrt_tab.image_address) {
> pr_notice("Ignoring BGRT: null image address\n");
> return;
> }
>
> -   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> MEMREMAP_WB);
> +   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
> if (!image) {
> pr_notice("Ignoring BGRT: failed to map image header 
> memory\n");
> return;
> }
>
> memcpy(_header, image, sizeof(bmp_header));
> -   memunmap(image);
> +   early_memunmap(image, sizeof(bmp_header));
> if (bmp_header.id != 0x4d42) {
> pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> (expected 0x4d42)\n",
> bmp_header.id);
> @@ 

Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Nicolai Stange
On Thu, Jan 12 2017, Dave Young wrote:

> -void __init efi_bgrt_init(void)
> +void __init efi_bgrt_init(struct acpi_table_header *table)
>  {
> - acpi_status status;
>   void *image;
>   struct bmp_header bmp_header;
>  
>   if (acpi_disabled)
>   return;
>  
> - status = acpi_get_table("BGRT", 0,
> - (struct acpi_table_header **)_tab);
> - if (ACPI_FAILURE(status))
> - return;


Not sure, but wouldn't it be safer to reverse the order of this assignment

> + bgrt_tab = *(struct acpi_table_bgrt *)table;

and this length check

> - if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> + if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
>   pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> -bgrt_tab->header.length, sizeof(*bgrt_tab));
> +bgrt_tab.header.length, sizeof(bgrt_tab));
>   return;
>   }

?

Also, from here on, all error paths should zero out
bgrt_tab.image_address (or so) to signal failure to bgrt_init():
bgrt_init() now checks for !bgrt_tab.image_address whereas before it had
tested bgrt_image and the latter used to be set at the very end of
efi_bgrt_init().


> - if (bgrt_tab->version != 1) {
> + if (bgrt_tab.version != 1) {
>   pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> -bgrt_tab->version);
> +bgrt_tab.version);
>   return;
>   }
> - if (bgrt_tab->status & 0xfe) {
> + if (bgrt_tab.status & 0xfe) {
>   pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> %u\n",
> -bgrt_tab->status);
> +bgrt_tab.status);
>   return;
>   }
> - if (bgrt_tab->image_type != 0) {
> + if (bgrt_tab.image_type != 0) {
>   pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
> -bgrt_tab->image_type);
> +bgrt_tab.image_type);
>   return;
>   }
> - if (!bgrt_tab->image_address) {
> + if (!bgrt_tab.image_address) {
>   pr_notice("Ignoring BGRT: null image address\n");
>   return;
>   }
>  
> - image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> MEMREMAP_WB);
> + image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
>   if (!image) {
>   pr_notice("Ignoring BGRT: failed to map image header memory\n");
>   return;
>   }
>  
>   memcpy(_header, image, sizeof(bmp_header));
> - memunmap(image);
> + early_memunmap(image, sizeof(bmp_header));
>   if (bmp_header.id != 0x4d42) {
>   pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> (expected 0x4d42)\n",
>   bmp_header.id);
> @@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
>   }
>   bgrt_image_size = bmp_header.size;
>  
> - bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
> MEMREMAP_WB);
> - if (!bgrt_image) {
> - pr_notice("Ignoring BGRT: failed to map image memory\n");
> - bgrt_image = NULL;
> - return;
> - }
> -
> - efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
> + efi_mem_reserve(bgrt_tab.image_address, bgrt_image_size);
>  }
> --- linux-x86.orig/drivers/acpi/bgrt.c
> +++ linux-x86/drivers/acpi/bgrt.c
> @@ -15,40 +15,41 @@
>  #include 
>  #include 
>  
> +static void *bgrt_image;

[...]

> @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
>  {
>   int ret;
>  
> - if (!bgrt_image)
> + if (!bgrt_tab.image_address)
>   return -ENODEV;
>  
> + bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> +   MEMREMAP_WB);
> + if (!bgrt_image) {
> + pr_notice("Ignoring BGRT: failed to map image memory\n");
> + bgrt_image = NULL;
> + return -ENOMEM;
> + }
> +
>   bin_attr_image.private = bgrt_image;
>   bin_attr_image.size = bgrt_image_size;
>  

Thanks,

Nicolai


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Nicolai Stange
On Thu, Jan 12 2017, Dave Young wrote:

> -void __init efi_bgrt_init(void)
> +void __init efi_bgrt_init(struct acpi_table_header *table)
>  {
> - acpi_status status;
>   void *image;
>   struct bmp_header bmp_header;
>  
>   if (acpi_disabled)
>   return;
>  
> - status = acpi_get_table("BGRT", 0,
> - (struct acpi_table_header **)_tab);
> - if (ACPI_FAILURE(status))
> - return;


Not sure, but wouldn't it be safer to reverse the order of this assignment

> + bgrt_tab = *(struct acpi_table_bgrt *)table;

and this length check

> - if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
> + if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
>   pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
> -bgrt_tab->header.length, sizeof(*bgrt_tab));
> +bgrt_tab.header.length, sizeof(bgrt_tab));
>   return;
>   }

?

Also, from here on, all error paths should zero out
bgrt_tab.image_address (or so) to signal failure to bgrt_init():
bgrt_init() now checks for !bgrt_tab.image_address whereas before it had
tested bgrt_image and the latter used to be set at the very end of
efi_bgrt_init().


> - if (bgrt_tab->version != 1) {
> + if (bgrt_tab.version != 1) {
>   pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
> -bgrt_tab->version);
> +bgrt_tab.version);
>   return;
>   }
> - if (bgrt_tab->status & 0xfe) {
> + if (bgrt_tab.status & 0xfe) {
>   pr_notice("Ignoring BGRT: reserved status bits are non-zero 
> %u\n",
> -bgrt_tab->status);
> +bgrt_tab.status);
>   return;
>   }
> - if (bgrt_tab->image_type != 0) {
> + if (bgrt_tab.image_type != 0) {
>   pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
> -bgrt_tab->image_type);
> +bgrt_tab.image_type);
>   return;
>   }
> - if (!bgrt_tab->image_address) {
> + if (!bgrt_tab.image_address) {
>   pr_notice("Ignoring BGRT: null image address\n");
>   return;
>   }
>  
> - image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
> MEMREMAP_WB);
> + image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
>   if (!image) {
>   pr_notice("Ignoring BGRT: failed to map image header memory\n");
>   return;
>   }
>  
>   memcpy(_header, image, sizeof(bmp_header));
> - memunmap(image);
> + early_memunmap(image, sizeof(bmp_header));
>   if (bmp_header.id != 0x4d42) {
>   pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
> (expected 0x4d42)\n",
>   bmp_header.id);
> @@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
>   }
>   bgrt_image_size = bmp_header.size;
>  
> - bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
> MEMREMAP_WB);
> - if (!bgrt_image) {
> - pr_notice("Ignoring BGRT: failed to map image memory\n");
> - bgrt_image = NULL;
> - return;
> - }
> -
> - efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
> + efi_mem_reserve(bgrt_tab.image_address, bgrt_image_size);
>  }
> --- linux-x86.orig/drivers/acpi/bgrt.c
> +++ linux-x86/drivers/acpi/bgrt.c
> @@ -15,40 +15,41 @@
>  #include 
>  #include 
>  
> +static void *bgrt_image;

[...]

> @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
>  {
>   int ret;
>  
> - if (!bgrt_image)
> + if (!bgrt_tab.image_address)
>   return -ENODEV;
>  
> + bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> +   MEMREMAP_WB);
> + if (!bgrt_image) {
> + pr_notice("Ignoring BGRT: failed to map image memory\n");
> + bgrt_image = NULL;
> + return -ENOMEM;
> + }
> +
>   bin_attr_image.private = bgrt_image;
>   bin_attr_image.size = bgrt_image_size;
>  

Thanks,

Nicolai


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
[snip]
> --- linux-x86.orig/drivers/acpi/bgrt.c
> +++ linux-x86/drivers/acpi/bgrt.c

[snip]
>  
> @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
>  {
>   int ret;
>  
> - if (!bgrt_image)
> + if (!bgrt_tab.image_address)
>   return -ENODEV;
>  
> + bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> +   MEMREMAP_WB);
> + if (!bgrt_image) {
> + pr_notice("Ignoring BGRT: failed to map image memory\n");
> + bgrt_image = NULL;
> + return -ENOMEM;
> + }
> +

Oops, later error path need unmap bgrt_image, will update in next
version after collecting more comments.

Also bgrt_image = NULL is useless, will drop it.

>   bin_attr_image.private = bgrt_image;
>   bin_attr_image.size = bgrt_image_size;
>  

Thanks
Dave


Re: [PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
[snip]
> --- linux-x86.orig/drivers/acpi/bgrt.c
> +++ linux-x86/drivers/acpi/bgrt.c

[snip]
>  
> @@ -84,9 +85,17 @@ static int __init bgrt_init(void)
>  {
>   int ret;
>  
> - if (!bgrt_image)
> + if (!bgrt_tab.image_address)
>   return -ENODEV;
>  
> + bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
> +   MEMREMAP_WB);
> + if (!bgrt_image) {
> + pr_notice("Ignoring BGRT: failed to map image memory\n");
> + bgrt_image = NULL;
> + return -ENOMEM;
> + }
> +

Oops, later error path need unmap bgrt_image, will update in next
version after collecting more comments.

Also bgrt_image = NULL is useless, will drop it.

>   bin_attr_image.private = bgrt_image;
>   bin_attr_image.size = bgrt_image_size;
>  

Thanks
Dave


[PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
Before invoking the arch specific handler, efi_mem_reserve() reserves
the given memory region through memblock.

efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
memblock is dead and it should not be used any more.

efi bgrt code depend on acpi intialization to get the bgrt acpi table,
moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
in efi bgrt code still use memblock safely. 

Signed-off-by: Dave Young 
---
 arch/x86/kernel/acpi/boot.c  |   12 +++
 arch/x86/platform/efi/efi-bgrt.c |   42 +--
 arch/x86/platform/efi/efi.c  |5 
 drivers/acpi/bgrt.c  |   21 +--
 include/linux/efi-bgrt.h |7 ++
 init/main.c  |1 
 6 files changed, 45 insertions(+), 43 deletions(-)

--- linux-x86.orig/arch/x86/kernel/acpi/boot.c
+++ linux-x86/arch/x86/kernel/acpi/boot.c
@@ -35,6 +35,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -1557,6 +1558,14 @@ int __init early_acpi_boot_init(void)
return 0;
 }
 
+#ifdef CONFIG_ACPI_BGRT
+static int __init acpi_parse_bgrt(struct acpi_table_header *table)
+{
+   efi_bgrt_init(table);
+   return 0;
+}
+#endif
+
 int __init acpi_boot_init(void)
 {
/* those are executed after early-quirks are executed */
@@ -1581,6 +1590,9 @@ int __init acpi_boot_init(void)
acpi_process_madt();
 
acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
+#ifdef CONFIG_ACPI_BGRT
+   acpi_table_parse("BGRT", acpi_parse_bgrt);
+#endif
 
if (!acpi_noirq)
x86_init.pci.init = pci_acpi_init;
--- linux-x86.orig/arch/x86/platform/efi/efi-bgrt.c
+++ linux-x86/arch/x86/platform/efi/efi-bgrt.c
@@ -19,8 +19,7 @@
 #include 
 #include 
 
-struct acpi_table_bgrt *bgrt_tab;
-void *__initdata bgrt_image;
+struct acpi_table_bgrt bgrt_tab;
 size_t __initdata bgrt_image_size;
 
 struct bmp_header {
@@ -28,53 +27,49 @@ struct bmp_header {
u32 size;
 } __packed;
 
-void __init efi_bgrt_init(void)
+void __init efi_bgrt_init(struct acpi_table_header *table)
 {
-   acpi_status status;
void *image;
struct bmp_header bmp_header;
 
if (acpi_disabled)
return;
 
-   status = acpi_get_table("BGRT", 0,
-   (struct acpi_table_header **)_tab);
-   if (ACPI_FAILURE(status))
-   return;
+   bgrt_tab = *(struct acpi_table_bgrt *)table;
 
-   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
+   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
-  bgrt_tab->header.length, sizeof(*bgrt_tab));
+  bgrt_tab.header.length, sizeof(bgrt_tab));
return;
}
-   if (bgrt_tab->version != 1) {
+   if (bgrt_tab.version != 1) {
pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
-  bgrt_tab->version);
+  bgrt_tab.version);
return;
}
-   if (bgrt_tab->status & 0xfe) {
+   if (bgrt_tab.status & 0xfe) {
pr_notice("Ignoring BGRT: reserved status bits are non-zero 
%u\n",
-  bgrt_tab->status);
+  bgrt_tab.status);
return;
}
-   if (bgrt_tab->image_type != 0) {
+   if (bgrt_tab.image_type != 0) {
pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
-  bgrt_tab->image_type);
+  bgrt_tab.image_type);
return;
}
-   if (!bgrt_tab->image_address) {
+   if (!bgrt_tab.image_address) {
pr_notice("Ignoring BGRT: null image address\n");
return;
}
 
-   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
MEMREMAP_WB);
+   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
if (!image) {
pr_notice("Ignoring BGRT: failed to map image header memory\n");
return;
}
 
memcpy(_header, image, sizeof(bmp_header));
-   memunmap(image);
+   early_memunmap(image, sizeof(bmp_header));
if (bmp_header.id != 0x4d42) {
pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
(expected 0x4d42)\n",
bmp_header.id);
@@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
}
bgrt_image_size = bmp_header.size;
 
-   bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
MEMREMAP_WB);
-   if (!bgrt_image) {
-   pr_notice("Ignoring BGRT: failed to map image memory\n");
-   bgrt_image = NULL;
-   return;
-   }
-
-   efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
+   efi_mem_reserve(bgrt_tab.image_address, 

[PATCH 2/4] efi/x86: move efi bgrt init code to early init code

2017-01-12 Thread Dave Young
Before invoking the arch specific handler, efi_mem_reserve() reserves
the given memory region through memblock.

efi_bgrt_init will call efi_mem_reserve after mm_init(), at that time
memblock is dead and it should not be used any more.

efi bgrt code depend on acpi intialization to get the bgrt acpi table,
moving bgrt parsing to acpi early boot code can make sure efi_mem_reserve
in efi bgrt code still use memblock safely. 

Signed-off-by: Dave Young 
---
 arch/x86/kernel/acpi/boot.c  |   12 +++
 arch/x86/platform/efi/efi-bgrt.c |   42 +--
 arch/x86/platform/efi/efi.c  |5 
 drivers/acpi/bgrt.c  |   21 +--
 include/linux/efi-bgrt.h |7 ++
 init/main.c  |1 
 6 files changed, 45 insertions(+), 43 deletions(-)

--- linux-x86.orig/arch/x86/kernel/acpi/boot.c
+++ linux-x86/arch/x86/kernel/acpi/boot.c
@@ -35,6 +35,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -1557,6 +1558,14 @@ int __init early_acpi_boot_init(void)
return 0;
 }
 
+#ifdef CONFIG_ACPI_BGRT
+static int __init acpi_parse_bgrt(struct acpi_table_header *table)
+{
+   efi_bgrt_init(table);
+   return 0;
+}
+#endif
+
 int __init acpi_boot_init(void)
 {
/* those are executed after early-quirks are executed */
@@ -1581,6 +1590,9 @@ int __init acpi_boot_init(void)
acpi_process_madt();
 
acpi_table_parse(ACPI_SIG_HPET, acpi_parse_hpet);
+#ifdef CONFIG_ACPI_BGRT
+   acpi_table_parse("BGRT", acpi_parse_bgrt);
+#endif
 
if (!acpi_noirq)
x86_init.pci.init = pci_acpi_init;
--- linux-x86.orig/arch/x86/platform/efi/efi-bgrt.c
+++ linux-x86/arch/x86/platform/efi/efi-bgrt.c
@@ -19,8 +19,7 @@
 #include 
 #include 
 
-struct acpi_table_bgrt *bgrt_tab;
-void *__initdata bgrt_image;
+struct acpi_table_bgrt bgrt_tab;
 size_t __initdata bgrt_image_size;
 
 struct bmp_header {
@@ -28,53 +27,49 @@ struct bmp_header {
u32 size;
 } __packed;
 
-void __init efi_bgrt_init(void)
+void __init efi_bgrt_init(struct acpi_table_header *table)
 {
-   acpi_status status;
void *image;
struct bmp_header bmp_header;
 
if (acpi_disabled)
return;
 
-   status = acpi_get_table("BGRT", 0,
-   (struct acpi_table_header **)_tab);
-   if (ACPI_FAILURE(status))
-   return;
+   bgrt_tab = *(struct acpi_table_bgrt *)table;
 
-   if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
+   if (bgrt_tab.header.length < sizeof(bgrt_tab)) {
pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
-  bgrt_tab->header.length, sizeof(*bgrt_tab));
+  bgrt_tab.header.length, sizeof(bgrt_tab));
return;
}
-   if (bgrt_tab->version != 1) {
+   if (bgrt_tab.version != 1) {
pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
-  bgrt_tab->version);
+  bgrt_tab.version);
return;
}
-   if (bgrt_tab->status & 0xfe) {
+   if (bgrt_tab.status & 0xfe) {
pr_notice("Ignoring BGRT: reserved status bits are non-zero 
%u\n",
-  bgrt_tab->status);
+  bgrt_tab.status);
return;
}
-   if (bgrt_tab->image_type != 0) {
+   if (bgrt_tab.image_type != 0) {
pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
-  bgrt_tab->image_type);
+  bgrt_tab.image_type);
return;
}
-   if (!bgrt_tab->image_address) {
+   if (!bgrt_tab.image_address) {
pr_notice("Ignoring BGRT: null image address\n");
return;
}
 
-   image = memremap(bgrt_tab->image_address, sizeof(bmp_header), 
MEMREMAP_WB);
+   image = early_memremap(bgrt_tab.image_address, sizeof(bmp_header));
if (!image) {
pr_notice("Ignoring BGRT: failed to map image header memory\n");
return;
}
 
memcpy(_header, image, sizeof(bmp_header));
-   memunmap(image);
+   early_memunmap(image, sizeof(bmp_header));
if (bmp_header.id != 0x4d42) {
pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x 
(expected 0x4d42)\n",
bmp_header.id);
@@ -82,12 +77,5 @@ void __init efi_bgrt_init(void)
}
bgrt_image_size = bmp_header.size;
 
-   bgrt_image = memremap(bgrt_tab->image_address, bmp_header.size, 
MEMREMAP_WB);
-   if (!bgrt_image) {
-   pr_notice("Ignoring BGRT: failed to map image memory\n");
-   bgrt_image = NULL;
-   return;
-   }
-
-   efi_mem_reserve(bgrt_tab->image_address, bgrt_image_size);
+   efi_mem_reserve(bgrt_tab.image_address, bgrt_image_size);
 }
---