From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xu2As-0006q6-4W for qemu-devel@nongnu.org; Thu, 27 Nov 2014 11:44:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xu2Al-0003ji-Vu for qemu-devel@nongnu.org; Thu, 27 Nov 2014 11:44:10 -0500 Received: from mx1.redhat.com ([209.132.183.28]:38584) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xu2Al-0003ja-Pc for qemu-devel@nongnu.org; Thu, 27 Nov 2014 11:44:03 -0500 Message-ID: <547754CC.5070704@redhat.com> Date: Thu, 27 Nov 2014 17:43:56 +0100 From: Paolo Bonzini MIME-Version: 1.0 References: <1417091366-4469-1-git-send-email-stefanha@redhat.com> <1417091366-4469-3-git-send-email-stefanha@redhat.com> In-Reply-To: <1417091366-4469-3-git-send-email-stefanha@redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [RFC 2/6] bitmap: add atomic test and clear List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi , qemu-devel@nongnu.org Cc: Peter Maydell , rth@redhat.com, Juan Quintela On 27/11/2014 13:29, Stefan Hajnoczi wrote: > +bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr) > +{ > + unsigned long *p = map + BIT_WORD(start); > + const long size = start + nr; > + int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); > + unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); > + unsigned long dirty = 0; > + unsigned long old_bits; > + > + while (nr - bits_to_clear >= 0) { > + old_bits = atomic_fetch_and(p, ~mask_to_clear); > + dirty |= old_bits & mask_to_clear; > + nr -= bits_to_clear; > + bits_to_clear = BITS_PER_LONG; > + mask_to_clear = ~0UL; > + p++; > + } > + if (nr) { > + mask_to_clear &= BITMAP_LAST_WORD_MASK(size); > + old_bits = atomic_fetch_and(p, ~mask_to_clear); > + dirty |= old_bits & mask_to_clear; > + } > + > + return dirty; > +} Same here; you can use atomic_xchg, which is faster because on x86 atomic_fetch_and must do a compare-and-swap loop. Paolo