[4/7] riscv: Simplify text patching loops

Message ID 20240212025529.1971876-5-samuel.holland@sifive.com
State New
Headers
Series riscv: Various text patching improvements |

Commit Message

Samuel Holland Feb. 12, 2024, 2:55 a.m. UTC
  This reduces the number of variables and makes the code easier to parse.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---

 arch/riscv/kernel/patch.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
  

Comments

Björn Töpel Feb. 19, 2024, 1:03 p.m. UTC | #1
Samuel Holland <samuel.holland@sifive.com> writes:

> This reduces the number of variables and makes the code easier to parse.
>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> ---
>
>  arch/riscv/kernel/patch.c | 21 +++++++++++----------
>  1 file changed, 11 insertions(+), 10 deletions(-)
>
> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
> index bccd9ed04a05..7f030b46eae5 100644
> --- a/arch/riscv/kernel/patch.c
> +++ b/arch/riscv/kernel/patch.c
> @@ -155,7 +155,6 @@ NOKPROBE_SYMBOL(__patch_insn_write);
>  
>  static int patch_insn_set(void *addr, u8 c, size_t len)
>  {
> -	size_t patched = 0;
>  	size_t size;
>  	int ret = 0;
>  
> @@ -163,11 +162,12 @@ static int patch_insn_set(void *addr, u8 c, size_t len)
>  	 * __patch_insn_set() can only work on 2 pages at a time so call it in a
>  	 * loop with len <= 2 * PAGE_SIZE.
>  	 */
> -	while (patched < len && !ret) {
> -		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
> -		ret = __patch_insn_set(addr + patched, c, size);
> +	while (len && !ret) {
> +		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr), len);
> +		ret = __patch_insn_set(addr, c, size);

While you're at it, do:
  ret = __patch_insn_set(addr, c, size);
  if (ret)
          return ret;
  ...
return 0;
}

and simplify the while-loop predicate?

>  
> -		patched += size;
> +		addr += size;
> +		len -= size;
>  	}
>  
>  	return ret;
> @@ -190,7 +190,6 @@ NOKPROBE_SYMBOL(patch_text_set_nosync);
>  
>  int patch_insn_write(void *addr, const void *insn, size_t len)
>  {
> -	size_t patched = 0;
>  	size_t size;
>  	int ret = 0;
>  
> @@ -198,11 +197,13 @@ int patch_insn_write(void *addr, const void *insn, size_t len)
>  	 * Copy the instructions to the destination address, two pages at a time
>  	 * because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE.
>  	 */
> -	while (patched < len && !ret) {
> -		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
> -		ret = __patch_insn_write(addr + patched, insn + patched, size);
> +	while (len && !ret) {
> +		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr), len);
> +		ret = __patch_insn_write(addr, insn, size);

Same comment as above.


Björn
  
David Laight Feb. 19, 2024, 10:13 p.m. UTC | #2
...
> > -	while (patched < len && !ret) {
> > -		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
> > -		ret = __patch_insn_set(addr + patched, c, size);
> > +	while (len && !ret) {
> > +		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr), len);

Does that need to be min_t()?
Both arguments seem to be unsigned.
(Did it even ever need to be?)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  

Patch

diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
index bccd9ed04a05..7f030b46eae5 100644
--- a/arch/riscv/kernel/patch.c
+++ b/arch/riscv/kernel/patch.c
@@ -155,7 +155,6 @@  NOKPROBE_SYMBOL(__patch_insn_write);
 
 static int patch_insn_set(void *addr, u8 c, size_t len)
 {
-	size_t patched = 0;
 	size_t size;
 	int ret = 0;
 
@@ -163,11 +162,12 @@  static int patch_insn_set(void *addr, u8 c, size_t len)
 	 * __patch_insn_set() can only work on 2 pages at a time so call it in a
 	 * loop with len <= 2 * PAGE_SIZE.
 	 */
-	while (patched < len && !ret) {
-		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
-		ret = __patch_insn_set(addr + patched, c, size);
+	while (len && !ret) {
+		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr), len);
+		ret = __patch_insn_set(addr, c, size);
 
-		patched += size;
+		addr += size;
+		len -= size;
 	}
 
 	return ret;
@@ -190,7 +190,6 @@  NOKPROBE_SYMBOL(patch_text_set_nosync);
 
 int patch_insn_write(void *addr, const void *insn, size_t len)
 {
-	size_t patched = 0;
 	size_t size;
 	int ret = 0;
 
@@ -198,11 +197,13 @@  int patch_insn_write(void *addr, const void *insn, size_t len)
 	 * Copy the instructions to the destination address, two pages at a time
 	 * because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE.
 	 */
-	while (patched < len && !ret) {
-		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr + patched), len - patched);
-		ret = __patch_insn_write(addr + patched, insn + patched, size);
+	while (len && !ret) {
+		size = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(addr), len);
+		ret = __patch_insn_write(addr, insn, size);
 
-		patched += size;
+		addr += size;
+		insn += size;
+		len -= size;
 	}
 
 	return ret;