Skip to contentSkip to conversation

ceph: Amend checking to fix `make W=1` build breakage

PATCH In review Andy Shevchenko 4 participants 17 messages Nov 10, 2025 – Nov 25, 2025 Ceph client

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: Amend checking to fix `make W=1` build breakage

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +1 -1
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
 	/* alloc new snap context */
 	err = -ENOMEM;
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
 		goto fail;
 	snapc = ceph_create_snap_context(num, GFP_NOFS);
 	if (!snapc)
Signature · 1 line
2.50.1
#1 lore raw
Quoted 21 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +0 -0
index c65f2b202b2b..521507ea8260 100644

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 5 lines
fs/ceph/snap.c +1 -1
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
 	/* alloc new snap context */
 	err = -ENOMEM;
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Thanks,
Slava.

Quoted 3 lines
fs/ceph/snap.c +0 -0
 		goto fail;
 	snapc = ceph_create_snap_context(num, GFP_NOFS);
 	if (!snapc)
#2 lore raw
Quoted 11 lines · Viacheslav Dubeyko
Quoted 10 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

...

Quoted 5 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 2 lines
fs/ceph/snap.c +1 -1
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Maybe. Or even maybe the U32_MAX is the way to go: Does anybody check those corner cases? Are those never tested? Potential (security) bug?

...

Whatever you find, in case if it will be not the proposed solution as is, consider these patches as Reported-by.

And thanks for the reviews!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by
#3 lore raw
Quoted 24 lines · On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:
Quoted 11 lines · Viacheslav Dubeyko
Quoted 10 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

...

Quoted 5 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 2 lines
fs/ceph/snap.c +1 -1
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Maybe. Or even maybe the U32_MAX is the way to go: Does anybody check those corner cases? Are those never tested? Potential (security) bug?

Quoted 6 lines

...

Whatever you find, in case if it will be not the proposed solution as is, consider these patches as Reported-by.

And thanks for the reviews!

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Thanks,
Slava.

#4 lore raw
Quoted 30 lines · Viacheslav Dubeyko
Quoted 29 lines · On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:
Quoted 11 lines · Viacheslav Dubeyko
Quoted 10 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

...

Quoted 5 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 2 lines
fs/ceph/snap.c +1 -1
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Maybe. Or even maybe the U32_MAX is the way to go: Does anybody check those corner cases? Are those never tested? Potential (security) bug?

...

Whatever you find, in case if it will be not the proposed solution as is, consider these patches as Reported-by.

Quoted 6 lines
Quoted 1 line

And thanks for the reviews!

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Thanks, can this be applied? My builds are still broken.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 31 lines · On Mon, 2025-11-24 at 17:09 +0200, [email protected] wrote:
Quoted 30 lines · Viacheslav Dubeyko
Quoted 29 lines · On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:
Quoted 11 lines · Viacheslav Dubeyko
Quoted 10 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

...

Quoted 5 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 2 lines
fs/ceph/snap.c +1 -1
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Maybe. Or even maybe the U32_MAX is the way to go: Does anybody check those corner cases? Are those never tested? Potential (security) bug?

...

Whatever you find, in case if it will be not the proposed solution as is, consider these patches as Reported-by.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

And thanks for the reviews!

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Thanks, can this be applied? My builds are still broken.

The patchset has been applied on testing branch already [1].

Ilya, when are we planning to send this patchset upstream?

Thanks,
Slava.

[1] https://github.com/ceph/ceph-client.git

#6 lore raw
Quoted 3 lines · Viacheslav Dubeyko
Quoted 2 lines · On Mon, 2025-11-24 at 17:09 +0200, [email protected] wrote:
Quoted 1 line · Viacheslav Dubeyko

On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:

...

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Thanks, can this be applied? My builds are still broken.

The patchset has been applied on testing branch already [1].

Thanks for the information. The Linux Next still has no that branch in. I recommend to write Stephen a message to include your testing branch or something like that into Linux Next, so it will get more test coverage.

Quoted 3 lines

Ilya, when are we planning to send this patchset upstream?

[1] https://github.com/ceph/ceph-client.git

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 12 lines · On Mon, 2025-11-24 at 20:43 +0200, [email protected] wrote:
Quoted 3 lines · Viacheslav Dubeyko
Quoted 2 lines · On Mon, 2025-11-24 at 17:09 +0200, [email protected] wrote:
Quoted 1 line · Viacheslav Dubeyko

On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:

...

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thanks, can this be applied? My builds are still broken.

The patchset has been applied on testing branch already [1].

Thanks for the information. The Linux Next still has no that branch in. I recommend to write Stephen a message to include your testing branch or something like that into Linux Next, so it will get more test coverage.

It's not my personal branch. :) It's the branch that receives all new patches for CephFS kernel client and, usually, it is used for internal testing by Ceph team. Ilya gathers all patches from there and sends it upstream. So, we need to ping Ilya. :)

Thanks,
Slava.

Quoted 4 lines
Quoted 4 lines

Ilya, when are we planning to send this patchset upstream?

[1] https://github.com/ceph/ceph-client.git

#8 lore raw
Quoted 5 lines · Viacheslav Dubeyko
Quoted 4 lines · On Mon, 2025-11-24 at 20:43 +0200, [email protected] wrote:
Quoted 3 lines · Viacheslav Dubeyko
Quoted 2 lines · On Mon, 2025-11-24 at 17:09 +0200, [email protected] wrote:
Quoted 1 line · Viacheslav Dubeyko

On Mon, 2025-11-10 at 21:43 +0200, [email protected] wrote:

...

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I think we can take the patch as it. It looks good. Probably, it makes sense to take a deeper look in the code on our side.

Thanks, can this be applied? My builds are still broken.

The patchset has been applied on testing branch already [1].

Quoted 7 lines
Quoted 3 lines

Thanks for the information. The Linux Next still has no that branch in. I recommend to write Stephen a message to include your testing branch or something like that into Linux Next, so it will get more test coverage.

It's not my personal branch. :) It's the branch that receives all new patches for CephFS kernel client and, usually, it is used for internal testing by Ceph team.

Exactly, that's why my proposal. I would not recommend to do the same for a (semi-)private branches.

Quoted 6 lines

Ilya gathers all patches from there and sends it upstream. So, we need to ping Ilya. :)

Quoted 3 lines
Quoted 3 lines

Ilya, when are we planning to send this patchset upstream?

[1] https://github.com/ceph/ceph-client.git

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
In reply to Viacheslav Dubeyko
Quoted 24 lines · Viacheslav Dubeyko
Quoted 22 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +0 -0
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
Quoted 7 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 4 lines
fs/ceph/snap.c +1 -1
      /* alloc new snap context */
      err = -ENOMEM;
-     if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+     if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Given that the number of snaps is constrained over the wire as a 32-bit integer, you probably want to keep that mapping...(Though I guess it's the sum of two 32-bit integers which technically could overflow, and I'm not sure what happens if you actually hit those boundaries on the server — but nobody generates snapshots on the same file in that quantity).

All that said, it'd be kind of nice if we could just annotate for clang that we are perfectly happy for the evaluation to always be true on a 64-bit architecture (as snapids are 64 bits, we will always be able to count them).
-Greg

Quoted 7 lines

Thanks,
Slava.

Quoted 3 lines
fs/ceph/snap.c +0 -0
              goto fail;
      snapc = ceph_create_snap_context(num, GFP_NOFS);
      if (!snapc)
#10 lore raw
Quoted 41 lines · Gregory Farnum
Quoted 24 lines · Viacheslav Dubeyko
Quoted 22 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +0 -0
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
Quoted 7 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 4 lines
fs/ceph/snap.c +1 -1
      /* alloc new snap context */
      err = -ENOMEM;
-     if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+     if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Given that the number of snaps is constrained over the wire as a 32-bit integer, you probably want to keep that mapping...(Though I guess it's the sum of two 32-bit integers which technically could overflow, and I'm not sure what happens if you actually hit those boundaries on the server — but nobody generates snapshots on the same file in that quantity).

Quoted 4 lines

All that said, it'd be kind of nice if we could just annotate for clang that we are perfectly happy for the evaluation to always be true on a 64-bit architecture (as snapids are 64 bits, we will always be able to count them).

So, are you suggesting to declare num as u64 here? Am I correct?

Thanks,
Slava.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
fs/ceph/snap.c +0 -0
              goto fail;
      snapc = ceph_create_snap_context(num, GFP_NOFS);
      if (!snapc)
#11 lore raw
Quoted 43 lines · Viacheslav Dubeyko
Quoted 41 lines · Gregory Farnum
Quoted 24 lines · Viacheslav Dubeyko
Quoted 22 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +0 -0
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
Quoted 7 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 4 lines
fs/ceph/snap.c +1 -1
      /* alloc new snap context */
      err = -ENOMEM;
-     if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+     if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Given that the number of snaps is constrained over the wire as a 32-bit integer, you probably want to keep that mapping...(Though I guess it's the sum of two 32-bit integers which technically could overflow, and I'm not sure what happens if you actually hit those boundaries on the server — but nobody generates snapshots on the same file in that quantity).

Quoted 6 lines
Quoted 4 lines

All that said, it'd be kind of nice if we could just annotate for clang that we are perfectly happy for the evaluation to always be true on a 64-bit architecture (as snapids are 64 bits, we will always be able to count them).

So, are you suggesting to declare num as u64 here? Am I correct?

What? No, the whole point of this block is checking that it can track all the snapshots and allocate them (on a <32-bit architecture, which are the only ones at risk), isn't it?

#12 lore raw
Quoted 50 lines · Gregory Farnum
Quoted 48 lines · Viacheslav Dubeyko
Quoted 45 lines · Gregory Farnum
Quoted 24 lines · Viacheslav Dubeyko
Quoted 22 lines · Andy Shevchenko

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +0 -0
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
Quoted 7 lines

fs/ceph/snap.c static int build_snap_context(struct ceph_mds_client *mdsc, · line 377

Quoted 4 lines
fs/ceph/snap.c +1 -1
      /* alloc new snap context */
      err = -ENOMEM;
-     if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+     if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

The same question is here. Does it makes sense to declare num as size_t? Could it be more clean solution? Or could it introduce another warnings/errors?

Given that the number of snaps is constrained over the wire as a 32-bit integer, you probably want to keep that mapping...(Though I guess it's the sum of two 32-bit integers which technically could overflow, and I'm not sure what happens if you actually hit those boundaries on the server — but nobody generates snapshots on the same file in that quantity).

All that said, it'd be kind of nice if we could just annotate for clang that we are perfectly happy for the evaluation to always be true on a 64-bit architecture (as snapids are 64 bits, we will always be able to count them).

Quoted 5 lines
Quoted 1 line

So, are you suggesting to declare num as u64 here? Am I correct?

What? No, the whole point of this block is checking that it can track all the snapshots and allocate them (on a <32-bit architecture, which are the only ones at risk), isn't it?

OK. So, what is your point? Are we taking the patch as it is? Or do we need to make some modifications?

Thanks,
Slava.

#13 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · On Mon, 10 Nov 2025 15:44:04 +0100 Andy Shevchenko…

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

Did you really read the code?
The test itself needs moving into ceph_create_snap_context(). Possibly by using kmalloc_array() to do the multiply.

But in any case are large values sane at all?
Allocating very large kernel memory blocks isn't a good idea at all.

In fact this does a kmalloc(... GFP_NOFS) which is pretty likely to fail for even moderate sized requests. I bet it fails 64k (order 4?) on a regular basis.

Perhaps all three value that get added to make 'num' need 'sanity limits' that mean a large allocation just can't happen.

David

Quoted 19 lines
---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +1 -1
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
 	/* alloc new snap context */
 	err = -ENOMEM;
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
 		goto fail;
 	snapc = ceph_create_snap_context(num, GFP_NOFS);
 	if (!snapc)
#14 lore raw
Quoted 7 lines · david laight
Quoted 4 lines · On Mon, 10 Nov 2025 15:44:04 +0100 Andy Shevchenko…

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

Quoted 8 lines
Quoted 6 lines

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

Did you really read the code?

I read the piece that prevents builds. The exercise on how to fix this properly is delegated to the authors and maintainers.

Quoted 6 lines

The test itself needs moving into ceph_create_snap_context(). Possibly by using kmalloc_array() to do the multiply.

But in any case are large values sane at all?
Allocating very large kernel memory blocks isn't a good idea at all.

Quoted 6 lines

In fact this does a kmalloc(... GFP_NOFS) which is pretty likely to fail for even moderate sized requests. I bet it fails 64k (order 4?) on a regular basis.

Perhaps all three value that get added to make 'num' need 'sanity limits' that mean a large allocation just can't happen.

Nice, can you send a followup to fix all that in a better way? (I don't care about the fix as long as it doesn't break my builds)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#15 lore raw
Quoted 30 lines · On Tue, 25 Nov 2025 12:17:10 +0200 Andy Shevchenko…
Quoted 7 lines · david laight
Quoted 4 lines · On Mon, 10 Nov 2025 15:44:04 +0100 Andy Shevchenko…

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

Quoted 8 lines
Quoted 6 lines

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

Did you really read the code?

I read the piece that prevents builds. The exercise on how to fix this properly is delegated to the authors and maintainers.

Quoted 10 lines

The test itself needs moving into ceph_create_snap_context(). Possibly by using kmalloc_array() to do the multiply.

But in any case are large values sane at all?
Allocating very large kernel memory blocks isn't a good idea at all.

In fact this does a kmalloc(... GFP_NOFS) which is pretty likely to fail for even moderate sized requests. I bet it fails 64k (order 4?) on a regular basis.

Quoted 6 lines
Quoted 2 lines

Perhaps all three value that get added to make 'num' need 'sanity limits' that mean a large allocation just can't happen.

Nice, can you send a followup to fix all that in a better way? (I don't care about the fix as long as it doesn't break my builds)

Perhaps -Wtautological-constant-out-of-range-compare should just be delegated to W=2 like (IIRC) -Wtype-bounds has been which is pretty much the same test.

David

#16 lore raw
In reply to david laight
Quoted 21 lines · david laight
Quoted 10 lines · On Mon, 10 Nov 2025 15:44:04 +0100 Andy Shevchenko…

In a few cases the code compares 32-bit value to a SIZE_MAX derived constant which is much higher than that value on 64-bit platforms, Clang, in particular, is not happy about this

fs/ceph/snap.c:377:10: error: result of comparison of constant 2305843009213693948 with expression of type 'u32' (aka 'unsigned int') is always false [-Werror,-Wtautological-constant-out-of-range-compare]
377 | if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))

      |             ~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by casting to size_t. Note, that possible replacement of SIZE_MAX by U32_MAX may lead to the behaviour changes on the corner cases.

Did you really read the code?
The test itself needs moving into ceph_create_snap_context(). Possibly by using kmalloc_array() to do the multiply.

But in any case are large values sane at all?
Allocating very large kernel memory blocks isn't a good idea at all.

Quoted 8 lines

In fact this does a kmalloc(... GFP_NOFS) which is pretty likely to fail for even moderate sized requests. I bet it fails 64k (order 4?) on a regular basis.

Perhaps all three value that get added to make 'num' need 'sanity limits' that mean a large allocation just can't happen.

Yeah, I also really dislike this pattern. The SIZE_MAX has been used in several places of net/ceph and in fs/ceph. I believe that this particular code works only because nobody creates crazy number of snapshots. But even if num value will be crazy big, then ceph_create_snap_context() will fail to allocate memory. So, functionally, this code is not so bad. But, logically this check of the num value is not very reasonable because, anyway, it doesn't protect from the try to allocate unreasonable amount of memory. So, probably, it makes sense to exchange the SIZE_MAX on KMALLOC_MAX_SIZE. I believe it will make likewise check more reasonable and functional.

Thanks,
Slava.

Quoted 21 lines
Quoted 20 lines
---
 fs/ceph/snap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ceph/snap.c +1 -1
index c65f2b202b2b..521507ea8260 100644
@@ -374,7 +374,7 @@ static int build_snap_context(struct ceph_mds_client *mdsc,
 
 	/* alloc new snap context */
 	err = -ENOMEM;
-	if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
+	if ((size_t)num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
 		goto fail;
 	snapc = ceph_create_snap_context(num, GFP_NOFS);
 	if (!snapc)