From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrea Arcangeli Subject: Re: unit tests and get_user_pages_ptes_fast() Date: Mon, 4 Oct 2010 15:40:52 +0200 Message-ID: <20101004134052.GQ26357@random.random> References: <4CA99FE0.7070709@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: KVM list To: Avi Kivity Return-path: Received: from mx1.redhat.com ([209.132.183.28]:48141 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755837Ab0JDNky (ORCPT ); Mon, 4 Oct 2010 09:40:54 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o94Der17012508 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 4 Oct 2010 09:40:53 -0400 Content-Disposition: inline In-Reply-To: <4CA99FE0.7070709@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Hi Avi, On Mon, Oct 04, 2010 at 11:35:28AM +0200, Avi Kivity wrote: > During the last kvm forum, I described a unit test framework that can > help test the kvm APIs. Briefly, it starts a process in host userspace, > which sets up a memory slot mapping gpa 0:3G to hva 0:3G. It then sets > up guest registers for unpaged protected mode (or paged protected mode > with 1:1 mapping). The effect is that we have a 1:1 gva->hva > translation, and can use KVM_RUN to run host code in guest mode. > > There is a snag however. kvm calls get_user_pages_fast(.write = 1), and > the host process maps its code pages read-only. > > The way I'd like to work around this is to map read-only accesses to > read-only pages as read-only. This also prevents ksm cow pages from > being broken by read accesses. It can also be used to get the page size > for transparent huge pages (and later hugetlbfs too). > > So, for a read fault we do: > > pte_t pte; > get_user_pages_ptes_fast(..., page, &pte, 1, .write = 0) > ... > if (pte_transhuge(pte)) // or however it's called > ... > ... > if (pte_write(pte)) > map writeable > else > map readonly > > Any snags? or alternatives? I think we tried to do this write=0 before, it provides benefits for swapins from swapcache too (after the page is unmapped but the swapcache is present and uptodate and clean). If I recall correctly the only obstacle was the fact the out of sync code wants to map the spte writable if it can, but if we use write=0 in gup, it won't know if it can. If it'd wait a real write access from guest, we'd risk creating a spte wrprotect fault for nothing (it should only write a write access from the guest if the linux VM solves the page fault with a readonly pte, even the minor-read-fault from exclusive swapcache will not set the dirty bit but it will still set the pte_write on the pte so the out of sync code should take advantage of that information in the host pte). In the past you suggested some TRY_WRITE operation instead of only read/write. We just need to pass the write bit of the pte somehow to the gup caller. Full agreement that once we use write=0 for read faults your problem with the debugging userland running in guest mode will have better chance to work too :). Andrea