[PATCH v3 1/3] x86/ioremap: introduce helper to implement xxx_is_setup_data()

Baoquan He posted 3 patches 1 year, 2 months ago
[PATCH v3 1/3] x86/ioremap: introduce helper to implement xxx_is_setup_data()
Posted by Baoquan He 1 year, 2 months ago
Functions memremap_is_setup_data() and early_memremap_is_setup_data()
share completely the same process and handling, except of the
different memremap/unmap invocations.

So add helper __memremap_is_setup_data() to extract the common part,
parameter 'early' is used to decide what kind of memremap/unmap
APIs are called. This simplifies codes a lot by removing the duplicated
codes, and also removes the similar code comment above them.

And '__ref' is added to __memremap_is_setup_data() to suppress below
section mismatch warning:

ARNING: modpost: vmlinux: section mismatch in reference: __memremap_is_setup_data+0x5f (section: .text) ->
early_memunmap (section: .init.text)

Signed-off-by: Baoquan He <bhe@redhat.com>
---
 arch/x86/mm/ioremap.c | 104 ++++++++++++++----------------------------
 1 file changed, 35 insertions(+), 69 deletions(-)

diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 8d29163568a7..aaf40a712b04 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -632,8 +632,8 @@ static bool memremap_is_efi_data(resource_size_t phys_addr,
  * Examine the physical address to determine if it is boot data by checking
  * it against the boot params setup_data chain.
  */
-static bool memremap_is_setup_data(resource_size_t phys_addr,
-				   unsigned long size)
+static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
+						bool early)
 {
 	struct setup_indirect *indirect;
 	struct setup_data *data;
@@ -641,31 +641,45 @@ static bool memremap_is_setup_data(resource_size_t phys_addr,
 
 	paddr = boot_params.hdr.setup_data;
 	while (paddr) {
-		unsigned int len;
+		unsigned int len, size;
 
 		if (phys_addr == paddr)
 			return true;
 
-		data = memremap(paddr, sizeof(*data),
-				MEMREMAP_WB | MEMREMAP_DEC);
+		if (early)
+			data = early_memremap_decrypted(paddr, sizeof(*data));
+		else
+			data = memremap(paddr, sizeof(*data),
+					MEMREMAP_WB | MEMREMAP_DEC);
 		if (!data) {
 			pr_warn("failed to memremap setup_data entry\n");
 			return false;
 		}
 
+		size = sizeof(*data);
+
 		paddr_next = data->next;
 		len = data->len;
 
 		if ((phys_addr > paddr) &&
-		    (phys_addr < (paddr + sizeof(struct setup_data) + len))) {
-			memunmap(data);
+		    (phys_addr < (paddr + sizeof(*data) + len))) {
+			if (early)
+				early_memunmap(data, sizeof(*data));
+			else
+				memunmap(data);
 			return true;
 		}
 
 		if (data->type == SETUP_INDIRECT) {
-			memunmap(data);
-			data = memremap(paddr, sizeof(*data) + len,
-					MEMREMAP_WB | MEMREMAP_DEC);
+			size += len;
+			if (early) {
+				early_memunmap(data, sizeof(*data));
+				data = early_memremap_decrypted(paddr, size);
+			} else {
+				memunmap(data);
+				data = memremap(paddr, size,
+						MEMREMAP_WB | MEMREMAP_DEC);
+			}
 			if (!data) {
 				pr_warn("failed to memremap indirect setup_data\n");
 				return false;
@@ -679,7 +693,10 @@ static bool memremap_is_setup_data(resource_size_t phys_addr,
 			}
 		}
 
-		memunmap(data);
+		if (early)
+			early_memunmap(data, size);
+		else
+			memunmap(data);
 
 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
 			return true;
@@ -690,67 +707,16 @@ static bool memremap_is_setup_data(resource_size_t phys_addr,
 	return false;
 }
 
-/*
- * Examine the physical address to determine if it is boot data by checking
- * it against the boot params setup_data chain (early boot version).
- */
+static bool memremap_is_setup_data(resource_size_t phys_addr,
+				   unsigned long size)
+{
+	return __memremap_is_setup_data(phys_addr, false);
+}
+
 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
 						unsigned long size)
 {
-	struct setup_indirect *indirect;
-	struct setup_data *data;
-	u64 paddr, paddr_next;
-
-	paddr = boot_params.hdr.setup_data;
-	while (paddr) {
-		unsigned int len, size;
-
-		if (phys_addr == paddr)
-			return true;
-
-		data = early_memremap_decrypted(paddr, sizeof(*data));
-		if (!data) {
-			pr_warn("failed to early memremap setup_data entry\n");
-			return false;
-		}
-
-		size = sizeof(*data);
-
-		paddr_next = data->next;
-		len = data->len;
-
-		if ((phys_addr > paddr) &&
-		    (phys_addr < (paddr + sizeof(struct setup_data) + len))) {
-			early_memunmap(data, sizeof(*data));
-			return true;
-		}
-
-		if (data->type == SETUP_INDIRECT) {
-			size += len;
-			early_memunmap(data, sizeof(*data));
-			data = early_memremap_decrypted(paddr, size);
-			if (!data) {
-				pr_warn("failed to early memremap indirect setup_data\n");
-				return false;
-			}
-
-			indirect = (struct setup_indirect *)data->data;
-
-			if (indirect->type != SETUP_INDIRECT) {
-				paddr = indirect->addr;
-				len = indirect->len;
-			}
-		}
-
-		early_memunmap(data, size);
-
-		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
-			return true;
-
-		paddr = paddr_next;
-	}
-
-	return false;
+	return __memremap_is_setup_data(phys_addr, true);
 }
 
 /*
-- 
2.41.0
Re: [PATCH v3 1/3] x86/ioremap: introduce helper to implement xxx_is_setup_data()
Posted by Borislav Petkov 1 year, 2 months ago
On Sat, Nov 23, 2024 at 07:42:19PM +0800, Baoquan He wrote:
> Functions memremap_is_setup_data() and early_memremap_is_setup_data()
> share completely the same process and handling, except of the
> different memremap/unmap invocations.
> 
> So add helper __memremap_is_setup_data() to extract the common part,
> parameter 'early' is used to decide what kind of memremap/unmap
> APIs are called. This simplifies codes a lot by removing the duplicated
> codes, and also removes the similar code comment above them.
> 
> And '__ref' is added to __memremap_is_setup_data() to suppress below
> section mismatch warning:
> 
> ARNING: modpost: vmlinux: section mismatch in reference: __memremap_is_setup_data+0x5f (section: .text) ->
> early_memunmap (section: .init.text)
> 
> Signed-off-by: Baoquan He <bhe@redhat.com>
> ---
>  arch/x86/mm/ioremap.c | 104 ++++++++++++++----------------------------
>  1 file changed, 35 insertions(+), 69 deletions(-)

Some touchups ontop:

diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index aaf40a712b04..fe44e8180bdd 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -632,9 +632,9 @@ static bool memremap_is_efi_data(resource_size_t phys_addr,
  * Examine the physical address to determine if it is boot data by checking
  * it against the boot params setup_data chain.
  */
-static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
-						bool early)
+static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool early)
 {
+	unsigned int setup_data_sz = sizeof(struct setup_data);
 	struct setup_indirect *indirect;
 	struct setup_data *data;
 	u64 paddr, paddr_next;
@@ -647,24 +647,23 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
 			return true;
 
 		if (early)
-			data = early_memremap_decrypted(paddr, sizeof(*data));
+			data = early_memremap_decrypted(paddr, setup_data_sz);
 		else
-			data = memremap(paddr, sizeof(*data),
-					MEMREMAP_WB | MEMREMAP_DEC);
+			data = memremap(paddr, setup_data_sz, MEMREMAP_WB | MEMREMAP_DEC);
 		if (!data) {
-			pr_warn("failed to memremap setup_data entry\n");
+			pr_warn("failed to remap setup_data entry\n");
 			return false;
 		}
 
-		size = sizeof(*data);
+		size = setup_data_sz;
 
 		paddr_next = data->next;
 		len = data->len;
 
 		if ((phys_addr > paddr) &&
-		    (phys_addr < (paddr + sizeof(*data) + len))) {
+		    (phys_addr < (paddr + setup_data_sz + len))) {
 			if (early)
-				early_memunmap(data, sizeof(*data));
+				early_memunmap(data, setup_data_sz);
 			else
 				memunmap(data);
 			return true;
@@ -673,15 +672,14 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
 		if (data->type == SETUP_INDIRECT) {
 			size += len;
 			if (early) {
-				early_memunmap(data, sizeof(*data));
+				early_memunmap(data, setup_data_sz);
 				data = early_memremap_decrypted(paddr, size);
 			} else {
 				memunmap(data);
-				data = memremap(paddr, size,
-						MEMREMAP_WB | MEMREMAP_DEC);
+				data = memremap(paddr, size, MEMREMAP_WB | MEMREMAP_DEC);
 			}
 			if (!data) {
-				pr_warn("failed to memremap indirect setup_data\n");
+				pr_warn("failed to remap indirect setup_data\n");
 				return false;
 			}
 

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v3 1/3] x86/ioremap: introduce helper to implement xxx_is_setup_data()
Posted by Baoquan He 1 year, 2 months ago
On 12/07/24 at 05:04pm, Borislav Petkov wrote:
> On Sat, Nov 23, 2024 at 07:42:19PM +0800, Baoquan He wrote:
> > Functions memremap_is_setup_data() and early_memremap_is_setup_data()
> > share completely the same process and handling, except of the
> > different memremap/unmap invocations.
> > 
> > So add helper __memremap_is_setup_data() to extract the common part,
> > parameter 'early' is used to decide what kind of memremap/unmap
> > APIs are called. This simplifies codes a lot by removing the duplicated
> > codes, and also removes the similar code comment above them.
> > 
> > And '__ref' is added to __memremap_is_setup_data() to suppress below
> > section mismatch warning:
> > 
> > ARNING: modpost: vmlinux: section mismatch in reference: __memremap_is_setup_data+0x5f (section: .text) ->
> > early_memunmap (section: .init.text)
> > 
> > Signed-off-by: Baoquan He <bhe@redhat.com>
> > ---
> >  arch/x86/mm/ioremap.c | 104 ++++++++++++++----------------------------
> >  1 file changed, 35 insertions(+), 69 deletions(-)
> 
> Some touchups ontop:
> 
> diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
> index aaf40a712b04..fe44e8180bdd 100644
> --- a/arch/x86/mm/ioremap.c
> +++ b/arch/x86/mm/ioremap.c

Thanks for optimizing these.

> @@ -632,9 +632,9 @@ static bool memremap_is_efi_data(resource_size_t phys_addr,
>   * Examine the physical address to determine if it is boot data by checking
>   * it against the boot params setup_data chain.
>   */
> -static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
> -						bool early)
> +static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool early)
>  {
> +	unsigned int setup_data_sz = sizeof(struct setup_data);
>  	struct setup_indirect *indirect;
>  	struct setup_data *data;
>  	u64 paddr, paddr_next;
> @@ -647,24 +647,23 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
>  			return true;
>  
>  		if (early)
> -			data = early_memremap_decrypted(paddr, sizeof(*data));
> +			data = early_memremap_decrypted(paddr, setup_data_sz);
>  		else
> -			data = memremap(paddr, sizeof(*data),
> -					MEMREMAP_WB | MEMREMAP_DEC);
> +			data = memremap(paddr, setup_data_sz, MEMREMAP_WB | MEMREMAP_DEC);
>  		if (!data) {
> -			pr_warn("failed to memremap setup_data entry\n");
> +			pr_warn("failed to remap setup_data entry\n");
>  			return false;
>  		}
>  
> -		size = sizeof(*data);
> +		size = setup_data_sz;
>  
>  		paddr_next = data->next;
>  		len = data->len;
>  
>  		if ((phys_addr > paddr) &&
> -		    (phys_addr < (paddr + sizeof(*data) + len))) {
> +		    (phys_addr < (paddr + setup_data_sz + len))) {
>  			if (early)
> -				early_memunmap(data, sizeof(*data));
> +				early_memunmap(data, setup_data_sz);
>  			else
>  				memunmap(data);
>  			return true;
> @@ -673,15 +672,14 @@ static bool __ref __memremap_is_setup_data(resource_size_t phys_addr,
>  		if (data->type == SETUP_INDIRECT) {
>  			size += len;
>  			if (early) {
> -				early_memunmap(data, sizeof(*data));
> +				early_memunmap(data, setup_data_sz);
>  				data = early_memremap_decrypted(paddr, size);
>  			} else {
>  				memunmap(data);
> -				data = memremap(paddr, size,
> -						MEMREMAP_WB | MEMREMAP_DEC);
> +				data = memremap(paddr, size, MEMREMAP_WB | MEMREMAP_DEC);
>  			}
>  			if (!data) {
> -				pr_warn("failed to memremap indirect setup_data\n");
> +				pr_warn("failed to remap indirect setup_data\n");
>  				return false;
>  			}
>  
> 
> -- 
> Regards/Gruss,
>     Boris.
> 
> https://people.kernel.org/tglx/notes-about-netiquette
>
[tip: x86/cleanups] x86/ioremap: Simplify setup_data mapping variants
Posted by tip-bot2 for Baoquan He 1 year, 2 months ago
The following commit has been merged into the x86/cleanups branch of tip:

Commit-ID:     095ac6fa19500fecd7c62e755dee45bb303d4d43
Gitweb:        https://git.kernel.org/tip/095ac6fa19500fecd7c62e755dee45bb303d4d43
Author:        Baoquan He <bhe@redhat.com>
AuthorDate:    Sat, 23 Nov 2024 19:42:19 +08:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
CommitterDate: Sat, 07 Dec 2024 17:23:15 +01:00

x86/ioremap: Simplify setup_data mapping variants

memremap_is_setup_data() and early_memremap_is_setup_data() share
completely the same process and handling, except for the differing
memremap/unmap invocations.

Add a helper __memremap_is_setup_data() extracting the common part and
simplify a lot of code while at it.

Mark __memremap_is_setup_data() as __ref to suppress this section
mismatch warning:

  WARNING: modpost: vmlinux: section mismatch in reference: __memremap_is_setup_data+0x5f (section: .text) ->
  early_memunmap (section: .init.text)

  [ bp: Massage a bit. ]

Signed-off-by: Baoquan He <bhe@redhat.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/r/20241123114221.149383-2-bhe@redhat.com
---
 arch/x86/mm/ioremap.c | 106 +++++++++++++----------------------------
 1 file changed, 35 insertions(+), 71 deletions(-)

diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
index 8d29163..fe44e81 100644
--- a/arch/x86/mm/ioremap.c
+++ b/arch/x86/mm/ioremap.c
@@ -632,42 +632,54 @@ static bool memremap_is_efi_data(resource_size_t phys_addr,
  * Examine the physical address to determine if it is boot data by checking
  * it against the boot params setup_data chain.
  */
-static bool memremap_is_setup_data(resource_size_t phys_addr,
-				   unsigned long size)
+static bool __ref __memremap_is_setup_data(resource_size_t phys_addr, bool early)
 {
+	unsigned int setup_data_sz = sizeof(struct setup_data);
 	struct setup_indirect *indirect;
 	struct setup_data *data;
 	u64 paddr, paddr_next;
 
 	paddr = boot_params.hdr.setup_data;
 	while (paddr) {
-		unsigned int len;
+		unsigned int len, size;
 
 		if (phys_addr == paddr)
 			return true;
 
-		data = memremap(paddr, sizeof(*data),
-				MEMREMAP_WB | MEMREMAP_DEC);
+		if (early)
+			data = early_memremap_decrypted(paddr, setup_data_sz);
+		else
+			data = memremap(paddr, setup_data_sz, MEMREMAP_WB | MEMREMAP_DEC);
 		if (!data) {
-			pr_warn("failed to memremap setup_data entry\n");
+			pr_warn("failed to remap setup_data entry\n");
 			return false;
 		}
 
+		size = setup_data_sz;
+
 		paddr_next = data->next;
 		len = data->len;
 
 		if ((phys_addr > paddr) &&
-		    (phys_addr < (paddr + sizeof(struct setup_data) + len))) {
-			memunmap(data);
+		    (phys_addr < (paddr + setup_data_sz + len))) {
+			if (early)
+				early_memunmap(data, setup_data_sz);
+			else
+				memunmap(data);
 			return true;
 		}
 
 		if (data->type == SETUP_INDIRECT) {
-			memunmap(data);
-			data = memremap(paddr, sizeof(*data) + len,
-					MEMREMAP_WB | MEMREMAP_DEC);
+			size += len;
+			if (early) {
+				early_memunmap(data, setup_data_sz);
+				data = early_memremap_decrypted(paddr, size);
+			} else {
+				memunmap(data);
+				data = memremap(paddr, size, MEMREMAP_WB | MEMREMAP_DEC);
+			}
 			if (!data) {
-				pr_warn("failed to memremap indirect setup_data\n");
+				pr_warn("failed to remap indirect setup_data\n");
 				return false;
 			}
 
@@ -679,7 +691,10 @@ static bool memremap_is_setup_data(resource_size_t phys_addr,
 			}
 		}
 
-		memunmap(data);
+		if (early)
+			early_memunmap(data, size);
+		else
+			memunmap(data);
 
 		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
 			return true;
@@ -690,67 +705,16 @@ static bool memremap_is_setup_data(resource_size_t phys_addr,
 	return false;
 }
 
-/*
- * Examine the physical address to determine if it is boot data by checking
- * it against the boot params setup_data chain (early boot version).
- */
+static bool memremap_is_setup_data(resource_size_t phys_addr,
+				   unsigned long size)
+{
+	return __memremap_is_setup_data(phys_addr, false);
+}
+
 static bool __init early_memremap_is_setup_data(resource_size_t phys_addr,
 						unsigned long size)
 {
-	struct setup_indirect *indirect;
-	struct setup_data *data;
-	u64 paddr, paddr_next;
-
-	paddr = boot_params.hdr.setup_data;
-	while (paddr) {
-		unsigned int len, size;
-
-		if (phys_addr == paddr)
-			return true;
-
-		data = early_memremap_decrypted(paddr, sizeof(*data));
-		if (!data) {
-			pr_warn("failed to early memremap setup_data entry\n");
-			return false;
-		}
-
-		size = sizeof(*data);
-
-		paddr_next = data->next;
-		len = data->len;
-
-		if ((phys_addr > paddr) &&
-		    (phys_addr < (paddr + sizeof(struct setup_data) + len))) {
-			early_memunmap(data, sizeof(*data));
-			return true;
-		}
-
-		if (data->type == SETUP_INDIRECT) {
-			size += len;
-			early_memunmap(data, sizeof(*data));
-			data = early_memremap_decrypted(paddr, size);
-			if (!data) {
-				pr_warn("failed to early memremap indirect setup_data\n");
-				return false;
-			}
-
-			indirect = (struct setup_indirect *)data->data;
-
-			if (indirect->type != SETUP_INDIRECT) {
-				paddr = indirect->addr;
-				len = indirect->len;
-			}
-		}
-
-		early_memunmap(data, size);
-
-		if ((phys_addr > paddr) && (phys_addr < (paddr + len)))
-			return true;
-
-		paddr = paddr_next;
-	}
-
-	return false;
+	return __memremap_is_setup_data(phys_addr, true);
 }
 
 /*