> The calculation is already complicated enough, let's limit it to one > location. > > Cc: "Michael S. Tsirkin" <mst@xxxxxxxxxx> > Cc: Jason Wang <jasowang@xxxxxxxxxx> > Cc: Pankaj Gupta <pankaj.gupta.linux@xxxxxxxxx> > Signed-off-by: David Hildenbrand <david@xxxxxxxxxx> > --- > drivers/virtio/virtio_mem.c | 20 +++++++++++++++----- > 1 file changed, 15 insertions(+), 5 deletions(-) > > diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c > index 2cc497ad8298..73ff6e9ba839 100644 > --- a/drivers/virtio/virtio_mem.c > +++ b/drivers/virtio/virtio_mem.c > @@ -327,6 +327,16 @@ static int virtio_mem_sbm_mb_states_prepare_next_mb(struct virtio_mem *vm) > _mb_id--) \ > if (virtio_mem_sbm_get_mb_state(_vm, _mb_id) == _state) > > +/* > + * Calculate the bit number in the sb_states bitmap for the given subblock > + * inside the given memory block. > + */ > +static int virtio_mem_sbm_sb_state_bit_nr(struct virtio_mem *vm, > + unsigned long mb_id, int sb_id) > +{ > + return (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; > +} > + > /* > * Mark all selected subblocks plugged. > * > @@ -336,7 +346,7 @@ static void virtio_mem_sbm_set_sb_plugged(struct virtio_mem *vm, > unsigned long mb_id, int sb_id, > int count) > { > - const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; > + const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); > > __bitmap_set(vm->sbm.sb_states, bit, count); > } > @@ -350,7 +360,7 @@ static void virtio_mem_sbm_set_sb_unplugged(struct virtio_mem *vm, > unsigned long mb_id, int sb_id, > int count) > { > - const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; > + const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); > > __bitmap_clear(vm->sbm.sb_states, bit, count); > } > @@ -362,7 +372,7 @@ static bool virtio_mem_sbm_test_sb_plugged(struct virtio_mem *vm, > unsigned long mb_id, int sb_id, > int count) > { > - const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; > + const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); > > if (count == 1) > return test_bit(bit, vm->sbm.sb_states); > @@ -379,7 +389,7 @@ static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm, > unsigned long mb_id, int sb_id, > int count) > { > - const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb + sb_id; > + const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, sb_id); > > /* TODO: Helper similar to bitmap_set() */ > return find_next_bit(vm->sbm.sb_states, bit + count, bit) >= > @@ -393,7 +403,7 @@ static bool virtio_mem_sbm_test_sb_unplugged(struct virtio_mem *vm, > static int virtio_mem_sbm_first_unplugged_sb(struct virtio_mem *vm, > unsigned long mb_id) > { > - const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb; > + const int bit = virtio_mem_sbm_sb_state_bit_nr(vm, mb_id, 0); > > return find_next_zero_bit(vm->sbm.sb_states, > bit + vm->nb_sb_per_mb, bit) - bit; Agree, there are alot of *b things, good to clean as much. Reviewed-by: Pankaj Gupta <pankaj.gupta.linux@xxxxxxxxx>