transferring ownership of domheap pages
I'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a free_domheap_pages followed by an alloc_domheap_pages (primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.
Any problems to watch out for?
Thanks,
Dan
P.S. Only has to work on 64-bit Xen if that matters.
Quoted 13 lines
From: Dan Magenheimer
Sent: Friday, February 13, 2009 8:59 AMI'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a
free_domheap_pagesfollowed by analloc_domheap_pages(primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.
Quoted 5 lines
Any problems to watch out for?
Thanks,
Dan
It's doable. I guess gnttab_transfer could be referenced similar to your purpose, but I didn't look into detail. :-)
Thanks,
Kevin
Here's a first cut. Works only from anon to non-anon or non-anon to anon (not non-anon1 to non-anon2).
Quoted 17 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Thursday, February 12, 2009 5:59 PM
To: Xen-Devel (E-mail)
Subject: transferring ownership of domheap pagesI'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a
free_domheap_pagesfollowed by analloc_domheap_pages(primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.
Quoted 7 lines
Any problems to watch out for?
Thanks,
Dan
P.S. Only has to work on 64-bit Xen if that matters.
xen/common/page_alloc.c +47 -0
@@ -931,6 +934,53 @@ void free_domheap_pages(struct page_infoput_domain (d); } +struct page_info *reassign_domheap_pages (struct domain *new, +struct page_info *pg,unsigned int order) +{ +struct domain *old =page_get_owner (pg); +struct domain *lock = (old !=NULL ) ? old : new; +int npages =1 << order; +int i; + +ASSERT (!in_irq ()); +ASSERT (!is_xen_heap_page (pg)); +ASSERT ((old !=NULL ) || (new !=NULL )); +if ( (new !=NULL ) && (new->tot_pages + npages > new->max_pages) ) +return NULL ; +spin_lock_recursive (&lock->page_alloc_lock); +if ( old !=NULL ) + { +ASSERT (old->tot_pages > npages); + old->tot_pages -=1 << order; + } +if ( new !=NULL ) + { +ASSERT (new->tot_pages !=0 ); + new->tot_pages += npages; + } +for ( i =0 ; i < (1 << order); i++ ) + { +if ( old !=NULL ) + { +ASSERT (page_get_owner (&pg[i])==old); +ASSERT (pg[i].count_info & (PGC_allocated |1 )); + pg[i].count_info &= ~(PGC_allocated |1 ); +page_list_del (&pg[i], &old->page_list); + } +page_set_owner (&pg[i], new); +wmb ();/* Domain pointer must be visible before updating refcnt. */ +if ( new !=NULL ) + { +ASSERT (page_get_owner (&pg[i])==NULL ); +ASSERT ((pg[i].count_info & ~(PGC_allocated |1 )) ==0 ); + pg[i].count_info = PGC_allocated |1 ; +page_list_add_tail (&pg[i], &new->page_list); + } + } +spin_unlock_recursive (&lock->page_alloc_lock); +return pg; +} +unsigned long avail_domheap_pages_region (unsigned int node,unsigned int min_width,unsigned int max_width) {
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Yeah, that's pretty... optimistic. ;-) Use steal_page() plus whatever bits of gnttab_transfer() you think you might need. If you can cleanly abstract that into a common function all the better, but you probably won't need much code in addition to steal_page() anyway.
-- Keir
Quoted 27 lines · Dan Magenheimer
Here's a first cut. Works only from anon to non-anon or non-anon to anon (not non-anon1 to non-anon2).
Quoted 24 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Thursday, February 12, 2009 5:59 PM
To: Xen-Devel (E-mail)
Subject: transferring ownership of domheap pagesI'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a
free_domheap_pagesfollowed by analloc_domheap_pages(primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.Any problems to watch out for?
Thanks,
Dan
P.S. Only has to work on 64-bit Xen if that matters.
Hmm... it looks like steal_page() only works one way, from owned-by-d to anonymous, true? I need the other direction as well. Other than that, the only difference I see is that page->count_info is being cleared/set in a cmpxchg loop; I assume this is because multiple domains might be contending to use/unuse the page? I wonder if I need that given that in my case the page is never addressed by any domain, just used inside the hypervisor, and the owned-by-d is mostly for accounting purposes.
Quoted 1 line
Yeah, that's pretty... optimistic. ;-)
What else do you see that is missing in my first cut? Do you see any other code there that's just wrong?
Thanks,
Dan
Quoted 50 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Friday, February 13, 2009 1:54 AM
To: Dan Magenheimer; Xen-Devel (E-mail)
Subject: Re: [Xen-devel] RE: transferring ownership of domheap pagesYeah, that's pretty... optimistic. ;-) Use
steal_page()plus whatever bits
ofgnttab_transfer()you think you might need. If you can cleanly abstract
that into a common function all the better, but you probably won't need much
code in addition tosteal_page()anyway.-- Keir
Quoted 27 lines · Dan Magenheimer
Here's a first cut. Works only from anon to non-anon or non-anon to anon (not non-anon1 to non-anon2).
Quoted 24 lines
-----Original Message-----
From: Dan Magenheimer
Sent: Thursday, February 12, 2009 5:59 PM
To: Xen-Devel (E-mail)
Subject: transferring ownership of domheap pagesI'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a
free_domheap_pagesfollowed by analloc_domheap_pages(primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.Any problems to watch out for?
Thanks,
Dan
P.S. Only has to work on 64-bit Xen if that matters.
Quoted 4 lines · Dan Magenheimer
Quoted 1 line
Yeah, that's pretty... optimistic. ;-)
What else do you see that is missing in my first cut? Do you see any other code there that's just wrong?
You can't just assert that count_info == PGC_allocated|1, for example. You need to be careful that mappings aren't created or destroyed while you are changing the ownership. Steal_page() does that. Once a page is anonymous, transferring it to a domain is easier, since mappings can't be created/destroyed while it is anonymous. So steal_page() just does the hard transition for you (non-anon -> anon). A benefit of using steal_page() is that we will maintain that for other callers -- a private implementation in tmem risks getting subtly unsafely broken every now and then.
-- Keir
Quoted 2 lines
A benefit of using
steal_page()is
that we will maintain that for other callers
OK, I'll use steal_page(), though I think it is overkill as the pages I am dealing with are not addressable outside of the hypervisor, so there aren't any mappings being created and destroyed.
I still need code for the other direction though:
I have an anonymous page (on my own private "free" page_list) and I want it to be owned by a domain, but I don't want to free it and then alloc it (because I might not get the same page back).
Does the attached patch look good? Are there any races that I am missing? Is the name ("donate_page" opposite of "steal_page") OK?
Thanks,
Dan
Quoted 30 lines
-----Original Message-----
From: Keir Fraser [mailto:[email protected]]
Sent: Friday, February 13, 2009 8:23 AM
To: Dan Magenheimer; Xen-Devel (E-mail)
Subject: Re: [Xen-devel] RE: transferring ownership of domheap pagesQuoted 4 lines · Dan Magenheimer
Quoted 1 line
Yeah, that's pretty... optimistic. ;-)
What else do you see that is missing in my first cut? Do you see any other code there that's just wrong?
You can't just assert that
count_info== PGC_allocated|1, for example. You
need to be careful that mappings aren't created or destroyed while you are
changing the ownership.Steal_page()does that. Once a page is anonymous,
transferring it to a domain is easier, since mappings can't be created/destroyed while it is anonymous. Sosteal_page()just does the hard
transition for you (non-anon -> anon). A benefit of usingsteal_page()is
that we will maintain that for other callers -- a private implementation in
tmem risks getting subtly unsafely broken every now and then.
Quoted 9 lines
-- Keir
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
xen/arch/x86/mm.c +33 -0
@@ -3469,6 +3469,39 @@ int replace_grant_host_mapping(return rc; } +int donate_page ( +struct domain *d,struct page_info *page,unsigned int memflags) +{ +spin_lock (&d->page_alloc_lock); + +if (is_xen_heap_page (page) || (page_get_owner (page) !=NULL ) ) + goto fail; + +if ( page->count_info & ~(PGC_allocated |1 ) ) + goto fail; + +if ( !(memflags & MEMF_no_refcount) ) + { +if ( d->tot_pages >= d->max_pages ) + goto fail; + d->tot_pages++; + } + + page->count_info = PGC_allocated |1 ; +page_set_owner (page, d); +page_list_add_tail (page,&d->page_list); + +spin_unlock (&d->page_alloc_lock); +return 0 ; + + fail: +spin_unlock (&d->page_alloc_lock); +MEM_LOG ("Bad page %p: ed=%p(%u), sd=%p, caf=%08lx, taf=%" PRtype_info, + (void *)page_to_mfn (page), d, d->domain_id, +page_get_owner (page), page->count_info, page->u.inuse.type_info); +return -1 ; +} +int steal_page (struct domain *d,struct page_info *page,unsigned int memflags) {
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Quoted 7 lines · Dan Magenheimer
Quoted 2 lines
A benefit of using
steal_page()is
that we will maintain that for other callersOK, I'll use
steal_page(), though I think it is overkill as the pages I am dealing with are not addressable outside of the hypervisor, so there aren't any mappings being created and destroyed.
If you're sure of that then indeed you don't need steal_page(). But usually you could not be sure of that if the page owner is non-NULL -- the whole point of that is to allow the owner to make mappings as it likes. If you've got extra mechanism to ensure your extra restrictions hold then you will know better than me whether your previous patch is correct or not.
Quoted 9 lines
I still need code for the other direction though:
I have an anonymous page (on my own private "free"page_list) and I want it to be owned by a domain, but I don't want to free it and then alloc it (because I might not get the same page back).Does the attached patch look good? Are there any races that I am missing? Is the name ("
donate_page" opposite of "steal_page") OK?
Roughly. You do need to check d->is_dying so you don't race domain destruction. Look at share_xen_page_with_guest() -- your function is sort of a subset of that.
-- Keir
Quoted 11 lines · Dan Magenheimer
I'd like to have routines to transfer ownership of a page both from domain D1 to anonymous and from anonymous to domain D2, without the necessity of doing a
free_domheap_pagesfollowed by analloc_domheap_pages(primarily because I don't want to scrub the page as I know it will be immediately overwritten by the new owner). The anon-to-D2 would fail (return NULL) if D2 is fully allocated.Any problems to watch out for?
See (and use) mm.c:steal_page(). Only works of course if D1 has no mappings of the page.
-- Keir