From: Song Xiang Subject: Re: [PATCH] x86/hvm/pmtimer: improving scalability of virtual time update on Xen 4.0.0 Date: Wed, 17 Nov 2010 23:13:43 +0000 Message-ID: <865E9996-A482-40FF-AB64-A2638B487C3C@gmail.com> References: <12AF73E6-A34C-46BC-BFC1-2B7993AAF069@gmail.com> <20101117144745.GG21112@whitby.uk.xensource.com> Mime-Version: 1.0 (Apple Message framework v936) Content-Type: text/plain; charset=GB2312; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20101117144745.GG21112@whitby.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Tim Deegan Cc: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org It can be as follows: @@ -90,8 +90,8 @@ static void pmt_update_time(PMTState *s) /* Update the timer */ curr_gtime =3D hvm_get_guest_time(s->vcpu); - s->pm.tmr_val +=3D ((curr_gtime - s->last_gtime) * s->scale) >> 32; - s->pm.tmr_val &=3D TMR_VAL_MASK; + *(volatile uint32_t *)&s->pm.tmr_val =3D (s->pm.tmr_val + + (((curr_gtime - s->last_gtime) * s->scale) >> 32)) & =20 TMR_VAL_MASK; s->last_gtime =3D curr_gtime; /* If the counter's MSB has changed, set the status bit */ but it seems gcc (gcc 4.4.4) has omitted the statement s->pm.tmr_val =20 &=3D TMR_VAL_MASK; in the object file =D4=DA 2010-11-17=A3=AC=CF=C2=CE=E72:47=A3=AC Tim Deegan =D0=B4=B5=C0=A3=BA= > At 21:39 +0000 on 17 Nov (1290029945), Song Xiang wrote: >> diff --git a/xen/arch/x86/hvm/pmtimer.c b/xen/arch/x86/hvm/pmtimer.c >> index 48fe26a..587944f 100644 >> --- a/xen/arch/x86/hvm/pmtimer.c >> +++ b/xen/arch/x86/hvm/pmtimer.c >> @@ -90,7 +90,7 @@ static void pmt_update_time(PMTState *s) >> >> /* Update the timer */ >> curr_gtime =3D hvm_get_guest_time(s->vcpu); >> - s->pm.tmr_val +=3D ((curr_gtime - s->last_gtime) * s->scale) >> =20= >> 32; >> + *(volatile uint32_t *)&s->pm.tmr_val =3D s->pm.tmr_val + =20 >> (((curr_gtime - s->last_gtime) * s->scale) >> 32); >> s->pm.tmr_val &=3D TMR_VAL_MASK; >> s->last_gtime =3D curr_gtime; > > That doesn't make it an atomic update! The variable is still written > to twice. :) You need to calculate the new tmr_val in a scratch > variable, and then write it back once at the end of the function. > (And no 'volatile' wll be necessary). > > Cheers, > > Tim. > >> @@ -206,10 +206,19 @@ static int handle_pmt_io( >> >> if ( dir =3D=3D IOREQ_READ ) >> { >> - spin_lock(&s->lock); >> - pmt_update_time(s); >> - *val =3D s->pm.tmr_val; >> - spin_unlock(&s->lock); >> + /* >> + * if acquired the PMTState lock then update the time >> + * else other vcpu is updating it ,it should be up to date. >> + */ >> + if (spin_trylock(&s->lock)) { >> + pmt_update_time(s); >> + *val =3D s->pm.tmr_val; >> + spin_unlock(&s->lock); >> + } >> + else { >> + spin_barrier(&s->lock); >> + *val =3D s->pm.tmr_val; >> + } >> return X86EMUL_OKAY; >> } >> > >> _______________________________________________ >> Xen-devel mailing list >> Xen-devel@lists.xensource.com >> http://lists.xensource.com/xen-devel > > > --=20 > Tim Deegan > Principal Software Engineer, Xen Platform Team > Citrix Systems UK Ltd. (Company #02937203, SL9 0BG)