[PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library

Jan Beulich posted 3 patches 4 days, 7 hours ago
[PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library
Posted by Jan Beulich 4 days, 7 hours ago
The file wasn't correctly named for our purposes anyway. Split it into its
"guest" and "unsafe" parts, thus allowing the latter to not be linked in
at all (for presently having no caller). The building of the "guest" part
can then (later) become conditional upon PV=y.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
 xen/arch/x86/Makefile                         |  4 ----
 xen/arch/x86/lib/Makefile                     |  2 ++
 xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
 xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
 4 files changed, 25 insertions(+), 15 deletions(-)
 rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%)
 create mode 100644 xen/arch/x86/lib/copy-unsafe.c

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 61e2293a467e..dfb258d7ac1c 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -72,7 +72,6 @@ obj-y += time.o
 obj-y += traps-setup.o
 obj-y += traps.o
 obj-$(CONFIG_INTEL) += tsx.o
-obj-y += usercopy.o
 obj-y += x86_emulate.o
 obj-$(CONFIG_TBOOT) += tboot.o
 obj-y += hpet.o
@@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
 
 $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
 
-# Allows usercopy.c to include itself
-$(obj)/usercopy.o: CFLAGS-y += -iquote .
-
 ifneq ($(CONFIG_HVM),y)
 $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
 endif
diff --git a/xen/arch/x86/lib/Makefile b/xen/arch/x86/lib/Makefile
index ddf7e19bdc1d..8fe2dfd88553 100644
--- a/xen/arch/x86/lib/Makefile
+++ b/xen/arch/x86/lib/Makefile
@@ -1 +1,3 @@
+lib-y += copy-guest.o
+lib-y += copy-unsafe.o
 lib-y += generic-hweightl.o
diff --git a/xen/arch/x86/usercopy.c b/xen/arch/x86/lib/copy-guest.c
similarity index 94%
rename from xen/arch/x86/usercopy.c
rename to xen/arch/x86/lib/copy-guest.c
index a24b52cc66c1..25eeb35427e2 100644
--- a/xen/arch/x86/usercopy.c
+++ b/xen/arch/x86/lib/copy-guest.c
@@ -1,4 +1,4 @@
-/* 
+/*
  * User address space access functions.
  *
  * Copyright 1997 Andi Kleen <ak@muc.de>
@@ -6,8 +6,6 @@
  * Copyright 2002 Andi Kleen <ak@suse.de>
  */
 
-#include <xen/lib.h>
-#include <xen/sched.h>
 #include <asm/uaccess.h>
 
 #ifndef GUARD
@@ -139,14 +137,6 @@ unsigned int copy_from_guest_pv(void *to, const void __user *from,
     return n;
 }
 
-# undef GUARD
-# define GUARD UA_DROP
-# define copy_to_guest_ll copy_to_unsafe_ll
-# define copy_from_guest_ll copy_from_unsafe_ll
-# undef __user
-# define __user
-# include __FILE__
-
 #endif /* GUARD(1) */
 
 /*
diff --git a/xen/arch/x86/lib/copy-unsafe.c b/xen/arch/x86/lib/copy-unsafe.c
new file mode 100644
index 000000000000..a51500370fb1
--- /dev/null
+++ b/xen/arch/x86/lib/copy-unsafe.c
@@ -0,0 +1,22 @@
+/*
+ * "Unsafe" access functions.
+ */
+
+#include <asm/uaccess.h>
+
+#define GUARD UA_DROP
+#define copy_to_guest_ll copy_to_unsafe_ll
+#define copy_from_guest_ll copy_from_unsafe_ll
+#undef __user
+#define __user
+#include "copy-guest.c"
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Re: [PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library
Posted by Grygorii Strashko 3 days, 21 hours ago

On 08.12.25 14:05, Jan Beulich wrote:
> The file wasn't correctly named for our purposes anyway. Split it into its
> "guest" and "unsafe" parts, thus allowing the latter to not be linked in
> at all (for presently having no caller). The building of the "guest" part
> can then (later) become conditional upon PV=y.
> 
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
>   xen/arch/x86/Makefile                         |  4 ----
>   xen/arch/x86/lib/Makefile                     |  2 ++
>   xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
>   xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
>   4 files changed, 25 insertions(+), 15 deletions(-)
>   rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%)
>   create mode 100644 xen/arch/x86/lib/copy-unsafe.c
> 
> diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
> index 61e2293a467e..dfb258d7ac1c 100644
> --- a/xen/arch/x86/Makefile
> +++ b/xen/arch/x86/Makefile
> @@ -72,7 +72,6 @@ obj-y += time.o
>   obj-y += traps-setup.o
>   obj-y += traps.o
>   obj-$(CONFIG_INTEL) += tsx.o
> -obj-y += usercopy.o
>   obj-y += x86_emulate.o
>   obj-$(CONFIG_TBOOT) += tboot.o
>   obj-y += hpet.o
> @@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
>   
>   $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
>   
> -# Allows usercopy.c to include itself
> -$(obj)/usercopy.o: CFLAGS-y += -iquote .
> -
>   ifneq ($(CONFIG_HVM),y)
>   $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
>   endif
> diff --git a/xen/arch/x86/lib/Makefile b/xen/arch/x86/lib/Makefile
> index ddf7e19bdc1d..8fe2dfd88553 100644
> --- a/xen/arch/x86/lib/Makefile
> +++ b/xen/arch/x86/lib/Makefile
> @@ -1 +1,3 @@
> +lib-y += copy-guest.o
> +lib-y += copy-unsafe.o
>   lib-y += generic-hweightl.o
> diff --git a/xen/arch/x86/usercopy.c b/xen/arch/x86/lib/copy-guest.c
> similarity index 94%
> rename from xen/arch/x86/usercopy.c
> rename to xen/arch/x86/lib/copy-guest.c
> index a24b52cc66c1..25eeb35427e2 100644
> --- a/xen/arch/x86/usercopy.c
> +++ b/xen/arch/x86/lib/copy-guest.c
> @@ -1,4 +1,4 @@
> -/*
> +/*
>    * User address space access functions.
>    *
>    * Copyright 1997 Andi Kleen <ak@muc.de>
> @@ -6,8 +6,6 @@
>    * Copyright 2002 Andi Kleen <ak@suse.de>
>    */
>   
> -#include <xen/lib.h>
> -#include <xen/sched.h>

This will not build :(

   CC      arch/x86/lib/copy-guest.o
In file included from ././include/xen/config.h:27,
                  from <command-line>:
arch/x86/lib/copy-guest.c: In function ‘copy_to_guest_pv’:
./arch/x86/include/asm/config.h:270:32: error: invalid use of undefined type ‘struct vcpu’
   270 |     (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT))


>   #include <asm/uaccess.h>
>   
>   #ifndef GUARD
> @@ -139,14 +137,6 @@ unsigned int copy_from_guest_pv(void *to, const void __user *from,
>       return n;
>   }
>   
> -# undef GUARD
> -# define GUARD UA_DROP
> -# define copy_to_guest_ll copy_to_unsafe_ll
> -# define copy_from_guest_ll copy_from_unsafe_ll
> -# undef __user
> -# define __user
> -# include __FILE__
> -
>   #endif /* GUARD(1) */
>   
>   /*
> diff --git a/xen/arch/x86/lib/copy-unsafe.c b/xen/arch/x86/lib/copy-unsafe.c
> new file mode 100644
> index 000000000000..a51500370fb1
> --- /dev/null
> +++ b/xen/arch/x86/lib/copy-unsafe.c
> @@ -0,0 +1,22 @@
> +/*
> + * "Unsafe" access functions.
> + */
> +
> +#include <asm/uaccess.h>
> +
> +#define GUARD UA_DROP
> +#define copy_to_guest_ll copy_to_unsafe_ll
> +#define copy_from_guest_ll copy_from_unsafe_ll
> +#undef __user
> +#define __user
> +#include "copy-guest.c"
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> 

-- 
Best regards,
-grygorii


Re: [PATCH v2 2/3] x86: move / split usercopy.c to / into arch-specific library
Posted by Jan Beulich 3 days, 10 hours ago
On 08.12.2025 22:48, Grygorii Strashko wrote:
> 
> 
> On 08.12.25 14:05, Jan Beulich wrote:
>> The file wasn't correctly named for our purposes anyway. Split it into its
>> "guest" and "unsafe" parts, thus allowing the latter to not be linked in
>> at all (for presently having no caller). The building of the "guest" part
>> can then (later) become conditional upon PV=y.
>>
>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>> ---
>>   xen/arch/x86/Makefile                         |  4 ----
>>   xen/arch/x86/lib/Makefile                     |  2 ++
>>   xen/arch/x86/{usercopy.c => lib/copy-guest.c} | 12 +---------
>>   xen/arch/x86/lib/copy-unsafe.c                | 22 +++++++++++++++++++
>>   4 files changed, 25 insertions(+), 15 deletions(-)
>>   rename xen/arch/x86/{usercopy.c => lib/copy-guest.c} (94%)
>>   create mode 100644 xen/arch/x86/lib/copy-unsafe.c
>>
>> diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
>> index 61e2293a467e..dfb258d7ac1c 100644
>> --- a/xen/arch/x86/Makefile
>> +++ b/xen/arch/x86/Makefile
>> @@ -72,7 +72,6 @@ obj-y += time.o
>>   obj-y += traps-setup.o
>>   obj-y += traps.o
>>   obj-$(CONFIG_INTEL) += tsx.o
>> -obj-y += usercopy.o
>>   obj-y += x86_emulate.o
>>   obj-$(CONFIG_TBOOT) += tboot.o
>>   obj-y += hpet.o
>> @@ -93,9 +92,6 @@ hostprogs-y += efi/mkreloc
>>   
>>   $(obj)/efi/mkreloc: HOSTCFLAGS += -I$(srctree)/include
>>   
>> -# Allows usercopy.c to include itself
>> -$(obj)/usercopy.o: CFLAGS-y += -iquote .
>> -
>>   ifneq ($(CONFIG_HVM),y)
>>   $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
>>   endif
>> diff --git a/xen/arch/x86/lib/Makefile b/xen/arch/x86/lib/Makefile
>> index ddf7e19bdc1d..8fe2dfd88553 100644
>> --- a/xen/arch/x86/lib/Makefile
>> +++ b/xen/arch/x86/lib/Makefile
>> @@ -1 +1,3 @@
>> +lib-y += copy-guest.o
>> +lib-y += copy-unsafe.o
>>   lib-y += generic-hweightl.o
>> diff --git a/xen/arch/x86/usercopy.c b/xen/arch/x86/lib/copy-guest.c
>> similarity index 94%
>> rename from xen/arch/x86/usercopy.c
>> rename to xen/arch/x86/lib/copy-guest.c
>> index a24b52cc66c1..25eeb35427e2 100644
>> --- a/xen/arch/x86/usercopy.c
>> +++ b/xen/arch/x86/lib/copy-guest.c
>> @@ -1,4 +1,4 @@
>> -/*
>> +/*
>>    * User address space access functions.
>>    *
>>    * Copyright 1997 Andi Kleen <ak@muc.de>
>> @@ -6,8 +6,6 @@
>>    * Copyright 2002 Andi Kleen <ak@suse.de>
>>    */
>>   
>> -#include <xen/lib.h>
>> -#include <xen/sched.h>
> 
> This will not build :(
> 
>    CC      arch/x86/lib/copy-guest.o
> In file included from ././include/xen/config.h:27,
>                   from <command-line>:
> arch/x86/lib/copy-guest.c: In function ‘copy_to_guest_pv’:
> ./arch/x86/include/asm/config.h:270:32: error: invalid use of undefined type ‘struct vcpu’
>    270 |     (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT))
> 

Hmm, yes. It does build here, but of course I have many other patches in
place. The issue looks to be that for me access_ok() isn't a macro anymore,
but a function.

Thanks for pointing out,
Jan