Skip to contentSkip to conversation

Arm: tidy page_get_owner_and_nr_reference()

PATCH In review Jan Beulich 2 participants 8 messages Jul 2, 2025 – Sep 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Arm: tidy page_get_owner_and_nr_reference()

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

---
Noticed while reviewing the RISC-V code copying this machinery almost verbatim.

xen/arch/arm/mm.c +2 -5
@@ -256,16 +256,13 @@ static struct domain *page_get_owner_and
 
     /* Restrict nr to avoid "double" overflow */
     if ( nr >= PGC_count_mask )
-    {
-        ASSERT_UNREACHABLE();
         return NULL;
-    }
 
     do {
         x = y;
         /*
-         * Count ==  0: Page is not allocated, so we cannot take a reference.
-         * Count == -1: Reference count would wrap, which is invalid.
+         * Count ==   0: Page is not allocated, so we cannot take a reference.
+         * Count >= -nr: Reference count would wrap, which is invalid.
          */
         if ( unlikely(((x + nr) & PGC_count_mask) <= nr) )
             return NULL;
#1 lore raw

Hi Jan,

Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

So I am reluctant to remove it at the moment.

Cheers,

[1] https://lore.kernel.org/xen-devel/[email protected]/

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 10 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

Quoted 1 line

So I am reluctant to remove it at the moment.

But then some checking needs to be added elsewhere.

Jan

#3 lore raw

Hi Jan,

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

Quoted 8 lines
Quoted 3 lines

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

I agree we probably want check upfront. But I still don't think we want to remove this ASSERT_UNREACHABLE().

By the way, I am not asking you to add those checks.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 18 lines · Julien Grall
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

Quoted 8 lines
Quoted 3 lines

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

Quoted 4 lines

I agree we probably want check upfront. But I still don't think we want to remove this ASSERT_UNREACHABLE().

By the way, I am not asking you to add those checks.

Sure, I wouldn't even know where and what. I can re-send just the comment change, but that would feel wrong as long as the ASSERT_UNREACHABLE() is actually reachable.

Jan

#5 lore raw

Hi Jan,

Sorry for the late answer.

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Julien Grall
Quoted 16 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

Quoted 8 lines
Quoted 4 lines

I agree we probably want check upfront. But I still don't think we want to remove this ASSERT_UNREACHABLE().

By the way, I am not asking you to add those checks.

Sure, I wouldn't even know where and what. I can re-send just the comment change, but that would feel wrong as long as the ASSERT_UNREACHABLE() is actually reachable.

I am guessing you mean this change:

         /*
-         * Count ==  0: Page is not allocated, so we cannot take a reference.
-         * Count == -1: Reference count would wrap, which is invalid.
+         * Count ==   0: Page is not allocated, so we cannot take a reference.
+         * Count >= -nr: Reference count would wrap, which is invalid.
          */

If so, I think it is still valid regardless whether we continue to use ASSERT_UNREACHABLE().

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw
Quoted 42 lines · Julien Grall

Hi Jan,

Sorry for the late answer.

Quoted 19 lines · Jan Beulich
Quoted 18 lines · Julien Grall
Quoted 16 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

Quoted 8 lines
Quoted 4 lines

I agree we probably want check upfront. But I still don't think we want to remove this ASSERT_UNREACHABLE().

By the way, I am not asking you to add those checks.

Sure, I wouldn't even know where and what. I can re-send just the comment change, but that would feel wrong as long as the ASSERT_UNREACHABLE() is actually reachable.

I am guessing you mean this change:

          /*

- * Count == 0: Page is not allocated, so we cannot take a
reference.

(quoted diff) +1 -1
-         * Count == -1: Reference count would wrap, which is invalid.
+         * Count ==   0: Page is not allocated, so we cannot take a 

reference.
+ * Count >= -nr: Reference count would wrap, which is invalid.

Quoted 4 lines
           */

If so, I think it is still valid regardless whether we continue to use ASSERT_UNREACHABLE().

Yes, that's the comment change which is valid regardless. My reply was about the dropping of the ASSERT_UNREACHABLE(), though: To me, dropping that change simply feels wrong, as it was put there by mistake at the same time as the comment was left unaltered. So to me both changes still go together, unless by a patch going in earlier the unreachability of that return path was indeed guaranteed.

In fact I guess I should have added a Fixes: tag to the patch.

Jan

#7 lore raw
Quoted 44 lines · Jan Beulich
Quoted 38 lines · Julien Grall
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Julien Grall
Quoted 16 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 5 lines · Jan Beulich

When the bumping by <nr> (instead of just 1) was introduced, a comment was left untouched, and a bogus ASSERT_UNREACHABLE() was inserted. That code path can in principle be taken (depending on configuration coming from the outside), and we shouldn't assert anything we didn't check elsewhere.

I suggested to add the ASSERT_UNREACHABLE (see [1]). My take is the overflow is not something that should happen and it is good to be able to catch very clearly in debug build.

But catching an anomalous entry in DT (which aiui the values come from, even if perhaps indirectly) isn't going to depend on people using debug builds. It depends on what DT blobs they use. And issues there shouldn't be checked by assertions, as those blobs live outside of Xen.

Quoted 8 lines
Quoted 4 lines

I agree we probably want check upfront. But I still don't think we want to remove this ASSERT_UNREACHABLE().

By the way, I am not asking you to add those checks.

Sure, I wouldn't even know where and what. I can re-send just the comment change, but that would feel wrong as long as the ASSERT_UNREACHABLE() is actually reachable.

I am guessing you mean this change:

          /*

- * Count == 0: Page is not allocated, so we cannot take a
reference.

(quoted diff) +1 -1
-         * Count == -1: Reference count would wrap, which is invalid.
+         * Count ==   0: Page is not allocated, so we cannot take a 

reference.
+ * Count >= -nr: Reference count would wrap, which is invalid.

Quoted 4 lines
           */

If so, I think it is still valid regardless whether we continue to use ASSERT_UNREACHABLE().

Quoted 6 lines

Yes, that's the comment change which is valid regardless. My reply was about the dropping of the ASSERT_UNREACHABLE(), though: To me, dropping that change simply feels wrong, as it was put there by mistake at the same time as the comment was left unaltered. So to me both changes still go together, unless by a patch going in earlier the unreachability of that return path was indeed guaranteed.

I think we want this sorted for 4.21. I'm happy to shrink the patch to just the comment change, but only if ahead of that whatever other change goes in, making the assertion actually legitimate to live there.

Quoted 1 line

In fact I guess I should have added a Fixes: tag to the patch.

Locally I've added

Fixes 5951b856d8d0 ("xen/arm: introduce put_page_nr and get_page_nr")

Jan