From: "Jan Beulich" Subject: [PATCH] x86/vHPET: use rwlock instead of simple one Date: Thu, 07 Aug 2014 09:40:44 +0100 Message-ID: <53E357AC020000780002A0C1@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7F4DC29C.0__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XFJFg-0000iU-DO for xen-devel@lists.xenproject.org; Thu, 07 Aug 2014 08:40:48 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part7F4DC29C.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This namely benefits guests heavily reading the main counter, but not touching the HPET much otherwise. Note that due to the way hpet_get_comparator() works hpet_read() has to special cases reads from the comparator registers and use a write lock there instead of the read one used for all other registers. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/hpet.c +++ b/xen/arch/x86/hvm/hpet.c @@ -77,7 +77,7 @@ =20 static inline uint64_t hpet_read_maincounter(HPETState *h, uint64_t = guest_time) { - ASSERT(spin_is_locked(&h->lock)); + ASSERT(rw_is_locked(&h->lock)); =20 if ( hpet_enabled(h) ) return guest_time + h->mc_offset; @@ -91,6 +91,8 @@ static uint64_t hpet_get_comparator(HPET uint64_t comparator; uint64_t elapsed; =20 + ASSERT(rw_is_write_locked(&h->lock)); + comparator =3D h->hpet.comparator64[tn]; if ( hpet_enabled(h) && timer_is_periodic(h, tn) ) { @@ -179,16 +181,24 @@ static int hpet_read( goto out; } =20 - spin_lock(&h->lock); + result =3D addr < HPET_Tn_CMP(0) || + ((addr - HPET_Tn_CMP(0)) % (HPET_Tn_CMP(1) - HPET_Tn_CMP(0)))= > 7; + if ( result ) + read_lock(&h->lock); + else + write_lock(&h->lock); =20 val =3D hpet_read64(h, addr, guest_time_hpet(h)); =20 + if ( result ) + read_unlock(&h->lock); + else + write_unlock(&h->lock); + result =3D val; if ( length !=3D 8 ) result =3D (val >> ((addr & 7) * 8)) & ((1ULL << (length * 8)) - = 1); =20 - spin_unlock(&h->lock); - out: *pval =3D result; return X86EMUL_OKAY; @@ -198,7 +208,7 @@ static void hpet_stop_timer(HPETState *h uint64_t guest_time) { ASSERT(tn < HPET_TIMER_NUM); - ASSERT(spin_is_locked(&h->lock)); + ASSERT(rw_is_write_locked(&h->lock)); TRACE_1D(TRC_HVM_EMUL_HPET_STOP_TIMER, tn); destroy_periodic_time(&h->pt[tn]); /* read the comparator to get it updated so a read while stopped will @@ -218,7 +228,7 @@ static void hpet_set_timer(HPETState *h, unsigned int oneshot; =20 ASSERT(tn < HPET_TIMER_NUM); - ASSERT(spin_is_locked(&h->lock)); + ASSERT(rw_is_write_locked(&h->lock)); =20 if ( (tn =3D=3D 0) && (h->hpet.config & HPET_CFG_LEGACY) ) { @@ -304,7 +314,7 @@ static int hpet_write( if ( hpet_check_access_length(addr, length) !=3D 0 ) goto out; =20 - spin_lock(&h->lock); + write_lock(&h->lock); =20 guest_time =3D guest_time_hpet(h); old_val =3D hpet_read64(h, addr, guest_time); @@ -473,7 +483,7 @@ static int hpet_write( #undef set_start_timer #undef set_restart_timer =20 - spin_unlock(&h->lock); + write_unlock(&h->lock); =20 out: return X86EMUL_OKAY; @@ -499,7 +509,7 @@ static int hpet_save(struct domain *d, h int rc; uint64_t guest_time; =20 - spin_lock(&hp->lock); + write_lock(&hp->lock); guest_time =3D guest_time_hpet(hp); =20 /* Write the proper value into the main counter */ @@ -545,7 +555,7 @@ static int hpet_save(struct domain *d, h rec->timers[2].cmp =3D hp->hpet.comparator64[2]; } =20 - spin_unlock(&hp->lock); + write_unlock(&hp->lock); =20 return rc; } @@ -558,12 +568,12 @@ static int hpet_load(struct domain *d, h uint64_t guest_time; int i; =20 - spin_lock(&hp->lock); + write_lock(&hp->lock); =20 /* Reload the HPET registers */ if ( _hvm_check_entry(h, HVM_SAVE_CODE(HPET), HVM_SAVE_LENGTH(HPET), = 1) ) { - spin_unlock(&hp->lock); + write_unlock(&hp->lock); return -EINVAL; } =20 @@ -604,7 +614,7 @@ static int hpet_load(struct domain *d, h if ( timer_enabled(hp, i) ) hpet_set_timer(hp, i, guest_time); =20 - spin_unlock(&hp->lock); + write_unlock(&hp->lock); =20 return 0; } @@ -618,7 +628,7 @@ void hpet_init(struct vcpu *v) =20 memset(h, 0, sizeof(HPETState)); =20 - spin_lock_init(&h->lock); + rwlock_init(&h->lock); =20 h->stime_freq =3D S_TO_NS; =20 @@ -648,7 +658,7 @@ void hpet_deinit(struct domain *d) int i; HPETState *h =3D domain_vhpet(d); =20 - spin_lock(&h->lock); + write_lock(&h->lock); =20 if ( hpet_enabled(h) ) { @@ -659,7 +669,7 @@ void hpet_deinit(struct domain *d) hpet_stop_timer(h, i, guest_time); } =20 - spin_unlock(&h->lock); + write_unlock(&h->lock); } =20 void hpet_reset(struct domain *d) --- a/xen/arch/x86/hvm/vpt.c +++ b/xen/arch/x86/hvm/vpt.c @@ -512,10 +512,10 @@ void pt_adjust_global_vcpu_target(struct pt_adjust_vcpu(&pl_time->vrtc.pt, v); spin_unlock(&pl_time->vrtc.lock); =20 - spin_lock(&pl_time->vhpet.lock); + write_lock(&pl_time->vhpet.lock); for ( i =3D 0; i < HPET_TIMER_NUM; i++ ) pt_adjust_vcpu(&pl_time->vhpet.pt[i], v); - spin_unlock(&pl_time->vhpet.lock); + write_unlock(&pl_time->vhpet.lock); } =20 =20 --- a/xen/include/asm-x86/hvm/vpt.h +++ b/xen/include/asm-x86/hvm/vpt.h @@ -96,7 +96,7 @@ typedef struct HPETState { uint64_t hpet_to_ns_limit; /* max hpet ticks convertable to ns = */ uint64_t mc_offset; struct periodic_time pt[HPET_TIMER_NUM]; - spinlock_t lock; + rwlock_t lock; } HPETState; =20 typedef struct RTCState { --=__Part7F4DC29C.0__= Content-Type: text/plain; name="x86-vHPET-rwlock.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-vHPET-rwlock.patch" x86/vHPET: use rwlock instead of simple one=0A=0AThis namely benefits = guests heavily reading the main counter, but not=0Atouching the HPET much = otherwise. Note that due to the way=0Ahpet_get_comparator() works = hpet_read() has to special cases reads from=0Athe comparator registers and = use a write lock there instead of the read=0Aone used for all other = registers.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/hvm/hpet.c=0A+++ b/xen/arch/x86/hvm/hpet.c=0A@@ -77,7 +77,7 = @@=0A =0A static inline uint64_t hpet_read_maincounter(HPETState *h, = uint64_t guest_time)=0A {=0A- ASSERT(spin_is_locked(&h->lock));=0A+ = ASSERT(rw_is_locked(&h->lock));=0A =0A if ( hpet_enabled(h) )=0A = return guest_time + h->mc_offset;=0A@@ -91,6 +91,8 @@ static uint64_t = hpet_get_comparator(HPET=0A uint64_t comparator;=0A uint64_t = elapsed;=0A =0A+ ASSERT(rw_is_write_locked(&h->lock));=0A+=0A = comparator =3D h->hpet.comparator64[tn];=0A if ( hpet_enabled(h) && = timer_is_periodic(h, tn) )=0A {=0A@@ -179,16 +181,24 @@ static int = hpet_read(=0A goto out;=0A }=0A =0A- spin_lock(&h->lock);=0A= + result =3D addr < HPET_Tn_CMP(0) ||=0A+ ((addr - = HPET_Tn_CMP(0)) % (HPET_Tn_CMP(1) - HPET_Tn_CMP(0))) > 7;=0A+ if ( = result )=0A+ read_lock(&h->lock);=0A+ else=0A+ write_lock(= &h->lock);=0A =0A val =3D hpet_read64(h, addr, guest_time_hpet(h));=0A = =0A+ if ( result )=0A+ read_unlock(&h->lock);=0A+ else=0A+ = write_unlock(&h->lock);=0A+=0A result =3D val;=0A if ( length = !=3D 8 )=0A result =3D (val >> ((addr & 7) * 8)) & ((1ULL << = (length * 8)) - 1);=0A =0A- spin_unlock(&h->lock);=0A-=0A out:=0A = *pval =3D result;=0A return X86EMUL_OKAY;=0A@@ -198,7 +208,7 @@ static = void hpet_stop_timer(HPETState *h=0A uint64_t = guest_time)=0A {=0A ASSERT(tn < HPET_TIMER_NUM);=0A- ASSERT(spin_is_= locked(&h->lock));=0A+ ASSERT(rw_is_write_locked(&h->lock));=0A = TRACE_1D(TRC_HVM_EMUL_HPET_STOP_TIMER, tn);=0A destroy_periodic_time(&h= ->pt[tn]);=0A /* read the comparator to get it updated so a read while = stopped will=0A@@ -218,7 +228,7 @@ static void hpet_set_timer(HPETState = *h,=0A unsigned int oneshot;=0A =0A ASSERT(tn < HPET_TIMER_NUM);=0A= - ASSERT(spin_is_locked(&h->lock));=0A+ ASSERT(rw_is_write_locked(&h-= >lock));=0A =0A if ( (tn =3D=3D 0) && (h->hpet.config & HPET_CFG_LEGACY= ) )=0A {=0A@@ -304,7 +314,7 @@ static int hpet_write(=0A if ( = hpet_check_access_length(addr, length) !=3D 0 )=0A goto out;=0A = =0A- spin_lock(&h->lock);=0A+ write_lock(&h->lock);=0A =0A = guest_time =3D guest_time_hpet(h);=0A old_val =3D hpet_read64(h, addr, = guest_time);=0A@@ -473,7 +483,7 @@ static int hpet_write(=0A #undef = set_start_timer=0A #undef set_restart_timer=0A =0A- spin_unlock(&h->lock= );=0A+ write_unlock(&h->lock);=0A =0A out:=0A return X86EMUL_OKAY;= =0A@@ -499,7 +509,7 @@ static int hpet_save(struct domain *d, h=0A int = rc;=0A uint64_t guest_time;=0A =0A- spin_lock(&hp->lock);=0A+ = write_lock(&hp->lock);=0A guest_time =3D guest_time_hpet(hp);=0A =0A = /* Write the proper value into the main counter */=0A@@ -545,7 +555,7 @@ = static int hpet_save(struct domain *d, h=0A rec->timers[2].cmp =3D = hp->hpet.comparator64[2];=0A }=0A =0A- spin_unlock(&hp->lock);=0A+ = write_unlock(&hp->lock);=0A =0A return rc;=0A }=0A@@ -558,12 +568,12 = @@ static int hpet_load(struct domain *d, h=0A uint64_t guest_time;=0A = int i;=0A =0A- spin_lock(&hp->lock);=0A+ write_lock(&hp->lock);= =0A =0A /* Reload the HPET registers */=0A if ( _hvm_check_entry(h,= HVM_SAVE_CODE(HPET), HVM_SAVE_LENGTH(HPET), 1) )=0A {=0A- = spin_unlock(&hp->lock);=0A+ write_unlock(&hp->lock);=0A = return -EINVAL;=0A }=0A =0A@@ -604,7 +614,7 @@ static int hpet_load(str= uct domain *d, h=0A if ( timer_enabled(hp, i) )=0A = hpet_set_timer(hp, i, guest_time);=0A =0A- spin_unlock(&hp->lock);= =0A+ write_unlock(&hp->lock);=0A =0A return 0;=0A }=0A@@ -618,7 = +628,7 @@ void hpet_init(struct vcpu *v)=0A =0A memset(h, 0, sizeof(HPE= TState));=0A =0A- spin_lock_init(&h->lock);=0A+ rwlock_init(&h->lock)= ;=0A =0A h->stime_freq =3D S_TO_NS;=0A =0A@@ -648,7 +658,7 @@ void = hpet_deinit(struct domain *d)=0A int i;=0A HPETState *h =3D = domain_vhpet(d);=0A =0A- spin_lock(&h->lock);=0A+ write_lock(&h->lock= );=0A =0A if ( hpet_enabled(h) )=0A {=0A@@ -659,7 +669,7 @@ void = hpet_deinit(struct domain *d)=0A hpet_stop_timer(h, i, = guest_time);=0A }=0A =0A- spin_unlock(&h->lock);=0A+ write_unlock= (&h->lock);=0A }=0A =0A void hpet_reset(struct domain *d)=0A--- a/xen/arch/= x86/hvm/vpt.c=0A+++ b/xen/arch/x86/hvm/vpt.c=0A@@ -512,10 +512,10 @@ void = pt_adjust_global_vcpu_target(struct=0A pt_adjust_vcpu(&pl_time->vrtc.pt= , v);=0A spin_unlock(&pl_time->vrtc.lock);=0A =0A- spin_lock(&pl_tim= e->vhpet.lock);=0A+ write_lock(&pl_time->vhpet.lock);=0A for ( i = =3D 0; i < HPET_TIMER_NUM; i++ )=0A pt_adjust_vcpu(&pl_time->vhpet.= pt[i], v);=0A- spin_unlock(&pl_time->vhpet.lock);=0A+ write_unlock(&p= l_time->vhpet.lock);=0A }=0A =0A =0A--- a/xen/include/asm-x86/hvm/vpt.h=0A+= ++ b/xen/include/asm-x86/hvm/vpt.h=0A@@ -96,7 +96,7 @@ typedef struct = HPETState {=0A uint64_t hpet_to_ns_limit; /* max hpet ticks convertable= to ns */=0A uint64_t mc_offset;=0A struct periodic_time = pt[HPET_TIMER_NUM];=0A- spinlock_t lock;=0A+ rwlock_t lock;=0A } = HPETState;=0A =0A typedef struct RTCState {=0A --=__Part7F4DC29C.0__= 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.xen.org http://lists.xen.org/xen-devel --=__Part7F4DC29C.0__=--