Skip to contentSkip to conversation

cxl/region: Use do_div() for 64-bit modulo operation

PATCH Applied Alison Schofield 2 participants 3 messages Jan 17 – Jan 19 CXL

lore
Share & tools
lore

Alison Schofield

author maintainer
#0 lore raw

PATCH 1/1 cxl/region: Use do_div() for 64-bit modulo operation

div64_u64_rem() was the wrong choice for doing a modulo operation and it was used incorrectly, causing a kernel oops by passing NULL as the remainder parameter. Replace it with the do_div() helper that does the intended math (gran_offset = offset % gran) and is architecture safe.

This bug appeared during testing of unaligned address translations. The visibility to userspace would be limited to folks doing poison injection or clear by HPA on unaligned regions.

Fixes 78b50b598462 ("cxl/region: Translate HPA to DPA and memdev in unaligned regions")
---
 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +2 -1
index d5979000fba1..96888d87a8df 100644
@@ -3324,6 +3324,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	u64 interleave_width, interleave_index;
 	u64 gran, gran_offset, dpa_offset;
 	u64 hpa = p->res->start + offset;
+	u64 tmp = offset;
 
 	/*
 	 * Unaligned addresses are not algebraically invertible. Calculate
@@ -3333,7 +3334,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	gran = cxld->interleave_granularity;
 	interleave_width = gran * cxld->interleave_ways;
 	interleave_index = div64_u64(offset, interleave_width);
-	gran_offset = div64_u64_rem(offset, gran, NULL);
+	gran_offset = do_div(tmp, gran);
 
 	dpa_offset = interleave_index * gran + gran_offset;
 

base-commit: 78b50b5984629d362f826e9615ce4599f429716e

Signature · 1 line
2.37.3
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Alison Schofield

div64_u64_rem() was the wrong choice for doing a modulo operation and it was used incorrectly, causing a kernel oops by passing NULL as the remainder parameter. Replace it with the do_div() helper that does the intended math (gran_offset = offset % gran) and is architecture safe.

Quoted 6 lines

This bug appeared during testing of unaligned address translations. The visibility to userspace would be limited to folks doing poison injection or clear by HPA on unaligned regions.

Fixes 78b50b598462 ("cxl/region: Translate HPA to DPA and memdev in unaligned regions")

Thanks Alison. I'll get this applied to next

Quoted 27 lines
---
 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +2 -1
index d5979000fba1..96888d87a8df 100644
@@ -3324,6 +3324,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	u64 interleave_width, interleave_index;
 	u64 gran, gran_offset, dpa_offset;
 	u64 hpa = p->res->start + offset;
+	u64 tmp = offset;
 
 	/*
 	 * Unaligned addresses are not algebraically invertible. Calculate
@@ -3333,7 +3334,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	gran = cxld->interleave_granularity;
 	interleave_width = gran * cxld->interleave_ways;
 	interleave_index = div64_u64(offset, interleave_width);
-	gran_offset = div64_u64_rem(offset, gran, NULL);
+	gran_offset = do_div(tmp, gran);
 
 	dpa_offset = interleave_index * gran + gran_offset;
 

base-commit: 78b50b5984629d362f826e9615ce4599f429716e

maintainer Applied
#2 lore raw
In reply to Alison Schofield
Quoted 6 lines · Alison Schofield

div64_u64_rem() was the wrong choice for doing a modulo operation and it was used incorrectly, causing a kernel oops by passing NULL as the remainder parameter. Replace it with the do_div() helper that does the intended math (gran_offset = offset % gran) and is architecture safe.

Quoted 6 lines

This bug appeared during testing of unaligned address translations. The visibility to userspace would be limited to folks doing poison injection or clear by HPA on unaligned regions.

Fixes 78b50b598462 ("cxl/region: Translate HPA to DPA and memdev in unaligned regions")

Applied to cxl/next
345f23df5d08fe58fb865cd17b696cd345455224

Quoted 27 lines
---
 drivers/cxl/core/region.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +2 -1
index d5979000fba1..96888d87a8df 100644
@@ -3324,6 +3324,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	u64 interleave_width, interleave_index;
 	u64 gran, gran_offset, dpa_offset;
 	u64 hpa = p->res->start + offset;
+	u64 tmp = offset;
 
 	/*
 	 * Unaligned addresses are not algebraically invertible. Calculate
@@ -3333,7 +3334,7 @@ static int unaligned_region_offset_to_dpa_result(struct cxl_region *cxlr,
 	gran = cxld->interleave_granularity;
 	interleave_width = gran * cxld->interleave_ways;
 	interleave_index = div64_u64(offset, interleave_width);
-	gran_offset = div64_u64_rem(offset, gran, NULL);
+	gran_offset = do_div(tmp, gran);
 
 	dpa_offset = interleave_index * gran + gran_offset;
 

base-commit: 78b50b5984629d362f826e9615ce4599f429716e