From: Shao Mingyin brd_do_discard() calls __xa_erase() for every page index in the discard range while holding the XArray lock. Large discards on mostly unwritten RAM disks therefore spend most of their time looking up indices that have no backing pages. Walk the populated entries with xas_for_each() and erase them with xas_store(). Keep the existing range rounding, capacity limit, page reference handling and allocated-page accounting. Signed-off-by: Shao Mingyin --- Tested in a QEMU/KVM guest with 4 KiB pages: BLKDISCARD over a 16 GiB range containing 64 pages took a median of 61.4 ms before and 9.16 us after; a fully populated 128 MiB range went from 2.28 ms to 1.80 ms. Each median uses 63 calls measured over three boots, with data population and readback outside the timed interval. drivers/block/brd.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/drivers/block/brd.c b/drivers/block/brd.c index 00cc8122068f..11fa833a3f1b 100644 --- a/drivers/block/brd.c +++ b/drivers/block/brd.c @@ -182,21 +182,22 @@ static void brd_do_discard(struct brd_device *brd, sector_t sector, u32 size) sector_t aligned_sector = round_up(sector, PAGE_SECTORS); sector_t aligned_end = round_down( sector + (size >> SECTOR_SHIFT), PAGE_SECTORS); + XA_STATE(xas, &brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT); + unsigned long last_index; struct page *page; + aligned_end = min_t(sector_t, aligned_end, rd_size * 2); if (aligned_end <= aligned_sector) return; - xa_lock(&brd->brd_pages); - while (aligned_sector < aligned_end && aligned_sector < rd_size * 2) { - page = __xa_erase(&brd->brd_pages, aligned_sector >> PAGE_SECTORS_SHIFT); - if (page) { - put_page(page); - brd->brd_nr_pages--; - } - aligned_sector += PAGE_SECTORS; + last_index = (aligned_end - 1) >> PAGE_SECTORS_SHIFT; + xas_lock(&xas); + xas_for_each(&xas, page, last_index) { + xas_store(&xas, NULL); + put_page(page); + brd->brd_nr_pages--; } - xa_unlock(&brd->brd_pages); + xas_unlock(&xas); } static void brd_submit_bio(struct bio *bio) base-commit: d09b6d9d1fcce0770c1824c8b2b8ab153f2672d2 -- 2.27.0