Skip to contentSkip to conversation

iio: Fix scan mask subset check logic

PATCH In review Fabio Estevam 2 participants 2 messages Apr 29, 2025 – Apr 30, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: Fix scan mask subset check logic

From: Fabio Estevam <[email protected]>

Since commit 2718f15403fb ("iio: sanity check available_scan_masks array"), verbose and misleading warnings are printed for devices like the MAX11601:

max1363 1-0064: available_scan_mask 8 subset of 0. Never used max1363 1-0064: available_scan_mask 9 subset of 0. Never used max1363 1-0064: available_scan_mask 10 subset of 0. Never used max1363 1-0064: available_scan_mask 11 subset of 0. Never used max1363 1-0064: available_scan_mask 12 subset of 0. Never used max1363 1-0064: available_scan_mask 13 subset of 0. Never used
...
[warnings continue]

Adding some debug prints show that the mask1 and mask2 indexes are incorrectly computed, leading to wrong array access.

These incorrect accesses causes the wrong subset output:

iio-core: ******** mask1[0] is 0x1
iio-core: ******** mask2[1] is 0x4
iio-core: ******** mask2[2] is 0x3
iio-core: ******** mask2[3] is 0xf
iio-core: ******** mask2[4] is 0x1000
iio-core: ******** mask2[5] is 0x40000
iio-core: ******** mask2[6] is 0x3000
iio-core: ******** mask2[7] is 0x0
iio-core: ******** mask2[8] is 0xffff0000000e2720 max1363 1-0064: available_scan_mask 8 subset of 0. Never used
iio-core: ******** mask2[9] is 0xffff0000000e2660 max1363 1-0064: available_scan_mask 9 subset of 0. Never used
iio-core: ******** mask2[10] is 0xffff0000000e25a0 max1363 1-0064: available_scan_mask 10 subset of 0. Never used
iio-core: ******** mask2[11] is 0xffff0000000e24e0 max1363 1-0064: available_scan_mask 11 subset of 0. Never used
iio-core: ******** mask2[12] is 0xffff0000000e2420 max1363 1-0064: available_scan_mask 12 subset of 0. Never used
iio-core: ******** mask2[13] is 0xffff0000000e2360 max1363 1-0064: available_scan_mask 13 subset of 0. Never used

Fix the available_scan_masks sanity check logic so that it only prints the warning when an element of available_scan_mask is in fact a subset of a previous one.

With this fix, the warning output becomes both correct and more informative:

max1363 1-0064: Mask 7 (0xc) is a subset of mask 6 (0xf) and will be ignored.

Fixes 2718f15403fb ("iio: sanity check available_scan_masks array")

---
Changes since v1:
- Make it a standalone patch. max1363 fix will be sent later.
- Keep using bitmap_subset().
- Add more information about the error in the commit log.

 drivers/iio/industrialio-core.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
drivers/iio/industrialio-core.c +5 -13
index 6a6568d4a2cb..dabfcb50e5fe 100644
@@ -1947,21 +1947,13 @@ static void iio_sanity_check_avail_scan_masks(struct iio_dev *indio_dev)
 	 * available masks in the order of preference (presumably the least
 	 * costy to access masks first).
 	 */
-	for (i = 0; i < num_masks - 1; i++) {
-		const unsigned long *mask1;
-		int j;
 
-		mask1 = av_masks + i * longs_per_mask;
-		for (j = i + 1; j < num_masks; j++) {
-			const unsigned long *mask2;
-
-			mask2 = av_masks + j * longs_per_mask;
-			if (bitmap_subset(mask2, mask1, masklength))
+	for (i = 1; i < num_masks; i++)
+		for (int j = 0; j < i; j++)
+			if (bitmap_subset(&av_masks[i], &av_masks[j], BITS_PER_LONG))
 				dev_warn(indio_dev->dev.parent,
-					 "available_scan_mask %d subset of %d. Never used\n",
-					 j, i);
-		}
-	}
+					 "Mask %d (0x%lx) is a subset of mask %d (0x%lx) and will be ignored.\n",
+					 i, av_masks[i], j, av_masks[j]);
 }
 
 /**
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · On Tue, 29 Apr 2025 17:17:17 -0300 Fabio Estevam <[email protected]> wrote:

From: Fabio Estevam <[email protected]>

Since commit 2718f15403fb ("iio: sanity check available_scan_masks array"), verbose and misleading warnings are printed for devices like the MAX11601:

max1363 1-0064: available_scan_mask 8 subset of 0. Never used max1363 1-0064: available_scan_mask 9 subset of 0. Never used max1363 1-0064: available_scan_mask 10 subset of 0. Never used max1363 1-0064: available_scan_mask 11 subset of 0. Never used max1363 1-0064: available_scan_mask 12 subset of 0. Never used max1363 1-0064: available_scan_mask 13 subset of 0. Never used
...
[warnings continue]

Quoted 2 lines

Adding some debug prints show that the mask1 and mask2 indexes are incorrectly computed, leading to wrong array access.

Just for reference, as per the max1363 thread, the
bug isn't what is changed here, but rather another driver side problem.

Quoted 76 lines

These incorrect accesses causes the wrong subset output:

iio-core: ******** mask1[0] is 0x1
iio-core: ******** mask2[1] is 0x4
iio-core: ******** mask2[2] is 0x3
iio-core: ******** mask2[3] is 0xf
iio-core: ******** mask2[4] is 0x1000
iio-core: ******** mask2[5] is 0x40000
iio-core: ******** mask2[6] is 0x3000
iio-core: ******** mask2[7] is 0x0
iio-core: ******** mask2[8] is 0xffff0000000e2720 max1363 1-0064: available_scan_mask 8 subset of 0. Never used
iio-core: ******** mask2[9] is 0xffff0000000e2660 max1363 1-0064: available_scan_mask 9 subset of 0. Never used
iio-core: ******** mask2[10] is 0xffff0000000e25a0 max1363 1-0064: available_scan_mask 10 subset of 0. Never used
iio-core: ******** mask2[11] is 0xffff0000000e24e0 max1363 1-0064: available_scan_mask 11 subset of 0. Never used
iio-core: ******** mask2[12] is 0xffff0000000e2420 max1363 1-0064: available_scan_mask 12 subset of 0. Never used
iio-core: ******** mask2[13] is 0xffff0000000e2360 max1363 1-0064: available_scan_mask 13 subset of 0. Never used

Fix the available_scan_masks sanity check logic so that it only prints the warning when an element of available_scan_mask is in fact a subset of a previous one.

With this fix, the warning output becomes both correct and more informative:

max1363 1-0064: Mask 7 (0xc) is a subset of mask 6 (0xf) and will be ignored.

Fixes 2718f15403fb ("iio: sanity check available_scan_masks array")

---
Changes since v1:
- Make it a standalone patch. max1363 fix will be sent later.
- Keep using bitmap_subset().
- Add more information about the error in the commit log.

 drivers/iio/industrialio-core.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
drivers/iio/industrialio-core.c +5 -13
index 6a6568d4a2cb..dabfcb50e5fe 100644
@@ -1947,21 +1947,13 @@ static void iio_sanity_check_avail_scan_masks(struct iio_dev *indio_dev)
 	 * available masks in the order of preference (presumably the least
 	 * costy to access masks first).
 	 */
-	for (i = 0; i < num_masks - 1; i++) {
-		const unsigned long *mask1;
-		int j;
 
-		mask1 = av_masks + i * longs_per_mask;
-		for (j = i + 1; j < num_masks; j++) {
-			const unsigned long *mask2;
-
-			mask2 = av_masks + j * longs_per_mask;
-			if (bitmap_subset(mask2, mask1, masklength))
+	for (i = 1; i < num_masks; i++)
+		for (int j = 0; j < i; j++)
+			if (bitmap_subset(&av_masks[i], &av_masks[j], BITS_PER_LONG))
 				dev_warn(indio_dev->dev.parent,
-					 "available_scan_mask %d subset of %d. Never used\n",
-					 j, i);
-		}
-	}
+					 "Mask %d (0x%lx) is a subset of mask %d (0x%lx) and will be ignored.\n",
+					 i, av_masks[i], j, av_masks[j]);
 }
 
 /**