Skip to contentSkip to conversation

paging_domctl() missing break statements?

DISCUSSION Jan Beulich 6 participants 10 messages Feb 17, 2010 – Jun 25, 2010

lore
Share & tools
lore
#0 lore raw

The main switch statement in that function looks suspicious, and with no explicit comment saying that fall-through is intended it would seem like one or two break statements are actually missing. Comments?

Jan

#1 lore raw
Quoted 3 lines · Jan Beulich

The main switch statement in that function looks suspicious, and with no explicit comment saying that fall-through is intended it would seem like one or two break statements are actually missing. Comments?

Yep, looks like that was just working by blind luck.

Tim.

xen/arch/x86/mm/paging.c +2 -0
diff -r 560277d2fd20 xen/arch/x86/mm/paging.c
@@ -717,11 +717,13 @@
                 hap_logdirty_init(d);
             return paging_log_dirty_enable(d);
         }
+        break;
 
     case XEN_DOMCTL_SHADOW_OP_OFF:
         if ( paging_mode_log_dirty(d) )
             if ( (rc = paging_log_dirty_disable(d)) != 0 )
                 return rc;
+        break;
 
     case XEN_DOMCTL_SHADOW_OP_CLEAN:
     case XEN_DOMCTL_SHADOW_OP_PEEK:
#2 lore raw

/me wonders if this explains the periodic but apparently harmless messages I often see on the console like:

(XEN) paging.c:170: paging_free_log_dirty_bitmap: used X pages for domain Y dirty logging

which I've never reported.

And, if not, is that message useful/meaningful to anyone or should it be removed?

Quoted 42 lines

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Wednesday, February 17, 2010 2:58 AM
To: Jan Beulich
Cc: [email protected]
Subject: Re: [Xen-devel] paging_domctl() missing break statements?

Quoted 1 line · Jan Beulich

The main switch statement in that function looks suspicious, and with

no

Quoted 1 line

explicit comment saying that fall-through is intended it would seem

like

Quoted 1 line

one or two break statements are actually missing. Comments?

Yep, looks like that was just working by blind luck.

Tim.

diff -r 560277d2fd20 xen/arch/x86/mm/paging.c

xen/arch/x86/mm/paging.c +2 -0
@@ -717,11 +717,13 @@
                 hap_logdirty_init(d);
             return paging_log_dirty_enable(d);
         }
+        break;
 
     case XEN_DOMCTL_SHADOW_OP_OFF:
         if ( paging_mode_log_dirty(d) )
             if ( (rc = paging_log_dirty_disable(d)) != 0 )
                 return rc;
+        break;
 
     case XEN_DOMCTL_SHADOW_OP_CLEAN:
     case XEN_DOMCTL_SHADOW_OP_PEEK:

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw
Quoted 6 lines · Dan Magenheimer

/me wonders if this explains the periodic but apparently harmless messages I often see on the console like:

(XEN) paging.c:170: paging_free_log_dirty_bitmap: used X pages for domain Y dirty logging

which I've never reported.

No, that's just some unrelated noise; I think it went in when the log-dirty bitmaps were made sparse.

Quoted 2 lines

And, if not, is that message useful/meaningful to anyone or should it be removed?

It should be removed.

xen/arch/x86/mm/paging.c +0 -3
diff -r 560277d2fd20 xen/arch/x86/mm/paging.c
@@ -165,9 +165,6 @@
 
     if ( !mfn_valid(d->arch.paging.log_dirty.top) )
         return;
-
-    dprintk(XENLOG_DEBUG, "%s: used %d pages for domain %d dirty logging\n",
-            __FUNCTION__, d->arch.paging.log_dirty.allocs, d->domain_id);
 
     l4 = map_domain_page(mfn_x(d->arch.paging.log_dirty.top));
#4 lore raw
In reply to Tim Deegan
Quoted 16 lines · Tim Deegan
Quoted 3 lines · Jan Beulich

The main switch statement in that function looks suspicious, and with no explicit comment saying that fall-through is intended it would seem like one or two break statements are actually missing. Comments?

Yep, looks like that was just working by blind luck.

Tim.

diff -r 560277d2fd20 xen/arch/x86/mm/paging.c

xen/arch/x86/mm/paging.c +0 -0
@@ -717,11 +717,13 @@
                  hap_logdirty_init(d);
              return paging_log_dirty_enable(d);
          }

xen/arch/x86/mm/paging.c line 729

Quoted 10 lines
xen/arch/x86/mm/paging.c +2 -0
+        break;
 
      case XEN_DOMCTL_SHADOW_OP_OFF:
          if ( paging_mode_log_dirty(d) )
              if ( (rc = paging_log_dirty_disable(d)) != 0 )
                  return rc;
+        break;
 
      case XEN_DOMCTL_SHADOW_OP_CLEAN:
      case XEN_DOMCTL_SHADOW_OP_PEEK:

This was never applied.

Paolo

#5 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 7 lines
Quoted 3 lines

The main switch statement in that function looks suspicious, and with no explicit comment saying that fall-through is intended it would seem like one or two break statements are actually missing. Comments?

Yep, looks like that was just working by blind luck.

Tim.

This was never applied.

It was applied as 20954:b4041e7bbe1b but then reverted as 20987:c4301c2c727d:
"""
Revert 20954:b4041e7bbe1b "paging_domctl: Add missing breaks in switch stmt"

This fixed a fairly innocuous bug (OP_ENABLE/OP_OFF both don't work properly) but unmasked a much nastier one (turning off shadow mode on a PV guest crashes the hypervisor).

So, for now, we pick the less of two evils. We don't really much rely on OP_ENABLE/OP_OFF anyway, as it happens.

"""

-- Keir

#7 lore raw

The problem with the patch is that with the break statements in the "else" cases of XEN_DOMCTL_SHADOW_OP_ENABLE and XEN_DOMCTL_SHADOW_OP_OFF it currently falls through. Simply sticking break in at the bottom changes these control flow paths.

So a (more) proper patch should replicate the code of XEN_DOMCTL_SHADOW_OP_OFF (and of OP_CLEAN and OP_PEEK):

        if ( paging_mode_log_dirty(d) )
            if ( (rc = paging_log_dirty_disable(d)) != 0 )
                return rc;

and

         return paging_log_dirty_op(d, sc);

before the break statement. Same with the XEN_DOMCTL_SHADOW_OP_OFF case statement... it should have:

         return paging_log_dirty_op(d, sc);

after the initial if statement as its "else" case.

I can whip up a patch to do that, although it's not clear to me that this is entirely cleaner. Or that XEN_DOMCTL_SHADOW_OP_ENABLE needs the XEN_DOMCTL_SHADOW_OP_OFF code. This would have to be analysed more carefully (unless somebody knows the answer off the top of their head). It could just be that in the "else" case for XEN_DOMCTL_SHADOW_OP_ENABLE it should just do "return paging_log_dirty_op(d, sc);". Any thoughts?

Patrick

Quoted 22 lines · Tim Deegan
Quoted 1 line · Paolo Bonzini

This was never applied.

It was applied and then reverted because it caused Xen crashes:
http://xenbits.xensource.com/xen-unstable.hg?rev/c4301c2c727d
and nobody's got round to properly reworking it.

Cheers,

Tim.

--
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#8 lore raw
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Patrick Colp

The problem with the patch is that with the break statements in the "else" cases of XEN_DOMCTL_SHADOW_OP_ENABLE and XEN_DOMCTL_SHADOW_OP_OFF it currently falls through. Simply sticking break in at the bottom changes these control flow paths.

So a (more) proper patch should replicate the code of XEN_DOMCTL_SHADOW_OP_OFF (and of OP_CLEAN and OP_PEEK):

        if ( paging_mode_log_dirty(d) )
            if ( (rc = paging_log_dirty_disable(d)) != 0 )
                return rc;

and

Quoted 8 lines
         return paging_log_dirty_op(d, sc);

before the break statement. Same with the XEN_DOMCTL_SHADOW_OP_OFF case statement... it should have:

         return paging_log_dirty_op(d, sc);

after the initial if statement as its "else" case.

That means you consider the current behavior right, i.e. the fall through being intentional. If that's indeed the case, rather than replicating the code I'd suggest just annotating the code to state that the fall through is intentional in both places (as is common practice elsewhere).

Jan

Jan

#9 lore raw
In reply to Patrick Colp
Quoted 4 lines · Patrick Colp

The problem with the patch is that with the break statements in the "else" cases of XEN_DOMCTL_SHADOW_OP_ENABLE and XEN_DOMCTL_SHADOW_OP_OFF it currently falls through. Simply sticking break in at the bottom changes these control flow paths.

Yep - and that would be the right thing to do; the problem is that it unmasked a different bug that had crept into the shadow-disable code. Since this bug was harmless, the other bug was a hypervisor crash, and the release was imminent, we just reverted to the old code.

The attached patch reinstates the breaks in the flow control (and folds two identical cases together) and fixes the memory leak that was causing the crash.

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, XenServer Engineering
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
xen/arch/x86/mm/paging.c +6 -8
diff -r 4001ab0d5785 xen/arch/x86/mm/paging.c
@@ -700,23 +700,21 @@
      */
     switch ( sc->op )
     {
+
+    case XEN_DOMCTL_SHADOW_OP_ENABLE:
+        if ( !(sc->mode & XEN_DOMCTL_SHADOW_ENABLE_LOG_DIRTY) )
+            break;
+        /* Else fall through... */
     case XEN_DOMCTL_SHADOW_OP_ENABLE_LOGDIRTY:
         if ( hap_enabled(d) )
             hap_logdirty_init(d);
         return paging_log_dirty_enable(d);
 
-    case XEN_DOMCTL_SHADOW_OP_ENABLE:
-        if ( sc->mode & XEN_DOMCTL_SHADOW_ENABLE_LOG_DIRTY )
-        {
-            if ( hap_enabled(d) )
-                hap_logdirty_init(d);
-            return paging_log_dirty_enable(d);
-        }
-
     case XEN_DOMCTL_SHADOW_OP_OFF:
         if ( paging_mode_log_dirty(d) )
             if ( (rc = paging_log_dirty_disable(d)) != 0 )
                 return rc;
+        break;
 
     case XEN_DOMCTL_SHADOW_OP_CLEAN:
     case XEN_DOMCTL_SHADOW_OP_PEEK:
xen/arch/x86/mm/shadow/common.c +17 -8
diff -r 4001ab0d5785 xen/arch/x86/mm/shadow/common.c
@@ -3241,9 +3241,12 @@
         {
             int i;
             mfn_t *oos_snapshot = v->arch.paging.shadow.oos_snapshot;
-            for(i = 0; i < SHADOW_OOS_PAGES; i++)
+            for ( i = 0; i < SHADOW_OOS_PAGES; i++ )
                 if ( mfn_valid(oos_snapshot[i]) )
+                {
                     shadow_free(d, oos_snapshot[i]);
+                    oos_snapshot[i] = _mfn(INVALID_MFN);
+                }
         }
 #endif /* OOS */
     }
@@ -3395,17 +3398,23 @@
 #endif
                 make_cr3(v, pagetable_get_pfn(v->arch.guest_table));
 
+#if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) 
+            {
+                int i;
+                mfn_t *oos_snapshot = v->arch.paging.shadow.oos_snapshot;
+                for ( i = 0; i < SHADOW_OOS_PAGES; i++ )
+                    if ( mfn_valid(oos_snapshot[i]) )
+                    {
+                        shadow_free(d, oos_snapshot[i]);
+                        oos_snapshot[i] = _mfn(INVALID_MFN);
+                    }
+            }
+#endif /* OOS */
         }
 
         /* Pull down the memory allocation */
         if ( sh_set_allocation(d, 0, NULL) != 0 )
-        {
-            // XXX - How can this occur?
-            //       Seems like a bug to return an error now that we've
-            //       disabled the relevant shadow mode.
-            //
-            return -ENOMEM;
-        }
+            BUG(); /* In fact, we will have BUG()ed already */
         shadow_hash_teardown(d);
         SHADOW_PRINTK("un-shadowing of domain %u done."
                        "  Shadow pages total = %u, free = %u, p2m=%u\n",

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel