From: Kieran Mansley Subject: Re: [PATCH] grant table and bogus mfns Date: Tue, 13 Nov 2007 16:40:04 +0000 Message-ID: <1194972004.4124.31.camel@moonstone.uk.level5networks.com> References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-fuHSpmGs9fbbYR7SZGl4" Return-path: In-Reply-To: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Keir Fraser Cc: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --=-fuHSpmGs9fbbYR7SZGl4 Content-Type: text/plain Content-Transfer-Encoding: 7bit On Tue, 2007-11-13 at 04:48 +0000, Keir Fraser wrote: > On 12/11/07 09:35, "Kieran Mansley" wrote: > > > 1) dom0 does a grant op for a page of I/O memory; at this stage no > > different to a normal grant. > > 2) grant reference passed (e.g. through xenstore) to domU > > 3) domU performs a map operation on that grant > > 4) hypervisor notices that the grant is for an I/O memory page and > > instead of mapping it to a domU virtual address it instead sets up the > > I/O mem permissions for that domain to access the region (ie. calls > > iomem_permit_access()) > > 5) domU can then call ioremap() to get a kernel virtual address for the > > I/O memory region, and access it as normal. > > I didn't realise this was how it worked. I think it's a bad idea -- mapping > the grantref should map the I/O page. The mapping domain's io capabilities > should not be affected. Apart from being the obvious semantics for > map_grant, I agree that mapping the I/O page when you map the grant ref makes for a much better interface. I'd just obviously got the wrong end of the stick and thought that you wanted it the other way when we first discussed this, and it hadn't occurred to me that doing the map without first getting the right permissions was sane. Attached is pair of patches against the current xen- unstable.hg/linux-2.6.18-xen.hg (i.e. instead of not on top of the other patch I posted in this thread) that should do the I/O page map when the grant is mapped. The I/O capabilities of the domain are no longer modified. It seems to work for me, is definitely an improvement in the API, and should fix the original bug that was posted. I added a GNTMAP_nocache flag to match the GNTMAP_readonly etc flags so that you can get the equivalent behaviour of ioremap_nocache() vs ioremap(). The grant is mapped in as normal; e.g.: struct gnttab_map_grant_ref op; gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map | GNTMAP_nocache, gnt_ref, dev->otherend_id); BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1)); I'm slightly surprised that without the call to iomem_permit_access() I don't trip over the iomem_access_permitted() check in xen/arch/x86/mm.c:get_page_from_l1e() when the grant is mapped. I hope this doesn't indicate that I've missed something. Thanks Kieran --=-fuHSpmGs9fbbYR7SZGl4 Content-Disposition: attachment; filename=iomem_page_test_fix Content-Type: text/x-patch; name=iomem_page_test_fix; charset=UTF-8 Content-Transfer-Encoding: 7bit diff -r e40591366a0f xen/arch/x86/mm.c --- a/xen/arch/x86/mm.c Mon Nov 12 09:53:14 2007 +0000 +++ b/xen/arch/x86/mm.c Tue Nov 13 16:22:08 2007 +0000 @@ -849,12 +849,14 @@ void put_page_from_l1e(l1_pgentry_t l1e, void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d) { unsigned long pfn = l1e_get_pfn(l1e); - struct page_info *page = mfn_to_page(pfn); + struct page_info *page; struct domain *e; struct vcpu *v; if ( !(l1e_get_flags(l1e) & _PAGE_PRESENT) || !mfn_valid(pfn) ) return; + + page = mfn_to_page(pfn); e = page_get_owner(page); @@ -2772,6 +2774,8 @@ int create_grant_host_mapping( l1e_add_flags(pte,_PAGE_USER); if ( !(flags & GNTMAP_readonly) ) l1e_add_flags(pte,_PAGE_RW); + if ( (flags & GNTMAP_nocache) ) + l1e_add_flags(pte,_PAGE_PCD); if ( flags & GNTMAP_contains_pte ) return create_grant_pte_mapping(addr, pte, current); diff -r e40591366a0f xen/common/grant_table.c --- a/xen/common/grant_table.c Mon Nov 12 09:53:14 2007 +0000 +++ b/xen/common/grant_table.c Tue Nov 13 15:50:01 2007 +0000 @@ -198,7 +198,6 @@ __gnttab_map_grant_ref( int handle; unsigned long frame = 0; int rc = GNTST_okay; - int is_iomem = 0; struct active_grant_entry *act; struct grant_mapping *mt; grant_entry_t *sha; @@ -329,24 +328,38 @@ __gnttab_map_grant_ref( spin_unlock(&rd->grant_table->lock); - if ( op->flags & GNTMAP_host_map ) - { - /* Could be an iomem page for setting up permission */ - if ( is_iomem_page(frame) ) - { - is_iomem = 1; - if ( iomem_permit_access(ld, frame, frame) ) - { - gdprintk(XENLOG_WARNING, - "Could not permit access to grant frame " - "%lx as iomem\n", frame); + /* Could be an iomem page for remapping */ + if ( rd->domain_id == 0 && + is_iomem_page(frame) && + iomem_access_permitted(rd, frame, frame) ) + { + if ( op->flags & GNTMAP_host_map ) + { + /* Reference counting for in-range I/O pages. */ + if ( mfn_valid(frame) && + !((op->flags & GNTMAP_readonly) ? + get_page(mfn_to_page(frame), rd) : + get_page_and_type(mfn_to_page(frame), rd, + PGT_writable_page))) { + if ( !rd->is_dying ) + gdprintk(XENLOG_WARNING, + "Could not pin grant frame %lx\n", frame); rc = GNTST_general_error; goto undo_out; } - } - } - - if ( !is_iomem ) + + rc = create_grant_host_mapping(op->host_addr, frame, op->flags); + if ( rc != GNTST_okay ) + { + if ( mfn_valid(frame) && + !(op->flags & GNTMAP_readonly) ) + put_page_type(mfn_to_page(frame)); + put_page(mfn_to_page(frame)); + goto undo_out; + } + } + } + else { if ( unlikely(!mfn_valid(frame)) || unlikely(!((op->flags & GNTMAP_readonly) ? @@ -527,12 +540,6 @@ __gnttab_unmap_common( op->flags)) < 0 ) goto unmap_out; } - else if ( is_iomem_page(op->frame) && - iomem_access_permitted(ld, op->frame, op->frame) ) - { - if ( (rc = iomem_deny_access(ld, op->frame, op->frame)) < 0 ) - goto unmap_out; - } ASSERT(act->pin & (GNTPIN_hstw_mask | GNTPIN_hstr_mask)); op->map->flags &= ~GNTMAP_host_map; @@ -607,10 +614,25 @@ __gnttab_unmap_common_complete(struct gn goto unmap_out; } - if ( op->flags & GNTMAP_readonly ) - put_page(mfn_to_page(op->frame)); - else - put_page_and_type(mfn_to_page(op->frame)); + if ( rd->domain_id == 0 && + is_iomem_page(op->frame) ) + { + /* Only do refcounting on in-range mfns */ + if ( mfn_valid(op->frame) ) + { + if ( op->flags & GNTMAP_readonly ) + put_page(mfn_to_page(op->frame)); + else + put_page_and_type(mfn_to_page(op->frame)); + } + } + else + { + if ( op->flags & GNTMAP_readonly ) + put_page(mfn_to_page(op->frame)); + else + put_page_and_type(mfn_to_page(op->frame)); + } } if ( (op->map->flags & (GNTMAP_device_map|GNTMAP_host_map)) == 0 ) @@ -1592,7 +1614,6 @@ gnttab_release_mappings( struct domain *rd; struct active_grant_entry *act; struct grant_entry *sha; - int rc; BUG_ON(!d->is_dying); @@ -1651,9 +1672,14 @@ gnttab_release_mappings( BUG_ON(!(act->pin & GNTPIN_hstw_mask)); act->pin -= GNTPIN_hstw_inc; - if ( is_iomem_page(act->frame) && - iomem_access_permitted(rd, act->frame, act->frame) ) - rc = iomem_deny_access(rd, act->frame, act->frame); + if ( rd->domain_id == 0 && + is_iomem_page(act->frame) ) + { + /* Only do reference count on in-range mfns */ + if( mfn_valid(act->frame) ) + gnttab_release_put_page_and_type + (mfn_to_page(act->frame)); + } else gnttab_release_put_page_and_type(mfn_to_page(act->frame)); } diff -r e40591366a0f xen/include/public/grant_table.h --- a/xen/include/public/grant_table.h Mon Nov 12 09:53:14 2007 +0000 +++ b/xen/include/public/grant_table.h Tue Nov 13 11:26:26 2007 +0000 @@ -380,6 +380,9 @@ DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_and #define _GNTMAP_contains_pte (4) #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte) +#define _GNTMAP_nocache (5) +#define GNTMAP_nocache (1<<_GNTMAP_nocache) + /* * Values for error status returns. All errors are -ve. */ --=-fuHSpmGs9fbbYR7SZGl4 Content-Disposition: attachment; filename=add_nocache_grant_flag Content-Type: text/x-patch; name=add_nocache_grant_flag; charset=UTF-8 Content-Transfer-Encoding: 7bit diff -r 4c96da4993b8 include/xen/interface/grant_table.h --- a/include/xen/interface/grant_table.h Mon Nov 12 10:17:44 2007 +0000 +++ b/include/xen/interface/grant_table.h Tue Nov 13 16:20:54 2007 +0000 @@ -380,6 +380,9 @@ DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_and #define _GNTMAP_contains_pte (4) #define GNTMAP_contains_pte (1<<_GNTMAP_contains_pte) +#define _GNTMAP_nocache (5) +#define GNTMAP_nocache (1<<_GNTMAP_nocache) + /* * Values for error status returns. All errors are -ve. */ --=-fuHSpmGs9fbbYR7SZGl4 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=-fuHSpmGs9fbbYR7SZGl4--