Skip to contentSkip to conversation

Make get_page_from_l1e refcount correctly on foreign pagetables.

PATCH In review Tim Deegan 4 participants 10 messages May 13, 2009 – Jun 3, 2009

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

PATCH 1/1 Make get_page_from_l1e refcount correctly on foreign pagetables.

Hypercalls from dom0 can end up doing resyncs on HVM guests' out-of-sync shadow pagetables. At that point the check against current->domain in get_page_from_l1e() triggers the typecount exemption for foreign mappings and a writeable typecount gets lost.

Make the foreign-domain check explicit by having get_page_from_l1e_for(), which understands both the dom whose right are being used and the dom whose pagetables are being updated. Most callers of get_page_from_l1e() have both the same (instead of one hard-coded to current->domain as before).

Analysis and fix from David Lively.

xen/arch/x86/mm.c +5 -5
diff -r 229d00b41a49 xen/arch/x86/mm.c
@@ -644,8 +644,8 @@
 
 
 int
-get_page_from_l1e(
-    l1_pgentry_t l1e, struct domain *d)
+get_page_from_l1e_for(
+    l1_pgentry_t l1e, struct domain *d, struct domain *target)
 {
     unsigned long mfn = l1e_get_pfn(l1e);
     struct page_info *page = mfn_to_page(mfn);
@@ -696,7 +696,7 @@
      * qemu-dm helper process in dom0 to map the domain's memory without
      * messing up the count of "real" writable mappings.) */
     okay = (((l1f & _PAGE_RW) && 
-             !(unlikely(paging_mode_external(d) && (d != curr->domain))))
+             !(unlikely((d != target) && paging_mode_external(d))))
             ? get_page_and_type(page, d, PGT_writable_page)
             : get_page(page, d));
     if ( !okay )
@@ -716,7 +716,7 @@
         {
             if ( (l1f & _PAGE_RW) &&
                  !(unlikely(paging_mode_external(d) &&
-                            (d != curr->domain))) )
+                            (d != target))) )
                 put_page_type(page);
             put_page(page);
             MEM_LOG("Attempt to change cache attributes of Xen heap page");
@@ -1493,7 +1493,7 @@
             return rc;
         }
 
-        if ( unlikely(!get_page_from_l1e(nl1e, FOREIGNDOM)) )
+        if ( unlikely(!get_page_from_l1e_for(nl1e, FOREIGNDOM, d)) )
             return page_unlock(l1pg), 0;
         
         adjust_guest_l1e(nl1e, d);
xen/arch/x86/mm/shadow/multi.c +1 -1
diff -r 229d00b41a49 xen/arch/x86/mm/shadow/multi.c
@@ -811,7 +811,7 @@
          (d != owner) &&
          IS_PRIV_FOR(d, owner))
     {
-        res = get_page_from_l1e(sl1e, owner);
+        res = get_page_from_l1e_for(sl1e, owner, d);
         SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx "
                        "which is owned by domain %d: %s\n",
                        d->domain_id, mfn_x(mfn), owner->domain_id,
xen/include/asm-x86/mm.h +9 -1
diff -r 229d00b41a49 xen/include/asm-x86/mm.h
@@ -165,8 +165,16 @@
 int  get_page(struct page_info *page, struct domain *domain);
 void put_page_type(struct page_info *page);
 int  get_page_type(struct page_info *page, unsigned long type);
-int  get_page_from_l1e(l1_pgentry_t l1e, struct domain *d);
+int  get_page_from_l1e_for(l1_pgentry_t l1e, struct domain *d,
+			   struct domain *target);
 void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d);
+
+
+static inline int get_page_from_l1e(l1_pgentry_t l1e, struct domain *d)
+{
+    return get_page_from_l1e_for(l1e, d, d);
+}
+
 
 static inline void put_page_and_type(struct page_info *page)
 {

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

#1 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

Tim Deegan <[email protected]> 13.05.09 18:07 >>>

Hypercalls from dom0 can end up doing resyncs on HVM guests' out-of-sync shadow pagetables. At that point the check against current->domain in get_page_from_l1e() triggers the typecount exemption for foreign mappings and a writeable typecount gets lost.

Quoted 6 lines

Make the foreign-domain check explicit by having get_page_from_l1e_for(), which understands both the dom whose right are being used and the dom whose pagetables are being updated. Most callers of get_page_from_l1e() have both the same (instead of one hard-coded to current->domain as before).

Analysis and fix from David Lively.

I have to admit that the change to mod_l1_entry() look suspicious to me - as I understand it, the third parameter of get_page_from_l1e_for() represents the target domain, and that's what FOREIGNDOM is to be used for.

Perhaps the whole thing gets more convoluted because of c/s 19383, which added a vcpu parameter for no apparent reason (current is used for that everywhere afaict).

Jan

author
#2 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

Quoted 12 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line

Tim Deegan <[email protected]> 13.05.09 18:07 >>>

Hypercalls from dom0 can end up doing resyncs on HVM guests' out-of-sync shadow pagetables. At that point the check against current->domain in get_page_from_l1e() triggers the typecount exemption for foreign mappings and a writeable typecount gets lost.

Make the foreign-domain check explicit by having get_page_from_l1e_for(), which understands both the dom whose right are being used and the dom whose pagetables are being updated. Most callers of get_page_from_l1e() have both the same (instead of one hard-coded to current->domain as before).

Quoted 5 lines
Quoted 1 line

Analysis and fix from David Lively.

I have to admit that the change to mod_l1_entry() look suspicious to me - as I understand it, the third parameter of get_page_from_l1e_for() represents the target domain, and that's what FOREIGNDOM is to be used for.

Possibly. IIUC get_page_from_l1e_for()'s first domain argument is the domain whose rights we are testing; so e.g. dom0 mapping domU memory uses FOREIGNDOM there to say "this should be domU's page". The second argument (whose pagetables are these) has always implicitly been "mine", i.e. current->domain. Again correct when dom0 maps domU's page.

In the case we're trying to fix, although current->domain is dom0 (who is making a shadow control hypercall) the pagetables belong to domU.

In the mod_l1_entry() call, get_page_from_l1e_for(nl1e, FOREIGNDOM, d) just gets us the old behaviour (since d == vcpu->domain).

Quoted 3 lines

Perhaps the whole thing gets more convoluted because of c/s 19383, which added a vcpu parameter for no apparent reason (current is used for that everywhere afaict).

That parameter is used so that dom0 can modify a PV domU's pagetables using the MMU ops (so dom0 tools can offline a suspect page without domU's help). In that case, I think the current patch actually fixes a latent bug in refcounting foreign mappings from the target domU to a third (HVM) domain, that was missed in cs 19383.

This is all pretty confusing, though, and Keir may correct me on any or all of it. :)

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Citrix Systems (R&D) Ltd.
[Company #02300071, SL9 0DZ, UK.]
#3 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctlyonforeign pagetables.

Quoted 9 lines
Quoted 1 line
Quoted 1 line

Tim Deegan <[email protected]> 14.05.09 10:22 >>>

Quoted 3 lines · Jan Beulich

Perhaps the whole thing gets more convoluted because of c/s 19383, which added a vcpu parameter for no apparent reason (current is used for that everywhere afaict).

That parameter is used so that dom0 can modify a PV domU's pagetables using the MMU ops (so dom0 tools can offline a suspect page without domU's help).

Hmm, now that you say that I do recall. Nevertheless, I can't spot any case where mod_lX_table() would be called with other than current as the vcpu argument. Is that perhaps only a future plan (and then c/s 19383 was maybe committed prematurely)? Or what am I overlooking?

Thanks, Jan

#4 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

In reply to Tim Deegan
Quoted 4 lines · Tim Deegan
Quoted 3 lines

Perhaps the whole thing gets more convoluted because of c/s 19383, which added a vcpu parameter for no apparent reason (current is used for that everywhere afaict).

Quoted 8 lines

That parameter is used so that dom0 can modify a PV domU's pagetables using the MMU ops (so dom0 tools can offline a suspect page without domU's help). In that case, I think the current patch actually fixes a latent bug in refcounting foreign mappings from the target domU to a third (HVM) domain, that was missed in cs 19383.

This is all pretty confusing, though, and Keir may correct me on any or all of it. :)

I wish I could. I need a damp towel wrapped round my head to understand the details of that code now. We may need to step back and refactor and rethink after 3.4. Hence my reluctance to mess with it now, as I fear introducing bugs more significant than latent stubdom issues.

-- Keir

author
#5 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

Quoted 5 lines · Keir Fraser
Quoted 2 lines

This is all pretty confusing, though, and Keir may correct me on any or all of it. :)

I wish I could. I need a damp towel wrapped round my head to understand the details of that code now.

You're not alone. Every time I look at that I need to start from scratch. It doesn't help that the put_page side also takes a domain pointer but has different semantics.

Quoted 3 lines

We may need to step back and refactor and rethink
after 3.4. Hence my reluctance to mess with it now, as I fear introducing bugs more significant than latent stubdom issues.

I think it'd be OK to leave this patch out if you're worried about it, especially if you're going to refactor this after 3.4. The crash it fixes hasn't been seen by anyone else, David has a heavily modified system, and the bug's been there since out-of-sync pagetables came in.

Cheers,

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Citrix Systems (R&D) Ltd.
[Company #02300071, SL9 0DZ, UK.]
#6 lore raw

RE: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

In reply to Keir Fraser
Quoted 12 lines · Keir Fraser
Quoted 10 lines · Tim Deegan
Quoted 3 lines

Perhaps the whole thing gets more convoluted because of c/s 19383, which added a vcpu parameter for no apparent reason (current is used for that everywhere afaict).

That parameter is used so that dom0 can modify a PV domU's pagetables using the MMU ops (so dom0 tools can offline a suspect page without domU's help). In that case, I think the current patch actually fixes a latent bug in refcounting foreign mappings from the target domU to a third (HVM) domain, that was missed in cs 19383.

Quoted 8 lines
Quoted 2 lines

This is all pretty confusing, though, and Keir may correct me on any or all of it. :)

I wish I could. I need a damp towel wrapped round my head to understand the details of that code now. We may need to step back and refactor and rethink
after 3.4. Hence my reluctance to mess with it now, as I fear introducing bugs more significant than latent stubdom issues.

Sorry for slow response.
Yes, that patch is for page ofline as stated by Tim. However, because it is too later to 3.4 release, the whole patchset is not in the tree in the end. I will re-send the patch once the unstable tree is open again, and we can discuss it again at that time.

-- jyh

Quoted 2 lines

-- Keir

#7 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

In reply to Tim Deegan
Quoted 4 lines · Tim Deegan
Quoted 3 lines

I have to admit that the change to mod_l1_entry() look suspicious to me - as I understand it, the third parameter of get_page_from_l1e_for() represents the target domain, and that's what FOREIGNDOM is to be used for.

Quoted 8 lines

Possibly. IIUC get_page_from_l1e_for()'s first domain argument is the domain whose rights we are testing; so e.g. dom0 mapping domU memory uses FOREIGNDOM there to say "this should be domU's page". The second argument (whose pagetables are these) has always implicitly been "mine", i.e. current->domain. Again correct when dom0 maps domU's page.

In the case we're trying to fix, although current->domain is dom0 (who is making a shadow control hypercall) the pagetables belong to domU.

Yes, there can be three domains involved: the one making the hypercall, the one who owns the PTE, and the one who owns the page being mapped into the PTE. I think some of the confusion around get_page_from_l1e() is that the domain argument is the page-owner not the PTE-owner. It would make sense for it to be the latter, and then as far as possible do the is-the-page-owner-valid checks hidden inside get_page_from_l1e(). The only fly in the ointment there is that FOREIGNDOM should only be permitted from mod_l1_entry(). Possibly that should be the only caller that directly accesses a more complex interface to get_page_from_l1e() (where the extra argument would be the page-owner, not the PTE-owner!).

-- Keir

#8 lore raw

Re: [PATCH] Make get_page_from_l1e refcount correctly onforeign pagetables.

Quoted 10 lines · Keir Fraser

Yes, there can be three domains involved: the one making the hypercall, the one who owns the PTE, and the one who owns the page being mapped into the PTE. I think some of the confusion around get_page_from_l1e() is that the domain argument is the page-owner not the PTE-owner. It would make sense for it to be the latter, and then as far as possible do the is-the-page-owner-valid checks hidden inside get_page_from_l1e(). The only fly in the ointment there is that FOREIGNDOM should only be permitted from mod_l1_entry(). Possibly that should be the only caller that directly accesses a more complex interface to get_page_from_l1e() (where the extra argument would be the page-owner, not the PTE-owner!).

Changeset 19708 should fix this satisfactorily, I hope.

-- Keir

#9 lore raw
In reply to Tim Deegan
Quoted 9 lines · Tim Deegan

Hypercalls from dom0 can end up doing resyncs on HVM guests' out-of-sync shadow pagetables. At that point the check against current->domain in get_page_from_l1e() triggers the typecount exemption for foreign mappings and a writeable typecount gets lost.

Make the foreign-domain check explicit by having get_page_from_l1e_for(), which understands both the dom whose right are being used and the dom whose pagetables are being updated. Most callers of get_page_from_l1e() have both the same (instead of one hard-coded to current->domain as before).

The patch doesn't apply to unstable/3.4. Further, the usage of domain pointers in get_page_from_l1e_for() is now pretty insane -- we have 'd', 'target', 'owner', and still a couple of references to 'current->domain'. Can we rename some of these, and/or remove redundant variables, and/or add a function comment explaining what is going on therein, and be really sure this won't bite us if we check it in so close to 3.4.0? I'm certainly nervous about it.

-- Keir