Skip to contentSkip to conversation

long latency of domain shutdown

DISCUSSION Jan Beulich 2 participants 12 messages May 8, 2008 – May 9, 2008 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): C43E4040.2022A%[email protected]

#2 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 30.04.08 16:26 >>>

Quoted 6 lines · Jan Beulich

According to two forced backtraces with about a second delta, the hypervisor is in the process of releasing the 1:1 mapping of the guest kernel and managed, during that one second, to increment i in free_l3_table() by just 1. This would make up for unbelievable 13,600 clocks per l1 entry being freed.

Quoted 4 lines

That's not great. :-) At such a high cost, perhaps some tracing might indicate if we are taking some stupid slow path in free_domheap_page() or cleanup_page_cacheattr()? I very much hope that 13600 cycles cannot be legitimately accounted for!

I'm afraid it's really that bad. I used another (local to my office) machine, and the numbers aren't exactly as bad as on the box they were originally measured on, but after getting the cumulative clock cycles spent in free_l1_table() and free_domheap_pages() (and their descendants, so the former obviously includes a large part of the latter) during the largest single run of relinquish_memory() I'm getting an average of 3,400 clocks spent in free_domheap_pages() (with all but very few pages going onto the scrub list) and 8,500 clocks spent per page table entry (assuming all entries are populated, so the number really is higher) in free_l1_table().

It's the relationship between the two numbers that makes me believe that there's really this much time spent on it.

For the specific case of cleaning up after a domain, there seems to be a pretty simple workaround, though: free_l{3,4}_table() can simply avoid recursing into put_page_from_l{3,4}e() by checking d->arch.relmem being RELMEM_dom_l{3,4}. This, as expected, reduces the latency of preempting relinquish_memory() (for a 5G domU) on the box I tested from about 3s to less than half a second - if that's considered still too much, the same kind of check could of course be added to free_l2_table().

But as there's no similarly simple mechanism to deal with the DoS potential in pinning/unpinning or installing L4 (and maybe L3) table entries, there'll need to be a way to preempt these call trees anyway. Since hypercalls cannot nest, storing respective state in the vcpu structure shouldn't be a problem, but what I'm unsure about is what side effects a partially validated page table might introduce.

While looking at this I wondered whether there really is a way for Xen heap pages to end up being guest page tables (or similarly descriptor table ones)? I would think if that happened this would be a bug (and perhaps a security issue). If it cannot happen, then the RELMEM_* states could be simplified and domain_relinquish_resources() shortened.

(I was traveling, so it took a while to get to do the measurements.)

Jan

#3 lore raw
Quoted 6 lines · Jan Beulich

While looking at this I wondered whether there really is a way for Xen heap pages to end up being guest page tables (or similarly descriptor table ones)? I would think if that happened this would be a bug (and perhaps a security issue). If it cannot happen, then the RELMEM_* states could be simplified and domain_relinquish_resources() shortened.

You mean just force page-table type counts to zero, and drop main reference count by the same amount? Might work. Would need some thought.

8500 cycles per pte is pretty fantastic. I suppose a few atomic ops are involved. Are you running on an old P4? :-)

-- Keir

#4 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 08.05.08 12:12 >>>

Quoted 6 lines · Jan Beulich

While looking at this I wondered whether there really is a way for Xen heap pages to end up being guest page tables (or similarly descriptor table ones)? I would think if that happened this would be a bug (and perhaps a security issue). If it cannot happen, then the RELMEM_* states could be simplified and domain_relinquish_resources() shortened.

Quoted 2 lines

You mean just force page-table type counts to zero, and drop main reference count by the same amount? Might work. Would need some thought.

No, here I mean having just RELMEM_xen and RELMEM_l{1,2,3,4}. Then simply release Xen pages first, then l4...l1.

For the suggested workaround to reduce latency of relinquish_memory() preemption, I simply mean utilizing the code to deal with circular references also for releasing simple ones (that code path doesn't seem to care to force the type count to zero, but as I understand that's no problem since these pages end up being freed anyway, and that's where the whole type_info field gets re-initialized - or was this happening when the page gets allocated the next time).

Quoted 2 lines

8500 cycles per pte is pretty fantastic. I suppose a few atomic ops are involved. Are you running on an old P4? :-)

Not too old, it's what they called Tulsa as codename (i.e. some of the about two year old Xeons). But I suppose that generally the bigger the box (in terms of number of threads/cores/sockets) the higher the price for atomic ops.

In trying to get a picture, I e.g. measured both the cumulative full free_domheap_pages()' and free_l1_table()'s contributions as well as just the d != NULL sub-part of free_domheap_pages() - example results are
0x2a4990400 clocks for full free_l1_table()
0x10f1a3749 clocks for full free_domheap_pages() 0x0ec6748eb clocks for the d != NULL body
Given how little it is that happens outside of that d != NULL body I'm concluding that the atomic ops are by far not alone responsible for the long execution time. These are dual-threaded CPUs, however, so that while the system was doing nothing else I cannot exclude that the CPUs do something dumb in switching between the threads. But excluding this possible effect from the picture seems to have little sense, since we need to be able to deal with the situation anyway.

Jan

#5 lore raw
Quoted 10 lines · Jan Beulich

No, here I mean having just RELMEM_xen and RELMEM_l{1,2,3,4}. Then simply release Xen pages first, then l4...l1.

For the suggested workaround to reduce latency of relinquish_memory() preemption, I simply mean utilizing the code to deal with circular references also for releasing simple ones (that code path doesn't seem to care to force the type count to zero, but as I understand that's no problem since these pages end up being freed anyway, and that's where the whole type_info field gets re-initialized - or was this happening when the page gets allocated the next time).

You've lost me. Either you are confused or I have forgotten the details of how that shutdown code works. Either is quite possible I suspect. :-) Basically I don't see how this avoids the recursive, and potentially rather expensive, teardown. Nor am I convinced about how much potential time-saving there is to be had here.

-- Keir

#6 lore raw
Quoted 14 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 08.05.08 12:52 >>>

Quoted 10 lines · Jan Beulich

No, here I mean having just RELMEM_xen and RELMEM_l{1,2,3,4}. Then simply release Xen pages first, then l4...l1.

For the suggested workaround to reduce latency of relinquish_memory() preemption, I simply mean utilizing the code to deal with circular references also for releasing simple ones (that code path doesn't seem to care to force the type count to zero, but as I understand that's no problem since these pages end up being freed anyway, and that's where the whole type_info field gets re-initialized - or was this happening when the page gets allocated the next time).

Quoted 4 lines

You've lost me. Either you are confused or I have forgotten the details of how that shutdown code works. Either is quite possible I suspect. :-) Basically I don't see how this avoids the recursive, and potentially rather expensive, teardown.

All I mean is a change like this:

xen/arch/x86/mm.c +6 -0
@@ -1341,6 +1341,9 @@ static void free_l3_table(struct page_in
     l3_pgentry_t *pl3e;
     int           i;
 
+    if(d->arch.relmem == RELMEM_dom_l3)
+        return;
+
     pl3e = map_domain_page(pfn);
 
     for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
@@ -1364,6 +1367,9 @@ static void free_l4_table(struct page_in
     l4_pgentry_t *pl4e = page_to_virt(page);
     int           i;
 
+    if(d->arch.relmem == RELMEM_dom_l4)
+        return;
+
     for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
         if ( is_guest_l4_slot(d, i) )
             put_page_from_l4e(pl4e[i], pfn);

I tried it out on SLE10 SP1 (3.0.4 derived), and it appeared to work and serve the purpose. With this, L3 and L2 tables are no longer freed recursively upon an L4/L3 one dropping its last type reference, but they rather get caught by the code that so far was only responsible for dealing with circular references. The result is that between individual full L2 tables (including the L1s hanging off of them) being released there now is a preemption check. Up until now, when the last L4 table got freed, everything hanging off of it needed to be dealt with in a single non-preemptible chunk.

Quoted 2 lines

Nor am I convinced about how much potential time-saving there is to be had here.

I'm not seeing any time saving here. The other thing I brought up was just an unrelated item pointing out potential for code simplification.

Jan

#7 lore raw
Quoted 6 lines · Jan Beulich
Quoted 2 lines

Nor am I convinced about how much potential time-saving there is to be had here.

I'm not seeing any time saving here. The other thing I brought up was just an unrelated item pointing out potential for code simplification.

Ah, yes, I see.

The approach looks plausible. I think in its current form it will leave zombie L2/L3 pages hanging around and the domain will never actually properly die (e.g., still will be visible with the 'q' key). Because although you do get around to doing free_lX_table(), the type count and ref count of the L2/L3 pages will not drop to zero because the dead L3/L4 page never actually dropped its references properly.

In actuality, since we know that we never have 'cross-domain' pagetable type references, we should actually be able to zap pagetable reference counts to zero. The only reason we don't do that right now is really because it provides good debugging info to see whether a domain's refcounts have got screwed up. But that would not prevent us doing something faster for NDEBUG builds, at least.

Does that make sense?

-- Keir

#8 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 08.05.08 14:11 >>>

The approach looks plausible. I think in its current form it will leave zombie L2/L3 pages hanging around and the domain will never actually properly die (e.g., still will be visible with the 'q' key). Because although you do get around to doing free_lX_table(), the type count and ref count of the L2/L3 pages will not drop to zero because the dead L3/L4 page never actually dropped its references properly.

Hmm, indeed, I should look for this after the next run.

Quoted 8 lines

In actuality, since we know that we never have 'cross-domain' pagetable type references, we should actually be able to zap pagetable reference counts to zero. The only reason we don't do that right now is really because it provides good debugging info to see whether a domain's refcounts have got screwed up. But that would not prevent us doing something faster for NDEBUG builds, at least.

Does that make sense?

Yes, except for me not immediately seeing why this is then not also a problem for the current circular reference handling.

But really, rather than introducing (and fixing) the hack here I'd much prefer a generic solution to the problem, and you didn't say a word on the thoughts I had on that (but in a mail a couple of days ago you indicated you might get around doing something in that area yourself, so I half way implied you may have a mechanism in mind already).

Jan

#9 lore raw
Quoted 11 lines · Jan Beulich
Quoted 8 lines

In actuality, since we know that we never have 'cross-domain' pagetable type references, we should actually be able to zap pagetable reference counts to zero. The only reason we don't do that right now is really because it provides good debugging info to see whether a domain's refcounts have got screwed up. But that would not prevent us doing something faster for NDEBUG builds, at least.

Does that make sense?

Yes, except for me not immediately seeing why this is then not also a problem for the current circular reference handling.

Because ultimately the reference(s) that are still being held on the page we are unvalidating and calling free_lX_table() on will get dropped, due to the fact we are breaking the circular chain and calling free_lX_table()->put_page_and_type()->...

Quoted 5 lines

But really, rather than introducing (and fixing) the hack here I'd much prefer a generic solution to the problem, and you didn't say a word on the thoughts I had on that (but in a mail a couple of days ago you indicated you might get around doing something in that area yourself, so I half way implied you may have a mechanism in mind already).

I don't have a very clear plan, except that some kind of continuation (basically encoding of how far we got) must be encoded in the page_info structure. We should be able to find spare bits for a page which is in this in-between state.

-- Keir

#10 lore raw
Quoted 11 lines
Quoted 6 lines
Quoted 1 line

Keir Fraser <[email protected]> 08.05.08 14:36 >>>

But really, rather than introducing (and fixing) the hack here I'd much prefer a generic solution to the problem, and you didn't say a word on the thoughts I had on that (but in a mail a couple of days ago you indicated you might get around doing something in that area yourself, so I half way implied you may have a mechanism in mind already).

I don't have a very clear plan, except that some kind of continuation (basically encoding of how far we got) must be encoded in the page_info structure. We should be able to find spare bits for a page which is in this in-between state.

Hmm, storing this in page_info seems questionable to me. It'd be at least 18 bits (on x86-64) that we'd need. I think this rather has to go into struct vcpu.

But what worries me more is that (obviously) any affected page will have to have its PGT_validated bit kept clear, which could lead to undesirable latencies in spin loops on other vcpus waiting for it to become set. In the worst case this could lead to deadlocks (at least in the UP case or when multiple vCPU-s of one guest are pinned to the same physical CPU) afaics. Perhaps this part could indeed be addressed with a new PGT_* bit, upon which waiters could exit their spin loops and consider themselves preempted.

Jan

#11 lore raw
Quoted 3 lines · Jan Beulich

Hmm, storing this in page_info seems questionable to me. It'd be at least 18 bits (on x86-64) that we'd need. I think this rather has to go into struct vcpu.

We can, for example, reuse tlbflush_timestamp for this purpose. Stick it in the vcpu structure and I think we make life hard for ourselves. What if the guest does not resume the hypercall, for example? What if the guest goes and tries to execute a different hypercall instead?

Quoted 8 lines

But what worries me more is that (obviously) any affected page will have to have its PGT_validated bit kept clear, which could lead to undesirable latencies in spin loops on other vcpus waiting for it to become set. In the worst case this could lead to deadlocks (at least in the UP case or when multiple vCPU-s of one guest are pinned to the same physical CPU) afaics. Perhaps this part could indeed be addressed with a new PGT_* bit, upon which waiters could exit their spin loops and consider themselves preempted.

Yes, the page state machine does need some more careful thought. I'm pretty sure we have enough page state bits though.

-- Keir

#12 lore raw
In reply to Keir Fraser
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 08.05.08 14:11 >>>

Quoted 6 lines · Jan Beulich
Quoted 2 lines

Nor am I convinced about how much potential time-saving there is to be had here.

I'm not seeing any time saving here. The other thing I brought up was just an unrelated item pointing out potential for code simplification.

Quoted 8 lines

Ah, yes, I see.

The approach looks plausible. I think in its current form it will leave zombie L2/L3 pages hanging around and the domain will never actually properly die (e.g., still will be visible with the 'q' key). Because although you do get around to doing free_lX_table(), the type count and ref count of the L2/L3 pages will not drop to zero because the dead L3/L4 page never actually dropped its references properly.

Indeed, the extended version below avoids this.

Quoted 6 lines

In actuality, since we know that we never have 'cross-domain' pagetable type references, we should actually be able to zap pagetable reference counts to zero. The only reason we don't do that right now is really because it provides good debugging info to see whether a domain's refcounts have got screwed up. But that would not prevent us doing something faster for NDEBUG builds, at least.

I still thought it'd be better to not simply zap the counts, but incrementally drop them using the proper interface:

xen/arch/x86/domain.c +20 -1
Index: 2008-05-08/xen/arch/x86/domain.c
===================================================================
@@ -1725,6 +1725,23 @@ static int relinquish_memory(
         if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
             put_page(page);
 
+        y = page->u.inuse.type_info;
+
+        /*
+         * Forcibly drop reference counts of page tables above top most (which
+         * were skipped to prevent long latencies due to deep recursion - see
+         * the special treatment in free_lX_table()).
+         */
+        if ( type < PGT_root_page_table &&
+             unlikely(((y + PGT_type_mask) &
+                      (PGT_type_mask|PGT_validated)) == type) ) {
+            BUG_ON((y & PGT_count_mask) >= (page->count_info & PGC_count_mask));
+            while ( y & PGT_count_mask ) {
+                put_page_and_type(page);
+                y = page->u.inuse.type_info;
+            }
+        }
+
         /*
          * Forcibly invalidate top-most, still valid page tables at this point
          * to break circular 'linear page table' references. This is okay
@@ -1732,7 +1749,6 @@ static int relinquish_memory(
          * is now dead. Thus top-most valid tables are not in use so a non-zero
          * count means circular reference.
          */
-        y = page->u.inuse.type_info;
         for ( ; ; )
         {
             x = y;
@@ -1896,6 +1912,9 @@ int domain_relinquish_resources(struct d
         /* fallthrough */
 
     case RELMEM_done:
+        ret = relinquish_memory(d, &d->page_list, PGT_l1_page_table);
+        if ( ret )
+            return ret;
         break;
 
     default:
xen/arch/x86/mm.c +6 -0
Index: 2008-05-08/xen/arch/x86/mm.c
===================================================================
@@ -1341,6 +1341,9 @@ static void free_l3_table(struct page_in
     l3_pgentry_t *pl3e;
     int           i;
 
+    if(d->arch.relmem == RELMEM_dom_l3)
+        return;
+
     pl3e = map_domain_page(pfn);
 
     for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
@@ -1364,6 +1367,9 @@ static void free_l4_table(struct page_in
     l4_pgentry_t *pl4e = page_to_virt(page);
     int           i;
 
+    if(d->arch.relmem == RELMEM_dom_l4)
+        return;
+
     for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
         if ( is_guest_l4_slot(d, i) )
             put_page_from_l4e(pl4e[i], pfn);
#13 lore raw
Quoted 11 lines · Jan Beulich

Indeed, the extended version below avoids this.

Quoted 6 lines

In actuality, since we know that we never have 'cross-domain' pagetable type references, we should actually be able to zap pagetable reference counts to zero. The only reason we don't do that right now is really because it provides good debugging info to see whether a domain's refcounts have got screwed up. But that would not prevent us doing something faster for NDEBUG builds, at least.

I still thought it'd be better to not simply zap the counts, but incrementally drop them using the proper interface:

Theoretically you can still race PIN_Lx_TABLE hypercalls from other dom0 VCPUs. Obviously that would only happen from a misbehaving dom0 though. I think this patch is a reasonable stopgap measure.

-- Keir