[Xen-devel,07/13] xen/arm: Simplify alternative patching

Message ID 20180522174254.27551-8-julien.grall@arm.com
State Superseded
Headers show
Series
  • xen/arm: SSBD (aka Spectre-v4) mitigation (XSA-263)
Related show

Commit Message

Julien Grall May 22, 2018, 5:42 p.m.
This is part of XSA-263.

Signed-off-by: Julien Grall <julien.grall@arm.com>

---
    I am aware of the missing commit message here. I wanted to send the
    series on the ML to get some feedback first.
---
 xen/arch/arm/alternative.c | 35 +++++++++++++----------------------
 1 file changed, 13 insertions(+), 22 deletions(-)

Comments

Stefano Stabellini May 25, 2018, 8:52 p.m. | #1
You might want to CC Konrad next time on this patch

On Tue, 22 May 2018, Julien Grall wrote:
> This is part of XSA-263.
> 
> Signed-off-by: Julien Grall <julien.grall@arm.com>
> 
> ---
>     I am aware of the missing commit message here. I wanted to send the
>     series on the ML to get some feedback first.
> ---
>  xen/arch/arm/alternative.c | 35 +++++++++++++----------------------
>  1 file changed, 13 insertions(+), 22 deletions(-)
> 
> diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c
> index 9ffdc475d6..bd62183def 100644
> --- a/xen/arch/arm/alternative.c
> +++ b/xen/arch/arm/alternative.c
> @@ -97,12 +97,16 @@ static u32 get_alt_insn(const struct alt_instr *alt,
>  /*
>   * The region patched should be read-write to allow __apply_alternatives
>   * to replacing the instructions when necessary.
> + *
> + * @update_offset: Offset between the region patched and the writable
> + * region for the update. 0 if the patched region is writable.
>   */
> -static int __apply_alternatives(const struct alt_region *region)
> +static int __apply_alternatives(const struct alt_region *region,
> +                                paddr_t update_offset)
>  {
>      const struct alt_instr *alt;
> -    const u32 *replptr;
> -    u32 *origptr;
> +    const u32 *replptr, *origptr;
> +    u32 *updptr;
>  
>      printk(XENLOG_INFO "alternatives: Patching with alt table %p -> %p\n",
>             region->begin, region->end);
> @@ -118,6 +122,7 @@ static int __apply_alternatives(const struct alt_region *region)
>          BUG_ON(alt->alt_len != alt->orig_len);
>  
>          origptr = ALT_ORIG_PTR(alt);
> +        updptr = (void *)origptr + update_offset;
>          replptr = ALT_REPL_PTR(alt);
>  
>          nr_inst = alt->alt_len / sizeof(insn);
> @@ -125,7 +130,7 @@ static int __apply_alternatives(const struct alt_region *region)
>          for ( i = 0; i < nr_inst; i++ )
>          {
>              insn = get_alt_insn(alt, origptr + i, replptr + i);
> -            *(origptr + i) = cpu_to_le32(insn);
> +            *(updptr + i) = cpu_to_le32(insn);
>          }
>  
>          /* Ensure the new instructions reached the memory and nuke */
> @@ -162,9 +167,6 @@ static int __apply_alternatives_multi_stop(void *unused)
>          paddr_t xen_size = _end - _start;
>          unsigned int xen_order = get_order_from_bytes(xen_size);
>          void *xenmap;
> -        struct virtual_region patch_region = {
> -            .list = LIST_HEAD_INIT(patch_region.list),
> -        };
>  
>          BUG_ON(patched);
>  
> @@ -178,30 +180,19 @@ static int __apply_alternatives_multi_stop(void *unused)
>          BUG_ON(!xenmap);
>  
>          /*
> -         * If we generate a new branch instruction, the target will be
> -         * calculated in this re-mapped Xen region. So we have to register
> -         * this re-mapped Xen region as a virtual region temporarily.
> -         */
> -        patch_region.start = xenmap;
> -        patch_region.end = xenmap + xen_size;
> -        register_virtual_region(&patch_region);
> -
> -        /*
>           * Find the virtual address of the alternative region in the new
>           * mapping.
>           * alt_instr contains relative offset, so the function
>           * __apply_alternatives will patch in the re-mapped version of
>           * Xen.
>           */
> -        region.begin = (void *)__alt_instructions - (void *)_start + xenmap;
> -        region.end = (void *)__alt_instructions_end - (void *)_start + xenmap;
> +        region.begin = __alt_instructions;
> +        region.end = __alt_instructions_end;
>  
> -        ret = __apply_alternatives(&region);
> +        ret = __apply_alternatives(&region, xenmap - (void *)_start);
>          /* The patching is not expected to fail during boot. */
>          BUG_ON(ret != 0);
>  
> -        unregister_virtual_region(&patch_region);
> -
>          vunmap(xenmap);
>  
>          /* Barriers provided by the cache flushing */
> @@ -235,7 +226,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
>          .end = end,
>      };
>  
> -    return __apply_alternatives(&region);
> +    return __apply_alternatives(&region, 0);
>  }
>  
>  /*
> -- 
> 2.11.0
>
Julien Grall May 25, 2018, 9:34 p.m. | #2
On Fri, 25 May 2018, 22:54 Stefano Stabellini, <sstabellini@kernel.org>
wrote:

> You might want to CC Konrad next time on this patch

>


May I ask why? This code falls under Arm maintainership.

Cheers,


> On Tue, 22 May 2018, Julien Grall wrote:

> > This is part of XSA-263.

> >

> > Signed-off-by: Julien Grall <julien.grall@arm.com>

> >

> > ---

> >     I am aware of the missing commit message here. I wanted to send the

> >     series on the ML to get some feedback first.

> > ---

> >  xen/arch/arm/alternative.c | 35 +++++++++++++----------------------

> >  1 file changed, 13 insertions(+), 22 deletions(-)

> >

> > diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c

> > index 9ffdc475d6..bd62183def 100644

> > --- a/xen/arch/arm/alternative.c

> > +++ b/xen/arch/arm/alternative.c

> > @@ -97,12 +97,16 @@ static u32 get_alt_insn(const struct alt_instr *alt,

> >  /*

> >   * The region patched should be read-write to allow __apply_alternatives

> >   * to replacing the instructions when necessary.

> > + *

> > + * @update_offset: Offset between the region patched and the writable

> > + * region for the update. 0 if the patched region is writable.

> >   */

> > -static int __apply_alternatives(const struct alt_region *region)

> > +static int __apply_alternatives(const struct alt_region *region,

> > +                                paddr_t update_offset)

> >  {

> >      const struct alt_instr *alt;

> > -    const u32 *replptr;

> > -    u32 *origptr;

> > +    const u32 *replptr, *origptr;

> > +    u32 *updptr;

> >

> >      printk(XENLOG_INFO "alternatives: Patching with alt table %p ->

> %p\n",

> >             region->begin, region->end);

> > @@ -118,6 +122,7 @@ static int __apply_alternatives(const struct

> alt_region *region)

> >          BUG_ON(alt->alt_len != alt->orig_len);

> >

> >          origptr = ALT_ORIG_PTR(alt);

> > +        updptr = (void *)origptr + update_offset;

> >          replptr = ALT_REPL_PTR(alt);

> >

> >          nr_inst = alt->alt_len / sizeof(insn);

> > @@ -125,7 +130,7 @@ static int __apply_alternatives(const struct

> alt_region *region)

> >          for ( i = 0; i < nr_inst; i++ )

> >          {

> >              insn = get_alt_insn(alt, origptr + i, replptr + i);

> > -            *(origptr + i) = cpu_to_le32(insn);

> > +            *(updptr + i) = cpu_to_le32(insn);

> >          }

> >

> >          /* Ensure the new instructions reached the memory and nuke */

> > @@ -162,9 +167,6 @@ static int __apply_alternatives_multi_stop(void

> *unused)

> >          paddr_t xen_size = _end - _start;

> >          unsigned int xen_order = get_order_from_bytes(xen_size);

> >          void *xenmap;

> > -        struct virtual_region patch_region = {

> > -            .list = LIST_HEAD_INIT(patch_region.list),

> > -        };

> >

> >          BUG_ON(patched);

> >

> > @@ -178,30 +180,19 @@ static int __apply_alternatives_multi_stop(void

> *unused)

> >          BUG_ON(!xenmap);

> >

> >          /*

> > -         * If we generate a new branch instruction, the target will be

> > -         * calculated in this re-mapped Xen region. So we have to

> register

> > -         * this re-mapped Xen region as a virtual region temporarily.

> > -         */

> > -        patch_region.start = xenmap;

> > -        patch_region.end = xenmap + xen_size;

> > -        register_virtual_region(&patch_region);

> > -

> > -        /*

> >           * Find the virtual address of the alternative region in the new

> >           * mapping.

> >           * alt_instr contains relative offset, so the function

> >           * __apply_alternatives will patch in the re-mapped version of

> >           * Xen.

> >           */

> > -        region.begin = (void *)__alt_instructions - (void *)_start +

> xenmap;

> > -        region.end = (void *)__alt_instructions_end - (void *)_start +

> xenmap;

> > +        region.begin = __alt_instructions;

> > +        region.end = __alt_instructions_end;

> >

> > -        ret = __apply_alternatives(&region);

> > +        ret = __apply_alternatives(&region, xenmap - (void *)_start);

> >          /* The patching is not expected to fail during boot. */

> >          BUG_ON(ret != 0);

> >

> > -        unregister_virtual_region(&patch_region);

> > -

> >          vunmap(xenmap);

> >

> >          /* Barriers provided by the cache flushing */

> > @@ -235,7 +226,7 @@ int apply_alternatives(const struct alt_instr

> *start, const struct alt_instr *en

> >          .end = end,

> >      };

> >

> > -    return __apply_alternatives(&region);

> > +    return __apply_alternatives(&region, 0);

> >  }

> >

> >  /*

> > --

> > 2.11.0

> >

>

> _______________________________________________

> Xen-devel mailing list

> Xen-devel@lists.xenproject.org

> https://lists.xenproject.org/mailman/listinfo/xen-devel
<br><br><div class="gmail_quote"><div dir="ltr">On Fri, 25 May 2018, 22:54 Stefano Stabellini, &lt;<a href="mailto:sstabellini@kernel.org">sstabellini@kernel.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You might want to CC Konrad next time on this patch<br></blockquote></div><div><br></div><div>May I ask why? This code falls under Arm maintainership.</div><div><br></div><div>Cheers,</div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
On Tue, 22 May 2018, Julien Grall wrote:<br>
&gt; This is part of XSA-263.<br>
&gt; <br>
&gt; Signed-off-by: Julien Grall &lt;<a href="mailto:julien.grall@arm.com" target="_blank">julien.grall@arm.com</a>&gt;<br>
&gt; <br>
&gt; ---<br>
&gt;     I am aware of the missing commit message here. I wanted to send the<br>
&gt;     series on the ML to get some feedback first.<br>
&gt; ---<br>
&gt;  xen/arch/arm/alternative.c | 35 +++++++++++++----------------------<br>
&gt;  1 file changed, 13 insertions(+), 22 deletions(-)<br>
&gt; <br>
&gt; diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c<br>
&gt; index 9ffdc475d6..bd62183def 100644<br>
&gt; --- a/xen/arch/arm/alternative.c<br>
&gt; +++ b/xen/arch/arm/alternative.c<br>
&gt; @@ -97,12 +97,16 @@ static u32 get_alt_insn(const struct alt_instr *alt,<br>
&gt;  /*<br>
&gt;   * The region patched should be read-write to allow __apply_alternatives<br>
&gt;   * to replacing the instructions when necessary.<br>
&gt; + *<br>
&gt; + * @update_offset: Offset between the region patched and the writable<br>
&gt; + * region for the update. 0 if the patched region is writable.<br>
&gt;   */<br>
&gt; -static int __apply_alternatives(const struct alt_region *region)<br>
&gt; +static int __apply_alternatives(const struct alt_region *region,<br>
&gt; +                                paddr_t update_offset)<br>
&gt;  {<br>
&gt;      const struct alt_instr *alt;<br>
&gt; -    const u32 *replptr;<br>
&gt; -    u32 *origptr;<br>
&gt; +    const u32 *replptr, *origptr;<br>
&gt; +    u32 *updptr;<br>
&gt;  <br>
&gt;      printk(XENLOG_INFO &quot;alternatives: Patching with alt table %p -&gt; %p\n&quot;,<br>
&gt;             region-&gt;begin, region-&gt;end);<br>
&gt; @@ -118,6 +122,7 @@ static int __apply_alternatives(const struct alt_region *region)<br>
&gt;          BUG_ON(alt-&gt;alt_len != alt-&gt;orig_len);<br>
&gt;  <br>
&gt;          origptr = ALT_ORIG_PTR(alt);<br>
&gt; +        updptr = (void *)origptr + update_offset;<br>
&gt;          replptr = ALT_REPL_PTR(alt);<br>
&gt;  <br>
&gt;          nr_inst = alt-&gt;alt_len / sizeof(insn);<br>
&gt; @@ -125,7 +130,7 @@ static int __apply_alternatives(const struct alt_region *region)<br>
&gt;          for ( i = 0; i &lt; nr_inst; i++ )<br>
&gt;          {<br>
&gt;              insn = get_alt_insn(alt, origptr + i, replptr + i);<br>
&gt; -            *(origptr + i) = cpu_to_le32(insn);<br>
&gt; +            *(updptr + i) = cpu_to_le32(insn);<br>
&gt;          }<br>
&gt;  <br>
&gt;          /* Ensure the new instructions reached the memory and nuke */<br>
&gt; @@ -162,9 +167,6 @@ static int __apply_alternatives_multi_stop(void *unused)<br>
&gt;          paddr_t xen_size = _end - _start;<br>
&gt;          unsigned int xen_order = get_order_from_bytes(xen_size);<br>
&gt;          void *xenmap;<br>
&gt; -        struct virtual_region patch_region = {<br>
&gt; -            .list = LIST_HEAD_INIT(patch_region.list),<br>
&gt; -        };<br>
&gt;  <br>
&gt;          BUG_ON(patched);<br>
&gt;  <br>
&gt; @@ -178,30 +180,19 @@ static int __apply_alternatives_multi_stop(void *unused)<br>
&gt;          BUG_ON(!xenmap);<br>
&gt;  <br>
&gt;          /*<br>
&gt; -         * If we generate a new branch instruction, the target will be<br>
&gt; -         * calculated in this re-mapped Xen region. So we have to register<br>
&gt; -         * this re-mapped Xen region as a virtual region temporarily.<br>
&gt; -         */<br>
&gt; -        patch_region.start = xenmap;<br>
&gt; -        patch_region.end = xenmap + xen_size;<br>
&gt; -        register_virtual_region(&amp;patch_region);<br>
&gt; -<br>
&gt; -        /*<br>
&gt;           * Find the virtual address of the alternative region in the new<br>
&gt;           * mapping.<br>
&gt;           * alt_instr contains relative offset, so the function<br>
&gt;           * __apply_alternatives will patch in the re-mapped version of<br>
&gt;           * Xen.<br>
&gt;           */<br>
&gt; -        region.begin = (void *)__alt_instructions - (void *)_start + xenmap;<br>
&gt; -        region.end = (void *)__alt_instructions_end - (void *)_start + xenmap;<br>
&gt; +        region.begin = __alt_instructions;<br>
&gt; +        region.end = __alt_instructions_end;<br>
&gt;  <br>
&gt; -        ret = __apply_alternatives(&amp;region);<br>
&gt; +        ret = __apply_alternatives(&amp;region, xenmap - (void *)_start);<br>
&gt;          /* The patching is not expected to fail during boot. */<br>
&gt;          BUG_ON(ret != 0);<br>
&gt;  <br>
&gt; -        unregister_virtual_region(&amp;patch_region);<br>
&gt; -<br>
&gt;          vunmap(xenmap);<br>
&gt;  <br>
&gt;          /* Barriers provided by the cache flushing */<br>
&gt; @@ -235,7 +226,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en<br>
&gt;          .end = end,<br>
&gt;      };<br>
&gt;  <br>
&gt; -    return __apply_alternatives(&amp;region);<br>
&gt; +    return __apply_alternatives(&amp;region, 0);<br>
&gt;  }<br>
&gt;  <br>
&gt;  /*<br>
&gt; -- <br>
&gt; 2.11.0<br>
&gt; <br>
<br>
_______________________________________________<br>
Xen-devel mailing list<br>
<a href="mailto:Xen-devel@lists.xenproject.org" target="_blank">Xen-devel@lists.xenproject.org</a><br>
<a href="https://lists.xenproject.org/mailman/listinfo/xen-devel" rel="noreferrer" target="_blank">https://lists.xenproject.org/mailman/listinfo/xen-devel</a></blockquote></div>
Stefano Stabellini May 25, 2018, 11:24 p.m. | #3
On Fri, 25 May 2018, Julien Grall wrote:
> On Fri, 25 May 2018, 22:54 Stefano Stabellini, <sstabellini@kernel.org> wrote:

>       You might want to CC Konrad next time on this patch

> 

> 

> May I ask why? This code falls under Arm maintainership.


I know, but I appreciate his input if he has time for it


> 

> 

>       On Tue, 22 May 2018, Julien Grall wrote:

>       > This is part of XSA-263.

>       >

>       > Signed-off-by: Julien Grall <julien.grall@arm.com>

>       >

>       > ---

>       >     I am aware of the missing commit message here. I wanted to send the

>       >     series on the ML to get some feedback first.

>       > ---

>       >  xen/arch/arm/alternative.c | 35 +++++++++++++----------------------

>       >  1 file changed, 13 insertions(+), 22 deletions(-)

>       >

>       > diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c

>       > index 9ffdc475d6..bd62183def 100644

>       > --- a/xen/arch/arm/alternative.c

>       > +++ b/xen/arch/arm/alternative.c

>       > @@ -97,12 +97,16 @@ static u32 get_alt_insn(const struct alt_instr *alt,

>       >  /*

>       >   * The region patched should be read-write to allow __apply_alternatives

>       >   * to replacing the instructions when necessary.

>       > + *

>       > + * @update_offset: Offset between the region patched and the writable

>       > + * region for the update. 0 if the patched region is writable.

>       >   */

>       > -static int __apply_alternatives(const struct alt_region *region)

>       > +static int __apply_alternatives(const struct alt_region *region,

>       > +                                paddr_t update_offset)

>       >  {

>       >      const struct alt_instr *alt;

>       > -    const u32 *replptr;

>       > -    u32 *origptr;

>       > +    const u32 *replptr, *origptr;

>       > +    u32 *updptr;

>       > 

>       >      printk(XENLOG_INFO "alternatives: Patching with alt table %p -> %p\n",

>       >             region->begin, region->end);

>       > @@ -118,6 +122,7 @@ static int __apply_alternatives(const struct alt_region *region)

>       >          BUG_ON(alt->alt_len != alt->orig_len);

>       > 

>       >          origptr = ALT_ORIG_PTR(alt);

>       > +        updptr = (void *)origptr + update_offset;

>       >          replptr = ALT_REPL_PTR(alt);

>       > 

>       >          nr_inst = alt->alt_len / sizeof(insn);

>       > @@ -125,7 +130,7 @@ static int __apply_alternatives(const struct alt_region *region)

>       >          for ( i = 0; i < nr_inst; i++ )

>       >          {

>       >              insn = get_alt_insn(alt, origptr + i, replptr + i);

>       > -            *(origptr + i) = cpu_to_le32(insn);

>       > +            *(updptr + i) = cpu_to_le32(insn);

>       >          }

>       > 

>       >          /* Ensure the new instructions reached the memory and nuke */

>       > @@ -162,9 +167,6 @@ static int __apply_alternatives_multi_stop(void *unused)

>       >          paddr_t xen_size = _end - _start;

>       >          unsigned int xen_order = get_order_from_bytes(xen_size);

>       >          void *xenmap;

>       > -        struct virtual_region patch_region = {

>       > -            .list = LIST_HEAD_INIT(patch_region.list),

>       > -        };

>       > 

>       >          BUG_ON(patched);

>       > 

>       > @@ -178,30 +180,19 @@ static int __apply_alternatives_multi_stop(void *unused)

>       >          BUG_ON(!xenmap);

>       > 

>       >          /*

>       > -         * If we generate a new branch instruction, the target will be

>       > -         * calculated in this re-mapped Xen region. So we have to register

>       > -         * this re-mapped Xen region as a virtual region temporarily.

>       > -         */

>       > -        patch_region.start = xenmap;

>       > -        patch_region.end = xenmap + xen_size;

>       > -        register_virtual_region(&patch_region);

>       > -

>       > -        /*

>       >           * Find the virtual address of the alternative region in the new

>       >           * mapping.

>       >           * alt_instr contains relative offset, so the function

>       >           * __apply_alternatives will patch in the re-mapped version of

>       >           * Xen.

>       >           */

>       > -        region.begin = (void *)__alt_instructions - (void *)_start + xenmap;

>       > -        region.end = (void *)__alt_instructions_end - (void *)_start + xenmap;

>       > +        region.begin = __alt_instructions;

>       > +        region.end = __alt_instructions_end;

>       > 

>       > -        ret = __apply_alternatives(&region);

>       > +        ret = __apply_alternatives(&region, xenmap - (void *)_start);

>       >          /* The patching is not expected to fail during boot. */

>       >          BUG_ON(ret != 0);

>       > 

>       > -        unregister_virtual_region(&patch_region);

>       > -

>       >          vunmap(xenmap);

>       > 

>       >          /* Barriers provided by the cache flushing */

>       > @@ -235,7 +226,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en

>       >          .end = end,

>       >      };

>       > 

>       > -    return __apply_alternatives(&region);

>       > +    return __apply_alternatives(&region, 0);

>       >  }

>       > 

>       >  /*

>       > --

>       > 2.11.0

>       >

> 

>       _______________________________________________

>       Xen-devel mailing list

>       Xen-devel@lists.xenproject.org

>       https://lists.xenproject.org/mailman/listinfo/xen-devel

> 

> 

>
Julien Grall May 29, 2018, 11:34 a.m. | #4
On 26/05/18 00:24, Stefano Stabellini wrote:
> On Fri, 25 May 2018, Julien Grall wrote:
>> On Fri, 25 May 2018, 22:54 Stefano Stabellini, <sstabellini@kernel.org> wrote:
>>        You might want to CC Konrad next time on this patch
>>
>>
>> May I ask why? This code falls under Arm maintainership.
> 
> I know, but I appreciate his input if he has time for it

I will, but you could also have CCed him from your first e-mail to give 
him more time.

Cheers,

Patch

diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c
index 9ffdc475d6..bd62183def 100644
--- a/xen/arch/arm/alternative.c
+++ b/xen/arch/arm/alternative.c
@@ -97,12 +97,16 @@  static u32 get_alt_insn(const struct alt_instr *alt,
 /*
  * The region patched should be read-write to allow __apply_alternatives
  * to replacing the instructions when necessary.
+ *
+ * @update_offset: Offset between the region patched and the writable
+ * region for the update. 0 if the patched region is writable.
  */
-static int __apply_alternatives(const struct alt_region *region)
+static int __apply_alternatives(const struct alt_region *region,
+                                paddr_t update_offset)
 {
     const struct alt_instr *alt;
-    const u32 *replptr;
-    u32 *origptr;
+    const u32 *replptr, *origptr;
+    u32 *updptr;
 
     printk(XENLOG_INFO "alternatives: Patching with alt table %p -> %p\n",
            region->begin, region->end);
@@ -118,6 +122,7 @@  static int __apply_alternatives(const struct alt_region *region)
         BUG_ON(alt->alt_len != alt->orig_len);
 
         origptr = ALT_ORIG_PTR(alt);
+        updptr = (void *)origptr + update_offset;
         replptr = ALT_REPL_PTR(alt);
 
         nr_inst = alt->alt_len / sizeof(insn);
@@ -125,7 +130,7 @@  static int __apply_alternatives(const struct alt_region *region)
         for ( i = 0; i < nr_inst; i++ )
         {
             insn = get_alt_insn(alt, origptr + i, replptr + i);
-            *(origptr + i) = cpu_to_le32(insn);
+            *(updptr + i) = cpu_to_le32(insn);
         }
 
         /* Ensure the new instructions reached the memory and nuke */
@@ -162,9 +167,6 @@  static int __apply_alternatives_multi_stop(void *unused)
         paddr_t xen_size = _end - _start;
         unsigned int xen_order = get_order_from_bytes(xen_size);
         void *xenmap;
-        struct virtual_region patch_region = {
-            .list = LIST_HEAD_INIT(patch_region.list),
-        };
 
         BUG_ON(patched);
 
@@ -178,30 +180,19 @@  static int __apply_alternatives_multi_stop(void *unused)
         BUG_ON(!xenmap);
 
         /*
-         * If we generate a new branch instruction, the target will be
-         * calculated in this re-mapped Xen region. So we have to register
-         * this re-mapped Xen region as a virtual region temporarily.
-         */
-        patch_region.start = xenmap;
-        patch_region.end = xenmap + xen_size;
-        register_virtual_region(&patch_region);
-
-        /*
          * Find the virtual address of the alternative region in the new
          * mapping.
          * alt_instr contains relative offset, so the function
          * __apply_alternatives will patch in the re-mapped version of
          * Xen.
          */
-        region.begin = (void *)__alt_instructions - (void *)_start + xenmap;
-        region.end = (void *)__alt_instructions_end - (void *)_start + xenmap;
+        region.begin = __alt_instructions;
+        region.end = __alt_instructions_end;
 
-        ret = __apply_alternatives(&region);
+        ret = __apply_alternatives(&region, xenmap - (void *)_start);
         /* The patching is not expected to fail during boot. */
         BUG_ON(ret != 0);
 
-        unregister_virtual_region(&patch_region);
-
         vunmap(xenmap);
 
         /* Barriers provided by the cache flushing */
@@ -235,7 +226,7 @@  int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
         .end = end,
     };
 
-    return __apply_alternatives(&region);
+    return __apply_alternatives(&region, 0);
 }
 
 /*