Skip to contentSkip to conversation

mainline build failure due to 6c77676645ad ("iov_iter: Fix iter_xarray_get_pages{,_alloc}()")

DISCUSSION Sudip Mukherjee 4 participants 10 messages Jun 11, 2022 – Jun 13, 2022

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

Hi All,

The latest mainline kernel branch fails to build for "arm allmodconfig", "xtensa allmodconfig" and "csky allmodconfig" with the error:

In file included from ./include/linux/kernel.h:26,

                 from ./include/linux/crypto.h:16,
                 from ./include/crypto/hash.h:11,
                 from lib/iov_iter.c:2:

lib/iov_iter.c: In function 'iter_xarray_get_pages':
./include/linux/minmax.h:20:35: error: comparison of distinct pointer types lacks a cast [-Werror]
20 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
| ^~
./include/linux/minmax.h:26:18: note: in expansion of macro '__typecheck'
26 | (__typecheck(x, y) && __no_side_effects(x, y))

      |                  ^~~~~~~~~~~
./include/linux/minmax.h:36:31: note: in expansion of macro '__safe_cmp'
   36 |         __builtin_choose_expr(__safe_cmp(x, y), \
      |                               ^~~~~~~~~~
./include/linux/minmax.h:45:25: note: in expansion of macro '__careful_cmp'
   45 | #define min(x, y)       __careful_cmp(x, y, <)
      |                         ^~~~~~~~~~~~~

lib/iov_iter.c:1464:16: note: in expansion of macro 'min'
1464 | return min(nr * PAGE_SIZE - offset, maxsize);
| ^~~
lib/iov_iter.c: In function 'iter_xarray_get_pages_alloc':
./include/linux/minmax.h:20:35: error: comparison of distinct pointer types lacks a cast [-Werror]
20 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
| ^~
./include/linux/minmax.h:26:18: note: in expansion of macro '__typecheck'
26 | (__typecheck(x, y) && __no_side_effects(x, y))

      |                  ^~~~~~~~~~~
./include/linux/minmax.h:36:31: note: in expansion of macro '__safe_cmp'
   36 |         __builtin_choose_expr(__safe_cmp(x, y), \
      |                               ^~~~~~~~~~
./include/linux/minmax.h:45:25: note: in expansion of macro '__careful_cmp'
   45 | #define min(x, y)       __careful_cmp(x, y, <)
      |                         ^~~~~~~~~~~~~

lib/iov_iter.c:1628:16: note: in expansion of macro 'min'
1628 | return min(nr * PAGE_SIZE - offset, maxsize);

git bisect pointed to 6c77676645ad ("iov_iter: Fix iter_xarray_get_pages{,_alloc}()")

And, reverting it on top of mainline branch has fixed the build failure.

--
Regards
Sudip

#1 lore raw
Quoted 5 lines · Sudip Mukherjee

Hi All,

The latest mainline kernel branch fails to build for "arm allmodconfig", "xtensa allmodconfig" and "csky allmodconfig" with the error:

missed adding "mips allmodconfig".

Signature · 2 lines
Regards
Sudip
maintainer
#2 lore raw
In reply to Sudip Mukherjee
Quoted 38 lines · Sudip Mukherjee

Hi All,

The latest mainline kernel branch fails to build for "arm allmodconfig", "xtensa allmodconfig" and "csky allmodconfig" with the error:

In file included from ./include/linux/kernel.h:26,

                 from ./include/linux/crypto.h:16,
                 from ./include/crypto/hash.h:11,
                 from lib/iov_iter.c:2:

lib/iov_iter.c: In function 'iter_xarray_get_pages':
./include/linux/minmax.h:20:35: error: comparison of distinct pointer types lacks a cast [-Werror]
20 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
| ^~
./include/linux/minmax.h:26:18: note: in expansion of macro '__typecheck'
26 | (__typecheck(x, y) && __no_side_effects(x, y))

      |                  ^~~~~~~~~~~
./include/linux/minmax.h:36:31: note: in expansion of macro '__safe_cmp'
   36 |         __builtin_choose_expr(__safe_cmp(x, y), \
      |                               ^~~~~~~~~~
./include/linux/minmax.h:45:25: note: in expansion of macro '__careful_cmp'
   45 | #define min(x, y)       __careful_cmp(x, y, <)
      |                         ^~~~~~~~~~~~~

lib/iov_iter.c:1464:16: note: in expansion of macro 'min'
1464 | return min(nr * PAGE_SIZE - offset, maxsize);
| ^~~
lib/iov_iter.c: In function 'iter_xarray_get_pages_alloc':
./include/linux/minmax.h:20:35: error: comparison of distinct pointer types lacks a cast [-Werror]
20 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
| ^~
./include/linux/minmax.h:26:18: note: in expansion of macro '__typecheck'
26 | (__typecheck(x, y) && __no_side_effects(x, y))

      |                  ^~~~~~~~~~~
./include/linux/minmax.h:36:31: note: in expansion of macro '__safe_cmp'
   36 |         __builtin_choose_expr(__safe_cmp(x, y), \
      |                               ^~~~~~~~~~
./include/linux/minmax.h:45:25: note: in expansion of macro '__careful_cmp'
   45 | #define min(x, y)       __careful_cmp(x, y, <)
      |                         ^~~~~~~~~~~~~
Quoted 5 lines

lib/iov_iter.c:1628:16: note: in expansion of macro 'min'
1628 | return min(nr * PAGE_SIZE - offset, maxsize);

git bisect pointed to 6c77676645ad ("iov_iter: Fix iter_xarray_get_pages{,_alloc}()")

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

maintainer
#3 lore raw
Quoted 6 lines · Al Viro

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);

there (both places).

Al, going back to sleep - 4 hours is not enough...

maintainer
#4 lore raw
Quoted 10 lines · Al Viro
Quoted 6 lines · Al Viro

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

Quoted 7 lines

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);

there (both places).

The reason we can't overflow on multiplication there, BTW, is that we have nr <= count, and count has come from weirdly open-coded
DIV_ROUND_UP(size + offset, PAGE_SIZE)
IMO we'd better make it explicit, so how about the following:

---

lib/iov_iter.c +4 -20
index dda6d5f481c1..150dbd314d25 100644
@@ -1445,15 +1445,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
 	offset = pos & ~PAGE_MASK;
 	*_start_offset = offset;
 
-	count = 1;
-	if (size > PAGE_SIZE - offset) {
-		size -= PAGE_SIZE - offset;
-		count += size >> PAGE_SHIFT;
-		size &= ~PAGE_MASK;
-		if (size)
-			count++;
-	}
-
+	count = DIV_ROUND_UP(size + offset, PAGE_SIZE);
 	if (count > maxpages)
 		count = maxpages;
 
@@ -1461,7 +1453,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
 	if (nr == 0)
 		return 0;
 
-	return min(nr * PAGE_SIZE - offset, maxsize);
+	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 }
 
 /* must be done on non-empty ITER_IOVEC one */
@@ -1607,15 +1599,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
 	offset = pos & ~PAGE_MASK;
 	*_start_offset = offset;
 
-	count = 1;
-	if (size > PAGE_SIZE - offset) {
-		size -= PAGE_SIZE - offset;
-		count += size >> PAGE_SHIFT;
-		size &= ~PAGE_MASK;
-		if (size)
-			count++;
-	}
-
+	count = DIV_ROUND_UP(size + offset, PAGE_SIZE);
 	p = get_pages_array(count);
 	if (!p)
 		return -ENOMEM;
@@ -1625,7 +1609,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
 	if (nr == 0)
 		return 0;
 
-	return min(nr * PAGE_SIZE - offset, maxsize);
+	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 }
 
 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
#5 lore raw
Quoted 15 lines · Al Viro
Quoted 14 lines · Al Viro
Quoted 6 lines · Al Viro

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

Quoted 7 lines
Quoted 3 lines
	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);

there (both places).

The reason we can't overflow on multiplication there, BTW, is that we have nr <= count, and count has come from weirdly open-coded
DIV_ROUND_UP(size + offset, PAGE_SIZE)

That is often done to avoid possible overflows. Is size + offset guaranteed to be smaller than ULONG_MAX ?

Guenter

Quoted 60 lines

IMO we'd better make it explicit, so how about the following:

---

lib/iov_iter.c +4 -20
index dda6d5f481c1..150dbd314d25 100644
@@ -1445,15 +1445,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
 	offset = pos & ~PAGE_MASK;
 	*_start_offset = offset;
 
-	count = 1;
-	if (size > PAGE_SIZE - offset) {
-		size -= PAGE_SIZE - offset;
-		count += size >> PAGE_SHIFT;
-		size &= ~PAGE_MASK;
-		if (size)
-			count++;
-	}
-
+	count = DIV_ROUND_UP(size + offset, PAGE_SIZE);
 	if (count > maxpages)
 		count = maxpages;
 
@@ -1461,7 +1453,7 @@ static ssize_t iter_xarray_get_pages(struct iov_iter *i,
 	if (nr == 0)
 		return 0;
 
-	return min(nr * PAGE_SIZE - offset, maxsize);
+	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 }
 
 /* must be done on non-empty ITER_IOVEC one */
@@ -1607,15 +1599,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
 	offset = pos & ~PAGE_MASK;
 	*_start_offset = offset;
 
-	count = 1;
-	if (size > PAGE_SIZE - offset) {
-		size -= PAGE_SIZE - offset;
-		count += size >> PAGE_SHIFT;
-		size &= ~PAGE_MASK;
-		if (size)
-			count++;
-	}
-
+	count = DIV_ROUND_UP(size + offset, PAGE_SIZE);
 	p = get_pages_array(count);
 	if (!p)
 		return -ENOMEM;
@@ -1625,7 +1609,7 @@ static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i,
 	if (nr == 0)
 		return 0;
 
-	return min(nr * PAGE_SIZE - offset, maxsize);
+	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 }
 
 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
maintainer
#6 lore raw
Quoted 18 lines · Guenter Roeck
Quoted 17 lines · Al Viro
Quoted 16 lines · Al Viro
Quoted 6 lines · Al Viro

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

there (both places).

The reason we can't overflow on multiplication there, BTW, is that we have nr <= count, and count has come from weirdly open-coded
DIV_ROUND_UP(size + offset, PAGE_SIZE)

That is often done to avoid possible overflows. Is size + offset guaranteed to be smaller than ULONG_MAX ?

You'd need iter->count and maxsize argument to be within PAGE_SIZE of ULONG_MAX. How would you populate that xarray, anyway?

maintainer
#7 lore raw
Quoted 25 lines · Al Viro
Quoted 24 lines · Guenter Roeck
Quoted 22 lines · Al Viro
Quoted 10 lines · Al Viro
Quoted 6 lines · Al Viro

At a guess, should be

	return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

Quoted 7 lines

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

	return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);

there (both places).

The reason we can't overflow on multiplication there, BTW, is that we have nr <= count, and count has come from weirdly open-coded
DIV_ROUND_UP(size + offset, PAGE_SIZE)

Quoted 5 lines
Quoted 2 lines

That is often done to avoid possible overflows. Is size + offset guaranteed to be smaller than ULONG_MAX ?

You'd need iter->count and maxsize argument to be within PAGE_SIZE of ULONG_MAX. How would you populate that xarray, anyway?

FWIW, it probably would be a good idea to truncate maxsize to LONG_MAX
in iov_iter_get_pages()/iov_iter_get_pages_alloc(), just to avoid that kind of crap in the future. Check that maxpages is not zero on the top level, while we are at it...

Any caller of iov_iter_get_pages{,_alloc}() must be ready to handle
getting less than what they'd asked for - if nothing else, get_user_pages_fast() might refuse to give you more than this many pages, etc. All in-tree callers do, AFAICS. And if anyone comes with "pin me more than LONG_MAX bytes of RAM in one call, I can't accept anything less than that", well... ISO9000-compliant response per Dilbert would be called for.

#8 lore raw
In reply to Al Viro
Quoted 16 lines · Al Viro
Quoted 14 lines · Al Viro
Quoted 6 lines · Al Viro

At a guess, should be

    return min((size_t)nr * PAGE_SIZE - offset, maxsize);

in both places. I'm more than half-asleep right now; could you verify that it (as the last lines of both iter_xarray_get_pages() and iter_xarray_get_pages_alloc()) builds correctly?

No, I'm misreading it - it's unsigned * unsigned long - unsigned vs. size_t. On arm it ends up with unsigned long vs. unsigned int; functionally it *is* OK (both have the same range there), but it triggers the tests. Try

Quoted 8 lines
Quoted 3 lines
      return min_t(size_t, nr * PAGE_SIZE - offset, maxsize);

there (both places).

The reason we can't overflow on multiplication there, BTW, is that we have nr <= count, and count has come from weirdly open-coded
DIV_ROUND_UP(size + offset, PAGE_SIZE)
IMO we'd better make it explicit, so how about the following:

Thanks. Fixed the build for me.

Signature · 2 lines
Regards
Sudip