Skip to content

Commit f4cf803

Browse files
davidhildenbrandmstsirkin
authored andcommitted
virtio-mem: simplify high-level plug handling in Sub Block Mode
Let's simplify high-level memory block selection when plugging in Sub Block Mode. No need for two separate loops when selecting memory blocks for plugging memory. Avoid passing the "online" state by simply obtaining the state in virtio_mem_sbm_plug_any_sb(). Signed-off-by: David Hildenbrand <david@redhat.com> Link: https://lore.kernel.org/r/20210602185720.31821-4-david@redhat.com Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 49d4287 commit f4cf803

1 file changed

Lines changed: 17 additions & 28 deletions

File tree

drivers/virtio/virtio_mem.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,9 +1583,9 @@ static int virtio_mem_sbm_plug_and_add_mb(struct virtio_mem *vm,
15831583
* Note: Can fail after some subblocks were successfully plugged.
15841584
*/
15851585
static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm,
1586-
unsigned long mb_id, uint64_t *nb_sb,
1587-
bool online)
1586+
unsigned long mb_id, uint64_t *nb_sb)
15881587
{
1588+
const int old_state = virtio_mem_sbm_get_mb_state(vm, mb_id);
15891589
unsigned long pfn, nr_pages;
15901590
int sb_id, count;
15911591
int rc;
@@ -1607,7 +1607,7 @@ static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm,
16071607
if (rc)
16081608
return rc;
16091609
*nb_sb -= count;
1610-
if (!online)
1610+
if (old_state == VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL)
16111611
continue;
16121612

16131613
/* fake-online the pages if the memory block is online */
@@ -1617,46 +1617,35 @@ static int virtio_mem_sbm_plug_any_sb(struct virtio_mem *vm,
16171617
virtio_mem_fake_online(pfn, nr_pages);
16181618
}
16191619

1620-
if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb)) {
1621-
if (online)
1622-
virtio_mem_sbm_set_mb_state(vm, mb_id,
1623-
VIRTIO_MEM_SBM_MB_ONLINE);
1624-
else
1625-
virtio_mem_sbm_set_mb_state(vm, mb_id,
1626-
VIRTIO_MEM_SBM_MB_OFFLINE);
1627-
}
1620+
if (virtio_mem_sbm_test_sb_plugged(vm, mb_id, 0, vm->sbm.sbs_per_mb))
1621+
virtio_mem_sbm_set_mb_state(vm, mb_id, old_state - 1);
16281622

16291623
return 0;
16301624
}
16311625

16321626
static int virtio_mem_sbm_plug_request(struct virtio_mem *vm, uint64_t diff)
16331627
{
1628+
const int mb_states[] = {
1629+
VIRTIO_MEM_SBM_MB_ONLINE_PARTIAL,
1630+
VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL,
1631+
};
16341632
uint64_t nb_sb = diff / vm->sbm.sb_size;
16351633
unsigned long mb_id;
1636-
int rc;
1634+
int rc, i;
16371635

16381636
if (!nb_sb)
16391637
return 0;
16401638

16411639
/* Don't race with onlining/offlining */
16421640
mutex_lock(&vm->hotplug_mutex);
16431641

1644-
/* Try to plug subblocks of partially plugged online blocks. */
1645-
virtio_mem_sbm_for_each_mb(vm, mb_id,
1646-
VIRTIO_MEM_SBM_MB_ONLINE_PARTIAL) {
1647-
rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb, true);
1648-
if (rc || !nb_sb)
1649-
goto out_unlock;
1650-
cond_resched();
1651-
}
1652-
1653-
/* Try to plug subblocks of partially plugged offline blocks. */
1654-
virtio_mem_sbm_for_each_mb(vm, mb_id,
1655-
VIRTIO_MEM_SBM_MB_OFFLINE_PARTIAL) {
1656-
rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb, false);
1657-
if (rc || !nb_sb)
1658-
goto out_unlock;
1659-
cond_resched();
1642+
for (i = 0; i < ARRAY_SIZE(mb_states); i++) {
1643+
virtio_mem_sbm_for_each_mb(vm, mb_id, mb_states[i]) {
1644+
rc = virtio_mem_sbm_plug_any_sb(vm, mb_id, &nb_sb);
1645+
if (rc || !nb_sb)
1646+
goto out_unlock;
1647+
cond_resched();
1648+
}
16601649
}
16611650

16621651
/*

0 commit comments

Comments
 (0)