diff mbox series

x86/mm: Refine address alignment checks in modify_xen_mappings_lite()

Message ID 20240319195856.4172284-1-andrew.cooper3@citrix.com (mailing list archive)
State New
Headers show
Series x86/mm: Refine address alignment checks in modify_xen_mappings_lite() | expand

Commit Message

Andrew Cooper March 19, 2024, 7:58 p.m. UTC
Require 's' to be superpage aligned if given over a superpage mapping.  This
is expected to be the case in practice, and prevents the v getting misaligned
over superpages during the processing loop.

Rmove the requirement for 'e' to be page aligned.  All this is doing is
forcing work for the caller just to satisfy an assertion.

Reported-by: Jan Beulich <JBeulich@suse.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
---
 xen/arch/x86/mm.c           | 10 ++++++----
 xen/common/virtual_region.c |  8 ++++----
 2 files changed, 10 insertions(+), 8 deletions(-)


base-commit: 188fa82305e72b725473db9146e20cc9abf7bff3

Comments

Andrew Cooper March 19, 2024, 8:04 p.m. UTC | #1
On 19/03/2024 7:58 pm, Andrew Cooper wrote:
> Require 's' to be superpage aligned if given over a superpage mapping.  This
> is expected to be the case in practice, and prevents the v getting misaligned
> over superpages during the processing loop.
>
> Rmove the requirement for 'e' to be page aligned.  All this is doing is

"Remove".  Fixed locally.

> forcing work for the caller just to satisfy an assertion.
>
> Reported-by: Jan Beulich <JBeulich@suse.com>
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> ---
> CC: Jan Beulich <JBeulich@suse.com>
> CC: Roger Pau Monné <roger.pau@citrix.com>

Code-gen wise, this drops 2x

48 8d b0 ff 0f 00 00    lea    0xfff(%rax),%rsi
48 81 e6 00 f0 ff ff    and    $0xfffffffffffff000,%rsi

from {relax,tighten}_virtual_region_perms().

~Andrew
Roger Pau Monne March 20, 2024, 8:11 a.m. UTC | #2
On Tue, Mar 19, 2024 at 07:58:56PM +0000, Andrew Cooper wrote:
> Require 's' to be superpage aligned if given over a superpage mapping.  This
> is expected to be the case in practice, and prevents the v getting misaligned
> over superpages during the processing loop.
> 
> Rmove the requirement for 'e' to be page aligned.  All this is doing is
> forcing work for the caller just to satisfy an assertion.
> 
> Reported-by: Jan Beulich <JBeulich@suse.com>
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>

One suggestion below.

> ---
> CC: Jan Beulich <JBeulich@suse.com>
> CC: Roger Pau Monné <roger.pau@citrix.com>
> ---
>  xen/arch/x86/mm.c           | 10 ++++++----
>  xen/common/virtual_region.c |  8 ++++----
>  2 files changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
> index 62f5b811bbe8..018d21f8bd92 100644
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -5900,9 +5900,9 @@ int destroy_xen_mappings(unsigned long s, unsigned long e)
>   *
>   * Must be limited to XEN_VIRT_{START,END}, i.e. over l2_xenmap[].
>   * Must be called with present flags, and over present mappings.
> - * It is the callers responsibility to not pass s or e in the middle of
> - * superpages if changing the permission on the whole superpage is going to be
> - * a problem.
> + * It is the callers repsonsibility to pass 's' on a PAGE/SUPERPAGE boundary,
> + * and for there to not be anything interesting in the PAGE/SUPERPAGE
> + * following 'e'.

Instead of mentioning 'anything interesting' I would just state that
'e' will be extended to the next page/superpage boundary:

"It is the callers repsonsibility to pass 's' on a PAGE/SUPERPAGE boundary,
note that 'e' will be rounded up to the next PAGE/SUPERPAGE boundary."

Thanks, Roger.
diff mbox series

Patch

diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 62f5b811bbe8..018d21f8bd92 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -5900,9 +5900,9 @@  int destroy_xen_mappings(unsigned long s, unsigned long e)
  *
  * Must be limited to XEN_VIRT_{START,END}, i.e. over l2_xenmap[].
  * Must be called with present flags, and over present mappings.
- * It is the callers responsibility to not pass s or e in the middle of
- * superpages if changing the permission on the whole superpage is going to be
- * a problem.
+ * It is the callers repsonsibility to pass 's' on a PAGE/SUPERPAGE boundary,
+ * and for there to not be anything interesting in the PAGE/SUPERPAGE
+ * following 'e'.
  */
 void init_or_livepatch modify_xen_mappings_lite(
     unsigned long s, unsigned long e, unsigned int nf)
@@ -5917,7 +5917,7 @@  void init_or_livepatch modify_xen_mappings_lite(
 
     ASSERT(flags & _PAGE_PRESENT);
     ASSERT(IS_ALIGNED(s, PAGE_SIZE) && s >= XEN_VIRT_START);
-    ASSERT(IS_ALIGNED(e, PAGE_SIZE) && e <= XEN_VIRT_END);
+    ASSERT(e <= XEN_VIRT_END);
 
     while ( v < e )
     {
@@ -5929,6 +5929,8 @@  void init_or_livepatch modify_xen_mappings_lite(
 
         if ( l2e_get_flags(l2e) & _PAGE_PSE )
         {
+            ASSERT(IS_ALIGNED(v, 1 << L2_PAGETABLE_SHIFT));
+
             l2e_write_atomic(pl2e, l2e_from_intpte((l2e.l2 & ~fm) | flags));
 
             v += 1UL << L2_PAGETABLE_SHIFT;
diff --git a/xen/common/virtual_region.c b/xen/common/virtual_region.c
index b4325bcda71e..142f21e18153 100644
--- a/xen/common/virtual_region.c
+++ b/xen/common/virtual_region.c
@@ -93,11 +93,11 @@  void relax_virtual_region_perms(void)
     list_for_each_entry_rcu( region, &virtual_region_list, list )
     {
         modify_xen_mappings_lite((unsigned long)region->text_start,
-                                 PAGE_ALIGN((unsigned long)region->text_end),
+                                 (unsigned long)region->text_end,
                                  PAGE_HYPERVISOR_RWX);
         if ( region->rodata_start )
             modify_xen_mappings_lite((unsigned long)region->rodata_start,
-                                     PAGE_ALIGN((unsigned long)region->rodata_end),
+                                     (unsigned long)region->rodata_end,
                                      PAGE_HYPERVISOR_RW);
     }
     rcu_read_unlock(&rcu_virtual_region_lock);
@@ -111,11 +111,11 @@  void tighten_virtual_region_perms(void)
     list_for_each_entry_rcu( region, &virtual_region_list, list )
     {
         modify_xen_mappings_lite((unsigned long)region->text_start,
-                                 PAGE_ALIGN((unsigned long)region->text_end),
+                                 (unsigned long)region->text_end,
                                  PAGE_HYPERVISOR_RX);
         if ( region->rodata_start )
             modify_xen_mappings_lite((unsigned long)region->rodata_start,
-                                     PAGE_ALIGN((unsigned long)region->rodata_end),
+                                     (unsigned long)region->rodata_end,
                                      PAGE_HYPERVISOR_RO);
     }
     rcu_read_unlock(&rcu_virtual_region_lock);