Commit c6bc1422 authored by David Hildenbrand's avatar David Hildenbrand Committed by Michael S. Tsirkin
Browse files

virtio-mem: simplify high-level unplug handling in Big Block Mode



Let's simplify high-level big block selection when unplugging in
Big Block Mode.

Combine handling of offline and online blocks. We can get rid of
virtio_mem_bbm_bb_is_offline() and simply use
virtio_mem_bbm_offline_remove_and_unplug_bb(), as that already tolerates
offline parts.

We can race with concurrent onlining/offlining either way, so we don;t
have to be super correct by failing if an offline big block we'd like to
unplug just got (partially) onlined.

Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/r/20210602185720.31821-7-david@redhat.com


Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent c740bb97
Loading
Loading
Loading
Loading
+24 −72
Original line number Diff line number Diff line
@@ -702,18 +702,6 @@ static int virtio_mem_sbm_remove_mb(struct virtio_mem *vm, unsigned long mb_id)
	return virtio_mem_remove_memory(vm, addr, size);
}

/*
 * See virtio_mem_remove_memory(): Try to remove all Linux memory blocks covered
 * by the big block.
 */
static int virtio_mem_bbm_remove_bb(struct virtio_mem *vm, unsigned long bb_id)
{
	const uint64_t addr = virtio_mem_bb_id_to_phys(vm, bb_id);
	const uint64_t size = vm->bbm.bb_size;

	return virtio_mem_remove_memory(vm, addr, size);
}

/*
 * Try offlining and removing memory from Linux.
 *
@@ -2114,35 +2102,6 @@ static int virtio_mem_bbm_offline_remove_and_unplug_bb(struct virtio_mem *vm,
	return rc;
}

/*
 * Try to remove a big block from Linux and unplug it. Will fail with
 * -EBUSY if some memory is online.
 *
 * Will modify the state of the memory block.
 */
static int virtio_mem_bbm_remove_and_unplug_bb(struct virtio_mem *vm,
					       unsigned long bb_id)
{
	int rc;

	if (WARN_ON_ONCE(virtio_mem_bbm_get_bb_state(vm, bb_id) !=
			 VIRTIO_MEM_BBM_BB_ADDED))
		return -EINVAL;

	rc = virtio_mem_bbm_remove_bb(vm, bb_id);
	if (rc)
		return -EBUSY;

	rc = virtio_mem_bbm_unplug_bb(vm, bb_id);
	if (rc)
		virtio_mem_bbm_set_bb_state(vm, bb_id,
					    VIRTIO_MEM_BBM_BB_PLUGGED);
	else
		virtio_mem_bbm_set_bb_state(vm, bb_id,
					    VIRTIO_MEM_BBM_BB_UNUSED);
	return rc;
}

/*
 * Test if a big block is completely offline.
 */
@@ -2166,21 +2125,26 @@ static int virtio_mem_bbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
{
	uint64_t nb_bb = diff / vm->bbm.bb_size;
	uint64_t bb_id;
	int rc;
	int rc, i;

	if (!nb_bb)
		return 0;

	/* Try to unplug completely offline big blocks first. */
	/*
	 * Try to unplug big blocks. Similar to SBM, start with offline
	 * big blocks.
	 */
	for (i = 0; i < 2; i++) {
		virtio_mem_bbm_for_each_bb_rev(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED) {
			cond_resched();

			/*
		 * As we're holding no locks, this check is racy as memory
		 * can get onlined in the meantime - but we'll fail gracefully.
			 * As we're holding no locks, these checks are racy,
			 * but we don't care.
			 */
		if (!virtio_mem_bbm_bb_is_offline(vm, bb_id))
			if (i == 0 && !virtio_mem_bbm_bb_is_offline(vm, bb_id))
				continue;
		rc = virtio_mem_bbm_remove_and_unplug_bb(vm, bb_id);
			rc = virtio_mem_bbm_offline_remove_and_unplug_bb(vm, bb_id);
			if (rc == -EBUSY)
				continue;
			if (!rc)
@@ -2188,20 +2152,8 @@ static int virtio_mem_bbm_unplug_request(struct virtio_mem *vm, uint64_t diff)
			if (rc || !nb_bb)
				return rc;
		}

	if (!unplug_online)
		if (i == 0 && !unplug_online)
			return 0;

	/* Try to unplug any big blocks. */
	virtio_mem_bbm_for_each_bb_rev(vm, bb_id, VIRTIO_MEM_BBM_BB_ADDED) {
		cond_resched();
		rc = virtio_mem_bbm_offline_remove_and_unplug_bb(vm, bb_id);
		if (rc == -EBUSY)
			continue;
		if (!rc)
			nb_bb--;
		if (rc || !nb_bb)
			return rc;
	}

	return nb_bb ? -EBUSY : 0;