Skip to contentSkip to conversation

use mask operations instead of test_bit()

PATCH In review Juergen Gross 5 participants 23 messages Oct 2, 2015 – Oct 5, 2015

lore
Share & tools
lore
#0 lore raw

cover use mask operations instead of test_bit()

Instead of using test_bit() which is an atomic operation and limits the compiler's choices to do optimization, use logical ANDs with bitmasks where possible.

The possible candidates have been detected by searching definitions of bitmasks in the form:

#define MASK 1 << _MASK

On x86 the resulting code is slightly smaller (about 2 bytes for each case, checked via disassembly in few examples).

I'm quite sure I didn't replace a test_bit() call required to be atomic, but I'd be grateful for a thorough review especially in the scheduler.

Juergen Gross (5):
xen: use masking operation instead of test_bit for RTDS bits
xen: use masking operation instead of test_bit for CSFLAG bits
xen: use masking operation instead of test_bit for VGCF bits
xen: use masking operation instead of test_bit for VPF bits
xen: use masking operation instead of test_bit for MCSF bits

 xen/arch/x86/domain.c           | 12 ++++++------
 xen/arch/x86/domctl.c           |  2 +-
 xen/arch/x86/hvm/hvm.c          |  4 ++--
 xen/arch/x86/hvm/vpt.c          |  2 +-
 xen/arch/x86/x86_64/compat/mm.c |  4 ++--
 xen/common/domain.c             |  4 ++--
 xen/common/domctl.c             |  8 ++++----
 xen/common/multicall.c          |  2 +-
 xen/common/sched_credit2.c      | 16 ++++++++--------
 xen/common/sched_rt.c           |  4 ++--
 xen/common/schedule.c           | 16 ++++++++--------
 11 files changed, 37 insertions(+), 37 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/5 xen: use masking operation instead of test_bit for RTDS bits

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

---
 xen/common/sched_rt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/common/sched_rt.c +2 -2
index 4b5c5e4..6a341b1 100644
@@ -931,7 +931,7 @@ rt_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         cpu_raise_softirq(vc->processor, SCHEDULE_SOFTIRQ);
     else if ( __vcpu_on_q(svc) )
         __q_remove(svc);
-    else if ( test_bit(__RTDS_delayed_runq_add, &svc->flags) )
+    else if ( svc->flags & RTDS_delayed_runq_add )
         clear_bit(__RTDS_delayed_runq_add, &svc->flags);
 }
 
@@ -1064,7 +1064,7 @@ rt_vcpu_wake(const struct scheduler *ops, struct vcpu *vc)
      * the Runqueue/DepletedQ. Instead, we set a flag so that it will be
      * put on the Runqueue/DepletedQ after the context has been saved.
      */
-    if ( unlikely(test_bit(__RTDS_scheduled, &svc->flags)) )
+    if ( unlikely(svc->flags & RTDS_scheduled) )
     {
         set_bit(__RTDS_delayed_runq_add, &svc->flags);
         return;
Signature · 1 line
2.1.4
#4 lore raw

PATCH 2/5 xen: use masking operation instead of test_bit for CSFLAG bits

In reply to Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

---
 xen/common/sched_credit2.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
xen/common/sched_credit2.c +8 -8
index bf1fe6f..912e1a2 100644
@@ -418,7 +418,7 @@ __runq_insert(struct list_head *runq, struct csched2_vcpu *svc)
     /* Idle vcpus not allowed on the runqueue anymore */
     BUG_ON(is_idle_vcpu(svc->vcpu));
     BUG_ON(svc->vcpu->is_running);
-    BUG_ON(test_bit(__CSFLAG_scheduled, &svc->flags));
+    BUG_ON(svc->flags & CSFLAG_scheduled);
 
     list_for_each( iter, runq )
     {
@@ -844,7 +844,7 @@ static void
 __runq_deassign(struct csched2_vcpu *svc)
 {
     BUG_ON(__vcpu_on_runq(svc));
-    BUG_ON(test_bit(__CSFLAG_scheduled, &svc->flags));
+    BUG_ON(svc->flags & CSFLAG_scheduled);
 
     list_del_init(&svc->rqd_elem);
     update_max_weight(svc->rqd, 0, svc->weight);
@@ -952,7 +952,7 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         __runq_remove(svc);
     }
-    else if ( test_bit(__CSFLAG_delayed_runq_add, &svc->flags) )
+    else if ( svc->flags & CSFLAG_delayed_runq_add )
         clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);
 }
 
@@ -988,7 +988,7 @@ csched2_vcpu_wake(const struct scheduler *ops, struct vcpu *vc)
     /* If the context hasn't been saved for this vcpu yet, we can't put it on
      * another runqueue.  Instead, we set a flag so that it will be put on the runqueue
      * after the context has been saved. */
-    if ( unlikely (test_bit(__CSFLAG_scheduled, &svc->flags) ) )
+    if ( unlikely(svc->flags & CSFLAG_scheduled) )
     {
         set_bit(__CSFLAG_delayed_runq_add, &svc->flags);
         goto out;
@@ -1204,7 +1204,7 @@ static void migrate(const struct scheduler *ops,
                     struct csched2_runqueue_data *trqd, 
                     s_time_t now)
 {
-    if ( test_bit(__CSFLAG_scheduled, &svc->flags) )
+    if ( svc->flags & CSFLAG_scheduled )
     {
         d2printk("%pv %d-%d a\n", svc->vcpu, svc->rqd->id, trqd->id);
         /* It's running; mark it to migrate. */
@@ -1348,7 +1348,7 @@ retry:
         __update_svc_load(ops, push_svc, 0, now);
 
         /* Skip this one if it's already been flagged to migrate */
-        if ( test_bit(__CSFLAG_runq_migrate_request, &push_svc->flags) )
+        if ( push_svc->flags & CSFLAG_runq_migrate_request )
             continue;
 
         list_for_each( pull_iter, &st.orqd->svc )
@@ -1361,7 +1361,7 @@ retry:
             }
         
             /* Skip this one if it's already been flagged to migrate */
-            if ( test_bit(__CSFLAG_runq_migrate_request, &pull_svc->flags) )
+            if ( pull_svc->flags & CSFLAG_runq_migrate_request )
                 continue;
 
             consider(&st, push_svc, pull_svc);
@@ -1378,7 +1378,7 @@ retry:
         struct csched2_vcpu * pull_svc = list_entry(pull_iter, struct csched2_vcpu, rqd_elem);
         
         /* Skip this one if it's already been flagged to migrate */
-        if ( test_bit(__CSFLAG_runq_migrate_request, &pull_svc->flags) )
+        if ( pull_svc->flags & CSFLAG_runq_migrate_request )
             continue;
 
         /* Consider pull only */
Signature · 1 line
2.1.4
#7 lore raw

PATCH 3/5 xen: use masking operation instead of test_bit for VGCF bits

In reply to Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

---
 xen/arch/x86/domain.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/domain.c +2 -2
index dc3bb08..4e96f6c 100644
@@ -1352,7 +1352,7 @@ static void load_segments(struct vcpu *n)
                 domain_crash(n->domain);
             }
 
-            if ( test_bit(_VGCF_failsafe_disables_events, &n->arch.vgc_flags) )
+            if ( n->arch.vgc_flags & VGCF_failsafe_disables_events )
                 vcpu_info(n, evtchn_upcall_mask) = 1;
 
             regs->entry_vector |= TRAP_syscall;
@@ -1394,7 +1394,7 @@ static void load_segments(struct vcpu *n)
             domain_crash(n->domain);
         }
 
-        if ( test_bit(_VGCF_failsafe_disables_events, &n->arch.vgc_flags) )
+        if ( n->arch.vgc_flags & VGCF_failsafe_disables_events )
             vcpu_info(n, evtchn_upcall_mask) = 1;
 
         regs->entry_vector |= TRAP_syscall;
Signature · 1 line
2.1.4
#8 lore raw

PATCH 4/5 xen: use masking operation instead of test_bit for VPF bits

In reply to Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

---
 xen/arch/x86/domctl.c  |  2 +-
 xen/arch/x86/hvm/hvm.c |  4 ++--
 xen/arch/x86/hvm/vpt.c |  2 +-
 xen/common/domain.c    |  4 ++--
 xen/common/domctl.c    |  8 ++++----
 xen/common/schedule.c  | 16 ++++++++--------
 6 files changed, 18 insertions(+), 18 deletions(-)
xen/arch/x86/domctl.c +1 -1
index 6172c0d..f8a559c 100644
@@ -1213,7 +1213,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
     c(flags = v->arch.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
     if ( v->fpu_initialised )
         c(flags |= VGCF_i387_valid);
-    if ( !test_bit(_VPF_down, &v->pause_flags) )
+    if ( !(v->pause_flags & VPF_down) )
         c(flags |= VGCF_online);
     if ( !compat )
     {
xen/arch/x86/hvm/hvm.c +2 -2
index 6afc344..3fa2280 100644
@@ -1728,7 +1728,7 @@ static int hvm_save_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     {
         /* We don't need to save state for a vcpu that is down; the restore 
          * code will leave it down if there is nothing saved. */
-        if ( test_bit(_VPF_down, &v->pause_flags) ) 
+        if ( v->pause_flags & VPF_down )
             continue;
 
         /* Architecture-specific vmcs/vmcb bits */
@@ -2512,7 +2512,7 @@ void hvm_vcpu_down(struct vcpu *v)
     /* Any other VCPUs online? ... */
     domain_lock(d);
     for_each_vcpu ( d, v )
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
             online_count++;
     domain_unlock(d);
 
xen/arch/x86/hvm/vpt.c +1 -1
index 0c8b22e..4fa6566 100644
@@ -178,7 +178,7 @@ void pt_save_timer(struct vcpu *v)
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
     struct periodic_time *pt;
 
-    if ( test_bit(_VPF_blocked, &v->pause_flags) )
+    if ( v->pause_flags & VPF_blocked )
         return;
 
     spin_lock(&v->arch.hvm_vcpu.tm_lock);
xen/common/domain.c +2 -2
index cda60a9..7c362eb 100644
@@ -1135,7 +1135,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
         return -EINVAL;
 
     /* Run this command on yourself or on other offline VCPUS. */
-    if ( (v != current) && !test_bit(_VPF_down, &v->pause_flags) )
+    if ( (v != current) && !(v->pause_flags & VPF_down) )
         return -EINVAL;
 
     page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
@@ -1263,7 +1263,7 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
 
     case VCPUOP_is_up:
-        rc = !test_bit(_VPF_down, &v->pause_flags);
+        rc = !(v->pause_flags & VPF_down);
         break;
 
     case VCPUOP_get_runstate_info:
xen/common/domctl.c +4 -4
index 08de32d..46b967e 100644
@@ -170,7 +170,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)
         vcpu_runstate_get(v, &runstate);
         cpu_time += runstate.time[RUNSTATE_running];
         info->max_vcpu_id = v->vcpu_id;
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
         {
             if ( !(v->pause_flags & VPF_blocked) )
                 flags &= ~XEN_DOMINF_blocked;
@@ -231,7 +231,7 @@ static unsigned int default_vcpu0_location(cpumask_t *online)
         rcu_read_lock(&domlist_read_lock);
         for_each_domain ( d )
             for_each_vcpu ( d, v )
-                if ( !test_bit(_VPF_down, &v->pause_flags)
+                if ( !(v->pause_flags & VPF_down)
                      && ((cpu = v->processor) < nr_cpus) )
                     cnt[cpu]++;
         rcu_read_unlock(&domlist_read_lock);
@@ -944,8 +944,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
         vcpu_runstate_get(v, &runstate);
 
-        op->u.getvcpuinfo.online   = !test_bit(_VPF_down, &v->pause_flags);
-        op->u.getvcpuinfo.blocked  = test_bit(_VPF_blocked, &v->pause_flags);
+        op->u.getvcpuinfo.online   = !(v->pause_flags & VPF_down);
+        op->u.getvcpuinfo.blocked  = !!(v->pause_flags & VPF_blocked);
         op->u.getvcpuinfo.running  = v->is_running;
         op->u.getvcpuinfo.cpu_time = runstate.time[RUNSTATE_running];
         op->u.getvcpuinfo.cpu      = v->processor;
xen/common/schedule.c +8 -8
index 5ffa1a1..c5f640f 100644
@@ -119,7 +119,7 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
 
     if ( unlikely(v->is_urgent) )
     {
-        if ( !test_bit(_VPF_blocked, &v->pause_flags) ||
+        if ( !(v->pause_flags & VPF_blocked) ||
              !test_bit(v->vcpu_id, v->domain->poll_mask) )
         {
             v->is_urgent = 0;
@@ -128,8 +128,8 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
     }
     else
     {
-        if ( unlikely(test_bit(_VPF_blocked, &v->pause_flags) &&
-                      test_bit(v->vcpu_id, v->domain->poll_mask)) )
+        if ( unlikely(v->pause_flags & VPF_blocked) &&
+             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
         {
             v->is_urgent = 1;
             atomic_inc(&per_cpu(schedule_data,v->processor).urgent_count);
@@ -418,7 +418,7 @@ void vcpu_wake(struct vcpu *v)
             vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
         SCHED_OP(VCPU2OP(v), wake, v);
     }
-    else if ( !test_bit(_VPF_blocked, &v->pause_flags) )
+    else if ( !(v->pause_flags & VPF_blocked) )
     {
         if ( v->runstate.state == RUNSTATE_blocked )
             vcpu_runstate_change(v, RUNSTATE_offline, NOW());
@@ -595,7 +595,7 @@ void vcpu_force_reschedule(struct vcpu *v)
         set_bit(_VPF_migrating, &v->pause_flags);
     vcpu_schedule_unlock_irq(lock, v);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -763,7 +763,7 @@ static int vcpu_set_affinity(
 
     domain_update_node_affinity(v->domain);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -1285,7 +1285,7 @@ static void schedule(void)
 
     vcpu_runstate_change(
         prev,
-        (test_bit(_VPF_blocked, &prev->pause_flags) ? RUNSTATE_blocked :
+        ((prev->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
          (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
         now);
     prev->last_run_time = now;
@@ -1327,7 +1327,7 @@ void context_saved(struct vcpu *prev)
 
     SCHED_OP(VCPU2OP(prev), context_saved, prev);
 
-    if ( unlikely(test_bit(_VPF_migrating, &prev->pause_flags)) )
+    if ( unlikely(prev->pause_flags & VPF_migrating) )
         vcpu_migrate(prev);
 }
 
Signature · 1 line
2.1.4
#17 lore raw

PATCH 5/5 xen: use masking operation instead of test_bit for MCSF bits

In reply to Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

---
 xen/arch/x86/domain.c           | 8 ++++----
 xen/arch/x86/x86_64/compat/mm.c | 4 ++--
 xen/common/multicall.c          | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/domain.c +4 -4
index 4e96f6c..7ca9b93 100644
@@ -1750,7 +1750,7 @@ void hypercall_cancel_continuation(void)
     struct cpu_user_regs *regs = guest_cpu_user_regs();
     struct mc_state *mcs = &current->mc_state;
 
-    if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
+    if ( mcs->flags & MCSF_in_multicall )
     {
         __clear_bit(_MCSF_call_preempted, &mcs->flags);
     }
@@ -1774,7 +1774,7 @@ unsigned long hypercall_create_continuation(
 
     va_start(args, format);
 
-    if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
+    if ( mcs->flags & MCSF_in_multicall )
     {
         __set_bit(_MCSF_call_preempted, &mcs->flags);
 
@@ -1852,9 +1852,9 @@ int hypercall_xlat_continuation(unsigned int *id, unsigned int nr,
 
     va_start(args, mask);
 
-    if ( test_bit(_MCSF_in_multicall, &mcs->flags) )
+    if ( mcs->flags & MCSF_in_multicall )
     {
-        if ( !test_bit(_MCSF_call_preempted, &mcs->flags) )
+        if ( !(mcs->flags & MCSF_call_preempted) )
         {
             va_end(args);
             return 0;
xen/arch/x86/x86_64/compat/mm.c +2 -2
index d034bd0..178e42d 100644
@@ -324,7 +324,7 @@ int compat_mmuext_op(XEN_GUEST_HANDLE_PARAM(mmuext_op_compat_t) cmp_uops,
             {
                 struct cpu_user_regs *regs = guest_cpu_user_regs();
                 struct mc_state *mcs = &current->mc_state;
-                unsigned int arg1 = !test_bit(_MCSF_in_multicall, &mcs->flags)
+                unsigned int arg1 = !(mcs->flags & MCSF_in_multicall)
                                     ? regs->ecx
                                     : mcs->call.args[1];
                 unsigned int left = arg1 & ~MMU_UPDATE_PREEMPTED;
@@ -338,7 +338,7 @@ int compat_mmuext_op(XEN_GUEST_HANDLE_PARAM(mmuext_op_compat_t) cmp_uops,
                 {
                     BUG_ON(!hypercall_xlat_continuation(&left, 4, 0x01, nat_ops,
                                                         cmp_uops));
-                    if ( !test_bit(_MCSF_in_multicall, &mcs->flags) )
+                    if ( !(mcs->flags & MCSF_in_multicall) )
                         regs->_ecx += count - i;
                     else
                         mcs->compat_call.args[1] += count - i;
xen/common/multicall.c +1 -1
index fa9d910..21661ee 100644
@@ -79,7 +79,7 @@ do_multicall(
 
         if ( unlikely(__copy_field_to_guest(call_list, &mcs->call, result)) )
             rc = -EFAULT;
-        else if ( test_bit(_MCSF_call_preempted, &mcs->flags) )
+        else if ( mcs->flags & MCSF_call_preempted )
         {
             /* Translate sub-call continuation to guest layout */
             xlat_multicall_entry(mcs);
Signature · 1 line
2.1.4
#18 lore raw
In reply to Juergen Gross
Quoted 4 lines · Juergen Gross

Instead of using test_bit() which is an atomic operation and limits the compiler's choices to do optimization, use logical ANDs with bitmasks where possible.

That's a good idea, I think.

Quoted 5 lines

The possible candidates have been detected by searching definitions of bitmasks in the form:

#define MASK 1 << _MASK

Right.

Quoted 7 lines

On x86 the resulting code is slightly smaller (about 2 bytes for each case, checked via disassembly in few examples).

I'm quite sure I didn't replace a test_bit() call required to be atomic, but I'd be grateful for a thorough review especially in the scheduler.

I'll have a deep look.

One question, can we introduce a __test_bit() macro/inline function, like Jan did with __set_bit?

I've quickly-&-dirtily tested this:

#define __test_bit(nr, addr) ({  \
    unsigned _flags = 1 << nr;   \
    addr & _flags; \
})

and the result (I've checked a couple of cases) seems the same to me.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#19 lore raw
Quoted 6 lines · Dario Faggioli
Quoted 4 lines · Juergen Gross

Instead of using test_bit() which is an atomic operation and limits the compiler's choices to do optimization, use logical ANDs with bitmasks where possible.

That's a good idea, I think.

It's a fallout from a cleanup patch:

http://lists.xen.org/archives/html/xen-devel/2015-09/msg03184.html

Quoted 8 lines
Quoted 6 lines

The possible candidates have been detected by searching definitions of bitmasks in the form:

#define MASK 1 << _MASK

Right.

Quoted 8 lines
Quoted 7 lines

On x86 the resulting code is slightly smaller (about 2 bytes for each case, checked via disassembly in few examples).

I'm quite sure I didn't replace a test_bit() call required to be atomic, but I'd be grateful for a thorough review especially in the scheduler.

I'll have a deep look.

Thanks.

Quoted 2 lines

One question, can we introduce a __test_bit() macro/inline function, like Jan did with __set_bit?

In the thread mentioned above you'll find a discussion about exactly this idea between Jan and me.

Quoted 8 lines

I've quickly-&-dirtily tested this:

#define __test_bit(nr, addr) ({  \
     unsigned _flags = 1 << nr;   \
     addr & _flags; \
})

and the result (I've checked a couple of cases) seems the same to me.

The problem is the limited scope where this scheme is really working and is a better solution at the same time (nr must be a constant less than the numbers of bits of *addr).

Juergen

#20 lore raw
Quoted 9 lines · Juergen Gross
Quoted 1 line · Dario Faggioli

That's a good idea, I think.

It's a fallout from a cleanup patch:

http://lists.xen.org/archives/html/xen-devel/2015-09/msg03184.html

Quoted 7 lines
Quoted 3 lines

One question, can we introduce a __test_bit() macro/inline function,
like Jan did with __set_bit?

In the thread mentioned above you'll find a discussion about exactly this idea between Jan and me.

Ah, I see. Sorry, I missed it.

Quoted 7 lines
Quoted 7 lines

I've quickly-&-dirtily tested this:

#define __test_bit(nr, addr) ({  \
     unsigned _flags = 1 << nr;   \
     addr & _flags; \
})
Quoted 9 lines
Quoted 2 lines

and the result (I've checked a couple of cases) seems the same to me.

The problem is the limited scope where this scheme is really working and
is a better solution at the same time (nr must be a constant less than
the numbers of bits of *addr).

Right, so we could only use __test_bit() in a subset of the cases, i.e., we won't gain that much more consistency... I see it now.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on the v1 series
#21 lore raw
In reply to Juergen Gross
Quoted 7 lines
Quoted 1 line
Quoted 1 line

Juergen Gross <[email protected]> 10/02/15 6:40 AM >>>

Juergen Gross (5):
xen: use masking operation instead of test_bit for RTDS bits
xen: use masking operation instead of test_bit for CSFLAG bits
xen: use masking operation instead of test_bit for VGCF bits
xen: use masking operation instead of test_bit for VPF bits
xen: use masking operation instead of test_bit for MCSF bits

Reviewed-by on the v1 series
#22 lore raw
In reply to Juergen Gross
Quoted 17 lines · Juergen Gross

Instead of using test_bit() which is an atomic operation and limits the compiler's choices to do optimization, use logical ANDs with bitmasks where possible.

The possible candidates have been detected by searching definitions of bitmasks in the form:

#define MASK 1 << _MASK

On x86 the resulting code is slightly smaller (about 2 bytes for each case, checked via disassembly in few examples).

I'm quite sure I didn't replace a test_bit() call required to be atomic, but I'd be grateful for a thorough review especially in the scheduler.

Quoted 6 lines

Juergen Gross (5):
xen: use masking operation instead of test_bit for RTDS bits
xen: use masking operation instead of test_bit for CSFLAG bits
xen: use masking operation instead of test_bit for VGCF bits
xen: use masking operation instead of test_bit for VPF bits
xen: use masking operation instead of test_bit for MCSF bits

Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] xen: use masking operation instead of test_bit for RTDS bits

In reply to Juergen Gross
Quoted 7 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no
atomic operation is needed, as this will lead to smaller and more effective code.

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] xen: use masking operation instead of test_bit for CSFLAG bits

In reply to Juergen Gross
Quoted 7 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no
atomic operation is needed, as this will lead to smaller and more effective code.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

For example, xen/common/schedule.c:vcpu_unblock(), vcpu_block(), do_poll; xen/common/event_channel.c:defaultxen_notification_fn(), and many more?

-George

Quoted 182 lines
---
 xen/arch/x86/domctl.c  |  2 +-
 xen/arch/x86/hvm/hvm.c |  4 ++--
 xen/arch/x86/hvm/vpt.c |  2 +-
 xen/common/domain.c    |  4 ++--
 xen/common/domctl.c    |  8 ++++----
 xen/common/schedule.c  | 16 ++++++++--------
 6 files changed, 18 insertions(+), 18 deletions(-)
xen/arch/x86/domctl.c +1 -1
index 6172c0d..f8a559c 100644
@@ -1213,7 +1213,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
     c(flags = v->arch.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
     if ( v->fpu_initialised )
         c(flags |= VGCF_i387_valid);
-    if ( !test_bit(_VPF_down, &v->pause_flags) )
+    if ( !(v->pause_flags & VPF_down) )
         c(flags |= VGCF_online);
     if ( !compat )
     {
xen/arch/x86/hvm/hvm.c +2 -2
index 6afc344..3fa2280 100644
@@ -1728,7 +1728,7 @@ static int hvm_save_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     {
         /* We don't need to save state for a vcpu that is down; the restore 
          * code will leave it down if there is nothing saved. */
-        if ( test_bit(_VPF_down, &v->pause_flags) ) 
+        if ( v->pause_flags & VPF_down )
             continue;
 
         /* Architecture-specific vmcs/vmcb bits */
@@ -2512,7 +2512,7 @@ void hvm_vcpu_down(struct vcpu *v)
     /* Any other VCPUs online? ... */
     domain_lock(d);
     for_each_vcpu ( d, v )
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
             online_count++;
     domain_unlock(d);
 
xen/arch/x86/hvm/vpt.c +1 -1
index 0c8b22e..4fa6566 100644
@@ -178,7 +178,7 @@ void pt_save_timer(struct vcpu *v)
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
     struct periodic_time *pt;
 
-    if ( test_bit(_VPF_blocked, &v->pause_flags) )
+    if ( v->pause_flags & VPF_blocked )
         return;
 
     spin_lock(&v->arch.hvm_vcpu.tm_lock);
xen/common/domain.c +2 -2
index cda60a9..7c362eb 100644
@@ -1135,7 +1135,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
         return -EINVAL;
 
     /* Run this command on yourself or on other offline VCPUS. */
-    if ( (v != current) && !test_bit(_VPF_down, &v->pause_flags) )
+    if ( (v != current) && !(v->pause_flags & VPF_down) )
         return -EINVAL;
 
     page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
@@ -1263,7 +1263,7 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
 
     case VCPUOP_is_up:
-        rc = !test_bit(_VPF_down, &v->pause_flags);
+        rc = !(v->pause_flags & VPF_down);
         break;
 
     case VCPUOP_get_runstate_info:
xen/common/domctl.c +4 -4
index 08de32d..46b967e 100644
@@ -170,7 +170,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)
         vcpu_runstate_get(v, &runstate);
         cpu_time += runstate.time[RUNSTATE_running];
         info->max_vcpu_id = v->vcpu_id;
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
         {
             if ( !(v->pause_flags & VPF_blocked) )
                 flags &= ~XEN_DOMINF_blocked;
@@ -231,7 +231,7 @@ static unsigned int default_vcpu0_location(cpumask_t *online)
         rcu_read_lock(&domlist_read_lock);
         for_each_domain ( d )
             for_each_vcpu ( d, v )
-                if ( !test_bit(_VPF_down, &v->pause_flags)
+                if ( !(v->pause_flags & VPF_down)
                      && ((cpu = v->processor) < nr_cpus) )
                     cnt[cpu]++;
         rcu_read_unlock(&domlist_read_lock);
@@ -944,8 +944,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
         vcpu_runstate_get(v, &runstate);
 
-        op->u.getvcpuinfo.online   = !test_bit(_VPF_down, &v->pause_flags);
-        op->u.getvcpuinfo.blocked  = test_bit(_VPF_blocked, &v->pause_flags);
+        op->u.getvcpuinfo.online   = !(v->pause_flags & VPF_down);
+        op->u.getvcpuinfo.blocked  = !!(v->pause_flags & VPF_blocked);
         op->u.getvcpuinfo.running  = v->is_running;
         op->u.getvcpuinfo.cpu_time = runstate.time[RUNSTATE_running];
         op->u.getvcpuinfo.cpu      = v->processor;
xen/common/schedule.c +8 -8
index 5ffa1a1..c5f640f 100644
@@ -119,7 +119,7 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
 
     if ( unlikely(v->is_urgent) )
     {
-        if ( !test_bit(_VPF_blocked, &v->pause_flags) ||
+        if ( !(v->pause_flags & VPF_blocked) ||
              !test_bit(v->vcpu_id, v->domain->poll_mask) )
         {
             v->is_urgent = 0;
@@ -128,8 +128,8 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
     }
     else
     {
-        if ( unlikely(test_bit(_VPF_blocked, &v->pause_flags) &&
-                      test_bit(v->vcpu_id, v->domain->poll_mask)) )
+        if ( unlikely(v->pause_flags & VPF_blocked) &&
+             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
         {
             v->is_urgent = 1;
             atomic_inc(&per_cpu(schedule_data,v->processor).urgent_count);
@@ -418,7 +418,7 @@ void vcpu_wake(struct vcpu *v)
             vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
         SCHED_OP(VCPU2OP(v), wake, v);
     }
-    else if ( !test_bit(_VPF_blocked, &v->pause_flags) )
+    else if ( !(v->pause_flags & VPF_blocked) )
     {
         if ( v->runstate.state == RUNSTATE_blocked )
             vcpu_runstate_change(v, RUNSTATE_offline, NOW());
@@ -595,7 +595,7 @@ void vcpu_force_reschedule(struct vcpu *v)
         set_bit(_VPF_migrating, &v->pause_flags);
     vcpu_schedule_unlock_irq(lock, v);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -763,7 +763,7 @@ static int vcpu_set_affinity(
 
     domain_update_node_affinity(v->domain);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -1285,7 +1285,7 @@ static void schedule(void)
 
     vcpu_runstate_change(
         prev,
-        (test_bit(_VPF_blocked, &prev->pause_flags) ? RUNSTATE_blocked :
+        ((prev->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
          (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
         now);
     prev->last_run_time = now;
@@ -1327,7 +1327,7 @@ void context_saved(struct vcpu *prev)
 
     SCHED_OP(VCPU2OP(prev), context_saved, prev);
 
-    if ( unlikely(test_bit(_VPF_migrating, &prev->pause_flags)) )
+    if ( unlikely(prev->pause_flags & VPF_migrating) )
         vcpu_migrate(prev);
 }
 
#15 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

On a side note, a handful of functions seem to access the vcpu structure with the *domain* lock held, rather than the *scheduler* lock; for example:

xen/arch/x86/hvm/vlapic.c:vlapic_accept_irq()
xen/common/domain.c:vcpu_reset()

Is this a bug (perhaps left over from when we used the domain lock for vcpus rather than the scheduler lock)?

-George

Quoted 182 lines
---
 xen/arch/x86/domctl.c  |  2 +-
 xen/arch/x86/hvm/hvm.c |  4 ++--
 xen/arch/x86/hvm/vpt.c |  2 +-
 xen/common/domain.c    |  4 ++--
 xen/common/domctl.c    |  8 ++++----
 xen/common/schedule.c  | 16 ++++++++--------
 6 files changed, 18 insertions(+), 18 deletions(-)
xen/arch/x86/domctl.c +1 -1
index 6172c0d..f8a559c 100644
@@ -1213,7 +1213,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
     c(flags = v->arch.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
     if ( v->fpu_initialised )
         c(flags |= VGCF_i387_valid);
-    if ( !test_bit(_VPF_down, &v->pause_flags) )
+    if ( !(v->pause_flags & VPF_down) )
         c(flags |= VGCF_online);
     if ( !compat )
     {
xen/arch/x86/hvm/hvm.c +2 -2
index 6afc344..3fa2280 100644
@@ -1728,7 +1728,7 @@ static int hvm_save_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
     {
         /* We don't need to save state for a vcpu that is down; the restore 
          * code will leave it down if there is nothing saved. */
-        if ( test_bit(_VPF_down, &v->pause_flags) ) 
+        if ( v->pause_flags & VPF_down )
             continue;
 
         /* Architecture-specific vmcs/vmcb bits */
@@ -2512,7 +2512,7 @@ void hvm_vcpu_down(struct vcpu *v)
     /* Any other VCPUs online? ... */
     domain_lock(d);
     for_each_vcpu ( d, v )
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
             online_count++;
     domain_unlock(d);
 
xen/arch/x86/hvm/vpt.c +1 -1
index 0c8b22e..4fa6566 100644
@@ -178,7 +178,7 @@ void pt_save_timer(struct vcpu *v)
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
     struct periodic_time *pt;
 
-    if ( test_bit(_VPF_blocked, &v->pause_flags) )
+    if ( v->pause_flags & VPF_blocked )
         return;
 
     spin_lock(&v->arch.hvm_vcpu.tm_lock);
xen/common/domain.c +2 -2
index cda60a9..7c362eb 100644
@@ -1135,7 +1135,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
         return -EINVAL;
 
     /* Run this command on yourself or on other offline VCPUS. */
-    if ( (v != current) && !test_bit(_VPF_down, &v->pause_flags) )
+    if ( (v != current) && !(v->pause_flags & VPF_down) )
         return -EINVAL;
 
     page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
@@ -1263,7 +1263,7 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
 
     case VCPUOP_is_up:
-        rc = !test_bit(_VPF_down, &v->pause_flags);
+        rc = !(v->pause_flags & VPF_down);
         break;
 
     case VCPUOP_get_runstate_info:
xen/common/domctl.c +4 -4
index 08de32d..46b967e 100644
@@ -170,7 +170,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)
         vcpu_runstate_get(v, &runstate);
         cpu_time += runstate.time[RUNSTATE_running];
         info->max_vcpu_id = v->vcpu_id;
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
         {
             if ( !(v->pause_flags & VPF_blocked) )
                 flags &= ~XEN_DOMINF_blocked;
@@ -231,7 +231,7 @@ static unsigned int default_vcpu0_location(cpumask_t *online)
         rcu_read_lock(&domlist_read_lock);
         for_each_domain ( d )
             for_each_vcpu ( d, v )
-                if ( !test_bit(_VPF_down, &v->pause_flags)
+                if ( !(v->pause_flags & VPF_down)
                      && ((cpu = v->processor) < nr_cpus) )
                     cnt[cpu]++;
         rcu_read_unlock(&domlist_read_lock);
@@ -944,8 +944,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
         vcpu_runstate_get(v, &runstate);
 
-        op->u.getvcpuinfo.online   = !test_bit(_VPF_down, &v->pause_flags);
-        op->u.getvcpuinfo.blocked  = test_bit(_VPF_blocked, &v->pause_flags);
+        op->u.getvcpuinfo.online   = !(v->pause_flags & VPF_down);
+        op->u.getvcpuinfo.blocked  = !!(v->pause_flags & VPF_blocked);
         op->u.getvcpuinfo.running  = v->is_running;
         op->u.getvcpuinfo.cpu_time = runstate.time[RUNSTATE_running];
         op->u.getvcpuinfo.cpu      = v->processor;
xen/common/schedule.c +8 -8
index 5ffa1a1..c5f640f 100644
@@ -119,7 +119,7 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
 
     if ( unlikely(v->is_urgent) )
     {
-        if ( !test_bit(_VPF_blocked, &v->pause_flags) ||
+        if ( !(v->pause_flags & VPF_blocked) ||
              !test_bit(v->vcpu_id, v->domain->poll_mask) )
         {
             v->is_urgent = 0;
@@ -128,8 +128,8 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
     }
     else
     {
-        if ( unlikely(test_bit(_VPF_blocked, &v->pause_flags) &&
-                      test_bit(v->vcpu_id, v->domain->poll_mask)) )
+        if ( unlikely(v->pause_flags & VPF_blocked) &&
+             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
         {
             v->is_urgent = 1;
             atomic_inc(&per_cpu(schedule_data,v->processor).urgent_count);
@@ -418,7 +418,7 @@ void vcpu_wake(struct vcpu *v)
             vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
         SCHED_OP(VCPU2OP(v), wake, v);
     }
-    else if ( !test_bit(_VPF_blocked, &v->pause_flags) )
+    else if ( !(v->pause_flags & VPF_blocked) )
     {
         if ( v->runstate.state == RUNSTATE_blocked )
             vcpu_runstate_change(v, RUNSTATE_offline, NOW());
@@ -595,7 +595,7 @@ void vcpu_force_reschedule(struct vcpu *v)
         set_bit(_VPF_migrating, &v->pause_flags);
     vcpu_schedule_unlock_irq(lock, v);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -763,7 +763,7 @@ static int vcpu_set_affinity(
 
     domain_update_node_affinity(v->domain);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
     {
         vcpu_sleep_nosync(v);
         vcpu_migrate(v);
@@ -1285,7 +1285,7 @@ static void schedule(void)
 
     vcpu_runstate_change(
         prev,
-        (test_bit(_VPF_blocked, &prev->pause_flags) ? RUNSTATE_blocked :
+        ((prev->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
          (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
         now);
     prev->last_run_time = now;
@@ -1327,7 +1327,7 @@ void context_saved(struct vcpu *prev)
 
     SCHED_OP(VCPU2OP(prev), context_saved, prev);
 
-    if ( unlikely(test_bit(_VPF_migrating, &prev->pause_flags)) )
+    if ( unlikely(prev->pause_flags & VPF_migrating) )
         vcpu_migrate(prev);
 }
 
Acked-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] xen: use masking operation instead of test_bit for RTDS bits

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

Quoted 27 lines
---
 xen/common/sched_rt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/common/sched_rt.c +2 -2
index 4b5c5e4..6a341b1 100644
@@ -931,7 +931,7 @@ rt_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         cpu_raise_softirq(vc->processor, SCHEDULE_SOFTIRQ);
     else if ( __vcpu_on_q(svc) )
         __q_remove(svc);
-    else if ( test_bit(__RTDS_delayed_runq_add, &svc->flags) )
+    else if ( svc->flags & RTDS_delayed_runq_add )
         clear_bit(__RTDS_delayed_runq_add, &svc->flags);
 }
 
@@ -1064,7 +1064,7 @@ rt_vcpu_wake(const struct scheduler *ops, struct vcpu *vc)
      * the Runqueue/DepletedQ. Instead, we set a flag so that it will be
      * put on the Runqueue/DepletedQ after the context has been saved.
      */
-    if ( unlikely(test_bit(__RTDS_scheduled, &svc->flags)) )
+    if ( unlikely(svc->flags & RTDS_scheduled) )
     {
         set_bit(__RTDS_delayed_runq_add, &svc->flags);
         return;
Acked-by on v1 2/5
#6 lore raw

Re: [PATCH 2/5] xen: use masking operation instead of test_bit for CSFLAG bits

In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

Quoted 81 lines
---
 xen/common/sched_credit2.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
xen/common/sched_credit2.c +8 -8
index bf1fe6f..912e1a2 100644
@@ -418,7 +418,7 @@ __runq_insert(struct list_head *runq, struct csched2_vcpu *svc)
     /* Idle vcpus not allowed on the runqueue anymore */
     BUG_ON(is_idle_vcpu(svc->vcpu));
     BUG_ON(svc->vcpu->is_running);
-    BUG_ON(test_bit(__CSFLAG_scheduled, &svc->flags));
+    BUG_ON(svc->flags & CSFLAG_scheduled);
 
     list_for_each( iter, runq )
     {
@@ -844,7 +844,7 @@ static void
 __runq_deassign(struct csched2_vcpu *svc)
 {
     BUG_ON(__vcpu_on_runq(svc));
-    BUG_ON(test_bit(__CSFLAG_scheduled, &svc->flags));
+    BUG_ON(svc->flags & CSFLAG_scheduled);
 
     list_del_init(&svc->rqd_elem);
     update_max_weight(svc->rqd, 0, svc->weight);
@@ -952,7 +952,7 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         __runq_remove(svc);
     }
-    else if ( test_bit(__CSFLAG_delayed_runq_add, &svc->flags) )
+    else if ( svc->flags & CSFLAG_delayed_runq_add )
         clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);
 }
 
@@ -988,7 +988,7 @@ csched2_vcpu_wake(const struct scheduler *ops, struct vcpu *vc)
     /* If the context hasn't been saved for this vcpu yet, we can't put it on
      * another runqueue.  Instead, we set a flag so that it will be put on the runqueue
      * after the context has been saved. */
-    if ( unlikely (test_bit(__CSFLAG_scheduled, &svc->flags) ) )
+    if ( unlikely(svc->flags & CSFLAG_scheduled) )
     {
         set_bit(__CSFLAG_delayed_runq_add, &svc->flags);
         goto out;
@@ -1204,7 +1204,7 @@ static void migrate(const struct scheduler *ops,
                     struct csched2_runqueue_data *trqd, 
                     s_time_t now)
 {
-    if ( test_bit(__CSFLAG_scheduled, &svc->flags) )
+    if ( svc->flags & CSFLAG_scheduled )
     {
         d2printk("%pv %d-%d a\n", svc->vcpu, svc->rqd->id, trqd->id);
         /* It's running; mark it to migrate. */
@@ -1348,7 +1348,7 @@ retry:
         __update_svc_load(ops, push_svc, 0, now);
 
         /* Skip this one if it's already been flagged to migrate */
-        if ( test_bit(__CSFLAG_runq_migrate_request, &push_svc->flags) )
+        if ( push_svc->flags & CSFLAG_runq_migrate_request )
             continue;
 
         list_for_each( pull_iter, &st.orqd->svc )
@@ -1361,7 +1361,7 @@ retry:
             }
 
             /* Skip this one if it's already been flagged to migrate */
-            if ( test_bit(__CSFLAG_runq_migrate_request, &pull_svc->flags) )
+            if ( pull_svc->flags & CSFLAG_runq_migrate_request )
                 continue;
 
             consider(&st, push_svc, pull_svc);
@@ -1378,7 +1378,7 @@ retry:
         struct csched2_vcpu * pull_svc = list_entry(pull_iter, struct csched2_vcpu, rqd_elem);
 
         /* Skip this one if it's already been flagged to migrate */
-        if ( test_bit(__CSFLAG_runq_migrate_request, &pull_svc->flags) )
+        if ( pull_svc->flags & CSFLAG_runq_migrate_request )
             continue;
 
         /* Consider pull only */
#10 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to George Dunlap
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:18, <[email protected]> wrote:
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

First of all - what is an atomic read from CPU perspective other than just a read? Since we talk about individual bits here, we don't care about the granularity the compiler may convert the read to, and even if the compiler chose to do multiple reads the result would still be correct, as only one of those reads can possibly have read the bit in question.

And then, the old mechanism was in no way "atomic", all it added was a kind of compiler barrier (due to the cast to volatile). Yet in none of the cases changed I was able to spot a need for such a barrier.

Jan

#14 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to George Dunlap
Quoted 10 lines · George Dunlap
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

If we do the write without lock doing an atomic read is required only if there is some other kind of serialization between both involved cpus, as otherwise things would go wrong if the write is happening just after the (non-atomic) read.

Same applies to reads without lock: the write could happen just afterwards, so such coding can serve only as an optimization, not as a guarantee for correctness. TBH: I think there are some places where testing of a flag could just be omitted, as the value will nearly always be the one expected just from a local point of view.
Example: vcpu_force_reschedule().

What must be ruled out is some compiler optimization leading to an omitted read. I hope I haven't introduced such a case.

Juergen

Quoted 191 lines

For example, xen/common/schedule.c:vcpu_unblock(), vcpu_block(), do_poll; xen/common/event_channel.c:defaultxen_notification_fn(), and many more?

-George

Quoted 182 lines
---
  xen/arch/x86/domctl.c  |  2 +-
  xen/arch/x86/hvm/hvm.c |  4 ++--
  xen/arch/x86/hvm/vpt.c |  2 +-
  xen/common/domain.c    |  4 ++--
  xen/common/domctl.c    |  8 ++++----
  xen/common/schedule.c  | 16 ++++++++--------
  6 files changed, 18 insertions(+), 18 deletions(-)
xen/arch/x86/domctl.c +1 -1
index 6172c0d..f8a559c 100644
@@ -1213,7 +1213,7 @@ void arch_get_info_guest(struct vcpu *v, vcpu_guest_context_u c)
      c(flags = v->arch.vgc_flags & ~(VGCF_i387_valid|VGCF_in_kernel));
      if ( v->fpu_initialised )
          c(flags |= VGCF_i387_valid);
-    if ( !test_bit(_VPF_down, &v->pause_flags) )
+    if ( !(v->pause_flags & VPF_down) )
          c(flags |= VGCF_online);
      if ( !compat )
      {
xen/arch/x86/hvm/hvm.c +2 -2
index 6afc344..3fa2280 100644
@@ -1728,7 +1728,7 @@ static int hvm_save_cpu_ctxt(struct domain *d, hvm_domain_context_t *h)
      {
          /* We don't need to save state for a vcpu that is down; the restore
           * code will leave it down if there is nothing saved. */
-        if ( test_bit(_VPF_down, &v->pause_flags) )
+        if ( v->pause_flags & VPF_down )
              continue;
 
          /* Architecture-specific vmcs/vmcb bits */
@@ -2512,7 +2512,7 @@ void hvm_vcpu_down(struct vcpu *v)
      /* Any other VCPUs online? ... */
      domain_lock(d);
      for_each_vcpu ( d, v )
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
              online_count++;
      domain_unlock(d);
 
xen/arch/x86/hvm/vpt.c +1 -1
index 0c8b22e..4fa6566 100644
@@ -178,7 +178,7 @@ void pt_save_timer(struct vcpu *v)
      struct list_head *head = &v->arch.hvm_vcpu.tm_list;
      struct periodic_time *pt;
 
-    if ( test_bit(_VPF_blocked, &v->pause_flags) )
+    if ( v->pause_flags & VPF_blocked )
          return;
 
      spin_lock(&v->arch.hvm_vcpu.tm_lock);
xen/common/domain.c +2 -2
index cda60a9..7c362eb 100644
@@ -1135,7 +1135,7 @@ int map_vcpu_info(struct vcpu *v, unsigned long gfn, unsigned offset)
          return -EINVAL;
 
      /* Run this command on yourself or on other offline VCPUS. */
-    if ( (v != current) && !test_bit(_VPF_down, &v->pause_flags) )
+    if ( (v != current) && !(v->pause_flags & VPF_down) )
          return -EINVAL;
 
      page = get_page_from_gfn(d, gfn, NULL, P2M_ALLOC);
@@ -1263,7 +1263,7 @@ long do_vcpu_op(int cmd, unsigned int vcpuid, XEN_GUEST_HANDLE_PARAM(void) arg)
          break;
 
      case VCPUOP_is_up:
-        rc = !test_bit(_VPF_down, &v->pause_flags);
+        rc = !(v->pause_flags & VPF_down);
          break;
 
      case VCPUOP_get_runstate_info:
xen/common/domctl.c +4 -4
index 08de32d..46b967e 100644
@@ -170,7 +170,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)
          vcpu_runstate_get(v, &runstate);
          cpu_time += runstate.time[RUNSTATE_running];
          info->max_vcpu_id = v->vcpu_id;
-        if ( !test_bit(_VPF_down, &v->pause_flags) )
+        if ( !(v->pause_flags & VPF_down) )
          {
              if ( !(v->pause_flags & VPF_blocked) )
                  flags &= ~XEN_DOMINF_blocked;
@@ -231,7 +231,7 @@ static unsigned int default_vcpu0_location(cpumask_t *online)
          rcu_read_lock(&domlist_read_lock);
          for_each_domain ( d )
              for_each_vcpu ( d, v )
-                if ( !test_bit(_VPF_down, &v->pause_flags)
+                if ( !(v->pause_flags & VPF_down)
                       && ((cpu = v->processor) < nr_cpus) )
                      cnt[cpu]++;
          rcu_read_unlock(&domlist_read_lock);
@@ -944,8 +944,8 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 
          vcpu_runstate_get(v, &runstate);
 
-        op->u.getvcpuinfo.online   = !test_bit(_VPF_down, &v->pause_flags);
-        op->u.getvcpuinfo.blocked  = test_bit(_VPF_blocked, &v->pause_flags);
+        op->u.getvcpuinfo.online   = !(v->pause_flags & VPF_down);
+        op->u.getvcpuinfo.blocked  = !!(v->pause_flags & VPF_blocked);
          op->u.getvcpuinfo.running  = v->is_running;
          op->u.getvcpuinfo.cpu_time = runstate.time[RUNSTATE_running];
          op->u.getvcpuinfo.cpu      = v->processor;
xen/common/schedule.c +8 -8
index 5ffa1a1..c5f640f 100644
@@ -119,7 +119,7 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
 
      if ( unlikely(v->is_urgent) )
      {
-        if ( !test_bit(_VPF_blocked, &v->pause_flags) ||
+        if ( !(v->pause_flags & VPF_blocked) ||
               !test_bit(v->vcpu_id, v->domain->poll_mask) )
          {
              v->is_urgent = 0;
@@ -128,8 +128,8 @@ static inline void vcpu_urgent_count_update(struct vcpu *v)
      }
      else
      {
-        if ( unlikely(test_bit(_VPF_blocked, &v->pause_flags) &&
-                      test_bit(v->vcpu_id, v->domain->poll_mask)) )
+        if ( unlikely(v->pause_flags & VPF_blocked) &&
+             unlikely(test_bit(v->vcpu_id, v->domain->poll_mask)) )
          {
              v->is_urgent = 1;
              atomic_inc(&per_cpu(schedule_data,v->processor).urgent_count);
@@ -418,7 +418,7 @@ void vcpu_wake(struct vcpu *v)
              vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
          SCHED_OP(VCPU2OP(v), wake, v);
      }
-    else if ( !test_bit(_VPF_blocked, &v->pause_flags) )
+    else if ( !(v->pause_flags & VPF_blocked) )
      {
          if ( v->runstate.state == RUNSTATE_blocked )
              vcpu_runstate_change(v, RUNSTATE_offline, NOW());
@@ -595,7 +595,7 @@ void vcpu_force_reschedule(struct vcpu *v)
          set_bit(_VPF_migrating, &v->pause_flags);
      vcpu_schedule_unlock_irq(lock, v);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
      {
          vcpu_sleep_nosync(v);
          vcpu_migrate(v);
@@ -763,7 +763,7 @@ static int vcpu_set_affinity(
 
      domain_update_node_affinity(v->domain);
 
-    if ( test_bit(_VPF_migrating, &v->pause_flags) )
+    if ( v->pause_flags & VPF_migrating )
      {
          vcpu_sleep_nosync(v);
          vcpu_migrate(v);
@@ -1285,7 +1285,7 @@ static void schedule(void)
 
      vcpu_runstate_change(
          prev,
-        (test_bit(_VPF_blocked, &prev->pause_flags) ? RUNSTATE_blocked :
+        ((prev->pause_flags & VPF_blocked) ? RUNSTATE_blocked :
           (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
          now);
      prev->last_run_time = now;
@@ -1327,7 +1327,7 @@ void context_saved(struct vcpu *prev)
 
      SCHED_OP(VCPU2OP(prev), context_saved, prev);
 
-    if ( unlikely(test_bit(_VPF_migrating, &prev->pause_flags)) )
+    if ( unlikely(prev->pause_flags & VPF_migrating) )
          vcpu_migrate(prev);
  }
 
#16 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to George Dunlap
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:24, <[email protected]> wrote:
Quoted 4 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

Quoted 7 lines
Quoted 1 line

On a side note, a handful of functions seem to access the vcpu structure with the *domain* lock held, rather than the *scheduler* lock; for example:

xen/arch/x86/hvm/vlapic.c:vlapic_accept_irq()
xen/common/domain.c:vcpu_reset()

Why would the scheduler lock be needed there? In both cases it would
- at a first glance - seem as if the domain lock isn't needed there.

Quoted 2 lines

Is this a bug (perhaps left over from when we used the domain lock for vcpus rather than the scheduler lock)?

Not completely impossible there is too little locking now, but generally I don't think all vcpu structure accesses need to happen under one single lock.

Jan

#11 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

In reply to Jan Beulich
Quoted 19 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:18, <[email protected]> wrote:
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

First of all - what is an atomic read from CPU perspective other than just a read? Since we talk about individual bits here, we don't care about the granularity the compiler may convert the read to, and even if the compiler chose to do multiple reads the result would still be correct, as only one of those reads can possibly have read the bit in question.

Quoted 3 lines

And then, the old mechanism was in no way "atomic", all it added was a kind of compiler barrier (due to the cast to volatile). Yet in none of the cases changed I was able to spot a need for such a barrier.

OK, so the key thing about test_bit isn't that it's atomic, so much that it's an implicit memory barrier. So as long as you're not doing a lockless careful-ordering sort of thing, then a simple memory read should be fine. Is that correct?

In that case, it's likely that the patch is correct (though I'll take a closer look just to be sure).

-George

#12 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:45, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:18, <[email protected]> wrote:
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

First of all - what is an atomic read from CPU perspective other than just a read? Since we talk about individual bits here, we don't care about the granularity the compiler may convert the read to, and even if the compiler chose to do multiple reads the result would still be correct, as only one of those reads can possibly have read the bit in question.

Quoted 8 lines
Quoted 3 lines

And then, the old mechanism was in no way "atomic", all it added was a kind of compiler barrier (due to the cast to volatile). Yet in none of the cases changed I was able to spot a need for such a barrier.

OK, so the key thing about test_bit isn't that it's atomic, so much that it's an implicit memory barrier. So as long as you're not doing a lockless careful-ordering sort of thing, then a simple memory read should be fine. Is that correct?

Yes.

Quoted 2 lines

In that case, it's likely that the patch is correct (though I'll take a closer look just to be sure).

Thanks.

Jan

Reviewed-by
#13 lore raw

Re: [PATCH 4/5] xen: use masking operation instead of test_bit for VPF bits

Quoted 30 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:45, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 05.10.15 at 15:18, <[email protected]> wrote:
Quoted 5 lines · Juergen Gross

Use a bit mask for testing of a set bit instead of test_bit in case no atomic operation is needed, as this will lead to smaller and more effective code.

I'm a bit confused here -- exactly when is an atomic operation needed or not needed? Isn't it the case that we always need to do an atomic read if we ever do an atomic write without a lock held?

First of all - what is an atomic read from CPU perspective other than just a read? Since we talk about individual bits here, we don't care about the granularity the compiler may convert the read to, and even if the compiler chose to do multiple reads the result would still be correct, as only one of those reads can possibly have read the bit in question.

Quoted 8 lines
Quoted 3 lines

And then, the old mechanism was in no way "atomic", all it added was a kind of compiler barrier (due to the cast to volatile). Yet in none of the cases changed I was able to spot a need for such a barrier.

OK, so the key thing about test_bit isn't that it's atomic, so much that it's an implicit memory barrier. So as long as you're not doing a lockless careful-ordering sort of thing, then a simple memory read should be fine. Is that correct?

Quoted 6 lines

Yes.

Quoted 2 lines

In that case, it's likely that the patch is correct (though I'll take a closer look just to be sure).

Thanks.

OK, I've looked through again and don't see anything that looks racy: