qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org,
	"Daniel P . Berrange" <berrange@redhat.com>,
	Leonardo Bras Soares Passos <lsoaresp@redhat.com>,
	Manish Mishra <manish.mishra@nutanix.com>,
	Juan Quintela <quintela@redhat.com>
Subject: Re: [PATCH RFC 01/13] migration: Use non-atomic ops for clear log bitmap
Date: Thu, 15 Sep 2022 19:49:57 +0100	[thread overview]
Message-ID: <YyNz1Z2etCS/NtlT@work-vm> (raw)
In-Reply-To: <20220829165659.96046-2-peterx@redhat.com>

* Peter Xu (peterx@redhat.com) wrote:
> Since we already have bitmap_mutex to protect either the dirty bitmap or
> the clear log bitmap, we don't need atomic operations to set/clear/test on
> the clear log bitmap.  Switching all ops from atomic to non-atomic
> versions, meanwhile touch up the comments to show which lock is in charge.
> 
> Introduced non-atomic version of bitmap_test_and_clear_atomic(), mostly the
> same as the atomic version but simplified a few places, e.g. dropped the
> "old_bits" variable, and also the explicit memory barriers.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>

Can you update the comment in ramblock.h above clear_bmap to say it's
always updated under that lock.

Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>

> ---
>  include/exec/ram_addr.h | 11 +++++-----
>  include/qemu/bitmap.h   |  1 +
>  util/bitmap.c           | 45 +++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 52 insertions(+), 5 deletions(-)
> 
> diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
> index f3e0c78161..5092a2e0ff 100644
> --- a/include/exec/ram_addr.h
> +++ b/include/exec/ram_addr.h
> @@ -42,7 +42,8 @@ static inline long clear_bmap_size(uint64_t pages, uint8_t shift)
>  }
>  
>  /**
> - * clear_bmap_set: set clear bitmap for the page range
> + * clear_bmap_set: set clear bitmap for the page range.  Must be with
> + * bitmap_mutex held.
>   *
>   * @rb: the ramblock to operate on
>   * @start: the start page number
> @@ -55,12 +56,12 @@ static inline void clear_bmap_set(RAMBlock *rb, uint64_t start,
>  {
>      uint8_t shift = rb->clear_bmap_shift;
>  
> -    bitmap_set_atomic(rb->clear_bmap, start >> shift,
> -                      clear_bmap_size(npages, shift));
> +    bitmap_set(rb->clear_bmap, start >> shift, clear_bmap_size(npages, shift));
>  }
>  
>  /**
> - * clear_bmap_test_and_clear: test clear bitmap for the page, clear if set
> + * clear_bmap_test_and_clear: test clear bitmap for the page, clear if set.
> + * Must be with bitmap_mutex held.
>   *
>   * @rb: the ramblock to operate on
>   * @page: the page number to check
> @@ -71,7 +72,7 @@ static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page)
>  {
>      uint8_t shift = rb->clear_bmap_shift;
>  
> -    return bitmap_test_and_clear_atomic(rb->clear_bmap, page >> shift, 1);
> +    return bitmap_test_and_clear(rb->clear_bmap, page >> shift, 1);
>  }
>  
>  static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
> diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
> index 82a1d2f41f..3ccb00865f 100644
> --- a/include/qemu/bitmap.h
> +++ b/include/qemu/bitmap.h
> @@ -253,6 +253,7 @@ void bitmap_set(unsigned long *map, long i, long len);
>  void bitmap_set_atomic(unsigned long *map, long i, long len);
>  void bitmap_clear(unsigned long *map, long start, long nr);
>  bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr);
> +bool bitmap_test_and_clear(unsigned long *map, long start, long nr);
>  void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src,
>                                    long nr);
>  unsigned long bitmap_find_next_zero_area(unsigned long *map,
> diff --git a/util/bitmap.c b/util/bitmap.c
> index f81d8057a7..8d12e90a5a 100644
> --- a/util/bitmap.c
> +++ b/util/bitmap.c
> @@ -240,6 +240,51 @@ void bitmap_clear(unsigned long *map, long start, long nr)
>      }
>  }
>  
> +bool bitmap_test_and_clear(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);
> +    bool dirty = false;
> +
> +    assert(start >= 0 && nr >= 0);
> +
> +    /* First word */
> +    if (nr - bits_to_clear > 0) {
> +        if ((*p) & mask_to_clear) {
> +            dirty = true;
> +        }
> +        *p &= ~mask_to_clear;
> +        nr -= bits_to_clear;
> +        bits_to_clear = BITS_PER_LONG;
> +        p++;
> +    }
> +
> +    /* Full words */
> +    if (bits_to_clear == BITS_PER_LONG) {
> +        while (nr >= BITS_PER_LONG) {
> +            if (*p) {
> +                dirty = true;
> +                *p = 0;
> +            }
> +            nr -= BITS_PER_LONG;
> +            p++;
> +        }
> +    }
> +
> +    /* Last word */
> +    if (nr) {
> +        mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
> +        if ((*p) & mask_to_clear) {
> +            dirty = true;
> +        }
> +        *p &= ~mask_to_clear;
> +    }
> +
> +    return dirty;
> +}
> +
>  bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
>  {
>      unsigned long *p = map + BIT_WORD(start);
> -- 
> 2.32.0
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



  reply	other threads:[~2022-09-15 18:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-29 16:56 [PATCH RFC 00/13] migration: Postcopy Preempt-Full Peter Xu
2022-08-29 16:56 ` [PATCH RFC 01/13] migration: Use non-atomic ops for clear log bitmap Peter Xu
2022-09-15 18:49   ` Dr. David Alan Gilbert [this message]
2022-09-15 19:44     ` Peter Xu
2022-08-29 16:56 ` [PATCH RFC 02/13] migration: Add postcopy_preempt_active() Peter Xu
2022-09-15 18:50   ` Dr. David Alan Gilbert
2022-08-29 16:56 ` [PATCH RFC 03/13] migration: Yield bitmap_mutex properly when sending/sleeping Peter Xu
2022-08-29 16:56 ` [PATCH RFC 04/13] migration: Cleanup xbzrle zero page cache update logic Peter Xu
2022-08-29 16:56 ` [PATCH RFC 05/13] migration: Disallow postcopy preempt to be used with compress Peter Xu
2022-08-29 16:56 ` [PATCH RFC 06/13] migration: Trivial cleanup save_page_header() on same block check Peter Xu
2022-08-29 16:56 ` [PATCH RFC 07/13] migration: Remove RAMState.f references in compression code Peter Xu
2022-08-29 16:56 ` [PATCH RFC 08/13] migration: Teach PSS about host page Peter Xu
2022-08-29 16:56 ` [PATCH RFC 09/13] migration: Introduce pss_channel Peter Xu
2022-08-29 16:56 ` [PATCH RFC 10/13] migration: Add pss_init() Peter Xu
2022-08-29 16:56 ` [PATCH RFC 11/13] migration: Make PageSearchStatus part of RAMState Peter Xu
2022-08-29 16:56 ` [PATCH RFC 12/13] migration: Move last_sent_block into PageSearchStatus Peter Xu
2022-08-29 16:56 ` [PATCH RFC 13/13] migration: Send requested page directly in rp-return thread Peter Xu
2022-08-29 17:39 ` [PATCH RFC 00/13] migration: Postcopy Preempt-Full Peter Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YyNz1Z2etCS/NtlT@work-vm \
    --to=dgilbert@redhat.com \
    --cc=berrange@redhat.com \
    --cc=lsoaresp@redhat.com \
    --cc=manish.mishra@nutanix.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).