[16/19] EDAC, ghes: Fix grain calculation

Message ID 20191010202418.25098-17-rrichter@marvell.com
State Superseded
Headers show
Series
  • EDAC: Rework edac_mc and ghes drivers
Related show

Commit Message

Robert Richter Oct. 10, 2019, 8:25 p.m.
The current code to convert a physical address mask to a grain
(defined as granularity in bytes) is:

	e->grain = ~(mem_err->physical_addr_mask & ~PAGE_MASK);

This is broken in several ways:

1) It calculates to wrong grain values. E.g., a physical address mask
of ~0xfff should give a grain of 0x1000. Without considering
PAGE_MASK, there is an off-by-one. Things are worse when also
filtering it with ~PAGE_MASK. This will calculate to a grain with the
upper bits set. In the example it even calculates to ~0.

2) The grain does not depend on and is unrelated to the kernel's
page-size. The page-size only matters when unmapping memory in
memory_failure(). Smaller grains are wrongly rounded up to the
page-size, on architectures with a configurable page-size (e.g. arm64)
this could round up to the even bigger page-size of the hypervisor.

Fix this with:

	e->grain = ~mem_err->physical_addr_mask + 1;

The grain_bits are defined as:

	grain = 1 << grain_bits;

Change also the grain_bits calculation accordingly, it is the same
formula as in edac_mc.c now and the code can be unified.

The value in ->physical_addr_mask coming from firmware is assumed to
be contiguous, but this is not sanity-checked. However, in case the
mask is non-contiguous, a conversion to grain_bits effectively
converts the grain bit mask to a power of 2 by rounding up.

Suggested-by: James Morse <james.morse@arm.com>
Signed-off-by: Robert Richter <rrichter@marvell.com>

---
 drivers/edac/ghes_edac.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

-- 
2.20.1

Comments

Mauro Carvalho Chehab Oct. 11, 2019, 11:22 a.m. | #1
Em Thu, 10 Oct 2019 20:25:37 +0000
Robert Richter <rrichter@marvell.com> escreveu:

> The current code to convert a physical address mask to a grain

> (defined as granularity in bytes) is:

> 

> 	e->grain = ~(mem_err->physical_addr_mask & ~PAGE_MASK);

> 

> This is broken in several ways:

> 

> 1) It calculates to wrong grain values. E.g., a physical address mask

> of ~0xfff should give a grain of 0x1000. Without considering

> PAGE_MASK, there is an off-by-one. Things are worse when also

> filtering it with ~PAGE_MASK. This will calculate to a grain with the

> upper bits set. In the example it even calculates to ~0.

> 

> 2) The grain does not depend on and is unrelated to the kernel's

> page-size. The page-size only matters when unmapping memory in

> memory_failure(). Smaller grains are wrongly rounded up to the

> page-size, on architectures with a configurable page-size (e.g. arm64)

> this could round up to the even bigger page-size of the hypervisor.

> 

> Fix this with:

> 

> 	e->grain = ~mem_err->physical_addr_mask + 1;

> 

> The grain_bits are defined as:

> 

> 	grain = 1 << grain_bits;

> 

> Change also the grain_bits calculation accordingly, it is the same

> formula as in edac_mc.c now and the code can be unified.

> 

> The value in ->physical_addr_mask coming from firmware is assumed to

> be contiguous, but this is not sanity-checked. However, in case the

> mask is non-contiguous, a conversion to grain_bits effectively

> converts the grain bit mask to a power of 2 by rounding up.

> 

> Suggested-by: James Morse <james.morse@arm.com>

> Signed-off-by: Robert Richter <rrichter@marvell.com>


Reviewed-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>


> ---

>  drivers/edac/ghes_edac.c | 10 ++++++++--

>  1 file changed, 8 insertions(+), 2 deletions(-)

> 

> diff --git a/drivers/edac/ghes_edac.c b/drivers/edac/ghes_edac.c

> index 851aad92e42d..97242cf18a88 100644

> --- a/drivers/edac/ghes_edac.c

> +++ b/drivers/edac/ghes_edac.c

> @@ -220,6 +220,7 @@ void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)

>  	/* Cleans the error report buffer */

>  	memset(e, 0, sizeof (*e));

>  	e->error_count = 1;

> +	e->grain = 1;

>  	strcpy(e->label, "unknown label");

>  	e->msg = pvt->msg;

>  	e->other_detail = pvt->other_detail;

> @@ -315,7 +316,7 @@ void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)

>  

>  	/* Error grain */

>  	if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)

> -		e->grain = ~(mem_err->physical_addr_mask & ~PAGE_MASK);

> +		e->grain = ~mem_err->physical_addr_mask + 1;

>  

>  	/* Memory error location, mapped on e->location */

>  	p = e->location;

> @@ -428,8 +429,13 @@ void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)

>  	if (p > pvt->other_detail)

>  		*(p - 1) = '\0';

>  

> +	/* Sanity-check driver-supplied grain value. */

> +	if (WARN_ON_ONCE(!e->grain))

> +		e->grain = 1;

> +

> +	grain_bits = fls_long(e->grain - 1);

> +

>  	/* Generate the trace event */

> -	grain_bits = fls_long(e->grain);

>  	snprintf(pvt->detail_location, sizeof(pvt->detail_location),

>  		 "APEI location: %s %s", e->location, e->other_detail);

>  	trace_mc_event(e->type, e->msg, e->label, e->error_count,




Thanks,
Mauro

Patch

diff --git a/drivers/edac/ghes_edac.c b/drivers/edac/ghes_edac.c
index 851aad92e42d..97242cf18a88 100644
--- a/drivers/edac/ghes_edac.c
+++ b/drivers/edac/ghes_edac.c
@@ -220,6 +220,7 @@  void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
 	/* Cleans the error report buffer */
 	memset(e, 0, sizeof (*e));
 	e->error_count = 1;
+	e->grain = 1;
 	strcpy(e->label, "unknown label");
 	e->msg = pvt->msg;
 	e->other_detail = pvt->other_detail;
@@ -315,7 +316,7 @@  void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
 
 	/* Error grain */
 	if (mem_err->validation_bits & CPER_MEM_VALID_PA_MASK)
-		e->grain = ~(mem_err->physical_addr_mask & ~PAGE_MASK);
+		e->grain = ~mem_err->physical_addr_mask + 1;
 
 	/* Memory error location, mapped on e->location */
 	p = e->location;
@@ -428,8 +429,13 @@  void ghes_edac_report_mem_error(int sev, struct cper_sec_mem_err *mem_err)
 	if (p > pvt->other_detail)
 		*(p - 1) = '\0';
 
+	/* Sanity-check driver-supplied grain value. */
+	if (WARN_ON_ONCE(!e->grain))
+		e->grain = 1;
+
+	grain_bits = fls_long(e->grain - 1);
+
 	/* Generate the trace event */
-	grain_bits = fls_long(e->grain);
 	snprintf(pvt->detail_location, sizeof(pvt->detail_location),
 		 "APEI location: %s %s", e->location, e->other_detail);
 	trace_mc_event(e->type, e->msg, e->label, e->error_count,