Skip to content

Commit 562e08c

Browse files
davidhildenbrandmstsirkin
authored andcommitted
virtio-mem: Unplug subblocks right-to-left
We unplug blocks right-to-left, let's also unplug subblocks within a block right-to-left. Cc: "Michael S. Tsirkin" <[email protected]> Cc: Pankaj Gupta <[email protected]> Signed-off-by: David Hildenbrand <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 3c42e19 commit 562e08c

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

drivers/virtio/virtio_mem.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,6 @@ static bool virtio_mem_mb_test_sb_unplugged(struct virtio_mem *vm,
353353
return find_next_bit(vm->sb_bitmap, bit + count, bit) >= bit + count;
354354
}
355355

356-
/*
357-
* Find the first plugged subblock. Returns vm->nb_sb_per_mb in case there is
358-
* none.
359-
*/
360-
static int virtio_mem_mb_first_plugged_sb(struct virtio_mem *vm,
361-
unsigned long mb_id)
362-
{
363-
const int bit = (mb_id - vm->first_mb_id) * vm->nb_sb_per_mb;
364-
365-
return find_next_bit(vm->sb_bitmap, bit + vm->nb_sb_per_mb, bit) - bit;
366-
}
367-
368356
/*
369357
* Find the first unplugged subblock. Returns vm->nb_sb_per_mb in case there is
370358
* none.
@@ -1016,21 +1004,27 @@ static int virtio_mem_mb_unplug_any_sb(struct virtio_mem *vm,
10161004
int sb_id, count;
10171005
int rc;
10181006

1007+
sb_id = vm->nb_sb_per_mb - 1;
10191008
while (*nb_sb) {
1020-
sb_id = virtio_mem_mb_first_plugged_sb(vm, mb_id);
1021-
if (sb_id >= vm->nb_sb_per_mb)
1009+
/* Find the next candidate subblock */
1010+
while (sb_id >= 0 &&
1011+
virtio_mem_mb_test_sb_unplugged(vm, mb_id, sb_id, 1))
1012+
sb_id--;
1013+
if (sb_id < 0)
10221014
break;
1015+
/* Try to unplug multiple subblocks at a time */
10231016
count = 1;
1024-
while (count < *nb_sb &&
1025-
sb_id + count < vm->nb_sb_per_mb &&
1026-
virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id + count,
1027-
1))
1017+
while (count < *nb_sb && sb_id > 0 &&
1018+
virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id - 1, 1)) {
10281019
count++;
1020+
sb_id--;
1021+
}
10291022

10301023
rc = virtio_mem_mb_unplug_sb(vm, mb_id, sb_id, count);
10311024
if (rc)
10321025
return rc;
10331026
*nb_sb -= count;
1027+
sb_id--;
10341028
}
10351029

10361030
return 0;
@@ -1337,12 +1331,12 @@ static int virtio_mem_mb_unplug_any_sb_online(struct virtio_mem *vm,
13371331
* we should sense via something like is_mem_section_removable()
13381332
* first if it makes sense to go ahead any try to allocate.
13391333
*/
1340-
for (sb_id = 0; sb_id < vm->nb_sb_per_mb && *nb_sb; sb_id++) {
1334+
for (sb_id = vm->nb_sb_per_mb - 1; sb_id >= 0 && *nb_sb; sb_id--) {
13411335
/* Find the next candidate subblock */
1342-
while (sb_id < vm->nb_sb_per_mb &&
1336+
while (sb_id >= 0 &&
13431337
!virtio_mem_mb_test_sb_plugged(vm, mb_id, sb_id, 1))
1344-
sb_id++;
1345-
if (sb_id >= vm->nb_sb_per_mb)
1338+
sb_id--;
1339+
if (sb_id < 0)
13461340
break;
13471341

13481342
start_pfn = PFN_DOWN(virtio_mem_mb_id_to_phys(mb_id) +

0 commit comments

Comments
 (0)