Skip to contentSkip to conversation

vdpa/mlx5: Iova mapping related fixes

PATCH In review Dragos Tatulea 3 participants 13 messages Oct 21, 2024 – Nov 13, 2024

Subsystems (4)
lore
Share & tools
lore

Dragos Tatulea

author maintainer
#0 lore raw

cover vdpa/mlx5: Iova mapping related fixes

Here are 2 fixes from Si-Wei:
- The first one is an important fix that has to be applied as far back as possible (hence CC'ing linux-stable).
- The second is more of an improvement. That's why it doesn't have the Fixes tag.

I'd like to thank Si-Wei for the effort of finding and fixing these issues. Especially the first issue which was very well hidden and was there since day 1.

Si-Wei Liu (2):
vdpa/mlx5: Fix PA offset with unaligned starting iotlb map vdpa/mlx5: Fix suboptimal range on iotlb iteration

 drivers/vdpa/mlx5/core/mr.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.46.1
author maintainer
#1 lore raw

PATCH 1/2 vdpa/mlx5: Fix PA offset with unaligned starting iotlb map

From: Si-Wei Liu <[email protected]>

When calculating the physical address range based on the iotlb and mr [start,end) ranges, the offset of mr->start relative to map->start is not taken into account. This leads to some incorrect and duplicate mappings.

For the case when mr->start < map->start the code is already correct:
the range in [mr->start, map->start) was handled by a different iteration.

Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")
---
 drivers/vdpa/mlx5/core/mr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/vdpa/mlx5/core/mr.c +5 -3
index 2dd21e0b399e..7d0c83b5b071 100644
@@ -373,7 +373,7 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	struct page *pg;
 	unsigned int nsg;
 	int sglen;
-	u64 pa;
+	u64 pa, offset;
 	u64 paend;
 	struct scatterlist *sg;
 	struct device *dma = mvdev->vdev.dma_dev;
@@ -396,8 +396,10 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	sg = mr->sg_head.sgl;
 	for (map = vhost_iotlb_itree_first(iotlb, mr->start, mr->end - 1);
 	     map; map = vhost_iotlb_itree_next(map, mr->start, mr->end - 1)) {
-		paend = map->addr + maplen(map, mr);
-		for (pa = map->addr; pa < paend; pa += sglen) {
+		offset = mr->start > map->start ? mr->start - map->start : 0;
+		pa = map->addr + offset;
+		paend = map->addr + offset + maplen(map, mr);
+		for (; pa < paend; pa += sglen) {
 			pg = pfn_to_page(__phys_to_pfn(pa));
 			if (!sg) {
 				mlx5_vdpa_warn(mvdev, "sg null. start 0x%llx, end 0x%llx\n",
Signature · 1 line
2.46.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH vhost 1/2] vdpa/mlx5: Fix PA offset with unaligned starting iotlb map

Quoted 12 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

When calculating the physical address range based on the iotlb and mr [start,end) ranges, the offset of mr->start relative to map->start is not taken into account. This leads to some incorrect and duplicate mappings.

For the case when mr->start < map->start the code is already correct:
the range in [mr->start, map->start) was handled by a different iteration.

Quoted 5 lines
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

---

Thanks

author maintainer
#3 lore raw

PATCH 2/2 vdpa/mlx5: Fix suboptimal range on iotlb iteration

In reply to Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")
---
 drivers/vdpa/mlx5/core/mr.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/vdpa/mlx5/core/mr.c +1 -3
index 7d0c83b5b071..8455f08f5d40 100644
@@ -368,7 +368,6 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	unsigned long lgcd = 0;
 	int log_entity_size;
 	unsigned long size;
-	u64 start = 0;
 	int err;
 	struct page *pg;
 	unsigned int nsg;
@@ -379,10 +378,9 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	struct device *dma = mvdev->vdev.dma_dev;
 
 	for (map = vhost_iotlb_itree_first(iotlb, mr->start, mr->end - 1);
-	     map; map = vhost_iotlb_itree_next(map, start, mr->end - 1)) {
+	     map; map = vhost_iotlb_itree_next(map, mr->start, mr->end - 1)) {
 		size = maplen(map, mr);
 		lgcd = gcd(lgcd, size);
-		start += size;
 	}
 	log_entity_size = ilog2(lgcd);
 
Signature · 1 line
2.46.1
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 9 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Quoted 4 lines
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

---

Thanks

#5 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

In reply to Dragos Tatulea
Quoted 9 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

But the cover letter says "that's why it does not have a fixes tag". Confused.

Quoted 32 lines
---
 drivers/vdpa/mlx5/core/mr.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/vdpa/mlx5/core/mr.c +1 -3
index 7d0c83b5b071..8455f08f5d40 100644
@@ -368,7 +368,6 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	unsigned long lgcd = 0;
 	int log_entity_size;
 	unsigned long size;
-	u64 start = 0;
 	int err;
 	struct page *pg;
 	unsigned int nsg;
@@ -379,10 +378,9 @@ static int map_direct_mr(struct mlx5_vdpa_dev *mvdev, struct mlx5_vdpa_direct_mr
 	struct device *dma = mvdev->vdev.dma_dev;
 
 	for (map = vhost_iotlb_itree_first(iotlb, mr->start, mr->end - 1);
-	     map; map = vhost_iotlb_itree_next(map, start, mr->end - 1)) {
+	     map; map = vhost_iotlb_itree_next(map, mr->start, mr->end - 1)) {
 		size = maplen(map, mr);
 		lgcd = gcd(lgcd, size);
-		start += size;
 	}
 	log_entity_size = ilog2(lgcd);
 
Signature · 1 line
2.46.1
author maintainer
#6 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Quoted 5 lines
Quoted 1 line
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

But the cover letter says "that's why it does not have a fixes tag". Confused.

Sorry about that. Patch is fine with fixes tag, I forgot to drop that part of the sentence from the cover letter.

Let me know if I need to resend something.

Thanks,
Dragos

#7 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 17 lines · Dragos Tatulea
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Quoted 5 lines
Quoted 1 line
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

But the cover letter says "that's why it does not have a fixes tag". Confused.

Quoted 7 lines

Sorry about that. Patch is fine with fixes tag, I forgot to drop that part of the sentence from the cover letter.

Let me know if I need to resend something.

Thanks,
Dragos

But why does it need the fixes tag? That one means "if you have that hash, you need this patch". Pls do not abuse it for optimizations.

author maintainer
#8 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Dragos Tatulea
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Quoted 5 lines
Quoted 1 line
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

But the cover letter says "that's why it does not have a fixes tag". Confused.

Sorry about that. Patch is fine with fixes tag, I forgot to drop that part of the sentence from the cover letter.

Quoted 9 lines
Quoted 4 lines

Let me know if I need to resend something.

Thanks,
Dragos

But why does it need the fixes tag? That one means "if you have that hash, you need this patch". Pls do not abuse it for optimizations.

Well, it is a fix but it happens that the code around still works without this fix. I figured that it would be better to take it into older stable kernels just like the other one. But if you consider it an improvement I will send a v2 without the Fixes tag.

Thanks,
Dragos

#9 lore raw

Re: [PATCH vhost 2/2] vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 33 lines · Dragos Tatulea
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Dragos Tatulea
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Dragos Tatulea

From: Si-Wei Liu <[email protected]>

The starting iova address to iterate iotlb map entry within a range was set to an irrelevant value when passing to the itree_next() iterator, although luckily it doesn't affect the outcome of finding out the granule of the smallest iotlb map size. Fix the code to make it consistent with the following for-loop.

Quoted 5 lines
Quoted 1 line
Fixes 94abbccdf291 ("vdpa/mlx5: Add shared memory registration code")

But the cover letter says "that's why it does not have a fixes tag". Confused.

Sorry about that. Patch is fine with fixes tag, I forgot to drop that part of the sentence from the cover letter.

Quoted 9 lines
Quoted 4 lines

Let me know if I need to resend something.

Thanks,
Dragos

But why does it need the fixes tag? That one means "if you have that hash, you need this patch". Pls do not abuse it for optimizations.

Quoted 7 lines

Well, it is a fix but it happens that the code around still works without this fix. I figured that it would be better to take it into older stable kernels just like the other one. But if you consider it an improvement I will send a v2 without the Fixes tag.

Thanks,
Dragos

No need.

author maintainer
#10 lore raw
In reply to Dragos Tatulea
Quoted 10 lines · Dragos Tatulea

Here are 2 fixes from Si-Wei:
- The first one is an important fix that has to be applied as far back as possible (hence CC'ing linux-stable).
- The second is more of an improvement. That's why it doesn't have the Fixes tag.

I'd like to thank Si-Wei for the effort of finding and fixing these issues. Especially the first issue which was very well hidden and was there since day 1.

Quoted 7 lines

Si-Wei Liu (2):
vdpa/mlx5: Fix PA offset with unaligned starting iotlb map vdpa/mlx5: Fix suboptimal range on iotlb iteration

 drivers/vdpa/mlx5/core/mr.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Gentle nudge for a review. The bug fixed by the first patch is a very serious and insidious one.

Thanks,
Dragos

#11 lore raw
Quoted 18 lines · Dragos Tatulea
Quoted 14 lines · Dragos Tatulea

Here are 2 fixes from Si-Wei:
- The first one is an important fix that has to be applied as far back as possible (hence CC'ing linux-stable).
- The second is more of an improvement. That's why it doesn't have the Fixes tag.

I'd like to thank Si-Wei for the effort of finding and fixing these issues. Especially the first issue which was very well hidden and was there since day 1.

Si-Wei Liu (2):
vdpa/mlx5: Fix PA offset with unaligned starting iotlb map vdpa/mlx5: Fix suboptimal range on iotlb iteration

Quoted 5 lines
Quoted 3 lines
 drivers/vdpa/mlx5/core/mr.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Gentle nudge for a review. The bug fixed by the first patch is a very serious and insidious one.

I think I've acked to those patches, have you received that?

Thanks

Quoted 5 lines

Thanks,
Dragos

#12 lore raw
Quoted 22 lines · Jason Wang
Quoted 21 lines · Dragos Tatulea
Quoted 17 lines · Dragos Tatulea

Here are 2 fixes from Si-Wei:
- The first one is an important fix that has to be applied as far back as possible (hence CC'ing linux-stable).
- The second is more of an improvement. That's why it doesn't have the Fixes tag.

I'd like to thank Si-Wei for the effort of finding and fixing these issues. Especially the first issue which was very well hidden and was there since day 1.

Si-Wei Liu (2):
vdpa/mlx5: Fix PA offset with unaligned starting iotlb map vdpa/mlx5: Fix suboptimal range on iotlb iteration

 drivers/vdpa/mlx5/core/mr.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 6 lines
Quoted 2 lines

Gentle nudge for a review. The bug fixed by the first patch is a very serious and insidious one.

I think I've acked to those patches, have you received that?

Thanks

I saw your acks Jason, thanks!
Patch 1 is now upstream. Patch 2 is queued but I asked a question about it.

Quoted 5 lines
Quoted 5 lines

Thanks,
Dragos