diff mbox series

[v1,1/3] powerpc/code-patching: Test patch_instructions() during boot

Message ID 20240315025736.404867-1-bgray@linux.ibm.com (mailing list archive)
State Superseded
Headers show
Series [v1,1/3] powerpc/code-patching: Test patch_instructions() during boot | expand

Commit Message

Benjamin Gray March 15, 2024, 2:57 a.m. UTC
patch_instructions() introduces new behaviour with a couple of
variations. Test each case of

  * a repeated 32-bit instruction,
  * a repeated 64-bit instruction (ppc64), and
  * a copied sequence of instructions

for both on a single page and when it crosses a page boundary.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
---
 arch/powerpc/lib/test-code-patching.c | 92 +++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

Comments

Christophe Leroy March 15, 2024, 7:14 a.m. UTC | #1
Le 15/03/2024 à 03:57, Benjamin Gray a écrit :
> patch_instructions() introduces new behaviour with a couple of
> variations. Test each case of
> 
>    * a repeated 32-bit instruction,
>    * a repeated 64-bit instruction (ppc64), and
>    * a copied sequence of instructions
> 
> for both on a single page and when it crosses a page boundary.
> 
> Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
> ---
>   arch/powerpc/lib/test-code-patching.c | 92 +++++++++++++++++++++++++++
>   1 file changed, 92 insertions(+)
> 
> diff --git a/arch/powerpc/lib/test-code-patching.c b/arch/powerpc/lib/test-code-patching.c
> index c44823292f73..35a3756272df 100644
> --- a/arch/powerpc/lib/test-code-patching.c
> +++ b/arch/powerpc/lib/test-code-patching.c
> @@ -347,6 +347,97 @@ static void __init test_prefixed_patching(void)
>   	check(!memcmp(iptr, expected, sizeof(expected)));
>   }
>   
> +static void __init test_multi_instruction_patching(void)
> +{
> +	u32 code[256];

Build failure:

   CC      arch/powerpc/lib/test-code-patching.o
arch/powerpc/lib/test-code-patching.c: In function 
'test_multi_instruction_patching':
arch/powerpc/lib/test-code-patching.c:439:1: error: the frame size of 
1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
   439 | }
       | ^
cc1: all warnings being treated as errors
make[4]: *** [scripts/Makefile.build:243: 
arch/powerpc/lib/test-code-patching.o] Error 1


I have to avoid big arrays on the stack.


> +	void *buf;
> +	u32 *addr32;
> +	u64 *addr64;
> +	ppc_inst_t inst64 = ppc_inst_prefix(OP_PREFIX << 26 | 3UL << 24, PPC_RAW_TRAP());
> +	u32 inst32 = PPC_RAW_NOP();
> +
> +	buf = vzalloc(PAGE_SIZE * 8);
> +	check(buf);
> +	if (!buf)
> +		return;
> +
> +	/* Test single page 32-bit repeated instruction */
> +	addr32 = buf + PAGE_SIZE;
> +	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
> +
> +	check(addr32[0] == 0);
> +	check(addr32[1] == inst32);
> +	check(addr32[2] == inst32);
> +	check(addr32[3] == inst32);
> +	check(addr32[4] == 0);
> +
> +	/* Test single page 64-bit repeated instruction */
> +	if (IS_ENABLED(CONFIG_PPC64)) {
> +		check(ppc_inst_prefixed(inst64));
> +
> +		addr64 = buf + PAGE_SIZE * 2;
> +		ppc_inst_write(code, inst64);
> +		check(!patch_instructions((u32 *)(addr64 + 1), code, 24, true));
> +
> +		check(addr64[0] == 0);
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[1]), inst64));
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[2]), inst64));
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[3]), inst64));
> +		check(addr64[4] == 0);
> +	}
> +
> +	/* Test single page memcpy */
> +	addr32 = buf + PAGE_SIZE * 3;
> +
> +	for (int i = 0; i < ARRAY_SIZE(code); i++)
> +		code[i] = i + 1;
> +
> +	check(!patch_instructions(addr32 + 1, code, sizeof(code), false));
> +
> +	check(addr32[0] == 0);
> +	check(!memcmp(&addr32[1], code, sizeof(code)));
> +	check(addr32[ARRAY_SIZE(code) + 1] == 0);
> +
> +	/* Test multipage 32-bit repeated instruction */
> +	addr32 = buf + PAGE_SIZE * 4 - 8;
> +	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
> +
> +	check(addr32[0] == 0);
> +	check(addr32[1] == inst32);
> +	check(addr32[2] == inst32);
> +	check(addr32[3] == inst32);
> +	check(addr32[4] == 0);
> +
> +	/* Test multipage 64-bit repeated instruction */
> +	if (IS_ENABLED(CONFIG_PPC64)) {
> +		check(ppc_inst_prefixed(inst64));
> +
> +		addr64 = buf + PAGE_SIZE * 5 - 8;
> +		ppc_inst_write(code, inst64);
> +		check(!patch_instructions((u32 *)(addr64 + 1), code, 24, true));
> +
> +		check(addr64[0] == 0);
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[1]), inst64));
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[2]), inst64));
> +		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[3]), inst64));
> +		check(addr64[4] == 0);
> +	}
> +
> +	/* Test multipage memcpy */
> +	addr32 = buf + PAGE_SIZE * 6 - 12;
> +
> +	for (int i = 0; i < ARRAY_SIZE(code); i++)
> +		code[i] = i + 1;
> +
> +	check(!patch_instructions(addr32 + 1, code, sizeof(code), false));
> +
> +	check(addr32[0] == 0);
> +	check(!memcmp(&addr32[1], code, sizeof(code)));
> +	check(addr32[ARRAY_SIZE(code) + 1] == 0);
> +
> +	vfree(buf);
> +}
> +
>   static int __init test_code_patching(void)
>   {
>   	pr_info("Running code patching self-tests ...\n");
> @@ -356,6 +447,7 @@ static int __init test_code_patching(void)
>   	test_create_function_call();
>   	test_translate_branch();
>   	test_prefixed_patching();
> +	test_multi_instruction_patching();
>   
>   	return 0;
>   }
Benjamin Gray March 17, 2024, 9:38 p.m. UTC | #2
On Fri, 2024-03-15 at 07:14 +0000, Christophe Leroy wrote:
> 
> 
> Le 15/03/2024 à 03:57, Benjamin Gray a écrit :
> > patch_instructions() introduces new behaviour with a couple of
> > variations. Test each case of
> > 
> >    * a repeated 32-bit instruction,
> >    * a repeated 64-bit instruction (ppc64), and
> >    * a copied sequence of instructions
> > 
> > for both on a single page and when it crosses a page boundary.
> > 
> > Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
> > ---
> >   arch/powerpc/lib/test-code-patching.c | 92
> > +++++++++++++++++++++++++++
> >   1 file changed, 92 insertions(+)
> > 
> > diff --git a/arch/powerpc/lib/test-code-patching.c
> > b/arch/powerpc/lib/test-code-patching.c
> > index c44823292f73..35a3756272df 100644
> > --- a/arch/powerpc/lib/test-code-patching.c
> > +++ b/arch/powerpc/lib/test-code-patching.c
> > @@ -347,6 +347,97 @@ static void __init
> > test_prefixed_patching(void)
> >   	check(!memcmp(iptr, expected, sizeof(expected)));
> >   }
> >   
> > +static void __init test_multi_instruction_patching(void)
> > +{
> > +	u32 code[256];
> 
> Build failure:
> 
>    CC      arch/powerpc/lib/test-code-patching.o
> arch/powerpc/lib/test-code-patching.c: In function 
> 'test_multi_instruction_patching':
> arch/powerpc/lib/test-code-patching.c:439:1: error: the frame size of
> 1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
>    439 | }
>        | ^
> cc1: all warnings being treated as errors
> make[4]: *** [scripts/Makefile.build:243: 
> arch/powerpc/lib/test-code-patching.o] Error 1
> 
> 
> I have to avoid big arrays on the stack.

All good, I can do that.

I do run my patches through a couple of 32-bit configs, but I didn't
see this error. Is this a standard config I should be testing with?

> 
> 
> > +	void *buf;
> > +	u32 *addr32;
> > +	u64 *addr64;
> > +	ppc_inst_t inst64 = ppc_inst_prefix(OP_PREFIX << 26 | 3UL
> > << 24, PPC_RAW_TRAP());
> > +	u32 inst32 = PPC_RAW_NOP();
> > +
> > +	buf = vzalloc(PAGE_SIZE * 8);
> > +	check(buf);
> > +	if (!buf)
> > +		return;
> > +
> > +	/* Test single page 32-bit repeated instruction */
> > +	addr32 = buf + PAGE_SIZE;
> > +	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
> > +
> > +	check(addr32[0] == 0);
> > +	check(addr32[1] == inst32);
> > +	check(addr32[2] == inst32);
> > +	check(addr32[3] == inst32);
> > +	check(addr32[4] == 0);
> > +
> > +	/* Test single page 64-bit repeated instruction */
> > +	if (IS_ENABLED(CONFIG_PPC64)) {
> > +		check(ppc_inst_prefixed(inst64));
> > +
> > +		addr64 = buf + PAGE_SIZE * 2;
> > +		ppc_inst_write(code, inst64);
> > +		check(!patch_instructions((u32 *)(addr64 + 1),
> > code, 24, true));
> > +
> > +		check(addr64[0] == 0);
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[1]), inst64));
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[2]), inst64));
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[3]), inst64));
> > +		check(addr64[4] == 0);
> > +	}
> > +
> > +	/* Test single page memcpy */
> > +	addr32 = buf + PAGE_SIZE * 3;
> > +
> > +	for (int i = 0; i < ARRAY_SIZE(code); i++)
> > +		code[i] = i + 1;
> > +
> > +	check(!patch_instructions(addr32 + 1, code, sizeof(code),
> > false));
> > +
> > +	check(addr32[0] == 0);
> > +	check(!memcmp(&addr32[1], code, sizeof(code)));
> > +	check(addr32[ARRAY_SIZE(code) + 1] == 0);
> > +
> > +	/* Test multipage 32-bit repeated instruction */
> > +	addr32 = buf + PAGE_SIZE * 4 - 8;
> > +	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
> > +
> > +	check(addr32[0] == 0);
> > +	check(addr32[1] == inst32);
> > +	check(addr32[2] == inst32);
> > +	check(addr32[3] == inst32);
> > +	check(addr32[4] == 0);
> > +
> > +	/* Test multipage 64-bit repeated instruction */
> > +	if (IS_ENABLED(CONFIG_PPC64)) {
> > +		check(ppc_inst_prefixed(inst64));
> > +
> > +		addr64 = buf + PAGE_SIZE * 5 - 8;
> > +		ppc_inst_write(code, inst64);
> > +		check(!patch_instructions((u32 *)(addr64 + 1),
> > code, 24, true));
> > +
> > +		check(addr64[0] == 0);
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[1]), inst64));
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[2]), inst64));
> > +		check(ppc_inst_equal(ppc_inst_read((u32
> > *)&addr64[3]), inst64));
> > +		check(addr64[4] == 0);
> > +	}
> > +
> > +	/* Test multipage memcpy */
> > +	addr32 = buf + PAGE_SIZE * 6 - 12;
> > +
> > +	for (int i = 0; i < ARRAY_SIZE(code); i++)
> > +		code[i] = i + 1;
> > +
> > +	check(!patch_instructions(addr32 + 1, code, sizeof(code),
> > false));
> > +
> > +	check(addr32[0] == 0);
> > +	check(!memcmp(&addr32[1], code, sizeof(code)));
> > +	check(addr32[ARRAY_SIZE(code) + 1] == 0);
> > +
> > +	vfree(buf);
> > +}
> > +
> >   static int __init test_code_patching(void)
> >   {
> >   	pr_info("Running code patching self-tests ...\n");
> > @@ -356,6 +447,7 @@ static int __init test_code_patching(void)
> >   	test_create_function_call();
> >   	test_translate_branch();
> >   	test_prefixed_patching();
> > +	test_multi_instruction_patching();
> >   
> >   	return 0;
> >   }
Benjamin Gray March 17, 2024, 10:23 p.m. UTC | #3
On Mon, 2024-03-18 at 08:38 +1100, Benjamin Gray wrote:
> On Fri, 2024-03-15 at 07:14 +0000, Christophe Leroy wrote:
> > 
> > 
> > Le 15/03/2024 à 03:57, Benjamin Gray a écrit :
> > > patch_instructions() introduces new behaviour with a couple of
> > > variations. Test each case of
> > > 
> > >    * a repeated 32-bit instruction,
> > >    * a repeated 64-bit instruction (ppc64), and
> > >    * a copied sequence of instructions
> > > 
> > > for both on a single page and when it crosses a page boundary.
> > > 
> > > Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
> > > ---
> > >   arch/powerpc/lib/test-code-patching.c | 92
> > > +++++++++++++++++++++++++++
> > >   1 file changed, 92 insertions(+)
> > > 
> > > diff --git a/arch/powerpc/lib/test-code-patching.c
> > > b/arch/powerpc/lib/test-code-patching.c
> > > index c44823292f73..35a3756272df 100644
> > > --- a/arch/powerpc/lib/test-code-patching.c
> > > +++ b/arch/powerpc/lib/test-code-patching.c
> > > @@ -347,6 +347,97 @@ static void __init
> > > test_prefixed_patching(void)
> > >   	check(!memcmp(iptr, expected, sizeof(expected)));
> > >   }
> > >   
> > > +static void __init test_multi_instruction_patching(void)
> > > +{
> > > +	u32 code[256];
> > 
> > Build failure:
> > 
> >    CC      arch/powerpc/lib/test-code-patching.o
> > arch/powerpc/lib/test-code-patching.c: In function 
> > 'test_multi_instruction_patching':
> > arch/powerpc/lib/test-code-patching.c:439:1: error: the frame size
> > of
> > 1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
> >    439 | }
> >        | ^
> > cc1: all warnings being treated as errors
> > make[4]: *** [scripts/Makefile.build:243: 
> > arch/powerpc/lib/test-code-patching.o] Error 1
> > 
> > 
> > I have to avoid big arrays on the stack.
> 
> All good, I can do that.
> 
> I do run my patches through a couple of 32-bit configs, but I didn't
> see this error. Is this a standard config I should be testing with?

Specifically pmac32_defconfig and ppc44x_defconfig
Benjamin Gray March 17, 2024, 10:24 p.m. UTC | #4
mjW5LaBPOdGiiDE1w95Ri9HRK27S2dRZpyib9L4mkfYWPAF41mTudjKmVpgtBLO//rO+zmF04OMB/4sWJhLfvhq1CXULDqw5dcuIAIYwf2ughOtyAPFK1ViDcMO5X1bVpNAFO5m4VBpZvFDQ0j0JfqfVBdL68uH05W1/8dMj76RaWj5m0rLM5slY1FQUPddSU+ic9vaZhlDepjU3ZyI8fmioofNGHaxJq6uNTytKdj87kwDV6PQ4hmuGtY56C7JCgjp053sRJ6sXqgKBWfe4ZOJH17mQm+fws93byLoZvvz4Z3im0Rb0MlFo/WirNyhu+TmTNLpnzFUZfenoKrqAkZLY8u1iCFquhgqA321P+sfYew66DtwQmaoi2GKmF89y2enXXzjLNKfLDKkuVoKxFSPeizYqrLi22R9iO8EGBYKACAWIQQ9K5v9I+L06Hi4yOJ5xrdpFsvehAUCYzuwkQIbAgCBCRB5xrdpFsvehHYgBBkRCAAdFiEESFUlaLYscsf4Dt5gaavCcpI6D/8FAmM7sJEACgkQaavCcpI6D/95UgEAqfSj0QhCrYfazQiLDKJstrz3oIKFjhB6+FYMZqt+K1MA/2ioFtHbypeeWbsqYYRhRyTjAKcvE1NZGtH/YWLgkViUidoBAN6gFX/P+VWB77/w8S/BnPmnJx45wmphlkCL8ckOyopFAQCj9eWamHCl2DSaASMSuoZed6C6Gm0OFtuZh/r8K485BQ==
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Date: Mon, 18 Mar 2024 08:55:02 +1100
MIME-Version: 1.0
User-Agent: Evolution 3.50.4 (3.50.4-1.fc39) 
X-Trend-IP-HD: ip=[9.192.253.14]helo={ozlabs.au.ibm.com}sender=(bgray@linux.ibm.com)recipient=<christophe.leroy@csgroup.eu;mpe@ellerman.id.au;linuxppc-dev@lists.ozlabs.org>

On Mon, 2024-03-18 at 08:38 +1100, Benjamin Gray wrote:
> On Fri, 2024-03-15 at 07:14 +0000, Christophe Leroy wrote:
> >=20
> >=20
> > Le 15/03/2024 =C3=A0 03:57, Benjamin Gray a =C3=A9crit=C2=A0:
> > > patch_instructions() introduces new behaviour with a couple of
> > > variations. Test each case of
> > >=20
> > > =C2=A0=C2=A0 * a repeated 32-bit instruction,
> > > =C2=A0=C2=A0 * a repeated 64-bit instruction (ppc64), and
> > > =C2=A0=C2=A0 * a copied sequence of instructions
> > >=20
> > > for both on a single page and when it crosses a page boundary.
> > >=20
> > > Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
> > > ---
> > > =C2=A0 arch/powerpc/lib/test-code-patching.c | 92
> > > +++++++++++++++++++++++++++
> > > =C2=A0 1 file changed, 92 insertions(+)
> > >=20
> > > diff --git a/arch/powerpc/lib/test-code-patching.c
> > > b/arch/powerpc/lib/test-code-patching.c
> > > index c44823292f73..35a3756272df 100644
> > > --- a/arch/powerpc/lib/test-code-patching.c
> > > +++ b/arch/powerpc/lib/test-code-patching.c
> > > @@ -347,6 +347,97 @@ static void __init
> > > test_prefixed_patching(void)
> > > =C2=A0=C2=A0	check(!memcmp(iptr, expected, sizeof(expected)));
> > > =C2=A0 }
> > > =C2=A0=20
> > > +static void __init test_multi_instruction_patching(void)
> > > +{
> > > +	u32 code[256];
> >=20
> > Build failure:
> >=20
> > =C2=A0=C2=A0 CC=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 arch/powerpc/lib/test-cod=
e-patching.o
> > arch/powerpc/lib/test-code-patching.c: In function=20
> > 'test_multi_instruction_patching':
> > arch/powerpc/lib/test-code-patching.c:439:1: error: the frame size
> > of
> > 1040 bytes is larger than 1024 bytes [-Werror=3Dframe-larger-than=3D]
> > =C2=A0=C2=A0 439 | }
> > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | ^
> > cc1: all warnings being treated as errors
> > make[4]: *** [scripts/Makefile.build:243:=20
> > arch/powerpc/lib/test-code-patching.o] Error 1
> >=20
> >=20
> > I have to avoid big arrays on the stack.
>=20
> All good, I can do that.
>=20
> I do run my patches through a couple of 32-bit configs, but I didn't
> see this error. Is this a standard config I should be testing with?
>=20

Specifically I build pmac32_defconfig and ppc44x_defconfig
Michael Ellerman March 18, 2024, 2:25 a.m. UTC | #5
Benjamin Gray <bgray@linux.ibm.com> writes:
> On Mon, 2024-03-18 at 08:38 +1100, Benjamin Gray wrote:
>> On Fri, 2024-03-15 at 07:14 +0000, Christophe Leroy wrote:
>> > Le 15/03/2024 à 03:57, Benjamin Gray a écrit :
>> > > diff --git a/arch/powerpc/lib/test-code-patching.c
>> > > b/arch/powerpc/lib/test-code-patching.c
>> > > index c44823292f73..35a3756272df 100644
>> > > --- a/arch/powerpc/lib/test-code-patching.c
>> > > +++ b/arch/powerpc/lib/test-code-patching.c
>> > > @@ -347,6 +347,97 @@ static void __init
>> > > test_prefixed_patching(void)
>> > >   	check(!memcmp(iptr, expected, sizeof(expected)));
>> > >   }
>> > >   
>> > > +static void __init test_multi_instruction_patching(void)
>> > > +{
>> > > +	u32 code[256];
>> > 
>> > Build failure:
>> > 
>> >    CC      arch/powerpc/lib/test-code-patching.o
>> > arch/powerpc/lib/test-code-patching.c: In function 
>> > 'test_multi_instruction_patching':
>> > arch/powerpc/lib/test-code-patching.c:439:1: error: the frame size
>> > of
>> > 1040 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]
>> >    439 | }
>> >        | ^
>> > cc1: all warnings being treated as errors
>> > make[4]: *** [scripts/Makefile.build:243: 
>> > arch/powerpc/lib/test-code-patching.o] Error 1
>> > 
>> > 
>> > I have to avoid big arrays on the stack.
>> 
>> All good, I can do that.
>> 
>> I do run my patches through a couple of 32-bit configs, but I didn't
>> see this error. Is this a standard config I should be testing with?
>
> Specifically pmac32_defconfig and ppc44x_defconfig

Both of those have CONFIG_FRAME_WARN=1024, so should have caught this.

But neither have CONFIG_CODE_PATCHING_SELFTEST=y, so I suspect that's
why you didn't see it.

I recommend ppc32_allmodconfig.

cheers
diff mbox series

Patch

diff --git a/arch/powerpc/lib/test-code-patching.c b/arch/powerpc/lib/test-code-patching.c
index c44823292f73..35a3756272df 100644
--- a/arch/powerpc/lib/test-code-patching.c
+++ b/arch/powerpc/lib/test-code-patching.c
@@ -347,6 +347,97 @@  static void __init test_prefixed_patching(void)
 	check(!memcmp(iptr, expected, sizeof(expected)));
 }
 
+static void __init test_multi_instruction_patching(void)
+{
+	u32 code[256];
+	void *buf;
+	u32 *addr32;
+	u64 *addr64;
+	ppc_inst_t inst64 = ppc_inst_prefix(OP_PREFIX << 26 | 3UL << 24, PPC_RAW_TRAP());
+	u32 inst32 = PPC_RAW_NOP();
+
+	buf = vzalloc(PAGE_SIZE * 8);
+	check(buf);
+	if (!buf)
+		return;
+
+	/* Test single page 32-bit repeated instruction */
+	addr32 = buf + PAGE_SIZE;
+	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
+
+	check(addr32[0] == 0);
+	check(addr32[1] == inst32);
+	check(addr32[2] == inst32);
+	check(addr32[3] == inst32);
+	check(addr32[4] == 0);
+
+	/* Test single page 64-bit repeated instruction */
+	if (IS_ENABLED(CONFIG_PPC64)) {
+		check(ppc_inst_prefixed(inst64));
+
+		addr64 = buf + PAGE_SIZE * 2;
+		ppc_inst_write(code, inst64);
+		check(!patch_instructions((u32 *)(addr64 + 1), code, 24, true));
+
+		check(addr64[0] == 0);
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[1]), inst64));
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[2]), inst64));
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[3]), inst64));
+		check(addr64[4] == 0);
+	}
+
+	/* Test single page memcpy */
+	addr32 = buf + PAGE_SIZE * 3;
+
+	for (int i = 0; i < ARRAY_SIZE(code); i++)
+		code[i] = i + 1;
+
+	check(!patch_instructions(addr32 + 1, code, sizeof(code), false));
+
+	check(addr32[0] == 0);
+	check(!memcmp(&addr32[1], code, sizeof(code)));
+	check(addr32[ARRAY_SIZE(code) + 1] == 0);
+
+	/* Test multipage 32-bit repeated instruction */
+	addr32 = buf + PAGE_SIZE * 4 - 8;
+	check(!patch_instructions(addr32 + 1, &inst32, 12, true));
+
+	check(addr32[0] == 0);
+	check(addr32[1] == inst32);
+	check(addr32[2] == inst32);
+	check(addr32[3] == inst32);
+	check(addr32[4] == 0);
+
+	/* Test multipage 64-bit repeated instruction */
+	if (IS_ENABLED(CONFIG_PPC64)) {
+		check(ppc_inst_prefixed(inst64));
+
+		addr64 = buf + PAGE_SIZE * 5 - 8;
+		ppc_inst_write(code, inst64);
+		check(!patch_instructions((u32 *)(addr64 + 1), code, 24, true));
+
+		check(addr64[0] == 0);
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[1]), inst64));
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[2]), inst64));
+		check(ppc_inst_equal(ppc_inst_read((u32 *)&addr64[3]), inst64));
+		check(addr64[4] == 0);
+	}
+
+	/* Test multipage memcpy */
+	addr32 = buf + PAGE_SIZE * 6 - 12;
+
+	for (int i = 0; i < ARRAY_SIZE(code); i++)
+		code[i] = i + 1;
+
+	check(!patch_instructions(addr32 + 1, code, sizeof(code), false));
+
+	check(addr32[0] == 0);
+	check(!memcmp(&addr32[1], code, sizeof(code)));
+	check(addr32[ARRAY_SIZE(code) + 1] == 0);
+
+	vfree(buf);
+}
+
 static int __init test_code_patching(void)
 {
 	pr_info("Running code patching self-tests ...\n");
@@ -356,6 +447,7 @@  static int __init test_code_patching(void)
 	test_create_function_call();
 	test_translate_branch();
 	test_prefixed_patching();
+	test_multi_instruction_patching();
 
 	return 0;
 }