From: Marcelo Tosatti <marcelo-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org>
To: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
Cc: Marcelo Tosatti <marcelo-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org>,
kvm-devel
<kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org>
Subject: Re: [PATCH] Fix SMP shadow instantiation race
Date: Mon, 10 Dec 2007 14:12:09 -0500 [thread overview]
Message-ID: <20071210191208.GA15500@dmt> (raw)
In-Reply-To: <475D726A.2040901-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 2176 bytes --]
On Mon, Dec 10, 2007 at 07:07:54PM +0200, Avi Kivity wrote:
> Marcelo Tosatti wrote:
> >There is a race where VCPU0 is shadowing a pagetable entry while VCPU1
> >is updating it, which results in a stale shadow copy.
> >
> >Fix that by comparing the contents of the cached guest pte with the
> >current guest pte after write-protecting the guest pagetable.
> >
> >Attached program kvm_shadow_race.c demonstrates the problem.
> >
> >
>
> Where is it?
Attached.
> >Signed-off-by: Marcelo Tosatti <mtosatti-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
> >
> >
> >diff --git a/drivers/kvm/paging_tmpl.h b/drivers/kvm/paging_tmpl.h
> >index 72d4816..4fece01 100644
> >--- a/drivers/kvm/paging_tmpl.h
> >+++ b/drivers/kvm/paging_tmpl.h
> >@@ -66,6 +66,7 @@ struct guest_walker {
> > int level;
> > gfn_t table_gfn[PT_MAX_FULL_LEVELS];
> > pt_element_t pte;
> >+ gpa_t pte_gpa;
> >
>
> I think this needs to be an array like table_gfn[]. The guest may play
> with the pde (and upper entries) as well as the pte.
I was working under the assumption that the only significant bits of
upper entries (WRITEABLE and PRESENT) that can be changed by the guest
must be reflected first in the lower level pte's.
Isnt that a fair assumption to make?
> >+ kvm_read_guest(vcpu->kvm, walker->pte_gpa, &curr_pte,
> >sizeof(curr_pte));
> >+
> >+ if (curr_pte != walker->pte)
> >+ return 0;
> >+
> >
>
> 'return NULL'
>
> It would also be preferable to read the pte only if we shadowed the page
> just now. Perhaps pass the pte and the index to kvm_mmu_get_page()
> which would use them as a guard when the page is being shadowed:
>
> if (lookup page succeeds)
> return it
> shadow page
> write protect it
> if (guard check succeeds)
> return it
> else
> return NULL
>
> or perhaps have kvm_mmu_get_page() return an additional bool signifying
> it is a new page. but this is ugly.
>
> >
> >- ++vcpu->stat.pf_fixed;
> >+ if (shadow_pte)
> >+ ++vcpu->stat.pf_fixed;
> >
>
> This is a very rare case; it isn't worth being so accurate maintaining
> the statistics.
>
> --
> error compiling committee.c: too many arguments to function
[-- Attachment #2: kvm_shadow_race.c --]
[-- Type: text/plain, Size: 1502 bytes --]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <pthread.h>
struct thread_param {
void *mmaped_region;
int start;
int len;
};
void* do_read(void *arg)
{
char buf[4096];
void *pos, *end;;
struct thread_param *t = (struct thread_param *)arg;
sleep(1);
end = t->mmaped_region + t->len;
for (pos=t->mmaped_region; pos < end; pos += sizeof(buf))
memcpy(&buf, pos, sizeof(buf));
}
#define FLEN 128*1024*1024
int main(void)
{
int fd, err, i;
char buf[4096];
void *mmaped_region;
fd = open("/tmp/largefile", O_RDWR|O_CREAT);
if (!fd) {
perror("failed to create /tmp/largefile");
exit(0);
}
chmod("/tmp/largefile", S_IRUSR|S_IWUSR);
memset(buf, 0xf, sizeof(buf));
for (i=0; i<FLEN; i+=sizeof buf) {
err = write(fd, buf, sizeof(buf));
if (err < 0) {
perror("write");
exit(0);
}
}
mmaped_region = mmap(0, FLEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0);
if (mmaped_region == MAP_FAILED) {
perror("mmap");
exit(0);
}
for (i = 0; i < FLEN; i += FLEN/32) {
pthread_t thread;
struct thread_param *t = malloc(sizeof(struct thread_param));
t->mmaped_region = mmaped_region;
t->start = i;
t->len = FLEN/32;
if (pthread_create(&thread, NULL, do_read, t))
perror("pthread_create");
}
sleep(1);
i = mprotect(mmaped_region, FLEN, PROT_READ);
if (i < 0)
perror("mprotect");
return 0;
}
[-- Attachment #3: Type: text/plain, Size: 277 bytes --]
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
[-- Attachment #4: Type: text/plain, Size: 186 bytes --]
_______________________________________________
kvm-devel mailing list
kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/kvm-devel
next prev parent reply other threads:[~2007-12-10 19:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-10 16:19 [PATCH] Fix SMP shadow instantiation race Marcelo Tosatti
2007-12-10 17:07 ` Avi Kivity
[not found] ` <475D726A.2040901-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-12-10 19:12 ` Marcelo Tosatti [this message]
2007-12-10 21:27 ` Avi Kivity
[not found] ` <475DAF51.8060804-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-12-10 22:22 ` Marcelo Tosatti
2007-12-12 0:12 ` Marcelo Tosatti
2007-12-13 8:37 ` Avi Kivity
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071210191208.GA15500@dmt \
--to=marcelo-bw31mazkks3ytjvyw6ydsg@public.gmane.org \
--cc=avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org \
--cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox