Michael Abd-El-Malek
2008-Jan-31 14:57 UTC
[Xen-devel] Mapping another domain''s user memory
Hello, Is it possible to map another domain''s user-level memory into a different domain''s kernel? (For clarity, let the source domain be the one with the user-space pages that a destination domain wants to write to.) I tried to do that by first getting a grant for the user-space memory in the source domain as follows: page_start = user_buf; buffer_mfn = virt_to_mfn(page_start); gnttab_grant_foreign_access_ref(grant_ref, info->dev->otherend_id, buffer_mfn, 0); But the virt_to_mfn call asserted, in include/asm-x86_64/mach-xen/asm/ maddr.h:pfn_to_mfn. The assertion that failed was: BUG_ON(end_pfn && pfn >= end_pfn); So a few questions: 1) is it even possible to map a domain''s user-space page into another domain? 2) for the above code snippet to work, must I install some mappings in the source domain first? 3) does the blktap driver do something similar? I tried looking at the blktap driver, because it looked like it might help. But I''m new to the Xen memory management code so it was a bit difficult to follow. 4) I looked at fs/filemap.c:file_read_actor and how it calls kmap before calling __copy_to_user. Would calling kmap help? __copy_to_user is a bit of a mystery to me (granted, no pun intended, I didn''t dig deep enough into the assembly code to understand what it''s doing.) 5) any other suggestions? Thanks, Mike _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Daniel Stodden
2008-Jan-31 15:09 UTC
Re: [Xen-devel] Mapping another domain''s user memory
On Thu, 2008-01-31 at 09:57 -0500, Michael Abd-El-Malek wrote:> Hello, > > Is it possible to map another domain''s user-level memory into a > different domain''s kernel? (For clarity, let the source domain be the > one with the user-space pages that a destination domain wants to write > to.) > > I tried to do that by first getting a grant for the user-space memory > in the source domain as follows: > > page_start = user_buf; > buffer_mfn = virt_to_mfn(page_start); > gnttab_grant_foreign_access_ref(grant_ref, info->dev->otherend_id, > buffer_mfn, 0);this functions wants a guest page frame number, not the mfn, e.g. via page_to_pfn().> But the virt_to_mfn call asserted, in include/asm-x86_64/mach-xen/asm/ > maddr.h:pfn_to_mfn. > The assertion that failed was: BUG_ON(end_pfn && pfn >= end_pfn); > > So a few questions: > 1) is it even possible to map a domain''s user-space page into another > domain?yes, any, by dom0, or else via grants.> 2) for the above code snippet to work, must I install some mappings in > the source domain first?no, afaik it does not have to be mapped, just owned.> 3) does the blktap driver do something similar? I tried looking at the > blktap driver, because it looked like it might help. But I''m new to > the Xen memory management code so it was a bit difficult to follow.not sure about blktap, but {blk|net}front should give you examples of how the above code is supposed to be used. grants are typically created by the frontend, as the backend drivers won''t dedicate memory of the driver domain to that purpose. regards, daniel -- Daniel Stodden LRR - Lehrstuhl für Rechnertechnik und Rechnerorganisation Institut für Informatik der TU München D-85748 Garching http://www.lrr.in.tum.de/~stodden mailto:stodden@cs.tum.edu PGP Fingerprint: F5A4 1575 4C56 E26A 0B33 3D80 457E 82AE B0D8 735B _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Hi Mike, On Jan 31, 2008 2:57 PM, Michael Abd-El-Malek <mabdelmalek@cmu.edu> wrote:> I tried to do that by first getting a grant for the user-space memory > in the source domain as follows: > > page_start = user_buf; > buffer_mfn = virt_to_mfn(page_start); > gnttab_grant_foreign_access_ref(grant_ref, info->dev->otherend_id, > buffer_mfn, 0); > > But the virt_to_mfn call asserted, in include/asm-x86_64/mach-xen/asm/ > maddr.h:pfn_to_mfn. > The assertion that failed was: BUG_ON(end_pfn && pfn >= end_pfn);You have another problem here, because virt_to_mfn (and virt_to_page etc.) operate on kernel virtual addresses, where converting to a physical address effectively just involves subtracting an offset. User virtual addresses are handled differently, and you''ll have to walk the page table to find the PTE and from that obtain an MFN. To respond to Daniel''s point, from my reading of the code, the grant table entry takes a "GMFN", which is equivalent to an MFN on PV guests without shadow translation. Therefore it shouldn''t be necessary to translate this to a pseudo-physical frame number.> So a few questions: > 1) is it even possible to map a domain''s user-space page into another > domain?I don''t see any problem in principle, though you''ll have to make sure that the page is locked in memory.> 3) does the blktap driver do something similar? I tried looking at the > blktap driver, because it looked like it might help. But I''m new to > the Xen memory management code so it was a bit difficult to follow.blktap maps memory that was granted by another domain''s kernel (in the blkfront driver) into user-space. Therefore, on the destination side, this is probably more complicated than you need. However, if you did want to map the grant into user-space, you could use gntdev, which employs the same mechanism as blktap. Regards, Derek Murray. _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Michael Abd-El-Malek
2008-Feb-01 03:32 UTC
Re: [Xen-devel] Mapping another domain''s user memory
On Jan 31, 2008, at 11:16 AM, Derek Murray wrote:> On Jan 31, 2008 2:57 PM, Michael Abd-El-Malek <mabdelmalek@cmu.edu> > wrote: >> I tried to do that by first getting a grant for the user-space memory >> in the source domain as follows: >> >> page_start = user_buf; >> buffer_mfn = virt_to_mfn(page_start); >> gnttab_grant_foreign_access_ref(grant_ref, info->dev- >> >otherend_id, >> buffer_mfn, 0); >> >> But the virt_to_mfn call asserted, in include/asm-x86_64/mach-xen/ >> asm/ >> maddr.h:pfn_to_mfn. >> The assertion that failed was: BUG_ON(end_pfn && pfn >= end_pfn); > > You have another problem here, because virt_to_mfn (and virt_to_page > etc.) operate on kernel virtual addresses, where converting to a > physical address effectively just involves subtracting an offset. User > virtual addresses are handled differently, and you''ll have to walk the > page table to find the PTE and from that obtain an MFN.Thanks for your help Derek. Fortunately I didn''t have to muck around with the page tables. get_user_pages already does this. So I called that function to get the relevant pages for the user pointer, kmap''ed those pages, got the virtual address using virt_to_mfn, and Xen''s grants worked. Cheers, Mike _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel