Skip to contentSkip to conversation

missing clear bdr in check_ram_in_range_map()?

PATCH Baochen Qiang 3 participants 5 messages Dec 9, 2024 – Mar 4, 2025 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

Hi,

while checking check_ram_in_range_map() I am confused by the condition set/check on bdr. If I am reading the code correctly, if bdr is set once, it would never get cleared, hence that function will always returns 0.

should we clear bdr before each new iteration?

kernel/dma/direct.c +1 -0
index 5b4e6d3bf7bc..1605b956b25e 100644
@@ -611,6 +611,7 @@ static int check_ram_in_range_map(unsigned long start_pfn,
                        return 1;
 
                start_pfn = PFN_DOWN(bdr->cpu_start) + PFN_DOWN(bdr->size);
+               bdr = NULL;
        }
 
        return 0;
#1 lore raw
Quoted 7 lines · Baochen Qiang

Hi,

while checking check_ram_in_range_map() I am confused by the condition set/check on bdr. If I am reading the code correctly, if bdr is set once, it would never get cleared, hence that function will always returns 0.

should we clear bdr before each new iteration?

I think so. Even better refactor the code so that the non-NULL bdr doesn't leak out:

kernel/dma/direct.c +18 -11
index 5b4e6d3bf7bc..181e244f410a 100644
@@ -584,6 +584,22 @@ int dma_direct_supported(struct device *dev, u64 mask)
 	return mask >= phys_to_dma_unencrypted(dev, min_mask);
 }
 
+static const struct bus_dma_region *dma_find_range(struct device *dev,
+		unsigned long start_pfn)
+{
+	const struct bus_dma_region *m;
+
+	for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
+		unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+
+		if (start_pfn >= cpu_start_pfn &&
+		    start_pfn - cpu_start_pfn < PFN_DOWN(m->size))
+			return m;
+	}
+
+	return NULL;
+}
+
 /*
  * To check whether all ram resource ranges are covered by dma range map
  * Returns 0 when further check is needed
@@ -593,23 +609,14 @@ static int check_ram_in_range_map(unsigned long start_pfn,
 				  unsigned long nr_pages, void *data)
 {
 	unsigned long end_pfn = start_pfn + nr_pages;
-	const struct bus_dma_region *bdr = NULL;
-	const struct bus_dma_region *m;
 	struct device *dev = data;
 
 	while (start_pfn < end_pfn) {
-		for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
-			unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+		const struct bus_dma_region *bdr;
 
-			if (start_pfn >= cpu_start_pfn &&
-			    start_pfn - cpu_start_pfn < PFN_DOWN(m->size)) {
-				bdr = m;
-				break;
-			}
-		}
+		bdr = dma_find_range(dev, start_pfn);
 		if (!bdr)
 			return 1;
-
 		start_pfn = PFN_DOWN(bdr->cpu_start) + PFN_DOWN(bdr->size);
 	}
 
#2 lore raw
Quoted 55 lines · Christoph Hellwig
Quoted 7 lines · Baochen Qiang

Hi,

while checking check_ram_in_range_map() I am confused by the condition set/check on bdr. If I am reading the code correctly, if bdr is set once, it would never get cleared, hence that function will always returns 0.

should we clear bdr before each new iteration?

I think so. Even better refactor the code so that the non-NULL bdr doesn't leak out:

kernel/dma/direct.c +17 -7
index 5b4e6d3bf7bc..181e244f410a 100644
@@ -584,6 +584,22 @@ int dma_direct_supported(struct device *dev, u64 mask)
 	return mask >= phys_to_dma_unencrypted(dev, min_mask);
 }
 
+static const struct bus_dma_region *dma_find_range(struct device *dev,
+		unsigned long start_pfn)
+{
+	const struct bus_dma_region *m;
+
+	for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
+		unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+
+		if (start_pfn >= cpu_start_pfn &&
+		    start_pfn - cpu_start_pfn < PFN_DOWN(m->size))
+			return m;
+	}
+
+	return NULL;
+}
+
 /*
  * To check whether all ram resource ranges are covered by dma range map
  * Returns 0 when further check is needed
@@ -593,23 +609,14 @@ static int check_ram_in_range_map(unsigned long start_pfn,
 				  unsigned long nr_pages, void *data)
 {
 	unsigned long end_pfn = start_pfn + nr_pages;
-	const struct bus_dma_region *bdr = NULL;
-	const struct bus_dma_region *m;
 	struct device *dev = data;
 
 	while (start_pfn < end_pfn) {
-		for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
-			unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+		const struct bus_dma_region *bdr;
 
-			if (start_pfn >= cpu_start_pfn &&
-			    start_pfn - cpu_start_pfn < PFN_DOWN(m->size)) {
-				bdr = m;

kernel/dma/direct.c static int check_ram_in_range_map(unsigned long start_pfn, · line 622

Quoted 10 lines
kernel/dma/direct.c +1 -4
-				break;
-			}
-		}
+		bdr = dma_find_range(dev, start_pfn);
 		if (!bdr)
 			return 1;
-
 		start_pfn = PFN_DOWN(bdr->cpu_start) + PFN_DOWN(bdr->size);
 	}
 

looks better. thanks for reply.

maintainer
#3 lore raw

Hi,

Quoted 52 lines · Baochen Qiang
Quoted 51 lines · Christoph Hellwig
Quoted 7 lines · Baochen Qiang

Hi,

while checking check_ram_in_range_map() I am confused by the condition set/check on bdr. If I am reading the code correctly, if bdr is set once, it would never get cleared, hence that function will always returns 0.

should we clear bdr before each new iteration?

I think so. Even better refactor the code so that the non-NULL bdr doesn't leak out:

kernel/dma/direct.c +17 -4
index 5b4e6d3bf7bc..181e244f410a 100644
@@ -584,6 +584,22 @@ int dma_direct_supported(struct device *dev, u64 mask)
  	return mask >= phys_to_dma_unencrypted(dev, min_mask);
  }
 
+static const struct bus_dma_region *dma_find_range(struct device *dev,
+		unsigned long start_pfn)
+{
+	const struct bus_dma_region *m;
+
+	for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
+		unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+
+		if (start_pfn >= cpu_start_pfn &&
+		    start_pfn - cpu_start_pfn < PFN_DOWN(m->size))
+			return m;
+	}
+
+	return NULL;
+}
+
  /*
   * To check whether all ram resource ranges are covered by dma range map
   * Returns 0 when further check is needed
@@ -593,23 +609,14 @@ static int check_ram_in_range_map(unsigned long start_pfn,
  				  unsigned long nr_pages, void *data)
  {
  	unsigned long end_pfn = start_pfn + nr_pages;
-	const struct bus_dma_region *bdr = NULL;
-	const struct bus_dma_region *m;
  	struct device *dev = data;
 
  	while (start_pfn < end_pfn) {
-		for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
-			unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+		const struct bus_dma_region *bdr;
 
Quoted 14 lines

kernel/dma/direct.c static int check_ram_in_range_map(unsigned long start_pfn, · line 622

Quoted 13 lines
kernel/dma/direct.c +1 -7
-			if (start_pfn >= cpu_start_pfn &&
-			    start_pfn - cpu_start_pfn < PFN_DOWN(m->size)) {
-				bdr = m;
-				break;
-			}
-		}
+		bdr = dma_find_range(dev, start_pfn);
  		if (!bdr)
  			return 1;
-
  		start_pfn = PFN_DOWN(bdr->cpu_start) + PFN_DOWN(bdr->size);
  	}
 

looks better. thanks for reply.

Could you send a formal patch with this fix, description and 'Suggested-by: Christoph Hellwig' tag?

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
#4 lore raw
Quoted 70 lines · Marek Szyprowski

Hi,

Quoted 52 lines · Baochen Qiang
Quoted 51 lines · Christoph Hellwig
Quoted 7 lines · Baochen Qiang

Hi,

while checking check_ram_in_range_map() I am confused by the condition set/check on bdr. If I am reading the code correctly, if bdr is set once, it would never get cleared, hence that function will always returns 0.

should we clear bdr before each new iteration?

I think so. Even better refactor the code so that the non-NULL bdr doesn't leak out:

kernel/dma/direct.c +17 -4
index 5b4e6d3bf7bc..181e244f410a 100644
@@ -584,6 +584,22 @@ int dma_direct_supported(struct device *dev, u64 mask)
  	return mask >= phys_to_dma_unencrypted(dev, min_mask);
  }
 
+static const struct bus_dma_region *dma_find_range(struct device *dev,
+		unsigned long start_pfn)
+{
+	const struct bus_dma_region *m;
+
+	for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
+		unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+
+		if (start_pfn >= cpu_start_pfn &&
+		    start_pfn - cpu_start_pfn < PFN_DOWN(m->size))
+			return m;
+	}
+
+	return NULL;
+}
+
  /*
   * To check whether all ram resource ranges are covered by dma range map
   * Returns 0 when further check is needed
@@ -593,23 +609,14 @@ static int check_ram_in_range_map(unsigned long start_pfn,
  				  unsigned long nr_pages, void *data)
  {
  	unsigned long end_pfn = start_pfn + nr_pages;
-	const struct bus_dma_region *bdr = NULL;
-	const struct bus_dma_region *m;
  	struct device *dev = data;
 
  	while (start_pfn < end_pfn) {
-		for (m = dev->dma_range_map; PFN_DOWN(m->size); m++) {
-			unsigned long cpu_start_pfn = PFN_DOWN(m->cpu_start);
+		const struct bus_dma_region *bdr;
 
Quoted 14 lines

kernel/dma/direct.c static int check_ram_in_range_map(unsigned long start_pfn, · line 622

Quoted 13 lines
kernel/dma/direct.c +1 -7
-			if (start_pfn >= cpu_start_pfn &&
-			    start_pfn - cpu_start_pfn < PFN_DOWN(m->size)) {
-				bdr = m;
-				break;
-			}
-		}
+		bdr = dma_find_range(dev, start_pfn);
  		if (!bdr)
  			return 1;
-
  		start_pfn = PFN_DOWN(bdr->cpu_start) + PFN_DOWN(bdr->size);
  	}
 

looks better. thanks for reply.

Quoted 2 lines

Could you send a formal patch with this fix, description and 'Suggested-by: Christoph Hellwig' tag?

sure, will submit soon.

Quoted 2 lines

Best regards