2020-06-26 00:08:08

by Atish Patra

[permalink] [raw]
Subject: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

32bit gcc doesn't support modulo operation on 64 bit data. It results in
a __umoddi3 error while building EFI for 32 bit.

Use bitwise operations instead of modulo operations to fix the issue.

Signed-off-by: Atish Patra <[email protected]>
---
drivers/firmware/efi/libstub/alignedmem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
index cc89c4d6196f..1de9878ddd3a 100644
--- a/drivers/firmware/efi/libstub/alignedmem.c
+++ b/drivers/firmware/efi/libstub/alignedmem.c
@@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
*addr = ALIGN((unsigned long)alloc_addr, align);

if (slack > 0) {
- int l = (alloc_addr % align) / EFI_PAGE_SIZE;
+ int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;

if (l) {
efi_bs_call(free_pages, alloc_addr, slack - l + 1);
--
2.24.0


2020-06-26 04:46:37

by Heinrich Schuchardt

[permalink] [raw]
Subject: Re: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

On 6/26/20 1:45 AM, Atish Patra wrote:
> 32bit gcc doesn't support modulo operation on 64 bit data. It results in
> a __umoddi3 error while building EFI for 32 bit.
>
> Use bitwise operations instead of modulo operations to fix the issue.
>
> Signed-off-by: Atish Patra <[email protected]>
> ---
> drivers/firmware/efi/libstub/alignedmem.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
> index cc89c4d6196f..1de9878ddd3a 100644
> --- a/drivers/firmware/efi/libstub/alignedmem.c
> +++ b/drivers/firmware/efi/libstub/alignedmem.c
> @@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
> *addr = ALIGN((unsigned long)alloc_addr, align);
>
> if (slack > 0) {
> - int l = (alloc_addr % align) / EFI_PAGE_SIZE;
> + int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;

Here you rely on the compiler to silently convert the division by
EFI_PAGE_SIZE into a right shift. Wouldn't it be cleaner to use
EFI_PAGE_SHIFT to explicitly avoid a dependency on __udivdi3()?

slack = (align >> EFI_PAGE_SHIFT) - 1;
...
int l = (alloc_addr & (align - 1)) >> EFI_PAGE_SHIFT;

Best regards

Heinrich

>
> if (l) {
> efi_bs_call(free_pages, alloc_addr, slack - l + 1);
>

2020-06-26 21:56:58

by Atish Patra

[permalink] [raw]
Subject: Re: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

On Thu, Jun 25, 2020 at 7:43 PM Heinrich Schuchardt <[email protected]> wrote:
>
> On 6/26/20 1:45 AM, Atish Patra wrote:
> > 32bit gcc doesn't support modulo operation on 64 bit data. It results in
> > a __umoddi3 error while building EFI for 32 bit.
> >
> > Use bitwise operations instead of modulo operations to fix the issue.
> >
> > Signed-off-by: Atish Patra <[email protected]>
> > ---
> > drivers/firmware/efi/libstub/alignedmem.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
> > index cc89c4d6196f..1de9878ddd3a 100644
> > --- a/drivers/firmware/efi/libstub/alignedmem.c
> > +++ b/drivers/firmware/efi/libstub/alignedmem.c
> > @@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
> > *addr = ALIGN((unsigned long)alloc_addr, align);
> >
> > if (slack > 0) {
> > - int l = (alloc_addr % align) / EFI_PAGE_SIZE;
> > + int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
>
> Here you rely on the compiler to silently convert the division by
> EFI_PAGE_SIZE into a right shift. Wouldn't it be cleaner to use
> EFI_PAGE_SHIFT to explicitly avoid a dependency on __udivdi3()?
>
> slack = (align >> EFI_PAGE_SHIFT) - 1;
> ...
> int l = (alloc_addr & (align - 1)) >> EFI_PAGE_SHIFT;
>

Sure. I will update it in the next version. Thanks for the suggestion.
> Best regards
>
> Heinrich
>
> >
> > if (l) {
> > efi_bs_call(free_pages, alloc_addr, slack - l + 1);
> >
>


--
Regards,
Atish

2020-06-26 22:05:43

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

On Fri, 26 Jun 2020 at 23:56, Atish Patra <[email protected]> wrote:
>
> On Thu, Jun 25, 2020 at 7:43 PM Heinrich Schuchardt <[email protected]> wrote:
> >
> > On 6/26/20 1:45 AM, Atish Patra wrote:
> > > 32bit gcc doesn't support modulo operation on 64 bit data. It results in
> > > a __umoddi3 error while building EFI for 32 bit.
> > >
> > > Use bitwise operations instead of modulo operations to fix the issue.
> > >
> > > Signed-off-by: Atish Patra <[email protected]>
> > > ---
> > > drivers/firmware/efi/libstub/alignedmem.c | 2 +-
> > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
> > > index cc89c4d6196f..1de9878ddd3a 100644
> > > --- a/drivers/firmware/efi/libstub/alignedmem.c
> > > +++ b/drivers/firmware/efi/libstub/alignedmem.c
> > > @@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
> > > *addr = ALIGN((unsigned long)alloc_addr, align);
> > >
> > > if (slack > 0) {
> > > - int l = (alloc_addr % align) / EFI_PAGE_SIZE;
> > > + int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
> >
> > Here you rely on the compiler to silently convert the division by
> > EFI_PAGE_SIZE into a right shift. Wouldn't it be cleaner to use
> > EFI_PAGE_SHIFT to explicitly avoid a dependency on __udivdi3()?
> >
> > slack = (align >> EFI_PAGE_SHIFT) - 1;
> > ...
> > int l = (alloc_addr & (align - 1)) >> EFI_PAGE_SHIFT;
> >
>
> Sure. I will update it in the next version. Thanks for the suggestion.

Please don't. Dividing by EFI_PAGE_SIZE is perfectly fine, and is more readable.

2020-06-27 09:25:40

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

On Sat, 27 Jun 2020 at 00:03, Ard Biesheuvel <[email protected]> wrote:
>
> On Fri, 26 Jun 2020 at 23:56, Atish Patra <[email protected]> wrote:
> >
> > On Thu, Jun 25, 2020 at 7:43 PM Heinrich Schuchardt <[email protected]> wrote:
> > >
> > > On 6/26/20 1:45 AM, Atish Patra wrote:
> > > > 32bit gcc doesn't support modulo operation on 64 bit data. It results in
> > > > a __umoddi3 error while building EFI for 32 bit.
> > > >
> > > > Use bitwise operations instead of modulo operations to fix the issue.
> > > >
> > > > Signed-off-by: Atish Patra <[email protected]>
> > > > ---
> > > > drivers/firmware/efi/libstub/alignedmem.c | 2 +-
> > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
> > > > index cc89c4d6196f..1de9878ddd3a 100644
> > > > --- a/drivers/firmware/efi/libstub/alignedmem.c
> > > > +++ b/drivers/firmware/efi/libstub/alignedmem.c
> > > > @@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
> > > > *addr = ALIGN((unsigned long)alloc_addr, align);
> > > >
> > > > if (slack > 0) {
> > > > - int l = (alloc_addr % align) / EFI_PAGE_SIZE;
> > > > + int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
> > >
> > > Here you rely on the compiler to silently convert the division by
> > > EFI_PAGE_SIZE into a right shift. Wouldn't it be cleaner to use
> > > EFI_PAGE_SHIFT to explicitly avoid a dependency on __udivdi3()?
> > >
> > > slack = (align >> EFI_PAGE_SHIFT) - 1;
> > > ...
> > > int l = (alloc_addr & (align - 1)) >> EFI_PAGE_SHIFT;
> > >
> >
> > Sure. I will update it in the next version. Thanks for the suggestion.
>
> Please don't. Dividing by EFI_PAGE_SIZE is perfectly fine, and is more readable.

Actually, I will take this patch as a fix right away - 32-bit ARM has
the same issue (although it does not actually incorporate this object
file)

In the meantime, please check how the stub gets pulled into your
kernel: the idea of a static library is that only the objects that are
used are included in the final build, but this turned out to be broken
for arm64 [0]. IOW, simply applying a similar change might fix your
build issue as well (assuming RISC-V does not actually call
efi_allocate_pages_aligned() anywhere)

[0] https://lore.kernel.org/linux-arm-kernel/[email protected]/

2020-06-29 20:11:43

by Atish Patra

[permalink] [raw]
Subject: Re: [RFC PATCH 01/11] efi: Fix gcc error around __umoddi3 for 32 bit builds

On Sat, Jun 27, 2020 at 2:22 AM Ard Biesheuvel <[email protected]> wrote:
>
> On Sat, 27 Jun 2020 at 00:03, Ard Biesheuvel <[email protected]> wrote:
> >
> > On Fri, 26 Jun 2020 at 23:56, Atish Patra <[email protected]> wrote:
> > >
> > > On Thu, Jun 25, 2020 at 7:43 PM Heinrich Schuchardt <[email protected]> wrote:
> > > >
> > > > On 6/26/20 1:45 AM, Atish Patra wrote:
> > > > > 32bit gcc doesn't support modulo operation on 64 bit data. It results in
> > > > > a __umoddi3 error while building EFI for 32 bit.
> > > > >
> > > > > Use bitwise operations instead of modulo operations to fix the issue.
> > > > >
> > > > > Signed-off-by: Atish Patra <[email protected]>
> > > > > ---
> > > > > drivers/firmware/efi/libstub/alignedmem.c | 2 +-
> > > > > 1 file changed, 1 insertion(+), 1 deletion(-)
> > > > >
> > > > > diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
> > > > > index cc89c4d6196f..1de9878ddd3a 100644
> > > > > --- a/drivers/firmware/efi/libstub/alignedmem.c
> > > > > +++ b/drivers/firmware/efi/libstub/alignedmem.c
> > > > > @@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
> > > > > *addr = ALIGN((unsigned long)alloc_addr, align);
> > > > >
> > > > > if (slack > 0) {
> > > > > - int l = (alloc_addr % align) / EFI_PAGE_SIZE;
> > > > > + int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
> > > >
> > > > Here you rely on the compiler to silently convert the division by
> > > > EFI_PAGE_SIZE into a right shift. Wouldn't it be cleaner to use
> > > > EFI_PAGE_SHIFT to explicitly avoid a dependency on __udivdi3()?
> > > >
> > > > slack = (align >> EFI_PAGE_SHIFT) - 1;
> > > > ...
> > > > int l = (alloc_addr & (align - 1)) >> EFI_PAGE_SHIFT;
> > > >
> > >
> > > Sure. I will update it in the next version. Thanks for the suggestion.
> >
> > Please don't. Dividing by EFI_PAGE_SIZE is perfectly fine, and is more readable.
>
> Actually, I will take this patch as a fix right away - 32-bit ARM has
> the same issue (although it does not actually incorporate this object
> file)
>

Thanks. I will drop it from my next version in that case.

> In the meantime, please check how the stub gets pulled into your
> kernel: the idea of a static library is that only the objects that are
> used are included in the final build, but this turned out to be broken
> for arm64 [0]. IOW, simply applying a similar change might fix your
> build issue as well (assuming RISC-V does not actually call
> efi_allocate_pages_aligned() anywhere)
>
Sorry. I missed this thread earlier. Yes. Applying a similar change
fixes the static library linking for RISC-V. I don't see
efi_allocate_pages_aligned or efi_random_alloc
in vmlinux anymore. I will update my patch. Thanks.

I did not see any new version of the next patch in that series [1].
Thus, I am leaving libstub/Makefile changes
as it is. Please let me know if I missed something.

[1] https://lore.kernel.org/linux-efi/CAMj1kXGg-w6N7jyG0pBJmeRctAhG2wWGoU=ryWj+Qi2UH-_m9Q@mail.gmail.com/

> [0] https://lore.kernel.org/linux-arm-kernel/[email protected]/



--
Regards,
Atish

Subject: [tip: efi/urgent] efi/libstub: Fix gcc error around __umoddi3 for 32 bit builds

The following commit has been merged into the efi/urgent branch of tip:

Commit-ID: 950accbabd4cfa83519fa920f99428bcc131c3c9
Gitweb: https://git.kernel.org/tip/950accbabd4cfa83519fa920f99428bcc131c3c9
Author: Atish Patra <[email protected]>
AuthorDate: Thu, 25 Jun 2020 16:45:06 -07:00
Committer: Ard Biesheuvel <[email protected]>
CommitterDate: Thu, 09 Jul 2020 09:45:09 +03:00

efi/libstub: Fix gcc error around __umoddi3 for 32 bit builds

32bit gcc doesn't support modulo operation on 64 bit data. It results in
a __umoddi3 error while building EFI for 32 bit.

Use bitwise operations instead of modulo operations to fix the issue.

Signed-off-by: Atish Patra <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Ard Biesheuvel <[email protected]>
---
drivers/firmware/efi/libstub/alignedmem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/firmware/efi/libstub/alignedmem.c b/drivers/firmware/efi/libstub/alignedmem.c
index cc89c4d..1de9878 100644
--- a/drivers/firmware/efi/libstub/alignedmem.c
+++ b/drivers/firmware/efi/libstub/alignedmem.c
@@ -44,7 +44,7 @@ efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
*addr = ALIGN((unsigned long)alloc_addr, align);

if (slack > 0) {
- int l = (alloc_addr % align) / EFI_PAGE_SIZE;
+ int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;

if (l) {
efi_bs_call(free_pages, alloc_addr, slack - l + 1);