[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v1 08/21] hw/i386/kvm/xen_evtchn: Fix off-by-one in do_remove_pci_vector()



On Wed, 2026-11-25 at 13:43 +0000, David Woodhouse wrote:
> On Wed, 2026-11-25 at 13:31 +0000, Paul Durrant wrote:
> > On 25/11/2026 05:03, David Woodhouse wrote:
> > > On Wed, 2026-11-25 at 11:16 +0000, Paul Durrant wrote:
> > > > 
> > > > I guess I'm missing a subtlety here, but the test only fires if pirq is
> > > > a multiple of 64... so why is advancing to the next word by adding 64
> > > > not correct?
> > > 
> > > You're missing the same subtlety that I missed when I wrote it in the
> > > first place: the loop *itself* does pirq++ every time, so when we do
> > > 'pirq += 64; continue;' we end up adding *65*.
> > > 
> > >      for (pirq = 0; pirq < s->nr_pirqs; pirq++) {
> > >          /*
> > >           * We could be cleverer here, but it isn't really a fast path, 
> > > and
> > >           * this trivial optimisation is enough to let us skip the big gap
> > >           * in the middle a bit quicker (in terms of both loop iterations,
> > >           * and cache lines).
> > >           */
> > >          if (!(pirq & 63) && !(pirq_inuse_word(s, pirq))) {
> > >              pirq += 63;
> > >              continue;
> > >          }
> > 
> > Gah! Recoding as a while loop where the increment is either 1 or 64
> > would make this much less fragile.
> 
> Or refactor it completely to use ffs? Do we have a for_each_set_bit()
> helper? Do we want one?

I iterated on this a few times, and even as a while() loop you still
end up skipping to the last *zero* and then adding one more, so it
might as well stay as for() loop and do the skip *every* time not just
for full empty words:

    for (pirq = 0; pirq < s->nr_pirqs; pirq++) {
        uint64_t bits;

        if (pirq_inuse(s, pirq) &&
            !(except_pirq && pirq == except_pirq) &&
            s->pirq[pirq].dev == dev &&
            (vector == -1 || s->pirq[pirq].vector == vector)) {
            /*
             * It could theoretically be bound to a port already, but that
             * is OK.
             */
            s->pirq[pirq].dev = dev;
            s->pirq[pirq].gsi = IRQ_UNBOUND;
            s->pirq[pirq].is_msix = false;
            s->pirq[pirq].vector = 0;
            s->pirq[pirq].is_masked = false;
            s->pirq[pirq].is_translated = false;
        }

        /*
         * Skip to the last clear bit before the next in-use PIRQ, or to
         * the end of the word if the rest of it is clear, leaving the
         * loop increment to do the final step.
         */
        bits = (pirq_inuse_word(s, pirq) >> (pirq & 63)) >> 1;
        if (bits) {
            pirq += ctz64(bits);
        } else {
            pirq |= 63;
        }
    }

And actually, written like this the *benefit* of the skip is largely
removed, as the subtle difference here is that we now check
pirq_inuse() before dereferencing s->pirq[pirq] so it isn't trashing
the cache anyway.

But on the whole, I think the single-character change I originally
posted is the simpler option in the short term. 

Attachment: smime.p7s
Description: S/MIME cryptographic signature


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.