diff mbox series

[v2,02/17] vdso: Clean header inclusion in getrandom

Message ID 2a081f1fff5e40f496153f8e0162fc7ec5adab2e.1724309198.git.christophe.leroy@csgroup.eu (mailing list archive)
State Handled Elsewhere
Headers show
Series Wire up getrandom() vDSO implementation on powerpc | expand

Commit Message

Christophe Leroy Aug. 22, 2024, 7:13 a.m. UTC
Building a VDSO32 on a 64 bits kernel is problematic when some
system headers are included. See commit 8c59ab839f52 ("lib/vdso:
Enable common headers") for more details.

Minimise the amount of headers by moving needed items into
dedicated common headers.

For PAGE_SIZE and PAGE_MASK, redefine them from CONFIG_PAGE_SHIFT
in the same way as commit cffaefd15a8f ("vdso: Use CONFIG_PAGE_SHIFT
in vdso/datapage.h")

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
 arch/x86/include/asm/pvclock.h |  1 +
 include/vdso/helpers.h         |  1 +
 lib/vdso/getrandom.c           | 15 ++++++++-------
 3 files changed, 10 insertions(+), 7 deletions(-)

Comments

Jason A. Donenfeld Aug. 26, 2024, 8:07 a.m. UTC | #1
On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
> diff --git a/arch/x86/include/asm/pvclock.h b/arch/x86/include/asm/pvclock.h
> index 0c92db84469d..6e4f8fae3ce9 100644
> --- a/arch/x86/include/asm/pvclock.h
> +++ b/arch/x86/include/asm/pvclock.h
> @@ -5,6 +5,7 @@
>  #include <asm/clocksource.h>
>  #include <asm/pvclock-abi.h>
>  
> +struct timespec64;
>  /* some helper functions for xen and kvm pv clock sources */
>  u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
>  u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);

This change isn't mentioned in the commit message and could probably
benefit from doing so.

> diff --git a/lib/vdso/getrandom.c b/lib/vdso/getrandom.c
> index b230f0b10832..cab153c5f9be 100644
> --- a/lib/vdso/getrandom.c
> +++ b/lib/vdso/getrandom.c
> @@ -3,15 +3,13 @@
>   * Copyright (C) 2022-2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
>   */
>  
> -#include <linux/cache.h>
> -#include <linux/kernel.h>
> -#include <linux/time64.h>
> +#include <linux/minmax.h>
>  #include <vdso/datapage.h>
>  #include <vdso/getrandom.h>
> +#include <vdso/unaligned.h>

Ah, that's where you do it. Ignore my comment on the previous commit,
then.

>  #include <asm/vdso/getrandom.h>
> -#include <asm/vdso/vsyscall.h>
> -#include <asm/unaligned.h>
>  #include <uapi/linux/mman.h>
> +#include <uapi/linux/random.h>
>  
>  #define MEMCPY_AND_ZERO_SRC(type, dst, src, len) do {				\
>  	while (len >= sizeof(type)) {						\
> @@ -23,6 +21,9 @@
>  	}									\
>  } while (0)
>  
> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
instead of _PAGE_SIZE? But if that's the case, why not put the vdso
definition of PAGE_SIZE into some vdso header included by this file?
Christophe Leroy Aug. 26, 2024, 8:37 a.m. UTC | #2
Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :
> On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
>>   
>> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
>> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))
> 
> If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
> instead of _PAGE_SIZE? But if that's the case, why not put the vdso
> definition of PAGE_SIZE into some vdso header included by this file?

It was working ok on powerpc but on x86 I got:

   CC      arch/x86/entry/vdso/vgetrandom.o
In file included from arch/x86/entry/vdso/vgetrandom.c:7:
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:24: error: 
"PAGE_SIZE" redefined [-Werror]
    24 | #define PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
       |
In file included from ./arch/x86/include/asm/page.h:9,
                  from ./arch/x86/include/asm/thread_info.h:12,
                  from ./include/linux/thread_info.h:60,
                  from ./include/linux/smp.h:118,
                  from ./include/linux/alloc_tag.h:14,
                  from ./include/linux/percpu.h:5,
                  from ./arch/x86/include/asm/msr.h:15,
                  from ./arch/x86/include/asm/vdso/gettimeofday.h:19,
                  from ./include/vdso/datapage.h:164,
                  from 
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:7,
                  from arch/x86/entry/vdso/vgetrandom.c:7:
./arch/x86/include/asm/page_types.h:11: note: this is the location of 
the previous definition
    11 | #define PAGE_SIZE  (_AC(1,UL) << PAGE_SHIFT)
       |
In file included from arch/x86/entry/vdso/vgetrandom.c:7:
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:25: error: 
"PAGE_MASK" redefined [-Werror]
    25 | #define PAGE_MASK (~(PAGE_SIZE - 1))
       |
In file included from ./arch/x86/include/asm/page.h:9,
                  from ./arch/x86/include/asm/thread_info.h:12,
                  from ./include/linux/thread_info.h:60,
                  from ./include/linux/smp.h:118,
                  from ./include/linux/alloc_tag.h:14,
                  from ./include/linux/percpu.h:5,
                  from ./arch/x86/include/asm/msr.h:15,
                  from ./arch/x86/include/asm/vdso/gettimeofday.h:19,
                  from ./include/vdso/datapage.h:164,
                  from 
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:7,
                  from arch/x86/entry/vdso/vgetrandom.c:7:
./arch/x86/include/asm/page_types.h:12: note: this is the location of 
the previous definition
    12 | #define PAGE_MASK  (~(PAGE_SIZE-1))
       |
cc1: all warnings being treated as errors


Christophe
Jason A. Donenfeld Aug. 26, 2024, 8:58 a.m. UTC | #3
On Mon, Aug 26, 2024 at 10:37:49AM +0200, Christophe Leroy wrote:
> 
> 
> Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :
> > On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
> >>   
> >> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
> >> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))
> > 
> > If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
> > instead of _PAGE_SIZE? But if that's the case, why not put the vdso
> > definition of PAGE_SIZE into some vdso header included by this file?
> 
> It was working ok on powerpc but on x86 I got:

Seems like there might be some more fiddling to do, then? Or did you
conclude it's impossible?
Christophe Leroy Aug. 26, 2024, 10:45 a.m. UTC | #4
Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :
> On Mon, Aug 26, 2024 at 10:37:49AM +0200, Christophe Leroy wrote:
>>
>>
>> Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :
>>> On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
>>>>    
>>>> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
>>>> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))
>>>
>>> If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
>>> instead of _PAGE_SIZE? But if that's the case, why not put the vdso
>>> definition of PAGE_SIZE into some vdso header included by this file?
>>
>> It was working ok on powerpc but on x86 I got:
> 
> Seems like there might be some more fiddling to do, then? Or did you
> conclude it's impossible?

Maybe someone who knows x86 in details could helps but after a first 
look I gave up because it looks very x86 specific, indeed that's 
x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and 
the same type of issue might arise for any new architecture coming in.

For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use 
CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But 
I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << 
(CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with 
a leading underscore to avoid any conflict with existing macros.

Christophe
Jason A. Donenfeld Aug. 26, 2024, 1:17 p.m. UTC | #5
On Mon, Aug 26, 2024 at 12:45:40PM +0200, Christophe Leroy wrote:
> 
> 
> Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :
> > On Mon, Aug 26, 2024 at 10:37:49AM +0200, Christophe Leroy wrote:
> >>
> >>
> >> Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :
> >>> On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
> >>>>    
> >>>> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
> >>>> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))
> >>>
> >>> If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
> >>> instead of _PAGE_SIZE? But if that's the case, why not put the vdso
> >>> definition of PAGE_SIZE into some vdso header included by this file?
> >>
> >> It was working ok on powerpc but on x86 I got:
> > 
> > Seems like there might be some more fiddling to do, then? Or did you
> > conclude it's impossible?
> 
> Maybe someone who knows x86 in details could helps but after a first 
> look I gave up because it looks very x86 specific, indeed that's 
> x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and 
> the same type of issue might arise for any new architecture coming in.
> 
> For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use 
> CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But 
> I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << 
> (CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with 
> a leading underscore to avoid any conflict with existing macros.

If that's the case and you can't sort it out or it's impossible, then it
seems like adding #ifndef PAGE_SIZE, #define... inside of some vdso header
(not inside getrandom.c) would make sense.

Jason
Thomas Gleixner Aug. 26, 2024, 1:24 p.m. UTC | #6
On Mon, Aug 26 2024 at 12:45, Christophe Leroy wrote:
> Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :
>> On Mon, Aug 26, 2024 at 10:37:49AM +0200, Christophe Leroy wrote:
>>>
>>>
>>> Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :
>>>> On Thu, Aug 22, 2024 at 09:13:10AM +0200, Christophe Leroy wrote:
>>>>>    
>>>>> +#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
>>>>> +#define _PAGE_MASK (~(_PAGE_SIZE - 1))
>>>>
>>>> If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE
>>>> instead of _PAGE_SIZE? But if that's the case, why not put the vdso
>>>> definition of PAGE_SIZE into some vdso header included by this file?
>>>
>>> It was working ok on powerpc but on x86 I got:
>> 
>> Seems like there might be some more fiddling to do, then? Or did you
>> conclude it's impossible?
>
> Maybe someone who knows x86 in details could helps but after a first 
> look I gave up because it looks very x86 specific, indeed that's 
> x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and 
> the same type of issue might arise for any new architecture coming in.

Of course :)

> For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use 
> CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But 
> I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << 
> (CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with 
> a leading underscore to avoid any conflict with existing macros.

#ifndef PAGE_SIZE
# define
#endif

Perhaps?

Thanks,

        tglx
diff mbox series

Patch

diff --git a/arch/x86/include/asm/pvclock.h b/arch/x86/include/asm/pvclock.h
index 0c92db84469d..6e4f8fae3ce9 100644
--- a/arch/x86/include/asm/pvclock.h
+++ b/arch/x86/include/asm/pvclock.h
@@ -5,6 +5,7 @@ 
 #include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
 
+struct timespec64;
 /* some helper functions for xen and kvm pv clock sources */
 u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
 u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);
diff --git a/include/vdso/helpers.h b/include/vdso/helpers.h
index 73501149439d..3ddb03bb05cb 100644
--- a/include/vdso/helpers.h
+++ b/include/vdso/helpers.h
@@ -4,6 +4,7 @@ 
 
 #ifndef __ASSEMBLY__
 
+#include <asm/barrier.h>
 #include <vdso/datapage.h>
 
 static __always_inline u32 vdso_read_begin(const struct vdso_data *vd)
diff --git a/lib/vdso/getrandom.c b/lib/vdso/getrandom.c
index b230f0b10832..cab153c5f9be 100644
--- a/lib/vdso/getrandom.c
+++ b/lib/vdso/getrandom.c
@@ -3,15 +3,13 @@ 
  * Copyright (C) 2022-2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  */
 
-#include <linux/cache.h>
-#include <linux/kernel.h>
-#include <linux/time64.h>
+#include <linux/minmax.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
+#include <vdso/unaligned.h>
 #include <asm/vdso/getrandom.h>
-#include <asm/vdso/vsyscall.h>
-#include <asm/unaligned.h>
 #include <uapi/linux/mman.h>
+#include <uapi/linux/random.h>
 
 #define MEMCPY_AND_ZERO_SRC(type, dst, src, len) do {				\
 	while (len >= sizeof(type)) {						\
@@ -23,6 +21,9 @@ 
 	}									\
 } while (0)
 
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))
+
 static void memcpy_and_zero_src(void *dst, void *src, size_t len)
 {
 	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
@@ -64,7 +65,7 @@  static __always_inline ssize_t
 __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_t len,
 		       unsigned int flags, void *opaque_state, size_t opaque_len)
 {
-	ssize_t ret = min_t(size_t, INT_MAX & PAGE_MASK /* = MAX_RW_COUNT */, len);
+	ssize_t ret = min_t(size_t, INT_MAX & _PAGE_MASK /* = MAX_RW_COUNT */, len);
 	struct vgetrandom_state *state = opaque_state;
 	size_t batch_len, nblocks, orig_len = len;
 	bool in_use, have_retried = false;
@@ -82,7 +83,7 @@  __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	}
 
 	/* The state must not straddle a page, since pages can be zeroed at any time. */
-	if (unlikely(((unsigned long)opaque_state & ~PAGE_MASK) + sizeof(*state) > PAGE_SIZE))
+	if (unlikely(((unsigned long)opaque_state & ~_PAGE_MASK) + sizeof(*state) > _PAGE_SIZE))
 		return -EFAULT;
 
 	/* If the caller passes the wrong size, which might happen due to CRIU, fallback. */